base.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. function vamtam_get_the_ID() {
  3. global $post;
  4. return (vamtam_has_woocommerce() && is_woocommerce() && ! is_singular( array( 'page', 'product' ) )) ? wc_get_page_id( 'shop' ) : (isset( $post ) ? $post->ID : null);
  5. }
  6. /**
  7. * Wrapper around get_post_meta which takes special pages into account
  8. *
  9. * @uses get_post_meta()
  10. *
  11. * @param int $post_id Post ID.
  12. * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys.
  13. * @param bool $single Whether to return a single value.
  14. * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true.
  15. */
  16. function vamtam_post_meta( $post_id, $meta = '', $single = false ) {
  17. $real_id = vamtam_get_the_ID();
  18. if ($real_id && $post_id != $real_id)
  19. $post_id = $real_id;
  20. return get_post_meta( $post_id, $meta, $single );
  21. }
  22. /**
  23. * helper function - returns second argument when the first is empty, otherwise returns the first
  24. *
  25. */
  26. function vamtam_default( $value, $default ) {
  27. if (empty( $value ))
  28. return $default;
  29. return $value;
  30. }
  31. function vamtam_get_portfolio_options() {
  32. global $post;
  33. $res = array();
  34. $res['image'] = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_id() ), 'full', true );
  35. $res['type'] = vamtam_default( get_post_meta( get_the_id(), 'portfolio_type', true ), 'image' );
  36. $res['link_target'] = '_self';
  37. // calculate some options depending on the project's type
  38. if ( $res['type'] == 'image' || $res['type'] == 'html' ) {
  39. $res['href'] = $res['image'][0];
  40. } elseif ( $res['type'] == 'video' ) {
  41. $res['href'] = get_post_meta( get_the_id(), 'vamtam-portfolio-format-video', true );
  42. if (empty( $res['href'] ))
  43. $res['href'] = $res['image'][0];
  44. } elseif ( $res['type'] == 'link' ) {
  45. $res['href'] = get_post_meta( get_the_ID(), 'vamtam-portfolio-format-link', true );
  46. $res['link_target'] = get_post_meta( get_the_ID(), '_link_target', true );
  47. $res['link_target'] = $res['link_target'] ? $res['link_target'] : '_self';
  48. } elseif ( $res['type'] == 'gallery' ) {
  49. list($res['gallery'], ) = VamtamPostFormats::get_first_gallery( get_the_content(), null, VAMTAM_THUMBNAIL_PREFIX . 'loop-4' );
  50. } elseif ( $res['type'] == 'document' ) {
  51. if ( is_single() ) {
  52. $res['href'] = $res['image'][0];
  53. } else {
  54. $res['href'] = get_permalink();
  55. }
  56. }
  57. return $res;
  58. }
  59. function vamtam_custom_js() {
  60. $custom_js = rd_vamtam_get_option( 'custom-js' );
  61. if ( ! empty( $custom_js ) ) :
  62. ?>
  63. <script><?php echo $custom_js; // xss ok ?></script>
  64. <?php
  65. endif;
  66. }
  67. add_action( 'wp_footer', 'vamtam_custom_js', 10000 );
  68. function vamtam_get_attachment_file( $src ) {
  69. $attachment_id = attachment_url_to_postid( $src );
  70. $upload_dir = wp_upload_dir();
  71. if ( $attachment_id !== false && wp_attachment_is_image( $attachment_id ) ) {
  72. $file = get_attached_file( $attachment_id );
  73. $file = preg_replace( '/^(' . preg_quote( $upload_dir['basedir'] . '/', '/' ) . ')?/', $upload_dir['basedir'] . '/', $file );
  74. return $file;
  75. }
  76. return str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], $src );
  77. }
  78. function vamtam_url_to_image( $src, $size = 'full', $attr = '' ) {
  79. $attachment_id = attachment_url_to_postid( $src );
  80. if ( $attachment_id !== false && wp_attachment_is_image( $attachment_id ) ) {
  81. echo wp_get_attachment_image( $attachment_id, $size, $attr );
  82. } else {
  83. // fallback, typically used on fresly imported demo content
  84. echo '<img src="' . esc_url( $src ) . '" />';
  85. }
  86. }
  87. function vamtam_sanitize_portfolio_item_type( $type ) {
  88. if ($type == 'gallery' || $type == 'video' || $type == 'image')
  89. return $type;
  90. return 'image';
  91. }
  92. add_filter( 'vamtam_fancy_portfolio_item_type', 'vamtam_sanitize_portfolio_item_type' );
  93. function vamtam_fix_shortcodes( $content ) {
  94. // array of custom shortcodes requiring the fix
  95. $block = join( '|', apply_filters( 'vamtam_escaped_shortcodes', array() ) );
  96. // opening tag
  97. $rep = preg_replace( "/(<p>\s*)?\[($block)(\s[^\]]+)?\](\s*<\/p>|<br \/>)?/",'[$2$3]', $content );
  98. // closing tag
  99. $rep = preg_replace( "/(?:<p>\s*)?\[\/($block)](?:\s*<\/p>|<br \/>)?/",'[/$1]', $rep );
  100. return $rep;
  101. }
  102. add_filter( 'the_content', 'vamtam_fix_shortcodes' );
  103. add_filter( 'fl_builder_before_render_shortcodes', 'vamtam_fix_shortcodes' );
  104. function vamtam_get_portfolio_terms() {
  105. $terms_slug = $terms_name = $terms_id = array();
  106. if ( class_exists( 'Jetpack_Portfolio' ) ) {
  107. $terms = get_the_terms( get_the_id(), Jetpack_Portfolio::CUSTOM_TAXONOMY_TYPE );
  108. if ( is_array( $terms ) ) {
  109. foreach ( $terms as $term ) {
  110. $terms_slug[] = preg_replace( '/[\pZ\pC]+/u', '-', $term->slug );
  111. $terms_name[] = $term->name;
  112. $terms_id[] = $term->term_id;
  113. }
  114. }
  115. }
  116. return array( $terms_slug, $terms_name, $terms_id );
  117. }
  118. function vamtam_recursive_preg_replace( $regex, $replace, $subject ) {
  119. if ( is_array( $subject ) || is_object( $subject ) ) {
  120. foreach ( $subject as &$sub ) {
  121. $sub = vamtam_recursive_preg_replace( $regex, $replace, $sub );
  122. }
  123. unset( $sub );
  124. }
  125. if ( is_string( $subject ) ) {
  126. $subject = preg_replace( $regex, $replace, $subject );
  127. }
  128. return $subject;
  129. }
  130. function vamtam_get_google_fonts_subsets() {
  131. global $vamtam_fonts;
  132. $subsets = array();
  133. foreach ( $vamtam_fonts as $font ) {
  134. if ( isset( $font['gf'] ) && $font['gf'] ) {
  135. $subsets = array_merge( $subsets, $font['subsets'] );
  136. }
  137. }
  138. sort( $subsets );
  139. return array_combine( $subsets, $subsets );
  140. }
  141. function vamtam_get_fonts_by_family() {
  142. global $vamtam_fonts, $vamtam_fonts_by_family;
  143. if ( ! isset( $vamtam_fonts_by_family ) ) {
  144. $vamtam_fonts_by_family = array();
  145. foreach ( $vamtam_fonts as $id => $font ) {
  146. $vamtam_fonts_by_family[ $font['family'] ] = $id;
  147. }
  148. }
  149. return $vamtam_fonts_by_family;
  150. }
  151. function vamtam_use_accent_preview() {
  152. return ! ( ( isset( $GLOBALS['is_IE'] ) && $GLOBALS['is_IE'] ) || ( isset( $GLOBALS['is_edge'] ) && $GLOBALS['is_edge'] ) ); // IE and Edge do not support CSS variables and do not intend to any time soon (comment added 9 June 2016)
  153. }
  154. /**
  155. * Returns an array of possible Beaver Builder layouts
  156. * @param array $options elements to be prepended to the result
  157. * @return array
  158. */
  159. function vamtam_get_beaver_layouts( $options = array(), $prefix = '' ) {
  160. $posts = get_posts( array(
  161. 'post_type' => 'fl-builder-template',
  162. 'posts_per_page' => -1,
  163. 'orderby' => 'title',
  164. 'order' => 'ASC',
  165. ) );
  166. foreach ( $posts as $post ) {
  167. $options[ $prefix . $post->post_name ] = strip_tags( $post->post_title );
  168. }
  169. return $options;
  170. }