From c59a631f548d37680784156062c7bc8cebf3f6c4 Mon Sep 17 00:00:00 2001 From: Norris Date: Thu, 10 May 2018 12:26:49 +0300 Subject: [PATCH 1/9] Use `_doing_it_wrong` instead of `trigger_error` trigger_error() is not good for production. `_doing_it_wrong` is going to run only when WP_DEBUG is enabled. --- inc/admin/class-ramp-for-gutenberg-post-type-settings-ui.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/admin/class-ramp-for-gutenberg-post-type-settings-ui.php b/inc/admin/class-ramp-for-gutenberg-post-type-settings-ui.php index 36f47af..c352e0d 100644 --- a/inc/admin/class-ramp-for-gutenberg-post-type-settings-ui.php +++ b/inc/admin/class-ramp-for-gutenberg-post-type-settings-ui.php @@ -170,7 +170,7 @@ function render_settings_section() { public function get_supported_post_types() { if ( 0 === did_action( 'init' ) && ! doing_action( 'init' ) ) { - trigger_error( "get_supported_post_types() was called before the init hook. Some post types might not be registered yet.", E_USER_WARNING ); + _doing_it_wrong( 'Ramp_For_Gutenberg_Post_Type_Settings_UI::get_supported_post_types', "get_supported_post_types() was called before the init hook. Some post types might not be registered yet.", '1.0.0' ); } $post_types = get_post_types( From 539f0239674c6354d1035e961a11ff356ce39d91 Mon Sep 17 00:00:00 2001 From: Norris Date: Thu, 10 May 2018 13:44:43 +0300 Subject: [PATCH 2/9] Move the `get_supported_post_types` method to main class --- ...mp-for-gutenberg-post-type-settings-ui.php | 44 +------------------ inc/class-ramp-for-gutenberg.php | 40 +++++++++++++++++ 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/inc/admin/class-ramp-for-gutenberg-post-type-settings-ui.php b/inc/admin/class-ramp-for-gutenberg-post-type-settings-ui.php index c352e0d..c34e160 100644 --- a/inc/admin/class-ramp-for-gutenberg-post-type-settings-ui.php +++ b/inc/admin/class-ramp-for-gutenberg-post-type-settings-ui.php @@ -52,7 +52,7 @@ public function add_writing_settings_section() { public function sanitize_post_types_callback( $post_types ) { $post_types = array_unique( (array) $post_types ); - $supported_post_types = array_keys( $this->get_supported_post_types() ); + $supported_post_types = array_keys( $this->ramp_for_gutenberg->get_supported_post_types() ); /** * Validate & Sanitize @@ -87,7 +87,7 @@ public function sanitize_post_types_callback( $post_types ) { */ function render_settings_section() { - $post_types = $this->get_supported_post_types(); + $post_types = $this->ramp_for_gutenberg->get_supported_post_types(); $helper_enabled_post_types = (array) $this->ramp_for_gutenberg->get_criteria( 'post_types' ); $enabled_post_types = $this->ramp_for_gutenberg->get_enabled_post_types(); ?> @@ -152,45 +152,5 @@ function render_settings_section() { 'label' ] - */ - public function get_supported_post_types() { - - if ( 0 === did_action( 'init' ) && ! doing_action( 'init' ) ) { - _doing_it_wrong( 'Ramp_For_Gutenberg_Post_Type_Settings_UI::get_supported_post_types', "get_supported_post_types() was called before the init hook. Some post types might not be registered yet.", '1.0.0' ); - } - - $post_types = get_post_types( - [ - 'show_ui' => true, - 'show_in_rest' => true, - ], - 'object' - ); - - $available_post_types = array(); - - // Remove post types that don't want an editor - foreach ( $post_types as $name => $post_type_object ) { - if ( post_type_supports( $name, 'editor' ) && ! empty( $post_type_object->label ) ) { - $available_post_types[ $name ] = $post_type_object->label; - } - } - - return $available_post_types; - } } \ No newline at end of file diff --git a/inc/class-ramp-for-gutenberg.php b/inc/class-ramp-for-gutenberg.php index e653ea7..2604e9d 100644 --- a/inc/class-ramp-for-gutenberg.php +++ b/inc/class-ramp-for-gutenberg.php @@ -155,6 +155,46 @@ public function gutenberg_should_load() { } } + /** + * Get post types that can be supported by Gutenberg. + * + * This will get all registered post types and remove post types: + * * that aren't shown in the admin menu + * * like attachment, revision, etc. + * * that don't support native editor UI + * + * + * Also removes post types that don't support `show_in_rest`: + * @link https://github.com/WordPress/gutenberg/issues/3066 + * + * @return array of formatted post types as [ 'slug' => 'label' ] + */ + public function get_supported_post_types() { + + if ( 0 === did_action( 'init' ) && ! doing_action( 'init' ) ) { + _doing_it_wrong( 'Ramp_For_Gutenberg::get_supported_post_types', "get_supported_post_types() was called before the init hook. Some post types might not be registered yet.", '1.0.0' ); + } + + $post_types = get_post_types( + [ + 'show_ui' => true, + 'show_in_rest' => true, + ], + 'object' + ); + + $available_post_types = array(); + + // Remove post types that don't want an editor + foreach ( $post_types as $name => $post_type_object ) { + if ( post_type_supports( $name, 'editor' ) && ! empty( $post_type_object->label ) ) { + $available_post_types[ $name ] = $post_type_object->label; + } + } + + return $available_post_types; + } + /** * Get all post types with Gutenberg enabled * From 26ee86cdedb876c2b7fab57cc2dd35a467f1099a Mon Sep 17 00:00:00 2001 From: Norris Date: Thu, 10 May 2018 14:38:01 +0300 Subject: [PATCH 3/9] Store the criteria on `admin_init` --- inc/class-ramp-for-gutenberg.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/inc/class-ramp-for-gutenberg.php b/inc/class-ramp-for-gutenberg.php index 2604e9d..c9fc14a 100644 --- a/inc/class-ramp-for-gutenberg.php +++ b/inc/class-ramp-for-gutenberg.php @@ -3,10 +3,18 @@ class Ramp_For_Gutenberg { private static $instance; + + /** + * Criteria is temporarily stored on class instance before it can be validated and updated + * @var array $criteria + */ + private static $criteria; + private $option_name = 'ramp_for_gutenberg_load_critera'; public $active = false; public $load_gutenberg = null; + public static function get_instance() { if ( ! self::$instance ) { self::$instance = new Ramp_For_Gutenberg(); @@ -16,6 +24,14 @@ public static function get_instance() { private function __construct() { $this->option_name = apply_filters( 'ramp_for_gutenberg_option_name', $this->option_name ); + + /** + * Store the criteria on admin_init + * + * $priority = 5 to ensure that the UI class has fresh data available + * To do that, we need this to run before `ramp_for_gutenberg_initialize_admin_ui()` + */ + add_action( 'admin_init', [ $this, 'store_criteria' ], 5, 0 ); } public function get_option_name() { @@ -45,8 +61,12 @@ public function get_criteria( $criteria_name = '' ) { } public function save_criteria( $criteria ) { - if ( $this->validate_criteria( $criteria ) ) { - return update_option( $this->get_option_name(), $criteria ); + self::$criteria = $criteria; + } + + public function store_criteria() { + if ( $this->validate_criteria( self::$criteria ) ) { + return update_option( $this->get_option_name(), self::$criteria ); } return false; } From 7e9cd7d954db8aa16d4531dc38deba3859b95ce9 Mon Sep 17 00:00:00 2001 From: Norris Date: Thu, 10 May 2018 14:44:18 +0300 Subject: [PATCH 4/9] Make sure that ramped post types can support Gutenberg --- inc/class-ramp-for-gutenberg.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/inc/class-ramp-for-gutenberg.php b/inc/class-ramp-for-gutenberg.php index c9fc14a..4075581 100644 --- a/inc/class-ramp-for-gutenberg.php +++ b/inc/class-ramp-for-gutenberg.php @@ -91,10 +91,17 @@ public function validate_criteria( $criteria ) { } break; case 'post_types': + + $supported_post_types = array_keys( $this->get_supported_post_types() ); + foreach ( $value as $post_type ) { if ( sanitize_title( $post_type ) !== $post_type ) { return false; } + if ( ! in_array( $post_type, $supported_post_types, true ) ) { + _doing_it_wrong( 'ramp_for_gutenberg_load_gutenberg', "Cannot enable Gutenberg support for post type \"{$post_type}\"", null ); + return false; + } } break; case 'load': From 0a4d0f89f790e8b6009d5a4c1ac3cc0b98d5e335 Mon Sep 17 00:00:00 2001 From: Norris Date: Fri, 11 May 2018 13:20:21 +0300 Subject: [PATCH 5/9] Rename `save_criteria` to `set_criteria` --- inc/class-ramp-for-gutenberg.php | 2 +- ramp-for-gutenberg.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/class-ramp-for-gutenberg.php b/inc/class-ramp-for-gutenberg.php index 4075581..4f2ff84 100644 --- a/inc/class-ramp-for-gutenberg.php +++ b/inc/class-ramp-for-gutenberg.php @@ -60,7 +60,7 @@ public function get_criteria( $criteria_name = '' ) { } - public function save_criteria( $criteria ) { + public function set_criteria( $criteria ) { self::$criteria = $criteria; } diff --git a/ramp-for-gutenberg.php b/ramp-for-gutenberg.php index 0674765..0b34cdc 100644 --- a/ramp-for-gutenberg.php +++ b/ramp-for-gutenberg.php @@ -43,7 +43,7 @@ function ramp_for_gutenberg_load_gutenberg( $criteria = false ) { $stored_criteria = $RFG->get_criteria(); if ( $criteria !== $stored_criteria ) { // the criteria specified in code have changed -- update them - $criteria = $RFG->save_criteria( $criteria ); + $criteria = $RFG->set_criteria( $criteria ); } // indicate that we've loaded the plugin. $RFG->active = true; From 2ff9b7b15fb7b89d55996d57a406dc9ca04addf9 Mon Sep 17 00:00:00 2001 From: Norris Date: Fri, 11 May 2018 13:21:02 +0300 Subject: [PATCH 6/9] Rename `store_criteria` to `save_criteria` --- inc/class-ramp-for-gutenberg.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/class-ramp-for-gutenberg.php b/inc/class-ramp-for-gutenberg.php index 4f2ff84..3380518 100644 --- a/inc/class-ramp-for-gutenberg.php +++ b/inc/class-ramp-for-gutenberg.php @@ -31,7 +31,7 @@ private function __construct() { * $priority = 5 to ensure that the UI class has fresh data available * To do that, we need this to run before `ramp_for_gutenberg_initialize_admin_ui()` */ - add_action( 'admin_init', [ $this, 'store_criteria' ], 5, 0 ); + add_action( 'admin_init', [ $this, 'save_criteria' ], 5, 0 ); } public function get_option_name() { @@ -64,7 +64,7 @@ public function set_criteria( $criteria ) { self::$criteria = $criteria; } - public function store_criteria() { + public function save_criteria() { if ( $this->validate_criteria( self::$criteria ) ) { return update_option( $this->get_option_name(), self::$criteria ); } From cb4e615806160078dc7e9c460d2b3a132559fc20 Mon Sep 17 00:00:00 2001 From: Norris Date: Fri, 11 May 2018 13:38:30 +0300 Subject: [PATCH 7/9] Add criteria class variable comment --- inc/class-ramp-for-gutenberg.php | 1 + 1 file changed, 1 insertion(+) diff --git a/inc/class-ramp-for-gutenberg.php b/inc/class-ramp-for-gutenberg.php index 3380518..5e4532e 100644 --- a/inc/class-ramp-for-gutenberg.php +++ b/inc/class-ramp-for-gutenberg.php @@ -6,6 +6,7 @@ class Ramp_For_Gutenberg { /** * Criteria is temporarily stored on class instance before it can be validated and updated + * Do not trust raw data stored in $criteria! * @var array $criteria */ private static $criteria; From 58adc2478f258e530874d9b0c2c54748d30e844c Mon Sep 17 00:00:00 2001 From: Norris Date: Fri, 11 May 2018 13:44:00 +0300 Subject: [PATCH 8/9] Split validation into sanitization and validation --- inc/class-ramp-for-gutenberg.php | 72 +++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/inc/class-ramp-for-gutenberg.php b/inc/class-ramp-for-gutenberg.php index 5e4532e..fe1115f 100644 --- a/inc/class-ramp-for-gutenberg.php +++ b/inc/class-ramp-for-gutenberg.php @@ -41,8 +41,8 @@ public function get_option_name() { /** * Get the desired criteria - * @param string $criteria_name - post_types, post_ids, load * + * @param string $criteria_name - post_types, post_ids, load * @return mixed */ public function get_criteria( $criteria_name = '' ) { @@ -61,19 +61,76 @@ public function get_criteria( $criteria_name = '' ) { } + /** + * Set the private class variable $criteria + * self::$criteria going to be used to update the option when `$this->save_criteria()` is run + * + * @param $criteria + * @return bool + */ public function set_criteria( $criteria ) { - self::$criteria = $criteria; + + if ( $this->sanitize_criteria( $criteria ) ) { + self::$criteria = $criteria; + return true; + } + + return false; } + /** + * Save criteria in WordPres options if it's valid + */ public function save_criteria() { + if ( $this->validate_criteria( self::$criteria ) ) { - return update_option( $this->get_option_name(), self::$criteria ); + update_option( $this->get_option_name(), self::$criteria ); } - return false; + } + /** + * Make sure that the passed $post_types exist and can support Gutenberg + * + * @param array $post_types + * @return bool + */ + public function validate_post_types( $post_types ) { + + $supported_post_types = array_keys( $this->get_supported_post_types() ); + foreach ( (array) $post_types as $post_type ) { + if ( ! in_array( $post_type, $supported_post_types, true ) ) { + _doing_it_wrong( 'ramp_for_gutenberg_load_gutenberg', "Cannot enable Gutenberg support for post type \"{$post_type}\"", null ); + return false; + } + } + + return true; + } + + /** + * Validate $criteria + * + * @param $criteria + * @return bool + */ public function validate_criteria( $criteria ) { + if ( ! empty( $criteria['post_types'] ) && ! $this->validate_post_types( $criteria['post_types'] ) ) { + return false; + } + + return true; + } + + /** + * Sanitize $criteria by making sure it's formatted properly + * + * @param $criteria + * @return bool + */ + public function sanitize_criteria( $criteria ) { + if ( ! is_array( $criteria ) || ! $criteria ) { return false; } @@ -92,17 +149,10 @@ public function validate_criteria( $criteria ) { } break; case 'post_types': - - $supported_post_types = array_keys( $this->get_supported_post_types() ); - foreach ( $value as $post_type ) { if ( sanitize_title( $post_type ) !== $post_type ) { return false; } - if ( ! in_array( $post_type, $supported_post_types, true ) ) { - _doing_it_wrong( 'ramp_for_gutenberg_load_gutenberg', "Cannot enable Gutenberg support for post type \"{$post_type}\"", null ); - return false; - } } break; case 'load': From d80d094db110b5d0db45699d2538a2029155cc7f Mon Sep 17 00:00:00 2001 From: Norris Date: Fri, 11 May 2018 18:21:49 +0300 Subject: [PATCH 9/9] Make sure $criteria is not null before updating option --- inc/class-ramp-for-gutenberg.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/inc/class-ramp-for-gutenberg.php b/inc/class-ramp-for-gutenberg.php index fe1115f..aba6be7 100644 --- a/inc/class-ramp-for-gutenberg.php +++ b/inc/class-ramp-for-gutenberg.php @@ -7,9 +7,9 @@ class Ramp_For_Gutenberg { /** * Criteria is temporarily stored on class instance before it can be validated and updated * Do not trust raw data stored in $criteria! - * @var array $criteria + * @var mixed null|array */ - private static $criteria; + private static $criteria = null; private $option_name = 'ramp_for_gutenberg_load_critera'; public $active = false; @@ -83,7 +83,8 @@ public function set_criteria( $criteria ) { */ public function save_criteria() { - if ( $this->validate_criteria( self::$criteria ) ) { + + if ( null !== self::$criteria && $this->validate_criteria( self::$criteria ) ) { update_option( $this->get_option_name(), self::$criteria ); }