/** * PNP ULTIMATE MASTER SPEED OPTIMIZER - v8.0 (FINAL WHOLESOME VERSION) * 1. Admin Fast-Lane (Fixes Update Button & Elementor) * 2. A+ Grade Asset Diet (GTmetrix Fix) * 3. Instant Typing (No Spinner Lag) * 4. PK Phone Checker & Validation (03XXXXXXXXX) * 5. Instant Celebration Popup & Confetti */ /* ========================================================================== 1. ADMIN & EDITOR FAST-LANE (PREVENTS LOOPS & 403 ERRORS) ========================================================================== */ if ( is_admin() || current_user_can('edit_posts') || isset($_GET['elementor-preview']) ) { // Stop RankMath Content AI background requests (Fixes 403 errors in logs) add_filter( 'rank_math/modules/content_ai', '__return_false' ); // Stop WooCommerce Analytics noise in the backend add_filter( 'woocommerce_admin_features', function( $features ) { return array_diff( $features, ['analytics', 'remote-inbox-notifications', 'onboarding'] ); }); // Speed up the "Update" button signal add_filter( 'nonce_life', function () { return 48 * HOUR_IN_SECONDS; } ); // Silence output buffer notices remove_action( 'shutdown', 'wp_ob_end_flush_all', 1 ); // Preload hero image even for admins to see it fast add_action( 'wp_head', function() { echo ''; }, 1 ); return; // STOP HERE FOR ADMINS - DO NOT RUN RESTRICTIONS } /* ========================================================================== 2. VISITOR SPEED: CORE & ASSET CLEANUP ========================================================================== */ add_action( 'init', function() { wp_deregister_script('heartbeat'); remove_action( 'shutdown', 'wp_ob_end_flush_all', 1 ); }, 1); add_action( 'wp_enqueue_scripts', function() { // A. Remove Gutenberg Bloat (Visitors only) wp_dequeue_style( 'wp-block-library' ); wp_dequeue_style( 'classic-theme-styles' ); // B. Clean Home for GTmetrix if ( is_front_page() ) { wp_dequeue_script( 'contact-form-7' ); wp_dequeue_style( 'contact-form-7' ); wp_dequeue_script( 'sbr-scripts' ); wp_dequeue_style( 'sbr_styles' ); } // C. Clean Checkout Page (Kills the 1.34s Fragments delay) if ( is_checkout() || is_order_received_page() ) { $killers = ['wc-cart-fragments','xt-woofc-js','sbi_scripts','cffscripts','trustindex-js','google-listings-and-ads']; foreach ( $killers as $h ) { wp_dequeue_script( $h ); wp_deregister_script( $h ); } wp_dequeue_style( 'xt-woofc-css' ); } }, 9999); /* ========================================================================== 3. CHECKOUT: INSTANT TYPING & PK PHONE VALIDATION ========================================================================== */ // A. Disable Spinner Lag on input add_filter( 'woocommerce_checkout_fields', function( $fields ) { $instant = ['billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone']; foreach ( $instant as $f ) { if ( isset( $fields['billing'][$f]['class'] ) ) { $fields['billing'][$f]['class'] = array_diff($fields['billing'][$f]['class'], ['address-field', 'update_totals_on_change']); } } return $fields; }, 999); // B. Server-Side Check for 11 digits starting with 03 add_action('woocommerce_checkout_process', function() { if (isset($_POST['billing_phone'])) { $phone = str_replace([' ', '-', '+'], '', $_POST['billing_phone']); if ( !preg_match('/^03[0-9]{9}$/', $phone) ) { wc_add_notice( 'Phone Number Error: Please enter exactly 11 digits starting with 03 (e.g. 03001234567). No spaces or +92 allowed.', 'error' ); } } }); /* ========================================================================== 4. FRONT-END: REAL-TIME CHECKER + CELEBRATION ========================================================================== */ add_action( 'wp_footer', function() { if ( ! is_checkout() ) return; ?>