urn $resources; } /** * @param string $path * * @return bool */ private function is_core_template( $path ) { return strpos( $path, WCML_PLUGIN_PATH ) === 0; } /** * @param mixed|string|null $name * @param string $path * * @return string */ private function get_unique_name( $name, $path ) { if ( is_null( $name ) ) { $name = basename( $path ); } if ( strpos( $path, $this->wpml_file->fix_dir_separator( get_template_directory() ) ) === 0 ) { $theme = wp_get_theme(); $name = $theme . ' - ' . $name; } elseif ( strpos( $path, $this->wpml_file->fix_dir_separator( $this->get_uploads_path() ) ) === 0 ) { $name = __( 'Uploads', 'woocommerce-multilingual' ) . ' - ' . $name; } elseif ( strpos( $path, $this->wpml_file->fix_dir_separator( WP_PLUGIN_DIR ) ) === 0 && ! $this->is_core_template( $path ) ) { $plugin_dir = $this->wpml_file->fix_dir_separator( WP_PLUGIN_DIR ); $plugin_dir = preg_replace( '#' . preg_quote( $plugin_dir ) . '#', '', $path, 1 ); $plugin_dir = ltrim( $plugin_dir, $this->ds ); $plugin_dir = explode( $this->ds, $plugin_dir ); if ( isset( $plugin_dir[0] ) ) { $require = ABSPATH . 'wp-admin' . $this->ds . 'includes' . $this->ds . 'plugin.php'; require_once $require; foreach ( get_plugins() as $slug => $plugin ) { if ( strpos( $slug, $plugin_dir[0] ) === 0 ) { $name = $plugin['Name'] . ' - ' . $name; break; } } } else { $name = substr( md5( $path ), 0, 8 ) . ' - ' . $name; } } return $name; } public function enqueue_template_resources( $templates = false ) { if ( ! $templates ) { $templates = $this->get_active_templates( true ); } $wcml_settings = $this->woocommerce_wpml->get_settings(); foreach ( $templates as $slug => $template ) { $this->enqueue_template_assets( $slug, $template ); if ( $template->has_styles() ) { $style_handler = $template->get_inline_style_handler(); } } if ( $templates ) { if ( isset( $wcml_settings['currency_switchers'] ) ) { foreach ( $wcml_settings['currency_switchers'] as $key => $switcher_data ) { $switcher_template = $switcher_data['switcher_style']; if ( ! isset( $templates[ $switcher_template ] ) ) { continue; } $css = $this->get_color_picket_css( $key, $switcher_data ); $template = $templates[ $switcher_template ]; if ( $template->has_styles() ) { wp_add_inline_style( $template->get_inline_style_handler(), $css ); } else { echo $this->get_inline_style( $key, $switcher_template, $css ); } } } if ( ! empty( $wcml_settings['currency_switcher_additional_css'] ) ) { $additional_css = $this->sanitize_css( $wcml_settings['currency_switcher_additional_css'] ); if ( ! empty( $style_handler ) ) { wp_add_inline_style( $style_handler, $additional_css ); } else { echo $this->get_inline_style( 'currency_switcher', 'additional_css', $additional_css ); } } } } /** * @param string $slug * @param WCML_Currency_Switcher_Template $template */ public function enqueue_template_assets( $slug, $template ) { $this->enqueued_templates[] = $slug; foreach ( $template->get_scripts() as $k => $url ) { wp_enqueue_script( $template->get_resource_handler( $k ), $url, [], WCML_VERSION, true ); } foreach ( $template->get_styles() as $k => $url ) { wp_enqueue_style( $template->get_resource_handler( $k ), $url, [], WCML_VERSION ); } } /** * @param string $slug * @param string $template */ public function maybe_late_enqueue_template( $slug, $template ) { if ( ! in_array( $slug, $this->enqueued_templates ) ) { $this->enqueue_template_assets( $slug, $template ); } } /** * @param string $css * * @return string */ private function sanitize_css( $css ) { $css = wp_strip_all_tags( $css ); $css = preg_replace( '/\s+/S', ' ', trim( $css ) ); return $css; } public function admin_enqueue_template_resources() { if ( AdminPages::isMultiCurrency() ) { $this->enqueue_template_resources( $this->templates ); } } public function get_color_picket_css( $switcher_id, $switcher_data ) { $css = ''; $wrapper_class = '.' . $switcher_id . '.' . $switcher_data['switcher_style']; if ( $switcher_data['color_scheme']['border_normal'] ) { $css .= "$wrapper_class, $wrapper_class li, $wrapper_class li li{"; $css .= 'border-color:' . $switcher_data['color_scheme']['border_normal'] . ' ;'; $css .= '}'; } if ( $switcher_data['color_scheme']['font_other_normal'] || $switcher_data['color_scheme']['background_other_normal'] ) { $css .= "$wrapper_class li>a {"; $css .= $switcher_data['color_scheme']['font_other_normal'] ? 'color:' . $switcher_data['color_scheme']['font_other_normal'] . ';' : ''; $css .= $switcher_data['color_scheme']['background_other_normal'] ? 'background-color:' . $switcher_data['color_scheme']['background_other_normal'] . ';' : ''; $css .= '}'; } if ( $switcher_data['color_scheme']['font_other_hover'] || $switcher_data['color_scheme']['background_other_hover'] ) { $css .= "$wrapper_class li:hover>a, $wrapper_class li:focus>a {"; $css .= $switcher_data['color_scheme']['font_other_hover'] ? 'color:' . $switcher_data['color_scheme']['font_other_hover'] . ';' : ''; $css .= $switcher_data['color_scheme']['background_other_hover'] ? 'background-color:' . $switcher_data['color_scheme']['background_other_hover'] . ';' : ''; $css .= '}'; } if ( $switcher_data['color_scheme']['font_current_normal'] || $switcher_data['color_scheme']['background_current_normal'] ) { $css .= "$wrapper_class .wcml-cs-active-currency>a {"; $css .= $switcher_data['color_scheme']['font_current_normal'] ? 'color:' . $switcher_data['color_scheme']['font_current_normal'] . ';' : ''; $css .= $switcher_data['color_scheme']['background_current_normal'] ? 'background-color:' . $switcher_data['color_scheme']['background_current_normal'] . ';' : ''; $css .= '}'; } if ( $switcher_data['color_scheme']['font_current_hover'] || $switcher_data['color_scheme']['background_current_hover'] ) { $css .= "$wrapper_class .wcml-cs-active-currency:hover>a, $wrapper_class .wcml-cs-active-currency:focus>a {"; $css .= $switcher_data['color_scheme']['font_current_hover'] ? 'color:' . $switcher_data['color_scheme']['font_current_hover'] . ';' : ''; $css .= $switcher_data['color_scheme']['background_current_hover'] ? 'background-color:' . $switcher_data['color_scheme']['background_current_hover'] . ';' : ''; $css .= '}'; } return $css; } public function get_inline_style( $switcher_id, $switcher_template, $css ) { $style_id = 'wcml-cs-inline-styles-' . $switcher_id . '-' . $switcher_template; return '' . PHP_EOL; } public function set_templates( $templates ) { $this->templates = $templates; } public function check_is_active( $template ) { $is_active = false; $active_templates = $this->get_active_templates( true ); foreach ( $active_templates as $template_key => $active_template ) { if ( $template === $template_key ) { $is_active = true; break; } } return $is_active; } public function get_first_active() { return current( array_keys( $this->get_active_templates( true ) ) ); } }