From efb97358716e25fae208fa349a7d84200cd8e050 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 1 Oct 2024 18:57:51 +0200 Subject: [PATCH 1/3] Cache: Warmup does not exit if an shortcode call has an exception --- src/Service/Cache.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Service/Cache.php b/src/Service/Cache.php index 0c6004ff2..0c492fa74 100644 --- a/src/Service/Cache.php +++ b/src/Service/Cache.php @@ -374,19 +374,26 @@ public static function renderClearCacheButton( $field_args, $field ) { } /** - * Iterates through array and executes shortcodecalls. - * @param $shortCodeCalls + * Iterates through array and statically executes given functions. + * + * @param string[] $shortCodeCalls array of tuples of shortcode name strings and tuples of class + static function. * * @return void */ - private static function runShortcodeCalls($shortCodeCalls) { + private static function runShortcodeCalls( array $shortCodeCalls ): void { foreach($shortCodeCalls as $shortcode) { $shortcodeFunction = array_keys($shortcode)[0]; $attributes = $shortcode[$shortcodeFunction]; if(array_key_exists($shortcodeFunction, self::$cbShortCodeFunctions)) { list($class, $function) = self::$cbShortCodeFunctions[$shortcodeFunction]; - $class::$function($attributes); + + try { + $class::$function( $attributes ); + } catch ( Exception $e ) { + // Writes error to log anyway + error_log( (string) $e ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log + } } } } From 75577011ee804d0c9d8c9d59bdd35333c72661f1 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 2 Oct 2024 10:46:05 +0200 Subject: [PATCH 2/3] Cache: Prevents "non-static calling"-exception in warmup phase. * refactors BaseShortcode execute to be static --- src/Map/BaseShortcode.php | 10 +++++----- src/Plugin.php | 2 +- src/Wordpress/CustomPostType/Map.php | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Map/BaseShortcode.php b/src/Map/BaseShortcode.php index dfcb9f960..8ecb29730 100644 --- a/src/Map/BaseShortcode.php +++ b/src/Map/BaseShortcode.php @@ -6,8 +6,9 @@ abstract class BaseShortcode { /** * the shortcode handler - load all the needed assets and render the map container **/ - public function execute($atts, $content): string { - $attrs = $this->parse_attributes($atts); + public static function execute( array $atts, string $content ): string { + $instance = new static(); + $attrs = $instance->parse_attributes($atts); $options = array_filter($atts, "is_int", ARRAY_FILTER_USE_KEY); if (! (int) $attrs['id']) { @@ -25,10 +26,9 @@ public function execute($atts, $content): string { } $cb_map_id = $post->ID; - $this->inject_script($cb_map_id); - return $this->create_container($cb_map_id, $attrs, $options, $content); + $instance->inject_script($cb_map_id); + return $instance->create_container($cb_map_id, $attrs, $options, $content); } - abstract protected function parse_attributes($atts); abstract protected function inject_script($cb_map_id); abstract protected function create_container($cb_map_id, $attrs, $options, $content); diff --git a/src/Plugin.php b/src/Plugin.php index 4e4f4700c..9055b64a0 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -652,7 +652,7 @@ public static function registerScriptsAndStyles() { } public function registerShortcodes() { - add_shortcode('cb_search', array(new SearchShortcode(), 'execute')); + add_shortcode( 'cb_search', array( SearchShortcode::class, 'execute' ) ); } /** diff --git a/src/Wordpress/CustomPostType/Map.php b/src/Wordpress/CustomPostType/Map.php index 5b00b2457..3903c6739 100644 --- a/src/Wordpress/CustomPostType/Map.php +++ b/src/Wordpress/CustomPostType/Map.php @@ -35,7 +35,7 @@ public function initHooks() { } // Add shortcodes - add_shortcode( 'cb_map', array( new MapShortcode(), 'execute' ) ); + add_shortcode( 'cb_map', array( MapShortcode::class, 'execute' ) ); // Add actions add_action( 'save_post_' . self::$postType, array( MapAdmin::class, 'validate_options' ), 10, 3 ); From 2b6bfe83947e64fedf1a57d45dbe6cf3ac12bdb8 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 2 Oct 2024 10:48:16 +0200 Subject: [PATCH 3/3] Map: Adds doc-strings --- src/Map/BaseShortcode.php | 11 ++++++++++- src/Map/MapShortcode.php | 3 +++ src/Map/SearchShortcode.php | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Map/BaseShortcode.php b/src/Map/BaseShortcode.php index 8ecb29730..21fa54817 100644 --- a/src/Map/BaseShortcode.php +++ b/src/Map/BaseShortcode.php @@ -2,9 +2,18 @@ namespace CommonsBooking\Map; +/** + * Map shortcode base implementation. + * Derive from this class to create custom map shortcode. + * Examples of how to implement the abstract methods called in `execute` are {@see MapShortcode} and {@see SearchShortcode}. + */ abstract class BaseShortcode { + /** - * the shortcode handler - load all the needed assets and render the map container + * The shortcode handler - load all the needed assets and render the map container + * + * @param array $atts attributes for parametrization. + * @param string $content content to display, if shortcode implementation allows to. **/ public static function execute( array $atts, string $content ): string { $instance = new static(); diff --git a/src/Map/MapShortcode.php b/src/Map/MapShortcode.php index 7d70df44e..aff758b6b 100644 --- a/src/Map/MapShortcode.php +++ b/src/Map/MapShortcode.php @@ -2,6 +2,9 @@ namespace CommonsBooking\Map; +/** + * Shortcode for the legacy map with the old non-responsive standard leaflet style. + */ class MapShortcode extends BaseShortcode { protected function create_container($cb_map_id, $attrs, $options, $content) { $map_height = MapAdmin::get_option( $cb_map_id, 'map_height' ); diff --git a/src/Map/SearchShortcode.php b/src/Map/SearchShortcode.php index 8eba5c246..ec9a0cfb1 100644 --- a/src/Map/SearchShortcode.php +++ b/src/Map/SearchShortcode.php @@ -1,6 +1,10 @@