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