From b1c8228381ac2d37e96a802050579a9e9884eb76 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Mon, 19 Mar 2018 12:40:12 -0400 Subject: [PATCH 01/33] Add text domain to plugin readme.txt For https://github.com/npr/nprapi-wordpress/issues/26 --- readme.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.txt b/readme.txt index 64ba4e4..dc1a2a1 100644 --- a/readme.txt +++ b/readme.txt @@ -7,6 +7,7 @@ Tested up to: 4.5.2 Stable tag: 1.6 License: GPLv2 License URI: http://www.gnu.org/licenses/gpl-2.0.html +Text Domain: nprapi A collection of tools for reusing content from NPR.org supplied by Digital Services. From 654bae04ddb29fc98b5e686b520af2dbae6915bd Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Mon, 19 Mar 2018 12:53:23 -0400 Subject: [PATCH 02/33] Clean up meta-boxes.php Applies code standards and text domain. --- meta-boxes.php | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/meta-boxes.php b/meta-boxes.php index 631ba7b..302e6e5 100644 --- a/meta-boxes.php +++ b/meta-boxes.php @@ -1,20 +1,36 @@ post_status != 'publish'); - if ($is_disabled) { - $helper_text = 'Publish this story in order to push it to NPR.'; +/** + * Output the NPR Story API publishing options metabox for the edit page admin interface + * + * @param WP_Post $post the WordPress post object. + */ +function nprstory_publish_meta_box( $post ) { + $helper_text = __( 'Push this story to NPR:', 'nprapi' ); + $is_disabled = ( 'post' !== $post->post_status ); + if ( $is_disabled ) { + $helper_text = __( 'Publish this story in order to push it to NPR.', 'nprapi' ); } - $attrs = array('id' => 'ds-npr-update-push'); - if ($is_disabled) + $attrs = array( 'id' => 'ds-npr-update-push' ); + if ( $is_disabled ) { $attrs['disabled'] = 'disabled'; -?> + } + + ?>
-

- +

+
Date: Mon, 19 Mar 2018 14:06:05 -0400 Subject: [PATCH 03/33] Add useless check boxes to the NPR Story API metabox, enqueue a stylesheet for same --- assets/css/meta-box.css | 6 ++++++ ds-npr-api.php | 2 ++ meta-boxes.php | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 assets/css/meta-box.css diff --git a/assets/css/meta-box.css b/assets/css/meta-box.css new file mode 100644 index 0000000..0145706 --- /dev/null +++ b/assets/css/meta-box.css @@ -0,0 +1,6 @@ +/** + * Styles for the NPR Story API meta box + */ +#ds_npr_document_meta ul ul { + padding-left: 24px; +} diff --git a/ds-npr-api.php b/ds-npr-api.php index e646a47..13a263a 100644 --- a/ds-npr-api.php +++ b/ds-npr-api.php @@ -49,6 +49,8 @@ define( 'NPR_POST_TYPE', 'npr_story_post' ); +define( 'NPRSTORY_PLUGIN_URL', plugin_dir_url(__FILE__) ); + // Load files define( 'NPRSTORY_PLUGIN_DIR', plugin_dir_path(__FILE__) ); require_once( NPRSTORY_PLUGIN_DIR . 'settings.php' ); diff --git a/meta-boxes.php b/meta-boxes.php index 302e6e5..ded35ab 100644 --- a/meta-boxes.php +++ b/meta-boxes.php @@ -19,8 +19,31 @@ function nprstory_publish_meta_box( $post ) { $attrs['disabled'] = 'disabled'; } + wp_enqueue_style( 'nprstory_publish_meta_box_stylesheet' ); + ?>
+
    + ', + __( 'Send to NPR API', 'nprapi' ) + ); + printf( + '
  • ', + __( 'Include for reading on NPR.org', 'nprapi' ) + ); + printf( + '
  • ', + __( 'Include for listening in NPR One', 'nprapi' ) + ); + printf( + '
  • ', + __( 'Set as featured story in NPR One', 'nprapi' ) + ); + ?> +
+

Date: Mon, 19 Mar 2018 23:22:37 -0400 Subject: [PATCH 04/33] Create the checkboxes in the NPR Story API box and make sure they save correctly. --- meta-boxes.php | 43 ++++++++++++++------ push_story.php | 106 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 116 insertions(+), 33 deletions(-) diff --git a/meta-boxes.php b/meta-boxes.php index ded35ab..3abdfaf 100644 --- a/meta-boxes.php +++ b/meta-boxes.php @@ -7,14 +7,20 @@ * Output the NPR Story API publishing options metabox for the edit page admin interface * * @param WP_Post $post the WordPress post object. + * @see nprstory_save_send_to_api + * @see nprstory_save_send_to_org + * @see nprstory_save_send_to_one + * @see nprstory_save_nprone_featured */ function nprstory_publish_meta_box( $post ) { $helper_text = __( 'Push this story to NPR:', 'nprapi' ); - $is_disabled = ( 'post' !== $post->post_status ); + $is_disabled = ( 'publish' !== $post->post_status ); + $attrs = array( 'id' => 'ds-npr-update-push' ); + if ( $is_disabled ) { $helper_text = __( 'Publish this story in order to push it to NPR.', 'nprapi' ); } - $attrs = array( 'id' => 'ds-npr-update-push' ); + if ( $is_disabled ) { $attrs['disabled'] = 'disabled'; } @@ -25,22 +31,35 @@ function nprstory_publish_meta_box( $post ) {
    ', - __( 'Send to NPR API', 'nprapi' ) - ); - printf( - '
  • ', - __( 'Include for reading on NPR.org', 'nprapi' ) + '
  • ', + __( 'Send to NPR API', 'nprapi' ), + checked( get_post_meta( $post->ID, '_send_to_nprone', true ), '1', false ) + // @see nprstory_save_send_to_api for a historical note on this metadata name ); + + // send to npr dot org printf( - '
  • ', - __( 'Include for listening in NPR One', 'nprapi' ) + '
  • ', + __( 'Include for reading on NPR.org', 'nprapi' ), + checked( get_post_meta( $post->ID, '_send_to_org', true ), '1', false ) ); + + // send to nprone printf( - '
  • ', - __( 'Set as featured story in NPR One', 'nprapi' ) + '
  • %3$s
  • ', + __( 'Include for listening in NPR One', 'nprapi' ), + checked( get_post_meta( $post->ID, '_send_to_one', true ), '1', false ), + // the following is an ul li within the "Send to npr one" li + // set the story as featured in NPR One + sprintf( + '
    ', + __( 'Set as featured story in NPR One', 'nprapi' ), + checked( get_post_meta( $post->ID, '_nprone_featured', true ), '1', false ) + ) ); + ?>

diff --git a/push_story.php b/push_story.php index c9fa5a2..740796c 100644 --- a/push_story.php +++ b/push_story.php @@ -7,14 +7,14 @@ * * Limited to users that can publish posts * - * @param unknown_type $post_ID - * @param unknown_type $post + * @param Int $post_ID + * @param WP_Post $post */ function nprstory_api_push ( $post_ID, $post ) { if ( ! current_user_can( 'publish_posts' ) ) { wp_die( - __('You do not have permission to publish posts, and therefore you do not have permission to push posts to the NPR API.'), - __('NPR Story API Error'), + __( 'You do not have permission to publish posts, and therefore you do not have permission to push posts to the NPR API.', 'nprapi' ), + __( 'NPR Story API Error', 'nprapi' ), 403 ); } @@ -528,26 +528,90 @@ function nprstory_bulk_action_push_action() { //exit(); } -add_action( 'post_submitbox_misc_actions', 'nprstory_submitbox_send_to_nprone' ); -function nprstory_submitbox_send_to_nprone() { - global $post; - if ( get_post_type( $post ) == get_option( 'ds_npr_push_post_type' ) ) { - $value = get_post_meta( $post->ID, '_send_to_nprone', true ); - $checked = ! empty( $value ) ? ' checked="checked" ' : ''; - echo '
'; - } +/** + * Save the "send to the API" metadata + * + * The meta name here is '_send_to_nprone' for backwards compatibility with plugin versions 1.6 and prior + * + * @param Int $post_ID The post ID of the post we're saving + * @since 1.6 at least + * @see nprstory_publish_meta_box + */ +function nprstory_save_send_to_api( $post_ID ) { + // safety checks + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false; + if ( ! current_user_can( 'edit_page', $post_ID ) ) return false; + if ( empty( $post_ID ) ) return false; + + global $post; + + if ( get_post_type($post) != get_option('ds_npr_push_post_type') ) return false; + $value = ( isset( $_POST['send_to_api'] ) && $_POST['send_to_api'] == 1 ) ? 1 : 0; + + // see historical note + update_post_meta( $post_ID, '_send_to_nprone', $value ); } +add_action( 'save_post', 'nprstory_save_send_to_api'); + +/** + * Save the "send to NPR.org" metadata + * + * @param Int $post_ID The post ID of the post we're saving + * @since 1.7 + */ +function nprstory_save_send_to_org( $post_ID ) { + // safety checks + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false; + if ( ! current_user_can( 'edit_page', $post_ID ) ) return false; + if ( empty( $post_ID ) ) return false; + + global $post; + + if ( get_post_type($post) != get_option('ds_npr_push_post_type') ) return false; + $value = ( isset( $_POST['send_to_org'] ) && $_POST['send_to_org'] == 1 ) ? 1 : 0; + update_post_meta( $post_ID, '_send_to_org', $value ); +} +add_action( 'save_post', 'nprstory_save_send_to_org'); + +/** + * Save the "Send to NPR One" metadata + * + * @param Int $post_ID The post ID of the post we're saving + * @since 1.7 + */ +function nprstory_save_send_to_one( $post_ID ) { + // safety checks + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false; + if ( ! current_user_can( 'edit_page', $post_ID ) ) return false; + if ( empty( $post_ID ) ) return false; + + global $post; + + if ( get_post_type($post) != get_option('ds_npr_push_post_type') ) return false; + $value = ( isset( $_POST['send_to_one'] ) && $_POST['send_to_one'] == 1 ) ? 1 : 0; + update_post_meta( $post_ID, '_send_to_one', $value ); +} +add_action( 'save_post', 'nprstory_save_send_to_one'); + +/** + * Save the "NPR One Featured" metadata + * + * @param Int $post_ID The post ID of the post we're saving + * @since 1.7 + */ +function nprstory_save_nprone_featured( $post_ID ) { + // safety checks + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false; + if ( ! current_user_can( 'edit_page', $post_ID ) ) return false; + if ( empty( $post_ID ) ) return false; + + global $post; -add_action( 'save_post', 'nprstory_save_send_to_nprone'); -function nprstory_save_send_to_nprone( $post_ID ) { - global $post; - if ( get_post_type($post) != get_option('ds_npr_push_post_type') ) return false; - 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 = ( isset( $_POST['send_to_nprone'] ) && $_POST['send_to_nprone'] == 1 ) ? 1 : 0; - update_post_meta( $post_ID, '_send_to_nprone', $value ); + if ( get_post_type($post) != get_option('ds_npr_push_post_type') ) return false; + $value = ( isset( $_POST['nprone_featured'] ) && $_POST['nprone_featured'] == 1 ) ? 1 : 0; + update_post_meta( $post_ID, '_nprone_featured', $value ); } +add_action( 'save_post', 'nprstory_save_nprone_featured'); /** * Add an admin notice to the post editor with the post's error message if it exists From 2eaf1c6da0cbc41fdd1799dd37ec21f97c10e986 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Tue, 20 Mar 2018 00:03:28 -0400 Subject: [PATCH 05/33] default the 'send to npr api' box to checked --- meta-boxes.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/meta-boxes.php b/meta-boxes.php index 3abdfaf..b10ccc1 100644 --- a/meta-boxes.php +++ b/meta-boxes.php @@ -32,10 +32,12 @@ function nprstory_publish_meta_box( $post ) {
    ID, '_send_to_nprone', true ); // 0 or 1 + if ( '0' !== $nprapi && '1' !== $nprapi ) { $nprapi = 1; } // defaults to checked; unset on new posts printf( '
  • ', __( 'Send to NPR API', 'nprapi' ), - checked( get_post_meta( $post->ID, '_send_to_nprone', true ), '1', false ) + checked( $nprapi, '1', false ) // @see nprstory_save_send_to_api for a historical note on this metadata name ); From 099130b21ce6252fff5f4c1f1b9b0b032ab59e35 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Thu, 22 Mar 2018 16:58:13 -0400 Subject: [PATCH 06/33] Checkbox validation javascript --- assets/css/meta-box.css | 3 ++ assets/js/meta-box.js | 74 +++++++++++++++++++++++++++++++++++++++++ meta-boxes.php | 20 +++++++++-- 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 assets/js/meta-box.js diff --git a/assets/css/meta-box.css b/assets/css/meta-box.css index 0145706..d15d02d 100644 --- a/assets/css/meta-box.css +++ b/assets/css/meta-box.css @@ -2,5 +2,8 @@ * Styles for the NPR Story API meta box */ #ds_npr_document_meta ul ul { + margin-top: 6px; +} +#ds_npr_document_meta ul ul ul { padding-left: 24px; } diff --git a/assets/js/meta-box.js b/assets/js/meta-box.js new file mode 100644 index 0000000..f170cb7 --- /dev/null +++ b/assets/js/meta-box.js @@ -0,0 +1,74 @@ +/** + * NPR Story API meta box functions and features + */ +document.addEventListener('DOMContentLoaded', () => { + $ = jQuery; + + // contains the inputs + $container = $( '#ds-npr-publish-actions' ); + + // cached input values, based on the default values of the form + c_api = $container.find( '#send_to_api' ).prop( 'checked' ); + c_org = $container.find( '#send_to_org' ).prop( 'checked' ); + c_one = $container.find( '#send_to_one' ).prop( 'checked' ); + c_featured = $container.find( '#nprone_featured' ).prop( 'checked' ); + + // Set up the checkboxes once everything is loaded + validitycheck( null ); + + // initialization: go through the list of checkboxes and uncheck them if their values are not valid + function validitycheck( event ) { + $api = $container.find( '#send_to_api' ); + $org = $container.find( '#send_to_org' ); + $one = $container.find( '#send_to_one' ); + $featured = $container.find( '#nprone_featured' ); + + // start at the top of the form and work our way down + if ( $api.prop( 'checked' ) !== c_api ) { + c_api = $api.prop( 'checked' ); + } + + // uncheck lower checkboxes if they are invalid + if ( false === c_api ) { + $org.prop( 'checked', false ).prop( 'disabled', true ); + $one.prop( 'checked', false ).prop( 'disabled', true ); + $featured.prop( 'checked', false ).prop( 'disabled', true ); + c_org = false; + c_one = false; + } else { + $org.prop( 'disabled', false ); + $one.prop( 'disabled', false ); + // $featured will be checked or unchecked by the value of $one later in this initialization function + } + + // start at the top of the form and work our way down + c_one = $one.prop( 'checked' ); + + // change the disabled state of the featured checkbox + if ( true === c_one ) { + $featured.prop( 'disabled', false ); + } + } + + // Upon update, do the thing + $container.find( 'input' ).on( 'change', li_checking ); + + /* + * If a checkbox in an li gets unchecked, uncheck and disable its child li + * If a checkbox in an li gets checked, enable its child li + */ + function li_checking( event ) { + checked = this.checked; + $results = $( this ).closest( 'li' ).children( 'ul' ).children( 'li' ); // only get the first level of lis + console.log( $results ); + $results.each( function( element ) { + if ( checked ) { + $( this ).children( 'label' ).children( 'input' ).prop( 'disabled', false ); + // in this case there is no need to trigger the change event on the input, because the children will be updated when the parent's box is checked + } else { + $( this ).children( 'label' ).children( 'input' ).prop( 'disabled', true ).prop( 'checked', false ).trigger( 'change' ); + // here, though, we need to invalidate the children when their parent changes, so here we trigger the change check. + } + }); + } +}); diff --git a/meta-boxes.php b/meta-boxes.php index b10ccc1..ab77e6f 100644 --- a/meta-boxes.php +++ b/meta-boxes.php @@ -26,6 +26,7 @@ function nprstory_publish_meta_box( $post ) { } wp_enqueue_style( 'nprstory_publish_meta_box_stylesheet' ); + wp_enqueue_script( 'nprstory_publish_meta_box_script' ); ?>
    @@ -34,13 +35,18 @@ function nprstory_publish_meta_box( $post ) { // send to the npr api $nprapi = get_post_meta( $post->ID, '_send_to_nprone', true ); // 0 or 1 if ( '0' !== $nprapi && '1' !== $nprapi ) { $nprapi = 1; } // defaults to checked; unset on new posts + + // this list item contains all other list items, because their enabled/disabled depends on this checkbox + echo '
  • '; printf( - '
  • ', + '', __( 'Send to NPR API', 'nprapi' ), checked( $nprapi, '1', false ) // @see nprstory_save_send_to_api for a historical note on this metadata name ); + echo '
      '; + // send to npr dot org printf( '
    • ', @@ -61,6 +67,7 @@ function nprstory_publish_meta_box( $post ) { checked( get_post_meta( $post->ID, '_nprone_featured', true ), '1', false ) ) ); + echo ''; // end the "Send to NPR API" list item ?>
    @@ -82,10 +89,17 @@ function nprstory_publish_meta_box( $post ) { /** * Register stylesheet for the NPR Story API publishing options metabox */ -function nprstory_publish_meta_box_styles() { +function nprstory_publish_meta_box_assets() { wp_register_style( 'nprstory_publish_meta_box_stylesheet', NPRSTORY_PLUGIN_URL . 'assets/css/meta-box.css' ); + wp_register_script( + 'nprstory_publish_meta_box_script', + NPRSTORY_PLUGIN_URL . 'assets/js/meta-box.js', + array( 'jquery' ), + null, + true + ); } -add_action( 'admin_enqueue_scripts', 'nprstory_publish_meta_box_styles' ); +add_action( 'admin_enqueue_scripts', 'nprstory_publish_meta_box_assets' ); From d9a7740d90225a67649e2aa7514a8c1d42dac47c Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Thu, 22 Mar 2018 17:20:49 -0400 Subject: [PATCH 07/33] Better recursion, cleaner form validation javascript --- assets/js/meta-box.js | 58 ++++++++----------------------------------- 1 file changed, 11 insertions(+), 47 deletions(-) diff --git a/assets/js/meta-box.js b/assets/js/meta-box.js index f170cb7..65a7e95 100644 --- a/assets/js/meta-box.js +++ b/assets/js/meta-box.js @@ -7,51 +7,11 @@ document.addEventListener('DOMContentLoaded', () => { // contains the inputs $container = $( '#ds-npr-publish-actions' ); - // cached input values, based on the default values of the form - c_api = $container.find( '#send_to_api' ).prop( 'checked' ); - c_org = $container.find( '#send_to_org' ).prop( 'checked' ); - c_one = $container.find( '#send_to_one' ).prop( 'checked' ); - c_featured = $container.find( '#nprone_featured' ).prop( 'checked' ); - - // Set up the checkboxes once everything is loaded - validitycheck( null ); - - // initialization: go through the list of checkboxes and uncheck them if their values are not valid - function validitycheck( event ) { - $api = $container.find( '#send_to_api' ); - $org = $container.find( '#send_to_org' ); - $one = $container.find( '#send_to_one' ); - $featured = $container.find( '#nprone_featured' ); - - // start at the top of the form and work our way down - if ( $api.prop( 'checked' ) !== c_api ) { - c_api = $api.prop( 'checked' ); - } - - // uncheck lower checkboxes if they are invalid - if ( false === c_api ) { - $org.prop( 'checked', false ).prop( 'disabled', true ); - $one.prop( 'checked', false ).prop( 'disabled', true ); - $featured.prop( 'checked', false ).prop( 'disabled', true ); - c_org = false; - c_one = false; - } else { - $org.prop( 'disabled', false ); - $one.prop( 'disabled', false ); - // $featured will be checked or unchecked by the value of $one later in this initialization function - } - - // start at the top of the form and work our way down - c_one = $one.prop( 'checked' ); - - // change the disabled state of the featured checkbox - if ( true === c_one ) { - $featured.prop( 'disabled', false ); - } - } + // initialize the form + $container.find( 'input' ).on( 'change', li_checking ); // Upon update, do the thing - $container.find( 'input' ).on( 'change', li_checking ); + li_checking.call( $container.find( '#send_to_api' ) ); /* * If a checkbox in an li gets unchecked, uncheck and disable its child li @@ -59,15 +19,19 @@ document.addEventListener('DOMContentLoaded', () => { */ function li_checking( event ) { checked = this.checked; - $results = $( this ).closest( 'li' ).children( 'ul' ).children( 'li' ); // only get the first level of lis + $results = $( this ).closest( 'li' ).children( 'ul' ).children( 'li' ); // Only get the first level of list. console.log( $results ); $results.each( function( element ) { if ( checked ) { $( this ).children( 'label' ).children( 'input' ).prop( 'disabled', false ); - // in this case there is no need to trigger the change event on the input, because the children will be updated when the parent's box is checked + // In this case there is no need to trigger the change event on the input, + // because the children will be updated when the parent's box is checked. } else { - $( this ).children( 'label' ).children( 'input' ).prop( 'disabled', true ).prop( 'checked', false ).trigger( 'change' ); - // here, though, we need to invalidate the children when their parent changes, so here we trigger the change check. + recurse = $( this ).children( 'label' ).children( 'input' ).prop( 'disabled', true ).prop( 'checked', false ); + li_checking.call( recurse ); + // Here, though, we need to invalidate the children when their parent changes, + // so here we call this function on the appropriate child. + // Triggering the change event on the child does not work. } }); } From 2896c9b93dc362720f670dbee4573112d0da9c52 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Thu, 22 Mar 2018 17:21:35 -0400 Subject: [PATCH 08/33] Metabox validation is less DRY than form javascript. --- push_story.php | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/push_story.php b/push_story.php index 740796c..dbc282c 100644 --- a/push_story.php +++ b/push_story.php @@ -556,6 +556,8 @@ function nprstory_save_send_to_api( $post_ID ) { /** * Save the "send to NPR.org" metadata * + * If the send_to_api value is falsy, then this should not be saved as truthy + * * @param Int $post_ID The post ID of the post we're saving * @since 1.7 */ @@ -568,7 +570,12 @@ function nprstory_save_send_to_org( $post_ID ) { global $post; if ( get_post_type($post) != get_option('ds_npr_push_post_type') ) return false; - $value = ( isset( $_POST['send_to_org'] ) && $_POST['send_to_org'] == 1 ) ? 1 : 0; + $value = ( + isset( $_POST['send_to_org'] ) + && $_POST['send_to_org'] == 1 + && isset( $_POST['send_to_api'] ) + && $_POST['send_to_api'] == 1 + ) ? 1 : 0; update_post_meta( $post_ID, '_send_to_org', $value ); } add_action( 'save_post', 'nprstory_save_send_to_org'); @@ -576,6 +583,8 @@ function nprstory_save_send_to_org( $post_ID ) { /** * Save the "Send to NPR One" metadata * + * If the send_to_api value is falsy, then this should not be saved as truthy + * * @param Int $post_ID The post ID of the post we're saving * @since 1.7 */ @@ -588,7 +597,12 @@ function nprstory_save_send_to_one( $post_ID ) { global $post; if ( get_post_type($post) != get_option('ds_npr_push_post_type') ) return false; - $value = ( isset( $_POST['send_to_one'] ) && $_POST['send_to_one'] == 1 ) ? 1 : 0; + $value = ( + isset( $_POST['send_to_one'] ) + && $_POST['send_to_one'] == 1 + && isset( $_POST['send_to_api'] ) + && $_POST['send_to_api'] == 1 + ) ? 1 : 0; update_post_meta( $post_ID, '_send_to_one', $value ); } add_action( 'save_post', 'nprstory_save_send_to_one'); @@ -596,6 +610,9 @@ function nprstory_save_send_to_one( $post_ID ) { /** * Save the "NPR One Featured" metadata * + * If the send_to_one value is falsy, then this should not be saved as truthy + * And thus, if the send_to_api value is falsy, then this should not be saved as truthy + * * @param Int $post_ID The post ID of the post we're saving * @since 1.7 */ @@ -608,7 +625,14 @@ function nprstory_save_nprone_featured( $post_ID ) { global $post; if ( get_post_type($post) != get_option('ds_npr_push_post_type') ) return false; - $value = ( isset( $_POST['nprone_featured'] ) && $_POST['nprone_featured'] == 1 ) ? 1 : 0; + $value = ( + isset( $_POST['nprone_featured'] ) + && $_POST['nprone_featured'] == 1 + && isset( $_POST['send_to_api'] ) + && $_POST['send_to_api'] == 1 + && isset( $_POST['send_to_one'] ) + && $_POST['send_to_one'] == 1 + ) ? 1 : 0; update_post_meta( $post_ID, '_nprone_featured', $value ); } add_action( 'save_post', 'nprstory_save_nprone_featured'); From 445b8e22ab62d56cf54c397cae174a0e840f816f Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Thu, 22 Mar 2018 17:38:39 -0400 Subject: [PATCH 09/33] console.log --- assets/js/meta-box.js | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/js/meta-box.js b/assets/js/meta-box.js index 65a7e95..4b6dd91 100644 --- a/assets/js/meta-box.js +++ b/assets/js/meta-box.js @@ -20,7 +20,6 @@ document.addEventListener('DOMContentLoaded', () => { function li_checking( event ) { checked = this.checked; $results = $( this ).closest( 'li' ).children( 'ul' ).children( 'li' ); // Only get the first level of list. - console.log( $results ); $results.each( function( element ) { if ( checked ) { $( this ).children( 'label' ).children( 'input' ).prop( 'disabled', false ); From dc50455704bb37ad366b92e1d431b338661380c9 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Thu, 22 Mar 2018 18:05:56 -0400 Subject: [PATCH 10/33] Date picker boilerplate elements --- meta-boxes.php | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/meta-boxes.php b/meta-boxes.php index ab77e6f..a14d611 100644 --- a/meta-boxes.php +++ b/meta-boxes.php @@ -71,17 +71,42 @@ function nprstory_publish_meta_box( $post ) { ?>
-
-

- +
+ +
+
+ Expires on: + + +
+
Date: Fri, 23 Mar 2018 00:25:13 -0400 Subject: [PATCH 11/33] Improvements to the presentation of the datepicker form, but no progress on saving it yet. --- assets/css/meta-box.css | 25 +++++++++++++++++++++++++ assets/js/meta-box.js | 22 ++++++++++++++++++++++ meta-boxes.php | 17 ++++++++--------- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/assets/css/meta-box.css b/assets/css/meta-box.css index d15d02d..aeb1ee3 100644 --- a/assets/css/meta-box.css +++ b/assets/css/meta-box.css @@ -7,3 +7,28 @@ #ds_npr_document_meta ul ul ul { padding-left: 24px; } +#nprone-expiry { + border-top: 1px solid #eee; + padding-top: 6px; +} +#nprone-expiry-form, +#nprone-expiry-form select, +#nprone-expiry-form input { + font-size: 12px; + padding-left: 0; + padding-right: 0; +} +#nprone-expiry-form .row { + clear: both; + margin-top: 6px; + line-height: 28px; +} +button.link-effect { + border: none; + color: #0073aa; + text-decoration: underline; +} +#nprone-expiry-month { + vertical-align: baseline; + margin: 0; +} diff --git a/assets/js/meta-box.js b/assets/js/meta-box.js index 4b6dd91..66a3d5f 100644 --- a/assets/js/meta-box.js +++ b/assets/js/meta-box.js @@ -34,4 +34,26 @@ document.addEventListener('DOMContentLoaded', () => { } }); } + + // edit the time selector + $( '#nprone-expiry-edit' ).on( 'click', function( event ) { + event.preventDefault(); + $( '#nprone-expiry-form' ).toggleClass( 'hidden' ); + $( this ).toggleClass( 'hidden' ); + }); + // close the time selector + $( '#nprone-expiry-cancel' ).on( 'click', function( event ) { + event.preventDefault(); + $( '#nprone-expiry-form' ).toggleClass( 'hidden' ); + $( '#nprone-expiry-edit' ).toggleClass( 'hidden' ); + }); + // save the time selector + $( '#nprone-expiry-save' ).on( 'click', function( event ) { + event.preventDefault(); + $( '#nprone-expiry-form' ).toggleClass( 'hidden' ); + $( '#nprone-expiry-edit' ).toggleClass( 'hidden' ); + + // but then it needs to update the displayed data in #nprone-expiry-display. How is it to do that? + // Is there an underscore template that we can fill with the data? + }); }); diff --git a/meta-boxes.php b/meta-boxes.php index a14d611..27e0438 100644 --- a/meta-boxes.php +++ b/meta-boxes.php @@ -93,19 +93,18 @@ function nprstory_publish_meta_box( $post ) { - + - - , - + , + @ - - : - + : - - +
+ + +
Date: Fri, 23 Mar 2018 15:57:06 -0400 Subject: [PATCH 12/33] enqueue jquery-ui-datepicker --- meta-boxes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meta-boxes.php b/meta-boxes.php index 27e0438..353ae19 100644 --- a/meta-boxes.php +++ b/meta-boxes.php @@ -121,7 +121,7 @@ function nprstory_publish_meta_box_assets() { wp_register_script( 'nprstory_publish_meta_box_script', NPRSTORY_PLUGIN_URL . 'assets/js/meta-box.js', - array( 'jquery' ), + array( 'jquery', 'jquery-ui-datepicker' ), null, true ); From 776d6f14277f0ed10b368cb2b042caed8027a791 Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Fri, 23 Mar 2018 21:25:42 -0400 Subject: [PATCH 13/33] Add the jquery-ui datepicker --- assets/css/meta-box.css | 29 +++++++++++++++++++++++++++++ assets/js/meta-box.js | 6 ++++++ meta-boxes.php | 22 ++++++---------------- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/assets/css/meta-box.css b/assets/css/meta-box.css index aeb1ee3..6e1da55 100644 --- a/assets/css/meta-box.css +++ b/assets/css/meta-box.css @@ -32,3 +32,32 @@ button.link-effect { vertical-align: baseline; margin: 0; } + +/* + * Minimal Datepicker styles + */ +#ui-datepicker-div { + background: #fff; + border: 1px solid #e5e5e5; + padding: 6px; + box-shadow: 0 3px 5px rgba(0,0,0,.2); +} +.ui-datepicker .ui-datepicker-header { + border: none; +} +.ui-datepicker-calendar .ui-state-default { + text-decoration: none; + background-color: transparent; +} +.ui-datepicker .ui-datepicker-current-day a { + border-color: #0073aa #006799 #006799; + background: #0085ba; + color: #fff; + font-weight: bold; +} +.ui-datepicker .ui-datepicker-today a { + background: #fff; + border: 1px solid #c5c5c5; + font-weight: bold; + color: #000; +} diff --git a/assets/js/meta-box.js b/assets/js/meta-box.js index 66a3d5f..0965cbb 100644 --- a/assets/js/meta-box.js +++ b/assets/js/meta-box.js @@ -56,4 +56,10 @@ document.addEventListener('DOMContentLoaded', () => { // but then it needs to update the displayed data in #nprone-expiry-display. How is it to do that? // Is there an underscore template that we can fill with the data? }); + + + // Activate the date picker + $( '#nprone-expiry-datepicker' ).datepicker({ + dateFormat: 'yy-mm-dd' + }); }); diff --git a/meta-boxes.php b/meta-boxes.php index 353ae19..4b7253c 100644 --- a/meta-boxes.php +++ b/meta-boxes.php @@ -25,6 +25,7 @@ function nprstory_publish_meta_box( $post ) { $attrs['disabled'] = 'disabled'; } + wp_enqueue_style( 'jquery-ui' ); wp_enqueue_style( 'nprstory_publish_meta_box_stylesheet' ); wp_enqueue_script( 'nprstory_publish_meta_box_script' ); @@ -82,22 +83,7 @@ function nprstory_publish_meta_box( $post ) {