@@ -13,10 +13,11 @@ class CommonToolkit {
1313
1414 private static $ instance ;
1515 private static $ version = '0.8.0 ' ;
16+ private static $ cache = [ 'key ' => 'config_registry ' , 'group ' => 'common_toolkit ' ];
1617 protected static $ config = [];
1718 protected static $ environment = [];
1819
19- public static function init () {
20+ final public static function init () {
2021
2122 if ( !isset ( self ::$ instance ) && !( self ::$ instance instanceof CommonToolkit ) ) {
2223
@@ -25,12 +26,14 @@ public static function init() {
2526 // Define version constant
2627 if ( !defined ( __CLASS__ . '\VERSION ' ) ) define ( __CLASS__ . '\VERSION ' , self ::$ version );
2728
28- // Get configuration variables
29+ // Get configuration registry
2930 if ( defined ( 'CTK_CONFIG ' ) ) {
3031 if ( is_array ( CTK_CONFIG ) ) {
3132 self ::$ config ['common_toolkit ' ] = CTK_CONFIG ;
32- } else if ( is_string ( CTK_CONFIG ) && file_exists ( realpath ( ABSPATH . CTK_CONFIG ) ) ) {
33- self ::$ config = @json_decode ( file_get_contents ( realpath ( ABSPATH . CTK_CONFIG ) ), true ) ?: [];
33+ } else if ( is_string ( CTK_CONFIG ) ) {
34+ self ::$ config = self ::get_cache_object ( $ cache [ 'key ' ], function () {
35+ return @json_decode ( file_get_contents ( realpath ( ABSPATH . CTK_CONFIG ) ), true ) ?: [];
36+ });
3437 }
3538 }
3639
@@ -51,8 +54,8 @@ public static function init() {
5154 'shortcodes ' => false ,
5255 'windows_live_writer ' => true
5356 ], self ::$ config ['common_toolkit ' ] );
54-
55- // Define environment variable
57+
58+ // Define environment
5659 switch ( true ) {
5760 case defined ( self ::get_config ( 'common_toolkit/environment_constant ' ) ):
5861 self ::$ config ['common_toolkit ' ]['environment ' ] = constant ( self ::get_config ( 'common_toolkit/environment_constant ' ) );
@@ -152,7 +155,7 @@ public static function init() {
152155
153156 }
154157
155- /*
158+ /**
156159 * Get configuration variable.
157160 * Example usage: echo apply_filter( 'ctk_config', 'common_toolkit/meta_generator' );
158161 *
@@ -219,7 +222,7 @@ public static function ctk_environment_filter( $key = null ) {
219222
220223 }
221224
222- /*
225+ /**
223226 * Remove Emoji code in page header.
224227 * Usage: define( 'CTK_CONFIG', [ 'disable_emojis' => true ] );
225228 *
@@ -241,7 +244,7 @@ public function disable_emojis() {
241244
242245 }
243246
244- /*
247+ /**
245248 * Remove WordPress core, plugin and/or theme update notices
246249 * Usage: define( 'CTK_CONFIG', [ 'disable_updates' => [ 'core', 'plugin', 'theme' ] ] );
247250 *
@@ -254,7 +257,7 @@ public function disable_updates() {
254257
255258 }
256259
257- /*
260+ /**
258261 * Disables WordPress site search and return 404
259262 *
260263 * @since 0.8.0
@@ -273,7 +276,7 @@ public function disable_search( $query, $error = true ) {
273276
274277 }
275278
276- /*
279+ /**
277280 * Set a different admin bar color color. Useful for differentiating among environnments.
278281 * Usage: define( 'CTK_CONFIG', [ 'admin_bar_color' => '#336699' ] );
279282 *
@@ -285,7 +288,7 @@ public function change_admin_bar_color() {
285288
286289 }
287290
288- /*
291+ /**
289292 * Quick defer or async loading of scripts via wp_enqueue_script(). Supports other custom attributes.
290293 * Usage: wp_enqueue_script( 'script-handle-async-example', get_template_directory_uri() . '/assets/js/script.js#async' );
291294 * wp_enqueue_script( 'script-handle-defer-example', get_template_directory_uri() . '/assets/js/script.js#defer' );
@@ -341,7 +344,7 @@ public function defer_async_scripts( $tag, $handle, $src ) {
341344
342345 }
343346
344- /*
347+ /**
345348 * Build URL from array created with parse_url()
346349 * Usage: $parse_uri = parse_url( 'https://example.com/?hello=world#hash );
347350 * $uri = \MU_Plugins\CommonToolkit::build_url( $parse_uri );
@@ -398,7 +401,7 @@ public static function set_default_atts( $pairs, $atts ) {
398401
399402 }
400403
401- /*
404+ /**
402405 * Modify the WordPress heartbeat
403406 *
404407 * @since 0.8.0
@@ -414,7 +417,7 @@ public function modify_heartbeat( $settings ) {
414417
415418 }
416419
417- /*
420+ /**
418421 * Remove or modify meta generator tag.
419422 *
420423 * @since 0.7.0
@@ -437,7 +440,7 @@ public function modify_meta_generator_tags( $current, $type ) {
437440
438441 }
439442
440- /*
443+ /**
441444 * Output a formatted date in WordPress configured timezone. Defaults to curreent
442445 * date/time in format configured in WP Admin.
443446 * Usage: Current date/time: [get_datetime]
@@ -455,7 +458,7 @@ public function shortcode_get_datetime( $atts ) {
455458 return current_time ( $ atts ['format ' ] );
456459 }
457460
458- /*
461+ /**
459462 * Remove extra HTML tags added by DomDocument
460463 *
461464 * @since 0.7.0
@@ -468,7 +471,48 @@ private function strip_extra_dom_elements( $element ) {
468471
469472 }
470473
471- /*
474+ /**
475+ * Get/set cache object
476+ *
477+ * @param string $key The name of the cache key to set/retrieve
478+ * @param function $callback The callback function that return the uncached value
479+ * @return mixed
480+ * @since 0.8.0
481+ */
482+ public function get_cache_object ( $ key , $ callback ) {
483+
484+ $ cache_expire = defined ( 'CTK_CACHE_EXPIRE ' ) && is_int ( CTK_CACHE_EXPIRE ) ? intval ( CTK_CACHE_EXPIRE ) : false ;
485+ if ( !is_int ( $ cache_expire ) ) return $ callback ();
486+
487+ $ result = unserialize ( wp_cache_get ( $ key , $ cache [ 'group ' ], false , $ cache_hit ) );
488+
489+ if ( !$ cache_hit ) {
490+
491+ $ result = $ callback ();
492+ wp_cache_set ( $ key , serialize ( $ result ), $ cache [ 'group ' ], $ cache_expire );
493+
494+ } else {
495+
496+ if ( is_string ( $ result ) && is_numeric ( $ result ) ) $ result = intval ( $ result ) ? (int ) $ result : (float ) $ result ;
497+
498+ }
499+
500+ return $ result ;
501+
502+ }
503+
504+ /**
505+ * Flush the config registry cache
506+ *
507+ * @since 0.8.0
508+ */
509+ public function delete_config_cache () {
510+
511+ wp_cache_delete ( $ cache [ 'group ' ], $ cache [ 'group ' ] );
512+
513+ }
514+
515+ /**
472516 * Magic method to return config as JSON string.
473517 *
474518 * @since 0.7.0
0 commit comments