array(), 'body' => array(), 'delayed' => 0, 'hosts' => array(), 'yt' => 0, 'lcp' => '', ); $html = apply_filters( 'tibospeed_html_before', $html ); // içine dokunma. $keep = array(); $html = preg_replace_callback( '##is', function ( $m ) use ( &$keep ) { $keep[] = $m[0]; return ''; }, $html ); if ( 'off' !== tibospeed_get( 'js_delay' ) ) { $html = tibospeed_opt_scripts( $html ); } $html = tibospeed_opt_styles( $html ); $html = tibospeed_opt_media( $html ); $html = tibospeed_inject( $html ); $html = preg_replace_callback( '##', function ( $m ) use ( $keep ) { return $keep[ (int) $m[1] ]; }, $html ); if ( tibospeed_get( 'html_minify' ) ) { $html = tibospeed_minify_html( $html ); } return apply_filters( 'tibospeed_html_after', $html ); } /** * Etiket özniteliklerini ayrıştır. * * @param string $s Öznitelik metni. * @return array ad => değer (değersizlerde true) */ function tibospeed_attrs( $s ) { $a = array(); if ( preg_match_all( '/([^\s=\/>]+)(?:\s*=\s*(?:"([^"]*)"|\'([^\']*)\'|([^\s>]+)))?/', (string) $s, $m, PREG_SET_ORDER ) ) { foreach ( $m as $x ) { $v = isset( $x[2] ) && '' !== $x[2] ? $x[2] : ( isset( $x[3] ) && '' !== $x[3] ? $x[3] : ( isset( $x[4] ) && '' !== $x[4] ? $x[4] : ( false !== strpos( $x[0], '=' ) ? '' : true ) ) ); $a[ strtolower( $x[1] ) ] = $v; } } return $a; } /** * Öznitelik dizisinden etiket aç. * * @param string $tag Etiket. * @param array $attrs Öznitelikler. * @return string */ function tibospeed_tag( $tag, $attrs ) { $s = '<' . $tag; foreach ( $attrs as $k => $v ) { $s .= true === $v ? ' ' . $k : ' ' . $k . '="' . str_replace( '"', '"', (string) $v ) . '"'; } return $s . '>'; } /** * Adres bu sitenin mi? * * @param string $url Adres. * @return bool */ function tibospeed_is_local( $url ) { if ( 0 === strpos( $url, '//' ) ) { $url = 'https:' . $url; } if ( ! preg_match( '#^https?://#i', $url ) ) { return true; } return strtolower( (string) wp_parse_url( $url, PHP_URL_HOST ) ) === strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ); } /* -------------------------------------------------------------------------- JavaScript */ /** * Bilinen üçüncü taraf (analiz, reklam, piksel, sohbet) kod imzaları. * * @return string[] */ function tibospeed_third_party() { return apply_filters( 'tibospeed_third_party', array( 'googletagmanager.com', 'google-analytics.com', 'gtag(', 'ga(\'', 'gtm.js', 'dataLayer.push', 'connect.facebook.net', 'fbq(', 'fbevents', 'hotjar', 'clarity.ms', 'static.ads-twitter.com', 'twq(', 'snap.licdn.com', '_linkedin_partner_id', 'analytics.tiktok.com', 'ttq.', 'tawk.to', 'Tawk_API', 'client.crisp.chat', '$crisp', 'widget.intercom.io', 'Intercom(', 'code.jivosite.com', 'jivo_', 'static.zdassets.com', 'zE(', 'mc.yandex.ru', 'ym(', 'pagead2.googlesyndication.com', 'adsbygoogle', 'googleadservices.com', 'doubleclick.net', 'platform.twitter.com', 's.pinimg.com', 'pintrk(', 'addthis.com', 'sharethis.com', 'disqus.com', 'livechatinc.com', 'tidio', 'smartsupp', 'cdn.onesignal.com', 'sc-static.net', 'snaptr(', 'bat.bing.com', 'uetq', 'cdn.segment.com', 'js.hs-scripts.com', 'js.hsforms.net', 'cdn.mxpnl.com', ) ); } /** * Hiç geciktirilmeyecek kodlar (ayardaki listeye ek olarak). * * @return string[] */ function tibospeed_delay_never() { return array_merge( array( 'tibospeed', 'tibokvkk', 'speculationrules', 'application/ld+json', 'document.write', 'recaptcha', 'hcaptcha', 'turnstile', 'wc-cart-fragments' ), tibospeed_list( 'delay_exclude' ) ); } /** * Kod bu listedeki bir parçayı içeriyor mu? * * @param string $hay Aranacak metin. * @param string[] $list Parçalar. * @return bool */ function tibospeed_match_any( $hay, $list ) { foreach ( $list as $p ) { if ( '' !== $p && false !== stripos( $hay, $p ) ) { return true; } } return false; } /** * Sayfa geciktirmeye uygun mu? (sepet / ödeme gibi sayfalarda JS hemen çalışmalı) * * @return bool */ function tibospeed_delay_allowed() { if ( function_exists( 'is_checkout' ) && ( is_checkout() || is_cart() || is_account_page() ) ) { return false; } return (bool) apply_filters( 'tibospeed_delay_allowed', true ); } /** * Script etiketleri. * * @param string $html Sayfa. * @return string */ function tibospeed_opt_scripts( $html ) { if ( ! tibospeed_delay_allowed() ) { return $html; } $mode = tibospeed_get( 'js_delay' ); $never = tibospeed_delay_never(); $third = tibospeed_third_party(); return preg_replace_callback( '##is', function ( $m ) use ( $mode, $never, $third ) { $a = tibospeed_attrs( $m[1] ); $type = isset( $a['type'] ) ? strtolower( trim( (string) $a['type'] ) ) : ''; if ( ! in_array( $type, array( '', 'text/javascript', 'application/javascript', 'module', 'text/ecmascript' ), true ) ) { return $m[0]; } if ( isset( $a['data-tibospeed-skip'] ) || isset( $a['data-no-optimize'] ) || isset( $a['nomodule'] ) ) { return $m[0]; } $src = isset( $a['src'] ) ? (string) $a['src'] : ''; $id = isset( $a['id'] ) ? (string) $a['id'] : ''; $body = $m[2]; $sig = $src . ' ' . $id . ' ' . substr( $body, 0, 3000 ); if ( tibospeed_match_any( $sig, $never ) ) { return $m[0]; } if ( '' === $src && '' === trim( $body ) ) { return $m[0]; } $delay = 'all' === $mode ? true : tibospeed_match_any( $sig, $third ); $delay = (bool) apply_filters( 'tibospeed_delay_script', $delay, $src, $body, $a ); if ( ! $delay ) { if ( $src && ! tibospeed_is_local( $src ) ) { $GLOBALS['tibospeed_ctx']['hosts'][ wp_parse_url( ( 0 === strpos( $src, '//' ) ? 'https:' : '' ) . $src, PHP_URL_HOST ) ] = 1; } return $m[0]; } ++$GLOBALS['tibospeed_ctx']['delayed']; $b = array( 'type' => 'tibospeed/javascript', 'data-tibospeed-type' => '' === $type ? 'text/javascript' : $type, ); if ( $src ) { $b['data-tibospeed-src'] = $src; } foreach ( $a as $k => $v ) { if ( ! in_array( $k, array( 'type', 'src', 'async', 'defer' ), true ) ) { $b[ $k ] = $v; } } return tibospeed_tag( 'script', $b ) . $body . ''; }, $html ); } /** * Geciktirilen kodları ilk etkileşimde sırayla çalıştıran yükleyici. * * @return string */ function tibospeed_delay_loader() { $timeout = 'all' === tibospeed_get( 'js_delay' ) ? 0 : (int) tibospeed_get( 'delay_timeout' ) * 1000; $js = '(function(){var d=document,w=window,E=["keydown","mousedown","mousemove","touchstart","scroll","wheel","pointerdown"],done=0,t,q=[],dA=d.addEventListener,wA=w.addEventListener;' . 'function hk(o,orig){return function(e,f,x){if(e==="DOMContentLoaded"||e==="load"||e==="readystatechange"){q.push([o,f]);return}return orig.call(o,e,f,x)}}' . 'function go(){if(done)return;done=1;clearTimeout(t);E.forEach(function(e){w.removeEventListener(e,go,{passive:true})});' . 'var s=[].slice.call(d.querySelectorAll("script[type=\"tibospeed/javascript\"]"));d.addEventListener=hk(d,dA);w.addEventListener=hk(w,wA);var ol=w.onload;' . '(function n(){var o=s.shift();if(!o){d.addEventListener=dA;w.addEventListener=wA;q.forEach(function(x){try{x[1].call(x[0],new Event(x[0]===w?"load":"DOMContentLoaded"))}catch(e){console.error(e)}});if(w.onload&&w.onload!==ol){try{w.onload(new Event("load"))}catch(e){}}if(w.jQuery){try{w.jQuery(w).trigger("load")}catch(e){}}d.dispatchEvent(new CustomEvent("tibospeed:loaded"));return}' . 'var c=d.createElement("script");[].forEach.call(o.attributes,function(a){if(a.name!=="type"&&a.name!=="data-tibospeed-src"&&a.name!=="data-tibospeed-type")c.setAttribute(a.name,a.value)});c.type=o.getAttribute("data-tibospeed-type");' . 'if(o.hasAttribute("data-tibospeed-src")){c.src=o.getAttribute("data-tibospeed-src");c.async=false;c.onload=c.onerror=n;o.parentNode.replaceChild(c,o)}else{c.text=o.text;o.parentNode.replaceChild(c,o);n()}})()}' . 'E.forEach(function(e){w.addEventListener(e,go,{passive:true})});' . ( $timeout ? 't=setTimeout(go,' . $timeout . ');' : '' ) . 'w.tibospeedLoad=go})();'; return wp_get_inline_script_tag( $js, array( 'id' => 'tibospeed-delay', 'data-tibospeed-skip' => true, ) ); } /* -------------------------------------------------------------------------- CSS */ /** * Stil dosyaları ve satır içi stiller. * * @param string $html Sayfa. * @return string */ function tibospeed_opt_styles( $html ) { $html = preg_replace_callback( '#]*)>#i', function ( $m ) { $a = tibospeed_attrs( $m[1] ); if ( empty( $a['href'] ) || ! isset( $a['rel'] ) || 'stylesheet' !== strtolower( (string) $a['rel'] ) ) { return $m[0]; } $href = html_entity_decode( (string) $a['href'] ); if ( false !== strpos( $href, 'fonts.googleapis.com' ) ) { $local = tibospeed_get( 'fonts_local' ) ? tibospeed_font_local_url( $href ) : ''; if ( $local ) { $a['href'] = $local; return tibospeed_tag( 'link', $a ); } $GLOBALS['tibospeed_ctx']['hosts']['fonts.googleapis.com'] = 1; $GLOBALS['tibospeed_ctx']['hosts']['fonts.gstatic.com'] = 1; return $m[0]; } if ( ! tibospeed_is_local( $href ) ) { $GLOBALS['tibospeed_ctx']['hosts'][ wp_parse_url( ( 0 === strpos( $href, '//' ) ? 'https:' : '' ) . $href, PHP_URL_HOST ) ] = 1; return $m[0]; } if ( tibospeed_get( 'css_minify' ) ) { $min = tibospeed_min_css_url( $href ); if ( $min ) { $a['href'] = $min; return apply_filters( 'tibospeed_stylesheet_tag', tibospeed_tag( 'link', $a ), $a, $href ); } } return apply_filters( 'tibospeed_stylesheet_tag', $m[0], $a, $href ); }, $html ); if ( tibospeed_get( 'css_minify' ) ) { $html = preg_replace_callback( '#()#is', function ( $m ) { return $m[1] . tibospeed_minify_css( $m[2], home_url( '/' ) ) . $m[3]; }, $html ); } return $html; } /** * Yerel dosya yolu (eklenti, tema, wp-includes, yüklemeler). * * @param string $url Adres. * @return string boş: bulunamadı. */ function tibospeed_url_to_path( $url ) { $url = strtok( (string) $url, '?#' ); if ( 0 === strpos( $url, '//' ) ) { $url = ( is_ssl() ? 'https:' : 'http:' ) . $url; } if ( '/' === substr( $url, 0, 1 ) ) { $url = home_url( $url ); } $url = preg_replace( '#^https?:#i', '', $url ); $maps = array( preg_replace( '#^https?:#i', '', content_url() ) => WP_CONTENT_DIR, preg_replace( '#^https?:#i', '', includes_url() ) => ABSPATH . WPINC . '/', preg_replace( '#^https?:#i', '', site_url( '/' ) ) => ABSPATH, ); foreach ( $maps as $u => $p ) { if ( 0 === strpos( $url, $u ) ) { $path = rtrim( $p, '/' ) . '/' . ltrim( rawurldecode( substr( $url, strlen( $u ) ) ), '/' ); if ( false === strpos( $path, '..' ) && is_file( $path ) ) { return $path; } } } return ''; } /** * Küçültülmüş kopyanın adresi (yoksa oluşturur). * * @param string $href Orijinal adres. * @return string */ function tibospeed_min_css_url( $href ) { $path = tibospeed_url_to_path( $href ); if ( ! $path || '.css' !== substr( $path, -4 ) || filesize( $path ) > 3 * MB_IN_BYTES ) { return ''; } $name = sanitize_file_name( basename( $path, '.css' ) ) . '-' . substr( md5( $path . filemtime( $path ) . TIBOSPEED_VERSION . tibospeed_get( 'fonts_swap' ) ), 0, 10 ) . '.css'; $file = tibospeed_dir( 'min/' . $name ); if ( ! is_file( $file ) ) { $css = (string) tibospeed_read( $path ); $abs = preg_match( '#^https?://#', $href ) ? strtok( $href, '?' ) : home_url( strtok( $href, '?' ) ); if ( ! tibospeed_write( $file, tibospeed_minify_css( $css, $abs ) ) ) { return ''; } } return tibospeed_url( 'min/' . $name ); } /** * Güvenli CSS küçültme. Göreli adresler (url(), @import) mutlak yapılır. * * @param string $css CSS. * @param string $base Dosyanın adresi. * @return string */ function tibospeed_minify_css( $css, $base ) { $base = strtok( $base, '?' ); $dir = '/' === substr( $base, -1 ) ? $base : trailingslashit( dirname( $base ) ); $css = preg_replace_callback( '/url\(\s*([\'"]?)([^\'")]+)\1\s*\)/i', function ( $m ) use ( $dir ) { $u = trim( $m[2] ); if ( preg_match( '#^(data:|https?:|//|/|\#|%23|var\()#i', $u ) ) { return 'url(' . $m[1] . $u . $m[1] . ')'; } $abs = $dir . $u; while ( preg_match( '#/(?!\.\./)[^/]+/\.\./#', $abs ) ) { $abs = preg_replace( '#/(?!\.\./)[^/]+/\.\./#', '/', $abs, 1 ); } $abs = str_replace( '/./', '/', $abs ); return 'url(' . $m[1] . $abs . $m[1] . ')'; }, $css ); // Yorumlar (/*! lisans */ korunur). $css = preg_replace( '#/\*(?!!).*?\*/#s', '', $css ); $css = preg_replace( '/\s+/', ' ', $css ); $css = preg_replace( '/\s*([{};,])\s*/', '$1', $css ); $css = preg_replace( '/;}/', '}', $css ); if ( tibospeed_get( 'fonts_swap' ) ) { $css = preg_replace_callback( '/@font-face\s*{([^}]*)}/i', function ( $m ) { return false === stripos( $m[1], 'font-display' ) ? '@font-face{font-display:swap;' . $m[1] . '}' : $m[0]; }, $css ); } return trim( $css ); } /* -------------------------------------------------------------------------- Görsel ve video */ /** * Görseller, iframe'ler, LCP. * * @param string $html Sayfa. * @return string */ function tibospeed_opt_media( $html ) { $bpos = stripos( $html, ' '', 'i' => null, 'bg' => '', ) ); $imgs = array(); preg_match_all( '#]*>#i', $body, $all ); foreach ( $all[0] as $i => $tag ) { $imgs[ $i ] = tibospeed_attrs( substr( $tag, 4, -1 ) ); } $lcp_i = -1; $plain = function ( $a ) { $src = isset( $a['src'] ) ? (string) $a['src'] : ''; $sig = strtolower( $src . ' ' . ( isset( $a['class'] ) ? $a['class'] : '' ) . ' ' . ( isset( $a['alt'] ) ? $a['alt'] : '' ) ); $w = isset( $a['width'] ) ? (int) $a['width'] : 0; return $src && 0 !== strpos( $src, 'data:' ) && ! preg_match( '/logo|icon|avatar|emoji|gravatar|spinner|placeholder/', $sig ) && ! ( $w && $w < 200 ); }; if ( tibospeed_get( 'lcp' ) && empty( $hint['bg'] ) ) { if ( ! empty( $hint['src'] ) ) { $want = basename( strtok( (string) $hint['src'], '?' ) ); foreach ( $imgs as $i => $a ) { $src = isset( $a['src'] ) ? (string) $a['src'] : ''; if ( $src && ( basename( strtok( $src, '?' ) ) === $want || basename( strtok( $src, '?' ) ) . '.webp' === $want ) ) { $lcp_i = $i; break; } } } if ( $lcp_i < 0 && null !== $hint['i'] && isset( $imgs[ (int) $hint['i'] ] ) && $plain( $imgs[ (int) $hint['i'] ] ) ) { $lcp_i = (int) $hint['i']; } if ( $lcp_i < 0 ) { foreach ( $imgs as $i => $a ) { if ( $i > 5 ) { break; } if ( $plain( $a ) ) { $lcp_i = $i; break; } } } } if ( tibospeed_get( 'lcp' ) && ! empty( $hint['bg'] ) ) { $GLOBALS['tibospeed_ctx']['lcp'] = array( 'src' => (string) $hint['bg'] ); } $idx = -1; $eager = 0; $body = preg_replace_callback( '#]*)>#i', function ( $m ) use ( &$idx, &$eager, $lcp_i ) { ++$idx; $a = tibospeed_attrs( $m[1] ); if ( isset( $a['data-tibospeed-skip'] ) ) { return $m[0]; } if ( tibospeed_get( 'webp' ) ) { foreach ( array( 'src', 'srcset', 'data-src', 'data-srcset' ) as $k ) { if ( ! empty( $a[ $k ] ) ) { $a[ $k ] = tibospeed_webp_swap( (string) $a[ $k ] ); } } } if ( tibospeed_get( 'img_dims' ) && empty( $a['width'] ) && empty( $a['height'] ) && ! empty( $a['src'] ) ) { $d = tibospeed_img_size( (string) $a['src'] ); if ( $d ) { $a['width'] = $d[0]; $a['height'] = $d[1]; } } if ( $idx === $lcp_i ) { $a['fetchpriority'] = 'high'; $a['loading'] = 'eager'; unset( $a['decoding'] ); $GLOBALS['tibospeed_ctx']['lcp'] = $a; } elseif ( tibospeed_get( 'lazy' ) ) { if ( $eager < (int) tibospeed_get( 'lazy_skip' ) ) { ++$eager; } elseif ( ! isset( $a['loading'] ) && ! isset( $a['fetchpriority'] ) ) { $a['loading'] = 'lazy'; } if ( ! isset( $a['decoding'] ) ) { $a['decoding'] = 'async'; } } return tibospeed_tag( 'img', $a ); }, $body ); // kaynakları. if ( tibospeed_get( 'webp' ) ) { $body = preg_replace_callback( '#]*)>#i', function ( $m ) { $a = tibospeed_attrs( $m[1] ); if ( ! empty( $a['srcset'] ) && empty( $a['type'] ) ) { $a['srcset'] = tibospeed_webp_swap( (string) $a['srcset'] ); } return tibospeed_tag( 'source', $a ); }, $body ); } // iframe: YouTube önizlemesi ya da tembel yükleme. $body = preg_replace_callback( '#]*)>\s*#i', function ( $m ) { $a = tibospeed_attrs( $m[1] ); $src = isset( $a['src'] ) ? html_entity_decode( (string) $a['src'] ) : ''; if ( tibospeed_get( 'yt_facade' ) && preg_match( '#^(?:https?:)?//(?:www\.)?youtube(?:-nocookie)?\.com/embed/([A-Za-z0-9_\-]{6,})#', $src, $v ) ) { ++$GLOBALS['tibospeed_ctx']['yt']; $a['src'] = $src . ( false === strpos( $src, '?' ) ? '?' : '&' ) . 'autoplay=1'; unset( $a['loading'] ); $w = isset( $a['width'] ) && (int) $a['width'] ? (int) $a['width'] : 16; $h = isset( $a['height'] ) && (int) $a['height'] ? (int) $a['height'] : 9; $title = isset( $a['title'] ) ? (string) $a['title'] : 'YouTube'; return '' . tibospeed_tag( 'iframe', $a ) . ''; } if ( tibospeed_get( 'lazy' ) && ! isset( $a['loading'] ) ) { $a['loading'] = 'lazy'; return tibospeed_tag( 'iframe', $a ) . ''; } return $m[0]; }, $body ); return $head . $body; } /** * Görselin .webp kopyası varsa onu kullan (src ya da srcset). * * @param string $val Değer. * @return string */ function tibospeed_webp_swap( $val ) { $up = wp_get_upload_dir(); return preg_replace_callback( '#(?:https?:)?//[^\s,"\']+?\.(?:jpe?g|png)(?=[\s,?]|$)|/[^\s,"\']+?\.(?:jpe?g|png)(?=[\s,?]|$)#i', function ( $m ) use ( $up ) { $u = $m[0]; $norm = preg_replace( '#^https?:#i', '', $u ); $base = preg_replace( '#^https?:#i', '', $up['baseurl'] ); if ( '/' === $u[0] && '/' !== substr( $u, 1, 1 ) ) { $norm = preg_replace( '#^https?:#i', '', home_url( $u ) ); } if ( 0 !== strpos( $norm, $base ) ) { return $u; } $path = $up['basedir'] . rawurldecode( substr( $norm, strlen( $base ) ) ); return is_file( $path . '.webp' ) ? $u . '.webp' : $u; }, $val ); } /** * Yerel görselin boyutu (önbellekli). * * @param string $src Adres. * @return array|null [ genişlik, yükseklik ] */ function tibospeed_img_size( $src ) { static $n = 0; $key = md5( $src ); $dims = get_transient( 'tibospeed_dims' ); $dims = is_array( $dims ) ? $dims : array(); if ( isset( $dims[ $key ] ) ) { return $dims[ $key ] ? $dims[ $key ] : null; } if ( ++$n > 25 ) { return null; } $path = tibospeed_url_to_path( $src ); $d = null; if ( $path && preg_match( '/\.(jpe?g|png|gif|webp|avif)$/i', $path ) ) { $s = wp_getimagesize( $path ); $d = $s ? array( (int) $s[0], (int) $s[1] ) : null; } elseif ( $path && preg_match( '/\.svg$/i', $path ) ) { $svg = (string) tibospeed_read( $path ); if ( preg_match( '/viewBox="[\d.\s-]+?\s([\d.]+)\s([\d.]+)"/i', $svg, $vb ) ) { $d = array( (int) $vb[1], (int) $vb[2] ); } } $dims[ $key ] = $d ? $d : 0; set_transient( 'tibospeed_dims', array_slice( $dims, -2000, null, true ), WEEK_IN_SECONDS ); return $d; } /* -------------------------------------------------------------------------- Ekleme */ /** * ve eklemeleri: ön bağlantı, LCP ön yükleme, yükleyiciler. * * @param string $html Sayfa. * @return string */ function tibospeed_inject( $html ) { $ctx = $GLOBALS['tibospeed_ctx']; $head = ''; if ( ! empty( $ctx['lcp']['src'] ) && false === strpos( $html, 'rel="preload" as="image"' ) ) { $l = $ctx['lcp']; $link = array( 'rel' => 'preload', 'as' => 'image', 'href' => $l['src'], 'fetchpriority' => 'high', ); if ( ! empty( $l['srcset'] ) ) { $link['imagesrcset'] = $l['srcset']; $link['imagesizes'] = ! empty( $l['sizes'] ) ? $l['sizes'] : '100vw'; } $head .= tibospeed_tag( 'link', $link ); } if ( tibospeed_get( 'preconnect' ) ) { $n = 0; foreach ( array_keys( $ctx['hosts'] ) as $h ) { if ( ! $h || $n >= 4 || preg_match( '#]+preconnect[^>]+//' . preg_quote( $h, '#' ) . '#i', $html ) ) { continue; } $head .= ''; ++$n; } } $head .= implode( '', (array) apply_filters( 'tibospeed_head_inject', $ctx['head'] ) ); if ( $head ) { if ( preg_match( '#]*>#i', $html, $cm, PREG_OFFSET_CAPTURE ) ) { $at = $cm[0][1] + strlen( $cm[0][0] ); $html = substr( $html, 0, $at ) . $head . substr( $html, $at ); } else { $html = preg_replace( '#]*>#i', '$0' . str_replace( '$', '\$', $head ), $html, 1 ); } } $foot = implode( '', (array) apply_filters( 'tibospeed_body_inject', $ctx['body'] ) ); if ( $ctx['delayed'] ) { $foot .= tibospeed_delay_loader(); } if ( $ctx['yt'] ) { $foot .= wp_get_inline_script_tag( 'document.addEventListener("click",function(e){var b=e.target.closest(".tibospeed-yt");if(!b)return;var t=b.querySelector("template");if(t){b.innerHTML=t.innerHTML}});document.addEventListener("keydown",function(e){if((e.key==="Enter"||e.key===" ")&&e.target.classList&&e.target.classList.contains("tibospeed-yt")){e.preventDefault();e.target.click()}});', array( 'id' => 'tibospeed-yt-js', 'data-tibospeed-skip' => true, ) ); } if ( $foot ) { $pos = strripos( $html, '' ); if ( false !== $pos ) { $html = substr( $html, 0, $pos ) . $foot . substr( $html, $pos ); } } return $html; } /* -------------------------------------------------------------------------- HTML */ /** * Güvenli HTML küçültme: yorumlar silinir, boşluklar teke iner; pre/textarea/script/style'a dokunulmaz. * * @param string $html Sayfa. * @return string */ function tibospeed_minify_html( $html ) { $keep = array(); $html = preg_replace_callback( '#<(pre|textarea|script|style)\b.*?\1>#is', function ( $m ) use ( &$keep ) { $keep[] = $m[0]; return "\x1aTIBOSPEED" . ( count( $keep ) - 1 ) . "\x1a"; }, $html ); $html = preg_replace( '#).)*-->#s', '', $html ); $html = preg_replace( '/[ \t\r\n\f]+/', ' ', $html ); return preg_replace_callback( "#\x1aTIBOSPEED(\d+)\x1a#", function ( $m ) use ( $keep ) { return $keep[ (int) $m[1] ]; }, $html ); }