Skip to content
Snippets Groups Projects
Commit be659782 authored by Erick Hitter's avatar Erick Hitter
Browse files

Switch to a singleton pattern for consistency

parent 0755575d
Branches
Tags
1 merge request!14Add native block-editor support
Pipeline #4803 failed
This commit is part of merge request !14. Comments created here will be created in the context of that merge request.
...@@ -24,6 +24,9 @@ ...@@ -24,6 +24,9 @@
* @package External_Permalinks_Redux * @package External_Permalinks_Redux
*/ */
// Include singleton trait used by all classes.
require_once dirname( __FILE__ ) . '/inc/trait-singleton.php';
// Include block-editor class. // Include block-editor class.
require_once dirname( __FILE__ ) . '/inc/class-external-permalinks-redux-block-editor.php'; require_once dirname( __FILE__ ) . '/inc/class-external-permalinks-redux-block-editor.php';
...@@ -32,12 +35,7 @@ require_once dirname( __FILE__ ) . '/inc/class-external-permalinks-redux-block-e ...@@ -32,12 +35,7 @@ require_once dirname( __FILE__ ) . '/inc/class-external-permalinks-redux-block-e
*/ */
// phpcs:ignore PEAR.NamingConventions.ValidClassName, Squiz.Commenting.ClassComment.Missing // phpcs:ignore PEAR.NamingConventions.ValidClassName, Squiz.Commenting.ClassComment.Missing
class external_permalinks_redux { class external_permalinks_redux {
/** use External_Permalinks_Redux_Singleton;
* Singleton!
*
* @var self
*/
protected static $instance;
/** /**
* Redirect URL meta key. * Redirect URL meta key.
...@@ -61,29 +59,70 @@ class external_permalinks_redux { ...@@ -61,29 +59,70 @@ class external_permalinks_redux {
public $status_codes; public $status_codes;
/** /**
* Instance of the block-editor integration class. * Supported post types.
*
* Cannot be used before `admin_init` hook.
* *
* @var External_Permalinks_Redux_Block_Editor * @var array
*/ */
public $block_editor; private $post_types;
/** /**
* Instantiate class as a singleton. * Allow access to certain private properties.
* *
* @return object * @param string $name Property name.
* @return array|null
*/ */
public static function get_instance() { public function __get( $name ) {
if ( ! isset( self::$instance ) ) { if ( 'post_types' === $name ) {
self::$instance = new self(); if ( ! did_action( 'admin_init' ) ) {
_doing_it_wrong(
__METHOD__,
'Cannot be used before `admin_init` hook.',
'1.0'
);
}
return $this->post_types;
} }
return self::$instance; return null;
}
/**
* Disallow setting private properties except via filters.
*
* @param string $name Property name.
* @param mixed $value Property value.
* @return false
*/
public function __set( $name, $value ) {
return false;
}
/**
* Check if certain private properties are set.
*
* @param string $name Property name.
* @return bool
*/
public function __isset( $name ) {
if ( 'post_types' === $name ) {
if ( ! did_action( 'admin_init' ) ) {
return false;
}
return is_array( $this->post_types )
&& ! empty( $this->post_types );
}
return false;
} }
/** /**
* Register actions and filters. * Register actions and filters.
*/ */
private function __construct() { protected function _setup() {
add_action( 'init', array( $this, 'action_init' ), 0 ); // Other init actions may rely on permalinks so filter early. add_action( 'init', array( $this, 'action_init' ), 0 ); // Other init actions may rely on permalinks so filter early.
add_action( 'admin_init', array( $this, 'action_admin_init' ) ); add_action( 'admin_init', array( $this, 'action_admin_init' ) );
add_action( 'save_post', array( $this, 'action_save_post' ) ); add_action( 'save_post', array( $this, 'action_save_post' ) );
...@@ -92,8 +131,6 @@ class external_permalinks_redux { ...@@ -92,8 +131,6 @@ class external_permalinks_redux {
add_filter( 'post_type_link', array( $this, 'filter_post_permalink' ), 1, 2 ); add_filter( 'post_type_link', array( $this, 'filter_post_permalink' ), 1, 2 );
add_filter( 'page_link', array( $this, 'filter_page_link' ), 1, 2 ); add_filter( 'page_link', array( $this, 'filter_page_link' ), 1, 2 );
add_action( 'wp', array( $this, 'action_wp' ) ); add_action( 'wp', array( $this, 'action_wp' ) );
$this->block_editor = new External_Permalinks_Redux_Block_Editor();
} }
/** /**
...@@ -116,13 +153,13 @@ class external_permalinks_redux { ...@@ -116,13 +153,13 @@ class external_permalinks_redux {
* Add meta box. * Add meta box.
*/ */
public function action_admin_init() { public function action_admin_init() {
$post_types = apply_filters( 'epr_post_types', array( 'post', 'page' ) ); $this->post_types = apply_filters( 'epr_post_types', array( 'post', 'page' ) );
if ( ! is_array( $post_types ) ) { if ( ! is_array( $this->post_types ) ) {
return; return;
} }
foreach ( $post_types as $post_type ) { foreach ( $this->post_types as $post_type ) {
if ( if (
function_exists( 'use_block_editor_for_post_type' ) function_exists( 'use_block_editor_for_post_type' )
&& use_block_editor_for_post_type( $post_type ) && use_block_editor_for_post_type( $post_type )
...@@ -298,6 +335,7 @@ class external_permalinks_redux { ...@@ -298,6 +335,7 @@ class external_permalinks_redux {
// Initialize the plugin if it hasn't already. // Initialize the plugin if it hasn't already.
external_permalinks_redux::get_instance(); external_permalinks_redux::get_instance();
External_Permalinks_Redux_Block_Editor::get_instance();
/** /**
* Wrapper for meta box function. * Wrapper for meta box function.
......
...@@ -8,4 +8,15 @@ ...@@ -8,4 +8,15 @@
/** /**
* Class Block_Editor. * Class Block_Editor.
*/ */
class External_Permalinks_Redux_Block_Editor {} class External_Permalinks_Redux_Block_Editor {
use External_Permalinks_Redux_Singleton;
/**
* Set up class.
*
* @return void
*/
protected function _setup() {
// TODO: Implement _setup() method.
}
}
<?php
/**
* Singleton trait.
*
* @package External_Permalinks_Redux
*/
trait External_Permalinks_Redux_Singleton {
/**
* Singleton!
*
* @var self
*/
protected static $instance;
/**
* Instantiate class as a singleton.
*
* @return object
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
self::$instance->_setup();
}
return self::$instance;
}
/**
* Unused constructor.
*/
final private function __construct() {}
/**
* Set up class.
*
* @return void
*/
abstract protected function _setup();
}
...@@ -76,6 +76,8 @@ class AdminCallbacks extends WP_UnitTestCase { ...@@ -76,6 +76,8 @@ class AdminCallbacks extends WP_UnitTestCase {
* Test metabox save. * Test metabox save.
*/ */
public function test_save_callback() { public function test_save_callback() {
add_filter( 'use_block_editor_for_post', '__return_false' );
$_POST[ $this->plugin->meta_key_target . '_nonce' ] = $this->nonce; $_POST[ $this->plugin->meta_key_target . '_nonce' ] = $this->nonce;
$_POST[ $this->plugin->meta_key_target . '_url' ] = static::DESTINATION; $_POST[ $this->plugin->meta_key_target . '_url' ] = static::DESTINATION;
$_POST[ $this->plugin->meta_key_target . '_type' ] = static::TYPE; $_POST[ $this->plugin->meta_key_target . '_type' ] = static::TYPE;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment