From 3cebf005c3207c51d843383b60ceff013cde694a Mon Sep 17 00:00:00 2001 From: root Date: Fri, 16 Dec 2016 09:11:00 +0100 Subject: [PATCH] Initial code drop --- README.md | 1 + admin/admin.php | 184 ++++++++++++++++++++++++++++++++++++++++++++++ autoload.php | 98 ++++++++++++++++++++++++ front/front.php | 48 ++++++++++++ front/wrapper.php | 19 +++++ mvp.php | 73 ++++++++++++++++++ settings.php | 8 ++ 7 files changed, 431 insertions(+) create mode 100644 README.md create mode 100644 admin/admin.php create mode 100644 autoload.php create mode 100644 front/front.php create mode 100644 front/wrapper.php create mode 100644 mvp.php create mode 100644 settings.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..d361b3a --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# superdesk-wordpress-plugin diff --git a/admin/admin.php b/admin/admin.php new file mode 100644 index 0000000..2bf9f5f --- /dev/null +++ b/admin/admin.php @@ -0,0 +1,184 @@ +Settings'; + return $links; +} + +function get_page_url() { + + $args = array('page' => 'mvp/admin/admin'); + + $url = add_query_arg($args, admin_url('options-general.php')); + + return $url; +} + +function mvp_admin() { + if (isset($_POST['url'])) { + $settings = array( + 'url' => $_POST['url'], + 'username' => $_POST['username'], + 'password' => $_POST['password'], + 'client_id' => $_POST['client_id'], + 'status' => $_POST['status'], + 'author' => $_POST['author'], + 'category' => $_POST['category'], + ); + update_option('mvp_settings', $settings); + } else if (get_option('mvp_settings')) { + $settings = get_option('mvp_settings'); + } else { + $settings = array( + 'url' => '', + 'client_id' => '', + 'username' => '', + 'password' => '', + 'status' => 'publish', + 'author' => '', + 'category' => '', + ); + } + $statuses = array( + 'publish' => 'Published', + 'draft' => 'Draft' + ); + + $authors = array(); + + $categories = array(); + + $args = array( + 'hide_empty' => 0, + ); + $all_categories = get_categories($args); + + foreach ($all_categories as $category) { + $categories[$category->cat_ID] = $category->cat_name; + } + + $all_users = get_users(); + foreach ($all_users as $user) { + $authors[$user->ID] = $user->data->display_name; + } + ?> +
+

Superdesk Publisher

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Autoload/wp-content/plugins/mvp/autoload.php
+ + + +
+ + + +
+ + + +
+ + + +
+ Status of the ingested articles/posts + +
+ $value) { + ?> + +
+ +
+
+ + + +
+ + + +
+

+ +

+
+
+ " . $obj['body_html']; + + $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) { + $edit_post = array( + 'ID' => $sync->post_id, + 'post_title' => wp_strip_all_tags($obj['headline']), + 'post_name' => wp_strip_all_tags($obj['headline']), + 'post_content' => $content, + 'post_content_filtered' => $content + ); + + wp_update_post($edit_post); + } else { + $postarr = array( + 'post_title' => wp_strip_all_tags($obj['headline']), + 'post_name' => wp_strip_all_tags($obj['headline']), + 'post_content' => $content, + 'post_content_filtered' => $content, + 'post_author' => (int) $settings['author'], + 'post_status' => $settings['status'], + 'post_category' => $settings['category'], + ); + + $post_ID = wp_insert_post($postarr, true); + + $table_name = $wpdb->prefix . DB_TABLE_SYNC_POST; + + $wpdb->insert( + $table_name, array( + 'post_id' => $post_ID, + 'guid' => wp_strip_all_tags($obj['guid']), + '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); + } + } + } elseif ($obj['pubstatus'] == 'canceled') { + /* remove article */ + $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) { + + $edit_post = array( + 'ID' => $sync->post_id, + 'post_status' => 'draft' + ); + + wp_update_post($edit_post); + } + } +} diff --git a/front/front.php b/front/front.php new file mode 100644 index 0000000..e52a257 --- /dev/null +++ b/front/front.php @@ -0,0 +1,48 @@ +token_type . ' ' . $config->access_token); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + + $response = curl_exec($ch); + curl_close($ch); + + if ($response) { + require_once MVP_PLUGIN_DIR . '/front/wrapper.php'; + + $content = mvp_view($response); + } + + return $content; +} diff --git a/front/wrapper.php b/front/wrapper.php new file mode 100644 index 0000000..df990f7 --- /dev/null +++ b/front/wrapper.php @@ -0,0 +1,19 @@ +_items as $article) { + if ($article->pubstatus == "usable") { + ?> +
+

headline ?>

+ body_html; ?> +
+ prefix . DB_TABLE_SYNC_POST; + + $charset_collate = $wpdb->get_charset_collate(); + + $sql = "CREATE TABLE $table_name ( + id bigint(20) NOT NULL AUTO_INCREMENT, + post_id bigint(20) NOT NULL, + guid text NOT NULL, + time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, + PRIMARY KEY (id) + ) $charset_collate;"; + + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); + dbDelta($sql); + + add_option('MVP_DATABASE_VERSION', MVP_DATABASE_VERSION); +} + +function mvp_database_update() { + if (get_site_option('MVP_DATABASE_VERSION') != MVP_DATABASE_VERSION) { + mvp_database_install(); + } +} + +function saveFile($from, $to) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $from); + + $fp = fopen($to, 'w+'); + curl_setopt($ch, CURLOPT_FILE, $fp); + + curl_exec($ch); + + curl_close($ch); + fclose($fp); +} + +register_activation_hook(__FILE__, 'mvp_database_install'); +add_action('plugins_loaded', 'mvp_database_update'); diff --git a/settings.php b/settings.php new file mode 100644 index 0000000..3eed0bb --- /dev/null +++ b/settings.php @@ -0,0 +1,8 @@ +