From d5e3b3be6bc4c68c1b56e66567303526e7d9961e Mon Sep 17 00:00:00 2001 From: Filip Penkava Date: Fri, 17 Feb 2017 14:42:51 +0100 Subject: [PATCH] edit/remove featuremedia --- autoload.php | 56 +++++++++++++++++++++++++--------------------------- mvp.php | 24 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 29 deletions(-) diff --git a/autoload.php b/autoload.php index 417d9f4..803c021 100644 --- a/autoload.php +++ b/autoload.php @@ -6,10 +6,10 @@ $json = file_get_contents('php://input'); //$json = file_get_contents('log.txt'); file_put_contents('log.txt', $json . "\n\n", FILE_APPEND); - +//die(); $obj = json_decode($json, true); - -if ($obj['type'] == 'composite') { +/* var_dump($obj);die(); */ +if ($obj['type'] == 'text') { $settings = get_option('mvp_settings'); @@ -20,11 +20,16 @@ if ($obj['type'] == 'composite') { $content.= "

" . wp_strip_all_tags($obj['associations']['featuremedia']['copyrightnotice']) . "

"; } - $guid = wp_strip_all_tags($obj['guid']); + if (isset($obj['evolvedfrom'])) { + $guid = wp_strip_all_tags($obj['evolvedfrom']); + } else { + $guid = wp_strip_all_tags($obj['guid']); + } $sync = $wpdb->get_row("SELECT post_id FROM " . $wpdb->prefix . DB_TABLE_SYNC_POST . " WHERE guid = '" . $guid . "'"); if ($sync) { + $post_ID = $sync->post_id; $edit_post = array( 'ID' => $sync->post_id, 'post_title' => wp_strip_all_tags($obj['headline']), @@ -34,6 +39,20 @@ if ($obj['type'] == 'composite') { ); wp_update_post($edit_post); + + $attachmentExist = get_post_thumbnail_id($post_ID); + + if ($attachmentExist) { + wp_delete_attachment($attachmentExist); + } + + $wpdb->insert( + $wpdb->prefix . DB_TABLE_SYNC_POST, array( + 'post_id' => $post_ID, + 'guid' => wp_strip_all_tags($obj['guid']), + 'time' => current_time('mysql') + ) + ); } else { if ($settings['author-byline'] && $settings['author-byline'] == 'on') { @@ -121,32 +140,11 @@ if ($obj['type'] == 'composite') { 'time' => current_time('mysql') ) ); + } - if ($obj['associations']['featuremedia'] && $obj['associations']['featuremedia']['type'] == 'picture') { - /* save featured media */ - $picture = $obj['associations']['featuremedia']; - - $filenameQ = explode("/", $picture['renditions']['original']['media']); - $filename = $filenameQ[1]; - saveFile($picture['renditions']['original']['href'], wp_upload_dir()['path'] . "/" . $filename); - - $attachment = array( - 'guid' => wp_upload_dir()['url'] . '/' . basename($filename), - 'post_mime_type' => $picture['mimetype'], - 'post_title' => preg_replace('/\.[^.]+$/', '', basename($picture['headline'])), - 'post_content' => '', - 'post_status' => 'inherit' - ); - - $attach_id = wp_insert_attachment($attachment, date("Y") . "/" . date("m") . "/" . $filename, $post_ID); - - require_once( ABSPATH . 'wp-admin/includes/image.php' ); - - $attach_data = wp_generate_attachment_metadata($attach_id, wp_upload_dir()['path'] . "/" . $filename); - - wp_update_attachment_metadata($attach_id, $attach_data); - set_post_thumbnail($post_ID, $attach_id); - } + /* save featured media */ + if ($obj['associations']['featuremedia'] && $obj['associations']['featuremedia']['type'] == 'picture') { + saveAttachment($obj['associations']['featuremedia'], $post_ID); } } elseif ($obj['pubstatus'] == 'canceled') { /* remove article */ diff --git a/mvp.php b/mvp.php index 3e63fb6..afb2403 100644 --- a/mvp.php +++ b/mvp.php @@ -26,6 +26,7 @@ define('MVP_PLUGIN_URL', untrailingslashit(plugins_url('', MVP_PLUGIN))); define('MVP_DATABASE_VERSION', '2'); define('DB_TABLE_SYNC_POST', 'sync_posts'); +define('DB_TABLE_POSTMETA', 'postmeta'); define('DB_TABLE_USERS', 'users'); @@ -81,5 +82,28 @@ function generatePassword() { return $password; } +function saveAttachment($picture, $post_ID) { + $filenameQ = explode("/", $picture['renditions']['original']['media']); + $filename = $filenameQ[1]; + saveFile($picture['renditions']['original']['href'], wp_upload_dir()['path'] . "/" . $filename); + + $attachment = array( + 'guid' => wp_upload_dir()['url'] . '/' . basename($filename), + 'post_mime_type' => $picture['mimetype'], + 'post_title' => preg_replace('/\.[^.]+$/', '', basename($picture['headline'])), + 'post_content' => '', + 'post_status' => 'inherit' + ); + + $attach_id = wp_insert_attachment($attachment, date("Y") . "/" . date("m") . "/" . $filename, $post_ID); + + require_once( ABSPATH . 'wp-admin/includes/image.php' ); + + $attach_data = wp_generate_attachment_metadata($attach_id, wp_upload_dir()['path'] . "/" . $filename); + + wp_update_attachment_metadata($attach_id, $attach_data); + set_post_thumbnail($post_ID, $attach_id); +} + register_activation_hook(__FILE__, 'mvp_database_install'); add_action('plugins_loaded', 'mvp_database_update');