509318396
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

use function WCML\functions\isStandAlone;
use WCML\Utilities\AdminPages;
use WCML\Utilities\AdminUrl;
use WPML\FP\Relation;

class WCML_Resources {

	private static $pagenow;

	private static $woocommerce_wpml;

	private static $sitepress;

	public static function add_hooks() {
		add_action( 'admin_enqueue_scripts', [ __CLASS__, 'admin_scripts' ] );
		add_action( 'wp_enqueue_scripts', [ __CLASS__, 'front_scripts' ] );
	}

	const FORCE_MODE_SETTINGS        = 'settings';
	const FORCE_MODE_MULTI_CURRENCY  = 'multi-currency';
	const FORCE_MODE_STORE_URL       = 'store-url';
	const FORCE_MODE_TROUBLESHOOTING = 'troubleshooting';
	const FORCE_MODE_STATUS          = 'status';

	private const PRIORITY_AFTER_WC_TIPTIP = 11;

	private static function force_modes() {
		return [
			self::FORCE_MODE_SETTINGS,
			self::FORCE_MODE_MULTI_CURRENCY,
			self::FORCE_MODE_STORE_URL,
			self::FORCE_MODE_TROUBLESHOOTING,
			self::FORCE_MODE_STATUS,
		];
	}

	public static function set_up_resources( $woocommerce_wpml, $sitepress ) {
		global $pagenow;

		self::$woocommerce_wpml = $woocommerce_wpml;
		self::$sitepress        = $sitepress;
		self::$pagenow          = $pagenow;

		self::load_css();

		if ( isStandAlone() ) {
			return;
		}

		$is_edit_product     = 'post.php' === self::$pagenow && isset( $_GET['post'] ) && 'product' === get_post_type( (int) $_GET['post'] );
		$is_original_product = isset( $_GET['post'] ) && ! is_array( $_GET['post'] ) && self::$woocommerce_wpml->products->is_original_product( (int) $_GET['post'] );
		$is_new_product      = 'post-new.php' === self::$pagenow && isset( $_GET['source_lang'] ) && isset( $_GET['post_type'] ) && 'product' === $_GET['post_type'];

		if ( ( $is_edit_product && ! $is_original_product ) || ( $is_new_product && self::isNewTranslationUsingNativeEditor() ) ) {
			add_action( 'init', [ __CLASS__, 'load_lock_fields_js' ] );
			add_action( 'admin_footer', [ __CLASS__, 'hidden_label' ] );
		}
	}

	private static function isNewTranslationUsingNativeEditor(): bool {
		if ( self::$woocommerce_wpml->is_wpml_prior_4_2() ) {
			return ! self::$woocommerce_wpml->settings['trnsl_interface'];
		}

		$originalProductId = self::getOriginalProductIdFromTrid();

		return $originalProductId
			&& ! WPML_TM_Post_Edit_TM_Editor_Mode::is_using_tm_editor( self::$sitepress, $originalProductId );
	}

	private static function getOriginalProductIdFromTrid(): int {
		$trid = absint( wp_unslash( $_GET['trid'] ?? 0 ) );

		return $trid ? (int) self::$sitepress->get_original_element_id_by_trid( $trid ) : 0;
	}

	public static function load_css( $force_mode = '' ) {

		$is_wcml_settings = '' !== $force_mode || AdminPages::isWcmlSettings() || AdminPages::isTranslationQueue();
		$is_multi_currency = self::FORCE_MODE_MULTI_CURRENCY === $force_mode
			|| ( '' === $force_mode && AdminPages::isMultiCurrency() );
		$is_store_url = self::FORCE_MODE_STORE_URL === $force_mode
			|| ( '' === $force_mode && AdminPages::isTab( AdminUrl::TAB_STORE_URL ) );

		if ( $is_wcml_settings ) {

			self::load_management_css();

			if ( $is_multi_currency || $is_store_url ) {
				wp_register_style( 'wcml-dialogs', WCML_PLUGIN_URL . '/res/css/dialogs.css', [ 'wpml-dialog' ], WCML_VERSION );
				wp_enqueue_style( 'wcml-dialogs' );
			}

			wp_enqueue_style( 'wp-color-picker' );
		}

		if ( is_admin() ) {
			wp_register_style( 'wcml_admin', WCML_PLUGIN_URL . '/res/css/admin.css', [ 'wp-pointer' ], WCML_VERSION );
			wp_enqueue_style( 'wcml_admin' );
		}
	}

	public static function load_management_css() {
		wp_register_style( 'wpml-wcml', WCML_PLUGIN_URL . '/res/css/management.css', [], WCML_VERSION );
		wp_enqueue_style( 'wpml-wcml' );
	}

	public static function load_taxonomy_translation_scripts() {
		wp_register_script( 'wcml-scripts', WCML_PLUGIN_URL . '/res/js/scripts' . WCML_JS_MIN . '.js', [ 'jquery', 'jquery-ui-core', 'jquery-ui-resizable' ], WCML_VERSION, true );
		wp_register_script( 'wcml-taxonomy-translation-scripts', WCML_PLUGIN_URL . '/res/js/taxonomy_translation' . WCML_JS_MIN . '.js', [ 'jquery', 'wcml-scripts' ], WCML_VERSION, true );
		wp_enqueue_script( 'wcml-taxonomy-translation-scripts' );
	}

	public static function admin_scripts( $force_mode = '' ) {

		if ( ! is_string( $force_mode ) || ! in_array( $force_mode, self::force_modes(), true ) ) {
			$force_mode = '';
		}

		$is_wcml_settings = '' !== $force_mode || AdminPages::isWcmlSettings();
		$is_multi_currency = self::FORCE_MODE_MULTI_CURRENCY === $force_mode
			|| ( '' === $force_mode && AdminPages::isMultiCurrency() );

		if ( $is_wcml_settings ) {

			wp_register_script( 'wcml-scripts', WCML_PLUGIN_URL . '/res/js/scripts' . WCML_JS_MIN . '.js', [ 'jquery', 'jquery-ui-core', 'jquery-ui-resizable' ], WCML_VERSION, true );

			self::load_taxonomy_translation_scripts();

			wp_register_script( 'wcml-dialogs', WCML_PLUGIN_URL . '/res/js/dialogs' . WCML_JS_MIN . '.js', [ 'jquery', 'jquery-ui-core', 'jquery-ui-dialog', 'underscore' ], WCML_VERSION, true );
			wp_register_script( 'wcml-troubleshooting', WCML_PLUGIN_URL . '/res/js/troubleshooting' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true );

			if ( ! isStandalone() && self::$woocommerce_wpml->is_wpml_prior_4_2() ) {
				wp_register_script( 'wcml-translation-interface-dialog-warning', WCML_PLUGIN_URL . '/res/js/trnsl_interface_dialog_warning' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true );
				wp_enqueue_script( 'wcml-translation-interface-dialog-warning' );
			}

			wp_enqueue_script( 'wcml-scripts' );
			wp_enqueue_script( 'wp-color-picker' );
			wp_enqueue_script( 'wcml-dialogs' );
			wp_enqueue_script( 'wcml-troubleshooting' );

			wp_localize_script(
				'wcml-scripts',
				'wcml_settings',
				[
					'nonce' => wp_create_nonce( 'woocommerce_multilingual' ),
				]
			);

			self::load_tooltip_resources();
		}

		if ( '' === $force_mode && AdminPages::isTranslationsDashboard() ) {
			wp_register_script( 'wpml_tm', WCML_PLUGIN_URL . '/res/js/wpml_tm' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true );
			wp_enqueue_script( 'wpml_tm' );
		}

		if ( self::$pagenow == 'widgets.php' ) {
			wp_register_script( 'wcml_widgets', WCML_PLUGIN_URL . '/res/js/widgets' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true );
			wp_enqueue_script( 'wcml_widgets' );
		}

		if ( $is_multi_currency ) {
			wp_register_script( 'multi-currency', WCML_PLUGIN_URL . '/res/js/multi-currency' . WCML_JS_MIN . '.js', [ 'jquery', 'jquery-ui-sortable' ], WCML_VERSION, true );
			wp_enqueue_script( 'multi-currency' );

			wp_register_script( 'currency-switcher-settings', WCML_PLUGIN_URL . '/res/js/currency-switcher-settings' . WCML_JS_MIN . '.js', [ 'jquery', 'jquery-ui-sortable', 'underscore' ], WCML_VERSION, true );
			wp_enqueue_script( 'currency-switcher-settings' );
			wp_localize_script(
				'currency-switcher-settings',
				'settings',
				[
					'pre_selected_colors' => WCML_Currency_Switcher_Options_Dialog::currency_switcher_pre_selected_colors(),
				]
			);

			wp_register_script( 'exchange-rates', WCML_PLUGIN_URL . '/res/js/exchange-rates' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true );
			wp_enqueue_script( 'exchange-rates' );
		}

		wp_enqueue_script(
			'wcml-pointer',
			WCML_PLUGIN_URL . '/res/js/pointer' . WCML_JS_MIN . '.js',
			[ 'wp-pointer' ],
			WCML_VERSION,
			true
		);

		wp_register_script( 'wcml-messages', WCML_PLUGIN_URL . '/res/js/wcml-messages' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true );
		wp_enqueue_script( 'wcml-messages' );

		$is_attr_page = apply_filters( 'wcml_is_attributes_page', AdminPages::isWcProductAttributesPage() );

		if ( $is_attr_page ) {
			wp_register_script( 'wcml-attributes', WCML_PLUGIN_URL . '/res/js/wcml-attributes' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true );
			wp_enqueue_script( 'wcml-attributes' );
		}

		if ( '' === $force_mode && AdminPages::isTranslationQueue() ) {

			self::load_tooltip_resources();
			wp_enqueue_media();
			wp_register_script( 'wcml-editor', WCML_PLUGIN_URL . '/res/js/wcml-translation-editor' . WCML_JS_MIN . '.js', [ 'jquery', 'jquery-ui-core' ], WCML_VERSION, true );
			wp_enqueue_script( 'wcml-editor' );
			wp_localize_script(
				'wcml-editor',
				'wcml_settings',
				[
					'strings'     => [
						/* translators: Button label in WPML's translation editor for a product that opens the file picker. Verb phrase, imperative. */
						'choose'         => __( 'Choose a file', 'woocommerce-multilingual' ),
						/* translators: Tooltip of the disabled save button in WPML's translation editor for a product. */
						'save_tooltip'   => __( 'At least one of these fields is required: title, content or excerpt', 'woocommerce-multilingual' ),
						/* translators: Tooltip of the button that gives a translation job back, in WPML's translation editor for a product. */
						'resign_tooltip' => __( 'This translation job will no longer be assigned to you. Other translators will be able take it and continue the translation.', 'woocommerce-multilingual' ),
					],
					'hide_resign' => self::$woocommerce_wpml->products->is_hide_resign_button(),
					'nonce'       => wp_create_nonce( WCML_Translation_Editor::AUTO_SLUG_NONCE ),
				]
			);
		}

		if ( isset( $_GET['post_type'] ) && 'product' === $_GET['post_type'] && 'edit.php' === self::$pagenow ) {
			self::load_tooltip_resources();
			wp_enqueue_script( 'products-screen-options', WCML_PLUGIN_URL . '/res/js/products-screen-option.js', [ 'jquery', 'wcml-tooltip-init' ], WCML_VERSION, true );
			wp_localize_script( 'products-screen-options', 'products_screen_option', [ 'nonce' => wp_create_nonce( 'products-screen-option-action' ) ] );
		}
	}

	public static function front_scripts() {

		if ( self::$pagenow !== 'wp-login.php' ) {
			wcml_register_script( 'cart-widget', 'res/js/cart_widget' . WCML_JS_MIN . '.js', [], [ 'strategy' => 'defer', 'in_footer' => true ] );
			wp_enqueue_script( 'cart-widget' );
			wp_localize_script(
				'cart-widget',
				'actions',
				[
					'current_language' => self::$sitepress->get_current_language(),
					'force_reset'      => apply_filters( 'wcml_force_reset_cart_fragments', 0 ),
				]
			);
		}

	}

	public static function load_tooltip_resources() {

		if ( ! class_exists( 'WooCommerce' ) || ! function_exists( 'WC' ) ) {
			return;
		}

		$enqueue = function() {
			$jQueryTipTipHandler = 'wc-jquery-tiptip';

			if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '10.3', '<' ) ) {
				$jQueryTipTipHandler = 'jquery-tiptip';
			}

			if ( wp_script_is( $jQueryTipTipHandler, 'registered' ) ) {
				wp_register_script( 'wcml-tooltip-init', WCML_PLUGIN_URL . '/res/js/tooltip_init' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true );
				wp_enqueue_script( $jQueryTipTipHandler );
				wp_enqueue_script( 'wcml-tooltip-init' );
			}
			if ( wp_style_is( 'woocommerce_admin_styles', 'registered' ) ) {
				wp_enqueue_style( 'woocommerce_admin_styles' );
			}
		};

		if ( did_action( 'admin_enqueue_scripts' ) && ! doing_action( 'admin_enqueue_scripts' ) ) {
			$enqueue();
		} else {
			add_action( 'admin_enqueue_scripts', $enqueue, self::PRIORITY_AFTER_WC_TIPTIP );
		}

	}

	public static function load_lock_fields_js() {
		global $pagenow;

		wp_register_script( 'wcml-lock-script', WCML_PLUGIN_URL . '/res/js/lock_fields' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true );
		wp_enqueue_script( 'wcml-lock-script' );

		$product_id = false;
		if ( $pagenow === 'post.php' && isset( $_GET['post'] ) ) {
			$product_id = $_GET['post'];
		} elseif ( isset( $_POST['product_id'] ) ) {
			$product_id = $_POST['product_id'];
		}

		$file_path_sync = WCML_Downloadable_Products::isDownloadableFilesSetToUseSame( $product_id );

		wp_localize_script(
			'wcml-lock-script',
			'unlock_fields',
			[
				'menu_order' => self::$woocommerce_wpml->settings['products_sync_order'],
				'file_paths' => $file_path_sync,
			]
		);
		wp_localize_script(
			'wcml-lock-script',
			'non_standard_fields',
			[
				'ids'         => apply_filters( 'wcml_js_lock_fields_ids', [] ),
				'classes'     => apply_filters( 'wcml_js_lock_fields_classes', [] ),
				'input_names' => apply_filters( 'wcml_js_lock_fields_input_names', [] ),
			]
		);

		do_action( 'wcml_after_load_lock_fields_js' );

	}

	public static function hidden_label() {
		/* translators: Tooltip and alternative text of the padlock icon next to a field the translator may not change; "its value" is the field's value. */
		$lockedFieldTooltip = __( 'This field is locked for editing because WPML will copy its value from the original language.', 'woocommerce-multilingual' );

		echo '<img src="' . esc_url( WCML_PLUGIN_URL . '/res/images/locked.png' ) . '" class="wcml_lock_img wcml_lock_icon" alt="' .
			esc_attr( $lockedFieldTooltip ) .
			'" title="' . esc_attr( $lockedFieldTooltip ) .
			'" style="display: none;position:relative;left:2px;top:2px;">';

		if ( isset( $_GET['post'] ) ) {
			$original_id = self::$woocommerce_wpml->products->get_original_product_id( sanitize_text_field( $_GET['post'] ) );
		} else {
			$original_id = self::getOriginalProductIdFromTrid();
		}

		if ( ! isset( $_GET['lang'] ) || ! $original_id ) {
			return;
		}

		echo '<h3 class="wcml_prod_hidden_notice">' .
			sprintf(
				/* translators: %1$s is the post title inside a HTML link pointing to the post edit screen, %2$s and %3$s are opening and closing HTML link tags */
				esc_html__(
					"This is a translation of %1\$s. Some of the fields are not editable. It's recommended that you translate products from the %2\$sTranslation Dashboard%3\$s.",
					'woocommerce-multilingual'
				),
				'<a href="' . esc_url( get_edit_post_link( $original_id ) ) . '" >' . esc_html( get_the_title( $original_id ) ) . '</a>',
				'<a href="' . esc_url( AdminUrl::getWPMLTMDashboardProducts() ) . '">',
				'</a>'
			) . '</h3>';
	}
}
