AI
9 months ago
admin-pages
1 year ago
api
2 months ago
blog
1 year ago
customizer
1 year ago
filters
1 month ago
full-site-editing
1 month ago
importer
1 year ago
integrations
1 month ago
menu
1 year ago
polyfills
1 year ago
preview
7 months ago
recommendations
1 month ago
shapes
2 years ago
shortcodes
1 year ago
src
1 month ago
add-edit-in-kubio.php
11 months ago
editor-assets.php
1 month ago
filters.php
1 year ago
frontend.php
1 year ago
global-data.php
1 year ago
init.php
1 year ago
kubio-block-library.php
1 month ago
kubio-editor.php
1 month ago
load.php
1 month ago
editor-assets.php
570 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Arr\Support\Arr; |
| 4 | use Kubio\AssetsDependencyInjector; |
| 5 | use Kubio\Core\Utils; |
| 6 | use Kubio\DemoSites\DemoSitesRepository; |
| 7 | use Kubio\Flags; |
| 8 | use Kubio\Core\KubioFrontPageRevertNotice; |
| 9 | |
| 10 | function kubio_override_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { |
| 11 | $script = $scripts->query( $handle, 'registered' ); |
| 12 | if ( $script ) { |
| 13 | |
| 14 | $script->src = $src; |
| 15 | $script->deps = $deps; |
| 16 | $script->ver = $ver; |
| 17 | $script->args = $in_footer; |
| 18 | |
| 19 | unset( $script->extra['group'] ); |
| 20 | if ( $in_footer ) { |
| 21 | $script->add_data( 'group', 1 ); |
| 22 | } |
| 23 | } else { |
| 24 | $scripts->add( $handle, $src, $deps, $ver, $in_footer ); |
| 25 | } |
| 26 | |
| 27 | if ( in_array( 'wp-i18n', $deps, true ) ) { |
| 28 | $translation_path = wp_normalize_path( KUBIO_ROOT_DIR . '/languages' ); |
| 29 | $scripts->set_translations( $handle, 'kubio', $translation_path ); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | function kubio_override_style( $styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) { |
| 34 | $style = $styles->query( $handle, 'registered' ); |
| 35 | if ( $style ) { |
| 36 | $styles->remove( $handle ); |
| 37 | } |
| 38 | $styles->add( $handle, $src, $deps, $ver, $media ); |
| 39 | } |
| 40 | |
| 41 | function kubio_register_kubio_scripts_scripts_dependencies( $version ) { |
| 42 | $scripts = array( |
| 43 | array( |
| 44 | 'handle' => 'fancybox', |
| 45 | 'deps' => array( 'jquery' ), |
| 46 | 'src' => 'fancybox/jquery.fancybox.min.js', |
| 47 | ), |
| 48 | ); |
| 49 | |
| 50 | $scripts = apply_filters( 'kubio/register_kubio_scripts_dependencies', $scripts ); |
| 51 | |
| 52 | foreach ( $scripts as $script ) { |
| 53 | AssetsDependencyInjector::registerKubioScriptsDependency( |
| 54 | $script['handle'], |
| 55 | kubio_url( "/static/{$script['src']}" ), |
| 56 | $script['deps'], |
| 57 | $version |
| 58 | ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | function kubio_register_frontend_script( $handle ) { |
| 63 | add_filter( |
| 64 | 'kubio/frontend/scripts', |
| 65 | function ( $scripts ) use ( $handle ) { |
| 66 | |
| 67 | if ( ! in_array( $handle, $scripts ) ) { |
| 68 | $scripts[] = $handle; |
| 69 | } |
| 70 | |
| 71 | return $scripts; |
| 72 | } |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | function kubio_get_frontend_scripts() { |
| 77 | return apply_filters( 'kubio/frontend/scripts', array() ); |
| 78 | } |
| 79 | |
| 80 | function kubio_enqueue_frontend_scripts() { |
| 81 | $scripts = apply_filters( 'kubio/frontend/scripts', array() ); |
| 82 | foreach ( $scripts as $handle ) { |
| 83 | wp_enqueue_script( $handle ); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function kubio_register_packages_scripts() { |
| 88 | |
| 89 | $registered = array(); |
| 90 | |
| 91 | $translation_path = wp_normalize_path( KUBIO_ROOT_DIR . '/languages' ); |
| 92 | |
| 93 | $paths = glob( KUBIO_ROOT_DIR . 'build/*/index.js' ); |
| 94 | foreach ( $paths as $path ) { |
| 95 | $handle = 'kubio-' . basename( dirname( $path ) ); |
| 96 | $asset_file = substr( $path, 0, - 3 ) . '.asset.php'; |
| 97 | $asset = file_exists( $asset_file ) |
| 98 | ? require $asset_file |
| 99 | : null; |
| 100 | $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array(); |
| 101 | |
| 102 | if ( Utils::isDebug() ) { |
| 103 | $version = uniqid( time() . '-' ); |
| 104 | } else { |
| 105 | $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path ); |
| 106 | } |
| 107 | |
| 108 | switch ( $handle ) { |
| 109 | case 'kubio-editor': |
| 110 | array_push( $dependencies, 'wp-dom-ready', 'editor', 'wp-editor' ); |
| 111 | |
| 112 | if ( kubio_is_kubio_editor_page() ) { |
| 113 | array_push( $dependencies, 'kubio-interface-store' ); |
| 114 | } |
| 115 | |
| 116 | break; |
| 117 | |
| 118 | case 'kubio-format-library': |
| 119 | array_push( $dependencies, 'wp-format-library' ); |
| 120 | break; |
| 121 | |
| 122 | case 'kubio-scripts': |
| 123 | kubio_register_kubio_scripts_scripts_dependencies( $version ); |
| 124 | $dependencies = array_merge( $dependencies, array( 'jquery' ) ); |
| 125 | $dependencies = array_diff( $dependencies, array( 'wp-polyfill' ) ); |
| 126 | break; |
| 127 | |
| 128 | case 'kubio-frontend': |
| 129 | $dependencies = array( 'kubio-scripts' ); |
| 130 | kubio_register_frontend_script( 'kubio-frontend' ); |
| 131 | break; |
| 132 | |
| 133 | case 'kubio-block-library': |
| 134 | array_push( $dependencies, 'kubio-format-library' ); |
| 135 | break; |
| 136 | |
| 137 | case 'kubio-block-editor': |
| 138 | if ( wp_script_is( 'wp-private-apis', 'registered' ) ) { |
| 139 | $dependencies[] = 'wp-private-apis'; |
| 140 | } |
| 141 | |
| 142 | if ( wp_script_is( 'wp-experiments', 'registered' ) ) { |
| 143 | $dependencies[] = 'wp-experiments'; |
| 144 | } |
| 145 | |
| 146 | //For backward compatability to 6.1 |
| 147 | if ( Utils::wpVersionCompare( '6.4', '<' ) ) { |
| 148 | if ( ( $key = array_search( 'wp-commands', $dependencies ) ) !== false ) { |
| 149 | unset( $dependencies[ $key ] ); |
| 150 | } |
| 151 | } |
| 152 | array_push( $dependencies, 'wp-block-directory' ); |
| 153 | break; |
| 154 | |
| 155 | } |
| 156 | |
| 157 | $kubio_path = substr( $path, strlen( KUBIO_ROOT_DIR ) ); |
| 158 | |
| 159 | $registered[] = array( |
| 160 | $handle, |
| 161 | kubio_url( $kubio_path ), |
| 162 | $dependencies, |
| 163 | $version, |
| 164 | true, |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | foreach ( $registered as $script ) { |
| 169 | |
| 170 | if ( is_array( $script ) && count( $script ) >= 2 ) { |
| 171 | $handle = $script[0]; |
| 172 | $deps = $script[2]; |
| 173 | if ( in_array( 'wp-i18n', $deps, true ) ) { |
| 174 | wp_set_script_translations( $handle, 'kubio' ); |
| 175 | } |
| 176 | |
| 177 | call_user_func_array( 'wp_register_script', $script ); |
| 178 | \wp_set_script_translations( $script[0], 'kubio', $translation_path ); |
| 179 | do_action( 'kubio_registered_script', $script[0], $script[3] ); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | do_action( 'kubio_scripts_registered', $registered ); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | function kubio_replace_default_scripts( $scripts ) { |
| 188 | |
| 189 | if ( ! kubio_is_kubio_editor_page() ) { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | $to_replace = array( |
| 194 | 'wp-block-editor' => 'block-editor', |
| 195 | ); |
| 196 | |
| 197 | foreach ( $to_replace as $old => $new ) { |
| 198 | $script_path = KUBIO_ROOT_DIR . "/build/{$new}/index.js"; |
| 199 | $asset_file = KUBIO_ROOT_DIR . "/build/{$new}/index.asset.php"; |
| 200 | |
| 201 | $asset = file_exists( $asset_file ) |
| 202 | ? require $asset_file |
| 203 | : null; |
| 204 | $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array(); |
| 205 | $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $script_path ); |
| 206 | |
| 207 | //For backward compatability to 6.1 |
| 208 | if ( Utils::wpVersionCompare( '6.4', '<' ) ) { |
| 209 | if ( ( $key = array_search( 'wp-commands', $dependencies ) ) !== false ) { |
| 210 | unset( $dependencies[ $key ] ); |
| 211 | } |
| 212 | } |
| 213 | kubio_override_script( |
| 214 | $scripts, |
| 215 | $old, |
| 216 | kubio_url( "/build/{$new}/index.js" ), |
| 217 | $dependencies, |
| 218 | $version, |
| 219 | true |
| 220 | ); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | function kubio_register_kubio_block_library_style_dependencies( $version ) { |
| 226 | $styles = array( |
| 227 | array( |
| 228 | 'handle' => 'fancybox', |
| 229 | 'src' => 'fancybox/jquery.fancybox.min.css', |
| 230 | ), |
| 231 | ); |
| 232 | |
| 233 | $styles = apply_filters( 'kubio/register_block_library_style_dependencies', $styles ); |
| 234 | |
| 235 | foreach ( $styles as $style ) { |
| 236 | AssetsDependencyInjector::registerKubioFrontendStyleDependency( |
| 237 | $style['handle'], |
| 238 | kubio_url( "/static/{$style['src']}" ), |
| 239 | isset( $style['deps'] ) ? $style['deps'] : array(), |
| 240 | $version |
| 241 | ); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | |
| 246 | function kubio_register_packages_styles() { |
| 247 | |
| 248 | $registered = array(); |
| 249 | |
| 250 | foreach ( glob( KUBIO_ROOT_DIR . 'build/*/style.css' ) as $path ) { |
| 251 | $handle = 'kubio-' . basename( dirname( $path ) ); |
| 252 | $kubio_path = substr( $path, strlen( KUBIO_ROOT_DIR ) ); |
| 253 | $version = filemtime( $path ); |
| 254 | $dependencies = array(); |
| 255 | |
| 256 | switch ( $handle ) { |
| 257 | case 'kubio-editor': |
| 258 | $dependencies = array( 'wp-edit-blocks'); |
| 259 | break; |
| 260 | |
| 261 | case 'kubio-format-library': |
| 262 | array_push( $dependencies, 'wp-format-library' ); |
| 263 | break; |
| 264 | |
| 265 | case 'kubio-admin-panel': |
| 266 | array_push( $dependencies, 'kubio-utils' ); |
| 267 | break; |
| 268 | |
| 269 | case 'kubio-ai': |
| 270 | array_push( $dependencies, 'wp-components' ); |
| 271 | break; |
| 272 | |
| 273 | case 'kubio-block-library': |
| 274 | kubio_register_kubio_block_library_style_dependencies( $version ); |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | $registered[] = array( |
| 279 | $handle, |
| 280 | kubio_url( $kubio_path ), |
| 281 | $dependencies, |
| 282 | $version, |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | foreach ( glob( KUBIO_ROOT_DIR . 'build/*/editor.css' ) as $path ) { |
| 287 | $handle = 'kubio-' . basename( dirname( $path ) ); |
| 288 | $kubio_path = substr( $path, strlen( KUBIO_ROOT_DIR ) ); |
| 289 | $version = filemtime( $path ); |
| 290 | $dependencies = array(); |
| 291 | |
| 292 | switch ( $handle ) { |
| 293 | case 'kubio-editor': |
| 294 | $dependencies = array( 'wp-edit-blocks' ); |
| 295 | break; |
| 296 | |
| 297 | case 'kubio-block-library': |
| 298 | $dependencies = array( /* 'wp-block-library' */ ); |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | $registered[] = array( |
| 303 | "{$handle}-editor", |
| 304 | kubio_url( $kubio_path ), |
| 305 | $dependencies, |
| 306 | $version, |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | foreach ( $registered as $style ) { |
| 311 | |
| 312 | if ( is_array( $style ) && count( $style ) >= 2 ) { |
| 313 | |
| 314 | call_user_func_array( 'wp_register_style', $style ); |
| 315 | |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
| 321 | function kubio_replace_default_styles( $styles ) { |
| 322 | |
| 323 | if ( ! kubio_is_kubio_editor_page() ) { |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | // Editor Styles . |
| 328 | kubio_override_style( |
| 329 | $styles, |
| 330 | 'wp-block-editor', |
| 331 | kubio_url( 'build/block-editor/style.css' ), |
| 332 | array( 'wp-components', 'wp-editor-font' ), |
| 333 | filemtime( KUBIO_ROOT_DIR . 'build/editor/style.css' ) |
| 334 | ); |
| 335 | $styles->add_data( 'wp-block-editor', 'rtl', 'replace' ); |
| 336 | } |
| 337 | |
| 338 | add_action( 'init', 'kubio_register_packages_scripts' ); |
| 339 | add_action( 'init', 'kubio_register_packages_styles' ); |
| 340 | |
| 341 | add_action( 'wp_default_styles', 'kubio_replace_default_styles' ); |
| 342 | add_action( 'wp_default_scripts', 'kubio_replace_default_scripts' ); |
| 343 | |
| 344 | |
| 345 | add_action( |
| 346 | 'kubio_registered_script', |
| 347 | function ( $handle, $version ) { |
| 348 | global $wp_version; |
| 349 | if ( $handle === 'kubio-utils' || $handle === 'kubio-admin-panel' ) { |
| 350 | $include_test_templates = defined( 'KUBIO_INCLUDE_TEST_TEMPLATES' ) && KUBIO_INCLUDE_TEST_TEMPLATES === true; |
| 351 | $is_wpml_active = kubio_wpml_is_active(); |
| 352 | $is_polylang_active = kubio_polylang_is_active(); |
| 353 | |
| 354 | $data = 'window.kubioUtilsData=' . wp_json_encode( |
| 355 | array_merge( |
| 356 | kubio_get_site_urls(), |
| 357 | array( |
| 358 | 'defaultAssetsURL' => kubio_url( 'static/default-assets' ), |
| 359 | 'staticAssetsURL' => kubio_url( 'static' ), |
| 360 | 'patternsAssetsUrl' => kubio_url( 'static/patterns' ), |
| 361 | 'kubioRemoteContentFile' => 'https://static-assets.kubiobuilder.com/content-2022-05-17.json', |
| 362 | 'kubioCloudPresetsUrl' => Utils::getGlobalSnippetsURL(), |
| 363 | 'kubioCloudPresetCategoriesUrl' => Utils::getGlobalSnippetsCategoriesURL(), |
| 364 | 'kubioCloudPresetTagsUrl' => Utils::getGlobalSnippetsTagsURL(), |
| 365 | 'kubioCloudUrl' => Utils::getCloudURL(), |
| 366 | 'kubioRemoteContent' => Utils::getSnippetsURL( '/globals' ), |
| 367 | 'kubioLocalContentFile' => kubio_url( 'static/patterns/content-converted.json' ), |
| 368 | 'kubioEditorURL' => add_query_arg( 'page', 'kubio', admin_url( 'admin.php' ) ), |
| 369 | 'showFreeImagesTab' => Utils::getIsImageHubPluginActive(), |
| 370 | 'patternsOnTheFly' => ( defined( 'KUBIO_PATTERNS_ON_THE_FLY' ) && KUBIO_PATTERNS_ON_THE_FLY ) ? KUBIO_PATTERNS_ON_THE_FLY : '', |
| 371 | 'base_url' => site_url(), |
| 372 | 'admin_url' => admin_url(), |
| 373 | 'admin_plugins_url' => admin_url( 'plugins.php' ), |
| 374 | 'demo_sites_url' => Utils::getStarterSitesURL(), |
| 375 | 'demo_parts_url' => Utils::getStarterPartsURL(), |
| 376 | 'plugins_states' => DemoSitesRepository::getInstance()->getPluginsStates(), |
| 377 | 'last_imported_starter' => Flags::get( 'last_imported_starter' ), |
| 378 | 'demo_site_ajax_nonce' => wp_create_nonce( 'kubio-ajax-demo-site-verification' ), |
| 379 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 380 | 'kubio_ajax_nonce' => wp_create_nonce( 'kubio_ajax_nonce' ), |
| 381 | 'enable_starter_sites' => apply_filters( 'kubio/starter-sites/enabled', true ), |
| 382 | 'wpVersion' => preg_replace( '/([0-9]+).([0-9]+).*/', '$1.$2', $wp_version ), |
| 383 | 'enable_try_online' => Utils::isTryOnlineEnabled(), |
| 384 | 'supplementary_upgrade_to_pro' => apply_filters( 'kubio/show-supplementary-upgrade-to-pro', false ), |
| 385 | 'kubioAIPricingURL' => Utils::getCloudURL( '/ui-route/my-plans?purchase_ai=1' ), |
| 386 | 'kubioAIParallelCalls' => apply_filters( 'kubio/ai/parallel-calls', 5 ), |
| 387 | 'showInternalFeatures' => defined( '\KUBIO_INTERNAL' ) && \KUBIO_INTERNAL, |
| 388 | 'sectionStylesTags' => array( 'shadow', 'flat', 'outlined', 'rounded', 'minimal' ), |
| 389 | 'kubio_is_ai_site_editor' => Utils::getIsAISiteEditor(), |
| 390 | 'activatedOnStage2' => Flags::getSetting( 'activatedOnStage2', false ), |
| 391 | 'aiStage2' => Flags::getSetting( 'aiStage2', false ), |
| 392 | 'advancedMode' => Flags::getSetting( 'advancedMode', true ), |
| 393 | 'featuresVersion' => Flags::getSetting( 'featuresVersion', 1 ), |
| 394 | 'allowSiteLeadsRecommendations' => Flags::getSetting( 'allowSiteLeadsRecommendations', false ), |
| 395 | 'siteName' => get_bloginfo( 'name' ), |
| 396 | 'wpAdminUpgradePage' => add_query_arg( |
| 397 | array( |
| 398 | 'tab' => 'pro-upgrade', |
| 399 | 'page' => 'kubio-get-started', |
| 400 | ), |
| 401 | admin_url( 'admin.php' ) |
| 402 | ), |
| 403 | 'adminLanguage' => get_user_locale(), |
| 404 | 'autoStartBlackWizardOnboarding' => Flags::get( 'auto_start_black_wizard_onboarding', false ), |
| 405 | 'importDesignIndex' => Flags::get( 'import_design_index', null ), |
| 406 | 'importDesignAiStructure' => Flags::get( 'import_design_ai_structure', null ), |
| 407 | 'aiWizardDescriptionOptional' => Flags::getSetting( 'aiWizardDescriptionOptional', false ), |
| 408 | 'showFrontPageRevertNotice' => KubioFrontPageRevertNotice::getShowNoticeInEditor(), |
| 409 | 'frontPageRevertBackupData' => KubioFrontPageRevertNotice::getInstance()->getFrontPageBackupData(), |
| 410 | 'frontPageRevertNoticeNonce' => wp_create_nonce( KubioFrontPageRevertNotice::$nonceKey ), |
| 411 | 'allow3rdPartyBlogOverride' => apply_filters( 'kubio/allow_3rd_party_blog_override', true ), |
| 412 | 'kubioRecommendationSettings' => (object) kubio_get_recommendations_settings(), |
| 413 | 'multilanguage' => array( |
| 414 | 'hasTranslator' => $is_wpml_active || $is_polylang_active, |
| 415 | 'isWpmlActive' => $is_wpml_active, |
| 416 | 'isPolylangActive' => $is_polylang_active, |
| 417 | 'polylang_add_page_translation_nonce' => wp_create_nonce( 'kubio_api_polylang_add_page_translation' ), |
| 418 | ), |
| 419 | |
| 420 | 'bpaNonce' => wp_create_nonce( 'bpa_wp_nonce' ), |
| 421 | ), |
| 422 | apply_filters( 'kubio/kubio-utils-data/extras', array() ) |
| 423 | ) |
| 424 | ); |
| 425 | |
| 426 | wp_add_inline_script( $handle, $data, 'before' ); |
| 427 | } |
| 428 | |
| 429 | if ( $handle === 'kubio-style-manager' ) { |
| 430 | |
| 431 | $url = add_query_arg( |
| 432 | array( |
| 433 | 'action' => 'kubio_style_manager_web_worker', |
| 434 | '_wpnonce' => wp_create_nonce( 'kubio_style_manager_web_worker_nonce' ), |
| 435 | 'v' => filemtime( KUBIO_ROOT_DIR . '/defaults/style-manager-web-worker-template.js' ) . '-' . ( Utils::isDebug() ? time() : KUBIO_VERSION . '-' . $wp_version ), |
| 436 | ), |
| 437 | admin_url( 'admin-ajax.php' ) |
| 438 | ); |
| 439 | |
| 440 | wp_add_inline_script( |
| 441 | $handle, |
| 442 | 'var _kubioStyleManagerWorkerURL=' . wp_json_encode( $url ), |
| 443 | 'before' |
| 444 | ); |
| 445 | } |
| 446 | }, |
| 447 | 10, |
| 448 | 2 |
| 449 | ); |
| 450 | |
| 451 | function kubio_print_style_manager_web_worker() { |
| 452 | check_ajax_referer( 'kubio_style_manager_web_worker_nonce' ); |
| 453 | header( 'content-type: application/javascript' ); |
| 454 | |
| 455 | $script = ''; |
| 456 | $done = wp_scripts()->done; |
| 457 | ob_start(); |
| 458 | wp_scripts()->done = array( 'wp-inert-polyfill', 'wp-polyfill' ); |
| 459 | wp_scripts()->do_items( 'kubio-style-manager' ); |
| 460 | wp_scripts()->done = $done; |
| 461 | $script = ob_get_clean(); |
| 462 | |
| 463 | $script = preg_replace_callback( |
| 464 | '#<script(.*?)>(.*?)</script>#s', |
| 465 | function ( $matches ) { |
| 466 | $script_attrs = Arr::get( $matches, 1, '' ); |
| 467 | preg_match( "#src=(\"|')(.*?)(\"|')#", $script_attrs, $attrs_match ); |
| 468 | $url = Arr::get( $attrs_match, 2, '' ); |
| 469 | $content = trim( Arr::get( $matches, 2, '' ) ); |
| 470 | |
| 471 | $result = array(); |
| 472 | |
| 473 | if ( ! empty( $url ) ) { |
| 474 | $result[] = sprintf( "importScripts('%s');", $url ); |
| 475 | } |
| 476 | |
| 477 | if ( ! empty( $content ) ) { |
| 478 | $result[] = $content; |
| 479 | } |
| 480 | |
| 481 | return trim( implode( "\n", $result ) ) . "\n\n"; |
| 482 | }, |
| 483 | $script |
| 484 | ); |
| 485 | |
| 486 | $content = file_get_contents( KUBIO_ROOT_DIR . '/defaults/style-manager-web-worker-template.js' ); |
| 487 | $content = str_replace( '// {{{importScriptsPlaceholder}}}', $script, $content ); |
| 488 | |
| 489 | if ( ! Utils::isDebug() ) { |
| 490 | header( 'Cache-control: public' ); |
| 491 | header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', time() ) . ' GMT' ); |
| 492 | header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + YEAR_IN_SECONDS ) . ' GMT' ); |
| 493 | header( 'Etag: ' . md5( $content ) ); |
| 494 | } |
| 495 | |
| 496 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 497 | die( $content ); |
| 498 | } |
| 499 | |
| 500 | add_action( 'wp_ajax_kubio_style_manager_web_worker', 'kubio_print_style_manager_web_worker' ); |
| 501 | |
| 502 | // quick test for safari |
| 503 | add_action( |
| 504 | 'admin_init', |
| 505 | function () { |
| 506 | ob_start(); |
| 507 | ?> |
| 508 | <script> |
| 509 | window.requestIdleCallback = |
| 510 | window.requestIdleCallback || |
| 511 | function (cb) { |
| 512 | var start = Date.now(); |
| 513 | return setTimeout(function () { |
| 514 | cb({ |
| 515 | didTimeout: false, |
| 516 | timeRemaining: function () { |
| 517 | return Math.max(0, 50 - (Date.now() - start)); |
| 518 | }, |
| 519 | }); |
| 520 | }, 1); |
| 521 | }; |
| 522 | |
| 523 | window.cancelIdleCallback = |
| 524 | window.cancelIdleCallback || |
| 525 | function (id) { |
| 526 | clearTimeout(id); |
| 527 | }; |
| 528 | </script> |
| 529 | <?php |
| 530 | |
| 531 | $content = wp_strip_all_tags( ob_get_clean() ); |
| 532 | |
| 533 | wp_add_inline_script( 'wp-polyfill', $content, 'after' ); |
| 534 | } |
| 535 | ); |
| 536 | |
| 537 | function kubio_defer_kubio_scripts( $tag, $handle, $src ) { |
| 538 | |
| 539 | if ( is_admin() ) { |
| 540 | return $tag; |
| 541 | } |
| 542 | |
| 543 | if ( strpos( $src, kubio_url() ) === 0 ) { |
| 544 | $tag = str_replace( 'src=', 'defer src=', $tag ); |
| 545 | } |
| 546 | |
| 547 | return $tag; |
| 548 | } |
| 549 | |
| 550 | add_filter( 'script_loader_tag', 'kubio_defer_kubio_scripts', 10, 3 ); |
| 551 | |
| 552 | function kubio_defer_kubio_styles( $tag, $handle, $href, $media ) { |
| 553 | |
| 554 | if ( is_admin() ) { |
| 555 | return $tag; |
| 556 | } |
| 557 | |
| 558 | $deferrable_handles = array( 'kubio-google-fonts', 'kubio-third-party-blocks' ); |
| 559 | |
| 560 | if ( in_array( $handle, $deferrable_handles ) ) { |
| 561 | $tag = preg_replace( "#rel='(.*?)'#", 'rel="preload" as="style" onload="this.onload=null;this.rel=\'$1\'"', $tag ); |
| 562 | // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet |
| 563 | $tag .= "<noscript><link rel='stylesheet' href='{$href}' media='{$media}'></noscript>"; |
| 564 | } |
| 565 | |
| 566 | return $tag; |
| 567 | } |
| 568 | |
| 569 | add_filter( 'style_loader_tag', 'kubio_defer_kubio_styles', 10, 4 ); |
| 570 |