/plugin-fw/yit-functions.php

  1. <?php 
  2. /** 
  3. * This file belongs to the YIT Plugin Framework. 
  4. * 
  5. * This source file is subject to the GNU GENERAL PUBLIC LICENSE (GPL 3.0) 
  6. * that is bundled with this package in the file LICENSE.txt. 
  7. * It is also available through the world-wide-web at this URL: 
  8. * http://www.gnu.org/licenses/gpl-3.0.txt 
  9. */ 
  10.  
  11. if ( ! defined ( 'ABSPATH' ) ) { 
  12. exit; 
  13. } // Exit if accessed directly 
  14.  
  15. if ( ! function_exists ( 'yit_plugin_locate_template' ) ) { 
  16. /** 
  17. * Locate the templates and return the path of the file found 
  18. * 
  19. * @param string $plugin_basename 
  20. * @param string $path 
  21. * @param array $var 
  22. * 
  23. * @return string 
  24. * @since 2.0.0 
  25. */ 
  26. function yit_plugin_locate_template ( $plugin_basename, $path, $var = NULL ) { 
  27.  
  28. $template_path = '/theme/templates/' . $path; 
  29.  
  30. $located = locate_template ( array ( 
  31. $template_path,  
  32. ) ); 
  33.  
  34. if ( ! $located ) { 
  35. $located = $plugin_basename . '/templates/' . $path; 
  36.  
  37. return $located; 
  38.  
  39.  
  40. if ( ! function_exists ( 'yit_plugin_get_template' ) ) { 
  41. /** 
  42. * Retrieve a template file. 
  43. * 
  44. * @param string $plugin_basename 
  45. * @param string $path 
  46. * @param mixed $var 
  47. * @param bool $return 
  48. * 
  49. * @return string 
  50. * @since 2.0.0 
  51. */ 
  52. function yit_plugin_get_template ( $plugin_basename, $path, $var = null, $return = false ) { 
  53.  
  54. $located = yit_plugin_locate_template ( $plugin_basename, $path, $var ); 
  55.  
  56. if ( $var && is_array ( $var ) ) { 
  57. extract ( $var ); 
  58.  
  59. if ( $return ) { 
  60. ob_start (); 
  61.  
  62. // include file located 
  63. if ( file_exists ( $located ) ) { 
  64. include ( $located ); 
  65.  
  66. if ( $return ) { 
  67. return ob_get_clean (); 
  68.  
  69. if ( ! function_exists ( 'yit_plugin_content' ) ) { 
  70. /** 
  71. * Return post content with read more link (if needed) 
  72. * 
  73. * @param string $what 
  74. * @param int|string $limit 
  75. * @param string $more_text 
  76. * @param string $split 
  77. * @param string $in_paragraph 
  78. * 
  79. * @return string 
  80. * @since 2.0.0 
  81. */ 
  82. function yit_plugin_content ( $what = 'content', $limit = 25, $more_text = '', $split = '[...]', $in_paragraph = 'true' ) { 
  83. if ( $what == 'content' ) { 
  84. $content = get_the_content ( $more_text ); 
  85. } else { 
  86. if ( $what == 'excerpt' ) { 
  87. $content = get_the_excerpt (); 
  88. } else { 
  89. $content = $what; 
  90.  
  91. if ( $limit == 0 ) { 
  92. if ( $what == 'excerpt' ) { 
  93. $content = apply_filters ( 'the_excerpt', $content ); 
  94. } else { 
  95. $content = preg_replace ( '/<img[^>]+./', '', $content ); //remove images 
  96. $content = apply_filters ( 'the_content', $content ); 
  97. $content = str_replace ( ']]>', ']]>', $content ); 
  98.  
  99. return $content; 
  100.  
  101. // remove the tag more from the content 
  102. if ( preg_match ( "/<(a)[^>]*class\s*=\s*(['\"])more-link\\2[^>]*>(.*?)<\/\\1>/", $content, $matches ) ) { 
  103.  
  104. if ( strpos ( $matches[ 0 ], '[button' ) ) { 
  105. $more_link = str_replace ( 'href="#"', 'href="' . get_permalink () . '"', do_shortcode ( $matches[ 3 ] ) ); 
  106. } else { 
  107. $more_link = $matches[ 0 ]; 
  108.  
  109. $content = str_replace ( $more_link, '', $content ); 
  110. $split = ''; 
  111.  
  112. if ( empty( $content ) ) { 
  113. return; 
  114. $content = explode ( ' ', $content ); 
  115.  
  116. if ( ! empty( $more_text ) && ! isset( $more_link ) ) { 
  117. //array_pop( $content ); 
  118. $more_link = strpos ( $more_text, '<a class="btn"' ) ? $more_text : '<a class="read-more' . apply_filters ( 'yit_simple_read_more_classes', ' ' ) . '" href="' . get_permalink () . '">' . $more_text . '</a>'; 
  119. $split = ''; 
  120. } elseif ( ! isset( $more_link ) ) { 
  121. $more_link = ''; 
  122.  
  123. // split 
  124. if ( count ( $content ) >= $limit ) { 
  125. $split_content = ''; 
  126. for ( $i = 0; $i < $limit; $i ++ ) { 
  127. $split_content .= $content[ $i ] . ' '; 
  128.  
  129. $content = $split_content . $split; 
  130. } else { 
  131. $content = implode ( " ", $content ); 
  132.  
  133. // TAGS UNCLOSED 
  134. $tags = array (); 
  135. // get all tags opened 
  136. preg_match_all ( "/(<([\w]+)[^>]*>)/", $content, $tags_opened, PREG_SET_ORDER ); 
  137. foreach ( $tags_opened as $tag ) { 
  138. $tags[] = $tag[ 2 ]; 
  139.  
  140. // get all tags closed and remove it from the tags opened.. the rest will be closed at the end of the content 
  141. preg_match_all ( "/(<\/([\w]+)[^>]*>)/", $content, $tags_closed, PREG_SET_ORDER ); 
  142. foreach ( $tags_closed as $tag ) { 
  143. unset( $tags[ array_search ( $tag[ 2 ], $tags ) ] ); 
  144.  
  145. // close the tags 
  146. if ( ! empty( $tags ) ) { 
  147. foreach ( $tags as $tag ) { 
  148. $content .= "</$tag>"; 
  149.  
  150. //$content = preg_replace( '/\[.+\]/', '', $content ); 
  151. if ( $in_paragraph == true ): $content .= $more_link; endif; 
  152. $content = preg_replace ( '/<img[^>]+./', '', $content ); //remove images 
  153. $content = apply_filters ( 'the_content', $content ); 
  154. $content = str_replace ( ']]>', ']]>', $content ); // echo str_replace( array( '<', '>' ), array( '<', '>' ), $content ); 
  155. if ( $in_paragraph == false ): $content .= $more_link; endif; 
  156.  
  157. return $content; 
  158.  
  159. if ( ! function_exists ( 'yit_plugin_string' ) ) { 
  160. /** 
  161. * Simple echo a string, with a before and after string, only if the main string is not empty. 
  162. * 
  163. * @param string $before What there is before the main string 
  164. * @param string $string The main string. If it is empty or null, the functions return null. 
  165. * @param string $after What there is after the main string 
  166. * @param bool $echo If echo or only return it 
  167. * 
  168. * @return string The complete string, if the main string is not empty or null 
  169. * @since 2.0.0 
  170. */ 
  171. function yit_plugin_string ( $before = '', $string = '', $after = '', $echo = true ) { 
  172. $html = ''; 
  173.  
  174. if ( $string != '' AND ! is_null ( $string ) ) { 
  175. $html = $before . $string . $after; 
  176.  
  177. if ( $echo ) { 
  178. echo $html; 
  179.  
  180. return $html; 
  181.  
  182. if ( ! function_exists ( 'yit_plugin_decode_title' ) ) { 
  183. /** 
  184. * Change some special characters to put easily html into a string 
  185. * 
  186. * E.G. 
  187. * string: This is [my title] with | a new line 
  188. * return: This is <span class="title-highlight">my title</span> with <br /> a new line 
  189. * 
  190. * @param string $title The string to convert 
  191. * 
  192. * @return string The html 
  193. * 
  194. * @since 1.0 
  195. */ 
  196. function yit_plugin_decode_title ( $title ) { 
  197. $replaces = apply_filters ( 'yit_title_special_characters', array () ); 
  198.  
  199. return preg_replace ( array_keys ( $replaces ), array_values ( $replaces ), $title ); 
  200.  
  201. if ( ! function_exists ( 'yit_plugin_get_attachment_id' ) ) { 
  202.  
  203. /** 
  204. * Return the ID of an attachment. 
  205. * 
  206. * @param string $url 
  207. * 
  208. * @return int 
  209. * 
  210. * @since 2.0.0 
  211. */ 
  212.  
  213. function yit_plugin_get_attachment_id ( $url ) { 
  214.  
  215. $upload_dir = wp_upload_dir (); 
  216. $dir = trailingslashit ( $upload_dir[ 'baseurl' ] ); 
  217.  
  218. if ( false === strpos ( $url, $dir ) ) { 
  219. return false; 
  220.  
  221. $file = basename ( $url ); 
  222.  
  223. $query = array ( 
  224. 'post_type' => 'attachment',  
  225. 'fields' => 'ids',  
  226. 'meta_query' => array ( 
  227. array ( 
  228. 'value' => $file,  
  229. 'compare' => 'LIKE',  
  230. ),  
  231. ),  
  232. ); 
  233.  
  234. $query[ 'meta_query' ][ 0 ][ 'key' ] = '_wp_attached_file'; 
  235. $ids = get_posts ( $query ); 
  236.  
  237. foreach ( $ids as $id ) { 
  238. $attachment_image = wp_get_attachment_image_src ( $id, 'full' ); 
  239. if ( $url == array_shift ( $attachment_image ) || $url == str_replace ( 'https://', 'http://', array_shift ( $attachment_image ) ) ) { 
  240. return $id; 
  241. $query[ 'meta_query' ][ 0 ][ 'key' ] = '_wp_attachment_metadata'; 
  242. $ids = get_posts ( $query ); 
  243.  
  244. foreach ( $ids as $id ) { 
  245.  
  246. $meta = wp_get_attachment_metadata ( $id ); 
  247. if ( ! isset( $meta[ 'sizes' ] ) ) { 
  248. continue; 
  249.  
  250. foreach ( (array)$meta[ 'sizes' ] as $size => $values ) { 
  251. if ( $values[ 'file' ] == $file && $url == str_replace ( 'https://', 'http://', array_shift ( wp_get_attachment_image_src ( $id, $size ) ) ) ) { 
  252.  
  253. return $id; 
  254.  
  255. return false; 
  256.  
  257. if ( ! function_exists ( 'yit_enqueue_script' ) ) { 
  258. /** 
  259. * Enqueues script. 
  260. * 
  261. * Registers the script if src provided (does NOT overwrite) and enqueues. 
  262. * 
  263. * @since 2.0.0 
  264. * @author Simone D'Amico <simone.damico@yithemes.com> 
  265. * @see yit_register_script() For parameter information. 
  266. */ 
  267. function yit_enqueue_script ( $handle, $src, $deps = array (), $ver = false, $in_footer = true ) { 
  268.  
  269. if ( function_exists ( 'YIT_Asset' ) && ! is_admin () ) { 
  270. $enqueue = true; 
  271. YIT_Asset ()->set ( 'script', $handle, compact ( 'src', 'deps', 'ver', 'in_footer', 'enqueue' ) ); 
  272. } else { 
  273. wp_enqueue_script ( $handle, $src, $deps, $ver, $in_footer ); 
  274.  
  275. if ( ! function_exists ( 'yit_enqueue_style' ) ) { 
  276. /** 
  277. * Enqueues style. 
  278. * 
  279. * Registers the style if src provided (does NOT overwrite) and enqueues. 
  280. * 
  281. * @since 2.0.0 
  282. * @author Simone D'Amico <simone.damico@yithemes.com> 
  283. * @see yit_register_style() For parameter information. 
  284. */ 
  285. function yit_enqueue_style ( $handle, $src, $deps = array (), $ver = false, $media = 'all' ) { 
  286.  
  287. if ( function_exists ( 'YIT_Asset' ) ) { 
  288. $enqueue = true; 
  289. $who = YIT_Asset ()->get_stylesheet_handle ( get_stylesheet_uri (), 'style' ); 
  290. $where = 'before'; 
  291.  
  292. if ( false == $who ) { 
  293. $who = ''; 
  294.  
  295. YIT_Asset ()->set ( 'style', $handle, compact ( 'src', 'deps', 'ver', 'media', 'enqueue' ), $where, $who ); 
  296. } else { 
  297. wp_enqueue_style ( $handle, $src, $deps, $ver, $media ); 
  298.  
  299. if ( ! function_exists ( 'yit_get_post_meta' ) ) { 
  300. /** 
  301. * Retrieve the value of a metabox. 
  302. * 
  303. * This function retrieve the value of a metabox attached to a post. It return either a single value or an array. 
  304. * 
  305. * @param int $id Post ID. 
  306. * @param string $meta The meta key to retrieve. 
  307. * 
  308. * @return mixed Single value or array 
  309. * @since 2.0.0 
  310. */ 
  311. function yit_get_post_meta ( $id, $meta ) { 
  312. if ( ! strpos ( $meta, '[' ) ) { 
  313. return get_post_meta ( $id, $meta, true ); 
  314.  
  315. $sub_meta = explode ( '[', $meta ); 
  316.  
  317. $meta = get_post_meta ( $id, $meta, true ); 
  318. for ( $i = 0; $i < count ( $sub_meta ); $i ++ ) { 
  319. $meta = $meta[ rtrim ( $sub_meta[ $i ], ']' ) ]; 
  320.  
  321. return $meta; 
  322.  
  323. if ( ! function_exists ( 'yit_string' ) ) { 
  324. /** 
  325. * Simple echo a string, with a before and after string, only if the main string is not empty. 
  326. * 
  327. * @param string $before What there is before the main string 
  328. * @param string $string The main string. If it is empty or null, the functions return null. 
  329. * @param string $after What there is after the main string 
  330. * @param bool $echo If echo or only return it 
  331. * 
  332. * @return string The complete string, if the main string is not empty or null 
  333. * @since 2.0.0 
  334. */ 
  335. function yit_string ( $before = '', $string = '', $after = '', $echo = true ) { 
  336. $html = ''; 
  337.  
  338. if ( $string != '' AND ! is_null ( $string ) ) { 
  339. $html = $before . $string . $after; 
  340.  
  341. if ( $echo ) { 
  342. echo $html; 
  343.  
  344. return $html; 
  345.  
  346.  
  347. if ( ! function_exists ( 'yit_pagination' ) ) { 
  348. /** 
  349. * Print pagination 
  350. * 
  351. * @param string $pages 
  352. * @param int $range 
  353. * 
  354. * @return string 
  355. * @since 2.0.0 
  356. */ 
  357. function yit_pagination ( $pages = '', $range = 10 ) { 
  358. $showitems = ( $range * 2 ) + 1; 
  359.  
  360. $paged = ( get_query_var ( 'paged' ) ) ? get_query_var ( 'paged' ) : false; 
  361. if ( $paged === false ) { 
  362. $paged = ( get_query_var ( 'page' ) ) ? get_query_var ( 'page' ) : false; 
  363. if ( $paged === false ) { 
  364. $paged = 1; 
  365.  
  366.  
  367. $html = ''; 
  368.  
  369. if ( $pages == '' ) { 
  370. global $wp_query; 
  371.  
  372. if ( isset( $wp_query->max_num_pages ) ) { 
  373. $pages = $wp_query->max_num_pages; 
  374.  
  375. if ( ! $pages ) { 
  376. $pages = 1; 
  377.  
  378. if ( 1 != $pages ) { 
  379. $html .= "<div class='general-pagination clearfix'>"; 
  380. if ( $paged > 2 ) { 
  381. $html .= sprintf ( '<a class="%s" href="%s">«</a>', 'yit_pagination_first', get_pagenum_link ( 1 ) ); 
  382. if ( $paged > 1 ) { 
  383. $html .= sprintf ( '<a class="%s" href="%s">‹</a>', 'yit_pagination_previous', get_pagenum_link ( $paged - 1 ) ); 
  384.  
  385. for ( $i = 1; $i <= $pages; $i ++ ) { 
  386. if ( 1 != $pages && ( ! ( $i >= $paged + $range + 1 || $i <= $paged - $range - 1 ) || $pages <= $showitems ) ) { 
  387. $class = ( $paged == $i ) ? " class='selected'" : ''; 
  388. $html .= "<a href='" . get_pagenum_link ( $i ) . "'$class >$i</a>"; 
  389.  
  390. if ( $paged < $pages ) { 
  391. $html .= sprintf ( '<a class="%s" href="%s">›</a>', 'yit_pagination_next', get_pagenum_link ( $paged + 1 ) ); 
  392. if ( $paged < $pages - 1 ) { 
  393. $html .= sprintf ( '<a class="%s" href="%s">»</a>', 'yit_pagination_last', get_pagenum_link ( $pages ) ); 
  394.  
  395. $html .= "</div>\n"; 
  396.  
  397. echo apply_filters ( 'yit_pagination_html', $html ); 
  398.  
  399. if ( ! function_exists ( 'yit_registered_sidebars' ) ) { 
  400. /** 
  401. * Retrieve all registered sidebars 
  402. * 
  403. * @return array 
  404. * @since 2.0.0 
  405. */ 
  406. function yit_registered_sidebars () { 
  407. global $wp_registered_sidebars; 
  408.  
  409. $return = array (); 
  410.  
  411. if ( empty( $wp_registered_sidebars ) ) { 
  412. $return = array ( '' => '' ); 
  413.  
  414. foreach ( (array)$wp_registered_sidebars as $the_ ) { 
  415. $return[ $the_[ 'name' ] ] = $the_[ 'name' ]; 
  416.  
  417. ksort ( $return ); 
  418.  
  419. return $return; 
  420.  
  421. if ( ! function_exists ( 'yit_layout_option' ) ) { 
  422. /** 
  423. * Retrieve a layout option 
  424. * 
  425. * @param $key 
  426. * @param bool $id 
  427. * @param string $type 
  428. * @param string $model 
  429. * 
  430. * @return array 
  431. * @since 2.0.0 
  432. */ 
  433. function yit_layout_option ( $key, $id = false, $type = "post", $model = "post_type" ) { 
  434.  
  435. $option = ''; 
  436.  
  437. if ( defined ( 'YIT' ) ) { 
  438. $option = YIT_Layout_Panel ()->get_option ( $key, $id, $type, $model ); 
  439. } else { 
  440. if ( ! $id && ( is_single () || is_page () ) ) { 
  441. global $post; 
  442. $id = $post->ID; 
  443. } elseif ( $id != 'all' ) { 
  444. $option = get_post_meta ( $id, $key ); 
  445.  
  446. return $option; 
  447.  
  448. if ( ! function_exists ( 'yit_curPageURL' ) ) { 
  449. /** 
  450. * Retrieve the current complete url 
  451. * 
  452. * @since 1.0 
  453. */ 
  454. function yit_curPageURL () { 
  455. $pageURL = 'http'; 
  456. if ( isset( $_SERVER[ "HTTPS" ] ) AND $_SERVER[ "HTTPS" ] == "on" ) { 
  457. $pageURL .= "s"; 
  458.  
  459. $pageURL .= "://"; 
  460.  
  461. if ( isset( $_SERVER[ "SERVER_PORT" ] ) AND $_SERVER[ "SERVER_PORT" ] != "80" ) { 
  462. $pageURL .= $_SERVER[ "SERVER_NAME" ] . ":" . $_SERVER[ "SERVER_PORT" ] . $_SERVER[ "REQUEST_URI" ]; 
  463. } else { 
  464. $pageURL .= $_SERVER[ "SERVER_NAME" ] . $_SERVER[ "REQUEST_URI" ]; 
  465.  
  466. return $pageURL; 
  467.  
  468. if ( ! function_exists ( 'yit_get_excluded_categories' ) ) { 
  469. /** 
  470. * 
  471. * Retrieve the escluded categories, set on Theme Options 
  472. * 
  473. * @param int $k 
  474. * 
  475. * @return string String with all id categories excluded, separated by a comma 
  476. * 
  477. * @since 2.0.0 
  478. */ 
  479.  
  480. function yit_get_excluded_categories ( $k = 1 ) { 
  481.  
  482. global $post; 
  483.  
  484. if ( ! isset( $post->ID ) ) { 
  485. return; 
  486.  
  487. $cf_cats = get_post_meta ( $post->ID, 'blog-cats', true ); 
  488.  
  489. if ( ! empty( $cf_cats ) ) { 
  490. return $cf_cats; 
  491.  
  492. $cats = function_exists ( 'yit_get_option' ) ? yit_get_option ( 'blog-excluded-cats' ) : ''; 
  493.  
  494.  
  495. if ( ! is_array ( $cats ) || empty( $cats ) || ! isset( $cats[ $k ] ) ) { 
  496. return; 
  497.  
  498. $cats = array_map ( 'trim', $cats[ $k ] ); 
  499.  
  500. $i = 0; 
  501. $query = ''; 
  502. foreach ( $cats as $cat ) { 
  503. $query .= ", -$cat"; 
  504.  
  505. $i ++; 
  506.  
  507. ltrim ( ', ', $query ); 
  508.  
  509. return $query; 
  510.  
  511.  
  512. if ( ! function_exists ( 'yit_add_extra_theme_headers' ) ) { 
  513. add_filter ( 'extra_theme_headers', 'yit_add_extra_theme_headers' ); 
  514.  
  515. /** 
  516. * Check the framework core version 
  517. * 
  518. * @param $headers Array 
  519. * 
  520. * @return bool 
  521. * @since 2.0.0 
  522. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  523. */ 
  524. function yit_add_extra_theme_headers ( $headers ) { 
  525. $headers[] = 'Core Framework Version'; 
  526.  
  527. return $headers; 
  528.  
  529. if ( ! function_exists ( 'yit_check_plugin_support' ) ) { 
  530. /** 
  531. * Check the framework core version 
  532. * 
  533. * @return bool 
  534. * @since 2.0.0 
  535. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  536. */ 
  537. function yit_check_plugin_support () { 
  538.  
  539. $headers[ 'core' ] = wp_get_theme ()->get ( 'Core Framework Version' ); 
  540. $headers[ 'author' ] = wp_get_theme ()->get ( 'Author' ); 
  541.  
  542. if ( ! $headers[ 'core' ] && defined ( 'YIT_CORE_VERSION' ) ) { 
  543. $headers[ 'core' ] = YIT_CORE_VERSION; 
  544.  
  545. if ( ( ! empty( $headers[ 'core' ] ) && version_compare ( $headers[ 'core' ], '2.0.0', '<=' ) ) || $headers[ 'author' ] != 'Your Inspiration Themes' ) { 
  546. return true; 
  547. } else { 
  548. return false; 
  549.  
  550. if ( ! function_exists ( 'yit_ie_version' ) ) { 
  551. /** 
  552. * Retrieve IE version. 
  553. * 
  554. * @return int|float 
  555. * @since 1.0.0 
  556. * @author Andrea Grillo <andrea.grillo@yithemes.com>, Andrea Frascaspata<andrea.frascaspata@yithemes.com> 
  557. */ 
  558. function yit_ie_version() { 
  559.  
  560. if ( ! isset( $_SERVER['HTTP_USER_AGENT'] ) ) { 
  561. return - 1; 
  562. preg_match( '/MSIE ([0-9]+\.[0-9])/', $_SERVER['HTTP_USER_AGENT'], $reg ); 
  563.  
  564. if ( ! isset( $reg[1] ) ) // IE 11 FIX 
  565. preg_match( '/rv:([0-9]+\.[0-9])/', $_SERVER['HTTP_USER_AGENT'], $reg ); 
  566. if ( ! isset( $reg[1] ) ) { 
  567. return - 1; 
  568. else { 
  569. return floatval( $reg[1] ); 
  570. else { 
  571. return floatval( $reg[1] ); 
  572.  
  573. if ( ! function_exists ( 'yit_avoid_duplicate' ) ) { 
  574. /** 
  575. * Check if something exists. If yes, add a -N to the value where N is a number. 
  576. * 
  577. * @param mixed $value 
  578. * @param array $array 
  579. * @param string $check 
  580. * 
  581. * @return mixed 
  582. * @since 2.0.0 
  583. * @author Antonino Scarf* <antonino.scarfi@yithemes.com> 
  584. */ 
  585. function yit_avoid_duplicate ( $value, $array, $check = 'value' ) { 
  586. $match = array (); 
  587.  
  588. if ( ! is_array ( $array ) ) { 
  589. return $value; 
  590.  
  591. if ( ( $check == 'value' && ! in_array ( $value, $array ) ) || ( $check == 'key' && ! isset( $array[ $value ] ) ) ) { 
  592. return $value; 
  593. } else { 
  594. if ( ! preg_match ( '/([a-z]+)-([0-9]+)/', $value, $match ) ) { 
  595. $i = 2; 
  596. } else { 
  597. $i = intval ( $match[ 2 ] ) + 1; 
  598. $value = $match[ 1 ]; 
  599.  
  600. return yit_avoid_duplicate ( $value . '-' . $i, $array, $check ); 
  601.  
  602. if ( ! function_exists ( 'yit_title_special_characters' ) ) { 
  603. /** 
  604. * The chars used in yit_decode_title() and yit_encode_title() 
  605. * 
  606. * E.G. 
  607. * string: This is [my title] with | a new line 
  608. * return: This is <span class="highlight">my title</span> with <br /> a new line 
  609. * 
  610. * @param string $title The string to convert 
  611. * 
  612. * @return string The html 
  613. * 
  614. * @since 1.0 
  615. */ 
  616. function yit_title_special_characters ( $chars ) { 
  617. return array_merge ( $chars, array ( 
  618. '/[=\[](.*?)[=\]]/' => '<span class="title-highlight">$1</span>',  
  619. '/\|/' => '<br />',  
  620. ) ); 
  621.  
  622. add_filter ( 'yit_title_special_characters', 'yit_title_special_characters' ); 
  623.  
  624. if ( ! function_exists ( 'yit_decode_title' ) ) { 
  625. /** 
  626. * Change some special characters to put easily html into a string 
  627. * 
  628. * E.G. 
  629. * string: This is [my title] with | a new line 
  630. * return: This is <span class="title-highlight">my title</span> with <br /> a new line 
  631. * 
  632. * @param string $title The string to convert 
  633. * 
  634. * @return string The html 
  635. * 
  636. * @since 1.0 
  637. */ 
  638. function yit_decode_title ( $title ) { 
  639. $replaces = apply_filters ( 'yit_title_special_characters', array () ); 
  640.  
  641. return preg_replace ( array_keys ( $replaces ), array_values ( $replaces ), $title ); 
  642.  
  643. if ( ! function_exists ( 'yit_encode_title' ) ) { 
  644. /** 
  645. * Change some special characters to put easily html into a string 
  646. * 
  647. * E.G. 
  648. * string: This is [my title] with | a new line 
  649. * return: This is <span class="title-highlight">my title</span> with <br /> a new line 
  650. * 
  651. * @param string $title The string to convert 
  652. * 
  653. * @return string The html 
  654. * 
  655. * @since 1.0 
  656. */ 
  657. function yit_encode_title ( $title ) { 
  658. $replaces = apply_filters ( 'yit_title_special_characters', array () ); 
  659.  
  660. return preg_replace ( array_values ( $replaces ), array_keys ( $replaces ), $title ); 
  661.  
  662. if ( ! function_exists ( 'yit_remove_chars_title' ) ) { 
  663. /** 
  664. * Change some special characters to put easily html into a string 
  665. * 
  666. * E.G. 
  667. * string: This is [my title] with | a new line 
  668. * return: This is <span class="title-highlight">my title</span> with <br /> a new line 
  669. * 
  670. * @param string $title The string to convert 
  671. * 
  672. * @return string The html 
  673. * 
  674. * @since 1.0 
  675. */ 
  676. function yit_remove_chars_title ( $title ) { 
  677. $replaces = apply_filters ( 'yit_title_special_characters', array () ); 
  678.  
  679. return preg_replace ( array_keys ( $replaces ), '$1', $title ); 
  680.  
  681. if ( ! function_exists ( 'is_shop_installed' ) ) { 
  682. /** 
  683. * Detect if there is a shop plugin installed 
  684. * 
  685. * @return bool 
  686. * @since 2.0.0 
  687. * @author Francesco Grasso <francesco.grasso@yithemes.com 
  688. */ 
  689. function is_shop_installed () { 
  690. global $woocommerce; 
  691. if ( isset( $woocommerce ) || defined ( 'JIGOSHOP_VERSION' ) ) { 
  692. return true; 
  693. } else { 
  694. return false; 
  695.  
  696. if ( ! function_exists ( 'yit_load_js_file' ) ) { 
  697. /** 
  698. * Load .min.js file if WP_Debug is not defined 
  699. * 
  700. * @param $filename The file name 
  701. * 
  702. * @return string The file path 
  703. * @since 2.0.0 
  704. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  705. */ 
  706. function yit_load_js_file ( $filename ) { 
  707.  
  708. if ( ! ( ( defined ( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined ( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) || isset( $_GET['yith_script_debug'] ) ) ) { 
  709. $filename = str_replace ( '.js', '.min.js', $filename ); 
  710.  
  711. return $filename; 
  712.  
  713. if ( ! function_exists ( 'yit_wpml_register_string' ) ) { 
  714. /** 
  715. * Register a string in wpml trnslation 
  716. * 
  717. * @param $contenxt context name 
  718. * @param $name string name 
  719. * @param $value value to translate 
  720. * 
  721. * @since 2.0.0 
  722. * @author Andrea Frascaspata <andrea.frascaspata@yithemes.com> 
  723. */ 
  724. function yit_wpml_register_string ( $contenxt, $name, $value ) { 
  725. // wpml string translation 
  726. do_action ( 'wpml_register_single_string', $contenxt, $name, $value ); 
  727.  
  728. if ( ! function_exists ( 'yit_wpml_string_translate' ) ) { 
  729. /** 
  730. * Get a string translation 
  731. * 
  732. * @param $contenxt context name 
  733. * @param $name string name 
  734. * @param $default_value value to translate 
  735. * 
  736. * @return string the string translated 
  737. * @since 2.0.0 
  738. * @author Andrea Frascaspata <andrea.frascaspata@yithemes.com> 
  739. */ 
  740. function yit_wpml_string_translate ( $contenxt, $name, $default_value ) { 
  741. return apply_filters ( 'wpml_translate_single_string', $default_value, $contenxt, $name ); 
  742.  
  743.  
  744. if ( ! function_exists ( 'yit_wpml_object_id' ) ) { 
  745. /** 
  746. * Get id of post translation in current language 
  747. * 
  748. * @param int $element_id 
  749. * @param string $element_type 
  750. * @param bool $return_original_if_missing 
  751. * @param null|string $ulanguage_code 
  752. * 
  753. * @return int the translation id 
  754. * @since 2.0.0 
  755. * @author Antonio La Rocca <antonio.larocca@yithemes.com> 
  756. */ 
  757. function yit_wpml_object_id ( $element_id, $element_type = 'post', $return_original_if_missing = false, $ulanguage_code = null ) { 
  758. if ( function_exists ( 'wpml_object_id_filter' ) ) { 
  759. return wpml_object_id_filter( $element_id, $element_type, $return_original_if_missing, $ulanguage_code ); 
  760. } elseif ( function_exists ( 'icl_object_id' ) ) { 
  761. return icl_object_id ( $element_id, $element_type, $return_original_if_missing, $ulanguage_code ); 
  762. } else { 
  763. return $element_id; 
  764.  
  765.  
  766.  
  767. if ( ! function_exists ( 'yith_get_formatted_price' ) ) { 
  768. /** 
  769. * Format the price with a currency symbol. 
  770. * 
  771. * @param float $price 
  772. * @param array $args (default: array()) 
  773. * 
  774. * @return string 
  775. */ 
  776. function yith_get_formatted_price ( $price, $args = array () ) { 
  777. extract ( apply_filters ( 'wc_price_args', wp_parse_args ( $args, array ( 
  778. 'ex_tax_label' => false,  
  779. 'currency' => '',  
  780. 'decimal_separator' => wc_get_price_decimal_separator (),  
  781. 'thousand_separator' => wc_get_price_thousand_separator (),  
  782. 'decimals' => wc_get_price_decimals (),  
  783. 'price_format' => get_woocommerce_price_format (),  
  784. ) ) ) ); 
  785.  
  786. $negative = $price < 0; 
  787. $price = apply_filters ( 'raw_woocommerce_price', floatval ( $negative ? $price * - 1 : $price ) ); 
  788. $price = apply_filters ( 'formatted_woocommerce_price', number_format ( $price, $decimals, $decimal_separator, $thousand_separator ), $price, $decimals, $decimal_separator, $thousand_separator ); 
  789.  
  790. if ( apply_filters ( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 ) { 
  791. $price = wc_trim_zeros ( $price ); 
  792.  
  793. $formatted_price = ( $negative ? '-' : '' ) . sprintf ( $price_format, get_woocommerce_currency_symbol ( $currency ), $price ); 
  794. $return = $formatted_price; 
  795.  
  796. return apply_filters ( 'wc_price', $return, $price, $args ); 
.