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