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 64bcdbb..c16e6d9 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -94,10 +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 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 ) { + 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'; @@ -154,6 +168,9 @@ function update_posts_from_stories( $publish = TRUE ) { 'post_type' => $pull_post_type, 'post_date' => $post_date, ); + 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 ) ) { @@ -272,16 +289,16 @@ function update_posts_from_stories( $publish = TRUE ) { //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 ); } - 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 ) ) { @@ -312,7 +329,24 @@ 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 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->image[ $image_index ], $post_id, $story, $image_index ); + + nprstory_error_log( 'Got image from: ' . $image_url ); // Download file to temp location $tmp = download_url( $image_url ); @@ -389,12 +423,12 @@ function update_posts_from_stories( $publish = TRUE ) { } $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 diff --git a/get_stories.php b/get_stories.php index 841fd1d..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); + $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 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' ); 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.";