From e02226b24942ba9e9762c91e83a925dfea181ddc Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 20 May 2019 16:57:41 +0100 Subject: [PATCH 001/319] Added the very first iteration of Advanced Cache. --- advanced-cache/advanced-cache.php | 7 +++++ advanced-cache/inc/advanced-cache.php | 43 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 advanced-cache/advanced-cache.php create mode 100644 advanced-cache/inc/advanced-cache.php diff --git a/advanced-cache/advanced-cache.php b/advanced-cache/advanced-cache.php new file mode 100644 index 00000000..8821348f --- /dev/null +++ b/advanced-cache/advanced-cache.php @@ -0,0 +1,7 @@ +' ); + + $debug = << +HTML; + + return substr_replace( $output, '' . $debug, $position, 6 ); + } + + /** + * Return the Singleton Instance of the class. + * + * @return void + */ + public static function instance() { + static $instance = false; + + if ( ! $instance ) { + $instance = new self(); + } + + return $instance; + } +} + +DM_Advanced_Cache::instance(); \ No newline at end of file From 57cdb6a6cd1f90d388571af5ae06e5df190d36f3 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 20 May 2019 18:41:21 +0100 Subject: [PATCH 002/319] Cleaned up the cache output code to use less logic and better naming convention. --- advanced-cache/inc/advanced-cache.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index e9315732..63048b50 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -2,13 +2,21 @@ defined( 'ABSPATH' ) || die; class DM_Advanced_Cache { + /** + * Constructor + */ public function __construct() { - ob_start( array( $this, 'start' ) ); + ob_start( array( $this, 'cache_output' ) ); } - public function start( $output = '' ) { - $position = stripos( $output, '' ); - + /** + * Handle the output caching for the request. This is done by utilising the + * output buffering feature of PHP. + * + * @param string $output HTML as generated by WordPress. + * @return string HTML, either from Cache or by WordPress. + */ + public function cache_output( $output = '' ) { $debug = << HTML; - return substr_replace( $output, '' . $debug, $position, 6 ); + return $output . $debug; } /** From 5168ad2f0ffee12b313e3c1bac4d3ffaca852bb6 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 20 May 2019 19:22:33 +0100 Subject: [PATCH 003/319] Added a check to make sure the response is HTML before applying the HTML comment. --- advanced-cache/inc/advanced-cache.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index 63048b50..f875f610 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -17,7 +17,10 @@ public function __construct() { * @return string HTML, either from Cache or by WordPress. */ public function cache_output( $output = '' ) { - $debug = <<cache['output'], ' HTML; + } return $output . $debug; } From df11ddb8a47d0e67be2e674d4256387e5f78877a Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 20 May 2019 19:23:04 +0100 Subject: [PATCH 004/319] Only attempt advanced-cache.php if the request method is GET or HEAD. --- advanced-cache/inc/advanced-cache.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index f875f610..dd028a39 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -1,6 +1,13 @@ Date: Mon, 27 May 2019 10:19:06 +0100 Subject: [PATCH 005/319] Added logic to retrieve the Status Code of a given WordPress request. --- advanced-cache/inc/advanced-cache.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index dd028a39..2075d52a 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -14,6 +14,8 @@ class DM_Advanced_Cache { */ public function __construct() { ob_start( array( $this, 'cache_output' ) ); + + add_filter( 'status_header', array( $this, 'status_header' ), 10, 2 ); } /** @@ -43,6 +45,19 @@ public function cache_output( $output = '' ) { return $output . $debug; } + /** + * Retrieve the Status Code of the current request. + * + * @param string $header The header as generated by WordPress. + * @param integer $code Status - i.e. 200 / 404 / 500 - which corresponds to the current request. + * @return string WordPress generated header string, returned unchanged. + */ + public function status_header( $header = '', $code = 0 ) { + $this->status_code = absint( $code ); + + return $header; + } + /** * Return the Singleton Instance of the class. * From 53db03ef3fca0c9247dafcce161e9ee0cb385f72 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 27 May 2019 10:28:15 +0100 Subject: [PATCH 006/319] Added a method to check if the current request should be cached. --- advanced-cache/inc/advanced-cache.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index 2075d52a..4c637777 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -29,6 +29,10 @@ public function cache_output( $output = '' ) { $debug = ''; if ( false !== strpos( $this->cache['output'], 'do_cache() ) { + return $output; + } + $debug = <<status_code / 100 ) ) { + $cache = false; + } + + return $cache; + } + /** * Retrieve the Status Code of the current request. * From 66146f629f3d3a18a623a721ed7b8ce0fca172f5 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 27 May 2019 10:30:31 +0100 Subject: [PATCH 007/319] Fixed the check to make sure the current request contains HTML for the debug message. --- advanced-cache/inc/advanced-cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index 4c637777..64481607 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -28,11 +28,11 @@ public function __construct() { public function cache_output( $output = '' ) { $debug = ''; - if ( false !== strpos( $this->cache['output'], 'do_cache() ) { return $output; } + if ( false !== strpos( $output, ' Date: Sat, 31 Aug 2019 10:14:54 +0100 Subject: [PATCH 008/319] Added properties for Status Code and Response Type. --- advanced-cache/inc/advanced-cache.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index 64481607..fbc88c37 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -9,6 +9,20 @@ } class DM_Advanced_Cache { + /** + * Determines the appropriate logic for response that WordPress has provided. + * + * @var string Type of request; `page`, `redirect`, `error`, `notfound`, and `unknown` are valid values. + */ + private $response_type = 'page'; + + /** + * Stores the Status Code in state for use through Advanced Cache. + * + * @var int HTTP Status Code for the current request. + */ + private $status_code = -1; + /** * Constructor */ From 9d12dadfac98d49a2e7c5c1c92663da7855c9d79 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 10:21:03 +0100 Subject: [PATCH 009/319] Added logic to set the ->response_type property. --- advanced-cache/inc/advanced-cache.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index fbc88c37..e4977b80 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -88,6 +88,20 @@ public function do_cache() { public function status_header( $header = '', $code = 0 ) { $this->status_code = absint( $code ); + /** + * Set the response type property based on the status code. This will be used later for determining the best way + * for Dark Matter to respond. + */ + if ( 200 === $this->status_code ) { + $this->response_type = 'page'; + } elseif ( 404 === $this->status_code ) { + $this->response_type = 'notfound'; + } elseif ( in_array( $this->status_code, [ 301, 302, 303, 307 ], true ) ) { + $this->response_type = 'redirect'; + } elseif ( 5 === intval( $this->status_code / 100 ) ) { + $this->response_type = 'error'; + } + return $header; } From b63c699ac31407fcd087922710e2159754906551 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 10:26:25 +0100 Subject: [PATCH 010/319] Moved DM_Advanced_Cache to it's own file. --- advanced-cache/classes/DM_Advanced_Cache.php | 117 +++++++++++++++++++ advanced-cache/inc/advanced-cache.php | 112 ------------------ 2 files changed, 117 insertions(+), 112 deletions(-) create mode 100644 advanced-cache/classes/DM_Advanced_Cache.php diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php new file mode 100644 index 00000000..59b0b26a --- /dev/null +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -0,0 +1,117 @@ +do_cache() ) { + return $output; + } + + if ( false !== strpos( $output, ' +HTML; + } + + return $output . $debug; + } + + /** + * Determine if the current response should be cached. + * + * @return boolean Return true if the current response should be cached. False if it should not. + */ + public function do_cache() { + $cache = true; + + if ( 5 === ( $this->status_code / 100 ) ) { + $cache = false; + } + + return $cache; + } + + /** + * Retrieve the Status Code of the current request. + * + * @param string $header The header as generated by WordPress. + * @param integer $code Status - i.e. 200 / 404 / 500 - which corresponds to the current request. + * @return string WordPress generated header string, returned unchanged. + */ + public function status_header( $header = '', $code = 0 ) { + $this->status_code = absint( $code ); + + /** + * Set the response type property based on the status code. This will be used later for determining the best way + * for Dark Matter to respond. + */ + if ( 200 === $this->status_code ) { + $this->response_type = 'page'; + } elseif ( 404 === $this->status_code ) { + $this->response_type = 'notfound'; + } elseif ( in_array( $this->status_code, [ 301, 302, 303, 307 ], true ) ) { + $this->response_type = 'redirect'; + } elseif ( 5 === intval( $this->status_code / 100 ) ) { + $this->response_type = 'error'; + } + + return $header; + } + + /** + * Return the Singleton Instance of the class. + * + * @return void + */ + public static function instance() { + static $instance = false; + + if ( ! $instance ) { + $instance = new self(); + } + + return $instance; + } +} + +DM_Advanced_Cache::instance(); \ No newline at end of file diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index e4977b80..3a562bdb 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -8,117 +8,5 @@ return; } -class DM_Advanced_Cache { - /** - * Determines the appropriate logic for response that WordPress has provided. - * - * @var string Type of request; `page`, `redirect`, `error`, `notfound`, and `unknown` are valid values. - */ - private $response_type = 'page'; - - /** - * Stores the Status Code in state for use through Advanced Cache. - * - * @var int HTTP Status Code for the current request. - */ - private $status_code = -1; - - /** - * Constructor - */ - public function __construct() { - ob_start( array( $this, 'cache_output' ) ); - - add_filter( 'status_header', array( $this, 'status_header' ), 10, 2 ); - } - - /** - * Handle the output caching for the request. This is done by utilising the - * output buffering feature of PHP. - * - * @param string $output HTML as generated by WordPress. - * @return string HTML, either from Cache or by WordPress. - */ - public function cache_output( $output = '' ) { - $debug = ''; - - if ( ! $this->do_cache() ) { - return $output; - } - - if ( false !== strpos( $output, ' -HTML; - } - - return $output . $debug; - } - - /** - * Determine if the current response should be cached. - * - * @return boolean Return true if the current response should be cached. False if it should not. - */ - public function do_cache() { - $cache = true; - - if ( 5 === ( $this->status_code / 100 ) ) { - $cache = false; - } - - return $cache; - } - - /** - * Retrieve the Status Code of the current request. - * - * @param string $header The header as generated by WordPress. - * @param integer $code Status - i.e. 200 / 404 / 500 - which corresponds to the current request. - * @return string WordPress generated header string, returned unchanged. - */ - public function status_header( $header = '', $code = 0 ) { - $this->status_code = absint( $code ); - - /** - * Set the response type property based on the status code. This will be used later for determining the best way - * for Dark Matter to respond. - */ - if ( 200 === $this->status_code ) { - $this->response_type = 'page'; - } elseif ( 404 === $this->status_code ) { - $this->response_type = 'notfound'; - } elseif ( in_array( $this->status_code, [ 301, 302, 303, 307 ], true ) ) { - $this->response_type = 'redirect'; - } elseif ( 5 === intval( $this->status_code / 100 ) ) { - $this->response_type = 'error'; - } - - return $header; - } - - /** - * Return the Singleton Instance of the class. - * - * @return void - */ - public static function instance() { - static $instance = false; - - if ( ! $instance ) { - $instance = new self(); - } - - return $instance; - } -} DM_Advanced_Cache::instance(); \ No newline at end of file From 9bf620cfc96a3912cb989089cdc552105fe9debf Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 10:26:46 +0100 Subject: [PATCH 011/319] Added the logic needed to add the DM_Advanced_Cache file where required. --- advanced-cache/inc/advanced-cache.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index 3a562bdb..30b7d074 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -8,5 +8,10 @@ return; } +/** + * Cannot utilise plugin_dir_path() as the inner function used is not available and this is preferable to include more + * files than is realistically needed. + */ +$dirname = str_replace( '/inc', '', dirname( __FILE__ ) ); -DM_Advanced_Cache::instance(); \ No newline at end of file +require_once $dirname . '/classes/DM_Advanced_Cache.php'; \ No newline at end of file From cb63c959a2e5f5a65608ba87598c4d5c161824dc Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 10:50:08 +0100 Subject: [PATCH 012/319] Added a check for the response type / status code on do_cache(). --- advanced-cache/classes/DM_Advanced_Cache.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 59b0b26a..0168daf8 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -62,13 +62,16 @@ public function cache_output( $output = '' ) { * @return boolean Return true if the current response should be cached. False if it should not. */ public function do_cache() { - $cache = true; + /** + * Ensure the Response Type can be cached. + */ + if ( 'page' !== $this->response_type ) { + return false; + } - if ( 5 === ( $this->status_code / 100 ) ) { - $cache = false; } - return $cache; + return true; } /** From 85f24125a4b72e2b1d3b63faa2c8d03cdba2f6a0 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 10:50:22 +0100 Subject: [PATCH 013/319] Added the cookie check, to stop caching when some one is logged in to WP. --- advanced-cache/classes/DM_Advanced_Cache.php | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 0168daf8..29b9ce21 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -69,6 +69,28 @@ public function do_cache() { return false; } + /** + * Check Cookies to make sure that caching is suitable, i.e. do not cache if the User is logged in. + */ + $cookies = $_COOKIE; + $bypass = apply_filters( 'dark_matter_cookie_bypass', [] ); + + if ( ! empty( $cookies ) && is_array( $cookies ) ) { + foreach ( $cookies as $name => $value ) { + /** + * Check for Login. We bypass the override options if the User is logged in. + */ + if ( 0 === stripos( $name, 'wp_' ) || 0 === stripos( $name, 'wordpress_' ) ) { + return false; + } + + /** + * Check to see if the cookie is included in the Bypass set. + */ + if ( in_array( $name, $bypass ) ) { + return false; + } + } } return true; From 966b65505a5298e572c29043b5d233d145b07aae Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 10:52:54 +0100 Subject: [PATCH 014/319] Added code to ensure the object cache is available and working before proceeding to cache any thing. --- advanced-cache/inc/advanced-cache.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index 30b7d074..72cdf840 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -8,6 +8,22 @@ return; } +/** + * Attempt to include the Object Cache (if it was not already). + */ +if ( ! include_once( WP_CONTENT_DIR . '/object-cache.php' ) ) { + return; +} + +/** + * Attempt to instantiate the cache and bail if it doesn't work. + */ +wp_cache_init(); + +if ( ! is_object( $wp_object_cache ) ) { + return; +} + /** * Cannot utilise plugin_dir_path() as the inner function used is not available and this is preferable to include more * files than is realistically needed. From 8d9cff330d77b4c8f2ab6be8f608061aa9b6eb4a Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 10:57:40 +0100 Subject: [PATCH 015/319] Added a global cache group for Fullpage caching. --- advanced-cache/inc/advanced-cache.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index 72cdf840..bd38a78f 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -24,6 +24,8 @@ return; } +wp_cache_add_global_groups( 'dark-matter-fullpage' ); + /** * Cannot utilise plugin_dir_path() as the inner function used is not available and this is preferable to include more * files than is realistically needed. From 13e945d96ab23396f33002b6add3895663d43529 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 11:11:15 +0100 Subject: [PATCH 016/319] Added a method for generating a key by the URL. --- advanced-cache/classes/DM_Advanced_Cache.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 29b9ce21..88f60a75 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -123,6 +123,18 @@ public function status_header( $header = '', $code = 0 ) { return $header; } + /** + * Uses the various URL parameters to produce a key for caching. + * + * @return string MD5 hash of the URL most suitable for caching. + */ + public function url_key() { + $host = rtrim( trim( $_SERVER['HTTP_HOST'] ), '/' ); + $path = trim( strtok( $_SERVER['REQUEST_URI'], '?' ) ); + + return md5( $host . '/' . $path ); + } + /** * Return the Singleton Instance of the class. * From 97582d97cfb0fcb32e6720165c7a2d378d313b15 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 11:11:44 +0100 Subject: [PATCH 017/319] Added very basic embryonic cache and return system. --- advanced-cache/classes/DM_Advanced_Cache.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 88f60a75..7ea0e02f 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -20,6 +20,13 @@ class DM_Advanced_Cache { * Constructor */ public function __construct() { + $key = $this->url_key(); + $cache = wp_cache_get( $key, 'dark-matter-fullpage' ); + + if ( ! empty( $cache ) ) { + die( $cache ); + } + ob_start( array( $this, 'cache_output' ) ); add_filter( 'status_header', array( $this, 'status_header' ), 10, 2 ); @@ -39,6 +46,8 @@ public function cache_output( $output = '' ) { return $output; } + $key = $this->url_key(); + if ( false !== strpos( $output, ' Date: Sat, 31 Aug 2019 11:21:53 +0100 Subject: [PATCH 018/319] Added the class stumps for Post Cache and Request Cache. --- advanced-cache/classes/DM_Post_Cache.php | 7 +++++++ advanced-cache/classes/DM_Request_Cache.php | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 advanced-cache/classes/DM_Post_Cache.php create mode 100644 advanced-cache/classes/DM_Request_Cache.php diff --git a/advanced-cache/classes/DM_Post_Cache.php b/advanced-cache/classes/DM_Post_Cache.php new file mode 100644 index 00000000..8a295789 --- /dev/null +++ b/advanced-cache/classes/DM_Post_Cache.php @@ -0,0 +1,7 @@ + Date: Sat, 31 Aug 2019 11:35:06 +0100 Subject: [PATCH 019/319] Added some basic methods needed to load / serialise DM_Post_Cache objects. --- advanced-cache/classes/DM_Post_Cache.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Post_Cache.php b/advanced-cache/classes/DM_Post_Cache.php index 8a295789..aac83934 100644 --- a/advanced-cache/classes/DM_Post_Cache.php +++ b/advanced-cache/classes/DM_Post_Cache.php @@ -2,6 +2,23 @@ defined( 'ABSPATH' ) || die; class DM_Post_Cache { - public function __construct( $post_id = 0 ) { + /** + * DM_Post_Cache constructor. + * + * @param string $url + */ + public function __construct( $url = '' ) { + foreach ( get_object_vars( $url ) as $key => $value ) { + $this->$key = $value; + } + } + + /** + * Converts this object to an array. + * + * @return array Object as array. + */ + public function to_array() { + return get_object_vars( $this ); } } \ No newline at end of file From 63882bffe307ca207299527d3866408d96b7a5b2 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 11:41:15 +0100 Subject: [PATCH 020/319] Added an outline for the DM_Request_Cache class. --- advanced-cache/classes/DM_Request_Cache.php | 37 ++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index b7fd36b1..b7d630b3 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -2,6 +2,41 @@ defined( 'ABSPATH' ) || die; class DM_Request_Cache { - public function __construct() { + /** + * @var string Cache Key for the URL - minus any variants - of the Request. + */ + private $url_cache_key = ''; + + /** + * DM_Request_Cache constructor. + * + * @param string $url URL to retrieve the Request Cache Entry. + */ + public function __construct( $url = '' ) { + } + + /** + * Delete the Request Cache Entry. + */ + public function delete() { + + } + + /** + * Retrieve the Request Cache Entry - if available - and return it. + */ + public function get() { + + } + + /** + * Store the generate HTML in cache. + * + * @param string $output HTML to be added to the Request Cache entry. + */ + public function set( $output = '' ) { + + + return md5( $host . '/' . $path ); } } \ No newline at end of file From bd2832427d9bf55270b73a519a8c9cb70d919ef1 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 11:43:12 +0100 Subject: [PATCH 021/319] Added the logic to get the base URL key. --- advanced-cache/classes/DM_Request_Cache.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index b7d630b3..2084048d 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -13,6 +13,7 @@ class DM_Request_Cache { * @param string $url URL to retrieve the Request Cache Entry. */ public function __construct( $url = '' ) { + $this->url_cache_key = $this->get_url_key(); } /** @@ -29,14 +30,24 @@ public function get() { } + /** + * Generates a URL Key for the Request. This can be used to retrieve + * + * @return string MD5 hash key. + */ + public function get_url_key() { + $host = rtrim( trim( $_SERVER['HTTP_HOST'] ), '/' ); + $path = trim( strtok( $_SERVER['REQUEST_URI'], '?' ) ); + + return md5( $host . '/' . $path ); + } + /** * Store the generate HTML in cache. * * @param string $output HTML to be added to the Request Cache entry. */ public function set( $output = '' ) { - - return md5( $host . '/' . $path ); } } \ No newline at end of file From 1c268b82455315a05b325fdbf15fdb7ed18fc6c3 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 11:44:52 +0100 Subject: [PATCH 022/319] Added logic for getting a Variant key. --- advanced-cache/classes/DM_Request_Cache.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 2084048d..89b6ee00 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -7,6 +7,11 @@ class DM_Request_Cache { */ private $url_cache_key = ''; + /** + * @var string Key distinguishing the variant. + */ + private $variant_key = ''; + /** * DM_Request_Cache constructor. * @@ -14,6 +19,7 @@ class DM_Request_Cache { */ public function __construct( $url = '' ) { $this->url_cache_key = $this->get_url_key(); + $this->variant_key = $this->get_variant_key(); } /** @@ -42,6 +48,15 @@ public function get_url_key() { return md5( $host . '/' . $path ); } + /** + * Allows third parties to determine if the request should be treated differently from the standard caching logic. + * + * @return string MD5 hash key for the Variant. + */ + public function get_variant_key() { + return ''; + } + /** * Store the generate HTML in cache. * From 5cee2b693f08bf395320d9bd49be6a07a02a23a7 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 11:48:22 +0100 Subject: [PATCH 023/319] Refactored the two get_ methods to be set_ instead. --- advanced-cache/classes/DM_Request_Cache.php | 33 ++++++++++++--------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 89b6ee00..65d0c768 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -18,8 +18,11 @@ class DM_Request_Cache { * @param string $url URL to retrieve the Request Cache Entry. */ public function __construct( $url = '' ) { - $this->url_cache_key = $this->get_url_key(); - $this->variant_key = $this->get_variant_key(); + $this->set_url_and_key(); + + + + $this->set_variant_key(); } /** @@ -36,16 +39,25 @@ public function get() { } + /** + * Store the generate HTML in cache. + * + * @param string $output HTML to be added to the Request Cache entry. + */ + public function set( $output = '' ) { + + } + /** * Generates a URL Key for the Request. This can be used to retrieve * * @return string MD5 hash key. */ - public function get_url_key() { + public function set_url_and_key() { $host = rtrim( trim( $_SERVER['HTTP_HOST'] ), '/' ); $path = trim( strtok( $_SERVER['REQUEST_URI'], '?' ) ); - return md5( $host . '/' . $path ); + $this->url_cache_key = md5( $host . '/' . $path ); } /** @@ -53,16 +65,9 @@ public function get_url_key() { * * @return string MD5 hash key for the Variant. */ - public function get_variant_key() { - return ''; - } - - /** - * Store the generate HTML in cache. - * - * @param string $output HTML to be added to the Request Cache entry. - */ - public function set( $output = '' ) { + public function set_variant_key() { + $variant = apply_filters( 'dark_matter_request_variant', '', $this->url, $this->url_cache_key ); + $this->variant_key = md5( strval( $variant ) ); } } \ No newline at end of file From aa7191fa33377bd44c9838e1f19e8d7fb28235f4 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 11:50:10 +0100 Subject: [PATCH 024/319] Added the ->url property and the logic to populate it. --- advanced-cache/classes/DM_Request_Cache.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 65d0c768..3de31832 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -2,6 +2,11 @@ defined( 'ABSPATH' ) || die; class DM_Request_Cache { + /** + * @var string Request URL. + */ + private $url = ''; + /** * @var string Cache Key for the URL - minus any variants - of the Request. */ @@ -57,7 +62,8 @@ public function set_url_and_key() { $host = rtrim( trim( $_SERVER['HTTP_HOST'] ), '/' ); $path = trim( strtok( $_SERVER['REQUEST_URI'], '?' ) ); - $this->url_cache_key = md5( $host . '/' . $path ); + $this->url = $host . '/' . $path; + $this->url_cache_key = md5( $this->url ); } /** From a1597456e7df50ef6c0bf064b21de7de3e4034c2 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 31 Aug 2019 11:50:58 +0100 Subject: [PATCH 025/319] Changed the access modifiers on the set_ methods to private. --- advanced-cache/classes/DM_Request_Cache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 3de31832..2d1c6bf9 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -58,7 +58,7 @@ public function set( $output = '' ) { * * @return string MD5 hash key. */ - public function set_url_and_key() { + private function set_url_and_key() { $host = rtrim( trim( $_SERVER['HTTP_HOST'] ), '/' ); $path = trim( strtok( $_SERVER['REQUEST_URI'], '?' ) ); @@ -71,7 +71,7 @@ public function set_url_and_key() { * * @return string MD5 hash key for the Variant. */ - public function set_variant_key() { + private function set_variant_key() { $variant = apply_filters( 'dark_matter_request_variant', '', $this->url, $this->url_cache_key ); $this->variant_key = md5( strval( $variant ) ); From 818b8fb254ee89a1968a5c30acab2d3fb74932f1 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 1 Sep 2019 16:30:16 +0100 Subject: [PATCH 026/319] Removed the DM_Post_Cache whilst the functionality is re-evaluated. --- advanced-cache/classes/DM_Post_Cache.php | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 advanced-cache/classes/DM_Post_Cache.php diff --git a/advanced-cache/classes/DM_Post_Cache.php b/advanced-cache/classes/DM_Post_Cache.php deleted file mode 100644 index aac83934..00000000 --- a/advanced-cache/classes/DM_Post_Cache.php +++ /dev/null @@ -1,24 +0,0 @@ - $value ) { - $this->$key = $value; - } - } - - /** - * Converts this object to an array. - * - * @return array Object as array. - */ - public function to_array() { - return get_object_vars( $this ); - } -} \ No newline at end of file From 15af4b8820a30bdbb6377d1a535b88e4856951d9 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 1 Sep 2019 16:36:39 +0100 Subject: [PATCH 027/319] Added a method to retrieve the whole key. --- advanced-cache/classes/DM_Request_Cache.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 2d1c6bf9..43019c47 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -44,6 +44,24 @@ public function get() { } + /** + * Returns the cache key for storing the request. + * + * @return string Cache Key, formatted using the MD5 of the base URL and the MD5 of the variant (if there is one). + */ + public function get_key() { + $key = $this->url_cache_key; + + /** + * Append the Variant Key if there is one. + */ + if ( ! empty( $this->variant_key ) ) { + $key .= '-' . $this->variant_key; + } + + return $key; + } + /** * Store the generate HTML in cache. * From 54eed0f6f11474d49a567bded5fa18ada8da7a07 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 1 Sep 2019 16:38:43 +0100 Subject: [PATCH 028/319] Micro-optimised the cache key to stop repeated generations. --- advanced-cache/classes/DM_Request_Cache.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 43019c47..5cc6c83e 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -2,6 +2,11 @@ defined( 'ABSPATH' ) || die; class DM_Request_Cache { + /** + * @var string Cache Key. + */ + private $key = ''; + /** * @var string Request URL. */ @@ -25,8 +30,6 @@ class DM_Request_Cache { public function __construct( $url = '' ) { $this->set_url_and_key(); - - $this->set_variant_key(); } @@ -50,16 +53,23 @@ public function get() { * @return string Cache Key, formatted using the MD5 of the base URL and the MD5 of the variant (if there is one). */ public function get_key() { - $key = $this->url_cache_key; + /** + * Check to see if we have already generated the Key. + */ + if ( ! empty( $this->key ) ) { + return $this->key; + } + + $this->key = $this->url_cache_key; /** * Append the Variant Key if there is one. */ if ( ! empty( $this->variant_key ) ) { - $key .= '-' . $this->variant_key; + $this->key .= '-' . $this->variant_key; } - return $key; + return $this->key; } /** From 0cada70fca932a75036710191430a5d7439fac42 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 1 Sep 2019 16:42:33 +0100 Subject: [PATCH 029/319] Changed get_key() to set_key() to make it consistent with the other methods. --- advanced-cache/classes/DM_Request_Cache.php | 39 ++++++++------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 5cc6c83e..68341426 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -24,42 +24,42 @@ class DM_Request_Cache { /** * DM_Request_Cache constructor. - * - * @param string $url URL to retrieve the Request Cache Entry. */ - public function __construct( $url = '' ) { + public function __construct() { $this->set_url_and_key(); $this->set_variant_key(); + + $this->set_key(); } /** * Delete the Request Cache Entry. */ public function delete() { - + return wp_cache_delete( $this->key, 'dark-matter-fullpage' ); } /** * Retrieve the Request Cache Entry - if available - and return it. */ public function get() { - + return wp_cache_get( $this->key, 'dark-matter-fullpage' ); } /** - * Returns the cache key for storing the request. + * Store the generate HTML in cache. * - * @return string Cache Key, formatted using the MD5 of the base URL and the MD5 of the variant (if there is one). + * @param string $output HTML to be added to the Request Cache entry. */ - public function get_key() { - /** - * Check to see if we have already generated the Key. - */ - if ( ! empty( $this->key ) ) { - return $this->key; - } + public function set( $output = '' ) { + + } + /** + * Sets the cache key for storing the request. + */ + public function set_key() { $this->key = $this->url_cache_key; /** @@ -68,17 +68,6 @@ public function get_key() { if ( ! empty( $this->variant_key ) ) { $this->key .= '-' . $this->variant_key; } - - return $this->key; - } - - /** - * Store the generate HTML in cache. - * - * @param string $output HTML to be added to the Request Cache entry. - */ - public function set( $output = '' ) { - } /** From b13a4f5a1d4062dde55c500b62ee1c590848cb98 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 1 Sep 2019 16:45:16 +0100 Subject: [PATCH 030/319] Fixed the phpdoc blocks. --- advanced-cache/classes/DM_Request_Cache.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 68341426..693a21bf 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -35,6 +35,8 @@ public function __construct() { /** * Delete the Request Cache Entry. + * + * @return bool True on success. False otherwise. */ public function delete() { return wp_cache_delete( $this->key, 'dark-matter-fullpage' ); @@ -42,6 +44,8 @@ public function delete() { /** * Retrieve the Request Cache Entry - if available - and return it. + * + * @return bool|mixed HTML if available. False otherwise. */ public function get() { return wp_cache_get( $this->key, 'dark-matter-fullpage' ); From 5c087108c7e339c3dedfb724e21c24d727d25c7a Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 1 Sep 2019 16:45:24 +0100 Subject: [PATCH 031/319] Implemented the set() method. --- advanced-cache/classes/DM_Request_Cache.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 693a21bf..dea02f45 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -54,10 +54,11 @@ public function get() { /** * Store the generate HTML in cache. * - * @param string $output HTML to be added to the Request Cache entry. + * @param string $output HTML to be added to the Request Cache entry. + * @return bool True on success. False otherwise. */ public function set( $output = '' ) { - + return wp_cache_set( $this->key, $output, 'dark-matter-fullpage', 1 * MINUTE_IN_SECONDS ); } /** From ef3807f06e747eb01d30a40cab7a96b6f25b12d5 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 1 Sep 2019 16:50:43 +0100 Subject: [PATCH 032/319] Included the Request Cache object where needed. --- advanced-cache/inc/advanced-cache.php | 1 + 1 file changed, 1 insertion(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index bd38a78f..fada69d9 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -32,4 +32,5 @@ */ $dirname = str_replace( '/inc', '', dirname( __FILE__ ) ); +require_once $dirname . '/classes/DM_Request_Cache.php'; require_once $dirname . '/classes/DM_Advanced_Cache.php'; \ No newline at end of file From 918f871943790411e84cd8f5d943ee71ad6d2552 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 1 Sep 2019 16:51:46 +0100 Subject: [PATCH 033/319] Implemented DM_Request_Cache in to the Advanced Cache class. --- advanced-cache/classes/DM_Advanced_Cache.php | 34 +++++++------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 7ea0e02f..9a5f0090 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -2,6 +2,11 @@ defined( 'ABSPATH' ) || die; class DM_Advanced_Cache { + /** + * @var DM_Request_Cache Request Cache object. + */ + private $request = null; + /** * Determines the appropriate logic for response that WordPress has provided. * @@ -20,11 +25,12 @@ class DM_Advanced_Cache { * Constructor */ public function __construct() { - $key = $this->url_key(); - $cache = wp_cache_get( $key, 'dark-matter-fullpage' ); + $this->request = new DM_Request_Cache(); - if ( ! empty( $cache ) ) { - die( $cache ); + $cached_html = $this->request->get(); + + if ( ! empty( $cached_html ) ) { + die( $cached_html ); } ob_start( array( $this, 'cache_output' ) ); @@ -46,7 +52,7 @@ public function cache_output( $output = '' ) { return $output; } - $key = $this->url_key(); + $this->request->set( $output ); if ( false !== strpos( $output, ' Date: Sun, 1 Sep 2019 16:56:44 +0100 Subject: [PATCH 034/319] Created a new do_output() method for handling the debug HTML comment. --- advanced-cache/classes/DM_Advanced_Cache.php | 44 +++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 9a5f0090..57062ebc 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -30,7 +30,7 @@ public function __construct() { $cached_html = $this->request->get(); if ( ! empty( $cached_html ) ) { - die( $cached_html ); + die( $this->do_output( $cached_html ) ); } ob_start( array( $this, 'cache_output' ) ); @@ -54,21 +54,7 @@ public function cache_output( $output = '' ) { $this->request->set( $output ); - if ( false !== strpos( $output, ' -HTML; - } - - return $output . $debug; + return $this->do_output( $output ); } /** @@ -111,6 +97,32 @@ public function do_cache() { return true; } + /** + * Produces the entire HTML output. + * + * @param string $html HTML, either generated or from Cache. + * @return string HTML, possibly with additional details from Dark Matter. + */ + private function do_output( $html = '' ) { + $debug = ''; + + if ( false !== strpos( $html, ' +HTML; + } + + return $html . $debug; + } + /** * Retrieve the Status Code of the current request. * From ddb3c5e7a7bdf7c7d8ba0e7b71ed89b48da36aaf Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 1 Sep 2019 17:03:07 +0100 Subject: [PATCH 035/319] Moved the URL retrieval in to the DM_Advanced_Cache and out of Request Cache. --- advanced-cache/classes/DM_Advanced_Cache.php | 12 ++++++++++-- advanced-cache/classes/DM_Request_Cache.php | 14 +++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 57062ebc..83dcaeeb 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -25,9 +25,10 @@ class DM_Advanced_Cache { * Constructor */ public function __construct() { - $this->request = new DM_Request_Cache(); + $this->set_url(); - $cached_html = $this->request->get(); + $this->request = new DM_Request_Cache( $this->url ); + $cached_html = $this->request->get(); if ( ! empty( $cached_html ) ) { die( $this->do_output( $cached_html ) ); @@ -123,6 +124,13 @@ private function do_output( $html = '' ) { return $html . $debug; } + public function set_url() { + $host = rtrim( trim( $_SERVER['HTTP_HOST'] ), '/' ); + $path = trim( $_SERVER['REQUEST_URI'] ); + + $this->url = $host . '/' . $path; + } + /** * Retrieve the Status Code of the current request. * diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index dea02f45..9a7d27b3 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -24,9 +24,13 @@ class DM_Request_Cache { /** * DM_Request_Cache constructor. + * + * @param string $url URL to work with for the Request Cache object. */ - public function __construct() { - $this->set_url_and_key(); + public function __construct( $url = '' ) { + $this->url = strtok( $url, '?' ); + + $this->set_url_key(); $this->set_variant_key(); @@ -80,11 +84,7 @@ public function set_key() { * * @return string MD5 hash key. */ - private function set_url_and_key() { - $host = rtrim( trim( $_SERVER['HTTP_HOST'] ), '/' ); - $path = trim( strtok( $_SERVER['REQUEST_URI'], '?' ) ); - - $this->url = $host . '/' . $path; + private function set_url_key() { $this->url_cache_key = md5( $this->url ); } From 361cfa618333e1098b70a0676379859568561755 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 1 Sep 2019 17:04:03 +0100 Subject: [PATCH 036/319] Removed a stray variable. --- advanced-cache/classes/DM_Advanced_Cache.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 83dcaeeb..6795963f 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -47,8 +47,6 @@ public function __construct() { * @return string HTML, either from Cache or by WordPress. */ public function cache_output( $output = '' ) { - $debug = ''; - if ( ! $this->do_cache() ) { return $output; } From 4c5b200783d4751313ff45744805a64b11c82f41 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 10:34:09 +0100 Subject: [PATCH 037/319] Moved the debug message to be appended just before the closing tag; to keep the HTML valid rather than rely on browsers to figure it out. --- advanced-cache/classes/DM_Advanced_Cache.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 6795963f..6626942a 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -21,6 +21,13 @@ class DM_Advanced_Cache { */ private $status_code = -1; + /** + * Current request URL. + * + * @var string URL. + */ + private $url = ''; + /** * Constructor */ @@ -103,9 +110,9 @@ public function do_cache() { * @return string HTML, possibly with additional details from Dark Matter. */ private function do_output( $html = '' ) { - $debug = ''; + $head_pos = strpos( $html, '' ); - if ( false !== strpos( $html, ' HTML; + + /** + * Insert the debug just before the closing tag. + */ + $html = substr_replace( $html, $debug, $head_pos, 0 ); } - return $html . $debug; + return $html; } public function set_url() { @@ -159,7 +171,7 @@ public function status_header( $header = '', $code = 0 ) { /** * Return the Singleton Instance of the class. * - * @return void + * @return bool|DM_Advanced_Cache */ public static function instance() { static $instance = false; From 7bd44d03e201a3008843423e496f56eb674c78d9 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 10:36:54 +0100 Subject: [PATCH 038/319] Added phpdoc for set_url() method. --- advanced-cache/classes/DM_Advanced_Cache.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 6626942a..d5c42694 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -134,6 +134,9 @@ private function do_output( $html = '' ) { return $html; } + /** + * Set the URL from the current request and to be used in later processing. + */ public function set_url() { $host = rtrim( trim( $_SERVER['HTTP_HOST'] ), '/' ); $path = trim( $_SERVER['REQUEST_URI'] ); From c335383ae7739bf56c975678c9fc7b765fb88778 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 10:45:08 +0100 Subject: [PATCH 039/319] Changed the way the do_cache() determines response type. --- advanced-cache/classes/DM_Advanced_Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index d5c42694..f8ba63f3 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -72,7 +72,7 @@ public function do_cache() { /** * Ensure the Response Type can be cached. */ - if ( 'page' !== $this->response_type ) { + if ( ! in_array( $this->response_type, [ 'page' ], true ) ) { return false; } From 9e4b563e63c5a44d3b79032f0e3c3eb0d8fb34b2 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 10:51:08 +0100 Subject: [PATCH 040/319] Added a filter to allow people to modify the do_cache() return. --- advanced-cache/classes/DM_Advanced_Cache.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index f8ba63f3..741efb8a 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -69,11 +69,13 @@ public function cache_output( $output = '' ) { * @return boolean Return true if the current response should be cached. False if it should not. */ public function do_cache() { + $do_cache = true; + /** * Ensure the Response Type can be cached. */ if ( ! in_array( $this->response_type, [ 'page' ], true ) ) { - return false; + $do_cache = false; } /** @@ -88,19 +90,19 @@ public function do_cache() { * Check for Login. We bypass the override options if the User is logged in. */ if ( 0 === stripos( $name, 'wp_' ) || 0 === stripos( $name, 'wordpress_' ) ) { - return false; + $do_cache = false; } /** * Check to see if the cookie is included in the Bypass set. */ if ( in_array( $name, $bypass ) ) { - return false; + $do_cache = false; } } } - return true; + return apply_filters( 'dark_matter_do_cache', $do_cache, $this->response_type, $this->status_code ); } /** From 7e9cb17eb841ad7fcaeb5f1f73c58e2f02862d7c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 11:08:02 +0100 Subject: [PATCH 041/319] Added the file check to include a file for extending advanced cache. --- advanced-cache/inc/advanced-cache.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index fada69d9..9b52ee0b 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -26,6 +26,18 @@ wp_cache_add_global_groups( 'dark-matter-fullpage' ); +/** + * Prior to loading the library and processing the cache, determine if the current installation includes a file for + * extending Dark Matter Fullpage Caching. + */ +if ( defined( 'WP_CONTENT_DIR' ) ) { + $extension = WP_CONTENT_DIR . '/mu-plugins/advanced-cache.php'; + + if ( file_exists( $extension ) ) { + include_once $extension; + } +} + /** * Cannot utilise plugin_dir_path() as the inner function used is not available and this is preferable to include more * files than is realistically needed. From 401404a19902e87fb6ff11ae8a36f323a835810a Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 11:08:21 +0100 Subject: [PATCH 042/319] Added the warning for the inevitable ... some one using the DB at a very low-level. --- advanced-cache/inc/advanced-cache.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index 9b52ee0b..739ec473 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -38,6 +38,15 @@ } } +/** + * Sanity check; as we offer extensibility, there will be a temptation to include WPDB. The problem with using database + * queries at this level, is that will nullify any performance and / or scalability benefits afforded by caching in the + * first place. + */ +if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ! empty( $GLOBALS['wpdb'] ) ) { + trigger_error( 'Please be aware that by using database calls for Advanced Cache removes any benefit of using it, both in performance and scalability.', E_WARNING ); +} + /** * Cannot utilise plugin_dir_path() as the inner function used is not available and this is preferable to include more * files than is realistically needed. From a484aad62e40e4e3d473c010d06048514ce51b8b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 11:13:35 +0100 Subject: [PATCH 043/319] Improve the extend file check. --- advanced-cache/inc/advanced-cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index 739ec473..4b245740 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -33,7 +33,7 @@ if ( defined( 'WP_CONTENT_DIR' ) ) { $extension = WP_CONTENT_DIR . '/mu-plugins/advanced-cache.php'; - if ( file_exists( $extension ) ) { + if ( is_readable( $extension ) ) { include_once $extension; } } From 6fbbcc6e1686ba98ee95a8d2dd2764b52605a565 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 16:49:02 +0100 Subject: [PATCH 044/319] Changed the cache entry to store headers. --- advanced-cache/classes/DM_Request_Cache.php | 22 +++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 9a7d27b3..801849d3 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -58,11 +58,25 @@ public function get() { /** * Store the generate HTML in cache. * - * @param string $output HTML to be added to the Request Cache entry. - * @return bool True on success. False otherwise. + * @param string $output HTML to be added to the Request Cache entry. + * @return array|bool Cache data on success. False otherwise. */ - public function set( $output = '' ) { - return wp_cache_set( $this->key, $output, 'dark-matter-fullpage', 1 * MINUTE_IN_SECONDS ); + public function set( $output = '', $headers = [] ) { + if ( ! empty( $output ) ) { + return false; + } + + $data = [ + 'body' => $output, + 'headers' => $headers, + 'redirect' => false, + ]; + + if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage', 1 * MINUTE_IN_SECONDS ) ) { + return $data; + } + + return false; } /** From 1b10cfbd672d26632ed6df8c04576ddf140b2deb Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 17:07:24 +0100 Subject: [PATCH 045/319] Fixed a syntax bug preventing the set() method working. --- advanced-cache/classes/DM_Request_Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 801849d3..62a4adac 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -62,7 +62,7 @@ public function get() { * @return array|bool Cache data on success. False otherwise. */ public function set( $output = '', $headers = [] ) { - if ( ! empty( $output ) ) { + if ( empty( $output ) ) { return false; } From e8473193b8ec556d800d38222fa81663624b4e43 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 17:16:15 +0100 Subject: [PATCH 046/319] Added logic to process headers in to the cache entry. --- advanced-cache/classes/DM_Request_Cache.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 62a4adac..e9e3751e 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -62,10 +62,23 @@ public function get() { * @return array|bool Cache data on success. False otherwise. */ public function set( $output = '', $headers = [] ) { + /** + * No output, no caching. + */ if ( empty( $output ) ) { return false; } + /** + * Get the headers in to a consistent and more programmatically appeasing way to use. + */ + $cache_headers = []; + + foreach ( $headers as $header ) { + list( $key, $value ) = array_map( 'trim', explode( ':', $header, 2 ) ); + $cache_headers[ $key ] = $value; + } + $data = [ 'body' => $output, 'headers' => $headers, From 242505b2be44ae6c4a372579247f53dd70613fc1 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 17:18:45 +0100 Subject: [PATCH 047/319] Mistakenly used the raw headers value rather than the sanitated in Request Cache. --- advanced-cache/classes/DM_Request_Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index e9e3751e..ea008c6c 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -81,7 +81,7 @@ public function set( $output = '', $headers = [] ) { $data = [ 'body' => $output, - 'headers' => $headers, + 'headers' => $cache_headers, 'redirect' => false, ]; From 8b8b3919a4906ad56231cf41274af1040b864239 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 17:20:25 +0100 Subject: [PATCH 048/319] Added a do_headers() method to Advanced Cache. --- advanced-cache/classes/DM_Advanced_Cache.php | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 741efb8a..86302a2c 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -105,6 +105,29 @@ public function do_cache() { return apply_filters( 'dark_matter_do_cache', $do_cache, $this->response_type, $this->status_code ); } + /** + * + * @param array $headers + */ + public function do_headers( $headers = [] ) { + /** + * This is for a generated response. + */ + if ( empty( $headers ) ) { + header( 'X-DarkMatter-Cache: MISS' ); + return; + } + + /** + * This is a cached response. + */ + header( 'X-DarkMatter-Cache: HIT' ); + + foreach ( $headers as $name => $value ) { + header( "{$name}: {$value}", true ); + } + } + /** * Produces the entire HTML output. * From ead7f0714a166808e04ab19fdba7e5d6f4ea4213 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 17:21:00 +0100 Subject: [PATCH 049/319] Updated the logic to handle body and headers. --- advanced-cache/classes/DM_Advanced_Cache.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 86302a2c..c09d0373 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -35,10 +35,11 @@ public function __construct() { $this->set_url(); $this->request = new DM_Request_Cache( $this->url ); - $cached_html = $this->request->get(); + $cache_data = $this->request->get(); - if ( ! empty( $cached_html ) ) { - die( $this->do_output( $cached_html ) ); + if ( ! empty( $cache_data ) ) { + $this->do_headers( $cache_data['headers'] ); + die( $this->do_output( $cache_data['body'] ) ); } ob_start( array( $this, 'cache_output' ) ); @@ -58,9 +59,10 @@ public function cache_output( $output = '' ) { return $output; } - $this->request->set( $output ); + $data = $this->request->set( $output, headers_list() ); - return $this->do_output( $output ); + $this->do_headers(); + return $this->do_output( $data['body'] ); } /** From 49c0d312bcedf598691b41e54ada4c575d96c146 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 19:27:27 +0100 Subject: [PATCH 050/319] Added a header to note when the cache has been bypassed. --- advanced-cache/classes/DM_Advanced_Cache.php | 1 + 1 file changed, 1 insertion(+) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index c09d0373..44aae793 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -56,6 +56,7 @@ public function __construct() { */ public function cache_output( $output = '' ) { if ( ! $this->do_cache() ) { + header( 'X-DarkMatter-Cache: BYPASS' ); return $output; } From 2cc5e298045a3aaeb547f8d1dd1bdfc9453d0a29 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 19:29:41 +0100 Subject: [PATCH 051/319] Fixed a phpdoc param on set() method. --- advanced-cache/classes/DM_Request_Cache.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index ea008c6c..aacca2a8 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -58,8 +58,9 @@ public function get() { /** * Store the generate HTML in cache. * - * @param string $output HTML to be added to the Request Cache entry. - * @return array|bool Cache data on success. False otherwise. + * @param string $output HTML to be added to the Request Cache entry. + * @param array $headers Headers to be added to Request Cache entry. + * @return array|bool Cache data on success. False otherwise. */ public function set( $output = '', $headers = [] ) { /** From 88a5147e6cfefe9549f7932557623ba679c21691 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 19:32:30 +0100 Subject: [PATCH 052/319] Added a sanitize headers method. --- advanced-cache/classes/DM_Request_Cache.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index aacca2a8..167d9d5c 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -55,6 +55,23 @@ public function get() { return wp_cache_get( $this->key, 'dark-matter-fullpage' ); } + /** + * Take headers and put them in a structure that is more consistent and more programmatically appeasing to use. + * + * @param array $headers Raw headers. + * @return array Sanitized headers. + */ + private function sanitize_headers( $headers = [] ) { + $cache_headers = []; + + foreach ( $headers as $header ) { + list( $key, $value ) = array_map( 'trim', explode( ':', $header, 2 ) ); + $cache_headers[ $key ] = $value; + } + + return $cache_headers; + } + /** * Store the generate HTML in cache. * From 7b8ef786b04c2231556a7186465b54e817b9929d Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 19:33:08 +0100 Subject: [PATCH 053/319] Changed set() method to use the new sanitize headers method. --- advanced-cache/classes/DM_Request_Cache.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 167d9d5c..16328640 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -90,16 +90,11 @@ public function set( $output = '', $headers = [] ) { /** * Get the headers in to a consistent and more programmatically appeasing way to use. */ - $cache_headers = []; - foreach ( $headers as $header ) { - list( $key, $value ) = array_map( 'trim', explode( ':', $header, 2 ) ); - $cache_headers[ $key ] = $value; - } $data = [ 'body' => $output, - 'headers' => $cache_headers, + 'headers' => $this->sanitize_headers( $headers ), 'redirect' => false, ]; From 4f3fc58e4cef27ea9daf5175e2b0732facf8afed Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 19:41:52 +0100 Subject: [PATCH 054/319] Added a $url_redirect property to Advanced Cache. --- advanced-cache/classes/DM_Advanced_Cache.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 44aae793..67e8316f 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -28,6 +28,13 @@ class DM_Advanced_Cache { */ private $url = ''; + /** + * URL Redirect. Only populates for instances where wp_redirect() and wp_safe_redirect() are called. + * + * @var string URL. + */ + private $url_redirect = ''; + /** * Constructor */ From d01ffc9ecb8b1f6442c9ed2368f554e8467e3d46 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 19:43:55 +0100 Subject: [PATCH 055/319] Added logic to retrieve a redirect in Advanced Cache. --- advanced-cache/classes/DM_Advanced_Cache.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 67e8316f..e7cd6cee 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -52,6 +52,7 @@ public function __construct() { ob_start( array( $this, 'cache_output' ) ); add_filter( 'status_header', array( $this, 'status_header' ), 10, 2 ); + add_filter( 'wp_redirect_status', array( $this, 'redirect_status' ), 10, 2 ); } /** @@ -84,7 +85,7 @@ public function do_cache() { /** * Ensure the Response Type can be cached. */ - if ( ! in_array( $this->response_type, [ 'page' ], true ) ) { + if ( ! in_array( $this->response_type, [ 'page', 'redirect' ], true ) ) { $do_cache = false; } @@ -169,6 +170,19 @@ private function do_output( $html = '' ) { return $html; } + /** + * Retrieve the destination for a redirect issued using the WordPress logic. + * + * @param integer $status HTTP status code for the Redirect (i.e. 301, 302, etc.) + * @param string $location Destination for the redirect to go to. + * @return integer HTTP status code, unmodified. + */ + public function redirect_status( $status = 0, $location = '' ) { + $this->url_redirect = $location; + + return $status; + } + /** * Set the URL from the current request and to be used in later processing. */ From e4d29432378736547d13b3af4bffbede51e9d88b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 19:49:08 +0100 Subject: [PATCH 056/319] Added a set redirect method on Request Cache. --- advanced-cache/classes/DM_Request_Cache.php | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 16328640..3a91283d 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -105,6 +105,29 @@ public function set( $output = '', $headers = [] ) { return false; } + /** + * Create / Update a Request Cache entry for a redirect. + * + * @param integer $http_code Redirect code such as 301 or 302. + * @param string $location Destination for the redirect. + * @param array $headers Headers to be added to Request Cache entry. + * @return array|bool Cache data on success. False otherwise. + */ + public function set_redirect( $http_code = 0, $location = '', $headers = [] ) { + $data = [ + 'body' => '', + 'headers' => $this->sanitize_headers( $headers ), + 'http_code' => $http_code, + 'redirect' => true, + ]; + + if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage' ) ) { + return $data; + } + + return false; + } + /** * Sets the cache key for storing the request. */ From 7d566a7ccd13b63a1bed3be0aa6b83098158a83f Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 19:49:21 +0100 Subject: [PATCH 057/319] Removed the $url_redirect property as it is not needed. --- advanced-cache/classes/DM_Advanced_Cache.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index e7cd6cee..9ffe5e5c 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -28,13 +28,6 @@ class DM_Advanced_Cache { */ private $url = ''; - /** - * URL Redirect. Only populates for instances where wp_redirect() and wp_safe_redirect() are called. - * - * @var string URL. - */ - private $url_redirect = ''; - /** * Constructor */ From 0b3fe693fce8230a2a811e3f9e6b15eefa9b018e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 19:49:29 +0100 Subject: [PATCH 058/319] Implemented the caching of the redirect. --- advanced-cache/classes/DM_Advanced_Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 9ffe5e5c..b12c2736 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -171,7 +171,7 @@ private function do_output( $html = '' ) { * @return integer HTTP status code, unmodified. */ public function redirect_status( $status = 0, $location = '' ) { - $this->url_redirect = $location; + $this->request->set_redirect( $status, $location, headers_list() ); return $status; } From 2f75e8e945bb9681f9d0b3700fbf46a3af7ff08d Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 20:07:15 +0100 Subject: [PATCH 059/319] Added location to the Request Cache entry for redirects. --- advanced-cache/classes/DM_Request_Cache.php | 1 + 1 file changed, 1 insertion(+) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 3a91283d..cd695c8f 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -118,6 +118,7 @@ public function set_redirect( $http_code = 0, $location = '', $headers = [] ) { 'body' => '', 'headers' => $this->sanitize_headers( $headers ), 'http_code' => $http_code, + 'location' => $location, 'redirect' => true, ]; From 285a3fe9e7a8e2aacdf07c0746d019b57438e156 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 20:07:38 +0100 Subject: [PATCH 060/319] Created a method to handle a cached redirect. --- advanced-cache/classes/DM_Advanced_Cache.php | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index b12c2736..bdfa3c10 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -48,6 +48,54 @@ public function __construct() { add_filter( 'wp_redirect_status', array( $this, 'redirect_status' ), 10, 2 ); } + /** + * Action the Cached redirect request. + * + * @param array $data Request Cache Entry. + * @return void + */ + private function action_redirect( $data = [] ) { + /** + * Pull together the other headers from WordPress when it was cached as well as the Dark Matter headers. + */ + $this->do_headers( $data['headers'] ); + + /** + * Determine which protocol PHP is using. + */ + $protocol = 'HTTP/1.0'; + + if ( in_array( $_SERVER['SERVER_PROTOCOL'], [ 'HTTP/2.0', 'HTTP/1.1', 'HTTP/1.0' ], true ) ) { + $protocol = $_SERVER['SERVER_PROTOCOL']; + } + + /** + * Generate the header which tells the browser that the request is a redirect. + */ + $text = [ + 300 => 'Multiple Choices', + 301 => 'Moved Permanently', + 302 => 'Found', + 303 => 'See Other', + 304 => 'Not Modified', + 305 => 'Use Proxy', + 306 => 'Reserved', + 307 => 'Temporary Redirect', + ]; + + if ( ! empty( $text[ $data['http_code'] ] ) ) { + header( $protocol . ' ' . $data['http_code'] . ' ' . $text[ $data['http_code'] ], true ); + } else { + header( $protocol . ' 302 Found', true ); + } + + /** + * Finally, issue the location header telling the browser where to go. + */ + header( 'Location: ' . $data['location'], true ); + die; + } + /** * Handle the output caching for the request. This is done by utilising the * output buffering feature of PHP. From 70dd4fc8a0b960287dee4beee235bfc25bc853bc Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 20:08:05 +0100 Subject: [PATCH 061/319] Included the logic to action a Request Cache entry for redirects. --- advanced-cache/classes/DM_Advanced_Cache.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index bdfa3c10..5a525326 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -37,6 +37,10 @@ public function __construct() { $this->request = new DM_Request_Cache( $this->url ); $cache_data = $this->request->get(); + if ( $cache_data['redirect'] ) { + $this->action_redirect( $cache_data ); + } + if ( ! empty( $cache_data ) ) { $this->do_headers( $cache_data['headers'] ); die( $this->do_output( $cache_data['body'] ) ); From 911d05209588c76fbf45908e764875743eddb7ec Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 20:13:50 +0100 Subject: [PATCH 062/319] Fixed a bug where the variant key always got a value, even if it was empty. --- advanced-cache/classes/DM_Request_Cache.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index cd695c8f..bc851184 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -160,6 +160,10 @@ private function set_url_key() { private function set_variant_key() { $variant = apply_filters( 'dark_matter_request_variant', '', $this->url, $this->url_cache_key ); - $this->variant_key = md5( strval( $variant ) ); + if ( empty( $variant ) ) { + $this->variant_key = ''; + } else { + $this->variant_key = md5( strval( $variant ) ); + } } } \ No newline at end of file From a8bcef576c325a9cce1f3b800bd9ff45d1198962 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 2 Sep 2019 20:18:16 +0100 Subject: [PATCH 063/319] Changed Request Cache entry to work with query strings. --- advanced-cache/classes/DM_Request_Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index bc851184..5d531036 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -28,7 +28,7 @@ class DM_Request_Cache { * @param string $url URL to work with for the Request Cache object. */ public function __construct( $url = '' ) { - $this->url = strtok( $url, '?' ); + $this->url = $url; $this->set_url_key(); From 3fc2ad13984be98b40807db739b4bbbab983913e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 08:35:08 +0100 Subject: [PATCH 064/319] Added URL base property for the data record logic. --- advanced-cache/classes/DM_Request_Cache.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 5d531036..14fe7ee7 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -12,6 +12,11 @@ class DM_Request_Cache { */ private $url = ''; + /** + * @var string Base URL, minus any query string parameters. + */ + private $url_base = ''; + /** * @var string Cache Key for the URL - minus any variants - of the Request. */ @@ -29,6 +34,7 @@ class DM_Request_Cache { */ public function __construct( $url = '' ) { $this->url = $url; + $this->url_base = strtok( $url, '?' ); $this->set_url_key(); From 3b3b79940a84c3c5f5e19f38c2ed99d62110b643 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 20:43:35 +0100 Subject: [PATCH 065/319] Added functionality to exclude / include query string parameters as part of the cache entry. --- advanced-cache/classes/DM_Request_Cache.php | 42 ++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 14fe7ee7..0af8c6b4 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -155,7 +155,47 @@ public function set_key() { * @return string MD5 hash key. */ private function set_url_key() { - $this->url_cache_key = md5( $this->url ); + $query_string = parse_url( $this->url, PHP_URL_QUERY ); + + if ( empty( $query_string ) ) { + $this->url_cache_key = md5( $this->url ); + return; + } + + /** + * Populate and provide an override capability for ignoring query string parameters. This can be used to prevent + * thrashing of the full page cache by telling Dark Matter to ignore query string parameters. + */ + $ignore_list = [ 'utm_campaign', 'utm_content', 'utm_medium', 'utm_source', 'utm_term' ]; + $ignore_list = apply_filters( 'dark_matter_querystring_ignore', $ignore_list, $this->url ); + + /** + * Handle a special case value. If the $ignore_list array is the string "all", then Dark Matter will ignore all + * query strings and store the request under the base URL instead. + */ + if ( 'all' === $ignore_list ) { + $this->url_cache_key = md5( $this->url_base ); + return; + } + + /** + * Process the query string parameters and remove those which are part of the ignore list. + */ + parse_str( $query_string, $parameters ); + + foreach ( $parameters as $key => $value ) { + if ( in_array( $key, $ignore_list, true ) ) { + unset( $parameters[ $key ] ); + } + } + + $cache_key = $this->url_base; + + if ( ! empty( $parameters ) ) { + $cache_key .= '?' . http_build_query( $parameters ); + } + + $this->url_cache_key = md5( $cache_key ); } /** From 375ee9247ac3ea666c2dea97b3f0c26ba55a91a7 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 20:44:52 +0100 Subject: [PATCH 066/319] Fixed an issue where the URL used for cache keys had double forward slash after the domain. --- advanced-cache/classes/DM_Advanced_Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 5a525326..f6c4cd18 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -233,7 +233,7 @@ public function redirect_status( $status = 0, $location = '' ) { */ public function set_url() { $host = rtrim( trim( $_SERVER['HTTP_HOST'] ), '/' ); - $path = trim( $_SERVER['REQUEST_URI'] ); + $path = ltrim( trim( $_SERVER['REQUEST_URI'] ), '/' ); $this->url = $host . '/' . $path; } From 96e3cbda24363cccab0a7eedf9ced78a2f732697 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 20:46:23 +0100 Subject: [PATCH 067/319] Changed the MISS to LOOKUP - as the term is more accurate to what is going on. --- advanced-cache/classes/DM_Advanced_Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index f6c4cd18..4fe8e5c3 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -170,7 +170,7 @@ public function do_headers( $headers = [] ) { * This is for a generated response. */ if ( empty( $headers ) ) { - header( 'X-DarkMatter-Cache: MISS' ); + header( 'X-DarkMatter-Cache: LOOKUP' ); return; } From 1ed47732ab367eea4b6e2f1e85b5728abb7869a7 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 20:47:43 +0100 Subject: [PATCH 068/319] Got rid of the old comment about sanitising headers, which was moved in to a separate method. --- advanced-cache/classes/DM_Request_Cache.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 0af8c6b4..448e1c48 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -93,11 +93,6 @@ public function set( $output = '', $headers = [] ) { return false; } - /** - * Get the headers in to a consistent and more programmatically appeasing way to use. - */ - - $data = [ 'body' => $output, 'headers' => $this->sanitize_headers( $headers ), From d11cd5878625999e80e98edcd91a857ab0f0c13d Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 20:58:57 +0100 Subject: [PATCH 069/319] Added a stub of the DM_Request_Data class. --- advanced-cache/classes/DM_Request_Data.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 advanced-cache/classes/DM_Request_Data.php diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php new file mode 100644 index 00000000..9cd7fd7b --- /dev/null +++ b/advanced-cache/classes/DM_Request_Data.php @@ -0,0 +1,18 @@ +cache_key = md5( $base_url ); + } +} \ No newline at end of file From 57f483a2a105c9600c79313eb9788b0430c12b44 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 21:18:17 +0100 Subject: [PATCH 070/319] Added methods to add / remove variants on the Request Data Record. --- advanced-cache/classes/DM_Request_Data.php | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index 9cd7fd7b..cd475c28 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -7,6 +7,11 @@ class DM_Request_Data { */ private $cache_key = ''; + /** + * @var array + */ + private $variants = []; + /** * DM_Request_Data constructor. * @@ -15,4 +20,28 @@ class DM_Request_Data { public function __construct( $base_url = '' ) { $this->cache_key = md5( $base_url ); } + + /** + * Add a variant to the Request Cache Data record. + * + * @param string $variant_key Variant key to be added. + */ + public function variant_add( $variant_key = '' ) { + if ( ! in_array( $variant_key, $this->variants, true ) ) { + $this->variants[] = $variant_key; + } + } + + /** + * Remove a variant to the Request Cache Data record. + * + * @param string $variant_key Variant key to be removed. + */ + public function variant_remove( $variant_key = '' ) { + $pos = array_search( $variant_key, $this->variants, true ); + + if ( false !== $pos ) { + unset( $this->variants[ $pos ] ); + } + } } \ No newline at end of file From a5d25a4a8e37275536fb18b3e58a9ca8341037e7 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 21:49:24 +0100 Subject: [PATCH 071/319] Added the value loading and saving to DM_Request_Data class. --- advanced-cache/classes/DM_Request_Data.php | 33 ++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index cd475c28..bc7302d3 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -2,10 +2,15 @@ defined( 'ABSPATH' ) || die; class DM_Request_Data { + /** + * @var array Data record. + */ + private $data = []; + /** * @var string Cache Key. */ - private $cache_key = ''; + private $key = ''; /** * @var array @@ -18,7 +23,31 @@ class DM_Request_Data { * @param string $base_url URL to retrieve Cache data for. */ public function __construct( $base_url = '' ) { - $this->cache_key = md5( $base_url ); + $this->key = md5( $base_url ); + + $data = wp_cache_get( $this->key, 'dark-matter-fullpage' ); + + $this->data = [ + 'count' => 0, + 'provider' => 'dark-matter', + 'variants' => [], + ]; + + if ( is_array( $data ) ) { + $this->data = array_merge( $data, $this->data ); + } + } + + /** + * Update the Request Cache Record. + * + * @return bool True on success. False otherwise. + */ + public function save() { + $this->data['count'] = count( $this->variants ); + $this->data['variants'] = $this->variants; + + return wp_cache_set( $this->key, $this->data, 'dark-matter-fullpage' ); } /** From 2d7304a1e01365c87098dd9ba7d2b39921212bf9 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 21:50:02 +0100 Subject: [PATCH 072/319] Implemented Request Cache Data in to the Request Cache Entry logic. --- advanced-cache/classes/DM_Request_Cache.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 448e1c48..9a76db0a 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -7,6 +7,11 @@ class DM_Request_Cache { */ private $key = ''; + /** + * @var DM_Request_Data Request Cache Data. + */ + private $request_data = null; + /** * @var string Request URL. */ @@ -41,6 +46,8 @@ public function __construct( $url = '' ) { $this->set_variant_key(); $this->set_key(); + + $this->request_data = new DM_Request_Data( $this->url_base ); } /** @@ -49,6 +56,8 @@ public function __construct( $url = '' ) { * @return bool True on success. False otherwise. */ public function delete() { + $this->request_data->variant_remove( $this->key ); + return wp_cache_delete( $this->key, 'dark-matter-fullpage' ); } @@ -100,6 +109,9 @@ public function set( $output = '', $headers = [] ) { ]; if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage', 1 * MINUTE_IN_SECONDS ) ) { + $this->request_data->variant_add( $this->key ); + $this->request_data->save(); + return $data; } @@ -124,6 +136,9 @@ public function set_redirect( $http_code = 0, $location = '', $headers = [] ) { ]; if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage' ) ) { + $this->request_data->variant_add( $this->key ); + $this->request_data->save(); + return $data; } From 3b1b0beb059cafbc964908cf6320ae8e579cd940 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 21:50:21 +0100 Subject: [PATCH 073/319] Loaded the DM_Request_Data class in advanced-cache.php. --- advanced-cache/inc/advanced-cache.php | 1 + 1 file changed, 1 insertion(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index 4b245740..af45c640 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -54,4 +54,5 @@ $dirname = str_replace( '/inc', '', dirname( __FILE__ ) ); require_once $dirname . '/classes/DM_Request_Cache.php'; +require_once $dirname . '/classes/DM_Request_Data.php'; require_once $dirname . '/classes/DM_Advanced_Cache.php'; \ No newline at end of file From 7a2e6d654a787813ad028aa4f9044e0ea2039b54 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 21:55:38 +0100 Subject: [PATCH 074/319] Added logic to ensure the Request Cache Entry is the correct format. --- advanced-cache/classes/DM_Advanced_Cache.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 4fe8e5c3..e776516b 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -37,6 +37,17 @@ public function __construct() { $this->request = new DM_Request_Cache( $this->url ); $cache_data = $this->request->get(); + /** + * Ensure the Cache entry is 1) available and 2) of the correct format. + */ + if ( empty( $cache_data ) || ! isset( $cache_data['redirect'] ) || ! isset( $cache_data['headers'] ) || ! isset( $cache_data['body'] ) ) { + ob_start( array( $this, 'cache_output' ) ); + + add_filter( 'status_header', array( $this, 'status_header' ), 10, 2 ); + add_filter( 'wp_redirect_status', array( $this, 'redirect_status' ), 10, 2 ); + return; + } + if ( $cache_data['redirect'] ) { $this->action_redirect( $cache_data ); } @@ -45,11 +56,6 @@ public function __construct() { $this->do_headers( $cache_data['headers'] ); die( $this->do_output( $cache_data['body'] ) ); } - - ob_start( array( $this, 'cache_output' ) ); - - add_filter( 'status_header', array( $this, 'status_header' ), 10, 2 ); - add_filter( 'wp_redirect_status', array( $this, 'redirect_status' ), 10, 2 ); } /** From 4f4db82040d62a1493dc9e876407e5e39b49db48 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 21:55:59 +0100 Subject: [PATCH 075/319] Changed the cache group for Request Cache Data. --- advanced-cache/classes/DM_Request_Data.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index bc7302d3..10bd7e97 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -25,7 +25,7 @@ class DM_Request_Data { public function __construct( $base_url = '' ) { $this->key = md5( $base_url ); - $data = wp_cache_get( $this->key, 'dark-matter-fullpage' ); + $data = wp_cache_get( $this->key, 'dark-matter-fullpage-data' ); $this->data = [ 'count' => 0, @@ -47,7 +47,7 @@ public function save() { $this->data['count'] = count( $this->variants ); $this->data['variants'] = $this->variants; - return wp_cache_set( $this->key, $this->data, 'dark-matter-fullpage' ); + return wp_cache_set( $this->key, $this->data, 'dark-matter-fullpage-data' ); } /** From ca2107aad505f5ab6a12cd6350d7c188824dfcf1 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 22:14:01 +0100 Subject: [PATCH 076/319] Fixed the way variants are merged together, between stored and incoming, on Request Cache Data. --- advanced-cache/classes/DM_Request_Data.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index 10bd7e97..7e9b26d8 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -34,7 +34,7 @@ public function __construct( $base_url = '' ) { ]; if ( is_array( $data ) ) { - $this->data = array_merge( $data, $this->data ); + $this->data = array_merge_recursive( $this->data, $data ); } } @@ -44,8 +44,7 @@ public function __construct( $base_url = '' ) { * @return bool True on success. False otherwise. */ public function save() { - $this->data['count'] = count( $this->variants ); - $this->data['variants'] = $this->variants; + $this->data['count'] = count( $this->data['variants'] ); return wp_cache_set( $this->key, $this->data, 'dark-matter-fullpage-data' ); } @@ -56,8 +55,8 @@ public function save() { * @param string $variant_key Variant key to be added. */ public function variant_add( $variant_key = '' ) { - if ( ! in_array( $variant_key, $this->variants, true ) ) { - $this->variants[] = $variant_key; + if ( ! in_array( $variant_key, $this->data['variants'], true ) ) { + $this->data['variants'][] = $variant_key; } } @@ -67,10 +66,10 @@ public function variant_add( $variant_key = '' ) { * @param string $variant_key Variant key to be removed. */ public function variant_remove( $variant_key = '' ) { - $pos = array_search( $variant_key, $this->variants, true ); + $pos = array_search( $variant_key, $this->data['variants'], true ); if ( false !== $pos ) { - unset( $this->variants[ $pos ] ); + unset( $this->data['variants'][ $pos ] ); } } } \ No newline at end of file From 34865e21406dd7c02ec79eedfae82ce29fe68e9e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Tue, 3 Sep 2019 22:24:03 +0100 Subject: [PATCH 077/319] Switched back to array_merge(). --- advanced-cache/classes/DM_Request_Data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index 7e9b26d8..e2e6741a 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -34,7 +34,7 @@ public function __construct( $base_url = '' ) { ]; if ( is_array( $data ) ) { - $this->data = array_merge_recursive( $this->data, $data ); + $this->data = array_merge( $this->data, $data ); } } From eae1c94537ec1c53ebd77bfb52c373ad74ca936e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 18:21:50 +0100 Subject: [PATCH 078/319] Added a get() method to Request Cache Data class. --- advanced-cache/classes/DM_Request_Data.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index e2e6741a..ed35ae01 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -38,6 +38,23 @@ public function __construct( $base_url = '' ) { } } + /** + * Retrieves a Request Cache Data record. + * + * @param string $url URL to retrieve. + * @return bool|array Dictionary object of the data record. False otherwise. + */ + public function get( $url = '' ) { + if ( empty( $url ) ) { + return false; + } + + $url = strtok( $url, '?' ); + $key = md5( $url ); + + return wp_cache_get( $key, 'dark-matter-fullpage-data' ); + } + /** * Update the Request Cache Record. * From a59cb80d8f961f9e08f9a41ad32aad75c77f6628 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 18:30:01 +0100 Subject: [PATCH 079/319] Changed the way the Domain Mapping module is loaded to better support future changes. --- advanced-cache/module.php | 3 +++ dark-matter.php | 25 ++++--------------------- domain-mapping/module.php | 27 +++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 advanced-cache/module.php create mode 100644 domain-mapping/module.php diff --git a/advanced-cache/module.php b/advanced-cache/module.php new file mode 100644 index 00000000..5f79f2b0 --- /dev/null +++ b/advanced-cache/module.php @@ -0,0 +1,3 @@ + Date: Sat, 7 Sep 2019 18:33:49 +0100 Subject: [PATCH 080/319] Added a comment explaining the way files are included. --- dark-matter.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dark-matter.php b/dark-matter.php index c3c7a582..505a0d51 100644 --- a/dark-matter.php +++ b/dark-matter.php @@ -39,6 +39,10 @@ wp_cache_add_global_groups( 'dark-matter' ); +/** + * Check the files exist before attempting to load them. This will allow code bases to exclude modules they do not want + * by using .gitignore or some other mechanism. + */ if ( is_readable( DM_PATH . '/domain-mapping/module.php' ) ) { require_once DM_PATH . '/domain-mapping/module.php'; } From 0c7e1ba56b32920504cce505e7d99e3e1e0c547c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 18:35:04 +0100 Subject: [PATCH 081/319] Altered the line break point for comment lines on the root plugin file to be more consistent with the rest (PhpStorm default). --- dark-matter.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/dark-matter.php b/dark-matter.php index 505a0d51..4fbd7e7c 100644 --- a/dark-matter.php +++ b/dark-matter.php @@ -12,18 +12,13 @@ * License URI: http://www.gnu.org/licenses/gpl-2.0.html * GitHub Plugin URI: https://github.com/cameronterry/dark-matter * - * Dark Matter is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * any later version. + * Dark Matter is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 2 of the License, or any later version. * - * Dark Matter is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * Dark Matter is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with Dark Matter. If not, see + * You should have received a copy of the GNU General Public License along with Dark Matter. If not, see * https://github.com/cameronterry/dark-matter/blob/master/license.txt. */ From fac556124317df27a71c8c050f75c7f1e431dcf6 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 18:57:22 +0100 Subject: [PATCH 082/319] Added includes for the Dark Matter advanced cache module. --- advanced-cache/module.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/advanced-cache/module.php b/advanced-cache/module.php index 5f79f2b0..0a2e1b71 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -1,3 +1,5 @@ Date: Sat, 7 Sep 2019 19:03:54 +0100 Subject: [PATCH 083/319] Removed the get() method as it is not used. --- advanced-cache/classes/DM_Request_Data.php | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index ed35ae01..a4aaf71b 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -38,22 +38,7 @@ public function __construct( $base_url = '' ) { } } - /** - * Retrieves a Request Cache Data record. - * - * @param string $url URL to retrieve. - * @return bool|array Dictionary object of the data record. False otherwise. - */ - public function get( $url = '' ) { - if ( empty( $url ) ) { - return false; - } - - $url = strtok( $url, '?' ); - $key = md5( $url ); - - return wp_cache_get( $key, 'dark-matter-fullpage-data' ); - } + public function invalidate() {} /** * Update the Request Cache Record. From fb504793de2f5439050d1cd4b59d4f03570aaa01 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 19:13:44 +0100 Subject: [PATCH 084/319] Implemented the invalidate() method on Request Cache Data class. --- advanced-cache/classes/DM_Request_Data.php | 31 +++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index a4aaf71b..5957df59 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -38,7 +38,36 @@ public function __construct( $base_url = '' ) { } } - public function invalidate() {} + /** + * Invalidates the cache for the current data record. Invalidates all variants unless a variant key is provided. + * + * @param string $variant_key Invalidate a specific key. + */ + public function invalidate( $variant_key = '' ) { + if ( empty( $variant_key ) && in_array( $variant_key, $this->data['variants'], true ) ) { + wp_cache_delete( $variant_key, 'dark-matter-fullpage-data' ); + + /** + * Remove from the list of variants. + */ + $position = array_search( $variant_key, $this->data['variants'], true ); + unset( $this->data['variants'][ $position ] ); + } else { + foreach ( $this->data['variants'] as $key ) { + wp_cache_delete( $key, 'dark-matter-fullpage-data' ); + } + + /** + * Empty the variants. + */ + $this->data['variants'] = []; + } + + /** + * Auto-save, as this method removes records. + */ + $this->save(); + } /** * Update the Request Cache Record. From 4c7cdf9398f94d00a606c716980028838bf27cf3 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 19:13:59 +0100 Subject: [PATCH 085/319] Removed the variants property as it is not used. --- advanced-cache/classes/DM_Request_Data.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index 5957df59..4402925b 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -12,11 +12,6 @@ class DM_Request_Data { */ private $key = ''; - /** - * @var array - */ - private $variants = []; - /** * DM_Request_Data constructor. * From d5585c14a0e2179e2c5c112abaed1f183934362e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 19:15:46 +0100 Subject: [PATCH 086/319] Added a stub of the DM_Cache_Post class. --- advanced-cache/classes/DM_Cache_Post.php | 83 ++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 advanced-cache/classes/DM_Cache_Post.php diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php new file mode 100644 index 00000000..529dd692 --- /dev/null +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -0,0 +1,83 @@ +invalidation ) || ! is_array( $this->invalidation ) ) { + return; + } + + $request = null; + + foreach ( $this->invalidation as $url ) { + $request = new DM_Request_Cache( $url ); + $request->invalidate(); + } + } + + /** + * Add URL to invalidate the cache before the process ends. + * + * @param int $id_or_url Post ID or URL. + */ + public function invalidate( $id_or_url = 0 ) { + if ( empty( $id_or_url ) ) { + return; + } + } + + /** + * Return the Singleton Instance of the class. + * + * @return bool|DM_Cache_Post + */ + public static function instance() { + static $instance = false; + + if ( ! $instance ) { + $instance = new self(); + } + + return $instance; + } +} +DM_Cache_Post::instance(); \ No newline at end of file From 40774deb340155dd3dd73823b3affdd1c1e89027 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 19:24:14 +0100 Subject: [PATCH 087/319] Added logic for the invalidate() method on DM_Cache_Post. --- advanced-cache/classes/DM_Cache_Post.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index 529dd692..6e63bc93 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -63,6 +63,19 @@ public function invalidate( $id_or_url = 0 ) { if ( empty( $id_or_url ) ) { return; } + + /** + * Handle a Post ID being provided by retrieving the Permalink. + */ + if ( is_numeric( $id_or_url ) ) { + $id_or_url = get_permalink( $id_or_url ); + + if ( empty( $id_or_url ) ) { + return; + } + } + + $this->invalidation[] = $id_or_url; } /** From edb721fa57d3a701bdcd31332134c3441f0cf51c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 19:27:06 +0100 Subject: [PATCH 088/319] Added the save post hook implementation. --- advanced-cache/classes/DM_Cache_Post.php | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index 6e63bc93..82b1c417 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -18,6 +18,12 @@ class DM_Cache_Post { public function __construct() { add_action( 'shutdown', [ $this, 'do_cache' ] ); add_action( 'shutdown', [ $this, 'do_invalidate' ] ); + /** + * Handle post creation and edits. We attempt to run this as late as possible to ensure plugins have a change to + * make changes before add entries to invalidate the cache. + */ + add_action( 'save_post', [ $this, 'handle_save_post' ], 999, 3 ); + } /** @@ -54,6 +60,25 @@ public function do_invalidate() { } } + /** + * Handle the Save Post action. + * + * @param int $post_id + * @param WP_Post $post + * @param bool $update + */ + public function handle_save_post( $post_id = 0, $post = null, $update = false ) { + /** + * Invalidate the post itself. + */ + $this->invalidate( $post_id ); + + /** + * Invalidate the homepage. + */ + $this->invalidate( home_url() ); + } + /** * Add URL to invalidate the cache before the process ends. * From 590b42c9d4196daf745e48aeeaa02647bbf5ea8a Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 19:27:23 +0100 Subject: [PATCH 089/319] Ensured that invalidating cache entries comes before instantly caching. --- advanced-cache/classes/DM_Cache_Post.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index 82b1c417..631ca30e 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -16,14 +16,17 @@ class DM_Cache_Post { * DM_Save_Post constructor. */ public function __construct() { - add_action( 'shutdown', [ $this, 'do_cache' ] ); - add_action( 'shutdown', [ $this, 'do_invalidate' ] ); /** * Handle post creation and edits. We attempt to run this as late as possible to ensure plugins have a change to * make changes before add entries to invalidate the cache. */ add_action( 'save_post', [ $this, 'handle_save_post' ], 999, 3 ); + /** + * Prioritise invalidating cache entries before attempting to instantly cache again. + */ + add_action( 'shutdown', [ $this, 'do_cache' ], 20 ); + add_action( 'shutdown', [ $this, 'do_invalidate' ], 10 ); } /** From b244e35062189d67306fea9be18505ac4cf101dd Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 19:29:48 +0100 Subject: [PATCH 090/319] Ensured that the fullpage cache global group is available. --- advanced-cache/module.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/advanced-cache/module.php b/advanced-cache/module.php index 0a2e1b71..464223de 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -1,5 +1,7 @@ Date: Sat, 7 Sep 2019 19:32:36 +0100 Subject: [PATCH 091/319] Added a cache clear when WordPress cleans the post cache data. --- advanced-cache/classes/DM_Cache_Post.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index 631ca30e..32280532 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -20,7 +20,8 @@ public function __construct() { * Handle post creation and edits. We attempt to run this as late as possible to ensure plugins have a change to * make changes before add entries to invalidate the cache. */ - add_action( 'save_post', [ $this, 'handle_save_post' ], 999, 3 ); + add_action( 'clean_post_cache', [ $this, 'handle_save_post' ], 999, 1 ); + add_action( 'save_post', [ $this, 'handle_save_post' ], 999, 1 ); /** * Prioritise invalidating cache entries before attempting to instantly cache again. From f3ee9edef62b3ed3fd00c8ece971b8706e47cf91 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 19:39:47 +0100 Subject: [PATCH 092/319] Added the RSS feed to the cache invalidation link. --- advanced-cache/classes/DM_Cache_Post.php | 3 ++- advanced-cache/module.php | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index 32280532..d25e5fd8 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -78,9 +78,10 @@ public function handle_save_post( $post_id = 0, $post = null, $update = false ) $this->invalidate( $post_id ); /** - * Invalidate the homepage. + * Invalidate the homepage and corresponding RSS feed. */ $this->invalidate( home_url() ); + $this->invalidate( home_url( '/feed/' ) ); } /** diff --git a/advanced-cache/module.php b/advanced-cache/module.php index 464223de..7ebd2994 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -3,5 +3,5 @@ wp_cache_add_global_groups( 'dark-matter-fullpage-data' ); -require_once DM_PATH . '/advanced-cache/DM_Request_Data.php'; -require_once DM_PATH . '/advanced-cache/DM_Save_Post.php'; \ No newline at end of file +require_once DM_PATH . 'advanced-cache/classes/DM_Cache_Post.php'; +require_once DM_PATH . 'advanced-cache/classes/DM_Request_Data.php'; From bf423d44025c96e7f348448304b76a524eaa1155 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 7 Sep 2019 19:40:06 +0100 Subject: [PATCH 093/319] Used the correct class, DM_Request_Data rather than DM_Request_Cache ... --- advanced-cache/classes/DM_Cache_Post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index d25e5fd8..d50ec46a 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -59,7 +59,7 @@ public function do_invalidate() { $request = null; foreach ( $this->invalidation as $url ) { - $request = new DM_Request_Cache( $url ); + $request = new DM_Request_Data( $url ); $request->invalidate(); } } From 7becc170df1200e5f844ea18756209c6bf1e1ad3 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 8 Sep 2019 13:23:03 +0100 Subject: [PATCH 094/319] Now deleting the correct cache entries in the dark-matter-fullpage group. --- advanced-cache/classes/DM_Request_Data.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index 4402925b..47a60f43 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -40,7 +40,7 @@ public function __construct( $base_url = '' ) { */ public function invalidate( $variant_key = '' ) { if ( empty( $variant_key ) && in_array( $variant_key, $this->data['variants'], true ) ) { - wp_cache_delete( $variant_key, 'dark-matter-fullpage-data' ); + wp_cache_delete( $variant_key, 'dark-matter-fullpage' ); /** * Remove from the list of variants. @@ -49,7 +49,7 @@ public function invalidate( $variant_key = '' ) { unset( $this->data['variants'][ $position ] ); } else { foreach ( $this->data['variants'] as $key ) { - wp_cache_delete( $key, 'dark-matter-fullpage-data' ); + wp_cache_delete( $key, 'dark-matter-fullpage' ); } /** From 81fed333d98680cf6e857a70067d914f98273e5b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 8 Sep 2019 13:23:24 +0100 Subject: [PATCH 095/319] Made sure the dark-matter-fullpage cache group is available for use for WP Admin. --- advanced-cache/module.php | 1 + 1 file changed, 1 insertion(+) diff --git a/advanced-cache/module.php b/advanced-cache/module.php index 7ebd2994..e02a6f32 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -1,6 +1,7 @@ Date: Sun, 8 Sep 2019 13:24:05 +0100 Subject: [PATCH 096/319] Added logic to prevent revisions, etc. from interfering with the invalidating cache entries. --- advanced-cache/classes/DM_Cache_Post.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index d50ec46a..c51a5e8a 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -72,6 +72,20 @@ public function do_invalidate() { * @param bool $update */ public function handle_save_post( $post_id = 0, $post = null, $update = false ) { + if ( empty( $post ) ) { + $post = get_post( $post_id ); + } + + if ( + empty( $post ) + || + 'revision' === $post->post_type + || + ! in_array( get_post_status( $post_id ), [ 'publish', 'trash' ], true ) + ) { + return; + } + /** * Invalidate the post itself. */ From 8c9d9251aaea099b44309c8b14bf1254d6cead46 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 8 Sep 2019 13:24:20 +0100 Subject: [PATCH 097/319] Moved the domain mapping logic in to a separate private method. --- advanced-cache/classes/DM_Cache_Post.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index c51a5e8a..a20fa0ef 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -119,7 +119,21 @@ public function invalidate( $id_or_url = 0 ) { } } - $this->invalidation[] = $id_or_url; + $this->invalidation[] = $this->prepare_url( $id_or_url ); + } + + /** + * Prepares the URL for use in caching or invalidation. + * + * @param string $url URL to prepare. + * @return string Prepared URL. + */ + private function prepare_url( $url = '' ) { + if ( class_exists( 'DM_URL' ) ) { + $url = DM_URL::instance()->map( $url ); + } + + return $url; } /** From 1cea798ff333c46d9bbbc0594c2ed9ab66e3a475 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 8 Sep 2019 13:24:36 +0100 Subject: [PATCH 098/319] Added protocol as part of the cache key. --- advanced-cache/classes/DM_Advanced_Cache.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index e776516b..775c7620 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -238,10 +238,19 @@ public function redirect_status( $status = 0, $location = '' ) { * Set the URL from the current request and to be used in later processing. */ public function set_url() { + $protocol = 'http://'; + if ( isset( $_SERVER['HTTPS'] ) ) { + if ( 'on' == strtolower( $_SERVER['HTTPS'] ) || '1' == $_SERVER['HTTPS'] ) { + $protocol = 'https://'; + } + } elseif ( isset( $_SERVER['SERVER_PORT'] ) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { + $protocol = 'https://'; + } + $host = rtrim( trim( $_SERVER['HTTP_HOST'] ), '/' ); $path = ltrim( trim( $_SERVER['REQUEST_URI'] ), '/' ); - $this->url = $host . '/' . $path; + $this->url = $protocol . $host . '/' . $path; } /** From 04b4b450063ce7b7c175c327cc202d15df5cf96d Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 8 Sep 2019 13:26:04 +0100 Subject: [PATCH 099/319] Make sure the homepage has a forward slash. --- advanced-cache/classes/DM_Cache_Post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index a20fa0ef..a55f9799 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -94,7 +94,7 @@ public function handle_save_post( $post_id = 0, $post = null, $update = false ) /** * Invalidate the homepage and corresponding RSS feed. */ - $this->invalidate( home_url() ); + $this->invalidate( home_url( '/' ) ); $this->invalidate( home_url( '/feed/' ) ); } From 48e74127c5de010787e45e0cfc385aa5c77a2223 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 8 Sep 2019 13:27:51 +0100 Subject: [PATCH 100/319] Removed save_post as the clean_post_cache is called then too. --- advanced-cache/classes/DM_Cache_Post.php | 1 - 1 file changed, 1 deletion(-) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index a55f9799..62ac0ae8 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -21,7 +21,6 @@ public function __construct() { * make changes before add entries to invalidate the cache. */ add_action( 'clean_post_cache', [ $this, 'handle_save_post' ], 999, 1 ); - add_action( 'save_post', [ $this, 'handle_save_post' ], 999, 1 ); /** * Prioritise invalidating cache entries before attempting to instantly cache again. From 265ffe9b794d25e45fc1126687edc48a2587e085 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Wed, 1 Jan 2020 15:38:55 +0000 Subject: [PATCH 101/319] Added the SSO Type disable cookie. --- domain-mapping/module.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/domain-mapping/module.php b/domain-mapping/module.php index e45b2f46..fc99ed05 100644 --- a/domain-mapping/module.php +++ b/domain-mapping/module.php @@ -15,7 +15,9 @@ require_once DM_PATH . '/domain-mapping/api/DarkMatter_Primary.php'; require_once DM_PATH . '/domain-mapping/api/DarkMatter_Restrict.php'; -require_once DM_PATH . '/domain-mapping/sso/DM_SSO_Cookie.php'; +if ( ! defined( 'DARKMATTER_SSO_TYPE' ) || 'disable' !== DARKMATTER_SSO_TYPE ) { + require_once DM_PATH . '/domain-mapping/sso/DM_SSO_Cookie.php'; +} require_once DM_PATH . '/domain-mapping/rest/DM_REST_Domains_Controller.php'; require_once DM_PATH . '/domain-mapping/rest/DM_REST_Restricted_Controller.php'; From 4fd3a67b669ae8e411ec3cb688d504325f1978b8 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Wed, 1 Jan 2020 16:00:04 +0000 Subject: [PATCH 102/319] Added class comments for clarification. --- advanced-cache/classes/DM_Cache_Post.php | 6 ++++++ advanced-cache/classes/DM_Request_Cache.php | 5 +++++ advanced-cache/classes/DM_Request_Data.php | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index 62ac0ae8..838eefc7 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -1,6 +1,12 @@ Date: Wed, 1 Jan 2020 16:10:18 +0000 Subject: [PATCH 103/319] Re-arranged the cookie check to a separate method. --- advanced-cache/classes/DM_Advanced_Cache.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 775c7620..edee8905 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -143,6 +143,19 @@ public function do_cache() { /** * Check Cookies to make sure that caching is suitable, i.e. do not cache if the User is logged in. */ + if ( $do_cache && ! $this->do_cache_cookies() ) { + $do_cache = false; + } + + return apply_filters( 'dark_matter_do_cache', $do_cache, $this->response_type, $this->status_code ); + } + + /** + * Check to see if there are any Cookies which can bypass the cache. + * + * @return boolean True if none of the cookie bypasses are present. + */ + private function do_cache_cookies() { $cookies = $_COOKIE; $bypass = apply_filters( 'dark_matter_cookie_bypass', [] ); @@ -153,6 +166,7 @@ public function do_cache() { */ if ( 0 === stripos( $name, 'wp_' ) || 0 === stripos( $name, 'wordpress_' ) ) { $do_cache = false; + break; } /** @@ -160,11 +174,12 @@ public function do_cache() { */ if ( in_array( $name, $bypass ) ) { $do_cache = false; + break; } } } - return apply_filters( 'dark_matter_do_cache', $do_cache, $this->response_type, $this->status_code ); + return $do_cache; } /** From 00873a96a3a08c40303fa61445dd9ea31a658517 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Wed, 1 Jan 2020 16:11:05 +0000 Subject: [PATCH 104/319] Missed instantiating the $do_cache variable in the new cookie method. --- advanced-cache/classes/DM_Advanced_Cache.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index edee8905..7bb408f5 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -156,8 +156,9 @@ public function do_cache() { * @return boolean True if none of the cookie bypasses are present. */ private function do_cache_cookies() { - $cookies = $_COOKIE; - $bypass = apply_filters( 'dark_matter_cookie_bypass', [] ); + $bypass = apply_filters( 'dark_matter_cookie_bypass', [] ); + $cookies = $_COOKIE; + $do_cache = true; if ( ! empty( $cookies ) && is_array( $cookies ) ) { foreach ( $cookies as $name => $value ) { From 9d299b4d16674e871d398217bcf611bd5ff60630 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Wed, 1 Jan 2020 16:17:58 +0000 Subject: [PATCH 105/319] Added the ability to set a querystring as a bypass for the cache. --- advanced-cache/classes/DM_Advanced_Cache.php | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 7bb408f5..315b454f 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -147,6 +147,10 @@ public function do_cache() { $do_cache = false; } + if ( $do_cache && ! $this->do_cache_querystring() ) { + $do_cache = false; + } + return apply_filters( 'dark_matter_do_cache', $do_cache, $this->response_type, $this->status_code ); } @@ -183,6 +187,36 @@ private function do_cache_cookies() { return $do_cache; } + /** + * Check to see if there are any query string parameters which can bypass the cache. + * + * @return boolean False if a query string which is marked as bypass is found. True / continue with cache otherwise. + */ + private function do_cache_querystring() { + $bypass = apply_filters( 'darkmatter_querystring_bypass', [] ); + $query_string = parse_url( $this->url, PHP_URL_QUERY ); + + if ( empty( $query_string ) || empty( $bypass ) ) { + return true; + } + + /** + * Convert the key / value string in to an array and attempt to find any of the bypass query string parameters. + */ + parse_str( $query_string, $parameters ); + + foreach ( $parameters as $key => $value ) { + if ( in_array( $key, $bypass, true ) ) { + return false; + } + } + + /** + * If we are here, then the cache can continue from the query string perspective. + */ + return true; + } + /** * * @param array $headers From 79c32308a82e64eaf107b360b6ea641460a2346d Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Wed, 1 Jan 2020 16:26:27 +0000 Subject: [PATCH 106/319] Added hard-set checks for the Request Method and ensuring that POST is not cached. --- advanced-cache/classes/DM_Advanced_Cache.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 315b454f..23364592 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -131,6 +131,22 @@ public function cache_output( $output = '' ) { * @return boolean Return true if the current response should be cached. False if it should not. */ public function do_cache() { + /** + * Hard-set checks. The caching cannot be perform if for the following; + * + * * Request method is not GET or HEAD. + * * Request is a POST (such as a form, etc.). + * * Basic Authentication has been provided. + */ + $request_method = ( isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( $_SERVER['REQUEST_METHOD'] ) : '' ); + if ( ! in_array( $request_method, [ 'GET', 'HEAD' ], true ) ) { + return false; + } + + if ( ! empty( $_POST ) || ! empty( $_SERVER['HTTP_AUTHORIZATION'] ) || ! empty( $_SERVER['PHP_AUTH_USER'] ) ) { + return false; + } + $do_cache = true; /** From 1ddab15182db17de7bab835d60d6a156d5f309a0 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Wed, 1 Jan 2020 17:32:31 +0000 Subject: [PATCH 107/319] Added a check for script name. --- advanced-cache/classes/DM_Advanced_Cache.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 23364592..3fc512a8 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -159,6 +159,10 @@ public function do_cache() { /** * Check Cookies to make sure that caching is suitable, i.e. do not cache if the User is logged in. */ + if ( $do_cache && ! $this->do_cache_scriptname() ) { + $do_cache = false; + } + if ( $do_cache && ! $this->do_cache_cookies() ) { $do_cache = false; } @@ -233,6 +237,19 @@ private function do_cache_querystring() { return true; } + /** + * Check to ensure that the script file is not one which should bypass the cache, like WordPress Cron request URL or + * the XML-RPC implementation. + * + * @return bool False if a script name is present in the bypass. True / continue with cache otherwise. + */ + private function do_cache_scriptname() { + $bypass = apply_filters( 'darkmatter_scriptname_bypass', [ 'wp-app.php', 'wp-cron.php', 'xmlrpc.php' ] ); + $script = strtolower( basename( $_SERVER['SCRIPT_FILENAME'] ) ); + + return ! in_array( $script, $bypass, true ); + } + /** * * @param array $headers From 013e194c45dca332067a04696306cbae1121c3a2 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Wed, 1 Jan 2020 18:07:15 +0000 Subject: [PATCH 108/319] Upped the version number to 3.0.0. --- dark-matter.php | 4 ++-- package.json | 2 +- readme.txt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dark-matter.php b/dark-matter.php index 0d54ba73..d7f46c15 100644 --- a/dark-matter.php +++ b/dark-matter.php @@ -3,7 +3,7 @@ * Plugin Name: Dark Matter * Plugin URI: https://cameronterry.supernovawp.com/dark-matter/ * Description: A domain mapping plugin from Project Dark Matter. - * Version: 2.0.5 + * Version: 3.0.0 * Author: Cameron Terry * Author URI: https://cameronterry.co.uk/ * Text Domain: dark-matter @@ -27,7 +27,7 @@ /** Setup the Plugin Constants */ define( 'DM_PATH', plugin_dir_path( __FILE__ ) ); -define( 'DM_VERSION', '2.0.5' ); +define( 'DM_VERSION', '3.0.0' ); define( 'DM_DB_VERSION', '20190114' ); define( 'DM_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); diff --git a/package.json b/package.json index 9aeb7fad..1c6c8522 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dark-matter", - "version": "2.0.5", + "version": "3.0.0", "description": "Domain Mapping plugin for WordPress.", "main": "index.js", "scripts": { diff --git a/readme.txt b/readme.txt index baf6b379..309796d0 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Tags: domain mapping, multisite Requires at least: 5.0 Requires PHP: 7.0.0 Tested up to: 5.3.1 -Stable tag: 2.0.5 +Stable tag: 3.0.0 License: GPLv2 License URI: http://www.gnu.org/licenses/gpl-2.0.html From faaf6e2808b3b510b8d8fb1da9a93ad850302b85 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 16:53:09 +0000 Subject: [PATCH 109/319] Removed unused do_cache() method on DM_Cache_Post. --- advanced-cache/classes/DM_Cache_Post.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/advanced-cache/classes/DM_Cache_Post.php b/advanced-cache/classes/DM_Cache_Post.php index 838eefc7..333322a5 100644 --- a/advanced-cache/classes/DM_Cache_Post.php +++ b/advanced-cache/classes/DM_Cache_Post.php @@ -31,7 +31,6 @@ public function __construct() { /** * Prioritise invalidating cache entries before attempting to instantly cache again. */ - add_action( 'shutdown', [ $this, 'do_cache' ], 20 ); add_action( 'shutdown', [ $this, 'do_invalidate' ], 10 ); } @@ -46,13 +45,6 @@ public function cache( $id_or_url = 0 ) { } } - /** - * Perform all the immediate cache entries. - */ - public function do_cache() { - - } - /** * Perform all cache invalidation entries. */ From 6ef50f50a47f9f02f0f374e9dc3a10f8cd4b4f50 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 16:53:17 +0000 Subject: [PATCH 110/319] Upped the cache to 5mins. --- advanced-cache/classes/DM_Request_Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 016ef3e5..30c373ec 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -113,7 +113,7 @@ public function set( $output = '', $headers = [] ) { 'redirect' => false, ]; - if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage', 1 * MINUTE_IN_SECONDS ) ) { + if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage', 5 * MINUTE_IN_SECONDS ) ) { $this->request_data->variant_add( $this->key ); $this->request_data->save(); From e3c7d86c1e26317b7c3563bf2b0b222daa392b35 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 17:05:32 +0000 Subject: [PATCH 111/319] Added the beginning of the Cache CLI. --- advanced-cache/cli/DarkMatter_Cache_CLI.php | 20 ++++++++++++++++++++ advanced-cache/module.php | 4 ++++ 2 files changed, 24 insertions(+) create mode 100644 advanced-cache/cli/DarkMatter_Cache_CLI.php diff --git a/advanced-cache/cli/DarkMatter_Cache_CLI.php b/advanced-cache/cli/DarkMatter_Cache_CLI.php new file mode 100644 index 00000000..00667fcd --- /dev/null +++ b/advanced-cache/cli/DarkMatter_Cache_CLI.php @@ -0,0 +1,20 @@ + + * : Full URL to retrieve full page cache statistics. + * + * @param $args + * @param $assoc_args + */ + public function stats( $args, $assoc_args ) { + + } +} +WP_CLI::add_command( 'darkmatter cache', 'DarkMatter_Cache_CLI' ); \ No newline at end of file diff --git a/advanced-cache/module.php b/advanced-cache/module.php index e02a6f32..2f699621 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -6,3 +6,7 @@ require_once DM_PATH . 'advanced-cache/classes/DM_Cache_Post.php'; require_once DM_PATH . 'advanced-cache/classes/DM_Request_Data.php'; + +if ( defined( 'WP_CLI' ) && WP_CLI ) { + require_once DM_PATH . 'advanced-cache/cli/DarkMatter_Cache_CLI.php'; +} \ No newline at end of file From c88a315cf8a520e78ca2923a43f46d0e2ba6647e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 18:52:40 +0000 Subject: [PATCH 112/319] Changing the variant array in the DM_Request_Data to use the variant key as the array key. --- advanced-cache/classes/DM_Request_Data.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index 30697449..c64679ba 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -85,8 +85,15 @@ public function save() { * Add a variant to the Request Cache Data record. * * @param string $variant_key Variant key to be added. + * @param array $data Useful data of the variant. */ - public function variant_add( $variant_key = '' ) { + public function variant_add( $variant_key = '', $data = [] ) { + $variant_data = []; + + if ( ! array_key_exists( $variant_key, $this->data['variants'] ) ) { + $this->data['variants'][ $variant_key ] = $variant_data; + } + if ( ! in_array( $variant_key, $this->data['variants'], true ) ) { $this->data['variants'][] = $variant_key; } From 148cdb32365ed68a55416e27c91e837078fd8b44 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 18:53:24 +0000 Subject: [PATCH 113/319] Updated the variant remove to use the variant key as the array key. --- advanced-cache/classes/DM_Request_Data.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index c64679ba..33eb8a1b 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -105,10 +105,8 @@ public function variant_add( $variant_key = '', $data = [] ) { * @param string $variant_key Variant key to be removed. */ public function variant_remove( $variant_key = '' ) { - $pos = array_search( $variant_key, $this->data['variants'], true ); - - if ( false !== $pos ) { - unset( $this->data['variants'][ $pos ] ); + if ( ! array_key_exists( $variant_key, $this->data['variants'] ) ) { + unset( $this->data['variants'][ $variant_key ] ); } } } \ No newline at end of file From be66c133783e504c80ee11d73d9ab2fde523d5bb Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 19:04:01 +0000 Subject: [PATCH 114/319] Added more data to variant record. --- advanced-cache/classes/DM_Request_Data.php | 30 +++++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index 33eb8a1b..824c92ef 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -85,17 +85,33 @@ public function save() { * Add a variant to the Request Cache Data record. * * @param string $variant_key Variant key to be added. - * @param array $data Useful data of the variant. + * @param array $cache_data Useful data of the variant. */ - public function variant_add( $variant_key = '', $data = [] ) { - $variant_data = []; + public function variant_add( $variant_key = '', $cache_data = [], $ttl = 0 ) { + $variant_data = [ + 'time' => time(), + 'ttl' => $ttl, + ]; - if ( ! array_key_exists( $variant_key, $this->data['variants'] ) ) { - $this->data['variants'][ $variant_key ] = $variant_data; + /** + * Store the size of the HTML. + */ + if ( ! empty( $cache_data['body'] ) ) { + $variant_data['size'] = strlen( $cache_data['body'] ); } - if ( ! in_array( $variant_key, $this->data['variants'], true ) ) { - $this->data['variants'][] = $variant_key; + /** + * Store whether the request being cached is a redirect. + */ + if ( ! empty( $cache_data['redirect'] ) ) { + $variant_data['is_redirect'] = strlen( $cache_data['redirect'] ); + } + + /** + * Add the Variant Data to the Request Data object. + */ + if ( ! array_key_exists( $variant_key, $this->data['variants'] ) ) { + $this->data['variants'][ $variant_key ] = $variant_data; } } From 7bff981d349043163d005cd1b95e3bf56c7a2625 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 19:04:32 +0000 Subject: [PATCH 115/319] Changed the Request set() method to pass the TTL to the DM_Request_Data object. --- advanced-cache/classes/DM_Request_Cache.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 30c373ec..36088722 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -113,8 +113,10 @@ public function set( $output = '', $headers = [] ) { 'redirect' => false, ]; - if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage', 5 * MINUTE_IN_SECONDS ) ) { - $this->request_data->variant_add( $this->key ); + $ttl = 5 * MINUTE_IN_SECONDS; + + if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage', $ttl ) ) { + $this->request_data->variant_add( $this->key, $data, $ttl ); $this->request_data->save(); return $data; From 1017c4254705f9008e986b1d2855e92746c6dfda Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 19:07:25 +0000 Subject: [PATCH 116/319] Added a header count to the variant data. --- advanced-cache/classes/DM_Request_Data.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index 824c92ef..5f573ef7 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -100,6 +100,13 @@ public function variant_add( $variant_key = '', $cache_data = [], $ttl = 0 ) { $variant_data['size'] = strlen( $cache_data['body'] ); } + /** + * Added a header count. + */ + if ( ! empty( $cache_data['headers'] ) ) { + $variant_data['headers'] = count( $cache_data['headers'] ); + } + /** * Store whether the request being cached is a redirect. */ From b48fd353c8fcb0a253b935762852f3efcd0233bf Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 19:11:13 +0000 Subject: [PATCH 117/319] Changed TTL, Time and Size to be indicative of what value is stored. --- advanced-cache/classes/DM_Request_Data.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index 5f573ef7..87753554 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -89,15 +89,15 @@ public function save() { */ public function variant_add( $variant_key = '', $cache_data = [], $ttl = 0 ) { $variant_data = [ - 'time' => time(), - 'ttl' => $ttl, + 'time_utc' => time(), + 'ttl_secs' => $ttl, ]; /** * Store the size of the HTML. */ if ( ! empty( $cache_data['body'] ) ) { - $variant_data['size'] = strlen( $cache_data['body'] ); + $variant_data['size_bytes'] = strlen( $cache_data['body'] ); } /** From c10712b28808016a42290fe1ede701e7e1594cae Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 19:13:32 +0000 Subject: [PATCH 118/319] Renamed the Cache CLI namespace to be Full Page. --- advanced-cache/cli/DarkMatter_Cache_CLI.php | 20 ----------- .../cli/DarkMatter_FullPage_CLI.php | 34 +++++++++++++++++++ advanced-cache/module.php | 2 +- 3 files changed, 35 insertions(+), 21 deletions(-) delete mode 100644 advanced-cache/cli/DarkMatter_Cache_CLI.php create mode 100644 advanced-cache/cli/DarkMatter_FullPage_CLI.php diff --git a/advanced-cache/cli/DarkMatter_Cache_CLI.php b/advanced-cache/cli/DarkMatter_Cache_CLI.php deleted file mode 100644 index 00667fcd..00000000 --- a/advanced-cache/cli/DarkMatter_Cache_CLI.php +++ /dev/null @@ -1,20 +0,0 @@ - - * : Full URL to retrieve full page cache statistics. - * - * @param $args - * @param $assoc_args - */ - public function stats( $args, $assoc_args ) { - - } -} -WP_CLI::add_command( 'darkmatter cache', 'DarkMatter_Cache_CLI' ); \ No newline at end of file diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php new file mode 100644 index 00000000..5a6b108f --- /dev/null +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -0,0 +1,34 @@ + + * : Full URL to retrieve full page cache statistics. + * + * @param $args + * @param $assoc_args + */ + public function stats( $args, $assoc_args ) { + if ( empty( $args[0] ) ) { + WP_CLI::error( __( 'Please provide a URL to retrieve cache statistics for.', 'dark-matter' ) ); + } + + $url = $args[0]; + + $cache_data = new DM_Request_Data( $url ); + + var_dump( $cache_data ); + + + if ( ! empty( $cache_data['variants'] ) ) { + foreach ( $cache_data['variants'] as $key => $data ) { + } + } + } +} +WP_CLI::add_command( 'darkmatter fullpage', 'DarkMatter_FullPage_CLI' ); \ No newline at end of file diff --git a/advanced-cache/module.php b/advanced-cache/module.php index 2f699621..2844c1ac 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -8,5 +8,5 @@ require_once DM_PATH . 'advanced-cache/classes/DM_Request_Data.php'; if ( defined( 'WP_CLI' ) && WP_CLI ) { - require_once DM_PATH . 'advanced-cache/cli/DarkMatter_Cache_CLI.php'; + require_once DM_PATH . 'advanced-cache/cli/DarkMatter_FullPage_CLI.php'; } \ No newline at end of file From a55484f25b3e2ff551ddb3ae581d92e10ba7977d Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 19:16:56 +0000 Subject: [PATCH 119/319] Added a bit of safety to the advanced-cache.php script. --- advanced-cache/advanced-cache.php | 1 + 1 file changed, 1 insertion(+) diff --git a/advanced-cache/advanced-cache.php b/advanced-cache/advanced-cache.php index 8821348f..40f95d61 100644 --- a/advanced-cache/advanced-cache.php +++ b/advanced-cache/advanced-cache.php @@ -1,4 +1,5 @@ Date: Sat, 4 Jan 2020 19:20:52 +0000 Subject: [PATCH 120/319] Added a constant for knowing if Dark Matter's full page caching is being used. --- advanced-cache/advanced-cache.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/advanced-cache/advanced-cache.php b/advanced-cache/advanced-cache.php index 40f95d61..94786f36 100644 --- a/advanced-cache/advanced-cache.php +++ b/advanced-cache/advanced-cache.php @@ -1,6 +1,16 @@ Date: Sat, 4 Jan 2020 19:22:36 +0000 Subject: [PATCH 121/319] Making the advanced-cache.php optional for installations using Dark Matter for domain mapping only. --- advanced-cache/module.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/advanced-cache/module.php b/advanced-cache/module.php index 2844c1ac..6f608272 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -1,6 +1,14 @@ Date: Sat, 4 Jan 2020 19:28:47 +0000 Subject: [PATCH 122/319] Ensured the CLI continues to work for the full page caching. --- advanced-cache/module.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/advanced-cache/module.php b/advanced-cache/module.php index 6f608272..0d8098d7 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -4,8 +4,11 @@ /** * Do not load the full page caching logic unless the current installation has the Dark Matter version of * advanced-cache.php, which defines the constant; DARKMATTER_FULLPAGECACHE. + * + * WP-CLI does not include the advanced-cache.php file as part of the bootstrap and therefore we need to pretend the + * Dark Matter cache is enabled. */ -if ( ! defined( 'DARKMATTER_FULLPAGECACHE' ) ) { +if ( ! defined( 'DARKMATTER_FULLPAGECACHE' ) && ! defined( 'WP_CLI' ) ) { return; } From ca4cfe15695ff3e5dcc2c51605ce02172cd1235e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 19:40:46 +0000 Subject: [PATCH 123/319] Added the ability to retrieve the Request Cache Data to DM_Request_Data. --- advanced-cache/classes/DM_Request_Data.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index 87753554..f4366222 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -39,6 +39,15 @@ public function __construct( $base_url = '' ) { } } + /** + * Returns the Request Cache Data. + * + * @return array Request Cache Data. + */ + public function data() { + return $this->data; + } + /** * Invalidates the cache for the current data record. Invalidates all variants unless a variant key is provided. * From 6a0cd4f3fdbe92c5f0eccd99cae1198345db6e5b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 19:41:56 +0000 Subject: [PATCH 124/319] CLI fullpage stats subcommand now displays a basic table. --- .../cli/DarkMatter_FullPage_CLI.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php index 5a6b108f..d1a20802 100644 --- a/advanced-cache/cli/DarkMatter_FullPage_CLI.php +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -20,15 +20,25 @@ public function stats( $args, $assoc_args ) { $url = $args[0]; - $cache_data = new DM_Request_Data( $url ); + $cache_data = new DM_Request_Data( $url ); + $request_data = $cache_data->data(); - var_dump( $cache_data ); + $data = []; - - if ( ! empty( $cache_data['variants'] ) ) { - foreach ( $cache_data['variants'] as $key => $data ) { - } + foreach ( $request_data['variants'] as $variant_key => $variant_data ) { + $data[] = [ + 'Variant Key' => $variant_key, + 'Provider' => $request_data['provider'], + 'Time' => $variant_data['time_utc'], + 'TTL' => $variant_data['ttl_secs'] / MINUTE_IN_SECONDS . ' ' . __( 'minutes', 'dark-matter' ), + 'Size' => size_format( $variant_data['size_bytes'] ), + 'Headers' => $variant_data['headers'], + ]; } + + $display = [ 'Variant Key', 'Provider', 'Time', 'TTL', 'Size', 'Headers' ]; + + WP_CLI\Utils\format_items( 'table', $data, $display ); } } WP_CLI::add_command( 'darkmatter fullpage', 'DarkMatter_FullPage_CLI' ); \ No newline at end of file From 46b62c7d17e185be98f03b1dd979f5942873d131 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 19:57:21 +0000 Subject: [PATCH 125/319] Fixed a bug updating the data for the variants since changing the array key. --- advanced-cache/classes/DM_Request_Data.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index f4366222..ee707945 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -41,7 +41,7 @@ public function __construct( $base_url = '' ) { /** * Returns the Request Cache Data. - * + * * @return array Request Cache Data. */ public function data() { @@ -126,9 +126,7 @@ public function variant_add( $variant_key = '', $cache_data = [], $ttl = 0 ) { /** * Add the Variant Data to the Request Data object. */ - if ( ! array_key_exists( $variant_key, $this->data['variants'] ) ) { - $this->data['variants'][ $variant_key ] = $variant_data; - } + $this->data['variants'][ $variant_key ] = $variant_data; } /** From 7a0ea088101855eb508faf0ab923665c97a13f3b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 20:06:09 +0000 Subject: [PATCH 126/319] Added a "Remaining" column which tells CLI admins how long the variant will remain in cache. --- advanced-cache/cli/DarkMatter_FullPage_CLI.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php index d1a20802..4742af05 100644 --- a/advanced-cache/cli/DarkMatter_FullPage_CLI.php +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -26,17 +26,25 @@ public function stats( $args, $assoc_args ) { $data = []; foreach ( $request_data['variants'] as $variant_key => $variant_data ) { + $expiry_time = $variant_data['time_utc'] + $variant_data['ttl_secs']; + $remaining = human_time_diff( time(), $expiry_time ); + + if ( time() > $expiry_time ) { + $remaining = __( 'Expired', 'dark-matter' ); + } + $data[] = [ 'Variant Key' => $variant_key, 'Provider' => $request_data['provider'], 'Time' => $variant_data['time_utc'], - 'TTL' => $variant_data['ttl_secs'] / MINUTE_IN_SECONDS . ' ' . __( 'minutes', 'dark-matter' ), + 'Remaining' => $remaining, + 'TTL' => human_time_diff( $variant_data['time_utc'], $expiry_time ), 'Size' => size_format( $variant_data['size_bytes'] ), 'Headers' => $variant_data['headers'], ]; } - $display = [ 'Variant Key', 'Provider', 'Time', 'TTL', 'Size', 'Headers' ]; + $display = [ 'Variant Key', 'Provider', 'Time', 'Remaining', 'TTL', 'Size', 'Headers' ]; WP_CLI\Utils\format_items( 'table', $data, $display ); } From 9266a825fe3deb7b7cec796f8647ebf83e182ae8 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 20:15:51 +0000 Subject: [PATCH 127/319] Added better formatting for the Time column. --- advanced-cache/cli/DarkMatter_FullPage_CLI.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php index 4742af05..860ad56e 100644 --- a/advanced-cache/cli/DarkMatter_FullPage_CLI.php +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -26,19 +26,26 @@ public function stats( $args, $assoc_args ) { $data = []; foreach ( $request_data['variants'] as $variant_key => $variant_data ) { - $expiry_time = $variant_data['time_utc'] + $variant_data['ttl_secs']; + $time = $variant_data['time_utc']; + $expiry_time = $time + $variant_data['ttl_secs']; $remaining = human_time_diff( time(), $expiry_time ); if ( time() > $expiry_time ) { $remaining = __( 'Expired', 'dark-matter' ); } + /** + * Convert the Unix timestamp to a human readable format and in the website's current timezone. + */ + $datetime = new DateTime( "@{$time}" ); + $datetime->setTimezone( wp_timezone() ); + $data[] = [ 'Variant Key' => $variant_key, 'Provider' => $request_data['provider'], - 'Time' => $variant_data['time_utc'], + 'Time' => $datetime->format( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ), 'Remaining' => $remaining, - 'TTL' => human_time_diff( $variant_data['time_utc'], $expiry_time ), + 'TTL' => human_time_diff( $time, $expiry_time ), 'Size' => size_format( $variant_data['size_bytes'] ), 'Headers' => $variant_data['headers'], ]; From 726bea040321fad540b845b202b2ca157880fe40 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 4 Jan 2020 20:18:28 +0000 Subject: [PATCH 128/319] Ensured the Time includes the timezone and more predictable output. --- advanced-cache/cli/DarkMatter_FullPage_CLI.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php index 860ad56e..e4b699e0 100644 --- a/advanced-cache/cli/DarkMatter_FullPage_CLI.php +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -43,7 +43,7 @@ public function stats( $args, $assoc_args ) { $data[] = [ 'Variant Key' => $variant_key, 'Provider' => $request_data['provider'], - 'Time' => $datetime->format( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ), + 'Time' => $datetime->format( 'r' ), 'Remaining' => $remaining, 'TTL' => human_time_diff( $time, $expiry_time ), 'Size' => size_format( $variant_data['size_bytes'] ), From 24b12f6811cb80bf87ae24119c33f9e63753fa0c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 17:50:05 +0000 Subject: [PATCH 129/319] Added logic to store the a friendly name for a cache variant. --- advanced-cache/classes/DM_Request_Data.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index ee707945..eeb72f38 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -93,11 +93,14 @@ public function save() { /** * Add a variant to the Request Cache Data record. * - * @param string $variant_key Variant key to be added. - * @param array $cache_data Useful data of the variant. + * @param string $variant_key Variant key to be added. + * @param string $variant_name Variant name to be added. + * @param array $cache_data Useful data of the variant. + * @param integer $ttl Length of time the output will be cached. */ - public function variant_add( $variant_key = '', $cache_data = [], $ttl = 0 ) { + public function variant_add( $variant_key = '', $variant_name = 'standard', $cache_data = [], $ttl = 0 ) { $variant_data = [ + 'name' => $variant_name, 'time_utc' => time(), 'ttl_secs' => $ttl, ]; From 1b6c5ded1fbfaeb5080aa6d636b8244f312cbd54 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 17:50:24 +0000 Subject: [PATCH 130/319] Added the logic to send the variant name for the full page caching. --- advanced-cache/classes/DM_Request_Cache.php | 22 ++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 36088722..8760f2ac 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -32,6 +32,11 @@ class DM_Request_Cache { */ private $url_cache_key = ''; + /** + * @var string Useful name for the Variant when debugging. + */ + private $variant_name = ''; + /** * @var string Key distinguishing the variant. */ @@ -116,7 +121,7 @@ public function set( $output = '', $headers = [] ) { $ttl = 5 * MINUTE_IN_SECONDS; if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage', $ttl ) ) { - $this->request_data->variant_add( $this->key, $data, $ttl ); + $this->request_data->variant_add( $this->key, $this->variant_name, $data, $ttl ); $this->request_data->save(); return $data; @@ -143,7 +148,7 @@ public function set_redirect( $http_code = 0, $location = '', $headers = [] ) { ]; if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage' ) ) { - $this->request_data->variant_add( $this->key ); + $this->request_data->variant_add( $this->key, 'redirect' ); $this->request_data->save(); return $data; @@ -221,12 +226,19 @@ private function set_url_key() { * @return string MD5 hash key for the Variant. */ private function set_variant_key() { - $variant = apply_filters( 'dark_matter_request_variant', '', $this->url, $this->url_cache_key ); + $default = [ + 'key' => '', + 'name' => 'standard', + ]; + + $variant = apply_filters( 'dark_matter_request_variant', $default, $this->url, $this->url_cache_key ); - if ( empty( $variant ) ) { + if ( empty( $variant['key'] ) ) { $this->variant_key = ''; } else { - $this->variant_key = md5( strval( $variant ) ); + $this->variant_key = md5( strval( $variant['key'] ) ); } + + $this->variant_name = $default['standard']; } } \ No newline at end of file From 3ac6472ffc4b3ba4b0a1db277984e212f2385322 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 17:53:21 +0000 Subject: [PATCH 131/319] Fixed a typo with the variant name being set on DM_Request_Cache. --- advanced-cache/classes/DM_Request_Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 8760f2ac..229a6991 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -239,6 +239,6 @@ private function set_variant_key() { $this->variant_key = md5( strval( $variant['key'] ) ); } - $this->variant_name = $default['standard']; + $this->variant_name = $default['name']; } } \ No newline at end of file From a424801ad96ea01835a6c8403f1d4a560b091b1e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 17:53:30 +0000 Subject: [PATCH 132/319] Added the variant name to the stats output. --- .../cli/DarkMatter_FullPage_CLI.php | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php index e4b699e0..79bb75c9 100644 --- a/advanced-cache/cli/DarkMatter_FullPage_CLI.php +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -12,6 +12,7 @@ class DarkMatter_FullPage_CLI { * * @param $args * @param $assoc_args + * @throws Exception */ public function stats( $args, $assoc_args ) { if ( empty( $args[0] ) ) { @@ -41,17 +42,27 @@ public function stats( $args, $assoc_args ) { $datetime->setTimezone( wp_timezone() ); $data[] = [ - 'Variant Key' => $variant_key, - 'Provider' => $request_data['provider'], - 'Time' => $datetime->format( 'r' ), - 'Remaining' => $remaining, - 'TTL' => human_time_diff( $time, $expiry_time ), - 'Size' => size_format( $variant_data['size_bytes'] ), - 'Headers' => $variant_data['headers'], + 'Variant Key' => $variant_key, + 'Variant Name' => $variant_data['name'], + 'Provider' => $request_data['provider'], + 'Time' => $datetime->format( 'r' ), + 'Remaining' => $remaining, + 'TTL' => human_time_diff( $time, $expiry_time ), + 'Size' => size_format( $variant_data['size_bytes'] ), + 'Headers' => $variant_data['headers'], ]; } - $display = [ 'Variant Key', 'Provider', 'Time', 'Remaining', 'TTL', 'Size', 'Headers' ]; + $display = [ + 'Variant Key', + 'Variant Name', + 'Provider', + 'Time', + 'Remaining', + 'TTL', + 'Size', + 'Headers' + ]; WP_CLI\Utils\format_items( 'table', $data, $display ); } From 5c6f9ae7f6d1ccf0274dfab8bc2cb0f630a96301 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 17:54:18 +0000 Subject: [PATCH 133/319] Changed stats subcommand to info as it is more accurate a name. --- advanced-cache/cli/DarkMatter_FullPage_CLI.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php index 79bb75c9..de92f830 100644 --- a/advanced-cache/cli/DarkMatter_FullPage_CLI.php +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -14,7 +14,7 @@ class DarkMatter_FullPage_CLI { * @param $assoc_args * @throws Exception */ - public function stats( $args, $assoc_args ) { + public function info( $args, $assoc_args ) { if ( empty( $args[0] ) ) { WP_CLI::error( __( 'Please provide a URL to retrieve cache statistics for.', 'dark-matter' ) ); } From 0a94622745a95aeb4b02fbf53c05ee4a0e754312 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 17:57:33 +0000 Subject: [PATCH 134/319] Added a format argument to the info subcommand. --- advanced-cache/cli/DarkMatter_FullPage_CLI.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php index de92f830..429d99dd 100644 --- a/advanced-cache/cli/DarkMatter_FullPage_CLI.php +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -10,6 +10,10 @@ class DarkMatter_FullPage_CLI { * * : Full URL to retrieve full page cache statistics. * + * [--format] + * : Determine which format that should be returned. Defaults to "table" and accepts "json", "csv", "yaml", and + * "count". + * * @param $args * @param $assoc_args * @throws Exception @@ -64,7 +68,9 @@ public function info( $args, $assoc_args ) { 'Headers' ]; - WP_CLI\Utils\format_items( 'table', $data, $display ); + $format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' ); + + WP_CLI\Utils\format_items( $format, $data, $display ); } } WP_CLI::add_command( 'darkmatter fullpage', 'DarkMatter_FullPage_CLI' ); \ No newline at end of file From 4cd80c36a3398abe45f304deabd2a7c0300f44ce Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 18:20:26 +0000 Subject: [PATCH 135/319] Added a get_data() to the DM_Request_Cache. --- advanced-cache/classes/DM_Request_Cache.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 229a6991..073bdd08 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -80,6 +80,15 @@ public function get() { return wp_cache_get( $this->key, 'dark-matter-fullpage' ); } + /** + * Retrieve the Request Cache Data record. + * + * @return DM_Request_Data Data record for the full page cache. + */ + public function get_data() { + return $this->request_data; + } + /** * Take headers and put them in a structure that is more consistent and more programmatically appeasing to use. * From e1b0656b4707a6dd93415cfde422eb3425faf918 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 18:25:10 +0000 Subject: [PATCH 136/319] Changed the info subcommand to make use of the DM_Request_Cache object instead. --- advanced-cache/cli/DarkMatter_FullPage_CLI.php | 4 ++-- advanced-cache/module.php | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php index 429d99dd..c70f5695 100644 --- a/advanced-cache/cli/DarkMatter_FullPage_CLI.php +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -25,8 +25,8 @@ public function info( $args, $assoc_args ) { $url = $args[0]; - $cache_data = new DM_Request_Data( $url ); - $request_data = $cache_data->data(); + $cache_entry = new DM_Request_Cache( $url ); + $request_data = $cache_entry->get_data()->data(); $data = []; diff --git a/advanced-cache/module.php b/advanced-cache/module.php index 0d8098d7..e85f5064 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -16,6 +16,7 @@ wp_cache_add_global_groups( 'dark-matter-fullpage-data' ); require_once DM_PATH . 'advanced-cache/classes/DM_Cache_Post.php'; +require_once DM_PATH . 'advanced-cache/classes/DM_Request_Cache.php'; require_once DM_PATH . 'advanced-cache/classes/DM_Request_Data.php'; if ( defined( 'WP_CLI' ) && WP_CLI ) { From 4ea9063b1e6e4eeb2b8c43f1f75a711f340fea47 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 18:55:14 +0000 Subject: [PATCH 137/319] Fixed a few notices on the invalidate method now that the Variant records contain more info. --- advanced-cache/classes/DM_Request_Data.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index eeb72f38..1bd5b7ae 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -60,10 +60,9 @@ public function invalidate( $variant_key = '' ) { /** * Remove from the list of variants. */ - $position = array_search( $variant_key, $this->data['variants'], true ); - unset( $this->data['variants'][ $position ] ); + unset( $this->data['variants'][ $variant_key ] ); } else { - foreach ( $this->data['variants'] as $key ) { + foreach ( $this->data['variants'] as $key => $data ) { wp_cache_delete( $key, 'dark-matter-fullpage' ); } From 676d06e8d74c0449f40d8ec85c23bd25439cfc8a Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 18:55:36 +0000 Subject: [PATCH 138/319] Added the logic for the invalidate subcommand. --- .../cli/DarkMatter_FullPage_CLI.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php index c70f5695..0df04cc3 100644 --- a/advanced-cache/cli/DarkMatter_FullPage_CLI.php +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -72,5 +72,45 @@ public function info( $args, $assoc_args ) { WP_CLI\Utils\format_items( $format, $data, $display ); } + + /** + * Invalidate a full page cache entry, either by Variant Key or URL. + * + * ### OPTIONS + * + * + * : URL to invalidate. + * + * [] + * : Specify a specific variant to remove, if there are multiples. + * + * @param $args + * @param $assoc_args + */ + public function invalidate( $args, $assoc_args ) { + if ( empty( $args[0] ) ) { + WP_CLI::error( __( 'Please provide either a Variant Key or a URL to perform an invalidation.', 'dark-matter' ) ); + } + + $url = $args[0]; + $key = ( empty( $args[1] ) ? '' : $args[1] ); + + $cache_entry = new DM_Request_Cache( $url ); + $data_entry = $cache_entry->get_data(); + $request_data = $data_entry->data(); + + if ( $request_data['count'] > 0 ) { + if ( $request_data['count'] > 1 ) { + WP_CLI::confirm( __( 'This URL has multiple cache variants and this command will clear all. Do you wish to proceed?', 'dark-matter' ), $assoc_args ); + } + + $data_entry->invalidate(); + + WP_CLI::success( $request_data['count'] . __( ' cache variants deleted.', 'dark-matter' ) ); + } + else { + WP_CLI::error( __( 'No cache entries are available for this URL.', 'dark-matter' ) ); + } + } } WP_CLI::add_command( 'darkmatter fullpage', 'DarkMatter_FullPage_CLI' ); \ No newline at end of file From 06be7b67d904e406df2943463d99e3403fcb7f7c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 19:10:47 +0000 Subject: [PATCH 139/319] Fixed a typo preventing another source using a custom variant. --- advanced-cache/classes/DM_Request_Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 073bdd08..1faab476 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -248,6 +248,6 @@ private function set_variant_key() { $this->variant_key = md5( strval( $variant['key'] ) ); } - $this->variant_name = $default['name']; + $this->variant_name = $variant['name']; } } \ No newline at end of file From e7d5aed11a2bcd6389d270e9cd11e22bbfbac05d Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 19:21:23 +0000 Subject: [PATCH 140/319] Added an optional config file include for advanced cache. --- advanced-cache/advanced-cache.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/advanced-cache/advanced-cache.php b/advanced-cache/advanced-cache.php index 94786f36..11ce49cf 100644 --- a/advanced-cache/advanced-cache.php +++ b/advanced-cache/advanced-cache.php @@ -11,6 +11,18 @@ define( 'DARKMATTER_FULLPAGECACHE', true ); } +/** + * Allow for the inclusion of third party logic which can utilise the hooks within Advanced Cache. + */ +$cache_config_path = WP_CONTENT_DIR . '/advanced-cache-config.php'; + +if ( is_readable( $cache_config_path ) ) { + require_once( $cache_config_path ); +} + +/** + * Load the Advanced Cache logic to perform the full page cache. + */ $cache_path = ( dirname( __FILE__ ) . '/plugins/dark-matter/advanced-cache/inc/advanced-cache.php' ); if ( is_readable( $cache_path ) ) { From 38bae176960f3546a49217ee6ea0ee3422cde81b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 19:28:39 +0000 Subject: [PATCH 141/319] Added a subcommand for updating the dropin plugin. --- .../cli/DarkMatter_FullPage_Dropin_CLI.php | 78 +++++++++++++++++++ advanced-cache/module.php | 1 + 2 files changed, 79 insertions(+) create mode 100644 advanced-cache/cli/DarkMatter_FullPage_Dropin_CLI.php diff --git a/advanced-cache/cli/DarkMatter_FullPage_Dropin_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_Dropin_CLI.php new file mode 100644 index 00000000..48cfc4b7 --- /dev/null +++ b/advanced-cache/cli/DarkMatter_FullPage_Dropin_CLI.php @@ -0,0 +1,78 @@ + false, + ] ); + + if ( false === is_writable( WP_CONTENT_DIR ) ) { + WP_CLI::error( __( 'The /wp-content/ directory needs to be writable by the current user in order to update.', 'dark-matter' ) ); + } + + if ( false === $opts['force'] && file_exists( $destination ) ) { + WP_CLI::error( __( 'advanced-cache.php is already present. Use the --force flag to override.', 'dark-matter' ) ); + } + + if ( false === is_readable( $source ) ) { + WP_CLI::error( __( 'Cannot read the advanced-cache.php dropin within the Dark Matter plugin folder.', 'dark-matter' ) ); + } + + if ( false === file_exists( $source ) ) { + WP_CLI::error( __( 'advanced-cache.php dropin within the Dark Matter plugin is missing.', 'dark-matter' ) ); + } + + if ( @copy( $source, $destination ) ) { + WP_CLI::success( __( 'Updated the advanced-cache.php dropin to the latest version.', 'dark-matter' ) ); + } else { + WP_CLI::error( __( 'Unknown error occurred preventing the update of Full Page Cache dropin.', 'dark-matter' ) ); + } + } +} +WP_CLI::add_command( 'darkmatter fullpage dropin', 'DarkMatter_FullPage_Dropin_CLI' ); \ No newline at end of file diff --git a/advanced-cache/module.php b/advanced-cache/module.php index e85f5064..b68c2110 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -21,4 +21,5 @@ if ( defined( 'WP_CLI' ) && WP_CLI ) { require_once DM_PATH . 'advanced-cache/cli/DarkMatter_FullPage_CLI.php'; + require_once DM_PATH . 'advanced-cache/cli/DarkMatter_FullPage_Dropin_CLI.php'; } \ No newline at end of file From 332baa5aca8c83c70e9941a0d67861922f43748a Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 19:46:40 +0000 Subject: [PATCH 142/319] Ensured that the advanced-cache-config.php loads for the WP CLI. --- advanced-cache/module.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/advanced-cache/module.php b/advanced-cache/module.php index b68c2110..b3880858 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -20,6 +20,12 @@ require_once DM_PATH . 'advanced-cache/classes/DM_Request_Data.php'; if ( defined( 'WP_CLI' ) && WP_CLI ) { + $cache_config_path = WP_CONTENT_DIR . '/advanced-cache-config.php'; + + if ( is_readable( $cache_config_path ) ) { + require_once( $cache_config_path ); + } + require_once DM_PATH . 'advanced-cache/cli/DarkMatter_FullPage_CLI.php'; require_once DM_PATH . 'advanced-cache/cli/DarkMatter_FullPage_Dropin_CLI.php'; } \ No newline at end of file From bdd29438c548875e9c7d198881b24965b338fded Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 19:47:13 +0000 Subject: [PATCH 143/319] Fixed an issue where the dark-matter-fullpage-data cache group wasn't network-wide. --- advanced-cache/inc/advanced-cache.php | 1 + 1 file changed, 1 insertion(+) diff --git a/advanced-cache/inc/advanced-cache.php b/advanced-cache/inc/advanced-cache.php index af45c640..05393639 100644 --- a/advanced-cache/inc/advanced-cache.php +++ b/advanced-cache/inc/advanced-cache.php @@ -25,6 +25,7 @@ } wp_cache_add_global_groups( 'dark-matter-fullpage' ); +wp_cache_add_global_groups( 'dark-matter-fullpage-data' ); /** * Prior to loading the library and processing the cache, determine if the current installation includes a file for From f36bc7e702f644ca53f037c156d6d090f8981edb Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 19:53:50 +0000 Subject: [PATCH 144/319] Fixed issues with the invalidate() method of the DM_Request_Data. --- advanced-cache/classes/DM_Request_Data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Data.php b/advanced-cache/classes/DM_Request_Data.php index 1bd5b7ae..368f8937 100644 --- a/advanced-cache/classes/DM_Request_Data.php +++ b/advanced-cache/classes/DM_Request_Data.php @@ -54,7 +54,7 @@ public function data() { * @param string $variant_key Invalidate a specific key. */ public function invalidate( $variant_key = '' ) { - if ( empty( $variant_key ) && in_array( $variant_key, $this->data['variants'], true ) ) { + if ( ! empty( $variant_key ) && ! empty( $this->data['variants'][ $variant_key ] ) ) { wp_cache_delete( $variant_key, 'dark-matter-fullpage' ); /** From 3be8cda205a86f7b028559809fbb93725e9adf50 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 5 Jan 2020 19:54:51 +0000 Subject: [PATCH 145/319] Added the ability to invalidate a specific variant. --- advanced-cache/cli/DarkMatter_FullPage_CLI.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php index 0df04cc3..017624c2 100644 --- a/advanced-cache/cli/DarkMatter_FullPage_CLI.php +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -99,7 +99,19 @@ public function invalidate( $args, $assoc_args ) { $data_entry = $cache_entry->get_data(); $request_data = $data_entry->data(); - if ( $request_data['count'] > 0 ) { + /** + * Invalidate a specifc key. + */ + if ( ! empty( $key ) ) { + $data_entry->invalidate( $key ); + + WP_CLI::success( __( 'Full Page Cache variant has been invalidated.', 'dark-matter' ) ); + } + else if ( $request_data['count'] > 0 ) { + /** + * If there is more than one variant, double-check to ensure the admin wants to delete ALL the variants. If + * there is only one, then just continue as normal. + */ if ( $request_data['count'] > 1 ) { WP_CLI::confirm( __( 'This URL has multiple cache variants and this command will clear all. Do you wish to proceed?', 'dark-matter' ), $assoc_args ); } From 7482857b1733805832f583a1363f471c87bae92b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 6 Jan 2020 20:54:57 +0000 Subject: [PATCH 146/319] Added a pre-cache output action. --- advanced-cache/classes/DM_Request_Cache.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 1faab476..45a0805f 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -127,6 +127,14 @@ public function set( $output = '', $headers = [] ) { 'redirect' => false, ]; + /** + * Enables the modification of the HTML output and corresponding headers prior to it to being stored in cache. + * This is useful for one-time modifications at the caching "point of entry". + * + * @param Array $data Array structure containing the HTML, headers, and whether it is a redirect. + */ + do_action( 'dark_matter_pre_cache_output', $data ); + $ttl = 5 * MINUTE_IN_SECONDS; if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage', $ttl ) ) { From a16b80c8bbdf41082c7a28cac787c9a647a0b0ef Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Mon, 6 Jan 2020 20:58:24 +0000 Subject: [PATCH 147/319] Moved the TTL in to its own method and added a filter. --- advanced-cache/classes/DM_Request_Cache.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index 45a0805f..ac381901 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -89,6 +89,15 @@ public function get_data() { return $this->request_data; } + /** + * Returns the TTL for full page cache entries, allowing for modification in the advanced-cache-config.php. + * + * @return integer TTL in seconds. + */ + private function get_ttl() { + return apply_filters( 'dark_matter_cache_ttl', 5 * MINUTE_IN_SECONDS, $this->url ); + } + /** * Take headers and put them in a structure that is more consistent and more programmatically appeasing to use. * @@ -135,7 +144,7 @@ public function set( $output = '', $headers = [] ) { */ do_action( 'dark_matter_pre_cache_output', $data ); - $ttl = 5 * MINUTE_IN_SECONDS; + $ttl = $this->get_ttl(); if ( wp_cache_set( $this->key, $data, 'dark-matter-fullpage', $ttl ) ) { $this->request_data->variant_add( $this->key, $this->variant_name, $data, $ttl ); From eb4632d6e6505dcdbc6f01945e28c64386fa2a15 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 13:57:11 +0000 Subject: [PATCH 148/319] Created a new Cache Info class. --- advanced-cache/classes/DM_Cache_Info.php | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 advanced-cache/classes/DM_Cache_Info.php diff --git a/advanced-cache/classes/DM_Cache_Info.php b/advanced-cache/classes/DM_Cache_Info.php new file mode 100644 index 00000000..7d872540 --- /dev/null +++ b/advanced-cache/classes/DM_Cache_Info.php @@ -0,0 +1,43 @@ +cache_entry = new DM_Request_Cache( $url ); + $this->cache_data = $this->cache_entry->get_data()->data(); + } + + /** + * Returns all the available cache information. + * + * @return array Cache information for the URL and all variants. + */ + public function get_all() { + return $this->data; + } +} \ No newline at end of file From c28f021e16313abf3de37bed828f28eb52c0a618 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 13:57:29 +0000 Subject: [PATCH 149/319] Added the logic for constructing Cache Info data set. --- advanced-cache/classes/DM_Cache_Info.php | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/advanced-cache/classes/DM_Cache_Info.php b/advanced-cache/classes/DM_Cache_Info.php index 7d872540..a6192889 100644 --- a/advanced-cache/classes/DM_Cache_Info.php +++ b/advanced-cache/classes/DM_Cache_Info.php @@ -30,6 +30,44 @@ class DM_Cache_Info { public function __construct( $url = '' ) { $this->cache_entry = new DM_Request_Cache( $url ); $this->cache_data = $this->cache_entry->get_data()->data(); + + $this->compile_data(); + } + + /** + * Construct the overall data record. + */ + private function compile_data() { + if ( empty( $this->cache_data ) ) { + return ''; + } + + foreach ( $this->cache_data['variants'] as $variant_key => $variant_data ) { + $time = $variant_data['time_utc']; + $expiry_time = $time + $variant_data['ttl_secs']; + $remaining = human_time_diff( time(), $expiry_time ); + + if ( time() > $expiry_time ) { + $remaining = __( 'Expired', 'dark-matter' ); + } + + /** + * Convert the Unix timestamp to a human readable format and in the website's current timezone. + */ + $datetime = new DateTime( "@{$time}" ); + $datetime->setTimezone( wp_timezone() ); + + $this->data[] = [ + 'Variant Key' => $variant_key, + 'Variant Name' => $variant_data['name'], + 'Provider' => $this->cache_data['provider'], + 'Time' => $datetime->format( 'r' ), + 'Remaining' => $remaining, + 'TTL' => human_time_diff( $time, $expiry_time ), + 'Size' => size_format( $variant_data['size_bytes'] ), + 'Headers' => $variant_data['headers'], + ]; + } } /** From 890f75e2e19e89416eb88166010f2c34533dde99 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 14:01:39 +0000 Subject: [PATCH 150/319] Included the DM_Cache_Info file. --- advanced-cache/module.php | 1 + 1 file changed, 1 insertion(+) diff --git a/advanced-cache/module.php b/advanced-cache/module.php index b3880858..adbe35fd 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -15,6 +15,7 @@ wp_cache_add_global_groups( 'dark-matter-fullpage' ); wp_cache_add_global_groups( 'dark-matter-fullpage-data' ); +require_once DM_PATH . 'advanced-cache/classes/DM_Cache_Info.php'; require_once DM_PATH . 'advanced-cache/classes/DM_Cache_Post.php'; require_once DM_PATH . 'advanced-cache/classes/DM_Request_Cache.php'; require_once DM_PATH . 'advanced-cache/classes/DM_Request_Data.php'; From c050323d22c624739d43e87ee1715f289e488c48 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 14:15:42 +0000 Subject: [PATCH 151/319] Made friendlier field names. --- advanced-cache/classes/DM_Cache_Info.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/advanced-cache/classes/DM_Cache_Info.php b/advanced-cache/classes/DM_Cache_Info.php index a6192889..97f12ca2 100644 --- a/advanced-cache/classes/DM_Cache_Info.php +++ b/advanced-cache/classes/DM_Cache_Info.php @@ -58,14 +58,14 @@ private function compile_data() { $datetime->setTimezone( wp_timezone() ); $this->data[] = [ - 'Variant Key' => $variant_key, - 'Variant Name' => $variant_data['name'], - 'Provider' => $this->cache_data['provider'], - 'Time' => $datetime->format( 'r' ), - 'Remaining' => $remaining, - 'TTL' => human_time_diff( $time, $expiry_time ), - 'Size' => size_format( $variant_data['size_bytes'] ), - 'Headers' => $variant_data['headers'], + 'variant_key' => $variant_key, + 'variant_name' => $variant_data['name'], + 'provider' => $this->cache_data['provider'], + 'time' => $datetime->format( 'r' ), + 'remaining' => $remaining, + 'ttl' => human_time_diff( $time, $expiry_time ), + 'size' => size_format( $variant_data['size_bytes'] ), + 'headers' => $variant_data['headers'], ]; } } From d141961a03edb5ef50f8421e5b9629ade0e55fdd Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 14:15:56 +0000 Subject: [PATCH 152/319] Replaced the old logic with the new DM_Cache_Info on the info subcommand. --- .../cli/DarkMatter_FullPage_CLI.php | 49 ++++--------------- 1 file changed, 10 insertions(+), 39 deletions(-) diff --git a/advanced-cache/cli/DarkMatter_FullPage_CLI.php b/advanced-cache/cli/DarkMatter_FullPage_CLI.php index 017624c2..c144de4c 100644 --- a/advanced-cache/cli/DarkMatter_FullPage_CLI.php +++ b/advanced-cache/cli/DarkMatter_FullPage_CLI.php @@ -25,47 +25,18 @@ public function info( $args, $assoc_args ) { $url = $args[0]; - $cache_entry = new DM_Request_Cache( $url ); - $request_data = $cache_entry->get_data()->data(); - - $data = []; - - foreach ( $request_data['variants'] as $variant_key => $variant_data ) { - $time = $variant_data['time_utc']; - $expiry_time = $time + $variant_data['ttl_secs']; - $remaining = human_time_diff( time(), $expiry_time ); - - if ( time() > $expiry_time ) { - $remaining = __( 'Expired', 'dark-matter' ); - } - - /** - * Convert the Unix timestamp to a human readable format and in the website's current timezone. - */ - $datetime = new DateTime( "@{$time}" ); - $datetime->setTimezone( wp_timezone() ); - - $data[] = [ - 'Variant Key' => $variant_key, - 'Variant Name' => $variant_data['name'], - 'Provider' => $request_data['provider'], - 'Time' => $datetime->format( 'r' ), - 'Remaining' => $remaining, - 'TTL' => human_time_diff( $time, $expiry_time ), - 'Size' => size_format( $variant_data['size_bytes'] ), - 'Headers' => $variant_data['headers'], - ]; - } + $cache_info = new DM_Cache_Info( $url ); + $data = $cache_info->get_all(); $display = [ - 'Variant Key', - 'Variant Name', - 'Provider', - 'Time', - 'Remaining', - 'TTL', - 'Size', - 'Headers' + 'variant_key', + 'variant_name', + 'provider', + 'time', + 'remaining', + 'ttl', + 'size', + 'headers' ]; $format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' ); From dbe9c7c48f3886565e6059f0bcdfe4d064d6dc7c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 14:22:48 +0000 Subject: [PATCH 153/319] Added a new class for handling the Admin UI functionality. --- advanced-cache/classes/DM_Cache_Admin_UI.php | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 advanced-cache/classes/DM_Cache_Admin_UI.php diff --git a/advanced-cache/classes/DM_Cache_Admin_UI.php b/advanced-cache/classes/DM_Cache_Admin_UI.php new file mode 100644 index 00000000..81ac7eb0 --- /dev/null +++ b/advanced-cache/classes/DM_Cache_Admin_UI.php @@ -0,0 +1,26 @@ + Date: Sat, 11 Jan 2020 14:24:22 +0000 Subject: [PATCH 154/319] Added logic to determine when to add to the Admin Menu Bar. --- advanced-cache/classes/DM_Cache_Admin_UI.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/advanced-cache/classes/DM_Cache_Admin_UI.php b/advanced-cache/classes/DM_Cache_Admin_UI.php index 81ac7eb0..16886a03 100644 --- a/advanced-cache/classes/DM_Cache_Admin_UI.php +++ b/advanced-cache/classes/DM_Cache_Admin_UI.php @@ -6,6 +6,12 @@ class DM_Cache_Admin_UI { * DM_Cache_Admin_UI constructor. */ public function __construct() { + /** + * Only show the Admin Bar elements if on the mapped / front-end domain. + */ + if ( is_admin() || ! is_user_logged_in() ) { + return; + } } /** From 69e1f7d27317d0df0d9e9551deaa6134394f184a Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 14:25:39 +0000 Subject: [PATCH 155/319] Added a permission check. --- advanced-cache/classes/DM_Cache_Admin_UI.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/advanced-cache/classes/DM_Cache_Admin_UI.php b/advanced-cache/classes/DM_Cache_Admin_UI.php index 16886a03..a9d94cfb 100644 --- a/advanced-cache/classes/DM_Cache_Admin_UI.php +++ b/advanced-cache/classes/DM_Cache_Admin_UI.php @@ -12,6 +12,13 @@ public function __construct() { if ( is_admin() || ! is_user_logged_in() ) { return; } + + /** + * Ensure the current user is an admin. + */ + if ( ! current_user_can( 'manage_options' ) ) { + return; + } } /** From f1027c6be33ae244373b22b55aec53f590d50189 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 14:59:18 +0000 Subject: [PATCH 156/319] Added a method for getting the current URL. --- advanced-cache/classes/DM_Cache_Admin_UI.php | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/advanced-cache/classes/DM_Cache_Admin_UI.php b/advanced-cache/classes/DM_Cache_Admin_UI.php index a9d94cfb..531a45ec 100644 --- a/advanced-cache/classes/DM_Cache_Admin_UI.php +++ b/advanced-cache/classes/DM_Cache_Admin_UI.php @@ -19,6 +19,27 @@ public function __construct() { if ( ! current_user_can( 'manage_options' ) ) { return; } + + /** + * Returns the current URL. + * + * @return string Current URL. + */ + private function get_url() { + $protocol = 'http://'; + if ( isset( $_SERVER['HTTPS'] ) ) { + if ( 'on' == strtolower( $_SERVER['HTTPS'] ) || '1' == $_SERVER['HTTPS'] ) { + $protocol = 'https://'; + } + } elseif ( isset( $_SERVER['SERVER_PORT'] ) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { + $protocol = 'https://'; + } + + $host = rtrim( trim( $_SERVER['HTTP_HOST'] ), '/' ); + $path = ltrim( trim( $_SERVER['REQUEST_URI'] ), '/' ); + + return $protocol . $host . '/' . $path; + } } /** From fbe0b16854753523509b1d9a58f0f2e301df33e5 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 15:02:14 +0000 Subject: [PATCH 157/319] Built a get_standard() method for the Cache Info. --- advanced-cache/classes/DM_Cache_Info.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/advanced-cache/classes/DM_Cache_Info.php b/advanced-cache/classes/DM_Cache_Info.php index 97f12ca2..57c086dc 100644 --- a/advanced-cache/classes/DM_Cache_Info.php +++ b/advanced-cache/classes/DM_Cache_Info.php @@ -78,4 +78,17 @@ private function compile_data() { public function get_all() { return $this->data; } + + /** + * Retrieves the standard variant. + * + * @return array Data record for the "standard" Variant. + */ + public function get_standard() { + foreach ( $this->data as $record ) { + if ( 'standard' === $record['variant_name'] ) { + return $record; + } + } + } } \ No newline at end of file From 61f68db7c63bdc928a68e048f5e8b961180dd16c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 15:08:22 +0000 Subject: [PATCH 158/319] Added a get_cache_info() method. --- advanced-cache/classes/DM_Cache_Admin_UI.php | 17 ++++++++++++++++- advanced-cache/classes/DM_Cache_Info.php | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Cache_Admin_UI.php b/advanced-cache/classes/DM_Cache_Admin_UI.php index 531a45ec..6156afb4 100644 --- a/advanced-cache/classes/DM_Cache_Admin_UI.php +++ b/advanced-cache/classes/DM_Cache_Admin_UI.php @@ -6,6 +6,10 @@ class DM_Cache_Admin_UI { * DM_Cache_Admin_UI constructor. */ public function __construct() { + add_action( 'admin_init', [ $this, 'init' ] ); + } + + public function init() { /** * Only show the Admin Bar elements if on the mapped / front-end domain. */ @@ -20,6 +24,18 @@ public function __construct() { return; } + add_action( 'admin_bar_menu', [ $this, 'admin_bar_info' ] ); + } + + /** + * Retrieves the cache information. + * + * @return DM_Cache_Info Object containing the relevant cache information. + */ + private function get_cache_info() { + return new DM_Cache_Info( $this->get_url() ); + } + /** * Returns the current URL. * @@ -40,7 +56,6 @@ private function get_url() { return $protocol . $host . '/' . $path; } - } /** * Return the Singleton Instance of the class. diff --git a/advanced-cache/classes/DM_Cache_Info.php b/advanced-cache/classes/DM_Cache_Info.php index 57c086dc..89f72061 100644 --- a/advanced-cache/classes/DM_Cache_Info.php +++ b/advanced-cache/classes/DM_Cache_Info.php @@ -90,5 +90,7 @@ public function get_standard() { return $record; } } + + return null; } } \ No newline at end of file From 3abcbb90b54f3ecb4d74f91c98aebed075fa56ca Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 15:29:29 +0000 Subject: [PATCH 159/319] Fixed an issue where checking if a cache entry can be created was too late in the process. --- advanced-cache/classes/DM_Advanced_Cache.php | 46 ++++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/advanced-cache/classes/DM_Advanced_Cache.php b/advanced-cache/classes/DM_Advanced_Cache.php index 3fc512a8..f232849a 100644 --- a/advanced-cache/classes/DM_Advanced_Cache.php +++ b/advanced-cache/classes/DM_Advanced_Cache.php @@ -2,6 +2,11 @@ defined( 'ABSPATH' ) || die; class DM_Advanced_Cache { + /** + * @var bool States if the current request can be cached. + */ + private $can_cache = false; + /** * @var DM_Request_Cache Request Cache object. */ @@ -34,6 +39,14 @@ class DM_Advanced_Cache { public function __construct() { $this->set_url(); + /** + * Determine if we can cache the current request. + */ + $this->can_cache = $this->can_cache(); + if ( ! $this->can_cache ) { + return; + } + $this->request = new DM_Request_Cache( $this->url ); $cache_data = $this->request->get(); @@ -126,11 +139,11 @@ public function cache_output( $output = '' ) { } /** - * Determine if the current response should be cached. + * Determines if a full page cache entry can be created. * - * @return boolean Return true if the current response should be cached. False if it should not. + * @return bool True if the request can be cached. False otherwise. */ - public function do_cache() { + public function can_cache() { /** * Hard-set checks. The caching cannot be perform if for the following; * @@ -147,31 +160,38 @@ public function do_cache() { return false; } - $do_cache = true; - /** * Ensure the Response Type can be cached. */ if ( ! in_array( $this->response_type, [ 'page', 'redirect' ], true ) ) { - $do_cache = false; + return false; } /** * Check Cookies to make sure that caching is suitable, i.e. do not cache if the User is logged in. */ - if ( $do_cache && ! $this->do_cache_scriptname() ) { - $do_cache = false; + if ( ! $this->do_cache_scriptname() ) { + return false; } - if ( $do_cache && ! $this->do_cache_cookies() ) { - $do_cache = false; + if ( ! $this->do_cache_cookies() ) { + return false; } - if ( $do_cache && ! $this->do_cache_querystring() ) { - $do_cache = false; + if ( ! $this->do_cache_querystring() ) { + return false; } - return apply_filters( 'dark_matter_do_cache', $do_cache, $this->response_type, $this->status_code ); + return true; + } + + /** + * Determine if the current response should be cached. + * + * @return boolean Return true if the current response should be cached. False if it should not. + */ + public function do_cache() { + return apply_filters( 'dark_matter_do_cache', $this->can_cache, $this->response_type, $this->status_code ); } /** From 806a374e5c5a7074cee6b6dcc53f2a81673fbe11 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 15:29:51 +0000 Subject: [PATCH 160/319] Added the logic for display a basic admin bar of the fullpage cache info. --- advanced-cache/classes/DM_Cache_Admin_UI.php | 39 +++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/advanced-cache/classes/DM_Cache_Admin_UI.php b/advanced-cache/classes/DM_Cache_Admin_UI.php index 6156afb4..8cf39633 100644 --- a/advanced-cache/classes/DM_Cache_Admin_UI.php +++ b/advanced-cache/classes/DM_Cache_Admin_UI.php @@ -6,7 +6,7 @@ class DM_Cache_Admin_UI { * DM_Cache_Admin_UI constructor. */ public function __construct() { - add_action( 'admin_init', [ $this, 'init' ] ); + add_action( 'init', [ $this, 'init' ] ); } public function init() { @@ -24,7 +24,42 @@ public function init() { return; } - add_action( 'admin_bar_menu', [ $this, 'admin_bar_info' ] ); + add_action( 'admin_bar_menu', [ $this, 'admin_bar_info' ], 100 ); + } + + /** + * Add the information box to the Admin Bar. + * + * @param WP_Admin_Bar $admin_bar + */ + public function admin_bar_info( $admin_bar = null ) { + $cache_info = $this->get_cache_info(); + + /** + * Retrieve the standard variant. All except the more exotic websites will use this version. + */ + $standard_variant = $cache_info->get_standard(); + + if ( empty( $standard_variant ) ) { + $icon = ''; + $text = 'Not cached.'; + } + else { + $dashicon = ( 'Expired' === $standard_variant['remaining'] ? 'dismiss' : 'yes-alt' ); + + $icon = sprintf( '', esc_attr( $dashicon ) ); + $text = $standard_variant['remaining']; + } + + $admin_bar->add_menu( [ + 'id' => 'darkmatter-fullpage', + 'parent' => null, + 'group' => null, + 'title' => $icon . $text, + 'meta' => [ + 'title' => __( 'Cache details', 'dark-matter' ), + ], + ] ); } /** From 36e2b8411722c3f2838bd23dfa19e72bd03784e3 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 15:39:01 +0000 Subject: [PATCH 161/319] Added a method to return the number of variants. --- advanced-cache/classes/DM_Cache_Info.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/advanced-cache/classes/DM_Cache_Info.php b/advanced-cache/classes/DM_Cache_Info.php index 89f72061..79119804 100644 --- a/advanced-cache/classes/DM_Cache_Info.php +++ b/advanced-cache/classes/DM_Cache_Info.php @@ -93,4 +93,13 @@ public function get_standard() { return null; } + + /** + * Returns the number of variant records. + * + * @return int Number of Variant records. + */ + public function get_variant_count() { + return count( $this->data ); + } } \ No newline at end of file From a8251908d501629d430570b866b66e1a93d489f0 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 15:39:23 +0000 Subject: [PATCH 162/319] Increase the amount of information displayed for the cache details on the admin bar. --- advanced-cache/classes/DM_Cache_Admin_UI.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Cache_Admin_UI.php b/advanced-cache/classes/DM_Cache_Admin_UI.php index 8cf39633..737c9bf1 100644 --- a/advanced-cache/classes/DM_Cache_Admin_UI.php +++ b/advanced-cache/classes/DM_Cache_Admin_UI.php @@ -48,7 +48,14 @@ public function admin_bar_info( $admin_bar = null ) { $dashicon = ( 'Expired' === $standard_variant['remaining'] ? 'dismiss' : 'yes-alt' ); $icon = sprintf( '', esc_attr( $dashicon ) ); - $text = $standard_variant['remaining']; + + $display_data = [ + $standard_variant['remaining'], + $standard_variant['size'], + sprintf( '%1$d %2$s', $cache_info->get_variant_count(), __( 'Variants', 'dark-matter' ) ), + ]; + + $text = implode( ' / ', $display_data ); } $admin_bar->add_menu( [ From b3b027a3ad997660679aaf2fd290b8edf9bbdab8 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 15:39:47 +0000 Subject: [PATCH 163/319] Included the Admin UI class file. --- advanced-cache/classes/DM_Cache_Admin_UI.php | 3 +++ advanced-cache/module.php | 1 + 2 files changed, 4 insertions(+) diff --git a/advanced-cache/classes/DM_Cache_Admin_UI.php b/advanced-cache/classes/DM_Cache_Admin_UI.php index 737c9bf1..5776efe6 100644 --- a/advanced-cache/classes/DM_Cache_Admin_UI.php +++ b/advanced-cache/classes/DM_Cache_Admin_UI.php @@ -9,6 +9,9 @@ public function __construct() { add_action( 'init', [ $this, 'init' ] ); } + /** + * Initialise the admin bar functionality. + */ public function init() { /** * Only show the Admin Bar elements if on the mapped / front-end domain. diff --git a/advanced-cache/module.php b/advanced-cache/module.php index adbe35fd..ce710e1e 100644 --- a/advanced-cache/module.php +++ b/advanced-cache/module.php @@ -15,6 +15,7 @@ wp_cache_add_global_groups( 'dark-matter-fullpage' ); wp_cache_add_global_groups( 'dark-matter-fullpage-data' ); +require_once DM_PATH . 'advanced-cache/classes/DM_Cache_Admin_UI.php'; require_once DM_PATH . 'advanced-cache/classes/DM_Cache_Info.php'; require_once DM_PATH . 'advanced-cache/classes/DM_Cache_Post.php'; require_once DM_PATH . 'advanced-cache/classes/DM_Request_Cache.php'; From fd14d361615c59e1e660c02bb6e0864bd2d2e8b2 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 11 Jan 2020 23:00:52 +0000 Subject: [PATCH 164/319] Added variant name to the TTL value filter. --- advanced-cache/classes/DM_Request_Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-cache/classes/DM_Request_Cache.php b/advanced-cache/classes/DM_Request_Cache.php index ac381901..defae28a 100644 --- a/advanced-cache/classes/DM_Request_Cache.php +++ b/advanced-cache/classes/DM_Request_Cache.php @@ -95,7 +95,7 @@ public function get_data() { * @return integer TTL in seconds. */ private function get_ttl() { - return apply_filters( 'dark_matter_cache_ttl', 5 * MINUTE_IN_SECONDS, $this->url ); + return apply_filters( 'dark_matter_cache_ttl', 5 * MINUTE_IN_SECONDS, $this->url, $this->variant_name ); } /** From b2206ab82a82629525527730d77486e414c824d1 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Fri, 15 Apr 2022 15:39:06 +0100 Subject: [PATCH 165/319] Added the PSR-4 autoloader to composer. --- composer.json | 5 +++++ composer.lock | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index aa953482..ccbd92fe 100644 --- a/composer.json +++ b/composer.json @@ -27,5 +27,10 @@ "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } + }, + "autoload": { + "psr-4": { + "DarkMatter\\": "includes/classes/" + } } } diff --git a/composer.lock b/composer.lock index 36dedc68..58f70bf1 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "629a939fe025f5824532e411494f8a21", + "content-hash": "d960fce3b1cce9180a7c5995435372e9", "packages": [], "packages-dev": [ { From 4ebaf067b77a770ab862d443edf96e32693b01cf Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Fri, 15 Apr 2022 15:39:15 +0100 Subject: [PATCH 166/319] Created a Registerable interface. --- includes/classes/Registerable.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 includes/classes/Registerable.php diff --git a/includes/classes/Registerable.php b/includes/classes/Registerable.php new file mode 100644 index 00000000..d0c23f15 --- /dev/null +++ b/includes/classes/Registerable.php @@ -0,0 +1,22 @@ + Date: Fri, 15 Apr 2022 15:42:41 +0100 Subject: [PATCH 167/319] Added the require for the PSR-4 autoloader. --- dark-matter.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dark-matter.php b/dark-matter.php index 6b3156d8..6c05cd57 100644 --- a/dark-matter.php +++ b/dark-matter.php @@ -44,6 +44,13 @@ */ wp_cache_add_global_groups( 'dark-matter' ); +/** + * Include the PSR-4 autoloader. + */ +if ( file_exists( DM_PATH . 'vendor/autoload.php' ) ) { + require_once DM_PATH . 'vendor/autoload.php'; +} + require_once DM_PATH . '/dark-matter/class-dm-pluginupdate.php'; new DM_PluginUpdate(); From eb0a3174ab988d3844a73bf051b8eb6f8ffda336 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 10:35:27 +0100 Subject: [PATCH 168/319] Stopped the deletion of the vendor folder for releases (needed for autoloader). --- scripts/release.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/release.sh b/scripts/release.sh index 0786bd40..33df62b9 100644 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -18,7 +18,6 @@ rm -rf domain-mapping/ui rm -rf node_modules rm -rf scripts rm -rf tests -rm -rf vendor rm -rf .* rm *.json rm *.lock From 19b4f2323f90d2258557c580aa1e3bf3b3eb1704 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 21 May 2022 08:38:18 +0100 Subject: [PATCH 169/319] Created a new class to handle the instantiation. --- includes/classes/DarkMatter.php | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 includes/classes/DarkMatter.php diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php new file mode 100644 index 00000000..e1334089 --- /dev/null +++ b/includes/classes/DarkMatter.php @@ -0,0 +1,35 @@ + Date: Sat, 29 Oct 2022 13:08:41 +0100 Subject: [PATCH 170/319] Removed the Instructions namespace from 3.0.0 branch. --- includes/classes/DarkMatter.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index e1334089..d59324b6 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -5,8 +5,6 @@ * @package DarkMatter */ -use DarkMatter\AdvancedCache\Processor\Instructions; - /** * Class DarkMatter. */ From d79f992fb46372f16aeb71e5fa808e67730b0726 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 13:12:18 +0100 Subject: [PATCH 171/319] Added the DarkMatter class instantiation. --- dark-matter.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dark-matter.php b/dark-matter.php index 57cae278..ce9a5a63 100644 --- a/dark-matter.php +++ b/dark-matter.php @@ -58,3 +58,8 @@ * Domain Mapping module. */ require DM_PATH . '/domain-mapping/domain-mapping.php'; + +/** + * Let the magic - and bugs ... probably bugs! - begin. + */ +\DarkMatter\DarkMatter::instance(); From de8c9444764c197dcbecf33a08c3a2770c63e988 Mon Sep 17 00:00:00 2001 From: cameronterry Date: Sat, 29 Oct 2022 13:13:29 +0100 Subject: [PATCH 172/319] Fix merge conflicts on cherry-pick 0135d1a473b5f644c9df846931c3ccec0f4a6d31 --- includes/classes/DarkMatter.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index d59324b6..2ee1f38f 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -5,6 +5,10 @@ * @package DarkMatter */ +namespace DarkMatter; + +use DarkMatter\Interfaces\Registerable; + /** * Class DarkMatter. */ @@ -16,6 +20,30 @@ public function __construct() { } + /** + * Register a set of classes used by the Dark Matter plugin. + * + * @param array $classes String of class names / namespaces to be instantiated. + * @return array Array of the resultant objects. + */ + private function class_register( $classes = [] ) { + if ( empty( $classes ) ) { + return []; + } + + + $objs = []; + foreach ( $classes as $class ) { + $obj = new $class(); + + if ( $obj instanceof Registerable ) { + $obj->register(); + } + } + + return $objs; + } + /** * Return the Singleton Instance of the class. * From 5c36a72b224a9d0110c3816fac6760e0c9ba087e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 13:15:08 +0100 Subject: [PATCH 173/319] Moved Registerable into the interfaces folder. --- includes/classes/{ => Interfaces}/Registerable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename includes/classes/{ => Interfaces}/Registerable.php (90%) diff --git a/includes/classes/Registerable.php b/includes/classes/Interfaces/Registerable.php similarity index 90% rename from includes/classes/Registerable.php rename to includes/classes/Interfaces/Registerable.php index d0c23f15..21bb137e 100644 --- a/includes/classes/Registerable.php +++ b/includes/classes/Interfaces/Registerable.php @@ -5,7 +5,7 @@ * @package DarkMatter */ -namespace DarkMatter; +namespace DarkMatter\Interfaces; /** * Interface Registerable From 5e2862a850898ccb482407a2611d51bd75dde337 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 13:29:13 +0100 Subject: [PATCH 174/319] Moved the two domain mapping classes into the new folder. --- .../DomainMapping/REST}/class-dm-rest-domains-controller.php | 0 .../DomainMapping/REST}/class-dm-rest-restricted-controller.php | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {domain-mapping/rest => includes/classes/DomainMapping/REST}/class-dm-rest-domains-controller.php (100%) rename {domain-mapping/rest => includes/classes/DomainMapping/REST}/class-dm-rest-restricted-controller.php (100%) diff --git a/domain-mapping/rest/class-dm-rest-domains-controller.php b/includes/classes/DomainMapping/REST/class-dm-rest-domains-controller.php similarity index 100% rename from domain-mapping/rest/class-dm-rest-domains-controller.php rename to includes/classes/DomainMapping/REST/class-dm-rest-domains-controller.php diff --git a/domain-mapping/rest/class-dm-rest-restricted-controller.php b/includes/classes/DomainMapping/REST/class-dm-rest-restricted-controller.php similarity index 100% rename from domain-mapping/rest/class-dm-rest-restricted-controller.php rename to includes/classes/DomainMapping/REST/class-dm-rest-restricted-controller.php From a84695b113a6c5d4ffa6210bf547044a5f5c9954 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 13:35:54 +0100 Subject: [PATCH 175/319] Converted the Domains REST controller into it's new namespace. --- .../REST/class-dm-rest-domains-controller.php | 89 +++++++++---------- 1 file changed, 40 insertions(+), 49 deletions(-) diff --git a/includes/classes/DomainMapping/REST/class-dm-rest-domains-controller.php b/includes/classes/DomainMapping/REST/class-dm-rest-domains-controller.php index cdfcef5c..c3100ca6 100644 --- a/includes/classes/DomainMapping/REST/class-dm-rest-domains-controller.php +++ b/includes/classes/DomainMapping/REST/class-dm-rest-domains-controller.php @@ -1,17 +1,21 @@ prepare_item_for_database( $request ); @@ -62,7 +66,7 @@ public function create_item( $request ) { * * @since 2.0.0 * - * @param WP_REST_Request $request Current request. + * @param \WP_REST_Request $request Current request. * @return boolean True if the current user is a Super Admin. False otherwise. */ public function create_item_permissions_check( $request ) { @@ -75,11 +79,11 @@ public function create_item_permissions_check( $request ) { * * @since 2.0.0 * - * @param WP_REST_Request $request Current request. - * @return WP_REST_Response|mixed WP_REST_Response on success. WP_Error on failure. + * @param \WP_REST_Request $request Current request. + * @return \WP_REST_Response|mixed WP_REST_Response on success. WP_Error on failure. */ public function delete_item( $request ) { - $db = DarkMatter_Domains::instance(); + $db = \DarkMatter_Domains::instance(); $result = $db->delete( $request['domain'], $request['force'] ); @@ -109,7 +113,7 @@ public function delete_item( $request ) { * * @since 2.0.0 * - * @param WP_REST_Request $request Current request. + * @param \WP_REST_Request $request Current request. * @return boolean True if the current user is a Super Admin. False otherwise. */ public function delete_item_permissions_check( $request ) { @@ -122,11 +126,11 @@ public function delete_item_permissions_check( $request ) { * * @since 2.0.0 * - * @param WP_REST_Request $request Current request. - * @return WP_REST_Response|mixed WP_REST_Response on success. WP_Error on failure. + * @param \WP_REST_Request $request Current request. + * @return \WP_REST_Response|mixed WP_REST_Response on success. WP_Error on failure. */ public function get_item( $request ) { - $db = DarkMatter_Domains::instance(); + $db = \DarkMatter_Domains::instance(); $result = $db->get( $request['domain'] ); @@ -298,8 +302,8 @@ public function get_item_schema() { * * @since 2.0.0 * - * @param WP_REST_Request $request Current request. - * @return WP_REST_Response|mixed WP_REST_Response on success. WP_Error on failure. + * @param \WP_REST_Request $request Current request. + * @return \WP_REST_Response|mixed WP_REST_Response on success. WP_Error on failure. */ public function get_items( $request ) { $site_id = null; @@ -316,7 +320,7 @@ public function get_items( $request ) { $site_id = get_current_blog_id(); } - $db = DarkMatter_Domains::instance(); + $db = \DarkMatter_Domains::instance(); $response = array(); @@ -345,7 +349,7 @@ public function get_items( $request ) { * * @since 2.0.0 * - * @param WP_REST_Request $request Current request. + * @param \WP_REST_Request $request Current request. * @return boolean True if the current user is a Super Admin. False otherwise. */ public function get_items_permissions_check( $request ) { @@ -358,7 +362,7 @@ public function get_items_permissions_check( $request ) { * * @since 2.0.0 * - * @param WP_REST_Request $request Current request. + * @param \WP_REST_Request $request Current request. * @return array Data provided by the call to the endpoint. */ protected function prepare_item_for_database( $request ) { @@ -379,15 +383,15 @@ protected function prepare_item_for_database( $request ) { $value = $request[ $key ]; } - if ( WP_REST_Server::CREATABLE === $method && null === $value && 'is_primary' === $key ) { + if ( \WP_REST_Server::CREATABLE === $method && null === $value && 'is_primary' === $key ) { $value = false; } - if ( WP_REST_Server::CREATABLE === $method && null === $value && 'is_https' === $key ) { + if ( \WP_REST_Server::CREATABLE === $method && null === $value && 'is_https' === $key ) { $value = false; } - if ( WP_REST_Server::CREATABLE === $method && null === $value && 'is_active' === $key ) { + if ( \WP_REST_Server::CREATABLE === $method && null === $value && 'is_active' === $key ) { $value = true; } @@ -402,8 +406,8 @@ protected function prepare_item_for_database( $request ) { * * @since 2.0.0 * - * @param DM_Domain $item Domain object to be prepared for response. - * @param WP_REST_Request $request Current request. + * @param \DM_Domain $item Domain object to be prepared for response. + * @param \WP_REST_Request $request Current request. * @return array Prepared item for REST response. */ public function prepare_item_for_response( $item, $request ) { @@ -480,10 +484,10 @@ public function register_routes() { $this->namespace, $this->rest_base, array( - 'methods' => WP_REST_Server::CREATABLE, + 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => array( $this, 'create_item_permissions_check' ), - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), + 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), ) ); @@ -499,13 +503,13 @@ public function register_routes() { ), ), array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'schema' => array( $this, 'get_item_schema' ), ), array( - 'methods' => WP_REST_Server::DELETABLE, + 'methods' => \WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 'args' => array( @@ -517,10 +521,10 @@ public function register_routes() { ), ), array( - 'methods' => WP_REST_Server::EDITABLE, + 'methods' => \WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'update_item_permissions_check' ), - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), + 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), ), ) ); @@ -529,7 +533,7 @@ public function register_routes() { $this->namespace, $this->rest_base_plural, array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'schema' => array( $this, 'get_item_schema' ), @@ -548,7 +552,7 @@ public function register_routes() { ), ), array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'schema' => array( $this, 'get_item_schema' ), @@ -562,11 +566,11 @@ public function register_routes() { * * @since 2.0.0 * - * @param WP_REST_Request $request Current request. - * @return WP_REST_Response|mixed WP_REST_Response on success. WP_Error on failure. + * @param \WP_REST_Request $request Current request. + * @return \WP_REST_Response|mixed WP_REST_Response on success. WP_Error on failure. */ public function update_item( $request ) { - $db = DarkMatter_Domains::instance(); + $db = \DarkMatter_Domains::instance(); $item = $this->prepare_item_for_database( $request ); @@ -601,7 +605,7 @@ public function update_item( $request ) { * * @since 2.0.0 * - * @param WP_REST_Request $request Current request. + * @param \WP_REST_Request $request Current request. * @return boolean True if the current user is a Super Admin. False otherwise. */ public function update_item_permissions_check( $request ) { @@ -609,16 +613,3 @@ public function update_item_permissions_check( $request ) { return current_user_can( apply_filters( 'dark_matter_domain_permission', 'upgrade_network', 'rest-update' ) ); } } - -/** - * Setup the REST Controller for Domains for use. - * - * @since 2.0.0 - * - * @return void - */ -function dark_matter_domains_rest() { - $controller = new DM_REST_Domains_Controller(); - $controller->register_routes(); -} -add_action( 'rest_api_init', 'dark_matter_domains_rest' ); From 0c51c4db907ec69255d13d4ce285b040bb98f177 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 13:38:40 +0100 Subject: [PATCH 176/319] Renamed Domains REST API file into a more PSR-4 friendly name. --- .../REST/{class-dm-rest-domains-controller.php => Domains.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename includes/classes/DomainMapping/REST/{class-dm-rest-domains-controller.php => Domains.php} (100%) diff --git a/includes/classes/DomainMapping/REST/class-dm-rest-domains-controller.php b/includes/classes/DomainMapping/REST/Domains.php similarity index 100% rename from includes/classes/DomainMapping/REST/class-dm-rest-domains-controller.php rename to includes/classes/DomainMapping/REST/Domains.php From 2ce298c83d5cbad175ad7afe638ce8d3168c6b28 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 13:38:56 +0100 Subject: [PATCH 177/319] Removed the file includes from the older domain mapping. --- domain-mapping/domain-mapping.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/domain-mapping/domain-mapping.php b/domain-mapping/domain-mapping.php index edd7288d..4f53c403 100644 --- a/domain-mapping/domain-mapping.php +++ b/domain-mapping/domain-mapping.php @@ -43,9 +43,6 @@ require_once DM_PATH . '/domain-mapping/sso/class-dm-sso-cookie.php'; } -require_once DM_PATH . '/domain-mapping/rest/class-dm-rest-domains-controller.php'; -require_once DM_PATH . '/domain-mapping/rest/class-dm-rest-restricted-controller.php'; - if ( defined( 'WP_CLI' ) && WP_CLI ) { require_once DM_PATH . '/domain-mapping/cli/class-darkmatter-domain-cli.php'; require_once DM_PATH . '/domain-mapping/cli/class-darkmatter-dropin-cli.php'; From b545086a15759bab1012e6387d1fc89ef51265d8 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 13:40:57 +0100 Subject: [PATCH 178/319] Added REST API support to the DarkMatter class register. --- includes/classes/DarkMatter.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index 2ee1f38f..f6366767 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -17,7 +17,7 @@ class DarkMatter { * Constructor */ public function __construct() { - + add_action( 'rest_api_init', [ $this, 'register_rest' ] ); } /** @@ -39,11 +39,23 @@ private function class_register( $classes = [] ) { if ( $obj instanceof Registerable ) { $obj->register(); } + + if ( $obj instanceof \WP_REST_Controller ) { + $obj->register_routes(); + } } return $objs; } + /** + * Register REST routes. This is separate due to the need to be fired on the `rest_api_init` hook. + * + * @return void + */ + public function register_rest() { + } + /** * Return the Singleton Instance of the class. * From 4ad8ccc15c22a10535e67e6592eb584530fc4af2 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 13:41:13 +0100 Subject: [PATCH 179/319] Instantiated the Domains class in the REST class register. --- includes/classes/DarkMatter.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index f6366767..db9e6e24 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -7,6 +7,7 @@ namespace DarkMatter; +use DarkMatter\DomainMapping; use DarkMatter\Interfaces\Registerable; /** @@ -54,6 +55,11 @@ private function class_register( $classes = [] ) { * @return void */ public function register_rest() { + $this->class_register( + [ + DomainMapping\REST\Domains::class, + ] + ); } /** From ee523973976b1b46181e3a5fd501e74b78736c68 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 13:40:57 +0100 Subject: [PATCH 180/319] Added REST API support to the DarkMatter class register. --- includes/classes/DarkMatter.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index 2ee1f38f..f6366767 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -17,7 +17,7 @@ class DarkMatter { * Constructor */ public function __construct() { - + add_action( 'rest_api_init', [ $this, 'register_rest' ] ); } /** @@ -39,11 +39,23 @@ private function class_register( $classes = [] ) { if ( $obj instanceof Registerable ) { $obj->register(); } + + if ( $obj instanceof \WP_REST_Controller ) { + $obj->register_routes(); + } } return $objs; } + /** + * Register REST routes. This is separate due to the need to be fired on the `rest_api_init` hook. + * + * @return void + */ + public function register_rest() { + } + /** * Return the Singleton Instance of the class. * From bdf5ebfe262778982fbe2002d619df1c3c461bf0 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 13:53:29 +0100 Subject: [PATCH 181/319] Prepared Restricted API to the new namespacing, --- .../class-dm-rest-restricted-controller.php | 53 ++++++++----------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/includes/classes/DomainMapping/REST/class-dm-rest-restricted-controller.php b/includes/classes/DomainMapping/REST/class-dm-rest-restricted-controller.php index 6d53e812..fe6c3f02 100644 --- a/includes/classes/DomainMapping/REST/class-dm-rest-restricted-controller.php +++ b/includes/classes/DomainMapping/REST/class-dm-rest-restricted-controller.php @@ -1,17 +1,21 @@ get() ); } @@ -140,7 +144,7 @@ public function get_items( $request ) { * * @since 2.0.0 * - * @param WP_REST_Request $request Current request. + * @param \WP_REST_Request $request Current request. * @return boolean True if the current user is a Super Admin. False otherwise. */ public function get_items_permissions_check( $request ) { @@ -160,7 +164,7 @@ public function register_routes() { $this->namespace, $this->rest_base, [ - 'methods' => WP_REST_Server::CREATABLE, + 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => array( $this, 'create_item_permissions_check' ), ] @@ -170,7 +174,7 @@ public function register_routes() { $this->namespace, $this->rest_base, [ - 'methods' => WP_REST_Server::DELETABLE, + 'methods' => \WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => array( $this, 'delete_item_permissions_check' ), ] @@ -180,23 +184,10 @@ public function register_routes() { $this->namespace, $this->rest_base, [ - 'methods' => WP_REST_Server::READABLE, + 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), ] ); } } - -/** - * Setup the REST Controller for Domains for use. - * - * @since 2.0.0 - * - * @return void - */ -function dark_matter_restricted_rest() { - $controller = new DM_REST_Restricted_Controller(); - $controller->register_routes(); -} -add_action( 'rest_api_init', 'dark_matter_restricted_rest' ); From 0bb7dcf584acf53230b69d90e6cdbd60a07413fa Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 13:54:53 +0100 Subject: [PATCH 182/319] Instantiated the new namespaced Restricted. --- includes/classes/DarkMatter.php | 1 + .../{class-dm-rest-restricted-controller.php => Restricted.php} | 0 2 files changed, 1 insertion(+) rename includes/classes/DomainMapping/REST/{class-dm-rest-restricted-controller.php => Restricted.php} (100%) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index db9e6e24..691789cc 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -58,6 +58,7 @@ public function register_rest() { $this->class_register( [ DomainMapping\REST\Domains::class, + DomainMapping\REST\Restricted::class, ] ); } diff --git a/includes/classes/DomainMapping/REST/class-dm-rest-restricted-controller.php b/includes/classes/DomainMapping/REST/Restricted.php similarity index 100% rename from includes/classes/DomainMapping/REST/class-dm-rest-restricted-controller.php rename to includes/classes/DomainMapping/REST/Restricted.php From 0854fde7a7e12b8dc0f2c0b1eb9e7111f251fc10 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:05:08 +0100 Subject: [PATCH 183/319] Moved the CLI files into their new home. --- .../classes/DomainMapping/CLI}/class-darkmatter-domain-cli.php | 0 .../classes/DomainMapping/CLI}/class-darkmatter-dropin-cli.php | 0 .../classes/DomainMapping/CLI}/class-darkmatter-restrict-cli.php | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename {domain-mapping/cli => includes/classes/DomainMapping/CLI}/class-darkmatter-domain-cli.php (100%) rename {domain-mapping/cli => includes/classes/DomainMapping/CLI}/class-darkmatter-dropin-cli.php (100%) rename {domain-mapping/cli => includes/classes/DomainMapping/CLI}/class-darkmatter-restrict-cli.php (100%) diff --git a/domain-mapping/cli/class-darkmatter-domain-cli.php b/includes/classes/DomainMapping/CLI/class-darkmatter-domain-cli.php similarity index 100% rename from domain-mapping/cli/class-darkmatter-domain-cli.php rename to includes/classes/DomainMapping/CLI/class-darkmatter-domain-cli.php diff --git a/domain-mapping/cli/class-darkmatter-dropin-cli.php b/includes/classes/DomainMapping/CLI/class-darkmatter-dropin-cli.php similarity index 100% rename from domain-mapping/cli/class-darkmatter-dropin-cli.php rename to includes/classes/DomainMapping/CLI/class-darkmatter-dropin-cli.php diff --git a/domain-mapping/cli/class-darkmatter-restrict-cli.php b/includes/classes/DomainMapping/CLI/class-darkmatter-restrict-cli.php similarity index 100% rename from domain-mapping/cli/class-darkmatter-restrict-cli.php rename to includes/classes/DomainMapping/CLI/class-darkmatter-restrict-cli.php From 6d56ef472652da6adb06247717a523f1a9a4430c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:09:51 +0100 Subject: [PATCH 184/319] Converted the domain CLI into the new namespace. --- .../CLI/class-darkmatter-domain-cli.php | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/includes/classes/DomainMapping/CLI/class-darkmatter-domain-cli.php b/includes/classes/DomainMapping/CLI/class-darkmatter-domain-cli.php index 0fffc3bc..797ff96c 100644 --- a/includes/classes/DomainMapping/CLI/class-darkmatter-domain-cli.php +++ b/includes/classes/DomainMapping/CLI/class-darkmatter-domain-cli.php @@ -1,21 +1,26 @@ add( $fqdn, $opts['primary'], $opts['https'], $opts['force'], ! $opts['disable'], $type ); if ( is_wp_error( $result ) ) { @@ -121,6 +126,15 @@ private function check_type_opt( $type = '' ) { return DM_DOMAIN_TYPE_MAIN; } + /** + * Include this CLI amongst the others. + * + * @return void + */ + public static function define() { + WP_CLI::add_command( 'darkmatter domain', self::class ); + } + /** * List a domain for the current Site. If the the URL is omitted and the * command is run on the root Site, it will list all domains available for @@ -171,7 +185,7 @@ public function list( $args, $assoc_args ) { } if ( $opts['primary'] ) { - $db = DarkMatter_Primary::instance(); + $db = \DarkMatter_Primary::instance(); $domains = $db->get_all(); } else { /** @@ -184,7 +198,7 @@ public function list( $args, $assoc_args ) { $site_id = null; } - $db = DarkMatter_Domains::instance(); + $db = \DarkMatter_Domains::instance(); $domains = $db->get_domains( $site_id ); } @@ -288,7 +302,7 @@ public function remove( $args, $assoc_args ) { ] ); - $db = DarkMatter_Domains::instance(); + $db = \DarkMatter_Domains::instance(); /** * Remove the domain. @@ -371,7 +385,7 @@ public function set( $args, $assoc_args ) { $fqdn = $args[0]; - $db = DarkMatter_Domains::instance(); + $db = \DarkMatter_Domains::instance(); $domain_before = $db->get( $fqdn ); $opts = wp_parse_args( @@ -462,4 +476,3 @@ public function set( $args, $assoc_args ) { WP_CLI::success( $fqdn . __( ': successfully updated.', 'dark-matter' ) ); } } -WP_CLI::add_command( 'darkmatter domain', 'DarkMatter_Domain_CLI' ); From d0050273d43919e972d6603b25fcedb4d986b11b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:10:53 +0100 Subject: [PATCH 185/319] Added CLI support in the class register. --- includes/classes/DarkMatter.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index 691789cc..ed898476 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -41,6 +41,10 @@ private function class_register( $classes = [] ) { $obj->register(); } + if ( $obj instanceof \WP_CLI_Command ) { + $obj::define(); + } + if ( $obj instanceof \WP_REST_Controller ) { $obj->register_routes(); } From c0e2b42145e0b10d6766560520a21c8b6e95a7ff Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:15:48 +0100 Subject: [PATCH 186/319] Added the logic to load CLI\Domains, and fixed the namespace. --- includes/classes/DarkMatter.php | 17 +++++++++++++++++ ...ss-darkmatter-domain-cli.php => Domains.php} | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) rename includes/classes/DomainMapping/CLI/{class-darkmatter-domain-cli.php => Domains.php} (99%) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index ed898476..7c017b65 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -19,6 +19,8 @@ class DarkMatter { */ public function __construct() { add_action( 'rest_api_init', [ $this, 'register_rest' ] ); + + $this->register_domainmapping(); } /** @@ -53,6 +55,21 @@ private function class_register( $classes = [] ) { return $objs; } + /** + * Register the classes for Domain Mapping. + * + * @return void + */ + public function register_domainmapping() { + if ( defined( 'WP_CLI' ) && WP_CLI ) { + $this->class_register( + [ + DomainMapping\CLI\Domains::class, + ] + ); + } + } + /** * Register REST routes. This is separate due to the need to be fired on the `rest_api_init` hook. * diff --git a/includes/classes/DomainMapping/CLI/class-darkmatter-domain-cli.php b/includes/classes/DomainMapping/CLI/Domains.php similarity index 99% rename from includes/classes/DomainMapping/CLI/class-darkmatter-domain-cli.php rename to includes/classes/DomainMapping/CLI/Domains.php index 797ff96c..9503648d 100644 --- a/includes/classes/DomainMapping/CLI/class-darkmatter-domain-cli.php +++ b/includes/classes/DomainMapping/CLI/Domains.php @@ -6,7 +6,7 @@ * @since 2.0.0 */ -namespace DomainMapping\CLI; +namespace DarkMatter\DomainMapping\CLI; use WP_CLI; use WP_CLI_Command; From 45f9d8c7b0c3d5214f1af0cf4962269eb99e04ac Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:15:59 +0100 Subject: [PATCH 187/319] Removed the old file includes for domain mapping CLIs. --- domain-mapping/domain-mapping.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/domain-mapping/domain-mapping.php b/domain-mapping/domain-mapping.php index 4f53c403..867ecc45 100644 --- a/domain-mapping/domain-mapping.php +++ b/domain-mapping/domain-mapping.php @@ -42,9 +42,3 @@ if ( DM_HealthChecks::instance()->cookie_domain_dm_set() && ( ! defined( 'DARKMATTER_SSO_TYPE' ) || 'disable' !== DARKMATTER_SSO_TYPE ) ) { require_once DM_PATH . '/domain-mapping/sso/class-dm-sso-cookie.php'; } - -if ( defined( 'WP_CLI' ) && WP_CLI ) { - require_once DM_PATH . '/domain-mapping/cli/class-darkmatter-domain-cli.php'; - require_once DM_PATH . '/domain-mapping/cli/class-darkmatter-dropin-cli.php'; - require_once DM_PATH . '/domain-mapping/cli/class-darkmatter-restrict-cli.php'; -} From e4a0108e88e04cd865165ef4c12ac7f867bdcbd5 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:19:05 +0100 Subject: [PATCH 188/319] Moved the Dropin CLI to the new namespace. --- includes/classes/DarkMatter.php | 1 + ...s-darkmatter-dropin-cli.php => Dropin.php} | 25 +++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) rename includes/classes/DomainMapping/CLI/{class-darkmatter-dropin-cli.php => Dropin.php} (87%) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index 7c017b65..d46739e7 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -65,6 +65,7 @@ public function register_domainmapping() { $this->class_register( [ DomainMapping\CLI\Domains::class, + DomainMapping\CLI\Dropin::class, ] ); } diff --git a/includes/classes/DomainMapping/CLI/class-darkmatter-dropin-cli.php b/includes/classes/DomainMapping/CLI/Dropin.php similarity index 87% rename from includes/classes/DomainMapping/CLI/class-darkmatter-dropin-cli.php rename to includes/classes/DomainMapping/CLI/Dropin.php index 546bedd9..936659e7 100644 --- a/includes/classes/DomainMapping/CLI/class-darkmatter-dropin-cli.php +++ b/includes/classes/DomainMapping/CLI/Dropin.php @@ -6,18 +6,19 @@ * @since 2.0.0 */ -defined( 'ABSPATH' ) || die; +namespace DarkMatter\DomainMapping\CLI; -if ( ! defined( 'WP_CLI' ) ) { - return; -} +use WP_CLI; +use WP_CLI_Command; /** - * Class DarkMatter_Dropin_CLI + * Class Dropin + * + * Previously called `DarkMatter_Dropin_CLI`. * * @since 2.0.0 */ -class DarkMatter_Dropin_CLI { +class Dropin extends WP_CLI_Command { /** * Helper command to see if the Sunrise dropin plugin within Dark Matter is * the same version as in use on the current WordPress installation. @@ -30,7 +31,7 @@ class DarkMatter_Dropin_CLI { * @since 2.0.0 */ public function check() { - $health_check = DM_HealthChecks::instance(); + $health_check = \DM_HealthChecks::instance(); if ( $health_check->is_dropin_latest() ) { WP_CLI::success( __( 'Current Sunrise dropin matches the Sunrise within Dark Matter plugin.', 'dark-matter' ) ); @@ -40,6 +41,15 @@ public function check() { WP_CLI::error( __( 'Sunrise dropin does not match the Sunrise within Dark Matter plugin. Consider using the "update" command to correct this issue.', 'dark-matter' ) ); } + /** + * Include this CLI amongst the others. + * + * @return void + */ + public static function define() { + WP_CLI::add_command( 'darkmatter dropin', self::class ); + } + /** * Upgrade the Sunrise dropin plugin to the latest version within the Dark * Matter plugin. @@ -100,4 +110,3 @@ public function update( $args, $assoc_args ) { } } } -WP_CLI::add_command( 'darkmatter dropin', 'DarkMatter_Dropin_CLI' ); From 97fe3028e37aeeba026f47d42bd2f5f8a1c86f12 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:21:31 +0100 Subject: [PATCH 189/319] Moved Restricted CLI to the new namespace. --- includes/classes/DarkMatter.php | 1 + ...matter-restrict-cli.php => Restricted.php} | 29 +++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) rename includes/classes/DomainMapping/CLI/{class-darkmatter-restrict-cli.php => Restricted.php} (85%) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index d46739e7..eba31051 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -66,6 +66,7 @@ public function register_domainmapping() { [ DomainMapping\CLI\Domains::class, DomainMapping\CLI\Dropin::class, + DomainMapping\CLI\Restricted::class, ] ); } diff --git a/includes/classes/DomainMapping/CLI/class-darkmatter-restrict-cli.php b/includes/classes/DomainMapping/CLI/Restricted.php similarity index 85% rename from includes/classes/DomainMapping/CLI/class-darkmatter-restrict-cli.php rename to includes/classes/DomainMapping/CLI/Restricted.php index ed2171fc..4b2d3956 100644 --- a/includes/classes/DomainMapping/CLI/class-darkmatter-restrict-cli.php +++ b/includes/classes/DomainMapping/CLI/Restricted.php @@ -1,21 +1,24 @@ add( $fqdn ); if ( is_wp_error( $result ) ) { @@ -51,6 +54,15 @@ public function add( $args, $assoc_args ) { WP_CLI::success( $fqdn . __( ': is now restricted.', 'dark-matter' ) ); } + /** + * Include this CLI amongst the others. + * + * @return void + */ + public static function define() { + WP_CLI::add_command( 'darkmatter restrict', self::class ); + } + /** * Retrieve a list of all Restricted domains for the Network. * @@ -94,7 +106,7 @@ public function list( $args, $assoc_args ) { $opts['format'] = 'table'; } - $db = DarkMatter_Restrict::instance(); + $db = \DarkMatter_Restrict::instance(); $restricted = $db->get(); @@ -146,7 +158,7 @@ public function remove( $args, $assoc_args ) { $fqdn = $args[0]; - $restricted = DarkMatter_Restrict::instance(); + $restricted = \DarkMatter_Restrict::instance(); $result = $restricted->delete( $fqdn ); if ( is_wp_error( $result ) ) { @@ -156,4 +168,3 @@ public function remove( $args, $assoc_args ) { WP_CLI::success( $fqdn . __( ': is no longer restricted.', 'dark-matter' ) ); } } -WP_CLI::add_command( 'darkmatter restrict', 'DarkMatter_Restrict_CLI' ); From eeedd7a714858c1b2590dea37d168145f50b1928 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:30:37 +0100 Subject: [PATCH 190/319] Moved the database class into the new folder. --- .../classes/DomainMapping}/class-dm-database.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {domain-mapping/classes => includes/classes/DomainMapping}/class-dm-database.php (100%) diff --git a/domain-mapping/classes/class-dm-database.php b/includes/classes/DomainMapping/class-dm-database.php similarity index 100% rename from domain-mapping/classes/class-dm-database.php rename to includes/classes/DomainMapping/class-dm-database.php From 6ed43f82ab951e0eb20bfd1d64cbd44fa76a0474 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:32:20 +0100 Subject: [PATCH 191/319] Renamed and reworked DM_Database into the new namespace. --- domain-mapping/domain-mapping.php | 1 - .../{class-dm-database.php => Installer.php} | 26 ++++--------------- 2 files changed, 5 insertions(+), 22 deletions(-) rename includes/classes/DomainMapping/{class-dm-database.php => Installer.php} (84%) diff --git a/domain-mapping/domain-mapping.php b/domain-mapping/domain-mapping.php index 867ecc45..ba826130 100644 --- a/domain-mapping/domain-mapping.php +++ b/domain-mapping/domain-mapping.php @@ -18,7 +18,6 @@ require_once DM_PATH . '/domain-mapping/inc/compat.php'; require_once DM_PATH . '/domain-mapping/classes/class-dm-media.php'; -require_once DM_PATH . '/domain-mapping/classes/class-dm-database.php'; require_once DM_PATH . '/domain-mapping/classes/class-dm-domain.php'; require_once DM_PATH . '/domain-mapping/classes/class-dm-healthchecks.php'; require_once DM_PATH . '/domain-mapping/classes/class-dm-url.php'; diff --git a/includes/classes/DomainMapping/class-dm-database.php b/includes/classes/DomainMapping/Installer.php similarity index 84% rename from includes/classes/DomainMapping/class-dm-database.php rename to includes/classes/DomainMapping/Installer.php index 752bbd60..07a7e2cc 100644 --- a/includes/classes/DomainMapping/class-dm-database.php +++ b/includes/classes/DomainMapping/Installer.php @@ -1,6 +1,6 @@ Date: Sat, 29 Oct 2022 14:37:09 +0100 Subject: [PATCH 192/319] Hooked up the Installer class. --- includes/classes/DarkMatter.php | 5 +++++ includes/classes/DomainMapping/Installer.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index eba31051..484b9315 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -20,7 +20,12 @@ class DarkMatter { public function __construct() { add_action( 'rest_api_init', [ $this, 'register_rest' ] ); + /** + * Register and handle Domain Mapping module. + */ $this->register_domainmapping(); + + new DomainMapping\Installer(); } /** diff --git a/includes/classes/DomainMapping/Installer.php b/includes/classes/DomainMapping/Installer.php index 07a7e2cc..463429ae 100644 --- a/includes/classes/DomainMapping/Installer.php +++ b/includes/classes/DomainMapping/Installer.php @@ -6,7 +6,7 @@ * @since 2.0.0 */ -defined( 'ABSPATH' ) || die; +namespace DarkMatter\DomainMapping; /** * Class Installer From 9b7bf4bb4f7be92531cadfe64c232491e7212c5c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:39:26 +0100 Subject: [PATCH 193/319] Moved DM_URL into its new namespace home. --- .../classes/DomainMapping/Processors}/class-dm-url.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {domain-mapping/classes => includes/classes/DomainMapping/Processors}/class-dm-url.php (100%) diff --git a/domain-mapping/classes/class-dm-url.php b/includes/classes/DomainMapping/Processors/class-dm-url.php similarity index 100% rename from domain-mapping/classes/class-dm-url.php rename to includes/classes/DomainMapping/Processors/class-dm-url.php From 97c39cc70e2804cbf441ff961d6ecec69d302317 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:43:17 +0100 Subject: [PATCH 194/319] Converted DM_URL into Mapping and adjusted to namespace. --- includes/classes/DarkMatter.php | 6 +++ .../DomainMapping/Processors/class-dm-url.php | 48 +++++++------------ 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index 484b9315..56daaa9a 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -66,6 +66,12 @@ private function class_register( $classes = [] ) { * @return void */ public function register_domainmapping() { + $this->class_register( + [ + DomainMapping\Processors\Mapping::class, + ] + ); + if ( defined( 'WP_CLI' ) && WP_CLI ) { $this->class_register( [ diff --git a/includes/classes/DomainMapping/Processors/class-dm-url.php b/includes/classes/DomainMapping/Processors/class-dm-url.php index 0a3cc032..8a59e618 100644 --- a/includes/classes/DomainMapping/Processors/class-dm-url.php +++ b/includes/classes/DomainMapping/Processors/class-dm-url.php @@ -1,19 +1,23 @@ is_request_mapped = ( defined( 'DOMAIN_MAPPING' ) && DOMAIN_MAPPING ); /** @@ -147,7 +151,7 @@ public function is_external( $external = false, $host = '' ) { * Attempt to find the domain in Dark Matter. If the domain is found, then tell WordPress it is an internal * domain. */ - $db = DarkMatter_Domains::instance(); + $db = \DarkMatter_Domains::instance(); $domain = $db->find( $host ); if ( is_a( $domain, 'DM_Domain' ) ) { @@ -193,7 +197,7 @@ private function is_mapped() { */ global $switched; if ( $switched && $this->is_request_mapped ) { - $primary = DarkMatter_Primary::instance()->get(); + $primary = \DarkMatter_Primary::instance()->get(); /** * If there is no primary or if it is inactive, then the site is not mapped. @@ -230,7 +234,7 @@ public function map( $value = '', $blog_id = 0 ) { * Retrieve the current blog. */ $blog = get_site( absint( $blog_id ) ); - $primary = DarkMatter_Primary::instance()->get( $blog->blog_id ); + $primary = \DarkMatter_Primary::instance()->get( $blog->blog_id ); $unmapped = untrailingslashit( $blog->domain . $blog->path ); @@ -397,8 +401,8 @@ public function prepare_rest() { * Ensures the "raw" version of the content, typically used by Gutenberg through it's middleware pre-load / JS * hydrate process, gets handled the same as content (which runs through the `the_content` hook). * - * @param WP_REST_Response $item Individual post / item in the response that is being processed. - * @return WP_REST_Response Post / item with the content.raw, if present, mapped. + * @param \WP_REST_Response $item Individual post / item in the response that is being processed. + * @return \WP_REST_Response Post / item with the content.raw, if present, mapped. */ public function prepare_rest_post_item( $item = null ) { if ( isset( $item->data['content']['raw'] ) ) { @@ -504,7 +508,7 @@ public function unmap( $value = '' ) { * Retrieve the current blog. */ $blog = get_site(); - $primary = DarkMatter_Primary::instance()->get(); + $primary = \DarkMatter_Primary::instance()->get(); /** * If there is no primary domain or the primary domain cannot be found @@ -538,22 +542,4 @@ public function upload( $uploads ) { return $uploads; } - - /** - * Return the Singleton Instance of the class. - * - * @since 2.0.0 - * - * @return DM_URL - */ - public static function instance() { - static $instance = false; - - if ( ! $instance ) { - $instance = new self(); - } - - return $instance; - } } -DM_URL::instance(); From 4f9aacdd26d0e040483568f36126d31859ab395b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:43:52 +0100 Subject: [PATCH 195/319] Updated the filename to be the new name, Mapping. --- .../DomainMapping/Processors/{class-dm-url.php => Mapping.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename includes/classes/DomainMapping/Processors/{class-dm-url.php => Mapping.php} (100%) diff --git a/includes/classes/DomainMapping/Processors/class-dm-url.php b/includes/classes/DomainMapping/Processors/Mapping.php similarity index 100% rename from includes/classes/DomainMapping/Processors/class-dm-url.php rename to includes/classes/DomainMapping/Processors/Mapping.php From a3355a1cc4a98934c99039fecf320c3980f2db67 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 14:57:47 +0100 Subject: [PATCH 196/319] Created a new class for housing various helper functions / methods. --- includes/classes/DomainMapping/Helpers.php | 104 +++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 includes/classes/DomainMapping/Helpers.php diff --git a/includes/classes/DomainMapping/Helpers.php b/includes/classes/DomainMapping/Helpers.php new file mode 100644 index 00000000..3361d5bd --- /dev/null +++ b/includes/classes/DomainMapping/Helpers.php @@ -0,0 +1,104 @@ +get( $blog->blog_id ); + + $unmapped = untrailingslashit( $blog->domain . $blog->path ); + + /** + * If there is no primary domain or the unmapped version cannot be found + * then we return the value as-is. + */ + if ( empty( $primary ) || false === stripos( $value, $unmapped ) ) { + return $value; + } + + $domain = 'http' . ( $primary->is_https ? 's' : '' ) . '://' . $primary->domain; + + return preg_replace( "#https?://{$unmapped}#", $domain, $value ); + } + + /** + * Converts a URL from a mapped domain to the admin domain. This will only convert a URL which is the primary + * domain. + * + * @since 3.0.0 + * + * @param mixed $value Potentially a value containing the site's unmapped URL. + * @return mixed If unmapped URL is found, then returns the primary URL. Untouched otherwise. + */ + public function unmap( $value = '' ) { + /** + * Ensure that we are working with a string. + */ + if ( ! is_string( $value ) ) { + return $value; + } + + /** + * Retrieve the current blog. + */ + $blog = get_site(); + $primary = \DarkMatter_Primary::instance()->get(); + + /** + * If there is no primary domain or the primary domain cannot be found + * then we return the value as-is. + */ + if ( empty( $primary ) || false === stripos( $value, $primary->domain ) ) { + return $value; + } + + $unmapped = 'http' . ( $primary->is_https ? 's' : '' ) . '://' . untrailingslashit( $blog->domain . $blog->path ); + + return preg_replace( "#https?://{$primary->domain}#", $unmapped, $value ); + } + + /** + * Return the Singleton Instance of the class. + * + * @return Helpers + */ + public static function instance() { + static $instance = false; + + if ( ! $instance ) { + $instance = new self(); + } + + return $instance; + } +} From bb026cc3bf7be1463b80242f391d467687a8d1d0 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 15:01:07 +0100 Subject: [PATCH 197/319] Replaced the Processor\Mapping with the Helper methods. Also temporarily patched the Yoast support. --- .../classes/third-party/class-dm-yoast.php | 3 +- domain-mapping/domain-mapping.php | 1 - .../DomainMapping/Processors/Mapping.php | 53 ++----------------- 3 files changed, 4 insertions(+), 53 deletions(-) diff --git a/domain-mapping/classes/third-party/class-dm-yoast.php b/domain-mapping/classes/third-party/class-dm-yoast.php index a6acdf21..0aa2f0c6 100644 --- a/domain-mapping/classes/third-party/class-dm-yoast.php +++ b/domain-mapping/classes/third-party/class-dm-yoast.php @@ -37,8 +37,7 @@ public function fix_indexable_permalinks( $intend_to_save, $indexable ) { * If saving to the database, then make sure the permalink is unmapped. */ if ( $intend_to_save ) { - $dm_url = DM_URL::instance(); - $indexable->permalink = $dm_url->unmap( $indexable->permalink ); + $indexable->permalink = \DarkMatter\DomainMapping\Helpers::instance()->unmap( $indexable->permalink ); } return $intend_to_save; diff --git a/domain-mapping/domain-mapping.php b/domain-mapping/domain-mapping.php index ba826130..5984f301 100644 --- a/domain-mapping/domain-mapping.php +++ b/domain-mapping/domain-mapping.php @@ -20,7 +20,6 @@ require_once DM_PATH . '/domain-mapping/classes/class-dm-media.php'; require_once DM_PATH . '/domain-mapping/classes/class-dm-domain.php'; require_once DM_PATH . '/domain-mapping/classes/class-dm-healthchecks.php'; -require_once DM_PATH . '/domain-mapping/classes/class-dm-url.php'; /** * Plugin compatibility. diff --git a/includes/classes/DomainMapping/Processors/Mapping.php b/includes/classes/DomainMapping/Processors/Mapping.php index 8a59e618..f23a24c3 100644 --- a/includes/classes/DomainMapping/Processors/Mapping.php +++ b/includes/classes/DomainMapping/Processors/Mapping.php @@ -8,6 +8,7 @@ namespace DarkMatter\DomainMapping\Processors; +use DarkMatter\DomainMapping\Helpers; use DarkMatter\Interfaces\Registerable; /** @@ -223,32 +224,7 @@ private function is_mapped() { * @return string If unmapped URL is found, then returns the primary URL. Untouched otherwise. */ public function map( $value = '', $blog_id = 0 ) { - /** - * Ensure that we are working with a string. - */ - if ( ! is_string( $value ) ) { - return $value; - } - - /** - * Retrieve the current blog. - */ - $blog = get_site( absint( $blog_id ) ); - $primary = \DarkMatter_Primary::instance()->get( $blog->blog_id ); - - $unmapped = untrailingslashit( $blog->domain . $blog->path ); - - /** - * If there is no primary domain or the unmapped version cannot be found - * then we return the value as-is. - */ - if ( empty( $primary ) || false === stripos( $value, $unmapped ) ) { - return $value; - } - - $domain = 'http' . ( $primary->is_https ? 's' : '' ) . '://' . $primary->domain; - - return preg_replace( "#https?://{$unmapped}#", $domain, $value ); + return Helpers::instance()->map( $value, $blog_id ); } /** @@ -497,30 +473,7 @@ public function siteurl( $url = '', $path = '', $scheme = null, $blog_id = 0 ) { * @return mixed If unmapped URL is found, then returns the primary URL. Untouched otherwise. */ public function unmap( $value = '' ) { - /** - * Ensure that we are working with a string. - */ - if ( ! is_string( $value ) ) { - return $value; - } - - /** - * Retrieve the current blog. - */ - $blog = get_site(); - $primary = \DarkMatter_Primary::instance()->get(); - - /** - * If there is no primary domain or the primary domain cannot be found - * then we return the value as-is. - */ - if ( empty( $primary ) || false === stripos( $value, $primary->domain ) ) { - return $value; - } - - $unmapped = 'http' . ( $primary->is_https ? 's' : '' ) . '://' . untrailingslashit( $blog->domain . $blog->path ); - - return preg_replace( "#https?://{$primary->domain}#", $unmapped, $value ); + return Helpers::instance()->unmap( $value ); } /** From 79798b0436b9c00ebe111a6bf2f2ef0097388a4e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 15:02:03 +0100 Subject: [PATCH 198/319] Renamed the Processors namespace to be singular. --- includes/classes/DarkMatter.php | 2 +- .../classes/DomainMapping/{Processors => Processor}/Mapping.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename includes/classes/DomainMapping/{Processors => Processor}/Mapping.php (99%) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index 56daaa9a..3463430c 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -68,7 +68,7 @@ private function class_register( $classes = [] ) { public function register_domainmapping() { $this->class_register( [ - DomainMapping\Processors\Mapping::class, + DomainMapping\Processor\Mapping::class, ] ); diff --git a/includes/classes/DomainMapping/Processors/Mapping.php b/includes/classes/DomainMapping/Processor/Mapping.php similarity index 99% rename from includes/classes/DomainMapping/Processors/Mapping.php rename to includes/classes/DomainMapping/Processor/Mapping.php index f23a24c3..e420fe83 100644 --- a/includes/classes/DomainMapping/Processors/Mapping.php +++ b/includes/classes/DomainMapping/Processor/Mapping.php @@ -6,7 +6,7 @@ * @since 2.0.0 */ -namespace DarkMatter\DomainMapping\Processors; +namespace DarkMatter\DomainMapping\Processor; use DarkMatter\DomainMapping\Helpers; use DarkMatter\Interfaces\Registerable; From 8cbd164bf9f69c98a35bb35b35a16a35ea4dff81 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 15:05:28 +0100 Subject: [PATCH 199/319] Renamed Helpers to singular. --- domain-mapping/classes/third-party/class-dm-yoast.php | 2 +- includes/classes/DomainMapping/{Helpers.php => Helper.php} | 4 ++-- includes/classes/DomainMapping/Processor/Mapping.php | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) rename includes/classes/DomainMapping/{Helpers.php => Helper.php} (98%) diff --git a/domain-mapping/classes/third-party/class-dm-yoast.php b/domain-mapping/classes/third-party/class-dm-yoast.php index 0aa2f0c6..4887b780 100644 --- a/domain-mapping/classes/third-party/class-dm-yoast.php +++ b/domain-mapping/classes/third-party/class-dm-yoast.php @@ -37,7 +37,7 @@ public function fix_indexable_permalinks( $intend_to_save, $indexable ) { * If saving to the database, then make sure the permalink is unmapped. */ if ( $intend_to_save ) { - $indexable->permalink = \DarkMatter\DomainMapping\Helpers::instance()->unmap( $indexable->permalink ); + $indexable->permalink = \DarkMatter\DomainMapping\Helper::instance()->unmap( $indexable->permalink ); } return $intend_to_save; diff --git a/includes/classes/DomainMapping/Helpers.php b/includes/classes/DomainMapping/Helper.php similarity index 98% rename from includes/classes/DomainMapping/Helpers.php rename to includes/classes/DomainMapping/Helper.php index 3361d5bd..102f0561 100644 --- a/includes/classes/DomainMapping/Helpers.php +++ b/includes/classes/DomainMapping/Helper.php @@ -12,7 +12,7 @@ * * @since 3.0.0 */ -class Helpers { +class Helper { /** * Map the primary domain on the passed in value if it contains the unmapped URL and the Site has a primary domain. * @@ -90,7 +90,7 @@ public function unmap( $value = '' ) { /** * Return the Singleton Instance of the class. * - * @return Helpers + * @return Helper */ public static function instance() { static $instance = false; diff --git a/includes/classes/DomainMapping/Processor/Mapping.php b/includes/classes/DomainMapping/Processor/Mapping.php index e420fe83..1c54711f 100644 --- a/includes/classes/DomainMapping/Processor/Mapping.php +++ b/includes/classes/DomainMapping/Processor/Mapping.php @@ -8,7 +8,7 @@ namespace DarkMatter\DomainMapping\Processor; -use DarkMatter\DomainMapping\Helpers; +use DarkMatter\DomainMapping\Helper; use DarkMatter\Interfaces\Registerable; /** @@ -224,7 +224,7 @@ private function is_mapped() { * @return string If unmapped URL is found, then returns the primary URL. Untouched otherwise. */ public function map( $value = '', $blog_id = 0 ) { - return Helpers::instance()->map( $value, $blog_id ); + return Helper::instance()->map( $value, $blog_id ); } /** @@ -473,7 +473,7 @@ public function siteurl( $url = '', $path = '', $scheme = null, $blog_id = 0 ) { * @return mixed If unmapped URL is found, then returns the primary URL. Untouched otherwise. */ public function unmap( $value = '' ) { - return Helpers::instance()->unmap( $value ); + return Helper::instance()->unmap( $value ); } /** From e6ea982ee737857f4b25588b0ab141677c5842f6 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sat, 29 Oct 2022 15:11:00 +0100 Subject: [PATCH 200/319] Fixed the unit tests for domain mapping. --- includes/classes/DomainMapping/Processor/Mapping.php | 8 ++++---- tests/phpunit/domain-mapping/MappingDomainsTest.php | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/includes/classes/DomainMapping/Processor/Mapping.php b/includes/classes/DomainMapping/Processor/Mapping.php index 1c54711f..df5b6508 100644 --- a/includes/classes/DomainMapping/Processor/Mapping.php +++ b/includes/classes/DomainMapping/Processor/Mapping.php @@ -26,7 +26,7 @@ class Mapping implements Registerable { * * @var bool */ - public $is_request_mapped = false; + public static $is_request_mapped = false; /** * Register the hooks and actions. @@ -34,7 +34,7 @@ class Mapping implements Registerable { * @since 3.0.0 */ public function register() { - $this->is_request_mapped = ( defined( 'DOMAIN_MAPPING' ) && DOMAIN_MAPPING ); + self::$is_request_mapped = ( defined( 'DOMAIN_MAPPING' ) && DOMAIN_MAPPING ); /** * In some circumstances, we always want to process the logic regardless of request type, circumstances, @@ -197,7 +197,7 @@ private function is_mapped() { * the context can be mapped (i.e. it has an active primary domain) and if so, we say the request is mapped. */ global $switched; - if ( $switched && $this->is_request_mapped ) { + if ( $switched && self::$is_request_mapped ) { $primary = \DarkMatter_Primary::instance()->get(); /** @@ -210,7 +210,7 @@ private function is_mapped() { return true; } - return $this->is_request_mapped; + return self::$is_request_mapped; } /** diff --git a/tests/phpunit/domain-mapping/MappingDomainsTest.php b/tests/phpunit/domain-mapping/MappingDomainsTest.php index e6ea9a74..c7cfd2d9 100644 --- a/tests/phpunit/domain-mapping/MappingDomainsTest.php +++ b/tests/phpunit/domain-mapping/MappingDomainsTest.php @@ -188,7 +188,7 @@ public function test_logout_url() { * @return void */ public function test_rest_url() { - DM_URL::instance()->is_request_mapped = true; + \DarkMatter\DomainMapping\Processor\Mapping::$is_request_mapped = true; $this->assertEquals( /** @@ -199,6 +199,6 @@ public function test_rest_url() { 'REST API URL' ); - DM_URL::instance()->is_request_mapped = false; + \DarkMatter\DomainMapping\Processor\Mapping::$is_request_mapped = false; } } From b00374158f75fa9ed8d307bd774a19a1c552d0ad Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 10:07:13 +0000 Subject: [PATCH 201/319] Moved DM_Media into it's new home. --- .../classes/DomainMapping/Processor}/class-dm-media.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {domain-mapping/classes => includes/classes/DomainMapping/Processor}/class-dm-media.php (100%) diff --git a/domain-mapping/classes/class-dm-media.php b/includes/classes/DomainMapping/Processor/class-dm-media.php similarity index 100% rename from domain-mapping/classes/class-dm-media.php rename to includes/classes/DomainMapping/Processor/class-dm-media.php From 2c4ddbba22bef61b7fb88ef642405ef30419de9c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 10:16:31 +0000 Subject: [PATCH 202/319] Namespaced and renamed DM_Media into the Processors. --- domain-mapping/domain-mapping.php | 1 - includes/classes/DarkMatter.php | 1 + .../{class-dm-media.php => Media.php} | 36 ++++++------------- 3 files changed, 11 insertions(+), 27 deletions(-) rename includes/classes/DomainMapping/Processor/{class-dm-media.php => Media.php} (93%) diff --git a/domain-mapping/domain-mapping.php b/domain-mapping/domain-mapping.php index 5984f301..bbe36a9c 100644 --- a/domain-mapping/domain-mapping.php +++ b/domain-mapping/domain-mapping.php @@ -17,7 +17,6 @@ require_once DM_PATH . '/domain-mapping/inc/compat.php'; -require_once DM_PATH . '/domain-mapping/classes/class-dm-media.php'; require_once DM_PATH . '/domain-mapping/classes/class-dm-domain.php'; require_once DM_PATH . '/domain-mapping/classes/class-dm-healthchecks.php'; diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index 3463430c..8a59e995 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -69,6 +69,7 @@ public function register_domainmapping() { $this->class_register( [ DomainMapping\Processor\Mapping::class, + DomainMapping\Processor\Media::class, ] ); diff --git a/includes/classes/DomainMapping/Processor/class-dm-media.php b/includes/classes/DomainMapping/Processor/Media.php similarity index 93% rename from includes/classes/DomainMapping/Processor/class-dm-media.php rename to includes/classes/DomainMapping/Processor/Media.php index 706b9bc5..3424f216 100644 --- a/includes/classes/DomainMapping/Processor/class-dm-media.php +++ b/includes/classes/DomainMapping/Processor/Media.php @@ -1,20 +1,22 @@ get( $site_id ); + $primary = \DarkMatter_Primary::instance()->get( $site_id ); if ( ! empty( $primary ) ) { $main_domains[] = $primary->domain; } @@ -306,8 +308,8 @@ public function prepare_rest() { * Ensures the "raw" version of the content, typically used by Gutenberg through it's middleware pre-load / JS * hydrate process, gets handled the same as content (which runs through the `the_content` hook). * - * @param WP_REST_Response $item Individual post / item in the response that is being processed. - * @return WP_REST_Response Post / item with the content.raw, if present, mapped. + * @param \WP_REST_Response $item Individual post / item in the response that is being processed. + * @return \WP_REST_Response Post / item with the content.raw, if present, mapped. * * @since 2.2.0 */ @@ -344,7 +346,7 @@ private function prime_site( $site_id = 0 ) { /** * Ensure we have media domains to use. */ - $media_domains = DarkMatter_Domains::instance()->get_domains_by_type( DM_DOMAIN_TYPE_MEDIA, $site_id ); + $media_domains = \DarkMatter_Domains::instance()->get_domains_by_type( DM_DOMAIN_TYPE_MEDIA, $site_id ); if ( empty( $media_domains ) ) { $this->sites[ $site_id ] = false; return; @@ -421,22 +423,4 @@ public function unmap( $value = '' ) { $value ); } - - /** - * Return the Singleton Instance of the class. - * - * @return DM_Media - * - * @since 2.2.0 - */ - public static function instance() { - static $instance = false; - - if ( ! $instance ) { - $instance = new self(); - } - - return $instance; - } } -DM_Media::instance(); From e6fd3fe35ba5ecac8fb4e45f254f2703d1aa6c0e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 10:57:45 +0000 Subject: [PATCH 203/319] Moved the DM_UI to it's new home. --- .../classes/DomainMapping/Admin}/class-dm-ui.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {domain-mapping/classes => includes/classes/DomainMapping/Admin}/class-dm-ui.php (100%) diff --git a/domain-mapping/classes/class-dm-ui.php b/includes/classes/DomainMapping/Admin/class-dm-ui.php similarity index 100% rename from domain-mapping/classes/class-dm-ui.php rename to includes/classes/DomainMapping/Admin/class-dm-ui.php From 5641881eb0c41fa0c8328a638fad4f0702293272 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 10:58:33 +0000 Subject: [PATCH 204/319] Renamed DM_UI to DomainSettings (which is more accurate name). --- .../DomainMapping/Admin/{class-dm-ui.php => DomainSettings.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename includes/classes/DomainMapping/Admin/{class-dm-ui.php => DomainSettings.php} (100%) diff --git a/includes/classes/DomainMapping/Admin/class-dm-ui.php b/includes/classes/DomainMapping/Admin/DomainSettings.php similarity index 100% rename from includes/classes/DomainMapping/Admin/class-dm-ui.php rename to includes/classes/DomainMapping/Admin/DomainSettings.php From cac6b0e6b7c1ad13bdd618b3e6566e4a3b7b6c50 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 11:06:29 +0000 Subject: [PATCH 205/319] Created a new AbstractAdminPage class for simplifying admin pages across the plugin. --- includes/classes/UI/AbstractAdminPage.php | 124 ++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 includes/classes/UI/AbstractAdminPage.php diff --git a/includes/classes/UI/AbstractAdminPage.php b/includes/classes/UI/AbstractAdminPage.php new file mode 100644 index 00000000..872ffacb --- /dev/null +++ b/includes/classes/UI/AbstractAdminPage.php @@ -0,0 +1,124 @@ +parent_slug, + $this->page_title, + $this->menu_title, + $this->permission, + $this->slug, + [ $this, 'page' ] + ); + + add_action( 'load-' . $hook_suffix, [ $this, 'enqueue' ] ); + } + + /** + * Used for enqueuing custom scripts and styles. + * + * @since 3.0.0 + * + * @return void + */ + abstract public function enqueue(); + + /** + * Handle the rendering of the page. + * + * @return void + */ + public function page() { + if ( ! current_user_can( $this->permission ) ) { + wp_die( esc_html__( 'You do not have permission to manage domains.', 'dark-matter' ) ); + } + + $this->render(); + } + + /** + * Produce the HTML output of the admin page. + * + * @since 3.0.0 + * + * @return void + */ + abstract public function render(); + + /** + * Register hooks for this class. + * + * @since 3.0.0 + * + * @return void + */ + public function register() { + add_action( 'admin_menu', [ $this, 'admin_menu' ] ); + } +} From 599df488bceebc912485d37df70442e4580b2119 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 11:07:04 +0000 Subject: [PATCH 206/319] Namespaced and updated the old DM_UI to use the new Abstract class. --- .../DomainMapping/Admin/DomainSettings.php | 80 +++++-------------- 1 file changed, 21 insertions(+), 59 deletions(-) diff --git a/includes/classes/DomainMapping/Admin/DomainSettings.php b/includes/classes/DomainMapping/Admin/DomainSettings.php index 105dd26b..a0170325 100644 --- a/includes/classes/DomainMapping/Admin/DomainSettings.php +++ b/includes/classes/DomainMapping/Admin/DomainSettings.php @@ -1,56 +1,42 @@ slug = 'domains'; + $this->menu_title = __( 'Domains', 'dark-matter' ); + $this->page_title = __( 'Domain Mappings', 'dark-matter' ); + /** - * The root website cannot be mapped. + * Allows the override of the default permission for per site domain management. + * + * @since 2.1.2 + * + * @param string $capability Capability required to manage domains (upgrade_network / Super Admin). + * @param string $context The context the permission is checked. */ - if ( is_main_site() ) { - return; - } - - add_action( 'admin_menu', [ $this, 'admin_menu' ] ); - } - - /** - * Initialise the admin menu and prep the hooks for the CSS and JavaScript - * includes. - * - * @since 2.0.0 - * - * @return void - */ - public function admin_menu() { - $hook_suffix = add_options_page( - __( 'Domain Mappings', 'dark-matter' ), - __( 'Domains', 'dark-matter' ), - $this->get_permission(), - 'domains', - array( - $this, - 'page', - ) - ); - - add_action( 'load-' . $hook_suffix, array( $this, 'enqueue' ) ); + $this->permission = apply_filters( 'dark_matter_domain_permission', 'upgrade_network', 'admin' ); } /** @@ -85,25 +71,6 @@ public function enqueue() { wp_enqueue_style( 'dark-matter-domains', DM_PLUGIN_URL . 'domain-mapping/build/domain-mapping-style' . $min . '.css', [], DM_VERSION ); } - /** - * Retrieve the capability that is required for using the admin page. - * - * @since 2.1.2 - * - * @return string Capability that must be met to use the Admin page. - */ - public function get_permission() { - /** - * Allows the override of the default permission for per site domain management. - * - * @since 2.1.2 - * - * @param string $capability Capability required to manage domains (upgrade_network / Super Admin). - * @param string $context The context the permission is checked. - */ - return apply_filters( 'dark_matter_domain_permission', 'upgrade_network', 'admin' ); - } - /** * Very basic HTML output for the * @@ -111,14 +78,9 @@ public function get_permission() { * * @return void */ - public function page() { - if ( ! current_user_can( $this->get_permission() ) ) { - wp_die( esc_html__( 'You do not have permission to manage domains.', 'dark-matter' ) ); - } + public function render() { ?>
Date: Sun, 30 Oct 2022 11:07:24 +0000 Subject: [PATCH 207/319] Added the logic for including the Settings > Domains admin page. --- domain-mapping/domain-mapping.php | 4 ---- includes/classes/DarkMatter.php | 19 +++++++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/domain-mapping/domain-mapping.php b/domain-mapping/domain-mapping.php index bbe36a9c..906bd436 100644 --- a/domain-mapping/domain-mapping.php +++ b/domain-mapping/domain-mapping.php @@ -25,10 +25,6 @@ */ require_once DM_PATH . '/domain-mapping/classes/third-party/class-dm-yoast.php'; -if ( ! defined( 'DARKMATTER_HIDE_UI' ) || ! DARKMATTER_HIDE_UI ) { - require_once DM_PATH . '/domain-mapping/classes/class-dm-ui.php'; -} - require_once DM_PATH . '/domain-mapping/api/class-darkmatter-domains.php'; require_once DM_PATH . '/domain-mapping/api/class-darkmatter-primary.php'; require_once DM_PATH . '/domain-mapping/api/class-darkmatter-restrict.php'; diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index 8a59e995..e8727f2e 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -66,12 +66,19 @@ private function class_register( $classes = [] ) { * @return void */ public function register_domainmapping() { - $this->class_register( - [ - DomainMapping\Processor\Mapping::class, - DomainMapping\Processor\Media::class, - ] - ); + $domainmapping_classes = [ + DomainMapping\Processor\Mapping::class, + DomainMapping\Processor\Media::class, + ]; + + if ( + ( ! defined( 'DARKMATTER_HIDE_UI' ) || ! DARKMATTER_HIDE_UI ) + && ! is_main_site() + ) { + $domainmapping_classes[] = DomainMapping\Admin\DomainSettings::class; + } + + $this->class_register( $domainmapping_classes ); if ( defined( 'WP_CLI' ) && WP_CLI ) { $this->class_register( From 46690682bdbcbd9ed73cc5fe0c353809b7c5ce8c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 11:09:36 +0000 Subject: [PATCH 208/319] Given the changes to DomainSettings from it's old class, changed the since to 3.0.0. --- .../DomainMapping/Admin/DomainSettings.php | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/includes/classes/DomainMapping/Admin/DomainSettings.php b/includes/classes/DomainMapping/Admin/DomainSettings.php index a0170325..96ac163b 100644 --- a/includes/classes/DomainMapping/Admin/DomainSettings.php +++ b/includes/classes/DomainMapping/Admin/DomainSettings.php @@ -2,8 +2,9 @@ /** * Settings page for controlling domains. * + * @since 3.0.0 + * * @package DarkMatter\DomainMapping - * @since 2.0.0 */ namespace DarkMatter\DomainMapping\Admin; @@ -13,15 +14,15 @@ /** * Class DomainSettings * - * Previously called `DM_UI`. + * Based on the old class called `DM_UI`. * - * @since 2.0.0 + * @since 3.0.0 */ class DomainSettings extends AbstractAdminPage { /** * Constructor * - * @since 2.0.0 + * @since 3.0.0 */ public function __construct() { $this->slug = 'domains'; @@ -34,7 +35,7 @@ public function __construct() { * @since 2.1.2 * * @param string $capability Capability required to manage domains (upgrade_network / Super Admin). - * @param string $context The context the permission is checked. + * @param string $context The context the permission is checked. */ $this->permission = apply_filters( 'dark_matter_domain_permission', 'upgrade_network', 'admin' ); } @@ -42,7 +43,7 @@ public function __construct() { /** * Enqueue assets for the Admin Page. * - * @since 2.0.0 + * @since 3.0.0 * * @return void */ @@ -60,10 +61,10 @@ public function enqueue() { wp_localize_script( 'dark-matter-domains', 'dmSettings', - array( + [ 'rest_root' => get_rest_url(), 'nonce' => wp_create_nonce( 'wp_rest' ), - ) + ] ); wp_enqueue_script( 'dark-matter-domains' ); @@ -72,9 +73,9 @@ public function enqueue() { } /** - * Very basic HTML output for the + * Output the HTML container for the React app. * - * @since 2.0.0 + * @since 3.0.0 * * @return void */ From 30f8ced7231b3110baa466d0b2e9359669bc143b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 11:06:29 +0000 Subject: [PATCH 209/319] Created a new AbstractAdminPage class for simplifying admin pages across the plugin. --- includes/classes/UI/AbstractAdminPage.php | 124 ++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 includes/classes/UI/AbstractAdminPage.php diff --git a/includes/classes/UI/AbstractAdminPage.php b/includes/classes/UI/AbstractAdminPage.php new file mode 100644 index 00000000..872ffacb --- /dev/null +++ b/includes/classes/UI/AbstractAdminPage.php @@ -0,0 +1,124 @@ +parent_slug, + $this->page_title, + $this->menu_title, + $this->permission, + $this->slug, + [ $this, 'page' ] + ); + + add_action( 'load-' . $hook_suffix, [ $this, 'enqueue' ] ); + } + + /** + * Used for enqueuing custom scripts and styles. + * + * @since 3.0.0 + * + * @return void + */ + abstract public function enqueue(); + + /** + * Handle the rendering of the page. + * + * @return void + */ + public function page() { + if ( ! current_user_can( $this->permission ) ) { + wp_die( esc_html__( 'You do not have permission to manage domains.', 'dark-matter' ) ); + } + + $this->render(); + } + + /** + * Produce the HTML output of the admin page. + * + * @since 3.0.0 + * + * @return void + */ + abstract public function render(); + + /** + * Register hooks for this class. + * + * @since 3.0.0 + * + * @return void + */ + public function register() { + add_action( 'admin_menu', [ $this, 'admin_menu' ] ); + } +} From 874884463982c284676380f3745aa323019d7e3c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 11:12:46 +0000 Subject: [PATCH 210/319] Moved the Installer class instantiation to the correct place. --- includes/classes/DarkMatter.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index e8727f2e..084aea20 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -24,8 +24,6 @@ public function __construct() { * Register and handle Domain Mapping module. */ $this->register_domainmapping(); - - new DomainMapping\Installer(); } /** @@ -67,6 +65,7 @@ private function class_register( $classes = [] ) { */ public function register_domainmapping() { $domainmapping_classes = [ + DomainMapping\Installer::class, DomainMapping\Processor\Mapping::class, DomainMapping\Processor\Media::class, ]; From 08901e49dce7455d057b86beae707b49b84aa5f7 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 11:13:57 +0000 Subject: [PATCH 211/319] Added the since for the DarkMatter class and methods. --- includes/classes/DarkMatter.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index 084aea20..54572c31 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -2,6 +2,8 @@ /** * Hello, this is the beginning of the Dark Matter plugin! * + * @since 3.0.0 + * * @package DarkMatter */ @@ -12,10 +14,14 @@ /** * Class DarkMatter. + * + * @since 3.0.0 */ class DarkMatter { /** * Constructor + * + * @since 3.0.0 */ public function __construct() { add_action( 'rest_api_init', [ $this, 'register_rest' ] ); @@ -29,6 +35,8 @@ public function __construct() { /** * Register a set of classes used by the Dark Matter plugin. * + * @since 3.0.0 + * * @param array $classes String of class names / namespaces to be instantiated. * @return array Array of the resultant objects. */ @@ -61,6 +69,8 @@ private function class_register( $classes = [] ) { /** * Register the classes for Domain Mapping. * + * @since 3.0.0 + * * @return void */ public function register_domainmapping() { @@ -93,6 +103,8 @@ public function register_domainmapping() { /** * Register REST routes. This is separate due to the need to be fired on the `rest_api_init` hook. * + * @since 3.0.0 + * * @return void */ public function register_rest() { @@ -107,6 +119,8 @@ public function register_rest() { /** * Return the Singleton Instance of the class. * + * @since 3.0.0 + * * @return DarkMatter */ public static function instance() { From 0e4cf78fb6a5cdbc1c9621d191e38ce27d549a53 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 11:16:02 +0000 Subject: [PATCH 212/319] Moved DM_Domain into it's new home. --- .../classes/DomainMapping/Data}/class-dm-domain.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {domain-mapping/classes => includes/classes/DomainMapping/Data}/class-dm-domain.php (100%) diff --git a/domain-mapping/classes/class-dm-domain.php b/includes/classes/DomainMapping/Data/class-dm-domain.php similarity index 100% rename from domain-mapping/classes/class-dm-domain.php rename to includes/classes/DomainMapping/Data/class-dm-domain.php From fe1d89f9a7b01aaaaaa7ccd063142d6e06e4fc39 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 11:20:07 +0000 Subject: [PATCH 213/319] Namespaced DM_Domain into it's new home. --- .../Data/{class-dm-domain.php => Domain.php} | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) rename includes/classes/DomainMapping/Data/{class-dm-domain.php => Domain.php} (88%) diff --git a/includes/classes/DomainMapping/Data/class-dm-domain.php b/includes/classes/DomainMapping/Data/Domain.php similarity index 88% rename from includes/classes/DomainMapping/Data/class-dm-domain.php rename to includes/classes/DomainMapping/Data/Domain.php index 4986174b..dff8d15f 100644 --- a/includes/classes/DomainMapping/Data/class-dm-domain.php +++ b/includes/classes/DomainMapping/Data/Domain.php @@ -1,19 +1,22 @@ $value ) { From 6f97baeab30b20005ffd56cdae1ee727aa5c29f8 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 11:24:40 +0000 Subject: [PATCH 214/319] Updated the references to DM_Domain to the new namespace version. --- .../api/class-darkmatter-domains.php | 24 +++++++++---------- .../api/class-darkmatter-primary.php | 2 +- domain-mapping/domain-mapping.php | 1 - domain-mapping/inc/sunrise.php | 1 - .../classes/DomainMapping/REST/Domains.php | 4 +++- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/domain-mapping/api/class-darkmatter-domains.php b/domain-mapping/api/class-darkmatter-domains.php index 2fee1b71..3db4c0c3 100644 --- a/domain-mapping/api/class-darkmatter-domains.php +++ b/domain-mapping/api/class-darkmatter-domains.php @@ -153,7 +153,7 @@ private function _clear_caches( $fqdn = '' ) { * @param boolean $force Whether the update should be forced. * @param boolean $active Default is active. Set to false if you wish to add a domain but not make it active. * @param integer $type Domain type. Defaults to `1`, which is "main". - * @return DM_Domain|WP_Error DM_Domain on success. WP_Error on failure. + * @return \DarkMatter\DomainMapping\Data\Domain|WP_Error Domain on success. WP_Error on failure. */ public function add( $fqdn = '', $is_primary = false, $is_https = false, $force = true, $active = true, $type = DM_DOMAIN_TYPE_MAIN ) { $fqdn = $this->_basic_check( $fqdn ); @@ -235,7 +235,7 @@ public function add( $fqdn = '', $is_primary = false, $is_https = false, $force $this->primary_set( $fqdn, get_current_blog_id() ); } - $dm_domain = new DM_Domain( (object) $_domain ); + $dm_domain = new \DarkMatter\DomainMapping\Data\Domain( (object) $_domain ); $this->update_last_changed(); @@ -247,7 +247,7 @@ public function add( $fqdn = '', $is_primary = false, $is_https = false, $force * * @since 2.0.0 * - * @param DM_Domain $dm_domain Domain object of the newly added Domain. + * @param \DarkMatter\DomainMapping\Data\Domain $dm_domain Domain object of the newly added Domain. */ do_action( 'darkmatter_domain_add', $dm_domain ); @@ -381,7 +381,7 @@ public function delete( $fqdn = '', $force = true ) { * * @since 2.0.0 * - * @param DM_Domain $_domain Domain object that was deleted. + * @param \DarkMatter\DomainMapping\Data\Domain $_domain Domain object that was deleted. */ do_action( 'darkmatter_domain_delete', $_domain ); @@ -398,7 +398,7 @@ public function delete( $fqdn = '', $force = true ) { * @since 2.0.0 * * @param string $fqdn FQDN to search for. - * @return DM_Domain|boolean Domain object. False on failure or not found. + * @return \DarkMatter\DomainMapping\Data\Domain|boolean Domain object. False on failure or not found. */ public function find( $fqdn = '' ) { if ( empty( $fqdn ) ) { @@ -414,7 +414,7 @@ public function find( $fqdn = '' ) { * @since 2.0.0 * * @param string $fqdn FQDN to search for. - * @return DM_Domain|boolean Domain object. False otherwise. + * @return \DarkMatter\DomainMapping\Data\Domain|boolean Domain object. False otherwise. */ public function get( $fqdn = '' ) { if ( empty( $fqdn ) ) { @@ -478,7 +478,7 @@ public function get( $fqdn = '' ) { /** * Return the DM_Domain object version. */ - return new DM_Domain( (object) $_domain ); + return new \DarkMatter\DomainMapping\Data\Domain( (object) $_domain ); } /** @@ -515,7 +515,7 @@ public function get_domains_by_type( $type = DM_DOMAIN_TYPE_MEDIA, $site_id = 0, * Convert the domains into DM_Domain objects. */ foreach ( $this->network_media as $i => $media_domain ) { - $media_domains[ $i ] = new DM_Domain( + $media_domains[ $i ] = new \DarkMatter\DomainMapping\Data\Domain( (object) [ 'active' => true, 'blog_id' => get_current_blog_id(), @@ -748,7 +748,7 @@ public function reserve( $fqdn = '' ) { * @param boolean $force Whether the update should be forced. * @param boolean $active Default is active. Set to false if you wish to add a domain but not make it active. * @param integer $type Domain type. Defaults to `null`, do not change current value. Accepts `1` for main and `2` for Media. - * @return DM_Domain|WP_Error DM_Domain on success. WP_Error on failure. + * @return \DarkMatter\DomainMapping\Data\Domain|WP_Error DM_Domain on success. WP_Error on failure. */ public function update( $fqdn = '', $is_primary = null, $is_https = null, $force = true, $active = true, $type = null ) { $fqdn = $this->_basic_check( $fqdn ); @@ -841,7 +841,7 @@ public function update( $fqdn = '', $is_primary = null, $is_https = null, $force $this->primary_unset( $domain_before->domain, $domain_before->blog_id ); } - $domain_after = new DM_Domain( (object) $_domain ); + $domain_after = new \DarkMatter\DomainMapping\Data\Domain( (object) $_domain ); $this->update_last_changed(); @@ -850,8 +850,8 @@ public function update( $fqdn = '', $is_primary = null, $is_https = null, $force * * @since 2.0.0 * - * @param DM_Domain $domain_after Domain object after the changes have been applied successfully. - * @param DM_Domain $domain_before Domain object before. + * @param \DarkMatter\DomainMapping\Data\Domain $domain_after Domain object after the changes have been applied successfully. + * @param \DarkMatter\DomainMapping\Data\Domain $domain_before Domain object before. */ do_action( 'darkmatter_domain_updated', $domain_after, $domain_before ); diff --git a/domain-mapping/api/class-darkmatter-primary.php b/domain-mapping/api/class-darkmatter-primary.php index 7379ba6d..627796d3 100644 --- a/domain-mapping/api/class-darkmatter-primary.php +++ b/domain-mapping/api/class-darkmatter-primary.php @@ -61,7 +61,7 @@ public function __construct() { * @since 2.0.0 * * @param integer $site_id Site ID to retrieve the primary domain for. - * @return DM_Domain|boolean Returns the DM_Domain object on success. False otherwise. + * @return \DarkMatter\DomainMapping\Data\Domain|boolean Returns the DM_Domain object on success. False otherwise. */ public function get( $site_id = 0 ) { $site_id = ( empty( $site_id ) ? get_current_blog_id() : $site_id ); diff --git a/domain-mapping/domain-mapping.php b/domain-mapping/domain-mapping.php index 906bd436..59ae416f 100644 --- a/domain-mapping/domain-mapping.php +++ b/domain-mapping/domain-mapping.php @@ -17,7 +17,6 @@ require_once DM_PATH . '/domain-mapping/inc/compat.php'; -require_once DM_PATH . '/domain-mapping/classes/class-dm-domain.php'; require_once DM_PATH . '/domain-mapping/classes/class-dm-healthchecks.php'; /** diff --git a/domain-mapping/inc/sunrise.php b/domain-mapping/inc/sunrise.php index c54f1beb..0ea28bde 100644 --- a/domain-mapping/inc/sunrise.php +++ b/domain-mapping/inc/sunrise.php @@ -25,7 +25,6 @@ /** * Load the necessary parts of Dark Matter in to place. */ -require_once $dirname . '/classes/class-dm-domain.php'; require_once $dirname . '/api/class-darkmatter-domains.php'; require_once $dirname . '/api/class-darkmatter-primary.php'; diff --git a/includes/classes/DomainMapping/REST/Domains.php b/includes/classes/DomainMapping/REST/Domains.php index c3100ca6..8acc7dd4 100644 --- a/includes/classes/DomainMapping/REST/Domains.php +++ b/includes/classes/DomainMapping/REST/Domains.php @@ -8,6 +8,8 @@ namespace DarkMatter\DomainMapping\REST; +use DarkMatter\DomainMapping\Data\Domain; + /** * Class Domains * @@ -406,7 +408,7 @@ protected function prepare_item_for_database( $request ) { * * @since 2.0.0 * - * @param \DM_Domain $item Domain object to be prepared for response. + * @param Domain $item Domain object to be prepared for response. * @param \WP_REST_Request $request Current request. * @return array Prepared item for REST response. */ From e84964718e60ec4f348e5ca57c7dc4c561335db5 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 11:26:31 +0000 Subject: [PATCH 215/319] Fixed a few DM_Domain references in the unit tests. --- tests/phpunit/domain-mapping/MediaDomainsTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/domain-mapping/MediaDomainsTest.php b/tests/phpunit/domain-mapping/MediaDomainsTest.php index 56c1befb..f9510302 100644 --- a/tests/phpunit/domain-mapping/MediaDomainsTest.php +++ b/tests/phpunit/domain-mapping/MediaDomainsTest.php @@ -36,7 +36,7 @@ public function setUp() : void { ] ); } - + /** * Media domains set by the `DM_NETWORK_MEDIA` constant. * @@ -54,7 +54,7 @@ public function test_get_media_domains_constant() { $expected = []; foreach ( $constant_domains as $media_domain ) { - $expected[] = new DM_Domain( + $expected[] = new \DarkMatter\DomainMapping\Data\Domain( (object) [ 'active' => true, 'blog_id' => get_current_blog_id(), @@ -114,7 +114,7 @@ public function test_get_media_domains_manual() { $expected = []; foreach ( $media_domains as $media_domain => $id ) { - $expected[] = new DM_Domain( + $expected[] = new \DarkMatter\DomainMapping\Data\Domain( (object) [ 'active' => true, 'blog_id' => get_current_blog_id(), From 1185cee5b26da885cdc61336fb7f6d50cdacda6b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:12:13 +0000 Subject: [PATCH 216/319] Moved DarkMatter_Primary to its new home. --- .../classes/DomainMapping/Manager}/class-darkmatter-primary.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {domain-mapping/api => includes/classes/DomainMapping/Manager}/class-darkmatter-primary.php (100%) diff --git a/domain-mapping/api/class-darkmatter-primary.php b/includes/classes/DomainMapping/Manager/class-darkmatter-primary.php similarity index 100% rename from domain-mapping/api/class-darkmatter-primary.php rename to includes/classes/DomainMapping/Manager/class-darkmatter-primary.php From 3c5c9cdf4c3ce6d85f47ff3e685c14da55f174b4 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:16:36 +0000 Subject: [PATCH 217/319] Namespaced the Primary domain in its new home. --- ...ass-darkmatter-primary.php => Primary.php} | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) rename includes/classes/DomainMapping/Manager/{class-darkmatter-primary.php => Primary.php} (86%) diff --git a/includes/classes/DomainMapping/Manager/class-darkmatter-primary.php b/includes/classes/DomainMapping/Manager/Primary.php similarity index 86% rename from includes/classes/DomainMapping/Manager/class-darkmatter-primary.php rename to includes/classes/DomainMapping/Manager/Primary.php index 627796d3..096f5965 100644 --- a/includes/classes/DomainMapping/Manager/class-darkmatter-primary.php +++ b/includes/classes/DomainMapping/Manager/Primary.php @@ -1,21 +1,25 @@ get( $primary_domain ); return $_domain; @@ -130,7 +134,7 @@ public function get_all() { return array(); } - $db = DarkMatter_Domains::instance(); + $db = \DarkMatter_Domains::instance(); /** * Retrieve the DM_Domain objects for each of the primary domains. @@ -154,13 +158,13 @@ public function get_all() { * @return boolean True on success, false otherwise. */ public function set( $site_id = 0, $domain = '' ) { - $new_primary_domain = DarkMatter_Domains::instance()->get( $domain ); + $new_primary_domain = \DarkMatter_Domains::instance()->get( $domain ); if ( $new_primary_domain->blog_id !== $site_id ) { return false; } - $result = DarkMatter_Domains::instance()->update( + $result = \DarkMatter_Domains::instance()->update( $new_primary_domain->domain, true, $new_primary_domain->is_https, @@ -188,13 +192,13 @@ public function set( $site_id = 0, $domain = '' ) { * @return boolean True on success. False otherwise. */ public function unset( $site_id = 0, $domain = '', $db = false ) { - $new_primary_domain = DarkMatter_Domains::instance()->get( $domain ); + $new_primary_domain = \DarkMatter_Domains::instance()->get( $domain ); if ( $new_primary_domain->blog_id !== $site_id ) { return false; } - $result = DarkMatter_Domains::instance()->update( + $result = \DarkMatter_Domains::instance()->update( $new_primary_domain->domain, false, $new_primary_domain->is_https, @@ -226,7 +230,7 @@ private function update_last_changed() { * * @since 2.0.0 * - * @return DarkMatter_Primary + * @return Primary */ public static function instance() { static $instance = false; From 6778fef69d502a0733db2703c3c10590edfe830a Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:27:06 +0000 Subject: [PATCH 218/319] Updated the references to Primary to the new namespace class. --- domain-mapping/api/class-darkmatter-domains.php | 6 +++--- domain-mapping/domain-mapping.php | 1 - domain-mapping/inc/redirect.php | 2 +- domain-mapping/inc/sunrise.php | 1 - includes/classes/DomainMapping/Helper.php | 6 ++++-- includes/classes/DomainMapping/Processor/Mapping.php | 3 ++- includes/classes/DomainMapping/Processor/Media.php | 4 +++- 7 files changed, 13 insertions(+), 10 deletions(-) diff --git a/domain-mapping/api/class-darkmatter-domains.php b/domain-mapping/api/class-darkmatter-domains.php index 3db4c0c3..7423dad8 100644 --- a/domain-mapping/api/class-darkmatter-domains.php +++ b/domain-mapping/api/class-darkmatter-domains.php @@ -169,7 +169,7 @@ public function add( $fqdn = '', $is_primary = false, $is_https = false, $force return new WP_Error( 'exists', __( 'This domain is already assigned to a Site.', 'dark-matter' ) ); } - $dm_primary = DarkMatter_Primary::instance(); + $dm_primary = \DarkMatter\DomainMapping\Manager\Primary::instance(); if ( $is_primary ) { $primary_domain = $dm_primary->get(); @@ -671,7 +671,7 @@ public function is_reserved( $fqdn = '' ) { * @return void */ private function primary_set( $domain = '', $blog_id = 0 ) { - $current_primary = DarkMatter_Primary::instance()->get( $blog_id ); + $current_primary = \DarkMatter\DomainMapping\Manager\Primary::instance()->get( $blog_id ); if ( ! empty( $current_primary ) && $domain !== $current_primary->domain ) { $this->update( $current_primary->domain, false, null, true, $current_primary->active ); @@ -763,7 +763,7 @@ public function update( $fqdn = '', $is_primary = null, $is_https = null, $force return new WP_Error( 'not found', __( 'Cannot find the domain to update.', 'dark-matter' ) ); } - $dm_primary = DarkMatter_Primary::instance(); + $dm_primary = \DarkMatter\DomainMapping\Manager\Primary::instance(); $_domain = array( 'active' => ( ! $active ? false : true ), diff --git a/domain-mapping/domain-mapping.php b/domain-mapping/domain-mapping.php index 59ae416f..b14890b7 100644 --- a/domain-mapping/domain-mapping.php +++ b/domain-mapping/domain-mapping.php @@ -25,7 +25,6 @@ require_once DM_PATH . '/domain-mapping/classes/third-party/class-dm-yoast.php'; require_once DM_PATH . '/domain-mapping/api/class-darkmatter-domains.php'; -require_once DM_PATH . '/domain-mapping/api/class-darkmatter-primary.php'; require_once DM_PATH . '/domain-mapping/api/class-darkmatter-restrict.php'; /** diff --git a/domain-mapping/inc/redirect.php b/domain-mapping/inc/redirect.php index f5fbee20..7febf404 100644 --- a/domain-mapping/inc/redirect.php +++ b/domain-mapping/inc/redirect.php @@ -107,7 +107,7 @@ function darkmatter_maybe_redirect() { $http_host = ( empty( $_SERVER['HTTP_HOST'] ) ? '' : filter_var( $_SERVER['HTTP_HOST'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW ) ); $host = trim( $http_host, '/' ); - $primary = DarkMatter_Primary::instance()->get(); + $primary = \DarkMatter\DomainMapping\Manager\Primary::instance()->get(); $is_admin = false; diff --git a/domain-mapping/inc/sunrise.php b/domain-mapping/inc/sunrise.php index 0ea28bde..c9617c54 100644 --- a/domain-mapping/inc/sunrise.php +++ b/domain-mapping/inc/sunrise.php @@ -26,7 +26,6 @@ * Load the necessary parts of Dark Matter in to place. */ require_once $dirname . '/api/class-darkmatter-domains.php'; -require_once $dirname . '/api/class-darkmatter-primary.php'; /** * Attempt to find the Site. diff --git a/includes/classes/DomainMapping/Helper.php b/includes/classes/DomainMapping/Helper.php index 102f0561..5dde98bf 100644 --- a/includes/classes/DomainMapping/Helper.php +++ b/includes/classes/DomainMapping/Helper.php @@ -7,6 +7,8 @@ namespace DarkMatter\DomainMapping; +use DarkMatter\DomainMapping\Manager\Primary; + /** * Class Helpers * @@ -34,7 +36,7 @@ public function map( $value = '', $blog_id = 0 ) { * Retrieve the current blog. */ $blog = get_site( absint( $blog_id ) ); - $primary = \DarkMatter_Primary::instance()->get( $blog->blog_id ); + $primary = Primary::instance()->get( $blog->blog_id ); $unmapped = untrailingslashit( $blog->domain . $blog->path ); @@ -72,7 +74,7 @@ public function unmap( $value = '' ) { * Retrieve the current blog. */ $blog = get_site(); - $primary = \DarkMatter_Primary::instance()->get(); + $primary = Primary::instance()->get(); /** * If there is no primary domain or the primary domain cannot be found diff --git a/includes/classes/DomainMapping/Processor/Mapping.php b/includes/classes/DomainMapping/Processor/Mapping.php index df5b6508..f8e59e03 100644 --- a/includes/classes/DomainMapping/Processor/Mapping.php +++ b/includes/classes/DomainMapping/Processor/Mapping.php @@ -9,6 +9,7 @@ namespace DarkMatter\DomainMapping\Processor; use DarkMatter\DomainMapping\Helper; +use DarkMatter\DomainMapping\Manager\Primary; use DarkMatter\Interfaces\Registerable; /** @@ -198,7 +199,7 @@ private function is_mapped() { */ global $switched; if ( $switched && self::$is_request_mapped ) { - $primary = \DarkMatter_Primary::instance()->get(); + $primary = Primary::instance()->get(); /** * If there is no primary or if it is inactive, then the site is not mapped. diff --git a/includes/classes/DomainMapping/Processor/Media.php b/includes/classes/DomainMapping/Processor/Media.php index 3424f216..7904bc0e 100644 --- a/includes/classes/DomainMapping/Processor/Media.php +++ b/includes/classes/DomainMapping/Processor/Media.php @@ -9,6 +9,8 @@ namespace DarkMatter\DomainMapping\Processor; +use DarkMatter\DomainMapping\Manager\Primary; + /** * Class Media * @@ -88,7 +90,7 @@ private function get_main_domains( $site_id = 0 ) { $unmapped, ]; - $primary = \DarkMatter_Primary::instance()->get( $site_id ); + $primary = Primary::instance()->get( $site_id ); if ( ! empty( $primary ) ) { $main_domains[] = $primary->domain; } From 8ad93233c8365f6532e504f0bcabe6130b9574c6 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:28:27 +0000 Subject: [PATCH 219/319] Updated the primary domain unit tests. --- tests/phpunit/domain-mapping/PrimaryDomainTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/domain-mapping/PrimaryDomainTest.php b/tests/phpunit/domain-mapping/PrimaryDomainTest.php index 36e3ec16..c0eefa15 100644 --- a/tests/phpunit/domain-mapping/PrimaryDomainTest.php +++ b/tests/phpunit/domain-mapping/PrimaryDomainTest.php @@ -26,7 +26,7 @@ class PrimaryDomainTest extends \WP_UnitTestCase { /** * Holder for Primary class. * - * @var DarkMatter_Primary|null + * @var \DarkMatter\DomainMapping\Manager\Primary|null */ private $darkmatter_primary = null; @@ -68,7 +68,7 @@ public function setUp(): void { parent::setUp(); $this->darkmatter_domains = DarkMatter_Domains::instance(); - $this->darkmatter_primary = DarkMatter_Primary::instance(); + $this->darkmatter_primary = \DarkMatter\DomainMapping\Manager\Primary::instance(); switch_to_blog( self::$blog_id ); } From e48f1c9e601376911de58d4970b24078acb412e9 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:36:21 +0000 Subject: [PATCH 220/319] Moved Restricted to its new home. --- .../classes/DomainMapping/Manager}/class-darkmatter-restrict.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {domain-mapping/api => includes/classes/DomainMapping/Manager}/class-darkmatter-restrict.php (100%) diff --git a/domain-mapping/api/class-darkmatter-restrict.php b/includes/classes/DomainMapping/Manager/class-darkmatter-restrict.php similarity index 100% rename from domain-mapping/api/class-darkmatter-restrict.php rename to includes/classes/DomainMapping/Manager/class-darkmatter-restrict.php From b135a45edad038f6e46ff5f9feada102a829542c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:36:49 +0000 Subject: [PATCH 221/319] Updated Restricted to its new namespace home. --- ...darkmatter-restrict.php => Restricted.php} | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) rename includes/classes/DomainMapping/Manager/{class-darkmatter-restrict.php => Restricted.php} (72%) diff --git a/includes/classes/DomainMapping/Manager/class-darkmatter-restrict.php b/includes/classes/DomainMapping/Manager/Restricted.php similarity index 72% rename from includes/classes/DomainMapping/Manager/class-darkmatter-restrict.php rename to includes/classes/DomainMapping/Manager/Restricted.php index 5b8ee87e..e40c19e2 100644 --- a/includes/classes/DomainMapping/Manager/class-darkmatter-restrict.php +++ b/includes/classes/DomainMapping/Manager/Restricted.php @@ -1,19 +1,22 @@ is_exist( $fqdn ) ) { - return new WP_Error( 'used', __( 'This domain is in use.', 'dark-matter' ) ); + return new \WP_Error( 'used', __( 'This domain is in use.', 'dark-matter' ) ); } return $fqdn; @@ -82,7 +85,7 @@ private function _basic_checks( $fqdn ) { * @since 2.0.0 * * @param string $fqdn Domain to be added to the reserve list. - * @return WP_Error|boolean True on success, WP_Error otherwise. + * @return \WP_Error|boolean True on success, WP_Error otherwise. */ public function add( $fqdn = '' ) { $fqdn = $this->_basic_checks( $fqdn ); @@ -92,7 +95,7 @@ public function add( $fqdn = '' ) { } if ( $this->is_exist( $fqdn ) ) { - return new WP_Error( 'exists', __( 'The Domain is already Restricted.', 'dark-matter' ) ); + return new \WP_Error( 'exists', __( 'The Domain is already Restricted.', 'dark-matter' ) ); } /** @@ -100,8 +103,8 @@ public function add( $fqdn = '' ) { */ global $wpdb; - // phpcs:ignore - $result = $wpdb->insert( + // phpcs:ignore + $result = $wpdb->insert( $this->restrict_table, array( 'domain' => $fqdn, @@ -110,7 +113,7 @@ public function add( $fqdn = '' ) { ); if ( ! $result ) { - return new WP_Error( 'unknown', __( 'An unknown error has occurred. The domain has not been removed from the Restrict list.', 'dark-matter' ) ); + return new \WP_Error( 'unknown', __( 'An unknown error has occurred. The domain has not been removed from the Restrict list.', 'dark-matter' ) ); } $this->refresh_cache(); @@ -133,7 +136,7 @@ public function add( $fqdn = '' ) { * @since 2.0.0 * * @param string $fqdn Domain to be deleted to the restrict list. - * @return WP_Error|boolean True on success, WP_Error otherwise. + * @return \WP_Error|boolean True on success, WP_Error otherwise. */ public function delete( $fqdn = '' ) { $fqdn = $this->_basic_checks( $fqdn ); @@ -143,7 +146,7 @@ public function delete( $fqdn = '' ) { } if ( ! $this->is_exist( $fqdn ) ) { - return new WP_Error( 'missing', __( 'The Domain is not found in the Restrict list.', 'dark-matter' ) ); + return new \WP_Error( 'missing', __( 'The Domain is not found in the Restrict list.', 'dark-matter' ) ); } /** @@ -151,8 +154,8 @@ public function delete( $fqdn = '' ) { */ global $wpdb; - // phpcs:ignore - $result = $wpdb->delete( + // phpcs:ignore + $result = $wpdb->delete( $this->restrict_table, array( 'domain' => $fqdn, @@ -161,7 +164,7 @@ public function delete( $fqdn = '' ) { ); if ( ! $result ) { - return new WP_Error( 'unknown', __( 'An unknown error has occurred. The domain has not been removed from the Restrict list.', 'dark-matter' ) ); + return new \WP_Error( 'unknown', __( 'An unknown error has occurred. The domain has not been removed from the Restrict list.', 'dark-matter' ) ); } $this->refresh_cache(); @@ -211,8 +214,8 @@ public function get() { */ global $wpdb; - // phpcs:ignore - $restricted_domains = $wpdb->get_col( "SELECT domain FROM {$this->restrict_table} ORDER BY domain" ); + // phpcs:ignore + $restricted_domains = $wpdb->get_col( "SELECT domain FROM {$this->restrict_table} ORDER BY domain" ); if ( empty( $restricted_domains ) ) { $restricted_domains = array(); @@ -263,7 +266,7 @@ public function refresh_cache() { * * @since 2.0.0 * - * @return DarkMatter_Restrict + * @return Restricted */ public static function instance() { static $instance = false; From 799f576aa7979868bfb267806fa298f882062fdd Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:37:14 +0000 Subject: [PATCH 222/319] Updated the references to Restricted. --- domain-mapping/api/class-darkmatter-domains.php | 2 +- domain-mapping/domain-mapping.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/domain-mapping/api/class-darkmatter-domains.php b/domain-mapping/api/class-darkmatter-domains.php index 7423dad8..e0b811aa 100644 --- a/domain-mapping/api/class-darkmatter-domains.php +++ b/domain-mapping/api/class-darkmatter-domains.php @@ -110,7 +110,7 @@ private function _basic_check( $fqdn = '' ) { return new WP_Error( 'root', __( 'Domains cannot be mapped to the main / root Site.', 'dark-matter' ) ); } - $reserve = DarkMatter_Restrict::instance(); + $reserve = \DarkMatter\DomainMapping\Manager\Restricted::instance(); if ( $reserve->is_exist( $fqdn ) ) { return new WP_Error( 'reserved', __( 'This domain has been reserved.', 'dark-matter' ) ); } diff --git a/domain-mapping/domain-mapping.php b/domain-mapping/domain-mapping.php index b14890b7..160e60c7 100644 --- a/domain-mapping/domain-mapping.php +++ b/domain-mapping/domain-mapping.php @@ -25,7 +25,6 @@ require_once DM_PATH . '/domain-mapping/classes/third-party/class-dm-yoast.php'; require_once DM_PATH . '/domain-mapping/api/class-darkmatter-domains.php'; -require_once DM_PATH . '/domain-mapping/api/class-darkmatter-restrict.php'; /** * Disable SSO if the COOKIE_DOMAIN constant is set. From 876ebd57070de1378c766ef19244ba6938262cb1 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:42:26 +0000 Subject: [PATCH 223/319] Updated the Restricted unit tests. --- .../phpunit/domain-mapping/RestrictedDomainsTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/domain-mapping/RestrictedDomainsTest.php b/tests/phpunit/domain-mapping/RestrictedDomainsTest.php index afb8937c..459790a6 100644 --- a/tests/phpunit/domain-mapping/RestrictedDomainsTest.php +++ b/tests/phpunit/domain-mapping/RestrictedDomainsTest.php @@ -44,7 +44,7 @@ public function setUp() : void { */ public function test_add_restrict_domain() { $domain = 'restricteddomain1.test'; - $result = DarkMatter_Restrict::instance()->add( $domain ); + $result = \DarkMatter\DomainMapping\Manager\Restricted::instance()->add( $domain ); $this->assertTrue( $result, 'Adding a restricted domain.' ); } @@ -60,13 +60,13 @@ public function test_remove_restrict_domain() { /** * Add domain. */ - $result = DarkMatter_Restrict::instance()->add( $domain ); + $result = \DarkMatter\DomainMapping\Manager\Restricted::instance()->add( $domain ); $this->assertTrue( $result, 'Adding a restricted domain.' ); /** * Remove domain. */ - $result = DarkMatter_Restrict::instance()->delete( $domain ); + $result = \DarkMatter\DomainMapping\Manager\Restricted::instance()->delete( $domain ); $this->assertTrue( $result, 'Removing a restricted domain.' ); } @@ -81,7 +81,7 @@ public function test_restrict_domain_add_domain() { /** * Add domain. */ - $result = DarkMatter_Restrict::instance()->add( $domain ); + $result = \DarkMatter\DomainMapping\Manager\Restricted::instance()->add( $domain ); $this->assertTrue( $result, 'Adding a restricted domain.' ); /** @@ -103,13 +103,13 @@ public function test_get_restricted_domains() { $domains = [ 'restricteddomain1.test', 'restricteddomain2.test' ]; foreach ( $domains as $domain ) { - $result = DarkMatter_Restrict::instance()->add( $domain ); + $result = \DarkMatter\DomainMapping\Manager\Restricted::instance()->add( $domain ); $this->assertTrue( $result, 'Adding a restricted domain.' ); } $this->assertSame( $domains, - DarkMatter_Restrict::instance()->get(), + \DarkMatter\DomainMapping\Manager\Restricted::instance()->get(), 'Get returns all the restricted domains properly.' ); } From d1da2df8449030f6a106f6d0e4515d905bb12a6b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:43:29 +0000 Subject: [PATCH 224/319] Moved Domains to its new home. --- .../classes/DomainMapping/Manager}/class-darkmatter-domains.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {domain-mapping/api => includes/classes/DomainMapping/Manager}/class-darkmatter-domains.php (100%) diff --git a/domain-mapping/api/class-darkmatter-domains.php b/includes/classes/DomainMapping/Manager/class-darkmatter-domains.php similarity index 100% rename from domain-mapping/api/class-darkmatter-domains.php rename to includes/classes/DomainMapping/Manager/class-darkmatter-domains.php From fae47305dd62fb1af9266c8dc7bced35f9a2209d Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:49:12 +0000 Subject: [PATCH 225/319] Updated Domain to be namespaced. --- ...lass-darkmatter-domains.php => Domain.php} | 87 ++++++++++--------- 1 file changed, 46 insertions(+), 41 deletions(-) rename includes/classes/DomainMapping/Manager/{class-darkmatter-domains.php => Domain.php} (85%) diff --git a/includes/classes/DomainMapping/Manager/class-darkmatter-domains.php b/includes/classes/DomainMapping/Manager/Domain.php similarity index 85% rename from includes/classes/DomainMapping/Manager/class-darkmatter-domains.php rename to includes/classes/DomainMapping/Manager/Domain.php index e0b811aa..42b72768 100644 --- a/includes/classes/DomainMapping/Manager/class-darkmatter-domains.php +++ b/includes/classes/DomainMapping/Manager/Domain.php @@ -1,19 +1,24 @@ wpdb = $wpdb; - if ( defined( 'DM_NETWORK_MEDIA' ) && ! empty( DM_NETWORK_MEDIA ) ) { - $this->network_media = DM_NETWORK_MEDIA; + if ( defined( '\DM_NETWORK_MEDIA' ) && ! empty( \DM_NETWORK_MEDIA ) ) { + $this->network_media = \DM_NETWORK_MEDIA; } } @@ -70,11 +75,11 @@ public function __construct() { * @since 2.0.0 * * @param string $fqdn Fully qualified domain name. - * @return WP_Error|boolean True on pass. WP_Error on failure. + * @return \WP_Error|boolean True on pass. WP_Error on failure. */ private function _basic_check( $fqdn = '' ) { if ( empty( $fqdn ) ) { - return new WP_Error( 'empty', __( 'Please include a fully qualified domain name to be added.', 'dark-matter' ) ); + return new \WP_Error( 'empty', __( 'Please include a fully qualified domain name to be added.', 'dark-matter' ) ); } /** @@ -90,7 +95,7 @@ private function _basic_check( $fqdn = '' ) { } if ( ! empty( $domain_parts['path'] ) || ! empty( $domain_parts['port'] ) || ! empty( $domain_parts['query'] ) ) { - return new WP_Error( 'unsure', __( 'The domain provided contains path, port, or query string information. Please removed this before continuing.', 'dark-matter' ) ); + return new \WP_Error( 'unsure', __( 'The domain provided contains path, port, or query string information. Please removed this before continuing.', 'dark-matter' ) ); } $fqdn = $domain_parts['host']; @@ -99,20 +104,20 @@ private function _basic_check( $fqdn = '' ) { * Check to ensure we have a valid domain to work with. */ if ( ! filter_var( $fqdn, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME ) ) { - return new WP_Error( 'domain', __( 'The domain is not valid.', 'dark-matter' ) ); + return new \WP_Error( 'domain', __( 'The domain is not valid.', 'dark-matter' ) ); } if ( defined( 'DOMAIN_CURRENT_SITE' ) && DOMAIN_CURRENT_SITE === $fqdn ) { - return new WP_Error( 'wp-config', __( 'You cannot configure the WordPress Network primary domain.', 'dark-matter' ) ); + return new \WP_Error( 'wp-config', __( 'You cannot configure the WordPress Network primary domain.', 'dark-matter' ) ); } if ( is_main_site() ) { - return new WP_Error( 'root', __( 'Domains cannot be mapped to the main / root Site.', 'dark-matter' ) ); + return new \WP_Error( 'root', __( 'Domains cannot be mapped to the main / root Site.', 'dark-matter' ) ); } $reserve = \DarkMatter\DomainMapping\Manager\Restricted::instance(); if ( $reserve->is_exist( $fqdn ) ) { - return new WP_Error( 'reserved', __( 'This domain has been reserved.', 'dark-matter' ) ); + return new \WP_Error( 'reserved', __( 'This domain has been reserved.', 'dark-matter' ) ); } /** @@ -153,7 +158,7 @@ private function _clear_caches( $fqdn = '' ) { * @param boolean $force Whether the update should be forced. * @param boolean $active Default is active. Set to false if you wish to add a domain but not make it active. * @param integer $type Domain type. Defaults to `1`, which is "main". - * @return \DarkMatter\DomainMapping\Data\Domain|WP_Error Domain on success. WP_Error on failure. + * @return Data\Domain|\WP_Error Domain on success. WP_Error on failure. */ public function add( $fqdn = '', $is_primary = false, $is_https = false, $force = true, $active = true, $type = DM_DOMAIN_TYPE_MAIN ) { $fqdn = $this->_basic_check( $fqdn ); @@ -166,10 +171,10 @@ public function add( $fqdn = '', $is_primary = false, $is_https = false, $force * Check that the FQDN is not already stored in the database. */ if ( $this->is_exist( $fqdn ) ) { - return new WP_Error( 'exists', __( 'This domain is already assigned to a Site.', 'dark-matter' ) ); + return new \WP_Error( 'exists', __( 'This domain is already assigned to a Site.', 'dark-matter' ) ); } - $dm_primary = \DarkMatter\DomainMapping\Manager\Primary::instance(); + $dm_primary = Primary::instance(); if ( $is_primary ) { $primary_domain = $dm_primary->get(); @@ -179,7 +184,7 @@ public function add( $fqdn = '', $is_primary = false, $is_https = false, $force */ if ( ! empty( $primary_domain ) ) { if ( ! $force ) { - return new WP_Error( 'primary', __( 'You cannot add this domain as the primary domain without using the force flag.', 'dark-matter' ) ); + return new \WP_Error( 'primary', __( 'You cannot add this domain as the primary domain without using the force flag.', 'dark-matter' ) ); } } } @@ -188,7 +193,7 @@ public function add( $fqdn = '', $is_primary = false, $is_https = false, $force * Check the type is valid. */ if ( DM_DOMAIN_TYPE_MAIN !== $type && DM_DOMAIN_TYPE_MEDIA !== $type ) { - return new WP_Error( 'type', __( 'The type for the new domain is not supported.', 'dark-matter' ) ); + return new \WP_Error( 'type', __( 'The type for the new domain is not supported.', 'dark-matter' ) ); } $_domain = array( @@ -235,7 +240,7 @@ public function add( $fqdn = '', $is_primary = false, $is_https = false, $force $this->primary_set( $fqdn, get_current_blog_id() ); } - $dm_domain = new \DarkMatter\DomainMapping\Data\Domain( (object) $_domain ); + $dm_domain = new Data\Domain( (object) $_domain ); $this->update_last_changed(); @@ -247,14 +252,14 @@ public function add( $fqdn = '', $is_primary = false, $is_https = false, $force * * @since 2.0.0 * - * @param \DarkMatter\DomainMapping\Data\Domain $dm_domain Domain object of the newly added Domain. + * @param Data\Domain $dm_domain Domain object of the newly added Domain. */ do_action( 'darkmatter_domain_add', $dm_domain ); return $dm_domain; } - return new WP_Error( 'unknown', __( 'Sorry, the domain could not be added. An unknown error occurred.', 'dark-matter' ) ); + return new \WP_Error( 'unknown', __( 'Sorry, the domain could not be added. An unknown error occurred.', 'dark-matter' ) ); } /** @@ -322,7 +327,7 @@ public function clear_cache_for_domain_type( $type = DM_DOMAIN_TYPE_MAIN, $site_ * * @param string $fqdn FQDN to be deleted. * @param boolean $force Force the FQDN to be deleted, even if it is the primary domain. - * @return WP_Error|boolean True on success. False otherwise. + * @return \WP_Error|boolean True on success. False otherwise. */ public function delete( $fqdn = '', $force = true ) { $fqdn = $this->_basic_check( $fqdn ); @@ -335,7 +340,7 @@ public function delete( $fqdn = '', $force = true ) { * Cannot delete what does not exist. */ if ( ! $this->is_exist( $fqdn ) ) { - return new WP_Error( 'exists', __( 'The domain cannot be found.', 'dark-matter' ) ); + return new \WP_Error( 'exists', __( 'The domain cannot be found.', 'dark-matter' ) ); } /** @@ -344,7 +349,7 @@ public function delete( $fqdn = '', $force = true ) { $_domain = $this->get( $fqdn ); if ( ! $_domain || get_current_blog_id() !== $_domain->blog_id ) { - return new WP_Error( 'not found', __( 'The domain cannot be found.', 'dark-matter' ) ); + return new \WP_Error( 'not found', __( 'The domain cannot be found.', 'dark-matter' ) ); } /** @@ -355,7 +360,7 @@ public function delete( $fqdn = '', $force = true ) { if ( $force ) { $this->primary_unset( $_domain->domain, $_domain->blog_id ); } else { - return new WP_Error( 'primary', __( 'This domain is the primary domain for this Site. Please provide the force flag to delete.', 'dark-matter' ) ); + return new \WP_Error( 'primary', __( 'This domain is the primary domain for this Site. Please provide the force flag to delete.', 'dark-matter' ) ); } } @@ -381,14 +386,14 @@ public function delete( $fqdn = '', $force = true ) { * * @since 2.0.0 * - * @param \DarkMatter\DomainMapping\Data\Domain $_domain Domain object that was deleted. + * @param Data\Domain $_domain Domain object that was deleted. */ do_action( 'darkmatter_domain_delete', $_domain ); return true; } - return new WP_Error( 'unknown', __( 'Sorry, the domain could not be deleted. An unknown error occurred.', 'dark-matter' ) ); + return new \WP_Error( 'unknown', __( 'Sorry, the domain could not be deleted. An unknown error occurred.', 'dark-matter' ) ); } @@ -398,7 +403,7 @@ public function delete( $fqdn = '', $force = true ) { * @since 2.0.0 * * @param string $fqdn FQDN to search for. - * @return \DarkMatter\DomainMapping\Data\Domain|boolean Domain object. False on failure or not found. + * @return Data\Domain|boolean Domain object. False on failure or not found. */ public function find( $fqdn = '' ) { if ( empty( $fqdn ) ) { @@ -414,7 +419,7 @@ public function find( $fqdn = '' ) { * @since 2.0.0 * * @param string $fqdn FQDN to search for. - * @return \DarkMatter\DomainMapping\Data\Domain|boolean Domain object. False otherwise. + * @return Data\Domain|boolean Domain object. False otherwise. */ public function get( $fqdn = '' ) { if ( empty( $fqdn ) ) { @@ -478,7 +483,7 @@ public function get( $fqdn = '' ) { /** * Return the DM_Domain object version. */ - return new \DarkMatter\DomainMapping\Data\Domain( (object) $_domain ); + return new Data\Domain( (object) $_domain ); } /** @@ -515,7 +520,7 @@ public function get_domains_by_type( $type = DM_DOMAIN_TYPE_MEDIA, $site_id = 0, * Convert the domains into DM_Domain objects. */ foreach ( $this->network_media as $i => $media_domain ) { - $media_domains[ $i ] = new \DarkMatter\DomainMapping\Data\Domain( + $media_domains[ $i ] = new Data\Domain( (object) [ 'active' => true, 'blog_id' => get_current_blog_id(), @@ -748,7 +753,7 @@ public function reserve( $fqdn = '' ) { * @param boolean $force Whether the update should be forced. * @param boolean $active Default is active. Set to false if you wish to add a domain but not make it active. * @param integer $type Domain type. Defaults to `null`, do not change current value. Accepts `1` for main and `2` for Media. - * @return \DarkMatter\DomainMapping\Data\Domain|WP_Error DM_Domain on success. WP_Error on failure. + * @return Data\Domain|\WP_Error DM_Domain on success. WP_Error on failure. */ public function update( $fqdn = '', $is_primary = null, $is_https = null, $force = true, $active = true, $type = null ) { $fqdn = $this->_basic_check( $fqdn ); @@ -760,7 +765,7 @@ public function update( $fqdn = '', $is_primary = null, $is_https = null, $force $domain_before = $this->get( $fqdn ); if ( ! $domain_before ) { - return new WP_Error( 'not found', __( 'Cannot find the domain to update.', 'dark-matter' ) ); + return new \WP_Error( 'not found', __( 'Cannot find the domain to update.', 'dark-matter' ) ); } $dm_primary = \DarkMatter\DomainMapping\Manager\Primary::instance(); @@ -779,7 +784,7 @@ public function update( $fqdn = '', $is_primary = null, $is_https = null, $force * Any update to the "is primary" requires the force flag. */ if ( ! $force ) { - return new WP_Error( 'primary', __( 'You cannot update the primary flag without setting the force parameter to true', 'dark-matter' ) ); + return new \WP_Error( 'primary', __( 'You cannot update the primary flag without setting the force parameter to true', 'dark-matter' ) ); } $_domain['is_primary'] = $is_primary; @@ -796,7 +801,7 @@ public function update( $fqdn = '', $is_primary = null, $is_https = null, $force if ( DM_DOMAIN_TYPE_MAIN === $type || DM_DOMAIN_TYPE_MEDIA === $type ) { $_domain['type'] = $type; } else { - return new WP_Error( 'type', __( 'The type for the new domain is not supported.', 'dark-matter' ) ); + return new \WP_Error( 'type', __( 'The type for the new domain is not supported.', 'dark-matter' ) ); } } @@ -841,7 +846,7 @@ public function update( $fqdn = '', $is_primary = null, $is_https = null, $force $this->primary_unset( $domain_before->domain, $domain_before->blog_id ); } - $domain_after = new \DarkMatter\DomainMapping\Data\Domain( (object) $_domain ); + $domain_after = new Data\Domain( (object) $_domain ); $this->update_last_changed(); @@ -850,15 +855,15 @@ public function update( $fqdn = '', $is_primary = null, $is_https = null, $force * * @since 2.0.0 * - * @param \DarkMatter\DomainMapping\Data\Domain $domain_after Domain object after the changes have been applied successfully. - * @param \DarkMatter\DomainMapping\Data\Domain $domain_before Domain object before. + * @param Data\Domain $domain_after Domain object after the changes have been applied successfully. + * @param Data\Domain $domain_before Domain object before. */ do_action( 'darkmatter_domain_updated', $domain_after, $domain_before ); return $domain_after; } - return new WP_Error( 'unknown', __( 'Sorry, the domain could not be updated. An unknown error occurred.', 'dark-matter' ) ); + return new \WP_Error( 'unknown', __( 'Sorry, the domain could not be updated. An unknown error occurred.', 'dark-matter' ) ); } /** @@ -877,7 +882,7 @@ private function update_last_changed() { * * @since 2.0.0 * - * @return DarkMatter_Domains + * @return Domain */ public static function instance() { static $instance = false; From 3fe02910d6bbb6217bf14cde6cc5ac069eb648ba Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:54:35 +0000 Subject: [PATCH 226/319] Updated most of the references to use the new Domain manager. --- domain-mapping/domain-mapping.php | 2 -- includes/classes/DomainMapping/Manager/Primary.php | 13 ++++++------- .../classes/DomainMapping/Manager/Restricted.php | 2 +- .../classes/DomainMapping/Processor/Mapping.php | 3 ++- includes/classes/DomainMapping/Processor/Media.php | 3 ++- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/domain-mapping/domain-mapping.php b/domain-mapping/domain-mapping.php index 160e60c7..fd31deed 100644 --- a/domain-mapping/domain-mapping.php +++ b/domain-mapping/domain-mapping.php @@ -24,8 +24,6 @@ */ require_once DM_PATH . '/domain-mapping/classes/third-party/class-dm-yoast.php'; -require_once DM_PATH . '/domain-mapping/api/class-darkmatter-domains.php'; - /** * Disable SSO if the COOKIE_DOMAIN constant is set. */ diff --git a/includes/classes/DomainMapping/Manager/Primary.php b/includes/classes/DomainMapping/Manager/Primary.php index 096f5965..fc0b134b 100644 --- a/includes/classes/DomainMapping/Manager/Primary.php +++ b/includes/classes/DomainMapping/Manager/Primary.php @@ -10,7 +10,6 @@ namespace DarkMatter\DomainMapping\Manager; // phpcs:disable PHPCompatibility.Keywords.ForbiddenNames.unsetFound -use DarkMatter\DomainMapping\Data\Domain; /** * Class Primary @@ -111,7 +110,7 @@ public function get( $site_id = 0 ) { /** * Retrieve the entire Domain object. */ - $db = \DarkMatter_Domains::instance(); + $db = Domain::instance(); $_domain = $db->get( $primary_domain ); return $_domain; @@ -134,7 +133,7 @@ public function get_all() { return array(); } - $db = \DarkMatter_Domains::instance(); + $db = Domain::instance(); /** * Retrieve the DM_Domain objects for each of the primary domains. @@ -158,13 +157,13 @@ public function get_all() { * @return boolean True on success, false otherwise. */ public function set( $site_id = 0, $domain = '' ) { - $new_primary_domain = \DarkMatter_Domains::instance()->get( $domain ); + $new_primary_domain = Domain::instance()->get( $domain ); if ( $new_primary_domain->blog_id !== $site_id ) { return false; } - $result = \DarkMatter_Domains::instance()->update( + $result = Domain::instance()->update( $new_primary_domain->domain, true, $new_primary_domain->is_https, @@ -192,13 +191,13 @@ public function set( $site_id = 0, $domain = '' ) { * @return boolean True on success. False otherwise. */ public function unset( $site_id = 0, $domain = '', $db = false ) { - $new_primary_domain = \DarkMatter_Domains::instance()->get( $domain ); + $new_primary_domain = Domain::instance()->get( $domain ); if ( $new_primary_domain->blog_id !== $site_id ) { return false; } - $result = \DarkMatter_Domains::instance()->update( + $result = Domain::instance()->update( $new_primary_domain->domain, false, $new_primary_domain->is_https, diff --git a/includes/classes/DomainMapping/Manager/Restricted.php b/includes/classes/DomainMapping/Manager/Restricted.php index e40c19e2..576cf61d 100644 --- a/includes/classes/DomainMapping/Manager/Restricted.php +++ b/includes/classes/DomainMapping/Manager/Restricted.php @@ -71,7 +71,7 @@ private function _basic_checks( $fqdn ) { return new \WP_Error( 'wp-config', __( 'You cannot configure the WordPress Network primary domain.', 'dark-matter' ) ); } - $domains = \DarkMatter_Domains::instance(); + $domains = Domain::instance(); if ( $domains->is_exist( $fqdn ) ) { return new \WP_Error( 'used', __( 'This domain is in use.', 'dark-matter' ) ); } diff --git a/includes/classes/DomainMapping/Processor/Mapping.php b/includes/classes/DomainMapping/Processor/Mapping.php index f8e59e03..a7e5da12 100644 --- a/includes/classes/DomainMapping/Processor/Mapping.php +++ b/includes/classes/DomainMapping/Processor/Mapping.php @@ -9,6 +9,7 @@ namespace DarkMatter\DomainMapping\Processor; use DarkMatter\DomainMapping\Helper; +use DarkMatter\DomainMapping\Manager\Domain; use DarkMatter\DomainMapping\Manager\Primary; use DarkMatter\Interfaces\Registerable; @@ -153,7 +154,7 @@ public function is_external( $external = false, $host = '' ) { * Attempt to find the domain in Dark Matter. If the domain is found, then tell WordPress it is an internal * domain. */ - $db = \DarkMatter_Domains::instance(); + $db = Domain::instance(); $domain = $db->find( $host ); if ( is_a( $domain, 'DM_Domain' ) ) { diff --git a/includes/classes/DomainMapping/Processor/Media.php b/includes/classes/DomainMapping/Processor/Media.php index 7904bc0e..3b2f7078 100644 --- a/includes/classes/DomainMapping/Processor/Media.php +++ b/includes/classes/DomainMapping/Processor/Media.php @@ -9,6 +9,7 @@ namespace DarkMatter\DomainMapping\Processor; +use DarkMatter\DomainMapping\Manager\Domain; use DarkMatter\DomainMapping\Manager\Primary; /** @@ -348,7 +349,7 @@ private function prime_site( $site_id = 0 ) { /** * Ensure we have media domains to use. */ - $media_domains = \DarkMatter_Domains::instance()->get_domains_by_type( DM_DOMAIN_TYPE_MEDIA, $site_id ); + $media_domains = Domain::instance()->get_domains_by_type( DM_DOMAIN_TYPE_MEDIA, $site_id ); if ( empty( $media_domains ) ) { $this->sites[ $site_id ] = false; return; From 26c8dce925b06172cdf8212d85cc5a3a013a39bb Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:55:06 +0000 Subject: [PATCH 227/319] Updated sunrise.php to include the autoloader and use the new Domain manager. --- domain-mapping/inc/sunrise.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/domain-mapping/inc/sunrise.php b/domain-mapping/inc/sunrise.php index c9617c54..94787562 100644 --- a/domain-mapping/inc/sunrise.php +++ b/domain-mapping/inc/sunrise.php @@ -23,17 +23,18 @@ $dirname = str_replace( '/inc', '', dirname( __FILE__ ) ); /** - * Load the necessary parts of Dark Matter in to place. + * Include the PSR-4 autoloader. */ -require_once $dirname . '/api/class-darkmatter-domains.php'; - +if ( file_exists( $dirname . 'vendor/autoload.php' ) ) { + require_once $dirname . 'vendor/autoload.php'; +} /** * Attempt to find the Site. */ $fqdn = ( empty( $_SERVER['HTTP_HOST'] ) ? '' : $_SERVER['HTTP_HOST'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized global $dm_domain; -$dm_domain = DarkMatter_Domains::instance()->get( $fqdn ); +$dm_domain = \DarkMatter\DomainMapping\Manager\Domain::instance()->get( $fqdn ); if ( $dm_domain && $dm_domain->active ) { /** From d4270e5e30e09cc9b8955ae7e1d27f2c29a4498c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 15:57:08 +0000 Subject: [PATCH 228/319] Updated the unit tests to work with the new Domains manager. --- tests/phpunit/domain-mapping/DomainsTest.php | 44 +++++++++---------- .../domain-mapping/MappingDomainsTest.php | 4 +- .../domain-mapping/MediaDomainsTest.php | 10 ++--- .../domain-mapping/PrimaryDomainTest.php | 4 +- .../domain-mapping/RestrictedDomainsTest.php | 2 +- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/tests/phpunit/domain-mapping/DomainsTest.php b/tests/phpunit/domain-mapping/DomainsTest.php index 14e4f599..ba87960d 100644 --- a/tests/phpunit/domain-mapping/DomainsTest.php +++ b/tests/phpunit/domain-mapping/DomainsTest.php @@ -46,7 +46,7 @@ public function setUp() : void { */ public function test_add_domain() { $domain = 'mappeddomain1.test'; - DarkMatter_Domains::instance()->add( $domain ); + \DarkMatter\DomainMapping\Manager\Domain::instance()->add( $domain ); global $wpdb; $data = $wpdb->get_row( @@ -71,12 +71,12 @@ public function test_add_add_again() { /** * Add the domain. */ - DarkMatter_Domains::instance()->add( $domain ); + \DarkMatter\DomainMapping\Manager\Domain::instance()->add( $domain ); /** * Attempt to add the domain again. */ - $error = DarkMatter_Domains::instance()->add( $domain ); + $error = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( $domain ); $this->assertWPError( $error ); $this->assertSame( $error->get_error_code(), 'exists' ); @@ -93,12 +93,12 @@ public function test_delete_domain() { /** * Add the domain. */ - DarkMatter_Domains::instance()->add( $domain ); + \DarkMatter\DomainMapping\Manager\Domain::instance()->add( $domain ); /** * Attempt to add the domain again. */ - $return = DarkMatter_Domains::instance()->delete( $domain ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->delete( $domain ); $this->assertTrue( $return ); @@ -125,12 +125,12 @@ public function test_find_domain() { /** * Add the domain. */ - DarkMatter_Domains::instance()->add( $domain ); + \DarkMatter\DomainMapping\Manager\Domain::instance()->add( $domain ); /** * Attempt to add the domain again. */ - $return = DarkMatter_Domains::instance()->find( $domain ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->find( $domain ); $this->assertNotFalse( $return ); $this->assertEquals( $return->domain, $domain ); @@ -147,12 +147,12 @@ public function test_update_domain() { /** * Add the domain. */ - DarkMatter_Domains::instance()->add( $domain ); + \DarkMatter\DomainMapping\Manager\Domain::instance()->add( $domain ); /** * Make sure the update did not return a WP_Error. */ - $return = DarkMatter_Domains::instance()->update( $domain, true, true ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->update( $domain, true, true ); $this->assertNotWPError( $return ); /** @@ -178,12 +178,12 @@ public function test_update_domain() { */ public function test_validation_international_domains() { /** Chinese - Unicode - Invalid */ - $return = DarkMatter_Domains::instance()->add( 'www.例如.中国' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( 'www.例如.中国' ); $this->assertWPError( $return, 'Chinese - Unicode - Invalid' ); $this->assertSame( $return->get_error_code(), 'domain' ); /** Chinese - ASCII - Valid */ - $return = DarkMatter_Domains::instance()->add( 'www.xn--fsqu6v.xn--fiqs8s' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( 'www.xn--fsqu6v.xn--fiqs8s' ); $this->assertNotWPError( $return, 'Chinese - ASCII - Valid' ); } @@ -194,37 +194,37 @@ public function test_validation_international_domains() { */ public function test_validation_invalid_domains() { /** Empty */ - $return = DarkMatter_Domains::instance()->add( '' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( '' ); $this->assertWPError( $return ); $this->assertSame( $return->get_error_code(), 'empty' ); /** URI */ - $return = DarkMatter_Domains::instance()->add( 'http://example.com/' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( 'http://example.com/' ); $this->assertWPError( $return ); $this->assertSame( $return->get_error_code(), 'unsure' ); /** Domain + Path */ - $return = DarkMatter_Domains::instance()->add( 'example.com/hello-world' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( 'example.com/hello-world' ); $this->assertWPError( $return ); $this->assertSame( $return->get_error_code(), 'unsure' ); /** Domain + Port */ - $return = DarkMatter_Domains::instance()->add( 'example.com:443' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( 'example.com:443' ); $this->assertWPError( $return ); $this->assertSame( $return->get_error_code(), 'unsure' ); /** DOMAIN_CURRENT_SITE */ - $return = DarkMatter_Domains::instance()->add( 'darkmatter.test' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( 'darkmatter.test' ); $this->assertWPError( $return ); $this->assertSame( $return->get_error_code(), 'wp-config' ); /** Non-ASCII - i.e. emojis, etc. */ - $return = DarkMatter_Domains::instance()->add( '🐲' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( '🐲' ); $this->assertWPError( $return ); $this->assertSame( $return->get_error_code(), 'domain' ); /** Stored XSS (and curious input from an administrator one time ... ... ...) */ - $return = DarkMatter_Domains::instance()->add( '' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( '' ); $this->assertWPError( $return ); $this->assertSame( $return->get_error_code(), 'unsure' ); } @@ -241,19 +241,19 @@ public function test_validation_valid_domains() { */ /** Localhost */ - $return = DarkMatter_Domains::instance()->add( 'localhost' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( 'localhost' ); $this->assertNotWPError( $return ); /** Example domain */ - $return = DarkMatter_Domains::instance()->add( 'example.com' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( 'example.com' ); $this->assertNotWPError( $return ); /** Example sub-domain */ - $return = DarkMatter_Domains::instance()->add( 'www.example.com' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( 'www.example.com' ); $this->assertNotWPError( $return ); /** Atypical test domain. */ - $return = DarkMatter_Domains::instance()->add( 'development.test' ); + $return = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( 'development.test' ); $this->assertNotWPError( $return ); } } diff --git a/tests/phpunit/domain-mapping/MappingDomainsTest.php b/tests/phpunit/domain-mapping/MappingDomainsTest.php index c7cfd2d9..ca85af62 100644 --- a/tests/phpunit/domain-mapping/MappingDomainsTest.php +++ b/tests/phpunit/domain-mapping/MappingDomainsTest.php @@ -64,7 +64,7 @@ public function setUp() : void { ] ); - DarkMatter_Domains::instance()->network_media = [ + \DarkMatter\DomainMapping\Manager\Domain::instance()->network_media = [ $this->media_domain, ]; @@ -73,7 +73,7 @@ public function setUp() : void { /** * Add domains to the new site. */ - DarkMatter_Domains::instance()->add( + \DarkMatter\DomainMapping\Manager\Domain::instance()->add( $this->primary_domain, true, true diff --git a/tests/phpunit/domain-mapping/MediaDomainsTest.php b/tests/phpunit/domain-mapping/MediaDomainsTest.php index f9510302..02b63633 100644 --- a/tests/phpunit/domain-mapping/MediaDomainsTest.php +++ b/tests/phpunit/domain-mapping/MediaDomainsTest.php @@ -47,9 +47,9 @@ public function test_get_media_domains_constant() { 'cdn1.darkmatter.test', ]; - DarkMatter_Domains::instance()->network_media = $constant_domains; + \DarkMatter\DomainMapping\Manager\Domain::instance()->network_media = $constant_domains; - $domains = DarkMatter_Domains::instance()->get_domains_by_type(); + $domains = \DarkMatter\DomainMapping\Manager\Domain::instance()->get_domains_by_type(); $expected = []; @@ -79,7 +79,7 @@ public function test_get_media_domains_manual() { /** * Reset any hard-coded media domains. */ - DarkMatter_Domains::instance()->network_media = []; + \DarkMatter\DomainMapping\Manager\Domain::instance()->network_media = []; $media_domains = [ 'cdn1.mappeddomain1.test' => -1, @@ -93,7 +93,7 @@ public function test_get_media_domains_manual() { * Create domains. */ foreach ( $media_domains as $domain => $id ) { - $result = DarkMatter_Domains::instance()->add( + $result = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( $domain, false, true, @@ -109,7 +109,7 @@ public function test_get_media_domains_manual() { /** * Retrieve the domains. */ - $domains = DarkMatter_Domains::instance()->get_domains_by_type(); + $domains = \DarkMatter\DomainMapping\Manager\Domain::instance()->get_domains_by_type(); $expected = []; diff --git a/tests/phpunit/domain-mapping/PrimaryDomainTest.php b/tests/phpunit/domain-mapping/PrimaryDomainTest.php index c0eefa15..2eb8578b 100644 --- a/tests/phpunit/domain-mapping/PrimaryDomainTest.php +++ b/tests/phpunit/domain-mapping/PrimaryDomainTest.php @@ -19,7 +19,7 @@ class PrimaryDomainTest extends \WP_UnitTestCase { /** * Holder for Domains class. * - * @var DarkMatter_Domains|null + * @var \DarkMatter\DomainMapping\Manager\Domain|null */ private $darkmatter_domains = null; @@ -67,7 +67,7 @@ public static function wpTearDownAfterClass() { public function setUp(): void { parent::setUp(); - $this->darkmatter_domains = DarkMatter_Domains::instance(); + $this->darkmatter_domains = \DarkMatter\DomainMapping\Manager\Domain::instance(); $this->darkmatter_primary = \DarkMatter\DomainMapping\Manager\Primary::instance(); switch_to_blog( self::$blog_id ); diff --git a/tests/phpunit/domain-mapping/RestrictedDomainsTest.php b/tests/phpunit/domain-mapping/RestrictedDomainsTest.php index 459790a6..a7911165 100644 --- a/tests/phpunit/domain-mapping/RestrictedDomainsTest.php +++ b/tests/phpunit/domain-mapping/RestrictedDomainsTest.php @@ -89,7 +89,7 @@ public function test_restrict_domain_add_domain() { */ switch_to_blog( $this->blog_id ); - $result = DarkMatter_Domains::instance()->add( $domain ); + $result = \DarkMatter\DomainMapping\Manager\Domain::instance()->add( $domain ); $this->assertWPError( $result, 'WP_Error for adding a restricted domain.' ); $this->assertSame( 'reserved', $result->get_error_code(), 'Correct WP_Error for restricted domain.' ); } From 5df614495d564b516ffa6bfbe4f57982b0c988f0 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 16:01:49 +0000 Subject: [PATCH 229/319] Moved the Yoast compat into its new home. --- .../classes/DomainMapping/Support/Yoast.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename domain-mapping/classes/third-party/class-dm-yoast.php => includes/classes/DomainMapping/Support/Yoast.php (100%) diff --git a/domain-mapping/classes/third-party/class-dm-yoast.php b/includes/classes/DomainMapping/Support/Yoast.php similarity index 100% rename from domain-mapping/classes/third-party/class-dm-yoast.php rename to includes/classes/DomainMapping/Support/Yoast.php From 2615bf88c08f0e1370254874d84750d892ca4df8 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 16:06:36 +0000 Subject: [PATCH 230/319] Updated Yoast to its new namespace home. --- includes/classes/DarkMatter.php | 4 ++ .../classes/DomainMapping/Support/Yoast.php | 42 ++++++++++--------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/includes/classes/DarkMatter.php b/includes/classes/DarkMatter.php index 54572c31..d0c890c2 100644 --- a/includes/classes/DarkMatter.php +++ b/includes/classes/DarkMatter.php @@ -87,6 +87,10 @@ public function register_domainmapping() { $domainmapping_classes[] = DomainMapping\Admin\DomainSettings::class; } + if ( defined( 'WPSEO_VERSION' ) ) { + $domainmapping_classes = DomainMapping\Support\Yoast::class; + } + $this->class_register( $domainmapping_classes ); if ( defined( 'WP_CLI' ) && WP_CLI ) { diff --git a/includes/classes/DomainMapping/Support/Yoast.php b/includes/classes/DomainMapping/Support/Yoast.php index 4887b780..9681bb53 100644 --- a/includes/classes/DomainMapping/Support/Yoast.php +++ b/includes/classes/DomainMapping/Support/Yoast.php @@ -2,32 +2,32 @@ /** * Compatibility adjustments for supporting Yoast SEO. * + * @since 2.1.3 + * * @package DarkMatter */ +namespace DarkMatter\DomainMapping\Support; + +use DarkMatter\DomainMapping\Helper; +use DarkMatter\Interfaces\Registerable; + /** - * Class DM_Yoast + * Class Yoast + * + * Previously called `DM_Yoast`. * * @since 2.1.3 */ -class DM_Yoast { - /** - * DM_Yoast constructor. - */ - public function __construct() { - if ( defined( 'WPSEO_VERSION' ) ) { - return; - } - - add_filter( 'wpseo_should_save_indexable', [ $this, 'fix_indexable_permalinks' ], 10, 2 ); - } - +class Yoast implements Registerable { /** * Correct indexables permalinks to be unmapped prior to save to the database. This works with versions 15.1+ of * Yoast SEO. Version 15.1 - which contains the `wpseo_should_save_indexable` was released on 14th October 2020. * * @link https://github.com/Yoast/wordpress-seo/blob/15.1/src/builders/indexable-builder.php#L296 * + * @since 2.1.3 + * * @param boolean $intend_to_save Whether the indexable is to be saved or not. * @param \Yoast\WP\SEO\Models\Indexable $indexable The indexable to be saved. * @return boolean The default value of "intend to save". @@ -37,14 +37,18 @@ public function fix_indexable_permalinks( $intend_to_save, $indexable ) { * If saving to the database, then make sure the permalink is unmapped. */ if ( $intend_to_save ) { - $indexable->permalink = \DarkMatter\DomainMapping\Helper::instance()->unmap( $indexable->permalink ); + $indexable->permalink = Helper::instance()->unmap( $indexable->permalink ); } return $intend_to_save; } -} -/** - * Only instantiate this class if Yoast SEO is in use. - */ -new DM_Yoast(); + /** + * Register hooks for this class. + * + * @return void + */ + public function register() { + add_filter( 'wpseo_should_save_indexable', [ $this, 'fix_indexable_permalinks' ], 10, 2 ); + } +} From d171c39d0da59109374f17153af7e80ac26a358b Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 16:10:35 +0000 Subject: [PATCH 231/319] Moved sunrise.php to its own file. --- {domain-mapping => includes/dropins}/sunrise.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {domain-mapping => includes/dropins}/sunrise.php (100%) diff --git a/domain-mapping/sunrise.php b/includes/dropins/sunrise.php similarity index 100% rename from domain-mapping/sunrise.php rename to includes/dropins/sunrise.php From caff8ada4de14483fff952a700db9ad5824992ad Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 16:11:19 +0000 Subject: [PATCH 232/319] Removed the old Yoast file. --- domain-mapping/domain-mapping.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/domain-mapping/domain-mapping.php b/domain-mapping/domain-mapping.php index fd31deed..f4f571f2 100644 --- a/domain-mapping/domain-mapping.php +++ b/domain-mapping/domain-mapping.php @@ -19,11 +19,6 @@ require_once DM_PATH . '/domain-mapping/classes/class-dm-healthchecks.php'; -/** - * Plugin compatibility. - */ -require_once DM_PATH . '/domain-mapping/classes/third-party/class-dm-yoast.php'; - /** * Disable SSO if the COOKIE_DOMAIN constant is set. */ From 9ca06f67d6bbd759c173f09a7217790aa235c5b1 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 16:15:39 +0000 Subject: [PATCH 233/319] Created a new class for handling Sunrise logic. --- includes/classes/DomainMapping/Sunrise.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 includes/classes/DomainMapping/Sunrise.php diff --git a/includes/classes/DomainMapping/Sunrise.php b/includes/classes/DomainMapping/Sunrise.php new file mode 100644 index 00000000..7ca23a8b --- /dev/null +++ b/includes/classes/DomainMapping/Sunrise.php @@ -0,0 +1,22 @@ + Date: Sun, 30 Oct 2022 16:21:11 +0000 Subject: [PATCH 234/319] Added a new helper method for getting the FDQN. --- includes/classes/DomainMapping/Helper.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/includes/classes/DomainMapping/Helper.php b/includes/classes/DomainMapping/Helper.php index 5dde98bf..3cde0e5e 100644 --- a/includes/classes/DomainMapping/Helper.php +++ b/includes/classes/DomainMapping/Helper.php @@ -15,6 +15,17 @@ * @since 3.0.0 */ class Helper { + /** + * Retrieves the FQDN as request from the URL. + * + * @since 3.0.0 + * + * @return string + */ + public function get_request_fqdn() { + return ( empty( $_SERVER['HTTP_HOST'] ) ? '' : $_SERVER['HTTP_HOST'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + } + /** * Map the primary domain on the passed in value if it contains the unmapped URL and the Site has a primary domain. * From 800245d04bd0aef92023df1e681d9dd8a74bc6f8 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 16:22:31 +0000 Subject: [PATCH 235/319] Added the logic for define global cache group and a few constants. --- includes/classes/DomainMapping/Sunrise.php | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/includes/classes/DomainMapping/Sunrise.php b/includes/classes/DomainMapping/Sunrise.php index 7ca23a8b..e8568fb8 100644 --- a/includes/classes/DomainMapping/Sunrise.php +++ b/includes/classes/DomainMapping/Sunrise.php @@ -17,6 +17,32 @@ class Sunrise { * @since 3.0.0 */ public function __construct() { + $this->init(); + } + + /** + * Set up various globals and constants which are used, either by Dark Matter itself or by other plugins and/or + * WordPress Core. + * + * @return void + */ + private function init() { + /** + * Ensure `dark-matter` cache group is available globally. + */ + wp_cache_add_global_groups( 'dark-matter' ); + + /** + * Define the `SUNRISE_LOADED`. Dark Matter Plugin does not make use of this but other plugins do in certain + * circumstances (i.e. Jetpack). So we define it for compatibility. + */ + if ( false === defined( 'SUNRISE_LOADED' ) ) { + define( 'SUNRISE_LOADED', true ); + } + /** + * Use to detect misconfiguration where the cookie domain is set. + */ + define( 'DARKMATTER_COOKIE_SET', ! defined( 'COOKIE_DOMAIN' ) ); } } From 9870fffd1962afa7f8cf401509ef54f987cb96a3 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 16:37:05 +0000 Subject: [PATCH 236/319] Added the logic to set the global variables. --- includes/classes/DomainMapping/Sunrise.php | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/includes/classes/DomainMapping/Sunrise.php b/includes/classes/DomainMapping/Sunrise.php index e8568fb8..c7d6822a 100644 --- a/includes/classes/DomainMapping/Sunrise.php +++ b/includes/classes/DomainMapping/Sunrise.php @@ -1,10 +1,19 @@ init(); + + /** + * Find the domain based on the request. + */ + $domain = $this->get_domain(); + if ( $domain && $domain->active && $this->set_globals( $domain ) ) { + + } + } + + /** + * Get the domain from the requested FQDN. + * + * @since 3.0.0 + * + * @return bool|Data\Domain + */ + private function get_domain() { + $fqdn = Helper::instance()->get_request_fqdn(); + return Manager\Domain::instance()->get( $fqdn ); } /** * Set up various globals and constants which are used, either by Dark Matter itself or by other plugins and/or * WordPress Core. * + * @since 3.0.0 + * * @return void */ private function init() { @@ -45,4 +76,47 @@ private function init() { */ define( 'DARKMATTER_COOKIE_SET', ! defined( 'COOKIE_DOMAIN' ) ); } + + /** + * Sets up a few global variables which are used through WordPress Core. Will do some basic detection to ensure the + * website should be served on the mapped domain. + * + * @since 3.0.0 + * + * @param Data\Domain $domain Domain data. + * @return bool True to proceed, false to disengage. + */ + private function set_globals( $domain ) { + global $blog_id, $current_blog, $current_site, $site_id; + + /** + * Set the current Blog (WP_Site) and current Site (WP_Network). + */ + $current_blog = get_site( $domain->blog_id ); + $blog_id = $current_blog->blog_id; + $current_site = \WP_Network::get_instance( $current_blog->site_id ); + $site_id = $current_blog->site_id; + + /** + * Make sure the current blog is public. + */ + if ( + /** + * Check the current blog is public and be compatible with plugins such as Restricted Site Access. + */ + 0 > (int) $current_blog->public + /** + * Check the current blog has not been archived. + */ + && 0 !== (int) $current_blog->archived + /** + * Check the current blog has not been "soft" deleted. + */ + && 0 !== (int) $current_blog->deleted + ) { + return false; + } + + return true; + } } From cfa156f5256d07a4ee0d49270f631826b155072c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 16:45:34 +0000 Subject: [PATCH 237/319] Added a method to update the global variables if a primary domain is detect. --- includes/classes/DomainMapping/Sunrise.php | 38 ++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/includes/classes/DomainMapping/Sunrise.php b/includes/classes/DomainMapping/Sunrise.php index c7d6822a..81cff8ba 100644 --- a/includes/classes/DomainMapping/Sunrise.php +++ b/includes/classes/DomainMapping/Sunrise.php @@ -32,8 +32,8 @@ public function __construct() { * Find the domain based on the request. */ $domain = $this->get_domain(); - if ( $domain && $domain->active && $this->set_globals( $domain ) ) { - + if ( $domain && $domain->active && $this->set_globals( $domain ) && $domain->is_primary ) { + $this->update_globals( $domain ); } } @@ -119,4 +119,38 @@ private function set_globals( $domain ) { return true; } + + /** + * Prepare WordPress globals to use the primary domain data instead. + * + * @since 3.0.0 + * + * @param Data\Domain $primary Primary Domain data. + * @return void + */ + private function update_globals( $primary ) { + /** + * Store the current WP_Site object in another global, so it is available for comparison and reference. + */ + global $current_blog, $original_blog; + $original_blog = clone $current_blog; + + /** + * Update the domain and path to match the primary. + */ + $current_blog->domain = $primary->domain; + $current_blog->path = '/'; + + /** + * Update the cookie domain to be the primary domain. + */ + if ( ! defined( 'COOKIE_DOMAIN' ) ) { + define( 'COOKIE_DOMAIN', $primary->domain ); + } + + /** + * Set the constant to state the current request has been mapped. + */ + define( 'DOMAIN_MAPPING', true ); + } } From 38fbca9a42a8ba7ffd668d9ba627fad7df260b4c Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 16:48:23 +0000 Subject: [PATCH 238/319] Updated the sunrise.php to use the new autoloader. --- includes/dropins/sunrise.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/includes/dropins/sunrise.php b/includes/dropins/sunrise.php index 64cefb5b..fb478f46 100644 --- a/includes/dropins/sunrise.php +++ b/includes/dropins/sunrise.php @@ -14,8 +14,13 @@ * @package DarkMatter */ -$sunrise_path = ( dirname( __FILE__ ) . '/plugins/dark-matter/domain-mapping/inc/sunrise.php' ); +$dirname = ( dirname( __FILE__ ) . '/plugins/dark-matter/' ); -if ( is_readable( $sunrise_path ) ) { - require_once $sunrise_path; +/** + * Include the PSR-4 autoloader. + */ +if ( file_exists( $dirname . 'vendor/autoload.php' ) ) { + require_once $dirname . 'vendor/autoload.php'; } + +new \DarkMatter\DomainMapping\Sunrise(); From f0b1f8cda8828a48c5227220becd13178da56f6e Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 16:49:07 +0000 Subject: [PATCH 239/319] Removed the old sunrise.php dropin. --- domain-mapping/inc/sunrise.php | 106 --------------------------------- 1 file changed, 106 deletions(-) delete mode 100644 domain-mapping/inc/sunrise.php diff --git a/domain-mapping/inc/sunrise.php b/domain-mapping/inc/sunrise.php deleted file mode 100644 index 94787562..00000000 --- a/domain-mapping/inc/sunrise.php +++ /dev/null @@ -1,106 +0,0 @@ -get( $fqdn ); - -if ( $dm_domain && $dm_domain->active ) { - /** - * Prepare all the global variables. This is require irrespective of whether - * it is a primary or secondary domain. - */ - global $current_blog, $original_blog; - $current_blog = get_site( $dm_domain->blog_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited - - global $current_site; - $current_site = WP_Network::get_instance( $current_blog->site_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited - - // @codingStandardsIgnoreStart - global $blog_id; - $blog_id = $current_blog->blog_id; - global $site_id; - $site_id = $current_blog->site_id; - // @codingStandardsIgnoreEnd - - /** - * Dark Matter will disengage if the website is no longer public or is - * archived or deleted. - */ - if ( (int) $current_blog->public < 0 || '0' !== $current_blog->archived || '0' !== $current_blog->deleted ) { - return; - } - - /** - * If the primary domain, then update the WP_Site properties to match the - * mapped domain and not the admin domain. - */ - if ( $dm_domain->is_primary ) { - $original_blog = clone $current_blog; - - $current_blog->domain = $dm_domain->domain; - $current_blog->path = '/'; - - /** - * Load and prepare the WordPress Network. - */ - global $current_site; // phpcs:ignore WordPressVIPMinimum.Variables.VariableAnalysis.VariableRedeclaration - $current_site = WP_Network::get_instance( $current_blog->site_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited - - if ( ! defined( 'COOKIE_DOMAIN' ) ) { - define( 'COOKIE_DOMAIN', $dm_domain->domain ); - } - - define( 'DOMAIN_MAPPING', true ); - - if ( empty( $current_site->blog_id ) ) { - $current_site->blog_id = get_main_site_id( $current_site->id ); - } - - /** - * Set the other necessary globals to ensure WordPress functions correctly. - */ - // @codingStandardsIgnoreStart - global $blog_id; - $blog_id = $current_blog->blog_id; - global $site_id; - $site_id = $current_blog->site_id; - // @codingStandardsIgnoreEnd - } -} - -/** - * Determine if we should perform a redirect. - */ -require_once $dirname . '/inc/redirect.php'; From e2c99997d404e369cbea7d937fd5a819e8f52b12 Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 16:55:35 +0000 Subject: [PATCH 240/319] Created new class to house the Redirect. --- .../DomainMapping/Processor/Redirect.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 includes/classes/DomainMapping/Processor/Redirect.php diff --git a/includes/classes/DomainMapping/Processor/Redirect.php b/includes/classes/DomainMapping/Processor/Redirect.php new file mode 100644 index 00000000..9784d0fe --- /dev/null +++ b/includes/classes/DomainMapping/Processor/Redirect.php @@ -0,0 +1,29 @@ + Date: Sun, 30 Oct 2022 16:57:52 +0000 Subject: [PATCH 241/319] Added a stub for the can_redirect method. --- .../classes/DomainMapping/Processor/Redirect.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/includes/classes/DomainMapping/Processor/Redirect.php b/includes/classes/DomainMapping/Processor/Redirect.php index 9784d0fe..8adb0ee1 100644 --- a/includes/classes/DomainMapping/Processor/Redirect.php +++ b/includes/classes/DomainMapping/Processor/Redirect.php @@ -17,6 +17,17 @@ * @since 3.0.0 */ class Redirect implements Registerable { + /** + * Determine if the current request is something we consider for redirecting. + * + * @since 3.0.0 + * + * @return bool True to redirect, false otherwise. + */ + private function can_redirect() { + return false; + } + /** * Register hooks for this class. * @@ -25,5 +36,8 @@ class Redirect implements Registerable { * @return void */ public function register() { + if ( $this->can_redirect() ) { + // todo add_action( 'muplugins_loaded', 'darkmatter_maybe_redirect', 20 ); + } } } From 218aeb28f1b50f21f59152cb4e514806af0bea0a Mon Sep 17 00:00:00 2001 From: Cameron Terry Date: Sun, 30 Oct 2022 17:00:44 +0000 Subject: [PATCH 242/319] Added the logic for the can_redirect() method. --- .../DomainMapping/Processor/Redirect.php | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/includes/classes/DomainMapping/Processor/Redirect.php b/includes/classes/DomainMapping/Processor/Redirect.php index 8adb0ee1..f978034b 100644 --- a/includes/classes/DomainMapping/Processor/Redirect.php +++ b/includes/classes/DomainMapping/Processor/Redirect.php @@ -25,7 +25,53 @@ class Redirect implements Registerable { * @return bool True to redirect, false otherwise. */ private function can_redirect() { - return false; + /** + * The function `rest_get_url_prefix()` is not available at this point in the load process. Therefore, we must + * substitute it with a close approximate of what the function does. + * + * There is a side effect of this. Basically if a site is to be setup with a different prefix, in order for this + * to work, the `add_filter()` call would need to be done in a Must-Use Plugin. + * + * @see https://developer.wordpress.org/reference/hooks/rest_url_prefix/ + */ + $rest_url_prefix = '/' . trim( apply_filters( 'rest_url_prefix', 'wp-json' ), '/' ) . '/'; + + return ! ( + /** + * Do not attempt to redirect during the CLI command. + */ + ( defined( 'WP_CLI' ) && WP_CLI ) + || + /** + * AJAX requests can be used on both the mapped and unmapped domains. + */ + ( defined( 'DOING_AJAX' ) && DOING_AJAX ) + || + /** + * Do not attempt to redirect during the execution of cron. + */ + ( defined( 'DOING_CRON' ) && DOING_CRON ) + || + /** + * XMLRPC Requests can be used on both the mapped and unmapped domains. + */ + ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) + || + /** + * REST API can be used on both the mapped and unmapped domains. + */ + ( ! empty( $_SERVER['REQUEST_URI'] ) && false !== strpos( $_SERVER['REQUEST_URI'], $rest_url_prefix ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + || + /** + * Customizer is presented in an