API
2 years ago
Blocks
5 years ago
License
5 years ago
AdminNotice.php
5 years ago
AdminNotices.php
4 years ago
AjaxActions.php
2 years ago
Blocks.php
4 years ago
Compatibility.php
4 years ago
Menu.php
2 years ago
Migrations.php
5 years ago
Player.php
2 years ago
ProCompatibility.php
2 years ago
ReusableVideos.php
2 years ago
Scripts.php
2 years ago
Settings.php
4 years ago
Shortcodes.php
2 years ago
Streamer.php
4 years ago
Translation.php
2 years ago
VideoPostType.php
2 years ago
Scripts.php
467 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Services; |
| 4 | |
| 5 | use Error; |
| 6 | use PrestoPlayer\Plugin; |
| 7 | use PrestoPlayer\Models\Block; |
| 8 | use PrestoPlayer\Models\Setting; |
| 9 | use PrestoPlayer\WPackio\Enqueue; |
| 10 | |
| 11 | |
| 12 | class Scripts |
| 13 | { |
| 14 | /** |
| 15 | * Register scripts used throughout the plugin |
| 16 | * |
| 17 | * @return void |
| 18 | */ |
| 19 | public function register() |
| 20 | { |
| 21 | add_action('enqueue_block_assets', [$this, 'registerPrestoComponents']); |
| 22 | add_action('init', [$this, 'registerPrestoComponents']); |
| 23 | add_filter('script_loader_tag', [$this, 'prestoComponentsTag'], 10, 3); |
| 24 | |
| 25 | // block assets. |
| 26 | add_action('enqueue_block_editor_assets', [$this, 'blockEditorAssets']); |
| 27 | add_action('enqueue_block_assets', [$this, 'blockAssets']); |
| 28 | |
| 29 | // learndash. |
| 30 | add_action('admin_enqueue_scripts', [$this, 'learndashAdminScripts']); |
| 31 | |
| 32 | // elementor editor scripts |
| 33 | add_action('elementor/frontend/before_enqueue_scripts', [$this, 'elementorPreviewScripts']); |
| 34 | add_action('elementor/frontend/before_enqueue_scripts', [$this, 'blockAssets']); |
| 35 | |
| 36 | // admin pages |
| 37 | add_action("admin_print_scripts-presto-player_page_presto_license", [$this, 'licenseScripts']); |
| 38 | add_action("presto_player_pro_register_license_page", [$this, 'licenseScripts']); |
| 39 | |
| 40 | add_action('after_setup_theme', [$this, 'addAppearanceToolsSupport'], 99999); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Add support for Appearance Tools. |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | public function addAppearanceToolsSupport() |
| 49 | { |
| 50 | add_theme_support('appearance-tools'); |
| 51 | add_theme_support('border'); |
| 52 | } |
| 53 | |
| 54 | public function learndashAdminScripts($hook_suffix) |
| 55 | { |
| 56 | global $post_type; |
| 57 | |
| 58 | // must be on learndash page |
| 59 | if (!in_array($post_type, ['sfwd-lessons', 'sfwd-topic'])) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // must be on new post page |
| 64 | if (!in_array($hook_suffix, array('post.php', 'post-new.php'))) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $assets = include trailingslashit(PRESTO_PLAYER_PLUGIN_DIR) . 'dist/learndash.asset.php'; |
| 69 | wp_enqueue_script( |
| 70 | 'surecart/learndash/admin', |
| 71 | trailingslashit(PRESTO_PLAYER_PLUGIN_URL) . 'dist/learndash.js', |
| 72 | array_merge(['jquery'], $assets['dependencies']), |
| 73 | $assets['version'], |
| 74 | true |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Preload components to increase performance |
| 80 | */ |
| 81 | public function preloadComponents() |
| 82 | { |
| 83 | /** |
| 84 | * Get base file needed for components |
| 85 | */ |
| 86 | $file_contents = file_get_contents(PRESTO_PLAYER_PLUGIN_DIR . "dist/components/web-components/web-components.esm.js"); |
| 87 | preg_match('/\.\\/p\-(.*)\.js/', $file_contents, $matches); |
| 88 | |
| 89 | // get entry file |
| 90 | if (!empty($matches[0])) { |
| 91 | $file = str_replace('./', '', $matches[0]); |
| 92 | echo "<link rel='modulepreload' href='" . esc_url_raw(PRESTO_PLAYER_PLUGIN_URL . "dist/components/web-components/" . $file) . "' as='script' />\n"; // base |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get entry files |
| 97 | */ |
| 98 | $files = scandir(PRESTO_PLAYER_PLUGIN_DIR . "dist/components/web-components/"); |
| 99 | foreach ($files as $file) { |
| 100 | if (strpos($file, '.entry.js') !== false) { |
| 101 | echo "<link rel='modulepreload' href='" . esc_url_raw(PRESTO_PLAYER_PLUGIN_URL . "dist/components/web-components/" . $file) . "' as='script' />\n"; // list |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Web components loader |
| 107 | */ |
| 108 | echo "<link rel='modulepreload' href='" . esc_url_raw(PRESTO_PLAYER_PLUGIN_URL . "dist/components/web-components/web-components.esm.js") . "?ver=" . filemtime(PRESTO_PLAYER_PLUGIN_DIR . "dist/components/web-components/web-components.esm.js") . "' as='script' />"; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Add a type="module" to our components tag to lazy load them |
| 113 | */ |
| 114 | public function prestoComponentsTag($tag, $handle, $source) |
| 115 | { |
| 116 | if ('presto-components' === $handle) { |
| 117 | $tag = '<script src="' . $source . '" type="module" defer></script>'; |
| 118 | } |
| 119 | |
| 120 | return $tag; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Register our components |
| 125 | */ |
| 126 | public function registerPrestoComponents() |
| 127 | { |
| 128 | |
| 129 | $file = is_admin() || !Setting::get('performance', 'module_enabled') ? "src/player/player-static.js" : "dist/components/web-components/web-components.esm.js"; |
| 130 | |
| 131 | wp_register_script( |
| 132 | 'hls.js', |
| 133 | PRESTO_PLAYER_PLUGIN_URL . 'src/libraries/hls.min.js', |
| 134 | [], |
| 135 | '1.4.8', |
| 136 | true |
| 137 | ); |
| 138 | |
| 139 | $deps = [ |
| 140 | 'jquery', |
| 141 | 'wp-hooks', |
| 142 | 'wp-i18n', |
| 143 | ]; |
| 144 | |
| 145 | if (is_admin()) { |
| 146 | $deps[] = 'hls.js'; |
| 147 | } |
| 148 | |
| 149 | wp_register_script( |
| 150 | 'presto-components', |
| 151 | PRESTO_PLAYER_PLUGIN_URL . $file, |
| 152 | $deps, |
| 153 | filemtime(PRESTO_PLAYER_PLUGIN_DIR . $file), |
| 154 | true |
| 155 | ); |
| 156 | |
| 157 | wp_localize_script('presto-components', 'prestoComponents', [ |
| 158 | 'url' => PRESTO_PLAYER_PLUGIN_URL . "dist/components/web-components/web-components.esm.js?ver=" . filemtime(PRESTO_PLAYER_PLUGIN_DIR . "dist/components/web-components/web-components.esm.js") |
| 159 | ]); |
| 160 | |
| 161 | if (function_exists('wp_set_script_translations')) { |
| 162 | wp_set_script_translations('presto-components', 'presto-player'); |
| 163 | } |
| 164 | |
| 165 | wp_localize_script( |
| 166 | 'presto-components', |
| 167 | 'prestoPlayer', |
| 168 | apply_filters('presto-settings-block-js-options', [ |
| 169 | 'plugin_url' => esc_url_raw(trailingslashit(PRESTO_PLAYER_PLUGIN_URL)), |
| 170 | 'logged_in' => is_user_logged_in(), |
| 171 | 'root' => esc_url_raw(get_rest_url()), |
| 172 | 'nonce' => wp_create_nonce('wp_rest'), |
| 173 | 'ajaxurl' => admin_url('admin-ajax.php'), |
| 174 | 'isAdmin' => is_admin(), |
| 175 | 'isSetup' => [ |
| 176 | 'bunny' => false |
| 177 | ], |
| 178 | 'proVersion' => Plugin::proVersion(), |
| 179 | 'isPremium' => Plugin::isPro(), |
| 180 | 'wpVersionString' => 'wp/v2/', |
| 181 | 'prestoVersionString' => 'presto-player/v1/', |
| 182 | 'debug' => defined('SCRIPT_DEBUG') && SCRIPT_DEBUG, |
| 183 | 'debug_navigator' => defined('PRESTO_DEBUG_NAVIGATOR') && PRESTO_DEBUG_NAVIGATOR, |
| 184 | 'i18n' => Translation::geti18n(), |
| 185 | ]) |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Elementor scripts (needed speifically on preview pages) |
| 191 | */ |
| 192 | public function elementorPreviewScripts() |
| 193 | { |
| 194 | if (!isset($_GET['elementor-preview'])) { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | $assets = include trailingslashit(PRESTO_PLAYER_PLUGIN_DIR) . 'dist/elementor.asset.php'; |
| 199 | wp_enqueue_script( |
| 200 | 'surecart/elementor', |
| 201 | trailingslashit(PRESTO_PLAYER_PLUGIN_URL) . 'dist/elementor.js', |
| 202 | array_merge(['jquery', 'hls.js'], $assets['dependencies']), |
| 203 | $assets['version'], |
| 204 | true |
| 205 | ); |
| 206 | |
| 207 | wp_localize_script( |
| 208 | 'surecart/elementor', |
| 209 | 'prestoEditorData', |
| 210 | [ |
| 211 | 'proVersion' => Plugin::proVersion(), |
| 212 | 'isPremium' => Plugin::isPro(), |
| 213 | 'root' => esc_url_raw(get_rest_url()), |
| 214 | 'nonce' => wp_create_nonce('wp_rest'), |
| 215 | 'wpVersionString' => 'wp/v2/', |
| 216 | 'siteURL' => esc_url_raw(untrailingslashit(get_site_url(get_current_blog_id()))), |
| 217 | ] |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Block Editor Assets |
| 223 | * |
| 224 | * @return void |
| 225 | */ |
| 226 | public function blockEditorAssets() |
| 227 | { |
| 228 | if (!is_admin()) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | $assets = include trailingslashit(PRESTO_PLAYER_PLUGIN_DIR) . 'dist/blocks.asset.php'; |
| 233 | wp_enqueue_script( |
| 234 | 'surecart/blocks/admin', |
| 235 | trailingslashit(PRESTO_PLAYER_PLUGIN_URL) . 'dist/blocks.js', |
| 236 | array_merge(['presto-components', 'hls.js'], $assets['dependencies']), |
| 237 | $assets['version'], |
| 238 | true |
| 239 | ); |
| 240 | wp_enqueue_style('surecart/blocks/admin', trailingslashit(PRESTO_PLAYER_PLUGIN_URL) . 'dist/blocks.css', [], $assets['version']); |
| 241 | |
| 242 | wp_localize_script( |
| 243 | 'surecart/blocks/admin', |
| 244 | 'prestoPlayer', |
| 245 | apply_filters( |
| 246 | 'presto_player_admin_script_options', |
| 247 | [ |
| 248 | 'plugin_url' => esc_url_raw(trailingslashit(PRESTO_PLAYER_PLUGIN_URL)), |
| 249 | 'root' => esc_url_raw(get_rest_url()), |
| 250 | 'nonce' => wp_create_nonce('wp_rest'), |
| 251 | 'ajaxurl' => admin_url('admin-ajax.php'), |
| 252 | 'isAdmin' => is_admin(), |
| 253 | 'proVersion' => Plugin::proVersion(), |
| 254 | 'isPremium' => Plugin::isPro(), |
| 255 | 'isSetup' => [ |
| 256 | 'bunny' => false |
| 257 | ], |
| 258 | 'wpVersionString' => 'wp/v2/', |
| 259 | 'prestoVersionString' => 'presto-player/v1/', |
| 260 | 'defaults' => [ |
| 261 | 'color' => Setting::getDefaultColor() |
| 262 | ], |
| 263 | 'i18n' => Translation::geti18n() |
| 264 | ] |
| 265 | ) |
| 266 | ); |
| 267 | |
| 268 | if (function_exists('wp_set_script_translations')) { |
| 269 | wp_set_script_translations('surecart/blocks/admin', 'presto-player'); |
| 270 | } |
| 271 | |
| 272 | wp_localize_script('surecart/blocks/admin', 'scIcons', ['path' => esc_url_raw(plugin_dir_url(PRESTO_PLAYER_PLUGIN_FILE) . 'dist/icon-assets')]); |
| 273 | |
| 274 | |
| 275 | wp_localize_script( |
| 276 | 'surecart/blocks/admin', |
| 277 | 'prestoPlayerAdmin', |
| 278 | apply_filters( |
| 279 | 'presto_player_admin_block_script_options', |
| 280 | [ |
| 281 | 'root' => esc_url_raw(get_rest_url()), |
| 282 | 'nonce' => wp_create_nonce('wp_rest'), |
| 283 | 'logged_in' => is_user_logged_in(), |
| 284 | 'ajaxurl' => admin_url('admin-ajax.php'), |
| 285 | 'wp_max_upload_size' => wp_max_upload_size(), |
| 286 | 'isAdmin' => is_admin(), |
| 287 | 'proVersion' => Plugin::proVersion(), |
| 288 | 'isPremium' => Plugin::isPro(), |
| 289 | 'isSetup' => [ |
| 290 | 'bunny' => false |
| 291 | ], |
| 292 | 'wpVersionString' => 'wp/v2/', |
| 293 | 'prestoVersionString' => 'presto-player/v1/', |
| 294 | 'defaults' => [ |
| 295 | 'color' => Setting::getDefaultColor() |
| 296 | ] |
| 297 | ] |
| 298 | ) |
| 299 | ); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Does the page have a player? |
| 304 | */ |
| 305 | public function hasPlayer() |
| 306 | { |
| 307 | // global is the most reliable between page builders |
| 308 | global $load_presto_js; |
| 309 | if ($load_presto_js) { |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | // must be a singular page |
| 314 | if (!is_singular()) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | $id = get_the_ID(); |
| 319 | $widget_blocks = get_option('widget_block'); |
| 320 | |
| 321 | // change to see if we have one of our blocks |
| 322 | $types = Block::getBlockTypes(); |
| 323 | foreach ($types as $type) { |
| 324 | if (has_block($type, $id)) { |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | if (!empty($widget_blocks)) { |
| 329 | foreach ($widget_blocks as $block) { |
| 330 | $content = isset($block['content']) ? $block['content'] : ''; |
| 331 | if (!empty($content) && has_block($type, $content)) { |
| 332 | return true; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // check for data-presto-config (player rendered) |
| 339 | $wp_post = get_post($id); |
| 340 | if ($wp_post instanceof \WP_Post) { |
| 341 | $post = $wp_post->post_content; |
| 342 | } |
| 343 | $has_player = false !== strpos($post, 'data-presto-config'); |
| 344 | if ($has_player) { |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | // check that we have a shortcode |
| 349 | if (has_shortcode($post, 'presto_player')) { |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | // enable on Elementor |
| 354 | if (!empty($_GET['action']) && 'elementor' === $_GET['action']) { |
| 355 | return true; |
| 356 | } |
| 357 | if (isset($_GET['elementor-preview'])) { |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | // load for beaver builder |
| 362 | if (isset($_GET['fl_builder'])) { |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | // tutor LMS |
| 367 | global $post; |
| 368 | if (!empty($post->post_type) && $post->post_type) { |
| 369 | if (defined('TUTOR_VERSION') && 'lesson' === $post->post_type) { |
| 370 | return true; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // load for Divi builder |
| 375 | if (isset($_GET['et_fb'])) { |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | // do we have the player |
| 380 | return $has_player; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Add global player styles inline. |
| 385 | * |
| 386 | * @return void |
| 387 | */ |
| 388 | public function globalStyles() |
| 389 | { ?> |
| 390 | <style> |
| 391 | <?php readfile(PRESTO_PLAYER_PLUGIN_DIR . 'src/player/global.css'); ?> |
| 392 | </style> |
| 393 | <?php } |
| 394 | |
| 395 | public function loadJavascript() |
| 396 | { |
| 397 | // global styles |
| 398 | if (!wp_doing_ajax() && !defined('REST_REQUEST') && !defined('PRESTO_TESTSUITE')) { |
| 399 | $this->globalStyles(); |
| 400 | } |
| 401 | |
| 402 | // direct load |
| 403 | if (Setting::get('performance', 'module_enabled') || !is_admin()) { |
| 404 | // preload components |
| 405 | add_action('wp_head', [$this, 'preloadComponents']); |
| 406 | } |
| 407 | |
| 408 | wp_enqueue_script('presto-components'); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Block frontend assets |
| 413 | * |
| 414 | * @return void |
| 415 | */ |
| 416 | public function blockAssets() |
| 417 | { |
| 418 | // don't output if it doesn't have our block |
| 419 | if (!apply_filters('presto_player_load_js', $this->hasPlayer())) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | $this->loadJavascript(); |
| 424 | |
| 425 | // fallback styles and script to load iframes |
| 426 | add_action('wp_footer', function () { |
| 427 | if (is_admin()) return; |
| 428 | if (!apply_filters('presto_player/scripts/load_iframe_fallback', false)) return; |
| 429 | $this->printFallbackScriptsAndStyles(); |
| 430 | }); |
| 431 | } |
| 432 | |
| 433 | public function licenseScripts($hook) |
| 434 | { |
| 435 | add_action("admin_print_scripts-{$hook}", function () { |
| 436 | $assets = include trailingslashit(PRESTO_PLAYER_PLUGIN_DIR) . 'dist/license.asset.php'; |
| 437 | wp_enqueue_script( |
| 438 | 'surecart/license/admin', |
| 439 | trailingslashit(PRESTO_PLAYER_PLUGIN_URL) . 'dist/license.js', |
| 440 | array_merge($assets['dependencies']), |
| 441 | $assets['version'], |
| 442 | true |
| 443 | ); |
| 444 | wp_enqueue_style('surecart/license/admin', trailingslashit(PRESTO_PLAYER_PLUGIN_URL) . 'dist/license.css', [], $assets['version']); |
| 445 | }); |
| 446 | } |
| 447 | |
| 448 | public function printFallbackScriptsAndStyles() |
| 449 | { |
| 450 | /* |
| 451 | * This CSS is duplicated in 'packages/components/src/components/core/player/presto-player/presto-player.scss' |
| 452 | */ |
| 453 | echo '<style>.presto-iframe-fallback-container{position:relative;padding-bottom:56.25%;padding-top:30px;height:0;overflow:hidden}.presto-iframe-fallback-container embed,.presto-iframe-fallback-container iframe,.presto-iframe-fallback-container object{position:absolute;top:0;left:0;width:100%;height:100%}</style>'; |
| 454 | echo '<script defer> |
| 455 | window.addEventListener("load", function(event) { |
| 456 | setTimeout(function() { |
| 457 | var deferVideo = document.getElementsByClassName("presto-fallback-iframe"); |
| 458 | if (!deferVideo.length) return; |
| 459 | Array.from(deferVideo).forEach(function(video) { |
| 460 | video && video.setAttribute("src", video.getAttribute("data-src")); |
| 461 | }); |
| 462 | }, 2000); |
| 463 | }, false); |
| 464 | </script>'; |
| 465 | } |
| 466 | } |
| 467 |