HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/gift.naniguide.com/wp-content/MU-Plugins/yoast-premium-helper.php
<?php
/**
 * Plugin Name: Red.Dot Yoast Premium Helper
 * Description: Stabilizes Yoast Premium by fixing ajaxurl and bypassing morphology download calls that cause 403 errors.
 * Author: Red.Dot
 * Version: 1.0.0
 * License: GPLv2 or later
 */

if (!defined('ABSPATH')) { exit; }

/**
 * Only load in admin where Yoast runs (Tools page, post editor, Elementor editor).
 */
add_action('admin_enqueue_scripts', function($hook){
	// Always ensure ajaxurl in admin.
	wp_enqueue_script('jquery');

	$should_load = false;

	// Yoast Tools.
	if (isset($_GET['page']) && $_GET['page'] === 'wpseo_tools') {
		$should_load = true;
	}

	// Post editor (classic or block).
	if (in_array($hook, array('post.php', 'post-new.php', 'edit.php'), true)) {
		$should_load = true;
	}

	// Elementor editor (common patterns).
	if (isset($_GET['action']) && $_GET['action'] === 'elementor') {
		$should_load = true;
	}

	if (!$should_load) {
		return;
	}

	$ajax_url = admin_url('admin-ajax.php');
	$rest_root = esc_url_raw( get_rest_url() );

	$inline = <<<JS
(function($){
	"use strict";

	// 1) Ensure window.ajaxurl is a valid string.
	try {
		if (!window.ajaxurl || typeof window.ajaxurl !== "string" || !/\\/wp-admin\\//.test(window.ajaxurl)) {
			window.ajaxurl = "{$ajax_url}";
			console.info("[SAEP Yoast Helper] ajaxurl set to: " + window.ajaxurl);
		}
	} catch(e) {}

	// 2) Patterns to bypass / normalize (morphology + odd 'true' placeholders).
	var RE_MORPH = /my\\.yoast\\.com\\/api\\/downloads\\/file\\/morphology-/i;
	var RE_TRUE_URL = /\\/wp-admin\\/true(\\?|$)/i;

	function isMorphology(url){ return RE_MORPH.test(url || ""); }
	function isTrueUrl(url){ return RE_TRUE_URL.test(url || ""); }

	// 3) Patch jQuery.ajax.
	if ($ && typeof $.ajax === "function") {
		var _ajax = $.ajax;
		$.ajax = function(opts){
			try {
				var s = (typeof opts === "string") ? {url: opts} : (opts || {});
				var url = String(s.url || "");
				if (isMorphology(url)) {
					// Pretend success with empty morphology data.
					var ok = (typeof s.success === "function") ? s.success : null;
					var complete = (typeof s.complete === "function") ? s.complete : null;
					if (ok) try { ok({}); } catch(e){}
					if (complete) try { complete({status: 200}, "success"); } catch(e){}
					console.info("[SAEP Yoast Helper] Bypassed morphology via $.ajax:", url);
					return $.Deferred().resolve({}).promise();
				}
				if (isTrueUrl(url)) {
					var ok2 = (typeof s.success === "function") ? s.success : null;
					var complete2 = (typeof s.complete === "function") ? s.complete : null;
					if (ok2) try { ok2({__mock:true}); } catch(e){}
					if (complete2) try { complete2({status: 200}, "success"); } catch(e){}
					console.info("[SAEP Yoast Helper] Normalized 'true' AJAX URL via $.ajax");
					return $.Deferred().resolve({__mock:true}).promise();
				}
			} catch(e){}
			return _ajax.apply(this, arguments);
		};
	}

	// 4) Patch window.fetch.
	if (typeof window.fetch === "function") {
		var _fetch = window.fetch;
		window.fetch = function(input, init){
			try {
				var url = (typeof input === "string") ? input : (input && input.url) || "";
				if (isMorphology(url)) {
					console.info("[SAEP Yoast Helper] Bypassed morphology via fetch:", url);
					return Promise.resolve(new Response(JSON.stringify({}), {
						status: 200,
						headers: { "Content-Type": "application/json" }
					}));
				}
				if (isTrueUrl(url)) {
					console.info("[SAEP Yoast Helper] Normalized 'true' fetch URL");
					return Promise.resolve(new Response(JSON.stringify({__mock:true}), {
						status: 200,
						headers: { "Content-Type": "application/json" }
					}));
				}
			} catch(e){}
			return _fetch.apply(this, arguments);
		};
	}

	// 5) Add no-cache headers to Yoast REST indexing calls (best-effort, client side).
	try {
		var _origOpen = XMLHttpRequest.prototype.open;
		XMLHttpRequest.prototype.open = function(method, url){
			this.addEventListener('readystatechange', function(){
				try {
					if (this.readyState === 1) {
						if ((/\\/wp-json\\/yoast\\/v1\\/indexing\\//i).test(url || "")) {
							this.setRequestHeader('Cache-Control', 'no-cache');
							this.setRequestHeader('Pragma', 'no-cache');
						}
					}
				} catch(e){}
			});
			return _origOpen.apply(this, arguments);
		};
	} catch(e){}

	// 6) Helpful ping
	console.info("[SAEP Yoast Helper] Loaded.");
})(window.jQuery);
JS;

	wp_add_inline_script('jquery-core', $inline, 'after');
}, 20);