<?php
/**
 * Editor Plugins Integration Base
 *
 * @link https://personalizewp.com/
 * @since 3.2.0
 *
 * @package PersonalizeWP
 * @subpackage PersonalizeWP/Integrations
 */

namespace PersonalizeWP\Integrations;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

use PersonalizeWP\Traits\SingletonTrait;

/**
 * Integrations that extend the PWP block editor 'Integrations', must extend this class.
 *
 * @since 3.2.0
 */
abstract class Editor_Plugins_Integration_Base {

	use SingletonTrait;

	/**
	 * Slug of the integrated plugin
	 *
	 * @since 3.2.0
	 *
	 * @var string
	 */
	protected $plugin_slug;

	/**
	 * Display title of the integrated plugin
	 *
	 * @since 3.2.0
	 *
	 * @var string
	 */
	protected $plugin_title;

	/**
	 * Class constructor - setting value and title of the addon plugin and add it to the settings integrations plugins.
	 *
	 * @since 3.2.0
	 *
	 * @param string $slug  Integration addon plugin slug
	 * @param string $title Integration addon plugin title
	 *
	 * @return void
	 */
	public function __construct( $slug, $title ) {
		$this->plugin_slug  = $slug;
		$this->plugin_title = $title;

		\add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_scripts' ] );

		\add_filter( 'personalizewp_rest_settings_schema', [ $this, 'add_integrations_rest_settings_schema' ], 30 );
		\add_filter( 'personalizewp_rest_settings', [ $this, 'add_integrations_rest_settings' ], 40 );

		\add_filter( 'personalizewp_check_render_block_logic', [ $this, 'check_render_block_logic' ], 10, 4 );
		\add_filter( 'personalizewp_block_render_placeholder_logic', [ $this, 'check_render_placeholder_logic' ], 10, 3 );
	}

	/**
	 * Enqueues scripts for the block editor and PersonalizeWP panels. Every integration is a separate JS asset.
	 *
	 * @since 3.1.0
	 */
	public function enqueue_block_editor_scripts() {

		/**
		 * Personalisation is not currently supported within legacy widgets.
		 *
		 * @since 3.1.0
		 */
		if ( wp_script_is( 'wp-edit-widgets' ) ) {
			return;
		}

		$pwp_plugin    = \personalizewp();
		$handle_prefix = $pwp_plugin->get_personalizewp();
		$properties    = require $pwp_plugin->plugin_path( 'admin/js/integrations/' . $this->plugin_slug . '.asset.php' );
		$js_version    = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? $properties['version'] : $pwp_plugin->get_version();
		wp_enqueue_script(
			"{$handle_prefix}/{$this->plugin_slug}",
			$pwp_plugin->plugin_url( "admin/js/integrations/{$this->plugin_slug}.js" ),
			array_merge( [ "{$handle_prefix}-block-editor" ], $properties['dependencies'] ),
			$js_version,
			false
		);
	}

	/**
	 * Check whether the current plugin is set in the integrations attribute.
	 *
	 * @since 3.2.0
	 *
	 * @param array $integrations The integrations attribute
	 *
	 * @return bool true if the current plugin is set, otherwise false.
	 */
	protected function uses_this_plugin( $integrations ) {
		return ! empty( $integrations['plugin'] ) && $this->plugin_slug === $integrations['plugin'];
	}

	/**
	 * Add Schema Integration Properties.
	 *
	 * @since 3.2.0
	 *
	 * @param array $properties Schema Integration Properties
	 * @return array
	 */
	abstract public function add_integrations_rest_settings_schema( $properties );

	/**
	 * Add Integrations REST Settings.
	 *
	 * @since 3.2.0
	 *
	 * @param array $settings Settings
	 * @return array
	 */
	abstract public function add_integrations_rest_settings( $settings );

	/**
	 * Checks block for specific attributes, triggering further logic checks.
	 *
	 * @since 3.2.0
	 *
	 * @param bool  $display_block The current status of block display.
	 * @param array $block_params  The set of Personalize display condition attributes.
	 * @param array $params        Array of user params passed via REST
	 * @param array $parsed_block  The full block, including name and attributes.
	 *
	 * @return bool $display_block
	 */
	abstract public function check_render_block_logic( $display_block, $block_params, $params, $parsed_block );

	/**
	 * Checks if a <pwp-block> placeholder should be displayed.
	 *
	 * @since 3.2.0
	 *
	 * @param bool  $display_placeholder Whether PersonalizeWP should show the block or not
	 * @param array $block_params        The set of Personalize display condition attributes.
	 * @param array $parsed_block        The full block, including name and attributes.
	 *
	 * @return bool $display_placeholder
	 */
	abstract public function check_render_placeholder_logic( $display_placeholder, $block_params, $parsed_block );
}
