| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Blocks_V2\Phone_Field; |
| 4 |
|
| 5 |
use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 6 |
use JFB_Components\Module\Base_Module_Handle_It; |
| 7 |
use JFB_Modules\Blocks_V2\Interfaces\Block_Asset_Interface; |
| 8 |
use JFB_Modules\Blocks_V2\Module; |
| 9 |
use Jet_Form_Builder\Blocks\Module as LegacyBlocksModule; |
| 10 |
|
| 11 |
/** |
| 12 |
* International Phone Field Assets Handler |
| 13 |
*/ |
| 14 |
class Block_Asset implements Block_Asset_Interface { |
| 15 |
|
| 16 |
const INTL_TEL_INPUT_VERSION = '25.2.0'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Initialize hooks |
| 20 |
*/ |
| 21 |
public function init_hooks() { |
| 22 |
add_action( |
| 23 |
'jet-form-builder/editor-assets/before', |
| 24 |
array( $this, 'enqueue_editor_assets' ) |
| 25 |
); |
| 26 |
add_action( |
| 27 |
'wp_enqueue_scripts', |
| 28 |
array( $this, 'register_frontend_assets' ) |
| 29 |
); |
| 30 |
add_action( |
| 31 |
'jet_plugins/frontend/register_scripts', |
| 32 |
array( $this, 'register_frontend_assets' ) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Register frontend assets |
| 38 |
* |
| 39 |
* @throws Repository_Exception |
| 40 |
*/ |
| 41 |
public function register_frontend_assets() { |
| 42 |
/** @var Module $blocks_v2 */ |
| 43 |
$blocks_v2 = jet_form_builder()->module( 'blocks-v2' ); |
| 44 |
|
| 45 |
// Register intl-tel-input library (local files) |
| 46 |
wp_register_style( |
| 47 |
'intl-tel-input', |
| 48 |
$blocks_v2->get_url( 'phone-field/assets/lib/intl-tel-input/intlTelInput.min.css' ), |
| 49 |
array(), |
| 50 |
self::INTL_TEL_INPUT_VERSION |
| 51 |
); |
| 52 |
|
| 53 |
wp_register_script( |
| 54 |
'intl-tel-input', |
| 55 |
$blocks_v2->get_url( 'phone-field/assets/lib/intl-tel-input/intlTelInputWithUtils.min.js' ), |
| 56 |
array(), |
| 57 |
self::INTL_TEL_INPUT_VERSION, |
| 58 |
true |
| 59 |
); |
| 60 |
|
| 61 |
// Check if our built assets exist |
| 62 |
$asset_file = $blocks_v2->get_dir( 'phone-field/assets/build/frontend/field.asset.php' ); |
| 63 |
|
| 64 |
if ( file_exists( $asset_file ) ) { |
| 65 |
$asset = require_once $asset_file; |
| 66 |
|
| 67 |
if ( true !== $asset ) { |
| 68 |
$asset['dependencies'][] = LegacyBlocksModule::MAIN_SCRIPT_HANDLE; |
| 69 |
$asset['dependencies'][] = 'intl-tel-input'; |
| 70 |
|
| 71 |
$handle = $blocks_v2->get_handle( 'phone-field' ); |
| 72 |
|
| 73 |
wp_register_script( |
| 74 |
$handle, |
| 75 |
$blocks_v2->get_url( 'phone-field/assets/build/frontend/field.js' ), |
| 76 |
$asset['dependencies'], |
| 77 |
$asset['version'], |
| 78 |
true |
| 79 |
); |
| 80 |
|
| 81 |
// Localize script with custom country name translations |
| 82 |
wp_localize_script( |
| 83 |
$handle, |
| 84 |
'jfbPhoneFieldI18n', |
| 85 |
$this->get_localized_country_names() |
| 86 |
); |
| 87 |
|
| 88 |
wp_localize_script( |
| 89 |
$handle, |
| 90 |
'jfbPhoneFieldLocaleContext', |
| 91 |
array( |
| 92 |
'pageLocale' => determine_locale(), |
| 93 |
'pageLang' => get_bloginfo( 'language' ), |
| 94 |
'siteLocale' => get_locale(), |
| 95 |
'siteLang' => str_replace( '_', '-', get_locale() ), |
| 96 |
) |
| 97 |
); |
| 98 |
|
| 99 |
wp_register_style( |
| 100 |
$blocks_v2->get_handle( 'phone-field' ), |
| 101 |
$blocks_v2->get_url( 'phone-field/assets/build/frontend/field.css' ), |
| 102 |
array( 'intl-tel-input' ), |
| 103 |
$asset['version'] |
| 104 |
); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get localized country names for intl-tel-input |
| 111 |
* |
| 112 |
* @return array Country names that can be translated |
| 113 |
*/ |
| 114 |
private function get_localized_country_names() { |
| 115 |
return apply_filters( |
| 116 |
'jet-form-builder/phone-field/i18n', |
| 117 |
array( |
| 118 |
// By default, intl-tel-input uses its own country name translations |
| 119 |
// Add entries here ONLY if you want to override specific countries |
| 120 |
// Example: |
| 121 |
// 'ua' => __( 'Ukraine', 'jet-form-builder' ), |
| 122 |
// 'us' => __( 'United States', 'jet-form-builder' ), |
| 123 |
// 'gb' => __( 'United Kingdom', 'jet-form-builder' ), |
| 124 |
) |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Enqueue editor assets |
| 130 |
* |
| 131 |
* @return void |
| 132 |
* @throws Repository_Exception |
| 133 |
*/ |
| 134 |
public function enqueue_editor_assets() { |
| 135 |
/** @var Module $blocks_v2 */ |
| 136 |
$blocks_v2 = jet_form_builder()->module( 'blocks-v2' ); |
| 137 |
|
| 138 |
$asset_file = $blocks_v2->get_dir( 'phone-field/assets/build/editor/index.asset.php' ); |
| 139 |
|
| 140 |
if ( ! file_exists( $asset_file ) ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
$asset = require_once $asset_file; |
| 145 |
|
| 146 |
if ( true === $asset ) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
wp_enqueue_script( |
| 151 |
$blocks_v2->get_handle( 'phone-field-editor' ), |
| 152 |
$blocks_v2->get_url( 'phone-field/assets/build/editor/index.js' ), |
| 153 |
$asset['dependencies'], |
| 154 |
$asset['version'], |
| 155 |
true |
| 156 |
); |
| 157 |
|
| 158 |
wp_enqueue_style( |
| 159 |
$blocks_v2->get_handle( 'phone-field-editor' ), |
| 160 |
$blocks_v2->get_url( 'phone-field/assets/build/editor/index.css' ), |
| 161 |
array(), |
| 162 |
$asset['version'] |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Enqueue field assets when field is rendered |
| 168 |
* Called from Block_Render::before_render() |
| 169 |
*/ |
| 170 |
public function enqueue_field_assets() { |
| 171 |
wp_enqueue_style( 'intl-tel-input' ); |
| 172 |
wp_enqueue_script( Base_Module_Handle_It::HANDLE_PREFIX . 'blocks-v2-phone-field' ); |
| 173 |
wp_enqueue_style( Base_Module_Handle_It::HANDLE_PREFIX . 'blocks-v2-phone-field' ); |
| 174 |
} |
| 175 |
} |
| 176 |
|