From 2736600801384c65604b40362ea022044e077a3f Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Wed, 6 Jul 2016 22:42:55 -0400 Subject: [PATCH 1/6] Fixed PHP 5.6 Strict Standards Error --- classes/NPRAPI.php | 20 ++++++++++++++++++-- classes/NPRAPIWordpress.php | 3 +++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/classes/NPRAPI.php b/classes/NPRAPI.php index 257370e..8d061c2 100644 --- a/classes/NPRAPI.php +++ b/classes/NPRAPI.php @@ -45,7 +45,15 @@ function prepare_request() { } - function send_request() { + /** + * This function will send the push request to the NPR API to add/update a story. + * + * @see NPRAPI::send_request() + * + * @param string $nprml + * @param int $ID + */ + function send_request ( $nprml, $ID ) { } @@ -61,8 +69,16 @@ function flatten() { } - function create_NPRML() { + /** + * Create NPRML from wordpress post. + * + * @param object $object + * @return string + * An NPRML string. + */ + function create_NPRML( $object ) { + return ''; } /** diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index 58128bf..d17526e 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -421,6 +421,9 @@ function create_NPRML( $post ) { * This function will send the push request to the NPR API to add/update a story. * * @see NPRAPI::send_request() + * + * @param string $nprml + * @param int $post_ID */ function send_request ( $nprml, $post_ID ) { $error_text = ''; From 64f5205b49b7aaf21a4f5865ccfc8bf8184e18dc Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Wed, 6 Jul 2016 22:49:47 -0400 Subject: [PATCH 2/6] Fixed "Function split() is deprecated" warning. --- classes/NPRAPIWordpress.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index d17526e..3ad4e13 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]; } } From a3790f6bceb15a27531d714fe3ae8be2ade17f90 Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Wed, 6 Jul 2016 22:53:52 -0400 Subject: [PATCH 3/6] Fixed "Undefined variable: ret" warning. --- classes/NPRAPIWordpress.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index 3ad4e13..48d0225 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -390,7 +390,7 @@ function update_posts_from_stories( $publish = TRUE ) { $category_ids[] = $category_id; } } - if ( count( $category_ids ) > 0 && is_integer( $ret ) ) { + if ( count( $category_ids ) > 0 && isset( $ret ) && is_integer( $ret ) ) { wp_set_post_categories( $ret, $category_ids ); } } From 7f9369229262ca27897d5c96851d45af337ade5a Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Thu, 7 Jul 2016 01:18:52 -0400 Subject: [PATCH 4/6] Fixed "Trying to get property of non-object" warnings --- classes/NPRAPIWordpress.php | 114 ++++++++++++++++++++++++------------ 1 file changed, 75 insertions(+), 39 deletions(-) diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index 48d0225..d5803b0 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -91,7 +91,7 @@ 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. * - * @param unknown_type $publish + * @param bool $publish */ function update_posts_from_stories( $publish = TRUE ) { $pull_post_type = get_option( 'ds_npr_pull_post_type' ); @@ -130,7 +130,7 @@ function update_posts_from_stories( $publish = TRUE ) { $post_pub_date = strtotime($post_pub_date_meta[0]); } } else { - $existing = null; + $existing = $existing_status = null; } //add the transcript @@ -159,28 +159,42 @@ function update_posts_from_stories( $publish = TRUE ) { $by_line = $story->byline->name->value; $multi_by_line = 0; //only single author, set multi to false if ( ! empty( $story->byline->link ) ) { - foreach( $story->byline->link as $link ) { - if ( $link->type == 'html' ) { - $byline_link = $link->value; + $links = $story->byline->link; + if ( is_string( $links ) ) { + $byline_link = $links; + } else if ( is_array( $links ) ) { + foreach ( $links as $link ) { + if ( empty( $link->type ) ) { + continue; + } + if ( 'html' === $link->type ) { + $byline_link = $link->value; + } } + } else if ( $links instanceof NPRMLElement && ! empty( $links->value ) ) { + $byline_link = $links->value; } } } //construct delimited string if there are multiple bylines - if ( is_array( $story->byline ) && !empty( $story->byline ) ) { + if ( ! 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 + foreach ( (array) $story->byline as $single ) { + if ( ! empty( $single->name->value ) ) { + 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 } - $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' ) { + foreach( (array) $single->link as $link ) { + if ( empty( $link->type ) ) { + continue; + } + if ( 'html' === $link->type ) { $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 } @@ -208,7 +222,7 @@ function update_posts_from_stories( $publish = TRUE ) { if ( isset($story->audio) ) { $mp3_array = array(); $m3u_array = array(); - foreach ( $story->audio as $n => $audio ) { + foreach ( (array) $story->audio as $n => $audio ) { if ( ! empty( $audio->format->mp3['mp3']) && $audio->permissions->download->allow == 'true' ) { if ($audio->format->mp3['mp3']->type == 'mp3' ) { $mp3_array[] = $audio->format->mp3['mp3']->value; @@ -232,7 +246,7 @@ function update_posts_from_stories( $publish = TRUE ) { //now that we have an id, we can add images //this is the way WP seems to do it, but we couldn't call media_sideload_image or media_ because that returned only the URL //for the attachment, and we want to be able to set the primary image, so we had to use this method to get the attachment ID. - if ( isset( $story->image[0] ) ) { + if ( ! empty( $story->image ) && is_array( $story->image ) && count( $story->image ) ) { //are there any images saved for this post, probably on update, but no sense looking of the post didn't already exist if ( $existing ) { @@ -246,31 +260,35 @@ function update_posts_from_stories( $publish = TRUE ) { ); $attached_images = get_children( $image_args ); } - foreach ( $story->image as $image ) { + foreach ( (array) $story->image as $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 ) ) { $image_url = $image->enlargement->src; } else { - if ( ! empty( $image->crop ) ) { + if ( ! empty( $image->crop ) && is_array( $image->crop ) ) { foreach ( $image->crop as $crop ) { - if ( $crop->type == 'enlargement' ) { - $image_url = $crop->src; + if ( empty( $crop->type ) ) { continue; } + if ( 'enlargement' === $crop->type ) { + $image_url = $crop->src; + } } if ( empty( $image_url ) ) { foreach ( $image->crop as $crop ) { - if ( $crop->type == 'standard' ) { - $image_url = $crop->src; + if ( empty( $crop->type ) ) { continue; } + if ( 'standard' === $crop->type ) { + $image_url = $crop->src; + } } } } } - if ( empty( $image_url ) ) { + if ( empty( $image_url ) && ! empty( $image->src ) ) { $image_url = $image->src; } nprstory_error_log( 'Got image from: ' . $image_url ); @@ -395,10 +413,10 @@ function update_posts_from_stories( $publish = TRUE ) { } } if ( $single_story ) { - return $post_id; + return isset( $post_id ) ? $post_id : 0; } } - return; + return null; } @@ -504,30 +522,48 @@ function send_delete( $api_id ) { } /** - * + * * This function will check a story to see if there are transcripts that should go with it, if there are * we'll return the transcript as one big strang with Transcript at the top and each paragraph separated by

* - * @param $story + * @param string $story + * @return string */ function get_transcript_body( $story ) { $transcript_body = ""; - if ( ! empty( $story->transcript ) ) { + if ( ! empty( $story->transcript ) && is_array( $story->transcript ) ) { foreach ( $story->transcript as $transcript ) { - if ( $transcript->type == 'api' ) { - $response = wp_remote_get( $transcript->value ); - if ( !is_wp_error( $response ) ) { - $transcript_body .= "

Transcript :

"; - $body_xml = simplexml_load_string( $response['body'] ); - if ( ! empty( $body_xml->paragraph ) ) { - foreach( $body_xml->paragraph as $paragraph ) { - $transcript_body .= (strip_tags( $paragraph )) . '

'; - } - } + if ( empty( $transcript->link ) ) { + continue; + } + foreach ( (array) $transcript->link as $link ) { + if ( ! isset( $link->type ) || 'api' !== $link->type ) { + continue; + } + $response = wp_remote_get( $link->value ); + if ( is_wp_error( $response ) ) { + /** + * @var WP_Error $response + */ + $code = $response->get_error_code(); + $message = $response->get_error_message(); + $message = sprintf( 'Error requesting story transcript via API URL: %s (%s [%d])', $link->value, $message, $code ); + error_log( $message ); + continue; + } + $body_xml = simplexml_load_string( $response[ 'body' ] ); + if ( empty( $body_xml->paragraph ) || ! is_array( $body_xml->paragraph ) ) { + continue; + } + $transcript_body .= "

Transcript :

"; + foreach ( $body_xml->paragraph as $paragraph ) { + $transcript_body .= ( strip_tags( $paragraph ) ) . '

'; } } } + } + return $transcript_body; } } From 2f35137a6ac6b8eb5037ff52fe2553633d824cbf Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Fri, 8 Jul 2016 03:24:15 -0400 Subject: [PATCH 5/6] Fix for "Undefined property: stdClass::$url" and related. --- classes/NPRAPI.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/classes/NPRAPI.php b/classes/NPRAPI.php index 8d061c2..a096eed 100644 --- a/classes/NPRAPI.php +++ b/classes/NPRAPI.php @@ -31,9 +31,11 @@ function __construct() { $this->request->data = NULL; $this->request->path = NULL; $this->request->base = NULL; + $this->request->request_url = NULL; $this->response = new stdClass; + $this->response->id = NULL; $this->response->code = NULL; } @@ -161,7 +163,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) && !stristr($this->request->request_url, 'sort=')){ $this->stories = array_reverse($this->stories); } //there are params, and sort is not one of them From 72bd0f4e4a6276154606b6d3d5a92f3f826c95c4 Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Fri, 8 Jul 2016 03:34:05 -0400 Subject: [PATCH 6/6] Closed

tags. --- classes/NPRAPIWordpress.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index d5803b0..8b8e44a 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -555,9 +555,9 @@ function get_transcript_body( $story ) { if ( empty( $body_xml->paragraph ) || ! is_array( $body_xml->paragraph ) ) { continue; } - $transcript_body .= "

Transcript :

"; + $transcript_body .= "

Transcript :

"; foreach ( $body_xml->paragraph as $paragraph ) { - $transcript_body .= ( strip_tags( $paragraph ) ) . '

'; + $transcript_body .= '

' . ( strip_tags( $paragraph ) ) . '

'; } } }