class-aawp-ad.php
1 week ago
class-aawp.php
1 year ago
class-admin-compatibility.php
1 week ago
class-capability-manager.php
1 year ago
class-compatibility.php
1 week ago
class-inline-js.php
1 year ago
class-peepso-ad.php
1 week ago
class-peepso.php
1 week ago
class-inline-js.php
116 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Compatibility Inline JS. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.48.2 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Compatibility; |
| 11 | |
| 12 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Compatibility Inline JS. |
| 18 | */ |
| 19 | class Inline_JS implements Integration_Interface { |
| 20 | |
| 21 | /** |
| 22 | * Array that holds strings that should not be optimized by other plugins. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | private $inline_js; |
| 27 | |
| 28 | /** |
| 29 | * Hook into WordPress. |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public function hooks(): void { |
| 34 | $this->critical_inline_js(); |
| 35 | |
| 36 | add_filter( 'rocket_delay_js_exclusions', [ $this, 'rocket_exclude_inline_js' ] ); |
| 37 | add_filter( 'rocket_excluded_inline_js_content', [ $this, 'rocket_exclude_inline_js' ] ); |
| 38 | add_filter( $this->get_cmplz_hook(), [ $this, 'complianz_exclude_inline_js' ], 10, 2 ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Prevent the 'advanced_ads_ready' function declaration from being merged with other JS |
| 43 | * and outputted into the footer. This is needed because WP Rocket does not output all |
| 44 | * the code that depends on this function into the footer. |
| 45 | * |
| 46 | * @param array $exclusions Patterns to match in inline JS content. |
| 47 | * |
| 48 | * @return array |
| 49 | */ |
| 50 | public function rocket_exclude_inline_js( $exclusions ): array { |
| 51 | return array_merge( $exclusions, $this->inline_js ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Prevent Complianz from suppressing our head inline script. |
| 56 | * |
| 57 | * @param string $classname The class Complianz adds to the script, `cmplz-script` for prevented scripts, `cmplz-native` for allowed. |
| 58 | * @param string $total_match The script string. |
| 59 | * |
| 60 | * @return string |
| 61 | */ |
| 62 | public function complianz_exclude_inline_js( $classname, $total_match ) { |
| 63 | // Early bail!! |
| 64 | if ( 'cmplz-native' === $classname ) { |
| 65 | return $classname; |
| 66 | } |
| 67 | |
| 68 | foreach ( $this->inline_js as $critical_inline_js ) { |
| 69 | if ( false !== strpos( $total_match, $critical_inline_js ) ) { |
| 70 | return 'cmplz-native'; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return $classname; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get an array of strings to exclude when plugins "optimize" JS. |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | private function critical_inline_js(): void { |
| 83 | $frontend_prefix = wp_advads()->get_frontend_prefix(); |
| 84 | $default = [ |
| 85 | sprintf( 'id="%sready"', $frontend_prefix ), |
| 86 | ]; |
| 87 | |
| 88 | /** |
| 89 | * Filters an array of strings of (inline) JavaScript "identifiers" that should not be "optimized"/delayed etc. |
| 90 | * |
| 91 | * @param array $default Array of excluded patterns. |
| 92 | */ |
| 93 | $exclusions = apply_filters( 'advanced-ads-compatibility-critical-inline-js', $default, $frontend_prefix ); |
| 94 | |
| 95 | if ( ! is_array( $exclusions ) ) { |
| 96 | $exclusions = $default; |
| 97 | } |
| 98 | |
| 99 | $this->inline_js = $exclusions; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get cmplz hook by version |
| 104 | * |
| 105 | * @return string |
| 106 | */ |
| 107 | private function get_cmplz_hook(): string { |
| 108 | $complianz_version = get_option( 'cmplz-current-version', false ); |
| 109 | if ( $complianz_version && version_compare( $complianz_version, '6.0.0', '>=' ) ) { |
| 110 | return 'cmplz_service_category'; |
| 111 | } |
| 112 | |
| 113 | return 'cmplz_script_class'; |
| 114 | } |
| 115 | } |
| 116 |