AssetManager.php
787 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Core; |
| 4 | |
| 5 | // Include LocalizationManager |
| 6 | // require_once __DIR__ . '/LocalizationManager.php'; |
| 7 | |
| 8 | use Embedpress\Core\LocalizationManager; |
| 9 | |
| 10 | /** |
| 11 | * EmbedPress Asset Manager |
| 12 | * |
| 13 | * Centralized asset management for JS/CSS files across blocks, Elementor, admin, and frontend |
| 14 | * Handles new build files from Vite build system |
| 15 | */ |
| 16 | class AssetManager |
| 17 | { |
| 18 | /** |
| 19 | * Track which handles should be treated as ES modules |
| 20 | */ |
| 21 | private static $module_handles = []; |
| 22 | |
| 23 | /** |
| 24 | * Track if the global module filter has been added |
| 25 | */ |
| 26 | private static $module_filter_added = false; |
| 27 | |
| 28 | /** |
| 29 | * Asset definitions with context-based loading |
| 30 | */ |
| 31 | private static $assets = [ |
| 32 | |
| 33 | // 🔧 Scripts (Ordered by Priority) |
| 34 | // ---------------------------------- |
| 35 | |
| 36 | // Vendor assets (copied to assets folder for consistency) |
| 37 | // Priority 1-5: Core vendor libraries |
| 38 | 'plyr-css' => [ |
| 39 | 'file' => 'css/plyr.css', |
| 40 | 'deps' => [], |
| 41 | 'contexts' => ['frontend', 'elementor', 'editor'], |
| 42 | 'type' => 'style', |
| 43 | 'handle' => 'embedpress-plyr-css', |
| 44 | 'priority' => 1, |
| 45 | ], |
| 46 | 'carousel-vendor-css' => [ |
| 47 | 'file' => 'css/carousel.min.css', |
| 48 | 'deps' => [], |
| 49 | 'contexts' => ['frontend', 'elementor'], |
| 50 | 'type' => 'style', |
| 51 | 'handle' => 'embedpress-carousel-vendor-css', |
| 52 | 'priority' => 1, |
| 53 | ], |
| 54 | 'glider-css' => [ |
| 55 | 'file' => 'css/glider.min.css', |
| 56 | 'deps' => [], |
| 57 | 'contexts' => ['frontend', 'elementor'], |
| 58 | 'type' => 'style', |
| 59 | 'handle' => 'embedpress-glider-css', |
| 60 | 'priority' => 1, |
| 61 | ], |
| 62 | 'plyr-js' => [ |
| 63 | 'file' => 'js/vendor/plyr.js', |
| 64 | 'deps' => ['jquery'], |
| 65 | 'contexts' => ['frontend', 'elementor', 'editor'], |
| 66 | 'type' => 'script', |
| 67 | 'footer' => true, |
| 68 | 'handle' => 'embedpress-plyr', |
| 69 | 'priority' => 2, |
| 70 | ], |
| 71 | 'plyr-polyfilled-js' => [ |
| 72 | 'file' => 'js/vendor/plyr.polyfilled.js', |
| 73 | 'deps' => ['jquery'], |
| 74 | 'contexts' => ['frontend', 'elementor', 'editor'], |
| 75 | 'type' => 'script', |
| 76 | 'footer' => true, |
| 77 | 'handle' => 'embedpress-plyr-polyfilled', |
| 78 | 'priority' => 2, |
| 79 | ], |
| 80 | 'carousel-vendor-js' => [ |
| 81 | 'file' => 'js/vendor/carousel.min.js', |
| 82 | 'deps' => ['jquery'], |
| 83 | 'contexts' => ['frontend', 'elementor'], |
| 84 | 'type' => 'script', |
| 85 | 'footer' => true, |
| 86 | 'handle' => 'embedpress-carousel-vendor', |
| 87 | 'priority' => 2, |
| 88 | ], |
| 89 | 'glider-js' => [ |
| 90 | 'file' => 'js/vendor/glider.min.js', |
| 91 | 'deps' => ['jquery'], |
| 92 | 'contexts' => ['frontend', 'elementor'], |
| 93 | 'type' => 'script', |
| 94 | 'footer' => true, |
| 95 | 'handle' => 'embedpress-glider', |
| 96 | 'priority' => 2, |
| 97 | ], |
| 98 | 'pdfobject-js' => [ |
| 99 | 'file' => 'js/vendor/pdfobject.js', |
| 100 | 'deps' => [], |
| 101 | 'contexts' => ['frontend', 'elementor', 'editor'], |
| 102 | 'type' => 'script', |
| 103 | 'footer' => true, |
| 104 | 'handle' => 'embedpress-pdfobject', |
| 105 | 'priority' => 2, |
| 106 | ], |
| 107 | 'vimeo-player-js' => [ |
| 108 | 'file' => 'js/vendor/vimeo-player.js', |
| 109 | 'deps' => [], |
| 110 | 'contexts' => ['frontend', 'elementor'], |
| 111 | 'type' => 'script', |
| 112 | 'footer' => true, |
| 113 | 'handle' => 'embedpress-vimeo-player', |
| 114 | 'priority' => 2, |
| 115 | ], |
| 116 | 'ytiframeapi-js' => [ |
| 117 | 'file' => 'js/vendor/ytiframeapi.js', |
| 118 | 'deps' => [], |
| 119 | 'contexts' => ['frontend', 'elementor'], |
| 120 | 'type' => 'script', |
| 121 | 'footer' => true, |
| 122 | 'handle' => 'embedpress-ytiframeapi', |
| 123 | 'priority' => 2, |
| 124 | ], |
| 125 | 'bootstrap-js' => [ |
| 126 | 'file' => 'js/vendor/bootstrap/bootstrap.min.js', |
| 127 | 'deps' => ['jquery'], |
| 128 | 'contexts' => ['admin'], |
| 129 | 'type' => 'script', |
| 130 | 'footer' => true, |
| 131 | 'handle' => 'embedpress-bootstrap', |
| 132 | 'priority' => 2, |
| 133 | 'page' => 'embedpress' |
| 134 | ], |
| 135 | 'bootbox-js' => [ |
| 136 | 'file' => 'js/vendor/bootbox.min.js', |
| 137 | 'deps' => ['jquery', 'embedpress-bootstrap'], |
| 138 | 'contexts' => ['admin'], |
| 139 | 'type' => 'script', |
| 140 | 'footer' => true, |
| 141 | 'handle' => 'embedpress-bootbox', |
| 142 | 'priority' => 3, |
| 143 | 'page' => 'embedpress' |
| 144 | ], |
| 145 | // Priority 5-6: Main application build assets |
| 146 | 'admin-js' => [ |
| 147 | 'file' => 'js/admin.build.js', |
| 148 | 'deps' => [], |
| 149 | 'contexts' => ['admin'], |
| 150 | 'type' => 'script', |
| 151 | 'footer' => true, |
| 152 | 'handle' => 'embedpress-admin', |
| 153 | 'priority' => 5, |
| 154 | 'page' => 'embedpress' |
| 155 | ], |
| 156 | // Priority 7-10: Blocks |
| 157 | 'blocks-js' => [ |
| 158 | 'file' => 'js/blocks.build.js', |
| 159 | 'deps' => ['wp-blocks', 'wp-i18n', 'wp-element', 'wp-api-fetch', 'wp-is-shallow-equal', 'wp-editor', 'wp-components'], |
| 160 | 'contexts' => ['editor', 'frontend'], |
| 161 | 'type' => 'script', |
| 162 | 'footer' => true, |
| 163 | 'handle' => 'embedpress-blocks-editor', |
| 164 | 'priority' => 10, |
| 165 | ], |
| 166 | 'blocks-editor-style' => [ |
| 167 | 'file' => 'css/blocks.build.css', |
| 168 | 'deps' => [], |
| 169 | 'contexts' => ['editor'], |
| 170 | 'type' => 'style', |
| 171 | 'handle' => 'embedpress-blocks-editor-style', |
| 172 | 'priority' => 10, |
| 173 | ], |
| 174 | 'blocks-style' => [ |
| 175 | 'file' => 'css/blocks.build.css', |
| 176 | 'deps' => [], |
| 177 | 'contexts' => ['frontend', 'editor'], |
| 178 | 'type' => 'style', |
| 179 | 'handle' => 'embedpress-blocks-style', |
| 180 | 'priority' => 10, |
| 181 | ], |
| 182 | |
| 183 | // Priority 15-20: Legacy JS files |
| 184 | 'admin-legacy-js' => [ |
| 185 | 'file' => 'js/admin.js', |
| 186 | 'deps' => ['jquery'], |
| 187 | 'contexts' => ['admin'], |
| 188 | 'type' => 'script', |
| 189 | 'footer' => true, |
| 190 | 'handle' => 'embedpress-admin-legacy', |
| 191 | 'priority' => 15, |
| 192 | 'page' => 'embedpress' |
| 193 | ], |
| 194 | 'ads-js' => [ |
| 195 | 'file' => 'js/sponsored.js', |
| 196 | 'deps' => ['jquery', 'embedpress-front'], |
| 197 | 'contexts' => ['editor', 'frontend', 'elementor'], |
| 198 | 'type' => 'script', |
| 199 | 'footer' => true, |
| 200 | 'handle' => 'embedpress-ads', |
| 201 | 'priority' => 16, |
| 202 | ], |
| 203 | 'analytics-tracker-js' => [ |
| 204 | 'file' => 'js/analytics-tracker.js', |
| 205 | 'deps' => ['jquery'], |
| 206 | 'contexts' => ['frontend', 'elementor'], |
| 207 | 'type' => 'script', |
| 208 | 'footer' => true, |
| 209 | 'handle' => 'embedpress-analytics-tracker', |
| 210 | 'priority' => 15, |
| 211 | ], |
| 212 | 'carousel-js' => [ |
| 213 | 'file' => 'js/carousel.js', |
| 214 | 'deps' => ['jquery', 'embedpress-carousel-vendor'], |
| 215 | 'contexts' => ['frontend', 'elementor'], |
| 216 | 'type' => 'script', |
| 217 | 'footer' => true, |
| 218 | 'handle' => 'embedpress-carousel', |
| 219 | 'priority' => 15, |
| 220 | ], |
| 221 | 'documents-viewer-js' => [ |
| 222 | 'file' => 'js/documents-viewer-script.js', |
| 223 | 'deps' => ['jquery'], |
| 224 | 'contexts' => ['frontend', 'elementor'], |
| 225 | 'type' => 'script', |
| 226 | 'footer' => true, |
| 227 | 'handle' => 'embedpress-documents-viewer', |
| 228 | 'priority' => 15, |
| 229 | ], |
| 230 | 'front-js' => [ |
| 231 | 'file' => 'js/front.js', |
| 232 | 'deps' => ['jquery'], |
| 233 | 'contexts' => ['frontend', 'editor', 'elementor'], |
| 234 | 'type' => 'script', |
| 235 | 'footer' => true, |
| 236 | 'handle' => 'embedpress-front', |
| 237 | 'priority' => 15, |
| 238 | ], |
| 239 | 'gallery-justify-js' => [ |
| 240 | 'file' => 'js/gallery-justify.js', |
| 241 | 'deps' => ['jquery'], |
| 242 | 'contexts' => ['editor', 'frontend', 'elementor'], |
| 243 | 'type' => 'script', |
| 244 | 'footer' => true, |
| 245 | 'handle' => 'embedpress-gallery-justify', |
| 246 | 'priority' => 15, |
| 247 | ], |
| 248 | 'gutenberg-script-js' => [ |
| 249 | 'file' => 'js/gutneberg-script.js', |
| 250 | 'deps' => ['wp-blocks', 'wp-element'], |
| 251 | 'contexts' => ['editor'], |
| 252 | 'type' => 'script', |
| 253 | 'footer' => true, |
| 254 | 'handle' => 'embedpress-gutenberg-script', |
| 255 | 'priority' => 15, |
| 256 | ], |
| 257 | 'init-plyr-js' => [ |
| 258 | 'file' => 'js/initplyr.js', |
| 259 | 'deps' => ['jquery', 'embedpress-plyr'], |
| 260 | 'contexts' => ['frontend', 'elementor'], |
| 261 | 'type' => 'script', |
| 262 | 'footer' => true, |
| 263 | 'handle' => 'embedpress-init-plyr', |
| 264 | 'priority' => 15, |
| 265 | ], |
| 266 | 'instafeed-js' => [ |
| 267 | 'file' => 'js/instafeed.js', |
| 268 | 'deps' => ['jquery'], |
| 269 | 'contexts' => ['frontend', 'elementor'], |
| 270 | 'type' => 'script', |
| 271 | 'footer' => true, |
| 272 | 'handle' => 'embedpress-instafeed', |
| 273 | 'priority' => 15, |
| 274 | ], |
| 275 | 'license-js' => [ |
| 276 | 'file' => 'js/license.js', |
| 277 | 'deps' => ['jquery'], |
| 278 | 'contexts' => ['admin'], |
| 279 | 'type' => 'script', |
| 280 | 'footer' => true, |
| 281 | 'handle' => 'embedpress-license', |
| 282 | 'priority' => 15, |
| 283 | 'page' => 'embedpress' |
| 284 | ], |
| 285 | 'preview-js' => [ |
| 286 | 'file' => 'js/preview.js', |
| 287 | 'deps' => ['jquery'], |
| 288 | 'contexts' => ['admin'], |
| 289 | 'type' => 'script', |
| 290 | 'footer' => true, |
| 291 | 'handle' => 'embedpress-preview', |
| 292 | 'priority' => 15, |
| 293 | 'page' => 'embedpress' |
| 294 | ], |
| 295 | 'settings-js' => [ |
| 296 | 'file' => 'js/settings.js', |
| 297 | 'deps' => ['jquery', 'wp-color-picker'], |
| 298 | 'contexts' => ['admin'], |
| 299 | 'type' => 'script', |
| 300 | 'footer' => true, |
| 301 | 'handle' => 'embedpress-settings', |
| 302 | 'priority' => 15, |
| 303 | 'page' => 'embedpress' |
| 304 | ], |
| 305 | |
| 306 | // 🎨 Styles (Ordered by Priority) |
| 307 | // ---------------------------------- |
| 308 | |
| 309 | // Build CSS files (analytics.build.css is handled by Analytics.php) |
| 310 | |
| 311 | // Legacy CSS files |
| 312 | 'admin-notices-css' => [ |
| 313 | 'file' => 'css/admin-notices.css', |
| 314 | 'deps' => [], |
| 315 | 'contexts' => ['admin'], |
| 316 | 'type' => 'style', |
| 317 | 'handle' => 'embedpress-admin-notices', |
| 318 | 'priority' => 5, |
| 319 | 'page' => 'embedpress' |
| 320 | ], |
| 321 | |
| 322 | 'el-icon-css' => [ |
| 323 | 'file' => 'css/el-icon.css', |
| 324 | 'deps' => [], |
| 325 | 'contexts' => ['admin', 'frontend', 'elementor'], |
| 326 | 'type' => 'style', |
| 327 | 'handle' => 'embedpress-el-icon', |
| 328 | 'priority' => 5, |
| 329 | ], |
| 330 | 'embedpress-elementor-css' => [ |
| 331 | 'file' => 'css/embedpress-elementor.css', |
| 332 | 'deps' => [], |
| 333 | 'contexts' => ['elementor'], |
| 334 | 'type' => 'style', |
| 335 | 'handle' => 'embedpress-elementor-css', |
| 336 | 'priority' => 5, |
| 337 | ], |
| 338 | 'embedpress-css' => [ |
| 339 | 'file' => 'css/embedpress.css', |
| 340 | 'deps' => [], |
| 341 | 'contexts' => ['editor', 'frontend', 'elementor'], |
| 342 | 'type' => 'style', |
| 343 | 'handle' => 'embedpress-css', |
| 344 | 'priority' => 5, |
| 345 | ], |
| 346 | 'font-css' => [ |
| 347 | 'file' => 'css/font.css', |
| 348 | 'deps' => [], |
| 349 | 'contexts' => ['admin', 'frontend', 'elementor'], |
| 350 | 'type' => 'style', |
| 351 | 'handle' => 'embedpress-font', |
| 352 | 'priority' => 5, |
| 353 | ], |
| 354 | 'preview-css' => [ |
| 355 | 'file' => 'css/preview.css', |
| 356 | 'deps' => [], |
| 357 | 'contexts' => ['admin'], |
| 358 | 'type' => 'style', |
| 359 | 'handle' => 'embedpress-preview-css', |
| 360 | 'priority' => 5, |
| 361 | 'page' => 'embedpress' |
| 362 | ], |
| 363 | 'settings-icons-css' => [ |
| 364 | 'file' => 'css/settings-icons.css', |
| 365 | 'deps' => [], |
| 366 | 'contexts' => ['admin'], |
| 367 | 'type' => 'style', |
| 368 | 'handle' => 'embedpress-settings-icons', |
| 369 | 'priority' => 5, |
| 370 | 'page' => 'embedpress' |
| 371 | ], |
| 372 | 'settings-css' => [ |
| 373 | 'file' => 'css/settings.css', |
| 374 | 'deps' => [], |
| 375 | 'contexts' => ['admin'], |
| 376 | 'type' => 'style', |
| 377 | 'handle' => 'embedpress-settings-css', |
| 378 | 'priority' => 5, |
| 379 | 'page' => 'embedpress' |
| 380 | ], |
| 381 | 'admin-css' => [ |
| 382 | 'file' => 'css/admin.css', |
| 383 | 'deps' => [], |
| 384 | 'contexts' => ['admin'], |
| 385 | 'type' => 'style', |
| 386 | 'handle' => 'embedpress-admin-css', |
| 387 | 'priority' => 5, |
| 388 | 'page' => 'embedpress' |
| 389 | ], |
| 390 | ]; |
| 391 | |
| 392 | /** |
| 393 | * Initialize asset manager |
| 394 | */ |
| 395 | public static function init() |
| 396 | { |
| 397 | |
| 398 | |
| 399 | // Use proper priorities to ensure correct load order |
| 400 | add_action('wp_enqueue_scripts', [__CLASS__, 'enqueue_frontend_assets'], 5); |
| 401 | add_action('admin_enqueue_scripts', [__CLASS__, 'enqueue_admin_assets'], 5); |
| 402 | add_action('enqueue_block_assets', [__CLASS__, 'enqueue_block_assets'], 5); |
| 403 | |
| 404 | add_action('enqueue_block_editor_assets', [__CLASS__, 'enqueue_editor_assets'], 5); |
| 405 | |
| 406 | add_action('elementor/frontend/after_enqueue_styles', [__CLASS__, 'enqueue_elementor_assets'], 5); |
| 407 | |
| 408 | add_action('elementor/editor/after_enqueue_styles', [__CLASS__, 'enqueue_elementor_editor_assets'], 5); |
| 409 | |
| 410 | |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Enqueue frontend assets |
| 415 | */ |
| 416 | public static function enqueue_frontend_assets() |
| 417 | { |
| 418 | self::enqueue_assets_for_context('frontend'); |
| 419 | |
| 420 | // Setup frontend localization |
| 421 | LocalizationManager::setup_frontend_localization(); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Enqueue admin assets |
| 426 | */ |
| 427 | public static function enqueue_admin_assets($hook = '') |
| 428 | { |
| 429 | self::enqueue_assets_for_context('admin', $hook); |
| 430 | |
| 431 | // Load settings assets only on EmbedPress settings pages |
| 432 | if (strpos($hook, 'embedpress') !== false) { |
| 433 | self::enqueue_assets_for_context('settings', $hook); |
| 434 | |
| 435 | // Ensure wp-color-picker is loaded for settings page |
| 436 | wp_enqueue_style('wp-color-picker'); |
| 437 | |
| 438 | // Ensure media scripts are loaded |
| 439 | if (!did_action('wp_enqueue_media')) { |
| 440 | wp_enqueue_media(); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // Setup admin localization |
| 445 | LocalizationManager::setup_admin_localization($hook); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Enqueue block assets (both editor and frontend) |
| 450 | */ |
| 451 | public static function enqueue_block_assets() |
| 452 | { |
| 453 | // This runs on both frontend and editor for blocks |
| 454 | // For frontend, we don't need the editor scripts |
| 455 | if (is_admin()) { |
| 456 | self::enqueue_assets_for_context('editor'); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Enqueue editor-only assets |
| 462 | */ |
| 463 | public static function enqueue_editor_assets() |
| 464 | { |
| 465 | // Ensure editor assets are loaded |
| 466 | self::enqueue_assets_for_context('editor'); |
| 467 | |
| 468 | // Setup editor localization |
| 469 | LocalizationManager::setup_editor_localization(); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Enqueue Elementor frontend assets |
| 474 | */ |
| 475 | public static function enqueue_elementor_assets() |
| 476 | { |
| 477 | self::enqueue_assets_for_context('elementor'); |
| 478 | |
| 479 | // Setup Elementor localization |
| 480 | LocalizationManager::setup_elementor_localization(); |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Enqueue Elementor editor assets |
| 485 | */ |
| 486 | public static function enqueue_elementor_editor_assets() |
| 487 | { |
| 488 | // In Elementor editor, we need both elementor and editor context assets |
| 489 | self::enqueue_assets_for_context('elementor'); |
| 490 | self::enqueue_assets_for_context('editor'); |
| 491 | |
| 492 | // Setup Elementor editor localization |
| 493 | LocalizationManager::setup_elementor_localization(); |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Enqueue assets for a specific context |
| 498 | */ |
| 499 | private static function enqueue_assets_for_context($context, $hook = '') |
| 500 | { |
| 501 | |
| 502 | $assets_to_enqueue = []; |
| 503 | |
| 504 | // Collect assets for this context |
| 505 | foreach (self::$assets as $key => $asset) { |
| 506 | if (in_array($context, $asset['contexts'])) { |
| 507 | // Check if asset has page restriction |
| 508 | if (isset($asset['page']) && !empty($asset['page'])) { |
| 509 | // Only enqueue if we're on the specified page |
| 510 | if (strpos($hook, $asset['page']) !== false) { |
| 511 | $assets_to_enqueue[] = array_merge($asset, ['key' => $key]); |
| 512 | } |
| 513 | // If page doesn't match, don't enqueue this asset |
| 514 | } else { |
| 515 | // No page restriction, enqueue normally |
| 516 | $assets_to_enqueue[] = array_merge($asset, ['key' => $key]); |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | // Sort by priority |
| 522 | usort($assets_to_enqueue, function ($a, $b) { |
| 523 | return $a['priority'] - $b['priority']; |
| 524 | }); |
| 525 | |
| 526 | // Enqueue assets |
| 527 | foreach ($assets_to_enqueue as $asset) { |
| 528 | self::enqueue_single_asset($asset); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Enqueue a single asset |
| 534 | */ |
| 535 | private static function enqueue_single_asset($asset) |
| 536 | { |
| 537 | $file_url = EMBEDPRESS_PLUGIN_DIR_URL . 'assets/' . $asset['file']; |
| 538 | $file_path = EMBEDPRESS_PLUGIN_DIR_PATH . '/assets/' . $asset['file']; |
| 539 | |
| 540 | if (! file_exists($file_path)) { |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | $version = filemtime($file_path); |
| 545 | |
| 546 | // Check if we should load this asset based on current context |
| 547 | if (!self::should_load_asset($asset)) { |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | // Enqueue asset |
| 552 | if ($asset['type'] === 'script') { |
| 553 | wp_enqueue_script( |
| 554 | $asset['handle'], |
| 555 | $file_url, |
| 556 | $asset['deps'], |
| 557 | $version, |
| 558 | ! empty($asset['footer']) |
| 559 | ); |
| 560 | |
| 561 | // Add module attribute for ES modules (only build files) |
| 562 | if (strpos($asset['file'], '.build.js') !== false) { |
| 563 | // Track this handle as a module |
| 564 | self::$module_handles[] = $asset['handle']; |
| 565 | |
| 566 | // Add the global filter only once |
| 567 | if (!self::$module_filter_added) { |
| 568 | self::$module_filter_added = true; |
| 569 | add_filter('script_loader_tag', [__CLASS__, 'add_module_attribute'], 10, 2); |
| 570 | } |
| 571 | } |
| 572 | } elseif ($asset['type'] === 'style') { |
| 573 | |
| 574 | wp_enqueue_style( |
| 575 | $asset['handle'], |
| 576 | $file_url, |
| 577 | $asset['deps'], |
| 578 | $version, |
| 579 | $asset['media'] ?? 'all' |
| 580 | ); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Determine if an asset should be loaded based on current context |
| 586 | */ |
| 587 | private static function should_load_asset($asset) |
| 588 | { |
| 589 | // Get current environment state |
| 590 | $is_admin = is_admin(); |
| 591 | $is_elementor_editor = false; |
| 592 | $is_elementor_preview = false; |
| 593 | $is_gutenberg_editor = false; |
| 594 | |
| 595 | // Check Elementor states |
| 596 | if (class_exists('\Elementor\Plugin')) { |
| 597 | $elementor = \Elementor\Plugin::$instance; |
| 598 | |
| 599 | if (isset($elementor->editor)) { |
| 600 | $is_elementor_editor = $elementor->editor->is_edit_mode(); |
| 601 | } |
| 602 | |
| 603 | if (isset($elementor->preview)) { |
| 604 | $is_elementor_preview = $elementor->preview->is_preview_mode(); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | // Check if we're in Gutenberg editor |
| 609 | if ($is_admin) { |
| 610 | global $pagenow; |
| 611 | $is_gutenberg_editor = ( |
| 612 | $pagenow === 'post.php' || |
| 613 | $pagenow === 'post-new.php' || |
| 614 | $pagenow === 'site-editor.php' |
| 615 | ) && function_exists('use_block_editor_for_post_type'); |
| 616 | } |
| 617 | |
| 618 | // Asset loading logic based on contexts |
| 619 | foreach ($asset['contexts'] as $context) { |
| 620 | switch ($context) { |
| 621 | case 'frontend': |
| 622 | // Load on frontend (not in any editor or admin) |
| 623 | if (!$is_admin && !$is_elementor_editor && !$is_elementor_preview) { |
| 624 | return true; |
| 625 | } |
| 626 | break; |
| 627 | |
| 628 | case 'admin': |
| 629 | // Load in WordPress admin (but not in Elementor editor) |
| 630 | if ($is_admin && !$is_elementor_editor && !$is_elementor_preview) { |
| 631 | // Check if asset has page restriction |
| 632 | if (isset($asset['page'])) { |
| 633 | return self::is_embedpress_admin_page($asset['page']); |
| 634 | } |
| 635 | return true; |
| 636 | } |
| 637 | break; |
| 638 | |
| 639 | case 'editor': |
| 640 | // Load in Gutenberg editor or when editing posts |
| 641 | if ($is_gutenberg_editor || ($is_admin && !$is_elementor_editor)) { |
| 642 | return true; |
| 643 | } |
| 644 | break; |
| 645 | |
| 646 | case 'elementor': |
| 647 | // Load in Elementor editor, preview, or frontend when Elementor is rendering |
| 648 | if ($is_elementor_editor || $is_elementor_preview) { |
| 649 | return true; |
| 650 | } |
| 651 | // Also load on frontend if Elementor content is present |
| 652 | if (!$is_admin && self::has_elementor_content()) { |
| 653 | return true; |
| 654 | } |
| 655 | break; |
| 656 | |
| 657 | case 'settings': |
| 658 | // Load only on EmbedPress settings pages |
| 659 | if ($is_admin && !$is_elementor_editor && !$is_elementor_preview) { |
| 660 | return true; |
| 661 | } |
| 662 | break; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // Check if this is an individual block script and if it should be loaded |
| 667 | if (strpos($asset['handle'], 'embedpress-block-') === 0) { |
| 668 | return self::should_load_individual_block($asset['handle']); |
| 669 | } |
| 670 | |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Check if we're on an EmbedPress admin page |
| 676 | */ |
| 677 | private static function is_embedpress_admin_page($page_type) |
| 678 | { |
| 679 | global $pagenow; |
| 680 | |
| 681 | // Get current page |
| 682 | $current_page = isset($_GET['page']) ? $_GET['page'] : ''; |
| 683 | |
| 684 | switch ($page_type) { |
| 685 | case 'embedpress': |
| 686 | // Check if we're on any EmbedPress admin page |
| 687 | return ( |
| 688 | strpos($current_page, 'embedpress') !== false || |
| 689 | $pagenow === 'admin.php' && strpos($current_page, 'embedpress') !== false |
| 690 | ); |
| 691 | case 'embedpress-analytics': |
| 692 | return $current_page === 'embedpress-analytics'; |
| 693 | default: |
| 694 | return false; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Check if individual block should be loaded based on active blocks |
| 700 | */ |
| 701 | private static function should_load_individual_block($handle) |
| 702 | { |
| 703 | // Get active blocks from settings |
| 704 | $elements = (array) get_option(EMBEDPRESS_PLG_NAME . ":elements", []); |
| 705 | $active_blocks = isset($elements['gutenberg']) ? (array) $elements['gutenberg'] : []; |
| 706 | |
| 707 | // Map handles to block names |
| 708 | $block_map = [ |
| 709 | 'embedpress-block-embedpress' => 'embedpress', |
| 710 | 'embedpress-block-document' => 'document', |
| 711 | 'embedpress-block-pdf' => 'embedpress-pdf', |
| 712 | 'embedpress-block-calendar' => 'embedpress-calendar', |
| 713 | 'embedpress-block-google-docs' => 'google-docs', |
| 714 | 'embedpress-block-google-drawings' => 'google-drawings', |
| 715 | 'embedpress-block-google-forms' => 'google-forms', |
| 716 | 'embedpress-block-google-maps' => 'google-maps', |
| 717 | 'embedpress-block-google-sheets' => 'google-sheets', |
| 718 | 'embedpress-block-google-slides' => 'google-slides', |
| 719 | 'embedpress-block-twitch' => 'twitch', |
| 720 | 'embedpress-block-wistia' => 'wistia', |
| 721 | 'embedpress-block-youtube' => 'youtube' |
| 722 | ]; |
| 723 | |
| 724 | $block_name = isset($block_map[$handle]) ? $block_map[$handle] : ''; |
| 725 | |
| 726 | // If no block name found or no active blocks set, load all blocks (default behavior) |
| 727 | if (empty($block_name) || empty($active_blocks)) { |
| 728 | return true; |
| 729 | } |
| 730 | |
| 731 | // Check if this specific block is active |
| 732 | return in_array($block_name, $active_blocks); |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Check if current page has Elementor content |
| 737 | */ |
| 738 | private static function has_elementor_content() |
| 739 | { |
| 740 | if (!class_exists('\Elementor\Plugin')) { |
| 741 | return false; |
| 742 | } |
| 743 | |
| 744 | // Check if we're on a singular post/page |
| 745 | if (is_singular()) { |
| 746 | $post_id = get_the_ID(); |
| 747 | return \Elementor\Plugin::$instance->documents->get($post_id)->is_built_with_elementor(); |
| 748 | } |
| 749 | |
| 750 | return false; |
| 751 | } |
| 752 | |
| 753 | |
| 754 | /** |
| 755 | * Get asset URL |
| 756 | */ |
| 757 | public static function get_asset_url($file) |
| 758 | { |
| 759 | return EMBEDPRESS_PLUGIN_DIR_URL . 'assets/' . $file; |
| 760 | } |
| 761 | |
| 762 | |
| 763 | |
| 764 | /** |
| 765 | * Add module attribute to script tags for ES modules |
| 766 | */ |
| 767 | public static function add_module_attribute($tag, $handle) |
| 768 | { |
| 769 | if (in_array($handle, self::$module_handles)) { |
| 770 | // Only add type="module" if it doesn't already exist |
| 771 | if (strpos($tag, 'type="module"') === false) { |
| 772 | return str_replace('<script ', '<script type="module" ', $tag); |
| 773 | } |
| 774 | } |
| 775 | return $tag; |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Check if asset exists |
| 780 | */ |
| 781 | public static function asset_exists($file) |
| 782 | { |
| 783 | $plugin_path = dirname(dirname(dirname(__DIR__))); |
| 784 | return file_exists($plugin_path . '/assets/' . $file); |
| 785 | } |
| 786 | } |
| 787 |