base_path = ABSPATH; $this->base_url = trailingslashit( get_site_url() ); $this->default_critical_path = trailingslashit( WP_CONTENT_DIR ) . 'cache/awpp/critical/'; if ( is_multisite() ) { $this->default_critical_path = trailingslashit( WP_CONTENT_DIR ) . 'cache/awpp/critical/' . get_current_blog_id() . '/'; } if ( ! file_exists( $this->default_critical_path ) ) { mkdir( $this->default_critical_path, 0777, true ); } } public function run() { add_action( 'awpp_settings', [ $this, 'register_settings' ] ); add_filter( 'awpp_sanitize_criticalcss', [ $this, 'minify_criticalcss' ] ); add_action( 'admin_bar_menu', [ $this, 'add_toolbar_item' ] ); if ( awpp_is_frontend() ) { add_action( 'wp_head', [ $this, 'add_critical_css' ], 1 ); } } public function register_settings() { $section = awpp_settings()->add_section( awpp_settings_page_assets(), 'ccss', __( 'Above the fold CSS', 'awpp' ) ); if ( ! defined( 'AWPP_CRITICALAPI' ) || ! AWPP_CRITICALAPI ) { if ( ! awpp_get_setting( 'deliverycss' ) ) { $content = '

' . __( 'Please enable "Optimize CSS Delivery". With a normal css delivery, this option is not necessary.', 'awpp' ) . '

'; awpp_settings()->add_message( $section, 'ccssmessage', __( 'Critical CSS', 'awpp' ), $content ); } else { $path = apply_filters( 'awpp_critical_dir', $this->default_critical_path ); if ( $this->default_critical_path != $path ) { $content = ''; // translators: Custom critical directory found: code $content .= '

' . sprintf( __( 'Custom critical directory found: %s', 'awpp' ), '' . $path . '' ) . '

'; if ( ! is_dir( $path ) ) { $content .= '

' . __( 'Folder does not exist!', 'awpp' ) . '

'; } if ( ! is_file( $path . 'index.css' ) ) { $content .= '

' . __( 'index.css does not exist!', 'awpp' ) . '

'; } awpp_settings()->add_message( $section, 'ccssmessage', __( 'Critical CSS', 'awpp' ), $content ); } else { $file = $this->default_critical_path . 'index.css'; if ( ! file_exists( $file ) ) { fopen( $file, 'w' ); } $file_url = str_replace( $this->base_path, $this->base_url, $file ); $val = file_get_contents( $file ); $args['after_field'] = "

File: $file_url

"; awpp_settings()->add_textarea( $section, 'criticalcss', __( 'Critical CSS', 'awpp' ), $val, $args ); } } // End if(). } // End if(). } public function minify_criticalcss( $css ) { $path = plugin_dir_path( awpp_get_instance()->file ) . 'Classes/Libs'; require_once $path . '/minify/autoload.php'; require_once $path . '/path-converter/autoload.php'; $minifier = new \MatthiasMullie\Minify\CSS( $css ); $minifier->minify( $this->default_critical_path . 'index.css' ); return $minifier->minify(); } /** * Toolbar */ public function add_toolbar_item( $wp_admin_bar ) { $html = ''; $html .= ''; $html .= ''; $args = [ 'id' => awpp_get_instance()->Init->admin_bar_id . '-criticalcss', 'parent' => awpp_get_instance()->Init->admin_bar_id, 'title' => __( 'Critical CSS', 'awpp' ), 'href' => '', 'meta' => [ 'class' => awpp_get_instance()->prefix . '-adminbar-criticalcss ' . ( awpp_get_setting( 'deliverycss' ) ? '' : 'disabled' ), ], ]; if ( awpp_is_frontend() && awpp_get_setting( 'deliverycss' ) ) { $args['meta']['html'] = '
' . $html . '
'; } $wp_admin_bar->add_node( $args ); } /** * Header Output */ public function add_critical_css() { if ( ! awpp_get_setting( 'deliverycss' ) ) { return; } $path = apply_filters( 'awpp_critical_dir', $this->default_critical_path ); $critical_id = ''; $ids = array_reverse( awpp_get_critical_keys() ); foreach ( $ids as $id ) { if ( file_exists( $path . $id . '.css' ) ) { $critical_id = $id; break; } } $content = ''; $content .= "/*\n"; $content .= "Critical CSS: set by Advanced WP Performance.\n"; $ccss_path = $path . $critical_id . '.css'; $ccss_url = str_replace( trailingslashit( ABSPATH ), trailingslashit( get_site_url() ), $ccss_path ); if ( is_user_logged_in() ) { $content .= "\nDebug Information (for logged in users):\n"; $content .= "- Critical ID: $critical_id\n"; $content .= "- File: $ccss_url\n"; if ( ! file_exists( $ccss_path ) ) { $content .= "\nError: File does not exist!\n"; } } if ( '' == $critical_id ) { $content .= "\nError: Critical CSS File not found!\n"; $content .= "*/\n"; } else { $content .= "*/\n"; if ( file_exists( $ccss_path ) ) { $content .= file_get_contents( $ccss_path ); } } echo "\n"; } }