<?php
/**
 * Generates distribution metadata.
 *
 * @package Tasty_Roundups
 */

namespace Tasty_Roundups;

/**
 * Generates distribution metadata.
 */
class Distribution_Metadata {

	/**
	 * Renders JSON+LD markup on singular views with roundups.
	 */
	public static function action_wp_head_google_schema() {
		if ( ! is_singular() ) {
			return;
		}

		/**
		 * Bailout without printing the json markup if we decided to use Yoast SEO using the filter.
		 */
		if ( self::use_yoast_schema() ) {
			return;
		}

		$blocks = Utils::parse_roundup_blocks(
			get_queried_object()->post_content
		);
		if ( empty( $blocks ) ) {
			return;
		}

		$schemas = array();
		foreach ( $blocks as $block ) {
			$schemas[] = self::get_enriched_google_schema_for_roundup_block( $block );
		}

		foreach ( $schemas as $schema ) {
			?>
<script type="application/ld+json">
			<?php echo wp_json_encode( $schema, JSON_PRETTY_PRINT ) . PHP_EOL; ?>
</script>
			<?php
		}
	}

	/**
	 * Check if older version of Yoast is active.
	 *
	 * @return bool
	 */
	private static function is_old_yoast() {
		return interface_exists( '\WPSEO_Graph_Piece' );
	}

	/**
	 * Check if newer version of Yoast is active.
	 *
	 * @return bool
	 */
	private static function is_new_yoast() {
		return class_exists( '\Yoast\WP\SEO\Generators\Schema\Abstract_Schema_Piece' );
	}

	/**
	 * Use Yoast Schema or not.
	 *
	 * @return bool
	 */
	private static function use_yoast_schema() {
		return (bool) apply_filters( 'tasty_roundups_use_yoast_schema', self::is_new_yoast() || self::is_old_yoast() );
	}

	/**
	 * Incorporate Tasty Roundups into Yoast SEO when present.
	 *
	 * @param array  $pieces  Existing Yoast SEO graph pieces.
	 * @param object $context Yoast SEO context instance.
	 * @return array
	 */
	public static function filter_wpseo_schema_graph_pieces( $pieces, $context ) {
		if ( ! self::use_yoast_schema() ) {
			return $pieces;
		}

		if ( self::is_new_yoast() ) {
			$pieces[] = new \Tasty_Roundups\Integrations\ItemList_Graph_Piece_Yoast( $context );
		}

		if ( self::is_old_yoast() ) {
			$pieces[] = new \Tasty_Roundups\Integrations\ItemList_Graph_Piece( $context );
			return $pieces;
		}

		return $pieces;
	}

	/**
	 * Gets the enriched form of a Google Schema for a roundups block.
	 *
	 * @param array $block Tasty Roundups block.
	 * @return array
	 */
	public static function get_enriched_google_schema_for_roundup_block( $block ) {
		$schema = self::get_google_schema_for_roundup_block( $block );
		if ( empty( $schema ) ) {
			return $schema;
		}

		return $schema;
	}

	/**
	 * Generates the Google Schema for a roundup block.
	 *
	 * @param array $block Roundup block.
	 * @return array
	 */
	public static function get_google_schema_for_roundup_block( $block ) {
		if ( empty( $block ) || empty( $block['innerBlocks'] ) ) {
			return array();
		}
		$schema = array(
			'@context'        => 'https://schema.org/',
			'@type'           => 'ItemList',
			'itemListElement' => array(),
		);
		$pos    = 1;
		foreach ( $block['innerBlocks'] as $inner_block ) {
			if ( empty( $inner_block['attrs']['href'] ) ) {
				continue;
			}
			$schema['itemListElement'][] = array(
				'@type'    => 'ListItem',
				'position' => $pos,
				'url'      => $inner_block['attrs']['href'],
			);
			++$pos;
		}
		return $schema;
	}
}
