| 1 |
<?php |
| 2 |
/** |
| 3 |
* Shared asset boundary for template-driven administration catalogs. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
* @since 11.6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Register and enqueue only the assets shared by independent catalogs. |
| 15 |
* |
| 16 |
* Domain data, SQL, permissions, and mutations deliberately remain outside |
| 17 |
* this class. Later phases can extend the catalog mechanics without coupling |
| 18 |
* this foundation to an individual administration page. |
| 19 |
*/ |
| 20 |
final class WPBC_UI_Catalog { |
| 21 |
|
| 22 |
/** |
| 23 |
* Build a cache-safe version for one generated shared asset. |
| 24 |
* |
| 25 |
* Shared catalog controllers can change independently during an in-version |
| 26 |
* migration. Appending the generated file modification time prevents a |
| 27 |
* cached lifecycle controller from running against a newer domain adapter. |
| 28 |
* |
| 29 |
* @param string $asset_filename Generated `_out` asset filename. |
| 30 |
* |
| 31 |
* @return string Plugin version with an optional file modification suffix. |
| 32 |
*/ |
| 33 |
private static function get_asset_version( $asset_filename ) { |
| 34 |
$asset_filename = is_scalar( $asset_filename ) ? basename( (string) $asset_filename ) : ''; |
| 35 |
$asset_path = __DIR__ . '/_out/' . $asset_filename; |
| 36 |
$modified_time = ( '' !== $asset_filename && is_file( $asset_path ) ) ? filemtime( $asset_path ) : false; |
| 37 |
|
| 38 |
return false === $modified_time |
| 39 |
? (string) WP_BK_VERSION_NUM |
| 40 |
: WP_BK_VERSION_NUM . '.' . (string) $modified_time; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Return a browser-safe configuration for one registered catalog mount. |
| 45 |
* |
| 46 |
* Trusted PHP template paths and provider objects live outside the stored |
| 47 |
* configuration, so the returned array can be localized directly. |
| 48 |
* |
| 49 |
* @param string $catalog_id Registered catalog identifier. |
| 50 |
* @param string $mount_id HTML element ID receiving the catalog. |
| 51 |
* @param array $initial_request Normalized initial shared request. |
| 52 |
* @param array $initial_response Normalized initial response contract. |
| 53 |
* |
| 54 |
* @return array<string,mixed> Browser configuration or an empty array. |
| 55 |
*/ |
| 56 |
public static function get_client_configuration( $catalog_id, $mount_id, $initial_request, $initial_response ) { |
| 57 |
$registry = WPBC_UI_Catalog_Registry::get_instance(); |
| 58 |
$configuration = $registry->get_configuration( $catalog_id ); |
| 59 |
$mount_id = is_scalar( $mount_id ) ? sanitize_html_class( (string) $mount_id ) : ''; |
| 60 |
|
| 61 |
if ( empty( $configuration ) || '' === $mount_id || ! is_array( $initial_request ) || ! is_array( $initial_response ) ) { |
| 62 |
return array(); |
| 63 |
} |
| 64 |
|
| 65 |
$configuration['mount_id'] = $mount_id; |
| 66 |
$configuration['preference_namespace'] = WPBC_UI_Catalog_Preferences::get_namespace( $catalog_id ); |
| 67 |
$configuration['initial_request'] = $initial_request; |
| 68 |
$configuration['initial_response'] = $initial_response; |
| 69 |
|
| 70 |
return $configuration; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Print allow-listed shared and catalog-owned WordPress templates. |
| 75 |
* |
| 76 |
* @param string $catalog_id Registered catalog identifier. |
| 77 |
* |
| 78 |
* @return array<int,string> Template identifiers made available. |
| 79 |
*/ |
| 80 |
public static function print_templates( $catalog_id ) { |
| 81 |
return WPBC_UI_Catalog_Template_Loader::print_catalog_templates( $catalog_id ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Enqueue the shared catalog stylesheet. |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public static function enqueue_styles() { |
| 90 |
wp_enqueue_style( |
| 91 |
'wpbc-ui-catalog', |
| 92 |
trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/wpbc_ui_catalog.css', |
| 93 |
array(), |
| 94 |
self::get_asset_version( 'wpbc_ui_catalog.css' ) |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Enqueue shared catalog, hierarchy, selection, and row-action controllers. |
| 100 |
* |
| 101 |
* WordPress' `wp-util` package provides the allow-listed `wp.template()` |
| 102 |
* renderer used by every template-driven catalog. The catalog script also |
| 103 |
* exposes opt-in inline-workflow mechanics for sticky bars, busy controls, |
| 104 |
* and changed-row presentation. Hierarchy, selection, inline workflow, and |
| 105 |
* action menus do not perform domain reads or mutations. |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public static function enqueue_scripts() { |
| 110 |
wp_enqueue_script( |
| 111 |
'wpbc-ui-catalog-sortable', |
| 112 |
wpbc_plugin_url( '/vendors/sortablejs/Sortable.min.js' ), |
| 113 |
array(), |
| 114 |
WP_BK_VERSION_NUM, |
| 115 |
array( 'in_footer' => WPBC_JS_IN_FOOTER ) |
| 116 |
); |
| 117 |
wp_enqueue_script( |
| 118 |
'wpbc-ui-catalog', |
| 119 |
trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/wpbc_ui_catalog.js', |
| 120 |
array( 'wp-util', 'wpbc-ui-catalog-sortable' ), |
| 121 |
self::get_asset_version( 'wpbc_ui_catalog.js' ), |
| 122 |
array( 'in_footer' => WPBC_JS_IN_FOOTER ) |
| 123 |
); |
| 124 |
wp_enqueue_script( |
| 125 |
'wpbc-ui-catalog-hierarchy', |
| 126 |
trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/wpbc_ui_catalog_hierarchy.js', |
| 127 |
array( 'wpbc-ui-catalog' ), |
| 128 |
self::get_asset_version( 'wpbc_ui_catalog_hierarchy.js' ), |
| 129 |
array( 'in_footer' => WPBC_JS_IN_FOOTER ) |
| 130 |
); |
| 131 |
wp_enqueue_script( |
| 132 |
'wpbc-ui-catalog-selection', |
| 133 |
trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/wpbc_ui_catalog_selection.js', |
| 134 |
array( 'wpbc-ui-catalog' ), |
| 135 |
self::get_asset_version( 'wpbc_ui_catalog_selection.js' ), |
| 136 |
array( 'in_footer' => WPBC_JS_IN_FOOTER ) |
| 137 |
); |
| 138 |
wp_enqueue_script( |
| 139 |
'wpbc-ui-catalog-actions', |
| 140 |
trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/wpbc_ui_catalog_actions.js', |
| 141 |
array( 'wpbc-ui-catalog' ), |
| 142 |
self::get_asset_version( 'wpbc_ui_catalog_actions.js' ), |
| 143 |
array( 'in_footer' => WPBC_JS_IN_FOOTER ) |
| 144 |
); |
| 145 |
} |
| 146 |
} |
| 147 |
|