diff --git a/classes/NPRAPI.php b/classes/NPRAPI.php index 4d0747e..bf9cabd 100644 --- a/classes/NPRAPI.php +++ b/classes/NPRAPI.php @@ -45,7 +45,9 @@ function prepare_request() { } - function send_request() { + // Edit - Function has no parameters. Without parameters, throws error + // Strict standards: Declaration of NPRAPIWordpress::send_request() should be compatible with NPRAPI::send_request() + function send_request($nprml, $post_ID ) { } @@ -61,7 +63,9 @@ function flatten() { } - function create_NPRML() { + // Edit - Function has no parameters. Without parameters, throws error + // Strict standards: Declaration of NPRAPIWordpress::create_NPRML() should be compatible with NPRAPI::create_NPRML() + function create_NPRML( $post ) { } @@ -81,12 +85,13 @@ function parse() { $object = simplexml_load_string($xml); $this->add_simplexml_attributes($object, $this); - if (!empty($object->message)) { + if ( isset( $object->message ) && !empty($object->message)) { + $this->message = new StdClass; $this->message->id = $this->get_attribute($object->message, 'id'); $this->message->level = $this->get_attribute($object->message, 'level'); } - if (!empty($object->list->story)) { + if ( isset($object->list->story ) && !empty($object->list->story)) { foreach ($object->list->story as $story) { $parsed = new NPRMLEntity(); $this->add_simplexml_attributes($story, $parsed); @@ -146,7 +151,7 @@ function parse() { //if the query didn't have a sort parameter, reverse the order so that we end up with //stories in reverse-chron order. //there are no params and 'sort=' is not in the URL - if (empty($this->request->params) && !stristr($this->request->url, 'sort=')){ + if (empty($this->request->params) && isset($this->request->url) && !stristr($this->request->url, 'sort=')){ $this->stories = array_reverse($this->stories); } //there are params, and sort is not one of them diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index 41d07c5..c9cb160 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -56,10 +56,10 @@ function query_by_url( $url ) { //fill out the $this->request->param array so we can know what params were sent $parsed_url = parse_url( $url ); if ( ! empty( $parsed_url['query'] ) ) { - $parms = split( '&', $parsed_url['query'] ); + $parms = explode( '&', $parsed_url['query'] ); if ( ! empty( $params ) ){ foreach ( $params as $p ){ - $attrs = split( '=', $p ); + $attrs = explode( '=', $p ); $this->request->param[$attrs[0]] = $attrs[1]; } } @@ -92,8 +92,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 unknown_type $publish + * @param bool|num $qnum - the query number of story being updated + * primarily used for adding tags to new stories */ - 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'; @@ -148,6 +150,12 @@ function update_posts_from_stories( $publish = TRUE ) { 'post_type' => $pull_post_type, 'post_date' => $post_date, ); + + if( false !== $qnum ) { + $args['tags_input'] = 'NPR, ' . get_option('ds_npr_query_tags_'.$qnum); + } else { + $args['tags_input'] = 'NPR'; + } //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 ) ) { @@ -168,22 +176,25 @@ function update_posts_from_stories( $publish = TRUE ) { } //construct delimited string if there are multiple bylines - if ( is_array( $story->byline ) && !empty( $story->byline ) ) { + if ( isset( $story->byline ) && is_array( $story->byline ) && !empty( $story->byline ) ) { $i = 0; foreach ( $story->byline as $single ) { - if ( $i==0 ) { - $multi_by_line .= $single->name->value; //builds multi byline string without delimiter on first pass - } else { - $multi_by_line .= '|' . $single->name->value ; //builds multi byline string with delimiter - } - $by_line = $single->name->value; //overwrites so as to save just the last byline for previous single byline themes - - if ( ! empty( $single->link ) ) { - foreach( $single->link as $link ) { - if ($link->type == 'html' ) { - $byline_link = $link->value; //overwrites so as to save just the last byline link for previous single byline themes - $multi_by_line .= '~' . $link->value; //builds multi byline string links - } + if( is_object( $single ) ) { + if( isset( $single->name ) ) { + if ( $i==0 ) { + $multi_by_line .= $single->name->value; //builds multi byline string without delimiter on first pass + } else { + $multi_by_line .= '|' . $single->name->value ; //builds multi byline string with delimiter + } + $by_line = $single->name->value; //overwrites so as to save just the last byline for previous single byline themes + } + if ( isset( $single->link ) && ! empty( $single->link ) ) { + foreach( $single->link as $link ) { + if ($link->type == 'html' ) { + $byline_link = $link->value; //overwrites so as to save just the last byline link for previous single byline themes + $multi_by_line .= '~' . $link->value; //builds multi byline string links + } + } } } $i++; @@ -252,16 +263,16 @@ function update_posts_from_stories( $publish = TRUE ) { if ( ! empty( $image->enlargement ) ) { $image_url = $image->enlargement->src; } else { - if ( ! empty( $image->crop ) ) { + if ( is_object( $image->crop ) && ! empty( $image->crop ) ) { foreach ( $image->crop as $crop ) { - if ( $crop->type == 'enlargement' ) { + if ( isset( $crop->type ) && $crop->type == 'enlargement' ) { $image_url = $crop->src; continue; } } if ( empty( $image_url ) ) { foreach ( $image->crop as $crop ) { - if ( $crop->type == 'standard' ) { + if ( isset( $crop->type ) && $crop->type == 'standard' ) { $image_url = $crop->src; continue; } @@ -511,7 +522,7 @@ function get_transcript_body( $story ) { $transcript_body = ""; if ( ! empty( $story->transcript ) ) { foreach ( $story->transcript as $transcript ) { - if ( $transcript->type == 'api' ) { + if ( is_object($transcript) && $transcript->type == 'api' ) { $response = wp_remote_get( $transcript->value ); if ( !is_wp_error( $response ) ) { $transcript_body .= "

Transcript :

"; diff --git a/ds-npr-api.php b/ds-npr-api.php index acf610f..5f46e53 100644 --- a/ds-npr-api.php +++ b/ds-npr-api.php @@ -1,9 +1,9 @@ update_posts_from_stories($pub_flag); + $story = $api->update_posts_from_stories($pub_flag, $i); } else { if ( empty($story) ) { error_log('Not going to save story. Return from query='. $query_string .', we got an error='.$api->message->id. ' error'); @@ -126,7 +126,7 @@ function DS_NPR_API() { return; } add_action( 'admin_menu', array( &$this, 'admin_menu' ) ); - add_action( 'load-posts_page_get-npr-stories', array( 'DS_NPR_API', 'load_page_hook' ) ); + add_action( 'load-posts_page_get-npr-stories', array( $this, 'load_page_hook' ) ); } function admin_menu() { diff --git a/push_story.php b/push_story.php index 3a5b4dd..21b6b92 100644 --- a/push_story.php +++ b/push_story.php @@ -66,7 +66,7 @@ function npr_delete ( $post_ID ) { } $api_id_meta = get_post_meta( $post_ID, NPR_STORY_ID_META_KEY ); - $api_id = $api_id_meta[0]; + $api_id = is_array($api_id_meta) && !empty($api_id_meta) ? $api_id_meta[0] : false; $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' ); @@ -421,6 +421,6 @@ function save_send_to_nprone( $post_ID ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false; if ( ! current_user_can( 'edit_page', $post_ID ) ) return false; if ( empty( $post_ID ) ) return false; - $value = ($_POST['send_to_nprone']) ? 1 : 0; + $value = isset($_POST['send_to_nprone']) and ($_POST['send_to_nprone']) ? 1 : 0; update_post_meta( $post_ID, '_send_to_nprone', $value ); } \ No newline at end of file diff --git a/settings.php b/settings.php index 8198944..4a8dada 100644 --- a/settings.php +++ b/settings.php @@ -56,7 +56,7 @@ function ds_npr_settings_init() { add_settings_section( 'ds_npr_api_get_multi_settings', 'NPR API multiple get settings', 'ds_npr_api_get_multi_settings_callback', 'ds_npr_api_get_multi_settings' ); - add_settings_field( 'ds_npr_num', 'Number of things to get', 'ds_npr_api_num_multi_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' ); + add_settings_field( 'ds_npr_num', 'Number of queries to run', 'ds_npr_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' ); $num = get_option( 'ds_npr_num' ); if ( empty($num) ) { @@ -69,6 +69,10 @@ function ds_npr_settings_init() { //ds_npr_query_publish_ add_settings_field( 'ds_npr_query_publish_' . $i, 'Publish Stories ' . $i, 'ds_npr_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 ); + + // add tag(s) to imported stories + 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', 'dp_npr_query_run_multi_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' ); @@ -156,7 +160,7 @@ function ds_npr_api_query_publish_callback( $i ){ $option_string .= "value='" . esc_attr($key) . "'>" . esc_html($key) . " "; echo $option_string; } - echo "


"; + echo " "; } function ds_npr_api_query_callback( $i ) { @@ -165,6 +169,14 @@ function ds_npr_api_query_callback( $i ) { echo ""; } +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). The NPR tag is added automatically.

"; + echo "


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

Increase the number of queries by changing the number in the field above.";