overrides.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * Various filters and actions configuring some of the shortcodes
  4. *
  5. * @package vamtam/consulting
  6. */
  7. /**
  8. * class VamtamOverrides
  9. */
  10. class VamtamOverrides {
  11. /**
  12. * add filters
  13. */
  14. public static function filters() {
  15. add_filter( 'excerpt_length', array( __CLASS__, 'excerpt_length' ) );
  16. add_filter( 'excerpt_more', array( __CLASS__, 'excerpt_more' ) );
  17. add_filter( 'wp_trim_excerpt', array( __CLASS__, 'prepare_excerpt' ), 1, 2 );
  18. add_filter( 'wp_title', array( __CLASS__, 'wp_title' ) );
  19. wp_embed_register_handler( 'vamtam-swf', '#https?://[^\s]+?.swf$#i', array( __CLASS__, 'embed_handler_swf' ) );
  20. add_filter( 'pre_option_page_for_posts', '__return_zero' );
  21. add_filter( 'oembed_dataparse', array( __CLASS__, 'oembed_dataparse' ), 90, 3 );
  22. add_filter( 'nav_menu_css_class', array( __CLASS__, 'nav_menu_css_class' ), 10, 2 );
  23. add_filter( 'nav_menu_css_class', array( __CLASS__, 'nav_menu_css_class' ), 11, 2 );
  24. add_filter( 'megamenu_nav_menu_css_class', array( __CLASS__, 'nav_menu_css_class' ), 10, 2 );
  25. add_filter( 'megamenu_nav_menu_css_class', array( __CLASS__, 'nav_menu_css_class' ), 11, 2 );
  26. add_action( 'vamtam_body', array( __CLASS__, 'vamtam_splash_screen' ) );
  27. add_filter( 'wpcf7_form_elements', array( __CLASS__, 'shortcodes_in_cf7' ) );
  28. add_filter( 'pre_option_vamtam_header-layout', array( __CLASS__, 'header_layout' ) );
  29. add_action( 'loop_start', array( __CLASS__, 'jetpack_remove_share' ) );
  30. add_action( 'wp_footer', array( __CLASS__, 'post_siblings' ) );
  31. add_filter( 'vamtam_customizer_get_options_vamtam_theme', array( __CLASS__, 'customizer_get_options' ) );
  32. add_filter( 'widget_title', array( __CLASS__, 'widget_title' ), 10, 3 );
  33. add_filter( 'post_thumbnail_html', array( __CLASS__, 'post_thumbnail_html' ), 20, 5 );
  34. add_filter( 'vamtam_maybe_lazyload', array( __CLASS__, 'maybe_lazyload' ), 10, 4 );
  35. add_filter( 'jetpack_tiled_gallery_partial', array( __CLASS__, 'jetpack_tiled_gallery_partial' ), 10, 3 );
  36. add_filter( 'option_jetpack_active_modules', array( __CLASS__, 'jetpack_active_modules' ) );
  37. }
  38. /**
  39. * We have alternative implementations of some Jetpack modules which are better integrated with the theme
  40. */
  41. public static function jetpack_active_modules( $modules ) {
  42. return array_diff( $modules, [
  43. 'infinite-scroll',
  44. 'lazy-images',
  45. ] );
  46. }
  47. /**
  48. * Override Jetpack's tiled gallery template so that it supports our lazyload implementation
  49. */
  50. public static function jetpack_tiled_gallery_partial( $path, $name, $context ) {
  51. if ( $name === 'item' ) {
  52. return locate_template( 'templates/jetpack-tiled-gallery-item.php' );
  53. }
  54. return $path;
  55. }
  56. public static function post_thumbnail_html( $html, $post_ID, $post_thumbnail_id, $size, $attr ) {
  57. return self::maybe_lazyload( $html, $post_thumbnail_id, $size );
  58. }
  59. /**
  60. * [maybe_lazyload description]
  61. * @param string $html
  62. * @param int $image_id attachment id
  63. * @param string|array $size thumbnail name or [width, height] array
  64. * @return string
  65. */
  66. public static function maybe_lazyload( $html, $image_id, $size, $with_wrapper = true ) {
  67. if ( $html === '' || $GLOBALS['is_IE'] ) {
  68. return $html;
  69. }
  70. if ( is_array( $size ) ) {
  71. list( $width, $height ) = $size;
  72. } else {
  73. list( , $width, $height ) = wp_get_attachment_image_src( $image_id, $size );
  74. }
  75. $svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ' . esc_attr( $width ) .' ' . esc_attr( $height ) .'" height="' . esc_attr( $height ) . 'px" width="' . esc_attr( $width ) . 'px"/>';
  76. $wrapped = '';
  77. if ( $with_wrapper ) {
  78. $wrapped .= '<div class="vamtam-responsive-wrapper">';
  79. }
  80. if ( apply_filters( 'vamtam_enable_image_lazyload', true ) ) {
  81. $new_srcset = 'srcset="data:image/svg+xml;utf8,' . esc_attr( str_replace( ' ', '%20', $svg ) ) . '"';
  82. $wrapped .= str_replace( 'srcset=', $new_srcset . ' data-srcset=', $html, $replacements );
  83. if ( $replacements === 0 ) {
  84. $wrapped = preg_replace( '/src="([^"]+)"/', 'src="$1" data-srcset="$1" ' . $new_srcset, $wrapped, 1 );
  85. }
  86. } else {
  87. $wrapped .= $html;
  88. }
  89. if ( $with_wrapper ) {
  90. $wrapped .= '</div>';
  91. }
  92. return $wrapped;
  93. }
  94. public static function widget_title( $title = '', $instance = null, $id_base = null ) {
  95. if ( ! is_null( $instance ) && $id_base === 'recent-posts' && empty( $instance['title'] ) ) {
  96. return '';
  97. }
  98. return $title;
  99. }
  100. public static function unlimited_image_sizes() {
  101. add_filter( 'wp_calculate_image_sizes', array( __CLASS__, 'wp_calculate_image_sizes' ), 10, 5 );
  102. }
  103. public static function limit_image_sizes() {
  104. remove_filter( 'wp_calculate_image_sizes', array( __CLASS__, 'wp_calculate_image_sizes' ), 10, 5 );
  105. }
  106. public static function wp_calculate_image_sizes( $sizes, $size, $image_src, $image_meta, $attachment_id ) {
  107. return '(min-width: 900px) 50vw, 100vw';
  108. }
  109. public static function post_siblings() {
  110. if ( VamtamTemplates::has_post_siblings_buttons() ) {
  111. get_template_part( 'templates/post-siblings-links' );
  112. }
  113. }
  114. public static function prepare_excerpt( $text = '', $raw_excerpt ) {
  115. if ( '' == $raw_excerpt ) {
  116. $text = get_the_content( '' );
  117. $text = VamtamTemplates::remove_outer_columns( $text );
  118. $text = strip_shortcodes( $text );
  119. /** This filter is documented in wp-includes/post-template.php */
  120. $text = apply_filters( 'the_content', $text );
  121. $text = str_replace( ']]>', ']]&gt;', $text );
  122. $excerpt_length = apply_filters( 'excerpt_length', 40 );
  123. $excerpt_more = apply_filters( 'excerpt_more', ' [&hellip;]' );
  124. $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
  125. }
  126. return $text;
  127. }
  128. public static function jetpack_remove_share() {
  129. remove_filter( 'the_content', 'sharing_display',19 );
  130. remove_filter( 'the_excerpt', 'sharing_display',19 );
  131. if ( class_exists( 'Jetpack_Likes' ) ) {
  132. remove_filter( 'the_content', array( Jetpack_Likes::init(), 'post_likes' ), 30, 1 );
  133. }
  134. }
  135. public static function header_layout( $layout ) {
  136. $logo_type = rd_vamtam_get_option( 'header-logo-type' );
  137. if ( $logo_type === 'names' ) {
  138. return 'standard';
  139. }
  140. return $layout;
  141. }
  142. public static function wp_title( $title ) {
  143. if ( empty( $title ) && ( is_home() || is_front_page() ) ) {
  144. $description = get_bloginfo( 'description' );
  145. return get_bloginfo( 'name' ) . ( ! empty( $description ) ? ' | ' . $description : '' );
  146. }
  147. return $title;
  148. }
  149. /**
  150. * enable any shortcodes in CF7
  151. * @param string $form original html
  152. * @return string parsed with do_shortcode
  153. */
  154. public static function shortcodes_in_cf7( $form ) {
  155. return do_shortcode( vamtam_fix_shortcodes( $form ) );
  156. }
  157. /**
  158. * Show a splash screen on some pages
  159. */
  160. public static function vamtam_splash_screen() {
  161. $local = vamtam_post_meta( null, 'show-splash-screen-local', true );
  162. $enabled = $local === 'default' ? rd_vamtam_get_option( 'show-splash-screen' ) : vamtam_sanitize_bool( $local );
  163. $style = '';
  164. if ( ! $enabled ) {
  165. if ( ! is_customize_preview() ) {
  166. return;
  167. }
  168. $style = 'style="display: none"'; // we need the html for the customizer preview, but there is no need to show it on the first load
  169. }
  170. $logo = rd_vamtam_get_option( 'splash-screen-logo' );
  171. echo '<div class="vamtam-splash-screen" ' . $style . '>'; // xss ok
  172. echo '<div class="vamtam-splash-screen-progress-wrapper">';
  173. if ( ! empty( $logo ) ) {
  174. vamtam_url_to_image( $logo, 'full' );
  175. }
  176. echo '<div class="vamtam-splash-screen-progress"></div>
  177. </div>
  178. </div>';
  179. echo '<noscript><style>.vamtam-splash-screen { display: none !important }</style></noscript>';
  180. wp_enqueue_script( 'vamtam-splash-screen' );
  181. }
  182. /**
  183. * Remove unnecessary menu item classes
  184. *
  185. * @param array $classes current menu item classes
  186. * @param object $item menu item
  187. * @param object $args menu item args
  188. * @return array filtered classes
  189. */
  190. public static function nav_menu_css_class( $classes, $item ) {
  191. if ( isset( $item->url ) && strpos( $item->url, '#' ) !== false && ( $key = array_search( 'mega-current-menu-item', $classes ) ) !== false ) {
  192. unset( $classes[ $key ] );
  193. $classes[] = 'maybe-current-menu-item';
  194. $GLOBALS['vamtam_menu_had_hash'] = true;
  195. }
  196. if ( isset( $GLOBALS['vamtam_menu_had_hash'] ) && $GLOBALS['vamtam_menu_had_hash'] ) {
  197. $classes = array_diff( $classes, array( 'mega-current-menu-item', 'mega-current-menu-ancestor', 'mega-current-menu-parent' ) );
  198. }
  199. return $classes;
  200. }
  201. /**
  202. * Wrap oEmbeds in .vamtam-video-frame
  203. *
  204. * @param string $output original oembed output
  205. * @param object $data data from the oEmbed provider
  206. * @param string $url original embed url
  207. * @return string $output wrapped in additional html
  208. */
  209. public static function oembed_dataparse( $output, $data, $url ) {
  210. if ( $data->type == 'video' )
  211. return '<div class="vamtam-video-frame">' . $output . '</div>';
  212. return $output;
  213. }
  214. /**
  215. * Implements .swf oEmbeds
  216. *
  217. * @param array $matches preg_match matches
  218. * @param array $attr embed attributes
  219. * @param string $url embed url
  220. * @param array $rawattr raw attributes
  221. * @return string output html
  222. */
  223. public static function embed_handler_swf( $matches, $attr, $url, $rawattr ) {
  224. $embed = sprintf(
  225. '
  226. <div class="vamtam-video-frame">
  227. <object width="%2$s" height="%3$s" type="application/x-shockwave-flash" data="%1$s">
  228. <param name="movie" value="%1$s" />
  229. <param name="allowFullScreen" value="true" />
  230. <param name="allowscriptaccess" value="always" />
  231. <param name="wmode" value="transparent" />
  232. <embed src="%1$s" type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" allowfullscreen="true" width="%2$s" height="%3$s" />
  233. </object>
  234. </div>',
  235. esc_attr( $matches[0] ),
  236. esc_attr( $attr['width'] ),
  237. esc_attr( $attr['height'] )
  238. );
  239. return apply_filters( 'vamtam_embed_swf', $embed, $matches, $attr, $url, $rawattr );
  240. }
  241. /**
  242. * Sets the excerpt length
  243. *
  244. * @param int $length original length
  245. * @return int excerpt length
  246. */
  247. public static function excerpt_length( $length ) {
  248. global $vamtam_loop_vars;
  249. if ( isset( $vamtam_loop_vars ) && $vamtam_loop_vars['news'] )
  250. return 15;
  251. return $length;
  252. }
  253. /**
  254. * Sets the excerpt ending
  255. *
  256. * @param string $more original ending
  257. * @return string excerpt ending
  258. */
  259. public static function excerpt_more( $more ) {
  260. return '...';
  261. }
  262. }