max_header_size = 1024 * 4; $this->header_size_accumulator = 0; $this->options = get_option( awpp_get_instance()->Settings->settings_option ); $this->serverpush_scan_action = 'awpp_scan_htaccess_push'; $this->serverpush_possfiles_option = 'awpp_serverpush_possible_files'; } public function run() { add_action( 'wp_ajax_' . $this->serverpush_scan_action, [ $this, 'ajax_get_frontpage_files' ] ); add_action( 'update_option_' . awpp_get_instance()->Settings->settings_option, [ $this, 'add_serverpush_htaccess_onoption' ], 100, 2 ); if ( 'php' == $this->options['serverpush'] && ! is_admin() ) { add_action( 'init', [ $this, 'ob_start' ] ); add_filter( 'script_loader_src', [ $this, 'link_preload_header' ], 99, 1 ); add_filter( 'style_loader_src', [ $this, 'link_preload_header' ], 99, 1 ); if ( $this->should_render_prefetch_headers() ) { add_action( 'wp_head', [ $this, 'resource_hints' ], 99, 1 ); } } } public function ajax_get_frontpage_files() { $add = $this->scan_frontpage_files(); awpp_exit_ajax( 'success', 'test', $add ); } public function add_serverpush_htaccess_onoption( $oldvalue, $newvalue ) { $this->add_serverpush_htaccess( $newvalue ); } public function ob_start() { ob_start(); } public function link_preload_header( $src ) { if ( strpos( $src, home_url() ) !== false ) { $preload_src = apply_filters( 'awpp_link_preload_src', $src ); if ( ! empty( $preload_src ) ) { $header = sprintf( 'Link: <%s>; rel=preload; as=%s', esc_url( $this->link_url_to_relative_path( $preload_src ) ), sanitize_html_class( $this->link_resource_hint_as( current_filter() ) ) ); if ( ( $this->header_size_accumulator + strlen( $header ) ) < $this->max_header_size ) { $this->header_size_accumulator += strlen( $header ); header( $header, false ); } $GLOBALS[ 'awpp_' . $this->link_resource_hint_as( current_filter() ) . '_srcs' ][] = $this->link_url_to_relative_path( $preload_src ); } } return $src; } public function resource_hints() { $resource_types = array( 'script', 'style' ); array_walk( $resource_types, function ( $resource_type ) { $resources = $this->get_resources( $GLOBALS, $resource_type ); array_walk( $resources, function ( $src ) use ( $resource_type ) { printf( '', esc_url( $src ), esc_html( $resource_type ) ); } ); } ); } /** * PHP Helpers */ public function get_resources( $globals = null, $resource_type ) { $globals = ( null === $globals ) ? $GLOBALS : $globals; $resource_type_key = "awpp_{$resource_type}_srcs"; if ( ! ( is_array( $globals ) && isset( $globals[ $resource_type_key ] ) ) ) { return array(); } elseif ( ! is_array( $globals[ $resource_type_key ] ) ) { return array( $globals[ $resource_type_key ] ); } else { return $globals[ $resource_type_key ]; } } public function link_url_to_relative_path( $src ) { return '//' === substr( $src, 0, 2 ) ? preg_replace( '/^\/\/([^\/]*)\//', '/', $src ) : preg_replace( '/^http(s)?:\/\/[^\/]*/', '', $src ); } public function should_render_prefetch_headers() { return apply_filters( 'awpp_render_resource_hints', ! function_exists( 'wp_resource_hints' ) ); } public function link_resource_hint_as( $current_hook ) { return 'style_loader_src' === $current_hook ? 'style' : 'script'; } /** * Server Push Helpers */ public function scan_frontpage_files() { $return = []; $file = file_get_contents( get_home_url() ); //echo $file; //awpp_exit_ajax( 'error', $file ); $attr_regex = '/([a-zA-Z0-9-]+)="([^"]+)"/'; $styles_regex = '//'; $scripts_regex = '/