<?php
/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://www.francescopepe.com
 * @since      1.0.0
 *
 * @package    Popper
 * @subpackage Popper/public
 */

namespace Popper;

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Popper
 * @subpackage Popper/public
 * @author     Francesco Pepe <info@francescopepe.com>
 */
class Frontend {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of the plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {
		$this->plugin_name = $plugin_name;
		$this->version = $version;
	}

	/**
	 * Output our popups.
	 *
	 * @since 1.0.0
	 */
	public function get_popups() {
		// phpcs:ignore
		echo $this->popups;
	}

	/**
	 * Get our popups based on conditions.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function popper_matcher() {
		if ( is_admin() || wp_is_json_request() ) {
			return false;
		}
		global $wpdb;
		global $wp_embed;

		//remove_filter( 'the_content', 'wpautop' );

		$popups = '';

		$rules = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT * FROM {$wpdb->prefix}posts 
				LEFT JOIN {$wpdb->prefix}postmeta 
				ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}postmeta.post_id 
				WHERE {$wpdb->prefix}posts.post_type = %s 
				AND {$wpdb->prefix}postmeta.meta_key = %s 
				AND {$wpdb->prefix}posts.post_status = %s 
				ORDER BY {$wpdb->prefix}posts.ID;",
				'popper',
				'popper_rules',
				'publish',
			)
		);

		$matched = false;

		// loop through all rules for all boxes.
		foreach ( $rules as $rule ) {
			$popper_content = $rule->post_content;

			// Get the rules.
			$rule = maybe_unserialize( $rule->meta_value );

			if ( empty( $rule['date'] ) ) {
				$rule['date'] = array();
			}

			$matched = Conditions::show_data( $rule['location'], $rule['exclude'], $rule['user'], $rule['date'], $rule['running'] );

			if ( $matched ) {

				$popups .= $wp_embed->autoembed( $popper_content );

				$matched = false;
			}
		}
		// phpcs:ignore
		$this->popups = do_blocks( $popups );
		//add_filter( 'the_content', 'wpautop' );
	}

	/**
	 * Filter to add data-popper
	 *
	 * @param string $block_content The block content.
	 * @param array  $block The block.
	 */
	public function add_data_attribute( $block_content, $block ) {
		// Make sure we have the blockName.
		if ( empty( $block['blockName'] ) ) {
			return $block_content;
		}

		if (
			'core/button' === $block['blockName']
			&& ! empty( $block['attrs']['popper'] )
		) {
			$processor = new \WP_HTML_Tag_Processor( $block_content );
			$processor->next_tag( 'a' );
			$processor->set_attribute( 'data-popper', $block['attrs']['popper'] );
			return $processor->get_updated_html();
		}
		if (
			'core/image' === $block['blockName']
			&& ! empty( $block['attrs']['popper'] )
		) {
			$processor = new \WP_HTML_Tag_Processor( $block_content );
			$processor->next_tag( 'img' );
			$processor->set_attribute( 'data-popper', $block['attrs']['popper'] );
			return $processor->get_updated_html();
		}
		// Return the block content.
		return $block_content;
	}
}
