Last active
December 9, 2018 14:42
-
-
Save RaffaeleSgarro/7a82d6c08da628a4e272ddba161048a9 to your computer and use it in GitHub Desktop.
Plugin that store article ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Vimarte | |
* Plugin URI: https://www.vimarte.it | |
* Description: Vimarte customization to WP | |
* Author: Raffaele | |
* Version: 0.1 | |
* Author URI: https://www.pediatria.it | |
*/ | |
// Hook for adding <script> and <style> to HTML <head> | |
add_action('admin_enqueue_scripts', 'vimarte_enqueue_admin_scripts_and_styles'); | |
// Hook for adding admin menus | |
add_action('admin_menu', 'vimarte_add_admin_menu'); | |
// AJAX hook. Important: key => value must be wp_ajax_FUNC => FUNC, where FUNC is the PHP handler | |
add_action('wp_ajax_vimarte_download_media', 'vimarte_download_media'); | |
// action function for above hook | |
function vimarte_add_admin_menu() { | |
add_menu_page('Vimarte', 'Vimarte', 'manage_options', 'vimarte-top-level-handle', 'vimarte_catalogo_admin_page', '', 4.9); | |
} | |
function vimarte_catalogo_admin_page() { | |
if (!isset($_POST['json_url'])) { | |
?> | |
<h1>Catalogo</h1> | |
<form method="POST"> | |
<table> | |
<tr> | |
<td><label for="json_url">JSON URL</label></td> | |
<td><input type="text" name="json_url" id="json_url" value="http://93.148.228.10:8787/api/vimarte" style="width: 350px"/></td> | |
</tr> | |
<tr> | |
<td><label for="debug_stop_after_first_n">Debug: stop after</label></td> | |
<td><input type="text" name="debug_stop_after_first_n" id="debug_stop_after_first_n" value="10"/></td> | |
</tr> | |
<tr> | |
<td></td> | |
<td><input type="submit"/></td> | |
</tr> | |
</table> | |
</form> | |
<?php | |
} else { | |
// Array of objects made like this: | |
// { | |
// "idArticolo": "VI00018", | |
// "note": "Autentica su fotografia a colori redatta e firmata dall'artista Rino Valido", | |
// "descrizione": null, | |
// "idCategoria": "QD", | |
// "categoria": "Quadri", | |
// "idAutore": "VALIDO", | |
// "autore": "Valido Rino", | |
// "titolo": "Il dialogo", | |
// "data": "2014", | |
// "stato": "PRON", | |
// "misura": "cm 50 x 50", | |
// "idFornitore": "f000092", | |
// "fornitore": null, | |
// "codiceFiscale": null, | |
// "eMail": null, | |
// "vendita": 1000, | |
// "pictureId": "VI00018", | |
// "tecnica": "Olio su tela", | |
// "epocaAutore": null | |
// } | |
$content = file_get_contents($_POST['json_url']); | |
$catalogueJson = json_decode($content); | |
global $wpdb; | |
$wpdb->query('START TRANSACTION'); | |
// TODO Delete all tagged attachments | |
// @function wp_delete_attachment | |
// @see https://developer.wordpress.org/reference/functions/wp_delete_attachment/ | |
foreach(get_posts(array('numberposts' => -1, 'meta_query' => array(array( | |
'key' => 'vimarte_managed_by_robot', | |
'value' => true, | |
)))) as $post) { | |
wp_delete_post($post->ID, true); | |
} | |
$post_id_or_err = null; | |
$catalogue = array(); | |
echo "<table>"; | |
?> | |
<tr> | |
<th>ID</th> | |
<th>Autore</th> | |
<th>Titolo</th> | |
<th>Stato media</th> | |
</tr> | |
<?php | |
foreach ($catalogueJson as $i => $workJson) { | |
if (isset($_POST['debug_stop_after_first_n']) && $_POST['debug_stop_after_first_n'] > 0) { | |
if (count($catalogue) >= $_POST['debug_stop_after_first_n']) { | |
break; | |
} | |
} | |
$post = array( | |
'post_title' => "Opera: " . $workJson->titolo, | |
'post_name' => 'vimarte-catalogue-work-' . $workJson->idArticolo, | |
'post_content' => "<div>Segue il JSON dell'opera</div><code><pre>" . json_encode($workJson, JSON_PRETTY_PRINT) . "</pre></code>", | |
'post_status' => 'publish', | |
'post_type' => 'post', | |
'meta_input' => array( | |
'vimarte_managed_by_robot' => true, | |
'vimarte_id_articolo' => $workJson->idArticolo, | |
'vimarte_id_categoria' => $workJson->idCategoria | |
) | |
); | |
$post_id_or_err = wp_insert_post($post, true); | |
if (is_wp_error($post_id_or_err)) { | |
break; | |
} | |
wp_create_category($workJson->categoria); | |
wp_set_post_terms($post_id_or_err, category_exists($workJson->categoria), 'category', false); | |
$catalogue []= array( | |
'post_id' => $post_id_or_err, | |
'post_permalink' => get_permalink($post_id_or_err), | |
'vimarte_idArticolo' => $workJson->idArticolo, | |
'vimarte_titolo' => $workJson->titolo, | |
'vimarte_autore' => $workJson->autore, | |
'vimarte_categoria' => $workJson->categoria, | |
'media_status_selector_id' => 'media-status-' . $workJson->idArticolo | |
); | |
?> | |
<tr> | |
<td><a href="<?php echo get_permalink($post_id_or_err) ?>" target="_blank"><?php echo $workJson->idArticolo ?></a></td> | |
<td><?php echo $workJson->autore ?></td> | |
<td><?php echo $workJson->titolo ?></td> | |
<td><span id="<?php echo end($catalogue)['media_status_selector_id'] ?>">In coda</span></td> | |
</tr> | |
<?php | |
} | |
echo "</table>"; | |
if (is_wp_error($post_id_or_err)) { | |
echo "ERROR: "; | |
print_r($post_id_or_err); | |
$wpdb->query('ROLLBACK'); | |
return; | |
} | |
$wpdb->query('COMMIT'); | |
echo "<h2>Importazione di " . count($catalogue) . " opere del catalogo completata!</h2>"; | |
echo "<script>window.vimarte_catalogue = " . json_encode($catalogue) . "</script>"; | |
// TODO Fire sync of images | |
// See https://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Administration_Side | |
} | |
} | |
function vimarte_enqueue_admin_scripts_and_styles() { | |
wp_enqueue_style('vimarte_css', plugins_url('vimarte.css', __FILE__)); | |
wp_enqueue_script('vimarte_js', plugins_url('vimarte.js', __FILE__), array(), null, true); | |
} | |
function vimarte_download_media() { | |
// TODO Actually download and store image | |
// @function media_handle_upload | |
// @see https://codex.wordpress.org/Function_Reference/media_handle_upload | |
header("Content-Type: application/json"); | |
usleep(rand(500, 1500) * 1000); | |
echo json_encode(array('result' => 'ok')); | |
wp_die(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment