<?php
/**
 * Astra Addon Customizer
 *
 * @package Astra Addon
 * @since 1.6.0
 */

if ( ! class_exists( 'Astra_Addon_Elementor_Compatibility' ) ) {

	/**
	 * Astra Addon Page Builder Compatibility base class
	 *
	 * @since 1.6.0
	 */
	class Astra_Addon_Elementor_Compatibility extends Astra_Addon_Page_Builder_Compatibility {
		/**
		 * Instance
		 *
		 * @since 1.6.0
		 *
		 * @var object Class object.
		 */
		private static $instance;

		/**
		 * Initiator
		 *
		 * @since 1.6.0
		 *
		 * @return object initialized object of class.
		 */
		public static function get_instance() {
			if ( ! isset( self::$instance ) ) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		/**
		 * Render content for post.
		 *
		 * @param int $post_id Post id.
		 *
		 * @since 1.6.0
		 */
		public function render_content( $post_id ) {

			// set post to glabal post.
			$elementor_instance = Elementor\Plugin::instance();
			echo do_shortcode( $elementor_instance->frontend->get_builder_content_for_display( $post_id ) );
		}

		/**
		 * Load styles and scripts.
		 *
		 * @param int $post_id Post id.
		 *
		 * @since 1.6.0
		 */
		public function enqueue_scripts( $post_id ) {

			if ( '' !== $post_id ) {
				if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
					$css_file = new \Elementor\Core\Files\CSS\Post( $post_id );
				} elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) {
					$css_file = new \Elementor\Post_CSS_File( $post_id );
				}

				$css_file->enqueue();

				// Only needed when Elementor's Atomic Widgets (Editor V4) feature is active.
				if ( class_exists( '\Elementor\Modules\AtomicWidgets\Module' ) && Elementor\Modules\AtomicWidgets\Module::is_active() ) {

					/**
					 * Elementor collects Atomic Widgets styles only for the main queried post,
					 * so register this Custom Layout's post ID as well.
					 *
					 * @since 4.13.8
					 */
					do_action( 'elementor/post/render', $post_id );

					// Don't flush `Frontend::enqueue_styles()` inline: it runs once per request, so it would consume that guard before Elementor Pro's
					// priority-10 flush and drop the template's styles. Defer to priority 20 as a fallback for when no Pro/core flush runs.
					if ( false === has_action( 'wp_enqueue_scripts', array( $this, 'flush_elementor_styles' ) ) ) {
						add_action( 'wp_enqueue_scripts', array( $this, 'flush_elementor_styles' ), 20 );
					}
				}
			}
		}

		/**
		 * Fallback flush of Elementor's style pipeline when no earlier Elementor path flushed it this request.
		 *
		 * @since 4.13.12
		 */
		public function flush_elementor_styles() {
			$elementor_frontend = Elementor\Plugin::instance()->frontend;

			if ( method_exists( $elementor_frontend, 'enqueue_styles' ) ) {
				$elementor_frontend->enqueue_styles();
			}
		}

	}

}
