LocalizationManager.php
423 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Core; |
| 4 | |
| 5 | use EmbedPress\Includes\Classes\Helper; |
| 6 | |
| 7 | /** |
| 8 | * EmbedPress Localization Manager |
| 9 | * |
| 10 | * Handles all JavaScript localization for EmbedPress blocks, admin, and frontend |
| 11 | * Provides clean separation of concerns from AssetManager |
| 12 | */ |
| 13 | class LocalizationManager |
| 14 | { |
| 15 | /** |
| 16 | * Setup admin localization scripts |
| 17 | * |
| 18 | * @param string $hook Current admin page hook |
| 19 | */ |
| 20 | public static function setup_admin_localization($hook) |
| 21 | { |
| 22 | global $pagenow; |
| 23 | |
| 24 | self::setup_license_localization(); |
| 25 | |
| 26 | // Setup settings page localization if on EmbedPress settings page |
| 27 | if (strpos($hook, 'embedpress') !== false) { |
| 28 | self::setup_settings_localization(); |
| 29 | self::setup_preview_localization(); |
| 30 | } |
| 31 | |
| 32 | // Only setup localization on post edit pages |
| 33 | if (!in_array($pagenow, ['post.php', 'post-new.php'])) { |
| 34 | return; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Setup editor localization scripts |
| 40 | */ |
| 41 | public static function setup_editor_localization() |
| 42 | { |
| 43 | self::setup_frontend_script_localization(); |
| 44 | self::setup_gutenberg_localization(); |
| 45 | self::setup_new_blocks_localization(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Setup frontend localization scripts |
| 50 | */ |
| 51 | public static function setup_frontend_localization() |
| 52 | { |
| 53 | self::setup_frontend_script_localization(); |
| 54 | self::setup_gutenberg_localization(); |
| 55 | self::setup_preview_localization(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Setup Elementor widget localization |
| 60 | */ |
| 61 | public static function setup_elementor_localization() |
| 62 | { |
| 63 | self::setup_frontend_script_localization(); |
| 64 | self::setup_calendar_widget_localization(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Setup preview localization (attached to admin script) |
| 69 | */ |
| 70 | private static function setup_preview_localization() |
| 71 | { |
| 72 | $script_handle = 'embedpress-admin'; |
| 73 | |
| 74 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | $url_schemes = apply_filters('embedpress:getAdditionalURLSchemes', self::get_url_schemes()); |
| 79 | |
| 80 | // Ensure required constants are defined with fallbacks |
| 81 | $version = defined('EMBEDPRESS_VERSION') ? EMBEDPRESS_VERSION : '1.0.0'; |
| 82 | $shortcode = defined('EMBEDPRESS_SHORTCODE') ? EMBEDPRESS_SHORTCODE : 'embedpress'; |
| 83 | $assets_url = defined('EMBEDPRESS_URL_ASSETS') ? EMBEDPRESS_URL_ASSETS : ''; |
| 84 | |
| 85 | wp_localize_script($script_handle, 'embedpressPreviewData', [ |
| 86 | 'previewSettings' => [ |
| 87 | 'baseUrl' => get_site_url() . '/', |
| 88 | 'versionUID' => $version, |
| 89 | 'debug' => defined('WP_DEBUG') && WP_DEBUG, |
| 90 | ], |
| 91 | 'shortcode' => $shortcode, |
| 92 | 'assetsUrl' => $assets_url, |
| 93 | 'urlSchemes' => $url_schemes, |
| 94 | ]); |
| 95 | |
| 96 | wp_localize_script($script_handle, 'embedpressAdminParams', [ |
| 97 | 'ajaxUrl' => admin_url('admin-ajax.php'), |
| 98 | 'nonce' => wp_create_nonce('embedpress') |
| 99 | ]); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Setup license script localization |
| 104 | */ |
| 105 | private static function setup_license_localization() |
| 106 | { |
| 107 | $script_handle = 'embedpress-admin'; |
| 108 | |
| 109 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | $item_id = defined('EMBEDPRESS_SL_ITEM_ID') ? EMBEDPRESS_SL_ITEM_ID : 'embedpress'; |
| 114 | |
| 115 | wp_localize_script($script_handle, 'embedpressLicenseData', [ |
| 116 | 'nonce' => wp_create_nonce('wpdeveloper_sl_' . $item_id . '_nonce') |
| 117 | ]); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Setup Gutenberg blocks localization (embedpressObj variable) |
| 122 | */ |
| 123 | private static function setup_gutenberg_localization() |
| 124 | { |
| 125 | $script_handle = 'embedpress-blocks-editor'; |
| 126 | |
| 127 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | // Ensure required constants are defined |
| 132 | if (!defined('EMBEDPRESS_PLG_NAME')) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | $elements = (array) get_option(EMBEDPRESS_PLG_NAME . ":elements", []); |
| 137 | $active_blocks = isset($elements['gutenberg']) ? (array) $elements['gutenberg'] : []; |
| 138 | |
| 139 | $wistia_labels = self::get_wistia_labels(); |
| 140 | $wistia_options = self::get_wistia_options(); |
| 141 | $pars_url = wp_parse_url(get_site_url()); |
| 142 | $documents_cta_options = (array) get_option(EMBEDPRESS_PLG_NAME . ':document'); |
| 143 | $current_user = wp_get_current_user(); |
| 144 | $assets_url = defined('EMBEDPRESS_URL_ASSETS') ? EMBEDPRESS_URL_ASSETS : ''; |
| 145 | $static_url = defined('EMBEDPRESS_URL_STATIC') ? EMBEDPRESS_URL_STATIC : ''; |
| 146 | |
| 147 | wp_localize_script($script_handle, 'embedpressGutenbergData', [ |
| 148 | 'wistiaLabels' => json_encode($wistia_labels), |
| 149 | 'wistiaOptions' => $wistia_options, |
| 150 | 'poweredBy' => apply_filters('embedpress_document_block_powered_by', true), |
| 151 | 'isProVersion' => defined('EMBEDPRESS_PRO_PLUGIN_FILE'), |
| 152 | 'twitchHost' => !empty($pars_url['host']) ? $pars_url['host'] : '', |
| 153 | 'siteUrl' => site_url(), |
| 154 | 'activeBlocks' => $active_blocks, |
| 155 | 'documentCta' => $documents_cta_options, |
| 156 | 'pdfRenderer' => Helper::get_pdf_renderer(), |
| 157 | 'isProPluginActive' => defined('EMBEDPRESS_SL_ITEM_SLUG'), |
| 158 | 'ajaxUrl' => admin_url('admin-ajax.php'), |
| 159 | 'sourceNonce' => wp_create_nonce('source_nonce_embedpress'), |
| 160 | 'canUploadMedia' => current_user_can('upload_files'), |
| 161 | 'assetsUrl' => $assets_url, |
| 162 | 'staticUrl' => $static_url, |
| 163 | 'iframeWidth' => Helper::get_options_value('enableEmbedResizeWidth', '600'), |
| 164 | 'iframeHeight' => Helper::get_options_value('enableEmbedResizeHeight', '400'), |
| 165 | 'pdfCustomColor' => Helper::get_options_value('custom_color', '#403A81'), |
| 166 | 'brandingLogos' => [ |
| 167 | 'youtube' => Helper::get_branding_value('logo_url', 'youtube'), |
| 168 | 'vimeo' => Helper::get_branding_value('logo_url', 'vimeo'), |
| 169 | 'wistia' => Helper::get_branding_value('logo_url', 'wistia'), |
| 170 | 'twitch' => Helper::get_branding_value('logo_url', 'twitch'), |
| 171 | 'dailymotion' => Helper::get_branding_value('logo_url', 'dailymotion'), |
| 172 | ], |
| 173 | 'userRoles' => Helper::get_user_roles(), |
| 174 | 'currentUser' => $current_user->data, |
| 175 | 'feedbackSubmitted' => get_option('embedpress_feedback_submited'), |
| 176 | 'ratingHelpDisabled' => Helper::get_options_value('turn_off_rating_help', false), |
| 177 | ]); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Setup frontend script localization (embedpressFrontendData variable) |
| 182 | */ |
| 183 | private static function setup_frontend_script_localization() |
| 184 | { |
| 185 | // The embedpressFrontendData variable should be attached to multiple frontend scripts |
| 186 | $script_handles = ['embedpress-front', 'embedpress-ads']; |
| 187 | |
| 188 | $localization_data = [ |
| 189 | 'ajaxurl' => admin_url('admin-ajax.php'), |
| 190 | 'isProPluginActive' => defined('EMBEDPRESS_SL_ITEM_SLUG'), |
| 191 | 'nonce' => wp_create_nonce('ep_nonce'), |
| 192 | ]; |
| 193 | |
| 194 | foreach ($script_handles as $script_handle) { |
| 195 | if (wp_script_is($script_handle, 'enqueued') || wp_script_is($script_handle, 'registered')) { |
| 196 | wp_localize_script($script_handle, 'embedpressFrontendData', $localization_data); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Setup settings page localization (attached to admin script) |
| 203 | */ |
| 204 | private static function setup_settings_localization() |
| 205 | { |
| 206 | $script_handle = 'embedpress-admin'; |
| 207 | |
| 208 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | wp_localize_script($script_handle, 'embedpressSettingsData', [ |
| 213 | 'nonce' => wp_create_nonce('embedpress_elements_action'), |
| 214 | ]); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Setup new blocks localization (for new block system) |
| 219 | */ |
| 220 | private static function setup_new_blocks_localization() |
| 221 | { |
| 222 | // Try multiple possible handles for new blocks |
| 223 | $possible_handles = [ |
| 224 | 'embedpress-blocks-editor', // Current handle from AssetManager |
| 225 | 'embedpress-blocks', // Old handle |
| 226 | 'embedpress_blocks-cgb-block-js', // Legacy handle |
| 227 | 'embedpress-blocks-js', // Alternative handle |
| 228 | ]; |
| 229 | |
| 230 | $script_handle = null; |
| 231 | foreach ($possible_handles as $handle) { |
| 232 | if (wp_script_is($handle, 'enqueued') || wp_script_is($handle, 'registered')) { |
| 233 | $script_handle = $handle; |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (!$script_handle) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | // Ensure required constants are defined |
| 243 | if (!defined('EMBEDPRESS_PLG_NAME')) { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | $elements = (array) get_option(EMBEDPRESS_PLG_NAME . ":elements", []); |
| 248 | $active_blocks = isset($elements['gutenberg']) ? (array) $elements['gutenberg'] : []; |
| 249 | |
| 250 | wp_localize_script($script_handle, 'embedpressNewBlocksData', [ |
| 251 | 'pluginDirPath' => defined('EMBEDPRESS_PATH_BASE') ? EMBEDPRESS_PATH_BASE : '', |
| 252 | 'pluginDirUrl' => defined('EMBEDPRESS_URL_STATIC') ? EMBEDPRESS_URL_STATIC . '../' : '', |
| 253 | 'activeBlocks' => $active_blocks, |
| 254 | 'canUploadMedia' => current_user_can('upload_files'), |
| 255 | 'ajaxUrl' => admin_url('admin-ajax.php'), |
| 256 | 'nonce' => wp_create_nonce('embedpress_nonce'), |
| 257 | 'restUrl' => rest_url('embedpress/v1/'), |
| 258 | 'siteUrl' => site_url(), |
| 259 | ]); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | /** |
| 267 | * Setup Google Calendar widget localization |
| 268 | */ |
| 269 | private static function setup_calendar_widget_localization() |
| 270 | { |
| 271 | $script_handle = 'epgc'; |
| 272 | |
| 273 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | $nonce = wp_create_nonce('epgc_nonce'); |
| 278 | wp_localize_script($script_handle, 'embedpressCalendarData', [ |
| 279 | 'ajaxUrl' => admin_url('admin-ajax.php'), |
| 280 | 'nonce' => $nonce, |
| 281 | 'translations' => [ |
| 282 | 'allDay' => __('All day', 'embedpress'), |
| 283 | 'createdBy' => __('Created by', 'embedpress'), |
| 284 | 'goToEvent' => __('Go to event', 'embedpress'), |
| 285 | 'unknownError' => __('Unknown error', 'embedpress'), |
| 286 | 'requestError' => __('Request error', 'embedpress'), |
| 287 | 'loading' => __('Loading', 'embedpress') |
| 288 | ] |
| 289 | ]); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Load plugin text domain for translations |
| 294 | */ |
| 295 | public static function load_text_domain() |
| 296 | { |
| 297 | $locale = determine_locale(); |
| 298 | $locale = apply_filters('plugin_locale', $locale, 'embedpress'); |
| 299 | |
| 300 | // Load from WordPress languages directory first |
| 301 | if (file_exists(WP_LANG_DIR . "/embedpress-" . $locale . '.mo')) { |
| 302 | unload_textdomain('embedpress'); |
| 303 | load_textdomain('embedpress', WP_LANG_DIR . "/embedpress-" . $locale . '.mo'); |
| 304 | } |
| 305 | |
| 306 | // Load from plugin languages directory |
| 307 | load_plugin_textdomain('embedpress', false, plugin_basename(dirname(EMBEDPRESS_FILE)) . '/languages'); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Get Wistia labels for localization |
| 312 | * |
| 313 | * @return array |
| 314 | */ |
| 315 | private static function get_wistia_labels() |
| 316 | { |
| 317 | return [ |
| 318 | 'watch_from_beginning' => __('Watch from the beginning', 'embedpress'), |
| 319 | 'skip_to_where_you_left_off' => __('Skip to where you left off', 'embedpress'), |
| 320 | 'you_have_watched_it_before' => __('It looks like you\'ve watched<br />part of this video before!', 'embedpress'), |
| 321 | ]; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Get Wistia options safely |
| 326 | * |
| 327 | * @return mixed|null |
| 328 | */ |
| 329 | private static function get_wistia_options() |
| 330 | { |
| 331 | if (!function_exists('embedpress_wisita_pro_get_options')) { |
| 332 | return null; |
| 333 | } |
| 334 | |
| 335 | try { |
| 336 | // return embedpress_wisita_pro_get_options(); |
| 337 | } catch (\Exception $e) { |
| 338 | // Silently fail if function throws an error |
| 339 | return null; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Get branding value safely |
| 345 | * |
| 346 | * @param string $type |
| 347 | * @param string $provider |
| 348 | * @return string |
| 349 | */ |
| 350 | private static function get_branding_value($type, $provider) |
| 351 | { |
| 352 | if (!function_exists('get_branding_value')) { |
| 353 | return ''; |
| 354 | } |
| 355 | |
| 356 | try { |
| 357 | return self::get_branding_value($type, $provider); |
| 358 | } catch (\Exception $e) { |
| 359 | return ''; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Get URL schemes for preview script |
| 365 | * |
| 366 | * @return array |
| 367 | */ |
| 368 | private static function get_url_schemes() |
| 369 | { |
| 370 | return [ |
| 371 | // Apple podcasts |
| 372 | 'podcasts.apple.com/*', |
| 373 | // YouTube |
| 374 | 'youtube.com/watch\\?*', |
| 375 | 'youtube.com/playlist\\?*', |
| 376 | 'youtube.com/channel/*', |
| 377 | 'youtube.com/c/*', |
| 378 | 'youtube.com/user/*', |
| 379 | 'youtube.com/(\w+)[^?\/]*$', |
| 380 | // Vimeo |
| 381 | 'vimeo.com/*', |
| 382 | 'vimeo.com/groups/*/videos/*', |
| 383 | |
| 384 | 'twitter.com/*/status/*', |
| 385 | 'twitter.com/i/moments/*', |
| 386 | 'twitter.com/*/timelines/*', |
| 387 | |
| 388 | 'facebook.com/*', |
| 389 | 'fb.watch/*', |
| 390 | |
| 391 | 'instagram.com/p/*', |
| 392 | 'instagr.am/p/*', |
| 393 | // SoundCloud |
| 394 | 'soundcloud.com/*', |
| 395 | // Twitch |
| 396 | '*.twitch.tv/*', |
| 397 | 'twitch.tv/*', |
| 398 | // Wistia |
| 399 | '*.wistia.com/medias/*', |
| 400 | 'fast.wistia.com/embed/medias/*.jsonp', |
| 401 | // Google services |
| 402 | 'google.com/*', |
| 403 | 'google.com.*/*', |
| 404 | 'google.co.*/*', |
| 405 | 'maps.google.com/*', |
| 406 | 'docs.google.com/presentation/*', |
| 407 | 'docs.google.com/document/*', |
| 408 | 'docs.google.com/spreadsheets/*', |
| 409 | 'docs.google.com/forms/*', |
| 410 | 'docs.google.com/drawings/*', |
| 411 | ]; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Initialize localization manager hooks |
| 416 | */ |
| 417 | public static function init() |
| 418 | { |
| 419 | // Load text domain early |
| 420 | add_action('plugins_loaded', [__CLASS__, 'load_text_domain'], 1); |
| 421 | } |
| 422 | } |
| 423 |