Sindbad~EG File Manager

Current Path : /home/xiedrke/entrepot/wp-content/plugins/realpress/app/Controllers/
Upload File :
Current File : /home/xiedrke/entrepot/wp-content/plugins/realpress/app/Controllers/EnqueueScriptsController.php

<?php

namespace RealPress\Controllers;

use RealPress\Helpers\Debug;
use RealPress\Helpers\General;
use RealPress\Helpers\Page;
use RealPress\Helpers\Config;
use RealPress\Helpers\Price;
use RealPress\Helpers\RestApi;
use RealPress\Helpers\Settings;
use RealPress\Helpers\Taxonomy;
use RealPress\Models\PropertyMetaModel;

/**
 * Class EnqueueScriptsController
 * @package RealPress\Controllers
 */
class EnqueueScriptsController {

	/**
	 * @var mixed|string
	 */
	private $version_assets = REALPRESS_VERSION;

	/**
	 * EnqueueScripts constructor.
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'init' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
	}

	public function init() {
		if ( Debug::is_debug() ) {
			$this->version_assets = uniqid();
		}
	}

	/**
	 * @param $args
	 *
	 * @return mixed|void
	 */
	public function can_load_asset( $args ) {
		$current_page = Page::get_current_page();
		$can_load     = false;

		if ( ! empty( $args['screens'] ) ) {
			if ( in_array( $current_page, $args['screens'] ) ) {
				$can_load = true;
			}
		} elseif ( ! empty( $args['exclude_screens'] ) ) {
			if ( ! in_array( $current_page, $args['exclude_screens'] ) ) {
				$can_load = true;
			}
		} else {
			$can_load = true;
		}

		$is_on = 'admin';
		if ( ! is_admin() ) {
			$is_on = 'frontend';
		}

		return apply_filters(
			'realpress/filter/' . $is_on . '/can-load-assets/' . $args['type'] . '/' . $args['handle'],
			$can_load,
			$current_page,
			$args['screens']
		);
	}

	/**
	 * @return void
	 */
	public function admin_enqueue_scripts() {
		$styles = Config::instance()->get( 'styles:admin' );

		$this->enqueue_css( $styles );

		$scripts          = Config::instance()->get( 'scripts:admin' );
		$register_scripts = $scripts['register'];

		$this->enqueue_js( $register_scripts );
		$this->localize_admin_script();

		$js_translation = $scripts['js-translation'];
		if ( ! empty( $js_translation ) ) {
			foreach ( $js_translation as $handle => $args ) {
				wp_set_script_translations( $handle, 'realpress', $args['src'] );
			}
		}
	}

	/**
	 * @return void
	 */
	public function wp_enqueue_scripts() {
		wp_enqueue_style( 'realpress-font-icon', REALPRESS_ASSETS_URL . 'fonts/realpress-icon/style.css', array(), $this->version_assets );
		$styles = Config::instance()->get( 'styles:frontend' );
		$this->enqueue_css( $styles );

		$scripts          = Config::instance()->get( 'scripts:frontend' );
		$register_scripts = $scripts['register'];
		$this->enqueue_js( $register_scripts );

		$this->localize_frontend_script();
	}

	/**
	 * @return void
	 */
	public function localize_frontend_script() {
		if ( is_singular( REALPRESS_PROPERTY_CPT ) ) {
			$meta_data = get_post_meta( get_the_ID(), REALPRESS_PROPERTY_META_KEY, true );
			wp_localize_script(
				'realpress-single-property',
				'REALPRESS_SINGLE_PROPERTY_PAGE',
				array(
					'map'         => array(
						'name' => $meta_data['group:map:section:map:fields:name'] ?? '',
						'lat'  => $meta_data['group:map:section:map:fields:lat'] ?? '',
						'lng'  => $meta_data['group:map:section:map:fields:lng'] ?? '',
					),
					'property_id' => get_the_ID(),
				)
			);
		}

		if ( Page::is_property_archive_page() ) {
			wp_localize_script(
				'realpress-archive-property',
				'REALPRESS_PROPERTY_ARCHIVE_OBJECT',
				array(
					'term_id'  => get_queried_object()->term_id ?? '',
					'taxonomy' => get_queried_object()->taxonomy ?? '',
				)
			);
		}

		if ( Page::is_agent_detail_page() ) {
			wp_localize_script(
				'realpress-agent-detail',
				'REALPRESS_AGENT_DETAIL_OBJECT',
				array(
					'user_id' => get_queried_object()->ID ?? '',
				)
			);
		}

		$advanced_search = array(
			'min_price' => Settings::get_setting_detail( 'group:advanced_search:fields:min_price' ),
			'max_price' => Settings::get_setting_detail( 'group:advanced_search:fields:max_price' ),
			'step'      => Settings::get_setting_detail( 'group:advanced_search:fields:step_price' ),
		);

		$archive_property_page_url = get_post_type_archive_link( REALPRESS_PROPERTY_CPT );
		if ( empty( Settings::get_setting_detail( 'group:advanced_search:fields:redirect_to' ) ) ) {
			$advanced_search['redirect_page_url'] = $archive_property_page_url;
		} else {
			$advanced_search['redirect_page_url'] = get_permalink( Settings::get_setting_detail( 'group:advanced_search:fields:redirect_to' ) );
		}

		$agent_list_page_id         = Settings::get_page_id( 'agent_list_page' );
		$compare_property_page_id   = Settings::get_page_id( 'compare_property_page' );
		$compare_property_page_link = empty( $compare_property_page_id ) ? '' : get_permalink( $compare_property_page_id );

		wp_localize_script(
			'realpress-global',
			'REALPRESS_GLOBAL_OBJECT',
			apply_filters(
				'realpress/filter/frontend-script/global-object',
				array(
					'rest_url'                  => get_rest_url(),
					'rest_namespace'            => RestApi::generate_namespace(),
					'user_id'                   => is_user_logged_in() ? get_current_user_id() : '',
					'image_placeholder'         => General::get_default_image(),
					'archive_property_page_url' => $archive_property_page_url,
					'login_page_url'            => apply_filters( 'realpress/filter/login-url', wp_login_url() ),
					'advanced_search'           => $advanced_search,
					'currency_symbol'           => Price::get_currency_symbol(),
					'thousands_separator'       => Settings::get_setting_detail( 'group:general:fields:currency_thousands_separator' ),
					'number_decimals'           => Settings::get_setting_detail( 'group:general:fields:currency_number_decimals' ),
					'currency_position'         => Settings::get_setting_detail( 'group:general:fields:currency_position' ),
					'agent_list_page_url'       => empty( $agent_list_page_id ) ? '' : get_permalink( $agent_list_page_id ),
					'compare_property_page_url' => $compare_property_page_link,
					'property_page_layout'      => Settings::get_setting_detail( 'group:property:fields:page_layout' ),
					'pagination_type'           => Settings::get_pagination_type(),
					'map_type'                  => Settings::get_setting_detail( 'group:property:fields:map' ),
					'nonce'                     => wp_create_nonce( 'wp_rest' ),
				)
			)
		);

		wp_localize_script(
			'realpress-global',
			'REALPRESS_TRANSLATION_OBJECT',
			apply_filters(
				'realpress/filter/frontend-script/translation-object',
				array(
					'compare_added_msg'   => sprintf( __( 'Property Compare Added. <a href="%s">View Compare</a>', 'realpress' ), $compare_property_page_link ),
					'compare_removed_msg' => sprintf( __( 'Property Compare Removed. <a href="%s">View Compare</a>', 'realpress' ), $compare_property_page_link ),
				)
			)
		);
	}

	/**
	 * @return void
	 */
	public function localize_admin_script() {
		wp_localize_script(
			'realpress-global',
			'REALPRESS_GLOBAL_OBJECT',
			apply_filters(
				'realpress/filter/admin-script/global-object',
				array(
					'rest_namespace'      => RestApi::generate_namespace(),
					'siteurl'             => site_url(),
					'map_type'            => Settings::get_setting_detail( 'group:property:fields:map' ),
					'currency_symbol'     => Price::get_currency_symbol(),
					'thousands_separator' => Settings::get_setting_detail( 'group:general:fields:currency_thousands_separator' ),
					'number_decimals'     => Settings::get_setting_detail( 'group:general:fields:currency_number_decimals' ),
					'currency_position'   => Settings::get_setting_detail( 'group:general:fields:currency_position' ),
				)
			)
		);

		if ( Page::is_admin_single_property_page() ) {
			$property_id = Page::get_property_single_edit_page();
			$taxonomies  = $this->get_property_taxonomies();
			$term_ids    = $this->get_post_terms( $property_id, $taxonomies );
			wp_localize_script(
				'realpress-property',
				'REALPRESS_ADMIN_SINGLE_PROPERTY_OBJECT',
				array(
					'post_terms'                   => $term_ids,
					'taxonomies'                   => $taxonomies,
					'selected_one_term_taxonomies' => Taxonomy::get_selected_one_term_taxonomies(),
				)
			);
		}

		wp_localize_script(
			'realpress-edit-block',
			'REALPRESS_ADMIN_EDIT_PROPERTY',
			array()
		);
	}

	/**
	 * @return int[]|string[]
	 */
	public function get_property_taxonomies() {
		$taxonomies = Config::instance()->get( 'property-type:taxonomies' );

		return array_keys( $taxonomies );
	}

	/**
	 * @param $post_id
	 * @param $taxonomies
	 *
	 * @return array
	 */
	public function get_post_terms( $post_id, $taxonomies ) {
		$term_ids = array();
		foreach ( $taxonomies as $taxonomy_slug ) {
			$all_post_terms = PropertyMetaModel::instance( $post_id )->get_property_terms( $taxonomy_slug );

			if ( ! empty( $all_post_terms ) ) {
				foreach ( $all_post_terms as $post_term ) {
					$term_ids[ $taxonomy_slug ][] = $post_term->term_id;
				}
			}
		}

		return $term_ids;
	}

	public function initMap() {
		?>
		<script>
			function initMap() {
			}
		</script>
		<?php
	}

	/**
	 * @param $styles
	 *
	 * @return void
	 */
	public function enqueue_css( $styles ) {
		foreach ( $styles as $handle => $args ) {
			wp_register_style(
				$handle,
				$args['src'] ?? '',
				$args['deps'] ?? array(),
				$this->version_assets,
				'all'
			);

			$can_load_asset = $this->can_load_asset(
				array(
					'handle'          => $handle,
					'screens'         => $args['screens'] ?? array(),
					'exclude_screens' => $args['exclude_screens'] ?? array(),
					'type'            => 'css',
				)
			);

			if ( $can_load_asset ) {
				wp_enqueue_style( $handle );
			}
		}
	}

	/**
	 * @param $register_scripts
	 *
	 * @return void
	 */
	public function enqueue_js( $register_scripts ) {
		foreach ( $register_scripts as $handle => $args ) {
			if ( isset( $args['condition'] ) && $args['condition'] === false ) {
				continue;
			}

			wp_register_script(
				$handle,
				$args['src'],
				$args['deps'] ?? array(),
				$this->version_assets,
				$args['in_footer'] ?? true
			);

			$can_load_asset = $this->can_load_asset(
				array(
					'handle'          => $handle,
					'screens'         => $args['screens'] ?? array(),
					'exclude_screens' => $args['exclude_screens'] ?? array(),
					'type'            => 'js',
				)
			);

			if ( $can_load_asset ) {
				if ( $handle === 'realpress-google-map' ) {
					$this->initMap();
				}

				wp_enqueue_script( $handle );
			}
		}
	}
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists