<?php

/**
 * Plugin Name: TSNV SEO
 * Description: For now, the plugin only adds structured data (microdata).
 * Version: 1.4.0
 */

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'acf_pro' ) ) {
  add_action( 'admin_notices', function() {
    echo '<div class="error"><p><strong>TSNV SEO</strong> requires the <a href="https://www.advancedcustomfields.com/pro/" target="_blank">Advanced Custom Fields Pro</a> plugin to be installed and activated.</p></div>';
  } );
  return;
}

define('TSNV_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('TSNV_PREFIX', 'tsnv_');

require_once TSNV_PLUGIN_PATH.'includes/acf-fields.php';
require_once TSNV_PLUGIN_PATH.'includes/abstract-schema.php';
require_once TSNV_PLUGIN_PATH.'includes/class-game-schema.php';
require_once TSNV_PLUGIN_PATH.'includes/class-faq-schema.php';
require_once TSNV_PLUGIN_PATH.'includes/class-breadcrumb-schema.php';
require_once TSNV_PLUGIN_PATH.'includes/class-review-schema.php';
require_once TSNV_PLUGIN_PATH.'includes/functions-schema.php';
require_once TSNV_PLUGIN_PATH.'includes/pll-translate-all-media.php';
require_once TSNV_PLUGIN_PATH.'includes/pll-acf-auto-translate.php';
require_once TSNV_PLUGIN_PATH.'includes/quick-alt-title-patch.php';
require_once TSNV_PLUGIN_PATH.'plugin-update-checker/plugin-update-checker.php';

use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
$updateChecker = PucFactory::buildUpdateChecker('https://github.com/evgeniyandrusenko/tsnv-seo/', __FILE__, 'tsnv-seo');
$updateChecker->setBranch('main');
$updateChecker->setAuthentication('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');

function tsnv_seo_run_update($updateChecker, $die_on_result = false) {
  $plugin_rel_path = str_replace(trailingslashit(WP_PLUGIN_DIR), '', __FILE__);

  $updateChecker->checkForUpdates();

  $updates = get_site_transient('update_plugins');
  if (!isset($updates->response[$plugin_rel_path])) {
    if ($die_on_result) {
      wp_die('No updates available.');
    }
    return false;
  }

  require_once ABSPATH.'wp-admin/includes/file.php';
  require_once ABSPATH.'wp-admin/includes/plugin.php';
  require_once ABSPATH.'wp-admin/includes/class-wp-upgrader.php';
  require_once ABSPATH.'wp-admin/includes/misc.php';

  $upgrader = new Plugin_Upgrader();
  $result = $upgrader->upgrade($plugin_rel_path);

  if (is_wp_error($result)) {
    if ($die_on_result) {
      wp_die('Update error: ' . $result->get_error_message());
    } else {
      error_log('TSNV SEO update error: ' . $result->get_error_message());
      return false;
    }
  } elseif ($result === false) {
    if ($die_on_result) {
      wp_die('Update was not performed — possibly no new version available.');
    } else {
      error_log('TSNV SEO update not performed — possibly no new version available.');
      return false;
    }
  } else {
    $activate_result = activate_plugin($plugin_rel_path);
    if (is_wp_error($activate_result)) {
      if ($die_on_result) {
        wp_die('Plugin updated but failed to activate: ' . $activate_result->get_error_message());
      } else {
        error_log('TSNV SEO plugin updated but failed to activate: ' . $activate_result->get_error_message());
        return false;
      }
    } else {
      if ($die_on_result) {
        wp_die('Plugin successfully updated and activated');
      } else {
        error_log('TSNV SEO plugin successfully updated and activated');
        return true;
      }
    }
  }
}

add_action('init', function () use ($updateChecker) {
  if (isset($_GET['tsnv_update'])) {
    tsnv_seo_run_update($updateChecker, true);
  }
});

add_action('wp', function () {
  if (!wp_next_scheduled('tsnv_seo_auto_update_event')) {
    wp_schedule_event(time(), 'hourly', 'tsnv_seo_auto_update_event');
  }
});

add_action('tsnv_seo_auto_update_event', function () use ($updateChecker) {
  tsnv_seo_run_update($updateChecker, false);
});

add_action('wp_head', function () {
  if (is_front_page()) {
    TSNV_SEO\render_game_schema();
  } else {
    TSNV_SEO\render_breadcrumb_schema();
  }

  TSNV_SEO\render_faq_schema();

  TSNV_SEO\render_review_schema();
});

// Ensure trailing slash at the end of home_url path
add_action('init', function () {
  add_filter('home_url', function($url, $path, $orig_scheme, $blog_id) {
    if ( is_admin() || defined('REST_REQUEST') ) {
      return $url;
    }

    if ($path === '' || $path === false) {
      return user_trailingslashit($url);
    }

    return $url;
  }, 100, 4);
});

