<?php
/**
 * Importer - Competitor Meta Reader
 *
 * @package SEOvaultSEO
 */

namespace SEOvaultSEO\Importer;

use SEOvaultSEO\Compatibility\Detector;

/**
 * Reads SEO meta from competitor plugins
 */
class Reader {

	/**
	 * Get SEO meta from Yoast SEO
	 *
	 * @param int $post_id Post ID
	 * @return array
	 */
	public static function read_yoast( $post_id ) {
		if ( ! Detector::is_competitor_plugin_active( Detector::PROVIDER_YOAST, defined( 'WPSEO_VERSION' ) ) ) {
			return array();
		}

		return array(
			'seo_title'       => get_post_meta( $post_id, '_yoast_wpseo_title', true ),
			'seo_description' => get_post_meta( $post_id, '_yoast_wpseo_metadesc', true ),
			'focus_keyword'   => get_post_meta( $post_id, '_yoast_wpseo_focuskw', true ),
			'canonical_url'   => get_post_meta( $post_id, '_yoast_wpseo_canonical', true ),
			'noindex'         => '1' === get_post_meta( $post_id, '_yoast_wpseo_meta-robots-noindex', true ),
			'nofollow'        => '1' === get_post_meta( $post_id, '_yoast_wpseo_meta-robots-nofollow', true ),
		);
	}

	/**
	 * Get SEO meta from Rank Math
	 *
	 * @param int $post_id Post ID
	 * @return array
	 */
	public static function read_rankmath( $post_id ) {
		if ( ! Detector::is_competitor_plugin_active( Detector::PROVIDER_RANKMATH, defined( 'RANK_MATH_VERSION' ) ) ) {
			return array();
		}

		$meta = get_post_meta( $post_id, 'rank_math_seo_score', true );
		if ( empty( $meta ) ) {
			return array();
		}

		return array(
			'seo_title'       => get_post_meta( $post_id, 'rank_math_title', true ),
			'seo_description' => get_post_meta( $post_id, 'rank_math_description', true ),
			'focus_keyword'   => get_post_meta( $post_id, 'rank_math_focus_keyword', true ),
			'canonical_url'   => get_post_meta( $post_id, 'rank_math_canonical_url', true ),
			'noindex'         => get_post_meta( $post_id, 'rank_math_robots', true ) === 'noindex',
			'nofollow'        => get_post_meta( $post_id, 'rank_math_robots', true ) === 'nofollow',
		);
	}

	/**
	 * Get SEO meta from AIOSEO
	 *
	 * @param int $post_id Post ID
	 * @return array
	 */
	public static function read_aioseo( $post_id ) {
		$real_active = class_exists( 'All_in_One_SEO_Pack' ) || defined( 'AIOSEOP_VERSION' ) || defined( 'AIOSEO_VERSION' );
		if ( ! Detector::is_competitor_plugin_active( Detector::PROVIDER_AIOSEO, $real_active ) ) {
			return array();
		}

		$meta = get_post_meta( $post_id, '_aioseop_title', true );
		if ( empty( $meta ) ) {
			// Try new AIOSEO format
			$meta = get_post_meta( $post_id, '_aioseo_title', true );
		}

		if ( empty( $meta ) ) {
			return array();
		}

		return array(
			'seo_title'       => get_post_meta( $post_id, '_aioseop_title', true ) ?: get_post_meta( $post_id, '_aioseo_title', true ),
			'seo_description' => get_post_meta( $post_id, '_aioseop_description', true ) ?: get_post_meta( $post_id, '_aioseo_description', true ),
			'focus_keyword'   => get_post_meta( $post_id, '_aioseop_keywords', true ) ?: get_post_meta( $post_id, '_aioseo_keywords', true ),
			'canonical_url'   => get_post_meta( $post_id, '_aioseop_custom_link', true ) ?: get_post_meta( $post_id, '_aioseo_canonical_url', true ),
			'noindex'         => 'on' === get_post_meta( $post_id, '_aioseop_noindex', true ) || 'on' === get_post_meta( $post_id, '_aioseo_noindex', true ),
			'nofollow'        => 'on' === get_post_meta( $post_id, '_aioseop_nofollow', true ) || 'on' === get_post_meta( $post_id, '_aioseo_nofollow', true ),
		);
	}

	/**
	 * Get SEO meta from Slim SEO
	 *
	 * @param int $post_id Post ID
	 * @return array
	 */
	public static function read_slimseo( $post_id ) {
		$real_active = defined( 'SLIM_SEO_VER' ) || class_exists( 'SlimSEO\\Container' );
		if ( ! Detector::is_competitor_plugin_active( Detector::PROVIDER_SLIMSEO, $real_active ) ) {
			return array();
		}

		return array(
			'seo_title'       => get_post_meta( $post_id, 'slim_seo_title', true ),
			'seo_description' => get_post_meta( $post_id, 'slim_seo_description', true ),
			'focus_keyword'   => '',
			'canonical_url'   => get_post_meta( $post_id, 'slim_seo_canonical_url', true ),
			'noindex'         => '1' === get_post_meta( $post_id, 'slim_seo_noindex', true ),
			'nofollow'        => false,
		);
	}

	/**
	 * Get SEO meta from The SEO Framework
	 *
	 * @param int $post_id Post ID
	 * @return array
	 */
	public static function read_theseoframework( $post_id ) {
		$real_active = defined( 'THE_SEO_FRAMEWORK_VERSION' ) || defined( 'TSF_VERSION' );
		if ( ! Detector::is_competitor_plugin_active( Detector::PROVIDER_THESEOFRAMEWORK, $real_active ) ) {
			return array();
		}

		return array(
			'seo_title'       => get_post_meta( $post_id, 'seo_title', true ),
			'seo_description' => get_post_meta( $post_id, 'seo_description', true ),
			'focus_keyword'   => get_post_meta( $post_id, '_genesis_title', true ),
			'canonical_url'   => get_post_meta( $post_id, 'canonical_url', true ),
			'noindex'         => '1' === get_post_meta( $post_id, 'noindex', true ),
			'nofollow'        => '1' === get_post_meta( $post_id, 'nofollow', true ),
		);
	}

	/**
	 * Get SEO meta from active competitor plugin
	 *
	 * @param int $post_id Post ID
	 * @return array
	 */
	public static function read_from_active_plugin( $post_id ) {
		$provider = \SEOvaultSEO\Compatibility\Detector::detect_active_provider();

		switch ( $provider ) {
			case \SEOvaultSEO\Compatibility\Detector::PROVIDER_YOAST:
				return self::read_yoast( $post_id );
			case \SEOvaultSEO\Compatibility\Detector::PROVIDER_RANKMATH:
				return self::read_rankmath( $post_id );
			case \SEOvaultSEO\Compatibility\Detector::PROVIDER_AIOSEO:
				return self::read_aioseo( $post_id );
			case \SEOvaultSEO\Compatibility\Detector::PROVIDER_SLIMSEO:
				return self::read_slimseo( $post_id );
			case \SEOvaultSEO\Compatibility\Detector::PROVIDER_THESEOFRAMEWORK:
				return self::read_theseoframework( $post_id );
			default:
				return array();
		}
	}

	/**
	 * Count posts with competitor meta
	 *
	 * @param string $provider Provider name
	 * @return int
	 */
	public static function count_posts_with_meta( $provider = null ) {
		if ( null === $provider ) {
			$provider = \SEOvaultSEO\Compatibility\Detector::detect_active_provider();
		}

		if ( \SEOvaultSEO\Compatibility\Detector::PROVIDER_NONE === $provider ) {
			return 0;
		}

		$meta_key = self::get_meta_key_for_provider( $provider );
		if ( empty( $meta_key ) ) {
			return 0;
		}

		$args = array(
			'post_type'      => array( 'post', 'page' ),
			'post_status'    => 'any',
			'posts_per_page' => -1,
            // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
			'meta_query'     => array(
				array(
					'key'     => $meta_key,
					'compare' => 'EXISTS',
				),
			),
		);

		$posts = get_posts( $args );
		return count( $posts );
	}

	/**
	 * Get meta key for provider
	 *
	 * @param string $provider Provider name
	 * @return string
	 */
	public static function get_meta_key_for_provider( $provider ) {
		switch ( $provider ) {
			case \SEOvaultSEO\Compatibility\Detector::PROVIDER_YOAST:
				return '_yoast_wpseo_title';
			case \SEOvaultSEO\Compatibility\Detector::PROVIDER_RANKMATH:
				return 'rank_math_seo_score';
			case \SEOvaultSEO\Compatibility\Detector::PROVIDER_AIOSEO:
				return '_aioseop_title';
			case \SEOvaultSEO\Compatibility\Detector::PROVIDER_SLIMSEO:
				return 'slim_seo_title';
			case \SEOvaultSEO\Compatibility\Detector::PROVIDER_THESEOFRAMEWORK:
				return 'seo_title';
			default:
				return '';
		}
	}
}
