guardian_api_keynameValue(), trim(esc_attr($_POST [$api->guardian_api_keynameValue()])) ); update_option ( 'guardian_powered_image', trim(esc_attr($_POST ['guardian_powered_image'])) ); // Put an options updated message on the screen ?>
This key is valid.
'; } } ?>In order to publish Guardian articles on your blog we require that you register and agree to the Terms and Conditions.
The process only takes a few moments. If you have any questions, please have a look through the FAQ or post your question in the Google Group.
An API key is not required for the 'Related Articles' sidebar widget included in this plugin, you can use it straight away.
We have created a number of logos to match your theme.
Normal
Black
White
Reverse
You need a Guardian API Key to publish an article, please click here to request one.
")); echo $error->get_error_message(); return null; } if (!empty($str_result)) { $str_result = "&api-key=".$str_result; } return $str_result; } /* * Function to replace the old content with the new. * * @param $content Old Content from DB * @param $new_content New content from the API */ function guardian_article_replace( $content, $new_content ) { return trim(preg_replace("/.*?/s", "{$new_content}", $content)); } /** * This is the function that reloads the blog posts from the API * * This function should be scheduled for daily access */ function Guardian_ContentAPI_refresh_articles($update_article = true, $activate = false) { global $wpdb; $api = new GuardianOpenPlatformAPI(); $str_api_key = get_option ( $api->guardian_api_keynameValue() ); $api = new GuardianOpenPlatformAPI($str_api_key); $articles = $wpdb->get_results( "SELECT `post_id`, `meta_value` FROM $wpdb->postmeta WHERE meta_key = 'guardian_content_api_id'", ARRAY_A ); if (!empty($articles)) { foreach ($articles as $article) { $arr_guard_article = array(); $data = array(); $find = array(); $new_content = ''; $tagarray = array(); $post = get_post($article['post_id'], ARRAY_A); if ($post['post_status'] == 'publish') { $arr_guard_article = $api->guardian_api_item($article['meta_value']); sleep(1); } if ($activate) { // Get the tags $tags = $arr_guard_article ['tags']; $tagarray = array(); foreach ($tags as $tag) { $tag = trim($tag['webTitle']); if (!empty($tag)) { $tagarray[] = $tag; } } $tagarray = implode(', ', $tagarray); if (empty($arr_guard_article ['fields'] ['body']) || $arr_guard_article ['fields'] ['body'] == '') { $new_content = "The content previously published here has been withdrawn. We apologise for any inconvenience.
"; $tagarray = array(); } else { // Article is fine and well $new_content = ""; if (!empty($arr_guard_article['mediaAssets'])) { foreach ($arr_guard_article['mediaAssets'] as $media) { if ($media['type'] == 'picture') { $new_content .= "guardian.co.uk © Guardian News & Media Limited 2010
Published via the Guardian News Feed plugin for WordPress.
"; } $replace = guardian_article_replace($post['post_content'], $new_content); $data = array( 'ID' => $article['post_id'], 'post_title' => $arr_guard_article ['fields'] ['headline'], 'post_content' => $replace, 'tags_input' => $tagarray, 'post_author'=>$post['post_author'] ); wp_update_post($data); // Delete revisions $sql = "DELETE a,b,c FROM $wpdb->posts a LEFT JOIN $wpdb->term_relationships b ON (a.ID = b.object_id) LEFT JOIN $wpdb->postmeta c ON (a.ID = c.post_id) WHERE a.post_type = 'revision' AND a.post_parent = {$article['post_id']}"; $wpdb->query($sql); } elseif (!empty($arr_guard_article) && $update_article) { if (empty($arr_guard_article ['fields'] ['body']) || $arr_guard_article ['fields'] ['body'] == '') { $new_content = "The content previously published here has been withdrawn. We apologise for any inconvenience.
"; } else { // Article is fine and well $new_content = ""; if (!empty($arr_guard_article['mediaAssets'])) { foreach ($arr_guard_article['mediaAssets'] as $media) { if ($media['type'] == 'picture') { $new_content .= "guardian.co.uk © Guardian News & Media Limited 2010
Published via the Guardian News Feed plugin for WordPress.
"; } $replace = guardian_article_replace($post['post_content'], $new_content); $data = array( 'ID' => $article['post_id'], 'post_content' => $replace, 'post_author'=>$post['post_author'] ); wp_update_post($data); // Delete revisions $sql = "DELETE a,b,c FROM $wpdb->posts a LEFT JOIN $wpdb->term_relationships b ON (a.ID = b.object_id) LEFT JOIN $wpdb->postmeta c ON (a.ID = c.post_id) WHERE a.post_type = 'revision' AND a.post_parent = {$article['post_id']}"; $wpdb->query($sql); } else { if ($post['post_status'] == 'publish') { $tier_status = $api->guardian_get_tier(); $post['post_content'] = guardian_article_replace($post['post_content'], "The content previously published here has been withdrawn. We apologise for any inconvenience.
"); if (!empty($tier_status)) { $data = array( 'ID' => $article['post_id'], 'post_content' => $post['post_content'], 'post_author'=>$post['post_author'] ); wp_update_post($data); // Delete revisions $sql = "DELETE a,b,c FROM $wpdb->posts a LEFT JOIN $wpdb->term_relationships b ON (a.ID = b.object_id) LEFT JOIN $wpdb->postmeta c ON (a.ID = c.post_id) WHERE a.post_type = 'revision' AND a.post_parent = {$article['post_id']}"; $wpdb->query($sql); } } } } } } // JSON support function guardian_id_got_json() { // WP 2.9+ handles everything for us if ( version_compare( get_bloginfo( 'version' ), '2.9', '>=' ) ) return true; // Functions exists already, assume they're good to go if ( function_exists( 'json_encode' ) && function_exists( 'json_decode' ) ) return true; // Load Services_JSON if we need it at this point if ( !class_exists( 'Services_JSON' ) ) include_once( dirname( __FILE__ ) . '/class.json.php' ); // This indicates that we need to define the functions. // Services_JSON *is* available one way or another at this point return false; } if ( !guardian_id_got_json() ) { function json_encode( $data ) { $json = new Services_JSON(); return( $json->encode( $data ) ); } function json_decode( $data ) { $json = new Services_JSON(); return( $json->decode( $data ) ); } } function Guardian_OpenPlatform_add_pages() { global $wpdb; if (function_exists ( "add_submenu_page" )) { add_submenu_page('plugins.php', __('The Guardian News Feed Configuration'), __('The Guardian News Feed Configuration'), 'manage_options', __FILE__, 'Guardian_OpenPlatform_settings_page'); } } // Plugin admin menus add_action ( "admin_menu", "Guardian_OpenPlatform_add_pages" ); /* * Code to enable the wordpress scheduling of refreshing the articles. */ register_activation_hook(__FILE__, 'activate_guardian_scheduling'); add_action('refresh_articles', 'Guardian_ContentAPI_refresh_articles'); /* * Activate the scheduling */ function activate_guardian_scheduling() { update_option ('GUARDIAN_NEWS_FEED_VERSION', GUARDIAN_NEWS_FEED_VERSION); set_time_limit (0); Guardian_ContentAPI_refresh_articles(true, true); wp_schedule_event (time(), 'daily', 'refresh_articles'); } /* * Code to deactivate plugin, remove the scheduler and remove the article contents. Plugin can be activated and contents will be restored. */ register_deactivation_hook(__FILE__, 'my_deactivation'); function my_deactivation() { set_time_limit (0); Guardian_ContentAPI_refresh_articles(false); wp_clear_scheduled_hook('refresh_articles'); } register_sidebar_widget ( __ ( 'The Guardian News Feed - Related Articles' ), 'widget_Guardian_Related' ); ?>