<?php
/*
Plugin Name: ProbonoSEO Basic
Plugin URI: https://seo.prbn.org
Description: AIO完全対応、日本語サイト向けに最適化された Made in Japan の SEO プラグイン
Version: 1.6.7
Author: ProbonoDesign
Author URI: https://prbn.org
License: GPLv2 or later
Text Domain: probonoseo-basic
*/

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

define('PROBONOSEO_PATH', plugin_dir_path(__FILE__));
define('PROBONOSEO_URL', plugin_dir_url(__FILE__));
define('PROBONOSEO_VERSION', '1.6.7');

function probonoseo_admin_assets($hook) {
	if ($hook !== 'toplevel_page_probonoseo') {
		return;
	}
	wp_enqueue_media();
	wp_enqueue_style(
		'probonoseo-admin-style',
		PROBONOSEO_URL . 'admin/admin-style.css',
		array(),
		PROBONOSEO_VERSION
	);
	wp_enqueue_style(
		'probonoseo-switch',
		PROBONOSEO_URL . 'admin/switch.css',
		array('probonoseo-admin-style'),
		PROBONOSEO_VERSION
	);
	wp_enqueue_style(
		'probonoseo-tabs',
		PROBONOSEO_URL . 'admin/tabs.css',
		array('probonoseo-admin-style'),
		PROBONOSEO_VERSION
	);
	wp_enqueue_script(
		'probonoseo-admin-switch',
		PROBONOSEO_URL . 'admin/admin-switch.js',
		array(),
		PROBONOSEO_VERSION,
		true
	);
}
add_action('admin_enqueue_scripts', 'probonoseo_admin_assets');

function probonoseo_register_menu() {
	add_menu_page(
		'ProbonoSEO',
		'ProbonoSEO',
		'manage_options',
		'probonoseo',
		'probonoseo_render_admin_page',
		'dashicons-chart-area',
		56
	);
}
add_action('admin_menu', 'probonoseo_register_menu');

function probonoseo_render_admin_page() {
	require_once PROBONOSEO_PATH . 'admin/admin-page.php';
}

require_once PROBONOSEO_PATH . 'admin/metabox-seo.php';

require_once PROBONOSEO_PATH . 'seo/title.php';
require_once PROBONOSEO_PATH . 'seo/meta.php';
require_once PROBONOSEO_PATH . 'seo/meta-defaults.php';
require_once PROBONOSEO_PATH . 'seo/canonical.php';

require_once PROBONOSEO_PATH . 'seo/ogp.php';
require_once PROBONOSEO_PATH . 'seo/ogp-meta.php';
require_once PROBONOSEO_PATH . 'seo/open-graph.php';
require_once PROBONOSEO_PATH . 'seo/twitter-card.php';

require_once PROBONOSEO_PATH . 'seo/schema.php';
require_once PROBONOSEO_PATH . 'seo/breadcrumb.php';
require_once PROBONOSEO_PATH . 'seo/breadcrumb-core.php';
require_once PROBONOSEO_PATH . 'seo/breadcrumb-data.php';
require_once PROBONOSEO_PATH . 'seo/breadcrumb-schema-cleaner.php';

require_once PROBONOSEO_PATH . 'seo/internal-links.php';
require_once PROBONOSEO_PATH . 'seo/speed.php';
require_once PROBONOSEO_PATH . 'seo/article-seo.php';
require_once PROBONOSEO_PATH . 'seo/diagnosis.php';
require_once PROBONOSEO_PATH . 'seo/meta-cleanup.php';
require_once PROBONOSEO_PATH . 'seo/gsc-verify.php';

require_once PROBONOSEO_PATH . 'seo/seo-core.php';
require_once PROBONOSEO_PATH . 'seo/seo-utils.php';

require_once PROBONOSEO_PATH . 'aio/faq-schema.php';
require_once PROBONOSEO_PATH . 'aio/article-schema.php';
require_once PROBONOSEO_PATH . 'aio/organization-schema.php';
require_once PROBONOSEO_PATH . 'aio/robots-aio.php';
require_once PROBONOSEO_PATH . 'aio/llms-txt.php';

function probonoseo_migrate_aeo_to_aio() {
	if (get_option('probonoseo_aeo_to_aio_migrated') === '1') {
		return;
	}

	$key_map = array(
		'probonoseo_aeo_faq_schema'          => 'probonoseo_aio_faq_schema',
		'probonoseo_aeo_article_schema'      => 'probonoseo_aio_article_schema',
		'probonoseo_aeo_organization_schema' => 'probonoseo_aio_organization_schema',
		'probonoseo_aeo_robots'              => 'probonoseo_aio_robots',
	);

	foreach ($key_map as $old_key => $new_key) {
		$old_value = get_option($old_key);
		if ($old_value !== false) {
			update_option($new_key, $old_value);
			delete_option($old_key);
		}
	}

	update_option('probonoseo_aeo_to_aio_migrated', '1');
}
add_action('plugins_loaded', 'probonoseo_migrate_aeo_to_aio', 1);

add_action('wp_ajax_probonoseo_diagnosis', 'probonoseo_handle_diagnosis_ajax');

function probonoseo_handle_diagnosis_ajax() {
	if (!isset($_POST['probonoseo_diagnosis_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['probonoseo_diagnosis_nonce'])), 'probonoseo_diagnosis_ajax')) {
		wp_send_json_error(array('message' => '不正なリクエストです'));
		return;
	}

	if (!current_user_can('manage_options')) {
		wp_send_json_error(array('message' => '権限がありません'));
		return;
	}

	$keys = array(
		'probonoseo_diagnosis_title_duplicate',
		'probonoseo_diagnosis_meta_duplicate',
		'probonoseo_diagnosis_speed'
	);

	foreach ($keys as $k) {
		if (isset($_POST[$k])) {
			update_option($k, sanitize_text_field(wp_unslash($_POST[$k])) === '1' ? '1' : '0');
		}
	}

	ProbonoSEO_Diagnosis::run_diagnosis();

	ob_start();
	ProbonoSEO_Diagnosis::display_results();
	$results_html = ob_get_clean();

	wp_send_json_success(array('html' => $results_html));
}

add_action('wp_ajax_probonoseo_export_settings', 'probonoseo_handle_export_settings_ajax');

function probonoseo_handle_export_settings_ajax() {
	if (!current_user_can('manage_options')) {
		wp_send_json_error(array('message' => '権限がありません。'));
	}

	if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'probonoseo_export_settings')) {
		wp_send_json_error(array('message' => '不正なリクエストです。'));
	}

	$free_keys = array(
		'probonoseo_basic_title',
		'probonoseo_title_separator',
		'probonoseo_title_sitename',
		'probonoseo_title_h1_check',
		'probonoseo_title_category',
		'probonoseo_title_duplicate',
		'probonoseo_title_symbols',
		'probonoseo_basic_metadesc',
		'probonoseo_meta_extraction',
		'probonoseo_meta_keywords',
		'probonoseo_meta_summary',
		'probonoseo_meta_forbidden',
		'probonoseo_meta_length',
		'probonoseo_meta_duplicate',
		'probonoseo_basic_canonical',
		'probonoseo_canonical_auto',
		'probonoseo_canonical_slash',
		'probonoseo_canonical_merge',
		'probonoseo_canonical_params',
		'probonoseo_basic_ogp',
		'probonoseo_ogp_title',
		'probonoseo_ogp_desc',
		'probonoseo_ogp_image_auto',
		'probonoseo_ogp_image_fixed',
		'probonoseo_ogp_facebook',
		'probonoseo_ogp_line',
		'probonoseo_ogp_thumbnail',
		'probonoseo_ogp_size_detect',
		'probonoseo_ogp_alt',
		'probonoseo_ogp_japanese_url',
		'probonoseo_basic_twitter',
		'probonoseo_basic_schema',
		'probonoseo_basic_breadcrumb',
		'probonoseo_internal_prev_next',
		'probonoseo_internal_category',
		'probonoseo_internal_child_pages',
		'probonoseo_internal_related',
		'probonoseo_internal_tag_logic',
		'probonoseo_internal_nofollow',
		'probonoseo_internal_category_format',
		'probonoseo_speed_lazy_images',
		'probonoseo_speed_lazy_iframes',
		'probonoseo_speed_minify_css',
		'probonoseo_speed_minify_js',
		'probonoseo_speed_optimize_wp_scripts',
		'probonoseo_article_heading_check',
		'probonoseo_article_alt_check',
		'probonoseo_article_image_count',
		'probonoseo_article_word_count',
		'probonoseo_article_category_match',
		'probonoseo_article_tag_duplicate',
		'probonoseo_diagnosis_title_duplicate',
		'probonoseo_diagnosis_meta_duplicate',
		'probonoseo_diagnosis_speed',
		'probonoseo_meta_cleanup',
		'probonoseo_gsc_verify',
		'probonoseo_aio_faq_schema',
		'probonoseo_aio_article_schema',
		'probonoseo_aio_organization_schema',
		'probonoseo_aio_robots',
		'probonoseo_aio_llms_txt',
		'probonoseo_fixed_ogp_image_url',
	);

	$settings = array();

	foreach ($free_keys as $key) {
		$settings[$key] = get_option($key, '');
	}

	$settings['_export_date'] = current_time('mysql');
	$settings['_plugin_version'] = PROBONOSEO_VERSION;

	$filename = 'probonoseo-settings-' . gmdate('Y-m-d-His') . '.json';

	wp_send_json_success(array(
		'filename' => $filename,
		'settings' => $settings,
	));
}

register_activation_hook(__FILE__, 'probonoseo_activation');

function probonoseo_activation() {
}