From fb44ebf7ff6c1b41b3db50f1085ea8ea82abb0ee Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Sat, 4 Feb 2017 20:49:45 -0500 Subject: [PATCH 1/6] Added an 'npr_image_crop_url' hook. --- classes/NPRAPIWordpress.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index 64bcdbb..0e80439 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -312,7 +312,22 @@ function update_posts_from_stories( $publish = TRUE ) { if ( empty( $image_url ) && ! empty( $image->src ) ) { $image_url = $image->src; } - nprstory_error_log( 'Got image from: ' . $image_url ); + + /** + * Filters the image crop url + * + * Allows a site to decide which crop it prefers to use for thumbnail/featured image. + * Especially useful if/when the crop is way too big. + * + * @since 1.7 + * + * @param string $image_url URL of image crop to download + * @param NPRMLEntity $story Story object created during import + * @param int $post_id Post ID or NULL if no post ID. + */ + $image_url = apply_filters( 'npr_image_crop_url', $image_url, $story, $post_id ); + + nprstory_error_log( 'Got image from: ' . $image_url ); // Download file to temp location $tmp = download_url( $image_url ); From ac9b27de5c4a12ea069048248579c25c718c2bc5 Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Sat, 4 Feb 2017 21:21:18 -0500 Subject: [PATCH 2/6] Applying changes from PR #39. --- classes/NPRAPIWordpress.php | 6 +++++- get_stories.php | 2 +- settings.php | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index 0e80439..b759525 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -95,9 +95,10 @@ function query_by_url( $url ) { * available from the NPR API if the pubDate on the API is after the pubDate originally stored locally. * * @param bool $publish + * @param int|bool $qnum * @return int|null $post_id or null */ - function update_posts_from_stories( $publish = TRUE ) { + function update_posts_from_stories( $publish = TRUE, $qnum = false ) { $pull_post_type = get_option( 'ds_npr_pull_post_type' ); if ( empty( $pull_post_type ) ) { $pull_post_type = 'post'; @@ -154,6 +155,9 @@ function update_posts_from_stories( $publish = TRUE ) { 'post_type' => $pull_post_type, 'post_date' => $post_date, ); + if ( false !== $qnum ) { + $args['tags_input'] = get_option('ds_npr_query_tags_'.$qnum); + } //check the last modified date and pub date (sometimes the API just updates the pub date), if the story hasn't changed, just go on if ( $post_mod_date != strtotime( $story->lastModifiedDate->value ) || $post_pub_date != strtotime( $story->pubDate->value ) ) { diff --git a/get_stories.php b/get_stories.php index 841fd1d..d64d114 100644 --- a/get_stories.php +++ b/get_stories.php @@ -54,7 +54,7 @@ public static function nprstory_cron_pull() { if ( $pub_option == 'Publish' ) { $pub_flag = TRUE; } - $story = $api->update_posts_from_stories($pub_flag); + $story = $api->update_posts_from_stories($pub_flag, $i); } else { if ( empty($story) ) { error_log('NPR Story API: not going to save story. Query '. $query_string .' returned an error '.$api->message->id. ' error'); // debug use diff --git a/settings.php b/settings.php index 870c67f..e481784 100644 --- a/settings.php +++ b/settings.php @@ -55,6 +55,8 @@ function nprstory_settings_init() { add_settings_section( 'ds_npr_api_get_multi_settings', 'NPR API multiple get settings', 'nprstory_api_get_multi_settings_callback', 'ds_npr_api_get_multi_settings' ); + add_settings_field( 'ds_npr_num', 'Number of things to get', 'nprstory_api_num_multi_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' ); + add_settings_field( 'ds_npr_num', 'Number of things to get', 'nprstory_api_num_multi_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' ); register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_num', 'intval' ); @@ -72,6 +74,10 @@ function nprstory_settings_init() { //ds_npr_query_publish_ add_settings_field( 'ds_npr_query_publish_' . $i, 'Publish Stories ' . $i, 'nprstory_api_query_publish_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings', $i ); register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_query_publish_' . $i , 'nprstory_validation_callback_select'); + + // Add tags + add_settings_field( 'ds_npr_query_tags_' . $i, 'Add Tags ' . $i, 'ds_npr_api_query_tags_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings', $i ); + register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_query_tags_' . $i ); } add_settings_field( 'dp_npr_query_run_multi', 'Run the queries on saving changes', 'nprstory_query_run_multi_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' ); @@ -170,6 +176,15 @@ function nprstory_api_query_callback( $i ) { } +function ds_npr_api_query_tags_callback( $i ) { + $name = 'ds_npr_query_tags_' . $i; + $option = get_option( $name ); + + echo "

Add tag(s) to each story pulled from NPR (comma separated).

"; + wp_nonce_field( 'nprstory_nonce_ds_npr_tags_' . $i, 'nprstory_nonce_ds_npr_tags_' . $i . '_name', true, true ); + echo "


"; +} + function nprstory_api_num_multi_callback() { $option = get_option('ds_npr_num'); echo "

Increase the number of queries by changing the number in the field above."; From 4734f8beaf0cafa0ef1448980cd38c4ca9e37af3 Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Sat, 4 Feb 2017 21:31:40 -0500 Subject: [PATCH 3/6] Suggested improvements to PR #39. --- classes/NPRAPIWordpress.php | 21 +++++++++++++++++---- get_stories.php | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index b759525..b2495d9 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -94,11 +94,24 @@ function query_by_url( $url ) { * This function will go through the list of stories in the object and check to see if there are updates * available from the NPR API if the pubDate on the API is after the pubDate originally stored locally. * + * @see https://github.com/nprds/nprapi-wordpress/commit/ac0a7f7e3b7428ba783853eb76696b499bc85ecc for is_numeric($args) + * + * @since 1.7 + * * @param bool $publish - * @param int|bool $qnum + * @param array|int $opts { If numeric it will be `'query_number' otherwise an array of named options. + * + * @type int $query_number The number of the query for $args['tags_input'] + * + * } * @return int|null $post_id or null */ - function update_posts_from_stories( $publish = TRUE, $qnum = false ) { + function update_posts_from_stories( $publish = TRUE, $opts = array() ) { + + $opts = wp_parse_args( $opts, array( + 'query_number' => is_numeric( $opts ) ? intval( $opts ) : false, + )); + $pull_post_type = get_option( 'ds_npr_pull_post_type' ); if ( empty( $pull_post_type ) ) { $pull_post_type = 'post'; @@ -155,8 +168,8 @@ function update_posts_from_stories( $publish = TRUE, $qnum = false ) { 'post_type' => $pull_post_type, 'post_date' => $post_date, ); - if ( false !== $qnum ) { - $args['tags_input'] = get_option('ds_npr_query_tags_'.$qnum); + if ( false !== $opts[ 'query_number' ] ) { + $args['tags_input'] = get_option( "ds_npr_query_tags_{$opts[ 'query_number' ]}" ); } //check the last modified date and pub date (sometimes the API just updates the pub date), if the story hasn't changed, just go on if ( $post_mod_date != strtotime( $story->lastModifiedDate->value ) || $post_pub_date != strtotime( $story->pubDate->value ) ) { diff --git a/get_stories.php b/get_stories.php index d64d114..09bfac3 100644 --- a/get_stories.php +++ b/get_stories.php @@ -54,7 +54,7 @@ public static function nprstory_cron_pull() { if ( $pub_option == 'Publish' ) { $pub_flag = TRUE; } - $story = $api->update_posts_from_stories($pub_flag, $i); + $story = $api->update_posts_from_stories($pub_flag, "query_number={$i}" ); } else { if ( empty($story) ) { error_log('NPR Story API: not going to save story. Query '. $query_string .' returned an error '.$api->message->id. ' error'); // debug use From 90b56df8204bd7876f126d15ed46efe99c55edce Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Sat, 4 Feb 2017 23:09:46 -0500 Subject: [PATCH 4/6] Updated parameters provided to 'npr_image_crop_url' after actually trying to use them! --- classes/NPRAPIWordpress.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index b2495d9..6fbbc54 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -298,7 +298,7 @@ function update_posts_from_stories( $publish = TRUE, $opts = array() ) { ); $attached_images = get_children( $image_args ); } - foreach ( (array) $story->image as $image ) { + foreach ( (array) $story->image as $image_index => $image ) { $image_url = ''; //check the and then the crops, in this order "enlargement", "standard" if they don't exist, just get the image->src if ( ! empty( $image->enlargement ) ) { @@ -339,10 +339,12 @@ function update_posts_from_stories( $publish = TRUE, $opts = array() ) { * @since 1.7 * * @param string $image_url URL of image crop to download - * @param NPRMLEntity $story Story object created during import + * @param NPRMLElement $image Image object containing crops * @param int $post_id Post ID or NULL if no post ID. + * @param NPRMLEntity $story Story object created during import + * @param int $image_index Index into $story->image[] that results in $image */ - $image_url = apply_filters( 'npr_image_crop_url', $image_url, $story, $post_id ); + $image_url = apply_filters( 'npr_image_crop_url', $image_url, $story->image[ $image_index ], $post_id, $story, $image_index ); nprstory_error_log( 'Got image from: ' . $image_url ); // Download file to temp location From f1819ee457b23a04e8917fc631c874b7f6d69176 Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Mon, 25 Sep 2017 16:23:32 -0400 Subject: [PATCH 5/6] Fixed redirects for City Lights and Closer Look --- classes/NPRAPI.php | 14 +++++++++++--- classes/NPRAPIWordpress.php | 24 ++++++++++++------------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/classes/NPRAPI.php b/classes/NPRAPI.php index a096eed..4d3dafd 100644 --- a/classes/NPRAPI.php +++ b/classes/NPRAPI.php @@ -96,12 +96,20 @@ function parse() { return; } - $object = simplexml_load_string($xml); + try { + $object = simplexml_load_string( $xml ); + } catch ( Exception $e ) { + echo "\nXML ERROR: {$e->getMessage()}\n"; + $this->stories = array(); + return; + } $this->add_simplexml_attributes($object, $this); if (!empty($object->message)) { - $this->message->id = $this->get_attribute($object->message, 'id'); - $this->message->level = $this->get_attribute($object->message, 'level'); + $this->message = (object) array( + 'id' => $this->get_attribute( $object->message, 'id' ), + 'level' => $this->get_attribute( $object->message, 'level' ) + ); } if (!empty($object->list->story)) { diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index 6fbbc54..c16e6d9 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -289,12 +289,12 @@ function update_posts_from_stories( $publish = TRUE, $opts = array() ) { //are there any images saved for this post, probably on update, but no sense looking of the post didn't already exist if ( $existing ) { $image_args = array( - 'order'=> 'ASC', - 'post_mime_type' => 'image', - 'post_parent' => $post_id, - 'post_status' => null, - 'post_type' => 'attachment', - 'post_date' => $post_date, + 'order' => 'ASC', + 'post_mime_type' => 'image', + 'post_parent' => $post_id, + 'post_status' => null, + 'post_type' => 'attachment', + 'post_date' => $post_date, ); $attached_images = get_children( $image_args ); } @@ -423,12 +423,12 @@ function update_posts_from_stories( $publish = TRUE, $opts = array() ) { } $args = array( - 'post_title' => $story->title, - 'post_content' => $story->body, - 'post_excerpt' => $story->teaser, - 'post_type' => $pull_post_type, - 'ID' => $post_id, - 'post_date' => $post_date, + 'post_title' => $story->title, + 'post_content' => $story->body, + 'post_excerpt' => $story->teaser, + 'post_type' => $pull_post_type, + 'ID' => $post_id, + 'post_date' => $post_date, ); //set author From 2cae51aa7c2534204a186300a3854b4026d7b6e8 Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Tue, 3 Oct 2017 02:08:30 -0400 Subject: [PATCH 6/6] Stop throwing an error if get_post_meta( $post_ID, NPR_STORY_ID_META_KEY ) returns null. --- push_story.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/push_story.php b/push_story.php index c9fa5a2..2b72c45 100644 --- a/push_story.php +++ b/push_story.php @@ -89,8 +89,8 @@ function nprstory_api_delete ( $post_ID ) { $push_post_type = 'post'; } - $api_id_meta = get_post_meta( $post_ID, NPR_STORY_ID_META_KEY ); - $api_id = $api_id_meta[0]; + $api_id = get_post_meta( $post_ID, NPR_STORY_ID_META_KEY, true ); + $post = get_post( $post_ID ); //if the push url isn't set, don't even try to delete. $push_url = get_option( 'ds_npr_api_push_url' );