| 1 |
<?php |
| 2 |
/** |
| 3 |
* Merchant Admin Assets. |
| 4 |
* |
| 5 |
* Handles enqueueing of admin scripts and styles for module |
| 6 |
* settings pages. Extracted from {@see Merchant_Admin_Options} |
| 7 |
* to isolate asset management concerns. |
| 8 |
* |
| 9 |
* @package Merchant |
| 10 |
* @since 1.9.3 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Merchant_Admin_Assets |
| 19 |
* |
| 20 |
* Enqueues Select2, air-datepicker, and their localized strings |
| 21 |
* when viewing a Merchant module settings page. |
| 22 |
* |
| 23 |
* @since 1.9.3 |
| 24 |
*/ |
| 25 |
class Merchant_Admin_Assets { |
| 26 |
|
| 27 |
/** |
| 28 |
* Enqueue admin scripts and styles for module settings pages. |
| 29 |
* |
| 30 |
* Delegates to dedicated helpers for each asset group when on |
| 31 |
* a Merchant module settings page. |
| 32 |
* |
| 33 |
* @since 1.0 |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function enqueue_scripts() { |
| 38 |
if ( ! $this->is_module_settings_page() ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$this->enqueue_select2_assets(); |
| 43 |
$this->enqueue_datepicker_assets(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Check if the current admin page is a Merchant module settings page. |
| 48 |
* |
| 49 |
* @since 1.9.3 |
| 50 |
* |
| 51 |
* @return bool True if on a module settings page. |
| 52 |
*/ |
| 53 |
private function is_module_settings_page() { |
| 54 |
return isset( $_GET['page'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 55 |
&& 'merchant' === $_GET['page'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 56 |
&& isset( $_GET['module'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Enqueue Select2 scripts, styles, and localized strings. |
| 61 |
* |
| 62 |
* @since 1.9.3 |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
private function enqueue_select2_assets() { |
| 67 |
wp_enqueue_script( 'merchant-select2', MERCHANT_URI . 'assets/vendor/select2/select2.full.min.js', array( 'jquery' ), '4.0.13', true ); |
| 68 |
wp_enqueue_style( 'merchant-select2', MERCHANT_URI . 'assets/vendor/select2/select2.min.css', array(), '4.0.13', 'all' ); |
| 69 |
|
| 70 |
wp_localize_script( 'merchant-select2', 'merchant_admin_options', array( |
| 71 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 72 |
'ajaxnonce' => wp_create_nonce( 'merchant_admin_options' ), |
| 73 |
'product_delete_confirmation_message' => esc_html__( 'Are you sure you want to remove this product?', 'merchant' ), |
| 74 |
'invalid_file' => esc_html__( 'Invalid file', 'merchant' ), |
| 75 |
) ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Enqueue air-datepicker scripts, styles, and locale strings. |
| 80 |
* |
| 81 |
* @since 1.9.3 |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
private function enqueue_datepicker_assets() { |
| 86 |
wp_enqueue_style( 'date-picker', MERCHANT_URI . 'assets/vendor/air-datepicker/air-datepicker.css', array(), MERCHANT_VERSION, 'all' ); |
| 87 |
wp_enqueue_script( 'date-picker', MERCHANT_URI . 'assets/vendor/air-datepicker/air-datepicker.js', array( 'jquery' ), MERCHANT_VERSION, true ); |
| 88 |
wp_localize_script( 'date-picker', 'merchant_datepicker_locale', array( |
| 89 |
wp_json_encode( $this->get_datepicker_locale() ), |
| 90 |
) ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the datepicker localization data. |
| 95 |
* |
| 96 |
* Returns translated day names, month names, and labels |
| 97 |
* for the air-datepicker library. |
| 98 |
* |
| 99 |
* @since 1.9.3 |
| 100 |
* |
| 101 |
* @return array<string, mixed> Datepicker locale strings. |
| 102 |
*/ |
| 103 |
private function get_datepicker_locale() { |
| 104 |
return array( |
| 105 |
'days' => array( |
| 106 |
esc_html__( 'Sunday', 'merchant' ), |
| 107 |
esc_html__( 'Monday', 'merchant' ), |
| 108 |
esc_html__( 'Tuesday', 'merchant' ), |
| 109 |
esc_html__( 'Wednesday', 'merchant' ), |
| 110 |
esc_html__( 'Thursday', 'merchant' ), |
| 111 |
esc_html__( 'Friday', 'merchant' ), |
| 112 |
esc_html__( 'Saturday', 'merchant' ), |
| 113 |
), |
| 114 |
'daysShort' => array( |
| 115 |
esc_html__( 'Sun', 'merchant' ), |
| 116 |
esc_html__( 'Mon', 'merchant' ), |
| 117 |
esc_html__( 'Tue', 'merchant' ), |
| 118 |
esc_html__( 'Wed', 'merchant' ), |
| 119 |
esc_html__( 'Thu', 'merchant' ), |
| 120 |
esc_html__( 'Fri', 'merchant' ), |
| 121 |
esc_html__( 'Sat', 'merchant' ), |
| 122 |
), |
| 123 |
'daysMin' => array( |
| 124 |
esc_html__( 'Su', 'merchant' ), |
| 125 |
esc_html__( 'Mo', 'merchant' ), |
| 126 |
esc_html__( 'Tu', 'merchant' ), |
| 127 |
esc_html__( 'We', 'merchant' ), |
| 128 |
esc_html__( 'Th', 'merchant' ), |
| 129 |
esc_html__( 'Fr', 'merchant' ), |
| 130 |
esc_html__( 'Sa', 'merchant' ), |
| 131 |
), |
| 132 |
'months' => array( |
| 133 |
esc_html__( 'January', 'merchant' ), |
| 134 |
esc_html__( 'February', 'merchant' ), |
| 135 |
esc_html__( 'March', 'merchant' ), |
| 136 |
esc_html__( 'April', 'merchant' ), |
| 137 |
esc_html__( 'May', 'merchant' ), |
| 138 |
esc_html__( 'June', 'merchant' ), |
| 139 |
esc_html__( 'July', 'merchant' ), |
| 140 |
esc_html__( 'August', 'merchant' ), |
| 141 |
esc_html__( 'September', 'merchant' ), |
| 142 |
esc_html__( 'October', 'merchant' ), |
| 143 |
esc_html__( 'November', 'merchant' ), |
| 144 |
esc_html__( 'December', 'merchant' ), |
| 145 |
), |
| 146 |
'monthsShort' => array( |
| 147 |
esc_html__( 'Jan', 'merchant' ), |
| 148 |
esc_html__( 'Feb', 'merchant' ), |
| 149 |
esc_html__( 'Mar', 'merchant' ), |
| 150 |
esc_html__( 'Apr', 'merchant' ), |
| 151 |
esc_html__( 'May', 'merchant' ), |
| 152 |
esc_html__( 'Jun', 'merchant' ), |
| 153 |
esc_html__( 'Jul', 'merchant' ), |
| 154 |
esc_html__( 'Aug', 'merchant' ), |
| 155 |
esc_html__( 'Sep', 'merchant' ), |
| 156 |
esc_html__( 'Oct', 'merchant' ), |
| 157 |
esc_html__( 'Nov', 'merchant' ), |
| 158 |
esc_html__( 'Dec', 'merchant' ), |
| 159 |
), |
| 160 |
'clear' => esc_html__( 'Clear', 'merchant' ), |
| 161 |
); |
| 162 |
} |
| 163 |
} |
| 164 |
|