options = get_option( 'aio_options', [] ); $perf_active = aio_is_module_active( 'opt-module-performance' ); $seo_active = aio_is_module_active( 'opt-module-optimizer' ); add_filter( 'cron_schedules', [ $this, 'add_cron_intervals' ] ); /* ------------------------------------------------------------------ * A. Speed & Media Optimization (tied to Speed & Optimize module) * ------------------------------------------------------------------ */ if ( $perf_active ) { if ( ! empty( $this->options['opt-perf-webp'] ) ) { add_filter( 'wp_generate_attachment_metadata', [ $this, 'schedule_webp_conversion' ], 10, 2 ); add_action( 'aio_convert_webp_cron', [ $this, 'process_webp_conversion' ] ); } add_action( 'aio_process_webp_bulk_queue', [ $this, 'process_webp_bulk_queue' ] ); if ( ! empty( $this->options['opt-perf-lazyload'] ) && ! aio_is_speed_safe_mode() ) { add_filter( 'wp_lazy_loading_enabled', '__return_true' ); } // Note: Smart LazyLoad, Smart LCP, Auto Image Dimensions, Font Swap, YouTube Facade, // Delay JS, and Safe HTML Minify are executed in a single unified pass by AIO_Speed_HTML // inside modules/performance/class-aio-speed-html.php. if ( ! empty( $this->options['optEnableMediaReplace'] ) ) { add_filter( 'attachment_fields_to_edit', [ $this, 'add_media_replace_field' ], 10, 2 ); add_action( 'edit_attachment', [ $this, 'process_media_replace' ] ); } require_once dirname( __FILE__ ) . '/class-aio-caching.php'; new AIO_Caching(); } /* ------------------------------------------------------------------ * B. On-Page & Technical SEO (tied to SEO Pro module) * Note: 404 Monitor & 301 Redirects are unified in AIO_Utility. * ------------------------------------------------------------------ */ if ( $seo_active ) { if ( ! empty( $this->options['optEnableAutoAltTags'] ) ) { add_filter( 'wp_get_attachment_image_attributes', [ $this, 'auto_fill_alt_tags' ], 10, 3 ); } $is_third_party_seo = defined( 'WPSEO_VERSION' ) || defined( 'RANK_MATH_VERSION' ) || class_exists( 'All_in_One_SEO_Pack' ); if ( ! $is_third_party_seo ) { if ( ! empty( $this->options['optEnableMetaTags'] ) ) { add_action( 'wp_head', [ $this, 'generate_meta_tags' ], 1 ); } if ( ! empty( $this->options['optEnableOpenGraph'] ) ) { add_filter( 'language_attributes', [ $this, 'add_og_xml_ns' ] ); add_action( 'wp_head', [ $this, 'generate_open_graph' ], 2 ); } if ( ! empty( $this->options['optEnableSitemap'] ) ) { add_action( 'init', [ $this, 'register_sitemap_rewrite' ] ); add_filter( 'query_vars', [ $this, 'register_sitemap_query_var' ] ); add_action( 'template_redirect', [ $this, 'render_sitemap' ], 1 ); } if ( ! empty( $this->options['optEnableSchema'] ) ) { add_action( 'wp_head', [ $this, 'generate_schema_markup' ], 2 ); } } } } public function schedule_webp_conversion( $meta, $attachment_id ) { if ( ! wp_next_scheduled( 'aio_convert_webp_cron', [ $attachment_id ] ) ) { wp_schedule_single_event( time() + 5, 'aio_convert_webp_cron', [ $attachment_id ] ); } return $meta; } public function add_cron_intervals( $schedules ) { $schedules['aio_every_minute'] = [ 'interval' => 60, 'display' => __( 'Every Minute', 'aio-tools' ), ]; return $schedules; } public function process_webp_conversion( $attachment_id ) { $delete_original = ! empty( $this->options['opt-perf-webp-delete-original'] ); aio_generate_webp_for_attachment( $attachment_id, $delete_original ); } public function process_webp_bulk_queue() { $stats = get_option( 'aio_webp_bulk_stats' ); if ( ! $stats || empty( $stats['is_running'] ) || empty( $stats['queue'] ) ) { wp_clear_scheduled_hook( 'aio_process_webp_bulk_queue' ); return; } $batch = array_splice( $stats['queue'], 0, 10 ); $delete_original = ! empty( $this->options['opt-perf-webp-delete-original'] ); foreach ( $batch as $id ) { $result = aio_generate_webp_for_attachment( $id, $delete_original ); $stats['processed']++; if ( is_array( $result ) && ! empty( $result['success'] ) ) { $stats['original_bytes'] += $result['original_size']; $stats['webp_bytes'] += $result['webp_size']; update_post_meta( $id, '_aio_webp_converted', true ); } else { update_post_meta( $id, '_aio_webp_converted', 'failed_or_skipped' ); } } if ( empty( $stats['queue'] ) ) { $stats['is_running'] = false; wp_clear_scheduled_hook( 'aio_process_webp_bulk_queue' ); } update_option( 'aio_webp_bulk_stats', $stats, false ); } public function force_lazy_load_content( $content ) { if ( empty( $content ) || ! is_string( $content ) ) { return $content; } return preg_replace_callback( '/<(img|iframe)\b[^>]+>/i', function ( $matches ) { $tag = $matches[0]; if ( stripos( $tag, 'loading=' ) === false ) { $tag = preg_replace( '/^<(' . preg_quote( $matches[1], '/' ) . ')/i', '<$1 loading="lazy"', $tag, 1 ); } return $tag; }, $content ); } public function start_minify_buffer() { if ( ! is_admin() && ! is_user_logged_in() && ! is_feed() && ! is_preview() ) { ob_start( [ $this, 'minify_html_output' ] ); } } /** * Safe HTML Minifier: preserves ' . "\n"; return; } if ( ! is_singular( 'post' ) ) { return; } global $post; $schema = [ '@context' => 'https://schema.org', '@type' => 'Article', 'headline' => get_the_title(), 'datePublished' => get_the_date( 'c' ), 'dateModified' => get_the_modified_date( 'c' ), 'author' => [ '@type' => 'Person', 'name' => get_the_author_meta( 'display_name', $post->post_author ), ], 'publisher' => [ '@type' => 'Organization', 'name' => get_bloginfo( 'name' ), ], ]; if ( has_post_thumbnail() ) { $schema['image'] = get_the_post_thumbnail_url( $post->ID, 'full' ); } echo '' . "\n"; } } new AIO_Optimizer(); if ( ! function_exists( 'aio_convert_single_file_to_webp' ) ) { function aio_convert_single_file_to_webp( $src_path, $webp_path, $mime_type ) { if ( ! file_exists( $src_path ) ) { return false; } $success = false; if ( function_exists( 'imagewebp' ) ) { $image = null; if ( $mime_type === 'image/jpeg' && function_exists( 'imagecreatefromjpeg' ) ) { $image = @imagecreatefromjpeg( $src_path ); } elseif ( $mime_type === 'image/png' && function_exists( 'imagecreatefrompng' ) ) { $image = @imagecreatefrompng( $src_path ); } if ( $image ) { if ( function_exists( 'imagepalettetotruecolor' ) ) { @imagepalettetotruecolor( $image ); } if ( $mime_type === 'image/png' ) { @imagealphablending( $image, false ); @imagesavealpha( $image, true ); } $success = @imagewebp( $image, $webp_path, 85 ); @imagedestroy( $image ); } } if ( ! $success || ! file_exists( $webp_path ) || filesize( $webp_path ) < 100 ) { $editor = wp_get_image_editor( $src_path ); if ( ! is_wp_error( $editor ) ) { $editor->set_quality( 85 ); $saved = $editor->save( $webp_path, 'image/webp' ); $success = ! is_wp_error( $saved ); } } return $success && file_exists( $webp_path ) && filesize( $webp_path ) > 100; } } if ( ! function_exists( 'aio_generate_webp_for_attachment' ) ) { function aio_generate_webp_for_attachment( $attachment_id, $delete_original = false ) { $file_path = get_attached_file( $attachment_id ); if ( ! $file_path || ! file_exists( $file_path ) ) { return false; } $filetype = wp_check_filetype( $file_path ); $mime_type = ! empty( $filetype['type'] ) ? $filetype['type'] : ( function_exists( 'mime_content_type' ) ? mime_content_type( $file_path ) : '' ); if ( ! in_array( $mime_type, [ 'image/jpeg', 'image/png' ], true ) ) { return false; } $path_info = pathinfo( $file_path ); $base_dir = $path_info['dirname']; $webp_path = $base_dir . '/' . $path_info['filename'] . '.webp'; if ( ! aio_convert_single_file_to_webp( $file_path, $webp_path, $mime_type ) ) { return [ 'success' => false ]; } $total_orig_bytes = (int) filesize( $file_path ); $total_webp_bytes = (int) filesize( $webp_path ); // Also convert all generated WordPress & WooCommerce intermediate image sizes $metadata = wp_get_attachment_metadata( $attachment_id ); $converted_files = []; if ( is_array( $metadata ) ) { if ( ! empty( $metadata['file'] ) ) { $meta_dir = pathinfo( $metadata['file'], PATHINFO_DIRNAME ); $metadata['file'] = ( $meta_dir && $meta_dir !== '.' ? $meta_dir . '/' : '' ) . $path_info['filename'] . '.webp'; } if ( ! empty( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) { foreach ( $metadata['sizes'] as $size_key => $size_info ) { if ( empty( $size_info['file'] ) ) { continue; } $sub_src = $base_dir . '/' . $size_info['file']; $sub_info = pathinfo( $sub_src ); $sub_webp = $base_dir . '/' . $sub_info['filename'] . '.webp'; if ( isset( $converted_files[ $sub_src ] ) ) { $metadata['sizes'][ $size_key ]['file'] = basename( $converted_files[ $sub_src ] ); $metadata['sizes'][ $size_key ]['mime-type'] = 'image/webp'; continue; } if ( file_exists( $sub_src ) && aio_convert_single_file_to_webp( $sub_src, $sub_webp, $mime_type ) ) { $total_orig_bytes += (int) filesize( $sub_src ); $total_webp_bytes += (int) filesize( $sub_webp ); $converted_files[ $sub_src ] = $sub_webp; $metadata['sizes'][ $size_key ]['file'] = basename( $sub_webp ); $metadata['sizes'][ $size_key ]['mime-type'] = 'image/webp'; } } } wp_update_attachment_metadata( $attachment_id, $metadata ); } update_attached_file( $attachment_id, $webp_path ); global $wpdb; $wpdb->update( $wpdb->posts, [ 'post_mime_type' => 'image/webp' ], [ 'ID' => $attachment_id ], [ '%s' ], [ '%d' ] ); if ( $delete_original ) { foreach ( array_keys( $converted_files ) as $old_thumb ) { if ( $old_thumb !== $webp_path && file_exists( $old_thumb ) ) { wp_delete_file( $old_thumb ); } } if ( $file_path !== $webp_path && file_exists( $file_path ) ) { wp_delete_file( $file_path ); } } return [ 'success' => true, 'original_size' => $total_orig_bytes, 'webp_size' => $total_webp_bytes, ]; } }