From 8a4040f860dee3b4e014483fcfd4cb90912445eb Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Wed, 26 Sep 2018 16:09:40 -0400 Subject: [PATCH 01/10] make sure wp_read_image_metadata() is defined when running media_handle_sideload() during wp_cron For https://github.com/npr/nprapi-wordpress/issues/59 --- classes/NPRAPIWordpress.php | 1 + 1 file changed, 1 insertion(+) diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index 480b3ea..22a1b3b 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -334,6 +334,7 @@ function update_posts_from_stories( $publish = TRUE, $qnum = false ) { } // do the validation and storage stuff + require_once( ABSPATH . 'wp-admin/includes/media.php'); // needed for media_handle_sideload during cron $id = media_handle_sideload( $file_array, $post_id, $image->title->value ); // If error storing permanently, unlink if ( is_wp_error($id) ) { From c6f505d2881a3a61058d575bfedb1a494e51c4e7 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Wed, 26 Sep 2018 16:10:11 -0400 Subject: [PATCH 02/10] Fix for Undefined Offset when attempting to delete a story in the API > PHP Notice: Undefined offset: 0 in /Users/blk/sites/nprapi/wp-content/plugins/nprapi-wordpress/push_story.php on line 99 Seen during GET /wp-admin/post.php?post=6&action=trash&_wpnonce=5b73650f23 --- push_story.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/push_story.php b/push_story.php index d99691e..386949b 100644 --- a/push_story.php +++ b/push_story.php @@ -96,7 +96,12 @@ function nprstory_api_delete ( $post_ID ) { } $api_id_meta = get_post_meta( $post_ID, NPR_STORY_ID_META_KEY ); - $api_id = $api_id_meta[0]; + if ( isset( $api_id_meta[0] ) ) { + $api_id = $api_id_meta[0]; + } else { + $api_id = null; + } + $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' ); From c30dde1fab4ec5b9639bf4ee43d5a6a0207967b2 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Wed, 26 Sep 2018 16:11:50 -0400 Subject: [PATCH 03/10] Remove duplicate
in settings for fetching multiple posts; there's a line after this hr for the tags. --- settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings.php b/settings.php index 5017295..7778224 100644 --- a/settings.php +++ b/settings.php @@ -173,7 +173,7 @@ function nprstory_api_query_publish_callback($i) { echo $option_string; } $option_string .= wp_nonce_field( 'nprstory_nonce_ds_npr_query_publish_' . $i, 'nprstory_nonce_ds_npr_query_publish_' . $i . '_name', true, false ); - echo "


"; + echo " "; } function nprstory_api_query_callback( $i ) { From 66e4e9da3123dc951c6d04741749379f3a882dd8 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Wed, 26 Sep 2018 16:19:48 -0400 Subject: [PATCH 04/10] Corrected fix for https://github.com/npr/nprapi-wordpress/issues/59 --- classes/NPRAPIWordpress.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index 22a1b3b..3a0f328 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -334,7 +334,7 @@ function update_posts_from_stories( $publish = TRUE, $qnum = false ) { } // do the validation and storage stuff - require_once( ABSPATH . 'wp-admin/includes/media.php'); // needed for media_handle_sideload during cron + require_once( ABSPATH . 'wp-admin/includes/image.php'); // needed for wp_read_image_metadata used by media_handle_sideload during cron $id = media_handle_sideload( $file_array, $post_id, $image->title->value ); // If error storing permanently, unlink if ( is_wp_error($id) ) { From 8dc1adcf87e701bb6eaa4729b441866f1359c29c Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Wed, 26 Sep 2018 16:37:42 -0400 Subject: [PATCH 05/10] Update when the save_post hook is run, because that was not done in https://github.com/npr/nprapi-wordpress/pull/47 This fixes the issue described in https://github.com/npr/nprapi-wordpress/issues/57, where the URL parameter ds_npr_update_push was removed along with the 'Push to NPR' submit button, but the URL parameter triggering the nprstory_api_push function was not changed. --- push_story.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/push_story.php b/push_story.php index 386949b..524b117 100644 --- a/push_story.php +++ b/push_story.php @@ -126,7 +126,7 @@ function nprstory_api_delete ( $post_ID ) { * Register nprstory_npr_push and nprstory_npr_delete on appropriate hooks * this is where the magic happens */ -if ( isset( $_POST['ds_npr_update_push'] ) ) { +if ( isset( $_POST['send_to_api'] ) ) { // No need to validate the ds_npr_update_push contents; we're checking only for its existence // permissions check is handled by nprstory_api_push add_action( 'save_post', 'nprstory_api_push', 10, 2 ); From c4745423ae80c319ed37295445c6860355f55e3a Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Wed, 26 Sep 2018 17:02:43 -0400 Subject: [PATCH 06/10] Fix Undefined variable message on "Get NPR Stories" page > PHP Notice: Undefined variable: story_id in /Users/blk/sites/nprapi/wp-content/plugins/nprapi-wordpress/get_stories.php on line 132 Caused by running is_numeric on a variable that isn't set. --- get_stories.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/get_stories.php b/get_stories.php index 438152c..b78eb62 100644 --- a/get_stories.php +++ b/get_stories.php @@ -129,7 +129,7 @@ function load_page_hook() { } // Don't do anything if $story_id isn't an ID - if ( is_numeric( $story_id ) ) { + if ( isset( $story_id ) && is_numeric( $story_id ) ) { // start the API class // todo: check that the API key is actually set $api = new NPRAPIWordpress(); From 1a55ebfea04b5565d44731f61218d0e841505cd3 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Wed, 26 Sep 2018 17:18:38 -0400 Subject: [PATCH 07/10] Fix for Non-static method DS_NPR_API::load_page_hook() should not be called statically Just had to change how the hook was registered, that's all. --- get_stories.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/get_stories.php b/get_stories.php index b78eb62..2e7deae 100644 --- a/get_stories.php +++ b/get_stories.php @@ -9,6 +9,11 @@ class DS_NPR_API { var $created_message = ''; + /** + * What is the post type that pulled stories should be created as? + * + * @return string The post type + */ public static function nprstory_get_pull_post_type() { $pull_post_type = get_option( 'ds_npr_pull_post_type' ); if ( empty( $pull_post_type ) ) { @@ -17,6 +22,9 @@ public static function nprstory_get_pull_post_type() { return $pull_post_type; } + /** + * The cron job to pull stories from the API + */ public static function nprstory_cron_pull() { // here we should get the list of IDs/full urls that need to be checked hourly //because this is run on cron, and may be fired off by an non-admin, we need to load a bunch of stuff @@ -79,7 +87,10 @@ public static function nprstory_cron_pull() { } } - function load_page_hook() { + /** + * Function to convert an alleged NPR story URL or ID into a story ID, then request it + */ + public function load_page_hook() { // find the input that is allegedly a story id // We validate these later if ( isset( $_POST ) && isset( $_POST[ 'story_id' ] ) ) { @@ -156,16 +167,22 @@ function load_page_hook() { } } - function __construct() { + /** + * Class constructor that hooks up the menu and the "Get NPR Stories" page action. + */ + public function __construct() { if ( ! is_admin() ) { 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() { - add_posts_page( 'Get NPR DS Stories', 'Get NPR Stories', 'edit_posts', 'get-npr-stories', 'nprstory_get_stories' ); + /** + * Register the admin menu for "Get NPR Stories" + */ + public function admin_menu() { + add_posts_page( 'Get NPR Stories', 'Get NPR Stories', 'edit_posts', 'get-npr-stories', 'nprstory_get_stories' ); } } From bd11818bcde85670863a5df0c12364a43a140ff2 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Wed, 26 Sep 2018 17:32:41 -0400 Subject: [PATCH 08/10] Modify test to reflect changed action load_page_hook --- tests/test-get_stories.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-get_stories.php b/tests/test-get_stories.php index 221e5ba..b18ff93 100644 --- a/tests/test-get_stories.php +++ b/tests/test-get_stories.php @@ -47,7 +47,7 @@ function test_DS_NPR_API() { # Re-create the $test_obj after setting admin context $test_obj = new DS_NPR_API; - $this->assertTrue( (bool) has_action( 'load-posts_page_get-npr-stories', array( 'DS_NPR_API', 'load_page_hook' ) ) ); + $this->assertTrue( (bool) has_action( 'load-posts_page_get-npr-stories', array( $test_obj, 'load_page_hook' ) ) ); $this->assertTrue( (bool) has_action( 'admin_menu', array( &$test_obj, 'admin_menu' ) ) ); # Restore globals From 5271e25b378c02a44f915bff1a7d7d4cc1d348ff Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Wed, 26 Sep 2018 17:39:04 -0400 Subject: [PATCH 09/10] Update changelog. --- readme.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readme.txt b/readme.txt index 46eee47..e79b649 100644 --- a/readme.txt +++ b/readme.txt @@ -75,8 +75,11 @@ NPR Stories having got gotten = [unreleased](https://github.com/npr/nprapi-wordpress/compare/1.7.1...HEAD) = +* Fixes issue preventing pushing to the API, introduced in V1.7. [PR #60](https://github.com/npr/nprapi-wordpress/pull/60) for [issue #57](https://github.com/npr/nprapi-wordpress/issues/57). +* Fixes issue where images were not properly sideloaded. [PR #60](https://github.com/npr/nprapi-wordpress/pull/60) for [issue #59](https://github.com/npr/nprapi-wordpress/issues/59). * Fixes invalid GMT offset error when creating new DateTimeZone object in post metabox. [PR #53](https://github.com/npr/nprapi-wordpress/pull/53) for [issue #52](https://github.com/npr/nprapi-wordpress/issues/52). * When interacting with a site using the plugin in an unconfigured state, users will be prompted to set the NPR API Push URL. [PR #56](https://github.com/npr/nprapi-wordpress/pull/56) for [issue #51](https://github.com/npr/nprapi-wordpress/issues/51). +* Miscellaneous code quality improvements. = V1.7.1 = From 63c25f0f5b6c4ec2d1da436bd202be803c885461 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Wed, 26 Sep 2018 19:34:00 -0400 Subject: [PATCH 10/10] Initialize a variable, that will later be used as an array, as an array. --- push_story.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/push_story.php b/push_story.php index 524b117..a964698 100644 --- a/push_story.php +++ b/push_story.php @@ -452,7 +452,7 @@ function nprstory_get_push_post_type() { } function nprstory_get_permission_groups(){ - $perm_groups = ''; + $perm_groups = array(); //query the API for the lists for this org. $perm_url = get_option( 'ds_npr_api_push_url' ) . '/orgs/' . get_option( 'ds_npr_api_org_id' ) . '/groups' . '?apiKey=' . get_option('ds_npr_api_key'); $http_result = wp_remote_get( $perm_url );