PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/yith-woocommerce-zoom-magnifier/plugin-fw/yit-functions.php

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