| 1 |
<?php |
| 2 |
/** |
| 3 |
* Allow-listed WordPress template loader for shared catalogs. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
* @since 11.6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Print shared and catalog-owned WP template files once per request. |
| 15 |
*/ |
| 16 |
final class WPBC_UI_Catalog_Template_Loader { |
| 17 |
|
| 18 |
/** |
| 19 |
* Template identifiers already made available in the page. |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
private static $printed_template_ids = array(); |
| 24 |
|
| 25 |
/** |
| 26 |
* Physical files already included in the page. |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
private static $printed_template_files = array(); |
| 31 |
|
| 32 |
/** |
| 33 |
* Print all registered template files referenced by one catalog. |
| 34 |
* |
| 35 |
* Unknown template identifiers are ignored. Paths come only from shared code |
| 36 |
* or a trusted registry declaration and are never accepted from requests. |
| 37 |
* |
| 38 |
* @param string $catalog_id Registered catalog identifier. |
| 39 |
* |
| 40 |
* @return array<int,string> Template identifiers available after loading. |
| 41 |
*/ |
| 42 |
public static function print_catalog_templates( $catalog_id ) { |
| 43 |
$catalog_id = is_scalar( $catalog_id ) ? sanitize_key( (string) $catalog_id ) : ''; |
| 44 |
$registry = WPBC_UI_Catalog_Registry::get_instance(); |
| 45 |
$configuration = $registry->get_configuration( $catalog_id ); |
| 46 |
|
| 47 |
if ( empty( $configuration ) ) { |
| 48 |
return array(); |
| 49 |
} |
| 50 |
|
| 51 |
$template_files = array_merge( self::get_shared_template_files(), $registry->get_template_files( $catalog_id ) ); |
| 52 |
$template_ids = self::get_referenced_template_ids( $configuration ); |
| 53 |
|
| 54 |
foreach ( $template_ids as $template_id ) { |
| 55 |
if ( in_array( $template_id, self::$printed_template_ids, true ) || empty( $template_files[ $template_id ] ) ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
$template_file = $template_files[ $template_id ]; |
| 60 |
if ( ! in_array( $template_file, self::$printed_template_files, true ) ) { |
| 61 |
require $template_file; |
| 62 |
self::$printed_template_files[] = $template_file; |
| 63 |
} |
| 64 |
|
| 65 |
self::$printed_template_ids[] = $template_id; |
| 66 |
} |
| 67 |
|
| 68 |
return array_values( array_intersect( $template_ids, array_keys( $template_files ) ) ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Return trusted shared template files keyed by template identifier. |
| 73 |
* |
| 74 |
* @return array<string,string> Shared template file map. |
| 75 |
*/ |
| 76 |
private static function get_shared_template_files() { |
| 77 |
return array( |
| 78 |
'wpbc-ui-catalog-shell' => __DIR__ . '/templates/catalog-shell-wptpl.php', |
| 79 |
'wpbc-ui-catalog-empty' => __DIR__ . '/templates/catalog-empty-wptpl.php', |
| 80 |
'wpbc-ui-catalog-error' => __DIR__ . '/templates/catalog-error-wptpl.php', |
| 81 |
'wpbc-ui-catalog-inspector-shell' => __DIR__ . '/templates/catalog-inspector-shell-wptpl.php', |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Collect template IDs declared by the base map and presentation packs. |
| 87 |
* |
| 88 |
* @param array $configuration Registered catalog configuration. |
| 89 |
* |
| 90 |
* @return array<int,string> Unique referenced template identifiers. |
| 91 |
*/ |
| 92 |
private static function get_referenced_template_ids( $configuration ) { |
| 93 |
$template_ids = array(); |
| 94 |
$template_maps = array(); |
| 95 |
|
| 96 |
if ( isset( $configuration['templates'] ) && is_array( $configuration['templates'] ) ) { |
| 97 |
$template_maps[] = $configuration['templates']; |
| 98 |
} |
| 99 |
|
| 100 |
if ( isset( $configuration['template_packs'] ) && is_array( $configuration['template_packs'] ) ) { |
| 101 |
foreach ( $configuration['template_packs'] as $template_pack ) { |
| 102 |
if ( is_array( $template_pack ) ) { |
| 103 |
$template_maps[] = $template_pack; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
foreach ( $template_maps as $template_map ) { |
| 109 |
foreach ( $template_map as $template_id ) { |
| 110 |
$template_id = is_scalar( $template_id ) ? sanitize_key( (string) $template_id ) : ''; |
| 111 |
if ( '' !== $template_id && ! in_array( $template_id, $template_ids, true ) ) { |
| 112 |
$template_ids[] = $template_id; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return $template_ids; |
| 118 |
} |
| 119 |
} |
| 120 |
|