/** * Checks if we can defer JS * * @since 3.8 * * @return boolean */ private function can_defer_js(): bool { if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) { return false; } if ( ! $this->options->get( 'defer_all_js', 0 ) ) { return false; } return ! is_rocket_post_excluded_option( 'defer_all_js' ); } /** * Get list of JS files to be excluded from defer JS. * * @since 3.8 * * @return array */ public function get_excluded(): array { if ( ! $this->can_defer_js() ) { return []; } $exclude_defer_js = array_unique( array_merge( $this->get_external_exclusions(), $this->options->get( 'exclude_defer_js', [] ) ) ); /** * Filter list of Deferred JavaScript files * * @since 2.10 * * @param array $exclude_defer_js An array of URLs for the JS files to be excluded. */ $exclude_defer_js = apply_filters( 'rocket_exclude_defer_js', $exclude_defer_js ); foreach ( $exclude_defer_js as $i => $exclude ) { $exclude_defer_js[ $i ] = str_replace( '#', '\#', $exclude ); } return $exclude_defer_js; } /** * Excludes jQuery from combine JS when defer and combine are enabled * * @since 3.8 * * @param array $excluded_files Array of excluded files from combine JS. * @return array */ public function exclude_jquery_combine( array $excluded_files ): array { if ( ! $this->can_defer_js() ) { return $excluded_files; } if ( ! (bool) $this->options->get( 'minify_concatenate_js', 0 ) ) { return $excluded_files; } $excluded_files[] = '/jquery-?[0-9.]*(.min|.slim|.slim.min)?.js'; return $excluded_files; } /** * Adds jQuery to defer JS exclusion field if safe mode was enabled before 3.8 * * @since 3.8 * * @return void */ public function exclude_jquery_upgrade() { $options = get_option( 'wp_rocket_settings' ); if ( ! isset( $options['defer_all_js_safe'] ) ) { return; } if ( ! (bool) $options['defer_all_js_safe'] ) { return; } $options['exclude_defer_js'][] = '/jquery-?[0-9.]*(.min|.slim|.slim.min)?.js'; update_option( 'wp_rocket_settings', $options ); } /** * Get exclusion list pattern. * * @return string */ private function get_inline_exclusions_list_pattern() { $inline_exclusions_list = $this->get_inline_exclusions(); /** * Filters the patterns used to find inline JS that should not be deferred * * @since 3.8 * * @param array $inline_exclusions_list Array of inline JS that should not be deferred. */ $additional_inline_exclusions_list = apply_filters( 'rocket_defer_inline_exclusions', [] ); $inline_exclusions = ''; // Check if filter return is string so convert it to array for backward compatibility. // @phpstan-ignore-next-line - Ignoring the safeguard as the result could be mixed. if ( is_string( $additional_inline_exclusions_list ) ) { $additional_inline_exclusions_list = explode( '|', $additional_inline_exclusions_list ); } // Cast filter return to array. $inline_exclusions_list = array_merge( $inline_exclusions_list, (array) $additional_inline_exclusions_list ); foreach ( $inline_exclusions_list as $inline_exclusions_item ) { $inline_exclusions .= preg_quote( (string) $inline_exclusions_item, '#' ) . '|'; } return rtrim( $inline_exclusions, '|' ); } /** * Get inline exclusions list * * @return array */ private function get_inline_exclusions(): array { $lists = $this->data_manager->get_lists(); return isset( $lists->defer_js_inline_exclusions ) ? $lists->defer_js_inline_exclusions : []; } /** * Get external exclusions list * * @return array */ private function get_external_exclusions(): array { $lists = $this->data_manager->get_lists(); return isset( $lists->defer_js_external_exclusions ) ? $lists->defer_js_external_exclusions : []; } } /** * Checks if we can defer JS * * @since 3.8 * * @return boolean */ private function can_defer_js(): bool { if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) { return false; } if ( ! $this->options->get( 'defer_all_js', 0 ) ) { return false; } return ! is_rocket_post_excluded_option( 'defer_all_js' ); } /** * Get list of JS files to be excluded from defer JS. * * @since 3.8 * * @return array */ public function get_excluded(): array { if ( ! $this->can_defer_js() ) { return []; } $exclude_defer_js = array_unique( array_merge( $this->get_external_exclusions(), $this->options->get( 'exclude_defer_js', [] ) ) ); /** * Filter list of Deferred JavaScript files * * @since 2.10 * * @param array $exclude_defer_js An array of URLs for the JS files to be excluded. */ $exclude_defer_js = apply_filters( 'rocket_exclude_defer_js', $exclude_defer_js ); foreach ( $exclude_defer_js as $i => $exclude ) { $exclude_defer_js[ $i ] = str_replace( '#', '\#', $exclude ); } return $exclude_defer_js; } /** * Excludes jQuery from combine JS when defer and combine are enabled * * @since 3.8 * * @param array $excluded_files Array of excluded files from combine JS. * @return array */ public function exclude_jquery_combine( array $excluded_files ): array { if ( ! $this->can_defer_js() ) { return $excluded_files; } if ( ! (bool) $this->options->get( 'minify_concatenate_js', 0 ) ) { return $excluded_files; } $excluded_files[] = '/jquery-?[0-9.]*(.min|.slim|.slim.min)?.js'; return $excluded_files; } /** * Adds jQuery to defer JS exclusion field if safe mode was enabled before 3.8 * * @since 3.8 * * @return void */ public function exclude_jquery_upgrade() { $options = get_option( 'wp_rocket_settings' ); if ( ! isset( $options['defer_all_js_safe'] ) ) { return; } if ( ! (bool) $options['defer_all_js_safe'] ) { return; } $options['exclude_defer_js'][] = '/jquery-?[0-9.]*(.min|.slim|.slim.min)?.js'; update_option( 'wp_rocket_settings', $options ); } /** * Get exclusion list pattern. * * @return string */ private function get_inline_exclusions_list_pattern() { $inline_exclusions_list = $this->get_inline_exclusions(); /** * Filters the patterns used to find inline JS that should not be deferred * * @since 3.8 * * @param array $inline_exclusions_list Array of inline JS that should not be deferred. */ $additional_inline_exclusions_list = apply_filters( 'rocket_defer_inline_exclusions', [] ); $inline_exclusions = ''; // Check if filter return is string so convert it to array for backward compatibility. // @phpstan-ignore-next-line - Ignoring the safeguard as the result could be mixed. if ( is_string( $additional_inline_exclusions_list ) ) { $additional_inline_exclusions_list = explode( '|', $additional_inline_exclusions_list ); } // Cast filter return to array. $inline_exclusions_list = array_merge( $inline_exclusions_list, (array) $additional_inline_exclusions_list ); foreach ( $inline_exclusions_list as $inline_exclusions_item ) { $inline_exclusions .= preg_quote( (string) $inline_exclusions_item, '#' ) . '|'; } return rtrim( $inline_exclusions, '|' ); } /** * Get inline exclusions list * * @return array */ private function get_inline_exclusions(): array { $lists = $this->data_manager->get_lists(); return isset( $lists->defer_js_inline_exclusions ) ? $lists->defer_js_inline_exclusions : []; } /** * Get external exclusions list * * @return array */ private function get_external_exclusions(): array { $lists = $this->data_manager->get_lists(); return isset( $lists->defer_js_external_exclusions ) ? $lists->defer_js_external_exclusions : []; } }