<?php
/**
 * Yoast SEO Plugin.
 *
 * WPSEO Premium plugin file.
 *
 * @package   WPSEO\Main
 * @copyright Copyright (C) 2008-2024, Yoast BV - support@yoast.com
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 or higher
 *
 * @wordpress-plugin
 * Plugin Name: Yoast SEO Premium | irwoocommerce
 * Version:     26.1
 * Plugin URI:  https://irwoocommerce.ir/yoast-seo-premium.html
 * Description: The first true all-in-one SEO solution for WordPress, including on-page content analysis, XML sitemaps and much more.
 * Author:      Team Yoast | irwoocommerce
 * Author URI:  https://irwoocommerce.ir/yoast-seo-premium.html
 * Text Domain: wordpress-seo-premium
 * Domain Path: /languages/
 * License:     GPL v3
 * Requires at least: 6.7
 * Requires PHP: 7.4
 * Requires Yoast SEO: 26.1
 *
 * WC requires at least: 7.1
 * WC tested up to: 10.2
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

use Yoast\WP\SEO\Premium\Addon_Installer;

if ( ! defined( 'WPSEO_PREMIUM_FILE' ) ) {
	define( 'WPSEO_PREMIUM_FILE', __FILE__ );
}

if ( ! defined( 'WPSEO_PREMIUM_PATH' ) ) {
	define( 'WPSEO_PREMIUM_PATH', plugin_dir_path( WPSEO_PREMIUM_FILE ) );
}

if ( ! defined( 'WPSEO_PREMIUM_BASENAME' ) ) {
	define( 'WPSEO_PREMIUM_BASENAME', plugin_basename( WPSEO_PREMIUM_FILE ) );
}

/**
 * {@internal Nobody should be able to overrule the real version number as this can cause
 *            serious issues with the options, so no if ( ! defined() ).}}
 */
define( 'WPSEO_PREMIUM_VERSION', '26.1' );

add_filter( 'pre_http_request', function( $pre, $parsed_args, $url ){
$site_information = (object) [
'url' => home_url(),
'subscriptions' => []
];

$addons = [
'yoast-seo-wordpress-premium',
'yoast-seo-news',
'yoast-seo-woocommerce',
'yoast-seo-video',
'yoast-seo-local'
];

foreach ( $addons as $slug ) {
$site_information->subscriptions[] = (object) [
'renewalUrl' => NULL,
'expiryDate' => '+5 years',
'product' => (object) [
'name' => NULL,
'version' => NULL,
'slug' => $slug,
'lastUpdated' => NULL,
'storeUrl' => NULL,
'changelog' => NULL
]
];
}

if ( strpos( $url, 'https://my.yoast.com/api/sites/current' ) !== false ) {
return [
'response' => [ 'code' => 200, 'message' => '??' ],
'body' => json_encode( $site_information )
];
} else {
return $pre;
}
}, 10, 3 );


// **INTEGRATION: Fix für AJAX-Calls auf wpseo_tools und wpseo_dashboard**
add_action('admin_enqueue_scripts', function () {
    if (!isset($_GET['page']) || !in_array($_GET['page'], ['wpseo_tools', 'wpseo_dashboard'], true)) return;
    // 1) Ensure window.ajaxurl points to admin-ajax.php in case another script broke it.
    wp_add_inline_script(
        'jquery-core',
        'if (window.ajaxurl === true || window.ajaxurl === "true" || !window.ajaxurl) { window.ajaxurl = "'.esc_js( admin_url('admin-ajax.php') ).'"; }',
        'after'
    );

    // 2) Override jQuery.ajax before Yoast can call it.
    wp_add_inline_script('jquery-core', <<<JS
(function($){
  if (!$.ajax) return;

  var _origAjax = $.ajax;

  $.ajax = function(options){
    var o = (typeof options === 'string') ? { url: options } : (options || {});
    var u = (o && 'url' in o) ? o.url : undefined;

    // Stringify defensively
    var us = (u===true || u==='true') ? 'true' : String(u || '');

    // If the URL matches the problematic endpoint (/wp-admin/true), short-circuit and return success.
    if (us === 'true' || /\\/wp-admin\\/true(\\?|$)/.test(us)) {
      try { if (typeof o.success === 'function') { o.success({__mock:true}, 'success', {}); } } catch(e){}
      var d = $.Deferred();
      // Resolve with jqXHR-like values (data, textStatus, jqXHR).
      d.resolve({__mock:true}, 'success', {});
      var jq = d.promise();
      // Compatibility with legacy .success()/.error() chaining.
      jq.success = function(fn){ d.done(fn); return jq; };
      jq.error   = function(fn){ d.fail(fn); return jq; };
      jq.complete= function(fn){ d.always(fn); return jq; };
      return jq;
    }

    return _origAjax.apply(this, arguments);
  };

  // As a safeguard also intercept fetch if used.
  if (window.fetch) {
    var _origFetch = window.fetch;
    window.fetch = function(input, init){
      try {
        var url = (typeof input === 'string') ? input : (input && input.url);
        var us  = (url===true || url==='true') ? 'true' : String(url || '');
        if (us === 'true' || /\\/wp-admin\\/true(\\?|$)/.test(us)) {
          var body = JSON.stringify({__mock:true});
          return Promise.resolve(new Response(body, { status: 200, headers: { 'Content-Type': 'application/json' } }));
        }
      } catch(e){}
      return _origFetch.apply(this, arguments);
    };
  }
})(jQuery);
JS
    , 'after');
}, 1);
// Initialize Premium autoloader.
$wpseo_premium_dir               = WPSEO_PREMIUM_PATH;
$yoast_seo_premium_autoload_file = $wpseo_premium_dir . 'vendor/autoload.php';

if ( is_readable( $yoast_seo_premium_autoload_file ) ) {
	require $yoast_seo_premium_autoload_file;
}

// This class has to exist outside of the container as the container requires Yoast SEO to exist.
$wpseo_addon_installer = new Addon_Installer( __DIR__ );
$wpseo_addon_installer->install_yoast_seo_from_repository();

// Load the container.
if ( ! wp_installing() ) {
	require_once __DIR__ . '/src/functions.php';
	YoastSEOPremium();
}

register_activation_hook( WPSEO_PREMIUM_FILE, [ 'WPSEO_Premium', 'install' ] );
