PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 4.4.1
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v4.4.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 9 months ago LocalizationManager.php 9 months ago init.php 9 months ago
LocalizationManager.php
490 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 self::setup_analytics_localization();
57 }
58
59 /**
60 * Setup Elementor widget localization
61 */
62 public static function setup_elementor_localization()
63 {
64 self::setup_frontend_script_localization();
65 self::setup_calendar_widget_localization();
66 self::setup_analytics_localization();
67 }
68
69 /**
70 * Setup preview localization (attached to admin script)
71 */
72 private static function setup_preview_localization()
73 {
74 $script_handle = 'embedpress-admin';
75
76 if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) {
77 return;
78 }
79
80 $url_schemes = apply_filters('embedpress:getAdditionalURLSchemes', self::get_url_schemes());
81
82 // Ensure required constants are defined with fallbacks
83 $version = defined('EMBEDPRESS_VERSION') ? EMBEDPRESS_VERSION : '1.0.0';
84 $shortcode = defined('EMBEDPRESS_SHORTCODE') ? EMBEDPRESS_SHORTCODE : 'embedpress';
85 $assets_url = defined('EMBEDPRESS_URL_ASSETS') ? EMBEDPRESS_URL_ASSETS : '';
86
87 wp_localize_script($script_handle, 'embedpressPreviewData', [
88 'previewSettings' => [
89 'baseUrl' => get_site_url() . '/',
90 'versionUID' => $version,
91 'debug' => defined('WP_DEBUG') && WP_DEBUG,
92 ],
93 'shortcode' => $shortcode,
94 'assetsUrl' => $assets_url,
95 'urlSchemes' => $url_schemes,
96 ]);
97
98 wp_localize_script($script_handle, 'embedpressAdminParams', [
99 'ajaxUrl' => admin_url('admin-ajax.php'),
100 'nonce' => wp_create_nonce('embedpress')
101 ]);
102 }
103
104 /**
105 * Setup license script localization
106 */
107 private static function setup_license_localization()
108 {
109 $script_handle = 'embedpress-admin';
110
111 if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) {
112 return;
113 }
114
115 $item_id = defined('EMBEDPRESS_SL_ITEM_ID') ? EMBEDPRESS_SL_ITEM_ID : 'embedpress';
116
117 wp_localize_script($script_handle, 'embedpressLicenseData', [
118 'nonce' => wp_create_nonce('wpdeveloper_sl_' . $item_id . '_nonce')
119 ]);
120 }
121
122 /**
123 * Setup Gutenberg blocks localization (embedpressObj variable)
124 */
125 private static function setup_gutenberg_localization()
126 {
127 $script_handle = 'embedpress-blocks-editor';
128
129 if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) {
130 return;
131 }
132
133 // Ensure required constants are defined
134 if (!defined('EMBEDPRESS_PLG_NAME')) {
135 return;
136 }
137
138 $elements = (array) get_option(EMBEDPRESS_PLG_NAME . ":elements", []);
139 $active_blocks = isset($elements['gutenberg']) ? (array) $elements['gutenberg'] : [];
140
141 $wistia_labels = self::get_wistia_labels();
142 $wistia_options = self::get_wistia_options();
143 $pars_url = wp_parse_url(get_site_url());
144 $documents_cta_options = (array) get_option(EMBEDPRESS_PLG_NAME . ':document');
145 $current_user = wp_get_current_user();
146 $assets_url = defined('EMBEDPRESS_URL_ASSETS') ? EMBEDPRESS_URL_ASSETS : '';
147 $static_url = defined('EMBEDPRESS_URL_STATIC') ? EMBEDPRESS_URL_STATIC : '';
148
149 wp_localize_script($script_handle, 'embedpressGutenbergData', [
150 'wistiaLabels' => json_encode($wistia_labels),
151 'wistiaOptions' => $wistia_options,
152 'poweredBy' => apply_filters('embedpress_document_block_powered_by', true),
153 'isProVersion' => defined('EMBEDPRESS_PRO_PLUGIN_FILE'),
154 'twitchHost' => !empty($pars_url['host']) ? $pars_url['host'] : '',
155 'siteUrl' => site_url(),
156 'activeBlocks' => $active_blocks,
157 'documentCta' => $documents_cta_options,
158 'pdfRenderer' => Helper::get_pdf_renderer(),
159 'isProPluginActive' => defined('EMBEDPRESS_SL_ITEM_SLUG'),
160 'ajaxUrl' => admin_url('admin-ajax.php'),
161 'sourceNonce' => wp_create_nonce('source_nonce_embedpress'),
162 'canUploadMedia' => current_user_can('upload_files'),
163 'assetsUrl' => $assets_url,
164 'staticUrl' => $static_url,
165 'iframeWidth' => Helper::get_options_value('enableEmbedResizeWidth', '600'),
166 'iframeHeight' => Helper::get_options_value('enableEmbedResizeHeight', '400'),
167 'pdfCustomColor' => Helper::get_options_value('custom_color', '#403A81'),
168 'brandingLogos' => [
169 'youtube' => Helper::get_branding_value('logo_url', 'youtube'),
170 'vimeo' => Helper::get_branding_value('logo_url', 'vimeo'),
171 'wistia' => Helper::get_branding_value('logo_url', 'wistia'),
172 'twitch' => Helper::get_branding_value('logo_url', 'twitch'),
173 'dailymotion' => Helper::get_branding_value('logo_url', 'dailymotion'),
174 ],
175 'userRoles' => Helper::get_user_roles(),
176 'currentUser' => $current_user->data,
177 'feedbackSubmitted' => get_option('embedpress_feedback_submited'),
178 'ratingHelpDisabled' => Helper::get_options_value('turn_off_rating_help', false),
179 ]);
180 }
181
182 /**
183 * Setup frontend script localization (embedpressFrontendData variable)
184 */
185 private static function setup_frontend_script_localization()
186 {
187 // The embedpressFrontendData variable should be attached to multiple frontend scripts
188 $script_handles = ['embedpress-front', 'embedpress-ads'];
189
190 $localization_data = [
191 'ajaxurl' => admin_url('admin-ajax.php'),
192 'isProPluginActive' => defined('EMBEDPRESS_SL_ITEM_SLUG'),
193 'nonce' => wp_create_nonce('ep_nonce'),
194 ];
195
196 foreach ($script_handles as $script_handle) {
197 if (wp_script_is($script_handle, 'enqueued') || wp_script_is($script_handle, 'registered')) {
198 wp_localize_script($script_handle, 'embedpressFrontendData', $localization_data);
199 }
200 }
201 }
202
203 /**
204 * Setup settings page localization (attached to admin script)
205 */
206 private static function setup_settings_localization()
207 {
208 $script_handle = 'embedpress-admin';
209
210 if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) {
211 return;
212 }
213
214 wp_localize_script($script_handle, 'embedpressSettingsData', [
215 'nonce' => wp_create_nonce('embedpress_elements_action'),
216 ]);
217 }
218
219 /**
220 * Setup new blocks localization (for new block system)
221 */
222 private static function setup_new_blocks_localization()
223 {
224 // Try multiple possible handles for new blocks
225 $possible_handles = [
226 'embedpress-blocks-editor', // Current handle from AssetManager
227 'embedpress-blocks', // Old handle
228 'embedpress_blocks-cgb-block-js', // Legacy handle
229 'embedpress-blocks-js', // Alternative handle
230 ];
231
232 $script_handle = null;
233 foreach ($possible_handles as $handle) {
234 if (wp_script_is($handle, 'enqueued') || wp_script_is($handle, 'registered')) {
235 $script_handle = $handle;
236 break;
237 }
238 }
239
240 if (!$script_handle) {
241 return;
242 }
243
244 // Ensure required constants are defined
245 if (!defined('EMBEDPRESS_PLG_NAME')) {
246 return;
247 }
248
249 $elements = (array) get_option(EMBEDPRESS_PLG_NAME . ":elements", []);
250 $active_blocks = isset($elements['gutenberg']) ? (array) $elements['gutenberg'] : [];
251
252 wp_localize_script($script_handle, 'embedpressNewBlocksData', [
253 'pluginDirPath' => defined('EMBEDPRESS_PATH_BASE') ? EMBEDPRESS_PATH_BASE : '',
254 'pluginDirUrl' => defined('EMBEDPRESS_URL_STATIC') ? EMBEDPRESS_URL_STATIC . '../' : '',
255 'activeBlocks' => $active_blocks,
256 'canUploadMedia' => current_user_can('upload_files'),
257 'ajaxUrl' => admin_url('admin-ajax.php'),
258 'nonce' => wp_create_nonce('embedpress_nonce'),
259 'restUrl' => rest_url('embedpress/v1/'),
260 'siteUrl' => site_url(),
261 ]);
262 }
263
264
265
266
267
268 /**
269 * Setup analytics tracker localization
270 */
271 private static function setup_analytics_localization()
272 {
273 $script_handle = 'embedpress-analytics-tracker';
274
275 if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) {
276 return;
277 }
278
279 // Get analytics manager instance to check for embedded content
280 $has_embedded_content = false;
281 if (class_exists('EmbedPress\Includes\Classes\Analytics\Analytics_Manager')) {
282 $analytics_manager = \EmbedPress\Includes\Classes\Analytics\Analytics_Manager::get_instance();
283 $has_embedded_content = $analytics_manager->has_embedded_content();
284 }
285
286 // Get tracking enabled setting
287 $tracking_enabled = get_option('embedpress_analytics_tracking_enabled', true);
288
289 // Get original referrer if available
290 $original_referrer = '';
291 if (defined('EMBEDPRESS_ORIGINAL_REFERRER') && !empty(EMBEDPRESS_ORIGINAL_REFERRER)) {
292 $original_referrer = EMBEDPRESS_ORIGINAL_REFERRER;
293 }
294
295 // Get session ID safely
296 $session_id = self::get_analytics_session_id();
297
298 wp_localize_script($script_handle, 'embedpress_analytics', [
299 'ajax_url' => admin_url('admin-ajax.php'),
300 'rest_url' => rest_url('embedpress/v1/analytics/'),
301 'nonce' => wp_create_nonce('wp_rest'),
302 'session_id' => $session_id,
303 'page_url' => get_permalink(),
304 'post_id' => get_the_ID(),
305 'tracking_enabled' => (bool) $tracking_enabled,
306 'original_referrer' => $original_referrer,
307 'has_embedded_content' => $has_embedded_content
308 ]);
309 }
310
311 /**
312 * Setup Google Calendar widget localization
313 */
314 private static function setup_calendar_widget_localization()
315 {
316 $script_handle = 'epgc';
317
318 if (!wp_script_is($script_handle, 'enqueued') && !wp_script_is($script_handle, 'registered')) {
319 return;
320 }
321
322 $nonce = wp_create_nonce('epgc_nonce');
323 wp_localize_script($script_handle, 'embedpressCalendarData', [
324 'ajaxUrl' => admin_url('admin-ajax.php'),
325 'nonce' => $nonce,
326 'translations' => [
327 'allDay' => __('All day', 'embedpress'),
328 'createdBy' => __('Created by', 'embedpress'),
329 'goToEvent' => __('Go to event', 'embedpress'),
330 'unknownError' => __('Unknown error', 'embedpress'),
331 'requestError' => __('Request error', 'embedpress'),
332 'loading' => __('Loading', 'embedpress')
333 ]
334 ]);
335 }
336
337 /**
338 * Load plugin text domain for translations
339 */
340 public static function load_text_domain()
341 {
342 $locale = determine_locale();
343 $locale = apply_filters('plugin_locale', $locale, 'embedpress');
344
345 // Load from WordPress languages directory first
346 if (file_exists(WP_LANG_DIR . "/embedpress-" . $locale . '.mo')) {
347 unload_textdomain('embedpress');
348 load_textdomain('embedpress', WP_LANG_DIR . "/embedpress-" . $locale . '.mo');
349 }
350
351 // Load from plugin languages directory
352 load_plugin_textdomain('embedpress', false, plugin_basename(dirname(EMBEDPRESS_FILE)) . '/languages');
353 }
354
355 /**
356 * Get Wistia labels for localization
357 *
358 * @return array
359 */
360 private static function get_wistia_labels()
361 {
362 return [
363 'watch_from_beginning' => __('Watch from the beginning', 'embedpress'),
364 'skip_to_where_you_left_off' => __('Skip to where you left off', 'embedpress'),
365 'you_have_watched_it_before' => __('It looks like you\'ve watched<br />part of this video before!', 'embedpress'),
366 ];
367 }
368
369 /**
370 * Get Wistia options safely
371 *
372 * @return mixed|null
373 */
374 private static function get_wistia_options()
375 {
376 if (!function_exists('embedpress_wisita_pro_get_options')) {
377 return null;
378 }
379
380 try {
381 // return embedpress_wisita_pro_get_options();
382 } catch (\Exception $e) {
383 // Silently fail if function throws an error
384 return null;
385 }
386 }
387
388 /**
389 * Get branding value safely
390 *
391 * @param string $type
392 * @param string $provider
393 * @return string
394 */
395 private static function get_branding_value($type, $provider)
396 {
397 if (!function_exists('get_branding_value')) {
398 return '';
399 }
400
401 try {
402 return self::get_branding_value($type, $provider);
403 } catch (\Exception $e) {
404 return '';
405 }
406 }
407
408 /**
409 * Get analytics session ID safely
410 *
411 * @return string
412 */
413 private static function get_analytics_session_id()
414 {
415 // Only start session if we're in a web context and headers haven't been sent
416 if (!session_id() && !headers_sent() && !defined('WP_CLI')) {
417 session_start();
418 }
419
420 if (session_id() && !isset($_SESSION['embedpress_session_id'])) {
421 $_SESSION['embedpress_session_id'] = wp_generate_uuid4();
422 }
423
424 // Fallback to a generated session ID if session is not available
425 return isset($_SESSION['embedpress_session_id'])
426 ? $_SESSION['embedpress_session_id']
427 : 'session_' . wp_generate_uuid4();
428 }
429
430 /**
431 * Get URL schemes for preview script
432 *
433 * @return array
434 */
435 private static function get_url_schemes()
436 {
437 return [
438 // Apple podcasts
439 'podcasts.apple.com/*',
440 // YouTube
441 'youtube.com/watch\\?*',
442 'youtube.com/playlist\\?*',
443 'youtube.com/channel/*',
444 'youtube.com/c/*',
445 'youtube.com/user/*',
446 'youtube.com/(\w+)[^?\/]*$',
447 // Vimeo
448 'vimeo.com/*',
449 'vimeo.com/groups/*/videos/*',
450 // Twitter
451 'twitter.com/*/status/*',
452 'twitter.com/i/moments/*',
453 'twitter.com/*/timelines/*',
454 // Facebook
455 'facebook.com/*',
456 'fb.watch/*',
457 // Instagram
458 'instagram.com/p/*',
459 'instagr.am/p/*',
460 // SoundCloud
461 'soundcloud.com/*',
462 // Twitch
463 '*.twitch.tv/*',
464 'twitch.tv/*',
465 // Wistia
466 '*.wistia.com/medias/*',
467 'fast.wistia.com/embed/medias/*.jsonp',
468 // Google services
469 'google.com/*',
470 'google.com.*/*',
471 'google.co.*/*',
472 'maps.google.com/*',
473 'docs.google.com/presentation/*',
474 'docs.google.com/document/*',
475 'docs.google.com/spreadsheets/*',
476 'docs.google.com/forms/*',
477 'docs.google.com/drawings/*',
478 ];
479 }
480
481 /**
482 * Initialize localization manager hooks
483 */
484 public static function init()
485 {
486 // Load text domain early
487 add_action('plugins_loaded', [__CLASS__, 'load_text_domain'], 1);
488 }
489 }
490