Skip to content

Commit

Permalink
Merge pull request #1637 from wielebenwir/bugfix/issue-1636
Browse files Browse the repository at this point in the history
Cache: Warmup does not exit if an shortcode call has an exception
  • Loading branch information
hansmorb authored Oct 2, 2024
2 parents e7636a3 + 2b6bfe8 commit 4841327
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
21 changes: 15 additions & 6 deletions src/Map/BaseShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

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 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']) {
Expand All @@ -25,10 +35,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);
Expand Down
3 changes: 3 additions & 0 deletions src/Map/MapShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
4 changes: 4 additions & 0 deletions src/Map/SearchShortcode.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

namespace CommonsBooking\Map;

/**
* Short code for a multi-widget with map, search and table capabilities.
*/
class SearchShortcode extends BaseShortcode {
protected $processed_map_ids = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/Service/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Wordpress/CustomPostType/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 4841327

Please sign in to comment.