/* Plugin Name: Nationaal Archief Zoeker Description: Zoek en importeer rechtenvrije foto's uit het Nationaal Archief. Version: 1.0 Author: Barend */

<?php

/*

Plugin Name: Nationaal Archief Zoeker

Description: Zoek en importeer rechtenvrije foto's uit het Nationaal Archief.

Version: 1.0

Author: Barend

*/


add_action('admin_menu', function() {

    add_media_page('Zoeken NA', 'Zoeken NA', 'manage_options', 'zoeken_na', 'na_admin_page');

});


function na_admin_page() {

    add_thickbox();

    echo '<div class="wrap"><h1>Zoeken in Nationaal Archief</h1>';

    echo '<form method="post"><input type="text" name="zoekterm" placeholder="Naam..." required> ';

    echo '<input type="text" name="jaar" placeholder="Jaartal (optioneel)"> ';

    echo '<input type="submit" name="na_zoek" class="button button-primary" value="Zoeken"></form>';


    if (isset($_POST['na_zoek'])) {

        $zoekterm = sanitize_text_field($_POST['zoekterm']);

        $jaar = sanitize_text_field($_POST['jaar']);

        na_voer_zoekopdracht_uit($zoekterm, $jaar);

    }


    if (isset($_POST['na_importeer']) && !empty($_POST['selecteer'])) {

        na_importeer_geselecteerde($_POST['selecteer']);

    }


    echo '</div>';

}


function na_voer_zoekopdracht_uit($zoekterm, $jaar = '') {

    $query = urlencode($zoekterm . ' ' . $jaar);

    $url = "https://www.nationaalarchief.nl/onderzoeken/zoeken?activeTab=photos&rm=gallery&q=$query";


    $response = wp_remote_get($url);

    if (is_wp_error($response)) {

        echo '<p>Fout bij ophalen van gegevens.</p>';

        return;

    }


    $body = wp_remote_retrieve_body($response);


    // Simpele scraping van thumbnails (pas aan op basis van echte HTML)

    preg_match_all('/<img[^>]+src="([^"]+)"[^>]*alt="([^"]*)"/', $body, $matches, PREG_SET_ORDER);


    if (empty($matches)) {

        echo '<p>Geen resultaten gevonden.</p>';

        return;

    }


    echo '<form method="post"><div style="display:flex;flex-wrap:wrap;">';

    foreach (array_slice($matches, 0, 40) as $index => $match) {

        $thumb = esc_url($match[1]);

        $alt = esc_attr($match[2]);

        $full = str_replace('/thumb/', '/full/', $thumb); // aannames, pas aan indien nodig


        echo '<div style="margin:10px;text-align:center;">';

        echo '<input type="checkbox" name="selecteer[]" value="' . esc_attr($full) . '">';

        echo '<a href="' . $full . '?TB_iframe=true&width=600&height=550" class="thickbox">';

        echo '<img src="' . $thumb . '" style="width:120px;height:auto;"><br>';

        echo '</a>';

        echo '<small>' . $alt . '</small>';

        echo '</div>';

    }

    echo '</div><input type="submit" name="na_importeer" class="button button-secondary" value="Importeer geselecteerde"></form>';

}


function na_importeer_geselecteerde($urls) {

    require_once(ABSPATH . 'wp-admin/includes/image.php');

    require_once(ABSPATH . 'wp-admin/includes/file.php');

    require_once(ABSPATH . 'wp-admin/includes/media.php');


    $count = 0;

    foreach ($urls as $img_url) {

        $filename = basename($img_url);

        $upload_dir = wp_upload_dir();

        $filepath = $upload_dir['path'] . '/' . $filename;


        // Check op bestaande media

        $existing = get_posts([

            'post_type' => 'attachment',

            'meta_query' => [

                ['key' => '_wp_attached_file', 'value' => $filename, 'compare' => 'LIKE']

            ]

        ]);

        if ($existing) {

            wp_delete_attachment($existing[0]->ID, true);

        }


        $tmp = download_url($img_url);

        if (is_wp_error($tmp)) continue;


        $file_array = [

            'name' => $filename,

            'tmp_name' => $tmp

        ];


        $id = media_handle_sideload($file_array, 0);

        if (is_wp_error($id)) continue;


        // Metadata invullen

        $beschrijving = 'Nationaal Archief: <a href="' . esc_url($img_url) . '" target="_blank">origineel</a><br>';

        $beschrijving .= 'Collectie / Archief / Beschrijving / Datum / Persoonsnamen / Fotograaf / Auteursrechthebbende';


        update_post_meta($id, '_wp_attachment_image_alt', 'Nationaal Archief');

        wp_update_post([

            'ID' => $id,

            'post_content' => $beschrijving,

            'post_excerpt' => $beschrijving

        ]);


        $count++;

    }


    echo "<p>$count afbeeldingen geïmporteerd.</p>";

}

https://tourpools.nl/post-sitemap.xml 2025-09-03T09:32:38+00:00 https://tourpools.nl/page-sitemap.xml 2025-06-26T12:42:35+00:00 https://tourpools.nl/category-sitemap.xml 2025-09-03T09:32:38+00:00 https://tourpools.nl/post_tag-sitemap.xml 2025-09-03T09:32:38+00:00 https://tourpools.nl/et_tb_item_type-sitemap.xml 2024-06-28T07:13:27+00:00 https://tourpools.nl/author-sitemap.xml 2025-09-03T09:51:04+00:00