LocalizationManager.php
678 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 | self::setup_feature_notices_localization(); |
| 26 | |
| 27 | // Setup settings page localization if on EmbedPress settings page |
| 28 | if (strpos($hook, 'embedpress') !== false) { |
| 29 | self::setup_settings_localization(); |
| 30 | self::setup_preview_localization(); |
| 31 | self::setup_onboarding_localization($hook); |
| 32 | } |
| 33 | |
| 34 | // Only setup localization on post edit pages |
| 35 | if (!in_array($pagenow, ['post.php', 'post-new.php'])) { |
| 36 | return; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Setup editor localization scripts |
| 42 | */ |
| 43 | public static function setup_editor_localization() |
| 44 | { |
| 45 | self::setup_frontend_script_localization(); |
| 46 | self::setup_gutenberg_localization(); |
| 47 | self::setup_new_blocks_localization(); |
| 48 | self::setup_preview_localization(); |
| 49 | // Needed for the Calendar block's editor ServerSideRender preview |
| 50 | // to authenticate AJAX requests via the epgc_nonce. |
| 51 | self::setup_calendar_widget_localization(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Setup frontend localization scripts |
| 56 | */ |
| 57 | public static function setup_frontend_localization() |
| 58 | { |
| 59 | self::setup_frontend_script_localization(); |
| 60 | self::setup_gutenberg_localization(); |
| 61 | self::setup_preview_localization(); |
| 62 | self::setup_analytics_localization(); |
| 63 | self::setup_pdf_gallery_localization(); |
| 64 | self::setup_pdf_lightbox_localization(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Setup Elementor widget localization |
| 69 | */ |
| 70 | public static function setup_elementor_localization() |
| 71 | { |
| 72 | self::setup_frontend_script_localization(); |
| 73 | self::setup_calendar_widget_localization(); |
| 74 | self::setup_analytics_localization(); |
| 75 | self::setup_pdf_gallery_localization(); |
| 76 | self::setup_pdf_lightbox_localization(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Setup preview localization (attached to preview script) |
| 81 | */ |
| 82 | private static function setup_preview_localization() |
| 83 | { |
| 84 | // Try both admin and preview script handles |
| 85 | $script_handles = ['embedpress-admin', 'embedpress-preview']; |
| 86 | |
| 87 | $url_schemes = apply_filters('embedpress:getAdditionalURLSchemes', self::get_url_schemes()); |
| 88 | |
| 89 | // Ensure required constants are defined with fallbacks |
| 90 | $version = defined('EMBEDPRESS_VERSION') ? EMBEDPRESS_VERSION : '1.0.0'; |
| 91 | $shortcode = defined('EMBEDPRESS_SHORTCODE') ? EMBEDPRESS_SHORTCODE : 'embedpress'; |
| 92 | $assets_url = defined('EMBEDPRESS_URL_ASSETS') ? EMBEDPRESS_URL_ASSETS : ''; |
| 93 | |
| 94 | $localization_data = [ |
| 95 | 'previewSettings' => [ |
| 96 | 'baseUrl' => get_site_url() . '/', |
| 97 | 'versionUID' => $version, |
| 98 | 'debug' => defined('WP_DEBUG') && WP_DEBUG, |
| 99 | ], |
| 100 | 'shortcode' => $shortcode, |
| 101 | 'assetsUrl' => $assets_url, |
| 102 | 'urlSchemes' => $url_schemes, |
| 103 | ]; |
| 104 | |
| 105 | foreach ($script_handles as $script_handle) { |
| 106 | if (wp_script_is($script_handle, 'enqueued') || wp_script_is($script_handle, 'registered')) { |
| 107 | wp_localize_script($script_handle, 'embedpressPreviewData', $localization_data); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | wp_localize_script($script_handle, 'embedpressAdminParams', [ |
| 112 | 'ajaxUrl' => admin_url('admin-ajax.php'), |
| 113 | 'nonce' => wp_create_nonce('embedpress') |
| 114 | ]); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Setup license script localization |
| 119 | */ |
| 120 | private static function setup_license_localization() |
| 121 | { |
| 122 | $script_handle = 'embedpress-admin'; |
| 123 | |
| 124 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | $item_id = defined('EMBEDPRESS_SL_ITEM_ID') ? EMBEDPRESS_SL_ITEM_ID : 'embedpress'; |
| 129 | |
| 130 | wp_localize_script($script_handle, 'embedpressLicenseData', [ |
| 131 | 'nonce' => wp_create_nonce('wpdeveloper_sl_' . $item_id . '_nonce') |
| 132 | ]); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Setup feature notices script localization |
| 137 | */ |
| 138 | private static function setup_feature_notices_localization() |
| 139 | { |
| 140 | $script_handle = 'embedpress-feature-notices'; |
| 141 | |
| 142 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | wp_localize_script($script_handle, 'embedpressFeatureNotices', [ |
| 147 | 'ajaxurl' => admin_url('admin-ajax.php'), |
| 148 | 'nonce' => wp_create_nonce('embedpress_feature_notice'), |
| 149 | ]); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Setup Gutenberg blocks localization (embedpressObj variable) |
| 154 | */ |
| 155 | private static function setup_gutenberg_localization() |
| 156 | { |
| 157 | $script_handle = 'embedpress-blocks-editor'; |
| 158 | |
| 159 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | // Ensure required constants are defined |
| 164 | if (!defined('EMBEDPRESS_PLG_NAME')) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | $elements = (array) get_option(EMBEDPRESS_PLG_NAME . ":elements", []); |
| 169 | $active_blocks = isset($elements['gutenberg']) ? (array) $elements['gutenberg'] : []; |
| 170 | |
| 171 | $wistia_labels = self::get_wistia_labels(); |
| 172 | $wistia_options = self::get_wistia_options(); |
| 173 | $pars_url = wp_parse_url(get_site_url()); |
| 174 | $documents_cta_options = (array) get_option(EMBEDPRESS_PLG_NAME . ':document'); |
| 175 | $current_user = wp_get_current_user(); |
| 176 | $assets_url = defined('EMBEDPRESS_URL_ASSETS') ? EMBEDPRESS_URL_ASSETS : ''; |
| 177 | $static_url = defined('EMBEDPRESS_URL_STATIC') ? EMBEDPRESS_URL_STATIC : ''; |
| 178 | |
| 179 | // Get global powered_by setting |
| 180 | $g_settings = get_option(EMBEDPRESS_PLG_NAME, []); |
| 181 | $powered_by_default = isset($g_settings['embedpress_document_powered_by']) && $g_settings['embedpress_document_powered_by'] === 'yes'; |
| 182 | $lazy_load_default = isset($g_settings['g_lazyload']) && $g_settings['g_lazyload'] == 1; |
| 183 | |
| 184 | wp_localize_script($script_handle, 'embedpressGutenbergData', [ |
| 185 | |
| 186 | |
| 187 | // Keep only the variables that are actually used in JavaScript |
| 188 | 'wistiaLabels' => json_encode($wistia_labels), |
| 189 | 'wistiaOptions' => $wistia_options, |
| 190 | 'poweredBy' => apply_filters('embedpress_document_block_powered_by', $powered_by_default), |
| 191 | 'isProVersion' => defined('EMBEDPRESS_PRO_PLUGIN_FILE'), |
| 192 | 'twitchHost' => !empty($pars_url['host']) ? $pars_url['host'] : '', |
| 193 | 'twitchSettings' => self::get_twitch_settings(), |
| 194 | 'siteUrl' => site_url(), |
| 195 | 'activeBlocks' => $active_blocks, |
| 196 | 'documentCta' => $documents_cta_options, |
| 197 | 'pdfRenderer' => Helper::get_pdf_renderer(), |
| 198 | 'flipbookRenderer' => Helper::get_flipbook_renderer(), |
| 199 | 'isProPluginActive' => defined('EMBEDPRESS_SL_ITEM_SLUG'), |
| 200 | 'ajaxUrl' => admin_url('admin-ajax.php'), |
| 201 | 'adminUrl' => admin_url(), |
| 202 | 'sourceNonce' => wp_create_nonce('source_nonce_embedpress'), |
| 203 | 'canUploadMedia' => current_user_can('upload_files'), |
| 204 | 'pdfGalleryNonce' => wp_create_nonce('ep_pdf_gallery_nonce'), |
| 205 | 'assetsUrl' => $assets_url, |
| 206 | 'staticUrl' => $static_url, |
| 207 | // Use underscore naming for consistency with block attributes |
| 208 | 'iframe_width' => Helper::get_options_value('enableEmbedResizeWidth', '600'), |
| 209 | 'iframe_height' => Helper::get_options_value('enableEmbedResizeHeight', '400'), |
| 210 | 'iframeWidth' => Helper::get_options_value('enableEmbedResizeWidth', '600'), // Keep camelCase for backward compatibility |
| 211 | 'iframeHeight' => Helper::get_options_value('enableEmbedResizeHeight', '400'), // Keep camelCase for backward compatibility |
| 212 | 'pdfCustomColor' => Helper::get_options_value('custom_color', '#403A81'), |
| 213 | 'lazyLoad' => $lazy_load_default, |
| 214 | 'brandingLogos' => [ |
| 215 | 'youtube' => Helper::get_branding_value('logo_url', 'youtube'), |
| 216 | 'vimeo' => Helper::get_branding_value('logo_url', 'vimeo'), |
| 217 | 'wistia' => Helper::get_branding_value('logo_url', 'wistia'), |
| 218 | 'twitch' => Helper::get_branding_value('logo_url', 'twitch'), |
| 219 | 'dailymotion' => Helper::get_branding_value('logo_url', 'dailymotion'), |
| 220 | 'document' => Helper::get_branding_value('logo_url', 'document'), |
| 221 | ], |
| 222 | 'userRoles' => Helper::get_user_roles(), |
| 223 | 'currentUser' => $current_user->data, |
| 224 | 'feedbackSubmitted' => get_option('embedpress_feedback_submited'), |
| 225 | 'ratingHelpDisabled' => Helper::get_options_value('turn_off_rating_help', false), |
| 226 | 'milestoneDisabled' => Helper::get_options_value('turn_off_milestone', false), |
| 227 | |
| 228 | // Legacy support |
| 229 | 'wistia_labels' => json_encode($wistia_labels), |
| 230 | 'wisita_options' => $wistia_options, |
| 231 | 'embedpress_powered_by' => apply_filters('embedpress_document_block_powered_by', $powered_by_default), |
| 232 | 'embedpress_pro' => defined('EMBEDPRESS_PRO_PLUGIN_FILE'), |
| 233 | 'twitch_host' => !empty($pars_url['host']) ? $pars_url['host'] : '', |
| 234 | 'site_url' => site_url(), |
| 235 | 'rest_url' => get_rest_url(), |
| 236 | 'embedpress_rest_url' => get_rest_url(null, 'embedpress/v1/oembed/embedpress'), |
| 237 | 'active_blocks' => $active_blocks, |
| 238 | 'document_cta' => $documents_cta_options, |
| 239 | 'pdf_renderer' => Helper::get_pdf_renderer(), |
| 240 | 'flipbook_renderer' => Helper::get_flipbook_renderer(), |
| 241 | 'is_pro_plugin_active' => defined('EMBEDPRESS_SL_ITEM_SLUG'), |
| 242 | 'ajaxurl' => admin_url('admin-ajax.php'), |
| 243 | 'source_nonce' => wp_create_nonce('source_nonce_embedpress'), |
| 244 | 'can_upload_media' => current_user_can('upload_files'), |
| 245 | 'permalink_structure' => get_option('permalink_structure'), |
| 246 | 'EMBEDPRESS_URL_ASSETS' => EMBEDPRESS_URL_ASSETS, |
| 247 | 'iframe_width' => Helper::get_options_value('enableEmbedResizeWidth'), |
| 248 | 'iframe_height' => Helper::get_options_value('enableEmbedResizeHeight'), |
| 249 | 'pdf_custom_color' => Helper::get_options_value('custom_color'), |
| 250 | 'pdf_custom_color' => Helper::get_options_value('custom_color'), |
| 251 | 'youtube_brand_logo_url' => Helper::get_branding_value('logo_url', 'youtube'), |
| 252 | 'vimeo_brand_logo_url' => Helper::get_branding_value('logo_url', 'vimeo'), |
| 253 | 'wistia_brand_logo_url' => Helper::get_branding_value('logo_url', 'wistia'), |
| 254 | 'twitch_brand_logo_url' => Helper::get_branding_value('logo_url', 'twitch'), |
| 255 | 'dailymotion_brand_logo_url' => Helper::get_branding_value('logo_url', 'dailymotion'), |
| 256 | 'user_roles' => Helper::get_user_roles(), |
| 257 | 'current_user' => $current_user->data, |
| 258 | 'is_embedpress_feedback_submited' => get_option('embedpress_feedback_submited'), |
| 259 | 'turn_off_rating_help' => Helper::get_options_value('turn_off_rating_help'), |
| 260 | 'turn_off_milestone' => Helper::get_options_value('turn_off_milestone'), |
| 261 | ]); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Setup frontend script localization (embedpressFrontendData variable) |
| 266 | */ |
| 267 | private static function setup_frontend_script_localization() |
| 268 | { |
| 269 | // The embedpressFrontendData variable should be attached to multiple frontend scripts |
| 270 | $script_handles = ['embedpress-front', 'embedpress-ads']; |
| 271 | |
| 272 | $localization_data = [ |
| 273 | 'ajaxurl' => admin_url('admin-ajax.php'), |
| 274 | 'isProPluginActive' => defined('EMBEDPRESS_SL_ITEM_SLUG'), |
| 275 | 'nonce' => wp_create_nonce('ep_nonce'), |
| 276 | ]; |
| 277 | |
| 278 | foreach ($script_handles as $script_handle) { |
| 279 | if (wp_script_is($script_handle, 'enqueued') || wp_script_is($script_handle, 'registered')) { |
| 280 | wp_localize_script($script_handle, 'embedpressFrontendData', $localization_data); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Setup settings page localization (attached to admin script) |
| 287 | */ |
| 288 | private static function setup_settings_localization() |
| 289 | { |
| 290 | $script_handle = 'embedpress-admin'; |
| 291 | |
| 292 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | wp_localize_script($script_handle, 'embedpressSettingsData', [ |
| 297 | 'nonce' => wp_create_nonce('embedpress_elements_action'), |
| 298 | 'ajaxNonce' => wp_create_nonce('embedpress_ajax_nonce'), |
| 299 | ]); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Setup onboarding wizard localization |
| 304 | * |
| 305 | * @param string $hook Current admin page hook |
| 306 | */ |
| 307 | private static function setup_onboarding_localization($hook) |
| 308 | { |
| 309 | if (strpos($hook, 'embedpress-onboarding') === false) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | $script_handle = 'embedpress-onboarding'; |
| 314 | |
| 315 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | $settings = (array) get_option(EMBEDPRESS_PLG_NAME, []); |
| 320 | $elements = (array) get_option(EMBEDPRESS_PLG_NAME . ':elements', []); |
| 321 | $pro_active = apply_filters('embedpress/is_allow_rander', false); |
| 322 | |
| 323 | wp_localize_script($script_handle, 'embedpressOnboardingData', [ |
| 324 | 'ajaxUrl' => admin_url('admin-ajax.php'), |
| 325 | 'nonce' => wp_create_nonce('embedpress_onboarding_nonce'), |
| 326 | 'settingsUrl' => admin_url('admin.php?page=embedpress&page_type=settings'), |
| 327 | 'dashboardUrl' => admin_url('admin.php?page=embedpress'), |
| 328 | 'proActive' => $pro_active, |
| 329 | 'upgradeUrl' => 'https://wpdeveloper.com/in/upgrade-embedpress', |
| 330 | 'settings' => $settings, |
| 331 | 'elements' => $elements, |
| 332 | 'assetsUrl' => EMBEDPRESS_URL_ASSETS, |
| 333 | ]); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Setup new blocks localization (for new block system) |
| 338 | */ |
| 339 | private static function setup_new_blocks_localization() |
| 340 | { |
| 341 | // Try multiple possible handles for new blocks |
| 342 | $possible_handles = [ |
| 343 | 'embedpress-blocks-editor', // Current handle from AssetManager |
| 344 | 'embedpress-blocks', // Old handle |
| 345 | 'embedpress_blocks-cgb-block-js', // Legacy handle |
| 346 | 'embedpress-blocks-js', // Alternative handle |
| 347 | ]; |
| 348 | |
| 349 | $script_handle = null; |
| 350 | foreach ($possible_handles as $handle) { |
| 351 | if (wp_script_is($handle, 'enqueued') || wp_script_is($handle, 'registered')) { |
| 352 | $script_handle = $handle; |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (!$script_handle) { |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | // Ensure required constants are defined |
| 362 | if (!defined('EMBEDPRESS_PLG_NAME')) { |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | $elements = (array) get_option(EMBEDPRESS_PLG_NAME . ":elements", []); |
| 367 | $active_blocks = isset($elements['gutenberg']) ? (array) $elements['gutenberg'] : []; |
| 368 | |
| 369 | wp_localize_script($script_handle, 'embedpressNewBlocksData', [ |
| 370 | 'pluginDirPath' => defined('EMBEDPRESS_PATH_BASE') ? EMBEDPRESS_PATH_BASE : '', |
| 371 | 'pluginDirUrl' => defined('EMBEDPRESS_URL_STATIC') ? EMBEDPRESS_URL_STATIC . '../' : '', |
| 372 | 'activeBlocks' => $active_blocks, |
| 373 | 'canUploadMedia' => current_user_can('upload_files'), |
| 374 | 'ajaxUrl' => admin_url('admin-ajax.php'), |
| 375 | 'nonce' => wp_create_nonce('embedpress_nonce'), |
| 376 | 'restUrl' => rest_url('embedpress/v1/'), |
| 377 | 'siteUrl' => site_url(), |
| 378 | ]); |
| 379 | } |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | /** |
| 386 | * Setup analytics tracker localization |
| 387 | */ |
| 388 | private static function setup_analytics_localization() |
| 389 | { |
| 390 | $script_handle = 'embedpress-analytics-tracker'; |
| 391 | |
| 392 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | // Get analytics manager instance to check for embedded content |
| 397 | $has_embedded_content = false; |
| 398 | if (class_exists('EmbedPress\Includes\Classes\Analytics\Analytics_Manager')) { |
| 399 | $analytics_manager = \EmbedPress\Includes\Classes\Analytics\Analytics_Manager::get_instance(); |
| 400 | $has_embedded_content = $analytics_manager->has_embedded_content(); |
| 401 | } |
| 402 | |
| 403 | // Get tracking enabled setting |
| 404 | $tracking_enabled = get_option('embedpress_analytics_tracking_enabled', true); |
| 405 | |
| 406 | // Get original referrer if available |
| 407 | $original_referrer = ''; |
| 408 | if (defined('EMBEDPRESS_ORIGINAL_REFERRER') && !empty(EMBEDPRESS_ORIGINAL_REFERRER)) { |
| 409 | $original_referrer = EMBEDPRESS_ORIGINAL_REFERRER; |
| 410 | } |
| 411 | |
| 412 | // Get session ID safely |
| 413 | $session_id = self::get_analytics_session_id(); |
| 414 | |
| 415 | wp_localize_script($script_handle, 'embedpress_analytics', [ |
| 416 | 'ajax_url' => admin_url('admin-ajax.php'), |
| 417 | 'rest_url' => rest_url('embedpress/v1/analytics/'), |
| 418 | 'nonce' => wp_create_nonce('wp_rest'), |
| 419 | 'session_id' => $session_id, |
| 420 | 'page_url' => get_permalink(), |
| 421 | 'post_id' => get_the_ID(), |
| 422 | 'tracking_enabled' => (bool) $tracking_enabled, |
| 423 | 'original_referrer' => $original_referrer, |
| 424 | 'has_embedded_content' => $has_embedded_content |
| 425 | ]); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Setup Google Calendar widget localization |
| 430 | */ |
| 431 | private static function setup_calendar_widget_localization() |
| 432 | { |
| 433 | $script_handle = 'epgc'; |
| 434 | |
| 435 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | $nonce = wp_create_nonce('epgc_nonce'); |
| 440 | wp_localize_script($script_handle, 'embedpressCalendarData', [ |
| 441 | 'ajaxUrl' => admin_url('admin-ajax.php'), |
| 442 | 'nonce' => $nonce, |
| 443 | 'translations' => [ |
| 444 | 'allDay' => __('All day', 'embedpress'), |
| 445 | 'createdBy' => __('Created by', 'embedpress'), |
| 446 | 'goToEvent' => __('Go to event', 'embedpress'), |
| 447 | 'unknownError' => __('Unknown error', 'embedpress'), |
| 448 | 'requestError' => __('Request error', 'embedpress'), |
| 449 | 'loading' => __('Loading', 'embedpress') |
| 450 | ] |
| 451 | ]); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Load plugin text domain for translations |
| 456 | */ |
| 457 | public static function load_text_domain() |
| 458 | { |
| 459 | $locale = determine_locale(); |
| 460 | $locale = apply_filters('plugin_locale', $locale, 'embedpress'); |
| 461 | |
| 462 | // Load from WordPress languages directory first |
| 463 | if (file_exists(WP_LANG_DIR . "/embedpress-" . $locale . '.mo')) { |
| 464 | unload_textdomain('embedpress'); |
| 465 | load_textdomain('embedpress', WP_LANG_DIR . "/embedpress-" . $locale . '.mo'); |
| 466 | } |
| 467 | |
| 468 | // Load from plugin languages directory |
| 469 | load_plugin_textdomain('embedpress', false, plugin_basename(dirname(EMBEDPRESS_FILE)) . '/languages'); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Get Wistia labels for localization |
| 474 | * |
| 475 | * @return array |
| 476 | */ |
| 477 | private static function get_wistia_labels() |
| 478 | { |
| 479 | return [ |
| 480 | 'watch_from_beginning' => __('Watch from the beginning', 'embedpress'), |
| 481 | 'skip_to_where_you_left_off' => __('Skip to where you left off', 'embedpress'), |
| 482 | 'you_have_watched_it_before' => __('It looks like you\'ve watched<br />part of this video before!', 'embedpress'), |
| 483 | ]; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Get Wistia options safely |
| 488 | * |
| 489 | * @return mixed|null |
| 490 | */ |
| 491 | private static function get_wistia_options() |
| 492 | { |
| 493 | if (!function_exists('embedpress_wisita_pro_get_options')) { |
| 494 | return null; |
| 495 | } |
| 496 | |
| 497 | try { |
| 498 | // return embedpress_wisita_pro_get_options(); |
| 499 | } catch (\Exception $e) { |
| 500 | // Silently fail if function throws an error |
| 501 | return null; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Get branding value safely |
| 507 | * |
| 508 | * @param string $type |
| 509 | * @param string $provider |
| 510 | * @return string |
| 511 | */ |
| 512 | private static function get_branding_value($type, $provider) |
| 513 | { |
| 514 | if (!function_exists('get_branding_value')) { |
| 515 | return ''; |
| 516 | } |
| 517 | |
| 518 | try { |
| 519 | return self::get_branding_value($type, $provider); |
| 520 | } catch (\Exception $e) { |
| 521 | return ''; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Get analytics session ID safely |
| 527 | * |
| 528 | * @return string |
| 529 | */ |
| 530 | private static function get_analytics_session_id() |
| 531 | { |
| 532 | // Prefer cookie-based session IDs to avoid server PHP session configuration issues |
| 533 | if (isset($_COOKIE['ep_session_id'])) { |
| 534 | $cookie = $_COOKIE['ep_session_id']; |
| 535 | // Allow only safe characters and a minimum length |
| 536 | if (is_string($cookie) && preg_match('/^[A-Za-z0-9._:-]{8,}$/', $cookie)) { |
| 537 | return sanitize_text_field($cookie); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | // Generate a new ephemeral session ID |
| 542 | $id = 'ep-sess-' . time() . '-' . wp_generate_password(8, false); |
| 543 | |
| 544 | // Set a session cookie (expires when the browser closes) |
| 545 | if (!headers_sent()) { |
| 546 | $path = defined('COOKIEPATH') ? COOKIEPATH : '/'; |
| 547 | $domain = (defined('COOKIE_DOMAIN') && COOKIE_DOMAIN) ? COOKIE_DOMAIN : ''; |
| 548 | $secure = is_ssl(); |
| 549 | $httponly = true; |
| 550 | setcookie('ep_session_id', $id, 0, $path, $domain, $secure, $httponly); |
| 551 | } |
| 552 | |
| 553 | return $id; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Get URL schemes for preview script |
| 558 | * |
| 559 | * @return array |
| 560 | */ |
| 561 | private static function get_url_schemes() |
| 562 | { |
| 563 | return [ |
| 564 | // Apple podcasts |
| 565 | 'podcasts.apple.com/*', |
| 566 | // YouTube |
| 567 | 'youtube.com/watch\\?*', |
| 568 | 'youtube.com/playlist\\?*', |
| 569 | 'youtube.com/channel/*', |
| 570 | 'youtube.com/c/*', |
| 571 | 'youtube.com/user/*', |
| 572 | 'youtube.com/(\w+)[^?\/]*$', |
| 573 | // Vimeo |
| 574 | 'vimeo.com/*', |
| 575 | 'vimeo.com/groups/*/videos/*', |
| 576 | |
| 577 | 'twitter.com/*/status/*', |
| 578 | 'twitter.com/i/moments/*', |
| 579 | 'twitter.com/*/timelines/*', |
| 580 | |
| 581 | 'facebook.com/*', |
| 582 | 'fb.watch/*', |
| 583 | |
| 584 | 'instagram.com/p/*', |
| 585 | 'instagr.am/p/*', |
| 586 | // SoundCloud |
| 587 | 'soundcloud.com/*', |
| 588 | // Twitch |
| 589 | '*.twitch.tv/*', |
| 590 | 'twitch.tv/*', |
| 591 | // Wistia |
| 592 | '*.wistia.com/medias/*', |
| 593 | 'fast.wistia.com/embed/medias/*.jsonp', |
| 594 | // Google services |
| 595 | 'google.com/*', |
| 596 | 'google.com.*/*', |
| 597 | 'google.co.*/*', |
| 598 | 'maps.google.com/*', |
| 599 | 'docs.google.com/presentation/*', |
| 600 | 'docs.google.com/document/*', |
| 601 | 'docs.google.com/spreadsheets/*', |
| 602 | 'docs.google.com/forms/*', |
| 603 | 'docs.google.com/drawings/*', |
| 604 | ]; |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Setup PDF gallery frontend localization |
| 609 | */ |
| 610 | private static function setup_pdf_gallery_localization() |
| 611 | { |
| 612 | $script_handle = 'embedpress-pdf-gallery'; |
| 613 | |
| 614 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | $plugin_url = defined('EMBEDPRESS_URL_ASSETS') ? str_replace('assets/', '', EMBEDPRESS_URL_ASSETS) : ''; |
| 619 | |
| 620 | wp_localize_script($script_handle, 'embedpressObj', [ |
| 621 | 'pdfRenderer' => Helper::get_pdf_renderer(), |
| 622 | 'flipbookRenderer' => Helper::get_flipbook_renderer(), |
| 623 | 'pluginUrl' => $plugin_url, |
| 624 | ]); |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Setup PDF lightbox frontend localization |
| 629 | */ |
| 630 | private static function setup_pdf_lightbox_localization() |
| 631 | { |
| 632 | $script_handle = 'embedpress-pdf-lightbox'; |
| 633 | |
| 634 | if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) { |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | // Only localize if not already done by gallery |
| 639 | if (wp_script_is('embedpress-pdf-gallery', 'enqueued')) { |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | $plugin_url = defined('EMBEDPRESS_URL_ASSETS') ? str_replace('assets/', '', EMBEDPRESS_URL_ASSETS) : ''; |
| 644 | |
| 645 | wp_localize_script($script_handle, 'embedpressObj', [ |
| 646 | 'pdfRenderer' => Helper::get_pdf_renderer(), |
| 647 | 'flipbookRenderer' => Helper::get_flipbook_renderer(), |
| 648 | 'pluginUrl' => $plugin_url, |
| 649 | ]); |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Initialize localization manager hooks |
| 654 | */ |
| 655 | /** |
| 656 | * Get Twitch settings for Gutenberg localization |
| 657 | */ |
| 658 | private static function get_twitch_settings() |
| 659 | { |
| 660 | $twitch_settings = get_option(EMBEDPRESS_PLG_NAME . ':twitch', []); |
| 661 | |
| 662 | return [ |
| 663 | 'autoplay' => isset($twitch_settings['embedpress_pro_twitch_autoplay']) ? $twitch_settings['embedpress_pro_twitch_autoplay'] : 'no', |
| 664 | 'mute' => isset($twitch_settings['embedpress_pro_twitch_mute']) ? $twitch_settings['embedpress_pro_twitch_mute'] : 'yes', |
| 665 | 'theme' => isset($twitch_settings['embedpress_pro_twitch_theme']) ? $twitch_settings['embedpress_pro_twitch_theme'] : 'dark', |
| 666 | 'fullscreen' => isset($twitch_settings['embedpress_pro_fs']) ? $twitch_settings['embedpress_pro_fs'] : 'yes', |
| 667 | 'chat' => isset($twitch_settings['embedpress_pro_twitch_chat']) ? $twitch_settings['embedpress_pro_twitch_chat'] : 'no', |
| 668 | 'startTime' => isset($twitch_settings['start_time']) ? intval($twitch_settings['start_time']) : 0, |
| 669 | ]; |
| 670 | } |
| 671 | |
| 672 | public static function init() |
| 673 | { |
| 674 | // Load text domain early |
| 675 | add_action('plugins_loaded', [__CLASS__, 'load_text_domain'], 1); |
| 676 | } |
| 677 | } |
| 678 |