<?php
/**
 * Elementor views manager
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

if ( ! class_exists( 'Jet_Engine_Blocks_Views' ) ) {

	/**
	 * Define Jet_Engine_Blocks_Views class
	 */
	class Jet_Engine_Blocks_Views {

		public $editor;
		public $render;
		public $block_types;
		public $dynamic_content;
		/**
		 * @var \Crocoblock\Blocks_Style\Manager
		 */
		public $style_manager;

		/**
		 * Constructor for the class
		 */
		function __construct() {

			if ( ! \Jet_Engine\Modules\Performance\Module::instance()->is_tweak_active( 'enable_blocks_views' ) ) {
				return;
			}

			if ( ! jet_engine()->components->is_component_active( 'listings' ) ) {
				return;
			}

			add_filter( 'jet-engine/templates/listing-views', array( $this, 'add_listing_view' ), 11 );
			add_filter( 'upload_mimes', array( $this, 'allow_svg' ), 10, 2 );
			add_filter( 'jet-engine/templates/create/data', array( $this, 'inject_listing_settings' ), 0 );

			if ( is_admin() ) {
				require $this->component_path( 'editor.php' );
				$this->editor = new Jet_Engine_Blocks_Views_Editor();
			}

			require $this->component_path( 'render.php' );
			require $this->component_path( 'block-types.php' );
			require $this->component_path( 'ajax-handlers.php' );
			require $this->component_path( 'dynamic-content/manager.php' );
			require $this->component_path( 'components/register.php' );
			require $this->component_path( 'content-setter.php' );
			require $this->component_path( 'core-styles.php' );

			$this->render          = new Jet_Engine_Blocks_Views_Render();
			$this->block_types     = new Jet_Engine_Blocks_Views_Types();
			$this->dynamic_content = new \Jet_Engine\Blocks_Views\Dynamic_Content\Manager();

			new Jet_Engine_Blocks_Views_Ajax_Handlers();
			new \Jet_Engine\Blocks_Views\Components\Register();
			new \Jet_Engine\Blocks_Views\Content_Setter();
			new \Jet_Engine\Blocks_Views\Core_Styles();

			$module_data = jet_engine()->framework->get_included_module_data( 'style-manager.php' );

			//Enable styles migrator in any of plugins when all be ready
			//\Crocoblock\Blocks_Style\Manager::$requires_migration = true;

			$this->style_manager = new \Crocoblock\Blocks_Style\Manager( array(
				'path' => $module_data['path'],
				'url'  => $module_data['url'],
			) );
		}

		/**
		 * Register listing view
		 *
		 * @param [type] $views [description]
		 */
		public function add_listing_view( $views ) {
			$views['blocks'] = __( 'Blocks (Gutenberg)', 'jet-engine' );
			return $views;
		}

		/**
		 * Allow SVG images uploading for users who can upload files.
		 *
		 * `is_admin()` cannot protect this filter because admin-ajax.php is an
		 * admin request for unauthenticated visitors too. Legacy Forms applies its
		 * own stricter policy and never accepts SVG uploads.
		 *
		 * @param  array            $mimes Allowed MIME types.
		 * @param  int|WP_User|null $user  Current user or null for the current user.
		 * @return array
		 */
		public function allow_svg( $mimes, $user = null ) {

			$user = $user ? $user : wp_get_current_user();

			if ( ! $user || ! user_can( $user, 'upload_files' ) ) {
				return $mimes;
			}

			$mimes['svg'] = 'image/svg+xml';
			return $mimes;
		}

		/**
		 * Return path to file inside component
		 *
		 * @param  [type] $path_inside_component [description]
		 * @return [type]                        [description]
		 */
		public function component_path( $path_inside_component ) {
			return jet_engine()->plugin_path( 'includes/components/blocks-views/' . $path_inside_component );
		}

		/**
		 * Return listing template ediit URL to redirect on
		 * @return [type] [description]
		 */
		public function get_redirect_url( $template_id ) {
			return get_edit_post_link( $template_id, '' );
		}

		/**
		 * Check if current listing is rendered with blocks
		 * @param  [type]  $listing_id [description]
		 * @return boolean             [description]
		 */
		public function is_blocks_listing( $listing_id ) {

			/**
			 * Do not use get_listing_type() method here
			 * because this method itself used inside get_listing_type() to define what listing type was used
			 */
			$meta = get_post_meta( $listing_id, '_listing_type', true );

			if ( ! $meta ) {
				$post    = get_post( $listing_id );
				$content = $post ? $post->post_content : '';

				if ( false !== strrpos( $content, 'wp:jet-engine' ) ) {
					$meta = 'blocks';
					update_post_meta( $listing_id, '_listing_type', $meta );
				}

			}

			return ( 'blocks' === $meta );
		}

		/**
		 * Inject listing settings from template into _elementor_page_settings meta
		 * @param  [type] $template_data [description]
		 * @return [type]                [description]
		 */
		public function inject_listing_settings( $template_data ) {

			// phpcs:disable WordPress.Security.NonceVerification
			if ( empty( $_REQUEST['listing_view_type'] ) || 'blocks' !== $_REQUEST['listing_view_type'] ) {
				return $template_data;
			}

			if ( ! isset( $_REQUEST['listing_source'] ) ) {
				return $template_data;
			}
			// phpcs:enable WordPress.Security.NonceVerification

			return $template_data;

		}

	}

}
