From 6599205292cd0069681f24610937d1278fb8c1a8 Mon Sep 17 00:00:00 2001 From: matheus Date: Sat, 11 Jul 2015 15:33:45 -0300 Subject: [PATCH 01/11] add post status class #266 --- core/assets/js/admin-custom-status.js | 28 +++++ core/classes/class-metabox.php | 1 + core/classes/class-post-status.php | 154 ++++++++++++++++++++++++++ functions.php | 1 + 4 files changed, 184 insertions(+) create mode 100644 core/assets/js/admin-custom-status.js create mode 100644 core/classes/class-post-status.php diff --git a/core/assets/js/admin-custom-status.js b/core/assets/js/admin-custom-status.js new file mode 100644 index 0000000..2629e30 --- /dev/null +++ b/core/assets/js/admin-custom-status.js @@ -0,0 +1,28 @@ +(function ( $ ) { + 'use strict'; + + /** + * Custom post status + */ + $( window ).load( function() { + $( 'meta.odin-custom-status-meta' ).each( function() { + if( $( document.body ).hasClass( 'post-php' ) || $( document.body ).hasClass( 'post-new-php' ) ) { + console.log('ahoy'); + var select = ''; + if( typeof args.select !== 'undefined' ) { + select = 'selected="selected"'; + $( 'label[for="post_status"]').append( ' ' + $.trim( args.appliedLabel ) + '' ); + } + var html = ''; + $( '#post_status' ).append( html ); + } + if( $( document.body ).hasClass( 'edit-php' ) ) { + var args = $.parseJSON( $(this).attr('value') ); + var html = ''; + $( '.inline-edit-status select' ).each(function(){ + $(this).append( html ); + }); + } + }); + }); +}( jQuery )); diff --git a/core/classes/class-metabox.php b/core/classes/class-metabox.php index 4ac650f..557273b 100644 --- a/core/classes/class-metabox.php +++ b/core/classes/class-metabox.php @@ -1,3 +1,4 @@ + post_status = $post_status; + $this->post_types = $post_types; + $this->action_label = isset( $args['label'] ) ? $args['label'] : $post_status; + $this->applied_label = isset( $args['applied_label'] ) ? $args['applied_label'] : $this->action_label; + $this->args = $args; + + // Removes the arguments that do not belong to register_post_type + unset( $this->args['applied_label'] ); + + if( ! isset( $this->args['label_count'] ) ) { + $this->args['label_count'] = _n_noop( $this->applied_label . ' (%s)', $this->applied_label . ' (%s)' ); + } + + // Register post status + add_action( 'init', array( $this, 'register_post_status' ) ); + + // Add meta tags to pass args + add_action( 'admin_head', array( $this, 'meta_tags' ) ); + + // Load scripts + add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) ); + + //add_action( 'admin_footer-post.php', array( $this, 'post_status_dropdown' ) ); + //add_action( 'admin_footer-edit.php', array( $this, 'inline_status_dropdown' ) ); + + // Display Post Status Text + //add_filter( 'display_post_states', array( $this, 'display_post_status_text' ) ); + } + + /** + * Register the Custom Post Status with Wordpress ;) + * + * @param string $post_status The name of Custom Post Status. + * @param array $args Array of arguments to pass register_post_status() + * + * @return void + **/ + public function register_post_status() { + register_post_status( $this->post_status, $this->args ); + } + + /** + * Add meta tags to JS + * + * @return void + */ + public function meta_tags() { + $screen = get_current_screen(); + if( ! in_array( $screen->post_type, $this->post_types ) ) + return; + + $args = array( + 'postTypes' => $this->post_types, + 'appliedLabel' => $this->applied_label, + 'slug' => $this->post_status, + ); + if( $screen->base === 'post' ) { + global $post; + if( is_object( $post ) && $post->post_status === $this->post_status ) { + $args['select'] = true; + } + } + printf( '', esc_attr( json_encode($args) ) ); + } + + /** + * Load post status scripts and inject JS vars + * + * @return void + */ + public function scripts() { + // Load admin JS + wp_enqueue_script( 'odin-custom-status', get_template_directory_uri() . '/core/assets/js/admin-custom-status.js', array( 'jquery' ), null, true ); + + } + + /** + * Update the text on edit.php to be more + * descriptive of the type of post + * + * @param array $states An array of post display states. + * @return void + **/ + public function display_post_status_text( $states ) { + global $post; + + $status = get_query_var('post_status'); + + if( $status !== $this->post_status && $post->post_status === $this->post_status ) + return array( $this->applied_label ); + + return $states; + } +} diff --git a/functions.php b/functions.php index e9c1f87..5ae7a5c 100644 --- a/functions.php +++ b/functions.php @@ -35,6 +35,7 @@ // require_once get_template_directory() . '/core/classes/class-contact-form.php'; // require_once get_template_directory() . '/core/classes/class-post-form.php'; // require_once get_template_directory() . '/core/classes/class-user-meta.php'; +// require_once get_template_directory() . '/core/classes/class-post-status.php'; /** * Odin Widgets. From e8f31854017788726a23a4d5e613c4b9eaec7db5 Mon Sep 17 00:00:00 2001 From: matheus Date: Sat, 11 Jul 2015 15:35:38 -0300 Subject: [PATCH 02/11] add post status class #266 --- core/classes/class-post-status.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/core/classes/class-post-status.php b/core/classes/class-post-status.php index 4cef382..d2cf1a0 100644 --- a/core/classes/class-post-status.php +++ b/core/classes/class-post-status.php @@ -80,11 +80,6 @@ public function __construct( $post_status, $post_types, $args ) { // Load scripts add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) ); - //add_action( 'admin_footer-post.php', array( $this, 'post_status_dropdown' ) ); - //add_action( 'admin_footer-edit.php', array( $this, 'inline_status_dropdown' ) ); - - // Display Post Status Text - //add_filter( 'display_post_states', array( $this, 'display_post_status_text' ) ); } /** From 02fb2e048bd0f16dbf127c3cfb148d8d15ef75a4 Mon Sep 17 00:00:00 2001 From: matheus Date: Sat, 11 Jul 2015 15:57:11 -0300 Subject: [PATCH 03/11] fix post status JS --- core/assets/js/admin-custom-status.js | 10 +++++----- core/classes/class-metabox.php | 1 - core/classes/class-post-status.php | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/core/assets/js/admin-custom-status.js b/core/assets/js/admin-custom-status.js index 2629e30..78e22ea 100644 --- a/core/assets/js/admin-custom-status.js +++ b/core/assets/js/admin-custom-status.js @@ -6,21 +6,21 @@ */ $( window ).load( function() { $( 'meta.odin-custom-status-meta' ).each( function() { + var meta = $( this ); if( $( document.body ).hasClass( 'post-php' ) || $( document.body ).hasClass( 'post-new-php' ) ) { - console.log('ahoy'); + var args = $.parseJSON( meta.attr( 'value' ) ); var select = ''; - if( typeof args.select !== 'undefined' ) { + if( ! args.select ) { select = 'selected="selected"'; - $( 'label[for="post_status"]').append( ' ' + $.trim( args.appliedLabel ) + '' ); + $( 'label[for="post_status"]' ).append( ' ' + $.trim( args.appliedLabel ) + '' ); } var html = ''; $( '#post_status' ).append( html ); } if( $( document.body ).hasClass( 'edit-php' ) ) { - var args = $.parseJSON( $(this).attr('value') ); var html = ''; $( '.inline-edit-status select' ).each(function(){ - $(this).append( html ); + meta.append( html ); }); } }); diff --git a/core/classes/class-metabox.php b/core/classes/class-metabox.php index 557273b..4ac650f 100644 --- a/core/classes/class-metabox.php +++ b/core/classes/class-metabox.php @@ -1,4 +1,3 @@ - Date: Sat, 11 Jul 2015 15:59:54 -0300 Subject: [PATCH 04/11] remove old method post status class --- core/classes/class-post-status.php | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/core/classes/class-post-status.php b/core/classes/class-post-status.php index 216f4bd..61aa481 100644 --- a/core/classes/class-post-status.php +++ b/core/classes/class-post-status.php @@ -61,7 +61,7 @@ public function __construct( $post_status, $post_types, $args ) { $this->post_status = $post_status; $this->post_types = $post_types; $this->action_label = isset( $args['label'] ) ? $args['label'] : $post_status; - $this->applied_label = isset( $args['applied_label'] ) ? $args['applied_label'] : $this->action_label; + $this->applied_label = isset( $args['applied_label'] ) ? $args['applied_label'] : $this->action_label; $this->args = $args; // Removes the arguments that do not belong to register_post_type @@ -128,22 +128,4 @@ public function scripts() { wp_enqueue_script( 'odin-custom-status', get_template_directory_uri() . '/core/assets/js/admin-custom-status.js', array( 'jquery' ), null, true ); } - - /** - * Update the text on edit.php to be more - * descriptive of the type of post - * - * @param array $states An array of post display states. - * @return void - **/ - public function display_post_status_text( $states ) { - global $post; - - $status = get_query_var('post_status'); - - if( $status !== $this->post_status && $post->post_status === $this->post_status ) - return array( $this->applied_label ); - - return $states; - } } From c32369c53739592ef5fe968d36d136ec4bc72b68 Mon Sep 17 00:00:00 2001 From: matheus Date: Sat, 11 Jul 2015 16:01:16 -0300 Subject: [PATCH 05/11] add brackets in conditional --- core/classes/class-post-status.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/classes/class-post-status.php b/core/classes/class-post-status.php index 61aa481..f979d47 100644 --- a/core/classes/class-post-status.php +++ b/core/classes/class-post-status.php @@ -101,8 +101,9 @@ public function register_post_status() { */ public function meta_tags() { $screen = get_current_screen(); - if( ! in_array( $screen->post_type, $this->post_types ) ) + if( ! in_array( $screen->post_type, $this->post_types ) ) { return; + } $args = array( 'postTypes' => $this->post_types, From a47222bc43030398cf57c4b7ecc1a9cbde31842e Mon Sep 17 00:00:00 2001 From: matheus Date: Sat, 11 Jul 2015 16:02:02 -0300 Subject: [PATCH 06/11] fix space post status class --- core/classes/class-post-status.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/classes/class-post-status.php b/core/classes/class-post-status.php index f979d47..fd2b8a5 100644 --- a/core/classes/class-post-status.php +++ b/core/classes/class-post-status.php @@ -116,7 +116,7 @@ public function meta_tags() { $args['select'] = true; } } - printf( '', esc_attr( json_encode($args) ) ); + printf( '', esc_attr( json_encode( $args ) ) ); } /** From 170b0ca3fe07a1d9dcb72eebc33d92e42081c665 Mon Sep 17 00:00:00 2001 From: matheus Date: Sat, 11 Jul 2015 16:08:29 -0300 Subject: [PATCH 07/11] fix align post status class --- core/classes/class-post-status.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/classes/class-post-status.php b/core/classes/class-post-status.php index fd2b8a5..e7554af 100644 --- a/core/classes/class-post-status.php +++ b/core/classes/class-post-status.php @@ -58,11 +58,11 @@ class Odin_Post_Status { * @return void **/ public function __construct( $post_status, $post_types, $args ) { - $this->post_status = $post_status; - $this->post_types = $post_types; - $this->action_label = isset( $args['label'] ) ? $args['label'] : $post_status; - $this->applied_label = isset( $args['applied_label'] ) ? $args['applied_label'] : $this->action_label; - $this->args = $args; + $this->post_status = $post_status; + $this->post_types = $post_types; + $this->action_label = isset( $args['label'] ) ? $args['label'] : $post_status; + $this->applied_label = isset( $args['applied_label'] ) ? $args['applied_label'] : $this->action_label; + $this->args = $args; // Removes the arguments that do not belong to register_post_type unset( $this->args['applied_label'] ); From a2d0608d87a5c0e962e46f40193143c7e8fbc6aa Mon Sep 17 00:00:00 2001 From: matheus Date: Sat, 11 Jul 2015 16:23:43 -0300 Subject: [PATCH 08/11] fix JS --- core/assets/js/admin-custom-status.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/assets/js/admin-custom-status.js b/core/assets/js/admin-custom-status.js index 78e22ea..f93dc84 100644 --- a/core/assets/js/admin-custom-status.js +++ b/core/assets/js/admin-custom-status.js @@ -20,7 +20,7 @@ if( $( document.body ).hasClass( 'edit-php' ) ) { var html = ''; $( '.inline-edit-status select' ).each(function(){ - meta.append( html ); + $( this ).append( html ); }); } }); From 863bb0f8344cde18f4a841df3100aac2325746b4 Mon Sep 17 00:00:00 2001 From: matheus Date: Sat, 11 Jul 2015 18:20:48 -0300 Subject: [PATCH 09/11] change jQuery each to forEach --- core/assets/js/admin-custom-status.js | 16 ++++++++++------ functions.php | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/core/assets/js/admin-custom-status.js b/core/assets/js/admin-custom-status.js index f93dc84..fbb92f4 100644 --- a/core/assets/js/admin-custom-status.js +++ b/core/assets/js/admin-custom-status.js @@ -5,12 +5,15 @@ * Custom post status */ $( window ).load( function() { - $( 'meta.odin-custom-status-meta' ).each( function() { - var meta = $( this ); + var $odinMeta = document.querySelectorAll( 'meta.odin-custom-status-meta' ); + Array.prototype.forEach.call( $odinMeta, function( item ) { + var $meta = $( item ); + var args = $.parseJSON( $meta.attr( 'value' ) ); + if( $( document.body ).hasClass( 'post-php' ) || $( document.body ).hasClass( 'post-new-php' ) ) { - var args = $.parseJSON( meta.attr( 'value' ) ); var select = ''; - if( ! args.select ) { + if( args.select ) { + console.log('ahoy!'); select = 'selected="selected"'; $( 'label[for="post_status"]' ).append( ' ' + $.trim( args.appliedLabel ) + '' ); } @@ -19,8 +22,9 @@ } if( $( document.body ).hasClass( 'edit-php' ) ) { var html = ''; - $( '.inline-edit-status select' ).each(function(){ - $( this ).append( html ); + var $inlineStatus = document.querySelectorAll( '.inline-edit-status select' ); + Array.prototype.forEach.call( $inlineStatus, function( item ) { + $( item ).append( html ); }); } }); diff --git a/functions.php b/functions.php index 5ae7a5c..1fe9c6e 100644 --- a/functions.php +++ b/functions.php @@ -30,7 +30,7 @@ // require_once get_template_directory() . '/core/classes/class-options-helper.php'; // require_once get_template_directory() . '/core/classes/class-post-type.php'; // require_once get_template_directory() . '/core/classes/class-taxonomy.php'; -// require_once get_template_directory() . '/core/classes/class-metabox.php'; +//require_once get_template_directory() . '/core/classes/class-metabox.php'; // require_once get_template_directory() . '/core/classes/abstracts/abstract-front-end-form.php'; // require_once get_template_directory() . '/core/classes/class-contact-form.php'; // require_once get_template_directory() . '/core/classes/class-post-form.php'; From c2472f1591dd40c83bcbd3aa10c95177f3215293 Mon Sep 17 00:00:00 2001 From: matheus Date: Sat, 11 Jul 2015 18:22:24 -0300 Subject: [PATCH 10/11] change jQuery each to forEach --- functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions.php b/functions.php index 1fe9c6e..5ae7a5c 100644 --- a/functions.php +++ b/functions.php @@ -30,7 +30,7 @@ // require_once get_template_directory() . '/core/classes/class-options-helper.php'; // require_once get_template_directory() . '/core/classes/class-post-type.php'; // require_once get_template_directory() . '/core/classes/class-taxonomy.php'; -//require_once get_template_directory() . '/core/classes/class-metabox.php'; +// require_once get_template_directory() . '/core/classes/class-metabox.php'; // require_once get_template_directory() . '/core/classes/abstracts/abstract-front-end-form.php'; // require_once get_template_directory() . '/core/classes/class-contact-form.php'; // require_once get_template_directory() . '/core/classes/class-post-form.php'; From 74fac056da500cde3f2524bbf589280765d7bc92 Mon Sep 17 00:00:00 2001 From: matheus Date: Sat, 11 Jul 2015 20:06:11 -0300 Subject: [PATCH 11/11] remove DOM manipulation in forEach --- core/assets/js/admin-custom-status.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/core/assets/js/admin-custom-status.js b/core/assets/js/admin-custom-status.js index fbb92f4..73a9751 100644 --- a/core/assets/js/admin-custom-status.js +++ b/core/assets/js/admin-custom-status.js @@ -5,7 +5,10 @@ * Custom post status */ $( window ).load( function() { + var html = ''; + var label = false; var $odinMeta = document.querySelectorAll( 'meta.odin-custom-status-meta' ); + Array.prototype.forEach.call( $odinMeta, function( item ) { var $meta = $( item ); var args = $.parseJSON( $meta.attr( 'value' ) ); @@ -13,20 +16,26 @@ if( $( document.body ).hasClass( 'post-php' ) || $( document.body ).hasClass( 'post-new-php' ) ) { var select = ''; if( args.select ) { - console.log('ahoy!'); select = 'selected="selected"'; - $( 'label[for="post_status"]' ).append( ' ' + $.trim( args.appliedLabel ) + '' ); + label = ' ' + $.trim( args.appliedLabel ) + ''; } - var html = ''; - $( '#post_status' ).append( html ); + html += ''; } if( $( document.body ).hasClass( 'edit-php' ) ) { - var html = ''; - var $inlineStatus = document.querySelectorAll( '.inline-edit-status select' ); - Array.prototype.forEach.call( $inlineStatus, function( item ) { - $( item ).append( html ); - }); + html += ''; } }); + if( $( document.body ).hasClass( 'post-php' ) || $( document.body ).hasClass( 'post-new-php' ) ) { + $( '#post_status ').append( html ); + if( label ) { + $( 'label[for="post_status"]' ).append( label ); + } + } + if( $( document.body ).hasClass( 'edit-php' ) ) { + var $inlineStatus = document.querySelectorAll( '.inline-edit-status select' ); + Array.prototype.forEach.call( $inlineStatus, function( item ) { + $( item ).append( html ); + }); + } }); }( jQuery ));