WCML_Currency_Switcher_Templates

The WooCommerce Multilingual WCML Currency Switcher Templates class.

Defined (1)

The class is defined in the following location(s).

/inc/currencies/currency-switcher/class-wcml-currency-switcher-templates.php  
  1. class WCML_Currency_Switcher_Templates { 
  2.  
  3. const CONFIG_FILE = 'config.json'; 
  4.  
  5. /** 
  6. * @var woocommerce_wpml 
  7. */ 
  8. private $woocommerce_wpml; 
  9.  
  10. /** @var string $uploads_path */ 
  11. private $uploads_path; 
  12.  
  13. /** 
  14. * @var WPML_File 
  15. */ 
  16. private $wpml_file; 
  17.  
  18. /** @var array $templates Collection of WCML_CS_Template */ 
  19. private $templates = false; 
  20.  
  21. /** @var string $ds */ 
  22. private $ds = DIRECTORY_SEPARATOR; 
  23.  
  24. public function __construct( &$woocommerce_wpml, WCML_File $wpml_file = null ) { 
  25.  
  26. $this->woocommerce_wpml = $woocommerce_wpml; 
  27.  
  28. if ( ! $wpml_file ) { 
  29. //TODO: use WPML_FILE class instead after changing requirements for WPML >= 3.6.0 
  30. $wpml_file = new WCML_File(); 
  31.  
  32. $this->wpml_file = $wpml_file; 
  33.  
  34. public function init_hooks() { 
  35. add_action( 'after_setup_theme', array( $this, 'after_setup_theme_action' ) ); 
  36. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_template_resources' ) ); 
  37. add_action( 'admin_head', array( $this, 'admin_enqueue_template_resources' ) ); 
  38.  
  39. public function after_setup_theme_action() { 
  40. $this->init_available_templates(); 
  41.  
  42. /** 
  43. * @param string $template_slug 
  44. * @return WCML_CS_Template 
  45. */ 
  46. public function get_template( $template_slug ) { 
  47. $ret = false; 
  48. if ( array_key_exists( $template_slug, $this->templates ) ) { 
  49. $ret = $this->templates[ $template_slug ]; 
  50.  
  51. return $ret; 
  52.  
  53. /** 
  54. * @return array of active WCML_CS_Templates 
  55. */ 
  56. public function get_active_templates() { 
  57.  
  58. $templates = array(); 
  59. $wcml_settings = $this->woocommerce_wpml->get_settings(); 
  60.  
  61. if( isset( $wcml_settings[ 'currency_switchers' ] ) ) { 
  62. foreach( $wcml_settings[ 'currency_switchers' ] as $switcher_id => $switcher ) { 
  63. if( 'product' === $switcher_id && 0 === $wcml_settings[ 'currency_switcher_product_visibility' ] ) { 
  64. continue; 
  65.  
  66. foreach( $this->templates as $key => $template ) { 
  67. if( $switcher['switcher_style'] == $key && !isset( $templates[$key] ) ) { 
  68. $templates[$key] = $template; 
  69. }elseif( isset( $wcml_settings[ 'currency_switcher_product_visibility' ] ) && $wcml_settings[ 'currency_switcher_product_visibility' ] === 1 ) { 
  70. //set default template to active 
  71. $templates['wcml-dropdown'] = $this->templates['wcml-dropdown']; 
  72.  
  73. return $templates; 
  74.  
  75. /** 
  76. * @return array of template data 
  77. */ 
  78. public function get_templates() { 
  79.  
  80. $templates = array(); 
  81.  
  82. foreach( $this->templates as $key => $template ) { 
  83.  
  84. $template_data = $template->get_template_data(); 
  85.  
  86. if( isset( $template_data['is_core'] ) && $template_data['is_core'] ) { 
  87. $templates[ 'core' ][ $key ] = $template_data; 
  88. }else{ 
  89. $templates[ 'custom' ][ $key ] = $template_data; 
  90.  
  91. return $templates; 
  92.  
  93. /** 
  94. * @return null|string 
  95. */ 
  96. private function get_uploads_path() { 
  97. if ( ! $this->uploads_path ) { 
  98. $uploads = wp_upload_dir( null, false ); 
  99.  
  100. if ( isset( $uploads['basedir'] ) ) { 
  101. $this->uploads_path = $uploads['basedir']; 
  102.  
  103. return $this->uploads_path; 
  104.  
  105. /** 
  106. * @param string $template_path 
  107. * @return array 
  108. */ 
  109. private function parse_template_config( $template_path ) { 
  110. $config = array(); 
  111. $configuration_file = $template_path . $this->ds . self::CONFIG_FILE; 
  112. if ( file_exists( $configuration_file ) ) { 
  113. $json_content = file_get_contents( $configuration_file ); 
  114. $config = json_decode( $json_content, true ); 
  115.  
  116. return $config; 
  117.  
  118. private function init_available_templates() { 
  119.  
  120. $templates = array(); 
  121. $dirs_to_scan = array(); 
  122.  
  123. /** 
  124. * Filter the directories to scan 
  125. * @param array $dirs_to_scan 
  126. */ 
  127. $dirs_to_scan = apply_filters( 'wcml_cs_directories_to_scan', $dirs_to_scan ); 
  128.  
  129. $sub_dir = $this->ds . 'templates' . $this->ds . 'currency-switchers'; 
  130.  
  131. $wcml_core_path = WCML_PLUGIN_PATH . $sub_dir; 
  132. $theme_path = get_template_directory() . $this->ds . 'wpml' . $sub_dir; 
  133. $child_theme_path = get_stylesheet_directory() . $this->ds . 'wpml' . $sub_dir; 
  134. $uploads_path = $this->get_uploads_path() . $this->ds . 'wpml' . $sub_dir; 
  135.  
  136. array_unshift( $dirs_to_scan, $wcml_core_path, $theme_path, $child_theme_path, $uploads_path ); 
  137.  
  138. $templates_paths = $this->scan_template_paths( $dirs_to_scan ); 
  139.  
  140. foreach ( $templates_paths as $template_path ) { 
  141. $template_path = $this->wpml_file->fix_dir_separator( $template_path ); 
  142. if ( file_exists( $template_path . $this->ds . WCML_Currency_Switcher_Template::FILENAME ) ) { 
  143. $tpl = array(); 
  144. $config = $this->parse_template_config( $template_path ); 
  145.  
  146. $tpl['path'] = $template_path; 
  147. $tpl['name'] = isset( $config['name'] ) ? $config['name'] : null; 
  148. $tpl['name'] = $this->get_unique_name( $tpl['name'], $template_path ); 
  149. $tpl['slug'] = sanitize_title_with_dashes( $tpl['name'] ); 
  150. $tpl['css'] = $this->get_files( 'css', $template_path, $config ); 
  151. $tpl['js'] = $this->get_files( 'js', $template_path, $config ); 
  152.  
  153. if ( $this->is_core_template( $template_path ) ) { 
  154. $tpl['is_core'] = true; 
  155. $tpl['slug'] = isset( $config['slug'] ) ? $config['slug'] : $tpl['slug']; 
  156.  
  157. $templates[ $tpl['slug'] ] = new WCML_Currency_Switcher_Template( $this->woocommerce_wpml, $tpl ); 
  158.  
  159. $this->set_templates( $templates ); 
  160.  
  161. /** 
  162. * @param array $dirs_to_scan 
  163. * @return array 
  164. */ 
  165. private function scan_template_paths( $dirs_to_scan ) { 
  166. $templates_paths = array(); 
  167.  
  168. foreach ( $dirs_to_scan as $dir ) { 
  169. if ( !is_dir( $dir ) ) { 
  170. continue; 
  171. $files = scandir( $dir ); 
  172. $files = array_diff( $files, array( '..', '.' ) ); 
  173. if ( count( $files ) > 0 ) { 
  174. foreach ( $files as $file ) { 
  175. $template_path = $dir . '/' . $file; 
  176. if ( is_dir( $template_path ) 
  177. && file_exists( $template_path . $this->ds . WCML_Currency_Switcher_Template::FILENAME ) 
  178. && file_exists( $template_path . $this->ds . self::CONFIG_FILE ) 
  179. ) { 
  180. $templates_paths[] = $template_path; 
  181.  
  182. return $templates_paths; 
  183.  
  184.  
  185. /** 
  186. * @param string $ext 
  187. * @param string $template_path 
  188. * @param array $config 
  189. * @return array|null 
  190. */ 
  191. private function get_files( $ext, $template_path, $config ) { 
  192. $resources = array(); 
  193.  
  194. if( isset( $config[ $ext ] ) ) { 
  195. $config[ $ext ] = is_array( $config[ $ext ] ) ? $config[ $ext ] : array( $config[ $ext ] ); 
  196. foreach ( $config[ $ext ] as $file ) { 
  197. $file = untrailingslashit( $template_path ) .$this->ds . $file; 
  198. $resources[] = $this->wpml_file->get_uri_from_path( $file ); 
  199. } else { 
  200. $search_path = $template_path . $this->ds . '*.' . $ext; 
  201. if ( glob( $search_path ) ) { 
  202. foreach ( glob( $search_path ) as $file ) { 
  203. $resources[] = $this->wpml_file->get_uri_from_path( $file ); 
  204.  
  205. return $resources; 
  206.  
  207. /** 
  208. * @param string $path 
  209. * @return bool 
  210. */ 
  211. private function is_core_template( $path ) { 
  212. return strpos( $path, WCML_PLUGIN_PATH ) === 0; 
  213.  
  214. /** 
  215. * @param mixed|string|null $name 
  216. * @param string $path 
  217. * @return string 
  218. */ 
  219. private function get_unique_name( $name, $path ) { 
  220. if ( is_null( $name ) ) { 
  221. $name = basename( $path ); 
  222.  
  223. if ( strpos( $path, $this->wpml_file->fix_dir_separator( get_template_directory() ) ) === 0 ) { 
  224. $theme = wp_get_theme(); 
  225. $name = $theme . ' - ' . $name; 
  226. } elseif ( strpos( $path, $this->wpml_file->fix_dir_separator( $this->get_uploads_path() ) ) === 0 ) { 
  227. $name = __( 'Uploads', 'woocommerce-multilingual' ) . ' - ' . $name; 
  228. } elseif ( 
  229. strpos( $path, $this->wpml_file->fix_dir_separator( WP_PLUGIN_DIR ) ) === 0 
  230. && ! $this->is_core_template( $path ) 
  231. ) { 
  232. $plugin_dir = $this->wpml_file->fix_dir_separator( WP_PLUGIN_DIR ); 
  233. $plugin_dir = preg_replace( '#' . preg_quote( $plugin_dir ) . '#' , '', $path, 1 ); 
  234. $plugin_dir = ltrim( $plugin_dir, $this->ds ); 
  235. $plugin_dir = explode( $this->ds, $plugin_dir ); 
  236.  
  237. if ( isset( $plugin_dir[0] ) ) { 
  238. $require = ABSPATH . 'wp-admin' . $this->ds . 'includes' . $this->ds . 'plugin.php'; 
  239. require_once( $require ); 
  240. foreach ( get_plugins() as $slug => $plugin ) { 
  241. if ( strpos( $slug, $plugin_dir[0] ) === 0 ) { 
  242. $name = $plugin['Name'] . ' - ' . $name; 
  243. break; 
  244. } else { 
  245. $name = substr( md5( $path ), 0, 8 ) . ' - ' . $name; 
  246.  
  247. return $name; 
  248.  
  249. public function enqueue_template_resources( $templates = false ) { 
  250.  
  251. if( !$templates ) { 
  252. $templates = $this->get_active_templates(); 
  253.  
  254. $wcml_settings = $this->woocommerce_wpml->get_settings(); 
  255.  
  256. foreach( $templates as $slug => $template ) { 
  257.  
  258. foreach ( $template->get_scripts() as $k => $url ) { 
  259. wp_enqueue_script( $template->get_resource_handler( $k ), $url, array(), WCML_VERSION ); 
  260.  
  261. foreach ( $template->get_styles() as $k => $url ) { 
  262. wp_enqueue_style( $template->get_resource_handler( $k ), $url, array(), WCML_VERSION ); 
  263.  
  264.  
  265. if ( $template->has_styles() ) { 
  266. $style_handler = $template->get_inline_style_handler(); 
  267.  
  268. if( $templates ) { 
  269. if( isset( $wcml_settings[ 'currency_switchers' ] ) ) { 
  270. foreach( $wcml_settings[ 'currency_switchers' ] as $key => $switcher_data ) { 
  271.  
  272. $switcher_template = $switcher_data['switcher_style']; 
  273. $css = $this->get_color_picket_css( $key, $switcher_data ); 
  274. $template = $templates[ $switcher_template ]; 
  275.  
  276. if ( $template->has_styles() ) { 
  277. wp_add_inline_style( $template->get_inline_style_handler(), $css ); 
  278. }else{ 
  279. echo $this->get_inline_style( $key, $switcher_template, $css ); 
  280.  
  281. if ( ! empty( $wcml_settings['currency_switcher_additional_css'] ) ) { 
  282. $additional_css = $this->sanitize_css( $wcml_settings['currency_switcher_additional_css'] ); 
  283.  
  284. if( $style_handler ) { 
  285. wp_add_inline_style( $style_handler, $additional_css ); 
  286. }else{ 
  287. echo $this->get_inline_style( 'currency_switcher', 'additional_css', $additional_css ); 
  288.  
  289. /** 
  290. * @param string $css 
  291. * @return string 
  292. */ 
  293. private function sanitize_css( $css ) { 
  294. $css = wp_strip_all_tags( $css ); 
  295. $css = preg_replace('/\s+/S', " ", trim( $css ) ); 
  296. return $css; 
  297.  
  298. public function admin_enqueue_template_resources() { 
  299. if( isset( $_GET['page'] ) && $_GET['page'] == 'wpml-wcml' && isset( $_GET['tab'] ) && $_GET['tab'] == 'multi-currency' ) { 
  300. $this->enqueue_template_resources( $this->templates ); 
  301.  
  302. public function get_color_picket_css( $switcher_id, $switcher_data ) { 
  303.  
  304. $css = ''; 
  305. $wrapper_class = '.'.$switcher_id.'.'.$switcher_data[ 'switcher_style' ]; 
  306.  
  307. if ( $switcher_data[ 'color_scheme' ][ 'border_normal' ] ) { 
  308. $css .= "$wrapper_class, $wrapper_class li, $wrapper_class li li{"; 
  309. $css .= "border-color:". $switcher_data[ 'color_scheme' ][ 'border_normal' ] ." ;"; 
  310. $css .= "}"; 
  311.  
  312. if ( $switcher_data[ 'color_scheme' ][ 'font_other_normal' ] || $switcher_data[ 'color_scheme' ][ 'background_other_normal' ] ) { 
  313. $css .= "$wrapper_class li>a {"; 
  314. $css .= $switcher_data[ 'color_scheme' ][ 'font_other_normal' ] ? "color:". $switcher_data[ 'color_scheme' ][ 'font_other_normal' ] .";" : ''; 
  315. $css .= $switcher_data[ 'color_scheme' ][ 'background_other_normal' ] ? "background-color:". $switcher_data[ 'color_scheme' ][ 'background_other_normal' ] .";" : ''; 
  316. $css .= "}"; 
  317.  
  318. if ( $switcher_data[ 'color_scheme' ][ 'font_other_hover' ] || $switcher_data[ 'color_scheme' ][ 'background_other_hover' ] ) { 
  319. $css .= "$wrapper_class li:hover>a, $wrapper_class li:focus>a {"; 
  320. $css .= $switcher_data[ 'color_scheme' ][ 'font_other_hover' ] ? "color:". $switcher_data[ 'color_scheme' ][ 'font_other_hover' ] .";" : ''; 
  321. $css .= $switcher_data[ 'color_scheme' ][ 'background_other_hover' ] ? "background-color:". $switcher_data[ 'color_scheme' ][ 'background_other_hover' ] .";" : ''; 
  322. $css .= "}"; 
  323.  
  324. if ( $switcher_data[ 'color_scheme' ][ 'font_current_normal' ] || $switcher_data[ 'color_scheme' ][ 'background_current_normal' ] ) { 
  325. $css .= "$wrapper_class .wcml-cs-active-currency>a {"; 
  326. $css .= $switcher_data[ 'color_scheme' ][ 'font_current_normal' ] ? "color:". $switcher_data[ 'color_scheme' ][ 'font_current_normal' ] .";" : ''; 
  327. $css .= $switcher_data[ 'color_scheme' ][ 'background_current_normal' ] ? "background-color:". $switcher_data[ 'color_scheme' ][ 'background_current_normal' ] .";" : ''; 
  328. $css .= "}"; 
  329.  
  330. if ( $switcher_data[ 'color_scheme' ][ 'font_current_hover' ]|| $switcher_data[ 'color_scheme' ][ 'background_current_hover' ] ) { 
  331. $css .= "$wrapper_class .wcml-cs-active-currency:hover>a, $wrapper_class .wcml-cs-active-currency:focus>a {"; 
  332. $css .= $switcher_data[ 'color_scheme' ][ 'font_current_hover' ] ? "color:". $switcher_data[ 'color_scheme' ][ 'font_current_hover' ] .";" : ''; 
  333. $css .= $switcher_data[ 'color_scheme' ][ 'background_current_hover' ] ? "background-color:". $switcher_data[ 'color_scheme' ][ 'background_current_hover' ] .";" : ''; 
  334. $css .= "}"; 
  335.  
  336. return $css; 
  337.  
  338.  
  339. public function get_inline_style( $switcher_id, $switcher_template, $css ) { 
  340. $style_id = 'wcml-cs-inline-styles-' . $switcher_id.'-'.$switcher_template; 
  341. return '<style type="text/css" id="' . $style_id . '">' . $css . '</style>' . PHP_EOL; 
  342.  
  343. public function set_templates( $templates ) { 
  344. $this->templates = $templates; 
  345.  
  346. public function check_is_active( $template ) { 
  347. $is_active = false; 
  348.  
  349. $active_templates = $this->get_active_templates(); 
  350.  
  351. foreach( $active_templates as $template_key => $active_template ) { 
  352. if ( $template === $template_key ) { 
  353. $is_active = true; 
  354. break; 
  355.  
  356. return $is_active; 
  357.  
  358.  
  359. public function get_first_active( ) { 
  360.  
  361. return current( array_keys( $this->get_active_templates() ) ); 
  362.