<?php
if (!defined('ABSPATH')) {
	exit;
}

class ProbonoSEO_Twitter {
	private static $instance = null;

	public static function get_instance() {
		if (self::$instance === null) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		$this->init_hooks();
	}

	private function init_hooks() {
		if (get_option('probonoseo_basic_twitter', '0') === '1') {
			add_action('wp_head', array($this, 'output_twitter_card'), 5);
		}
	}

	public function output_twitter_card() {
		if ($this->should_skip_twitter()) {
			return;
		}

		$probonoseo_ogp = ProbonoSEO_OGP::get_instance();
		$probonoseo_data = $probonoseo_ogp->get_data();

		$probonoseo_title = isset($probonoseo_data['title']) ? $probonoseo_data['title'] : '';
		$probonoseo_desc = isset($probonoseo_data['desc']) ? $probonoseo_data['desc'] : '';
		$probonoseo_image = isset($probonoseo_data['image']) ? $probonoseo_data['image'] : '';

		$probonoseo_card_type = !empty($probonoseo_image) ? 'summary_large_image' : 'summary';

		echo '<meta name="twitter:card" content="' . esc_attr($probonoseo_card_type) . '">' . "\n";

		if (!empty($probonoseo_title)) {
			echo '<meta name="twitter:title" content="' . esc_attr($probonoseo_title) . '">' . "\n";
		}

		if (!empty($probonoseo_desc)) {
			echo '<meta name="twitter:description" content="' . esc_attr($probonoseo_desc) . '">' . "\n";
		}

		if (!empty($probonoseo_image)) {
			echo '<meta name="twitter:image" content="' . esc_url($probonoseo_image) . '">' . "\n";
		}
	}

	private function should_skip_twitter() {
		if (function_exists('yoast_get_options')) {
			return true;
		}

		if (defined('RANK_MATH_VERSION')) {
			return true;
		}

		if (defined('AIOSEO_VERSION')) {
			return true;
		}

		return false;
	}
}

function probonoseo_init_twitter() {
	ProbonoSEO_Twitter::get_instance();
}
add_action('init', 'probonoseo_init_twitter');
