post-formats.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * Post-format functions
  4. *
  5. * @package vamtam/consulting
  6. */
  7. /**
  8. * class VamtamPostFormats
  9. */
  10. class VamtamPostFormats {
  11. /**
  12. * Returns the icon denoting the current post format
  13. *
  14. * @param string $format post format name
  15. * @return string icon name
  16. */
  17. public static function get_post_format_icon( $format ) {
  18. if ( is_sticky() ) {
  19. return 'pushpin';
  20. }
  21. $formats = apply_filters( 'vamtam_post_format_icons', array(
  22. 'aside' => 'notebook',
  23. 'audio' => 'music2',
  24. 'gallery' => 'vamtam-theme-gallery',
  25. 'image' => 'vamtam-theme-camera',
  26. 'link' => 'link',
  27. 'quote' => 'quotes-right2',
  28. 'standard' => 'pencil1',
  29. 'status' => 'notebook',
  30. 'video' => 'vamtam-theme-video',
  31. ) );
  32. if ( isset( $formats[ $format ] ) ) {
  33. return $formats[ $format ];
  34. }
  35. return 'vamtam-theme-pencil';
  36. }
  37. /**
  38. * Process the data for the current post according to its format
  39. *
  40. * @param array $post_data current post data
  41. * @return array current post data, possibly modified for the respective post format
  42. */
  43. public static function process( $post_data ) {
  44. $post_data_unchanged = $post_data;
  45. $process_method = 'format_' . $post_data['format'];
  46. if ( method_exists( __CLASS__, $process_method ) ) {
  47. $post_data = call_user_func( array( __CLASS__, $process_method ), $post_data );
  48. }
  49. if ( isset( $post_data['media'] ) && empty( $post_data['media'] ) ) {
  50. unset( $post_data['media'] );
  51. unset( $post_data['act_as_image'] );
  52. $post_data['act_as_standard'] = true;
  53. }
  54. return apply_filters( 'vamtam_post_format_process', $post_data, $post_data_unchanged );
  55. }
  56. /**
  57. * Get the first gallery from the post content
  58. *
  59. * @param array $post_data current post data
  60. * @return array current post data modified for the gallery post format
  61. */
  62. private static function format_gallery( $post_data ) {
  63. list( $gallery, $post_data['content'] ) = self::get_first_gallery( $post_data['content'], $post_data['p']->post_content, self::get_thumb_name( $post_data ) );
  64. $post_data['media'] = do_shortcode( $gallery );
  65. return $post_data;
  66. }
  67. /**
  68. * Get the post format image
  69. *
  70. * @param array $post_data current post data
  71. * @return array current post data modified for the image post format
  72. */
  73. private static function format_image( $post_data ) {
  74. $blog_query = isset( $GLOBALS['vamtam_blog_query'] ) ? $GLOBALS['vamtam_blog_query'] : $GLOBALS['wp_query'];
  75. if ( ! $blog_query->is_single() ) {
  76. VamtamOverrides::unlimited_image_sizes();
  77. }
  78. $post_data['media'] = get_the_post_thumbnail( $post_data['p']->ID, self::get_thumb_name( $post_data ) );
  79. if ( ! $blog_query->is_single() ) {
  80. VamtamOverrides::limit_image_sizes();
  81. }
  82. return $post_data;
  83. }
  84. /**
  85. * Standard post format
  86. *
  87. * @param array $post_data current post data
  88. * @return array current post data modified for the image post format
  89. */
  90. private static function format_standard( $post_data ) {
  91. $post_data = self::format_image( $post_data );
  92. $post_data['act_as_image'] = true;
  93. return $post_data;
  94. }
  95. /**
  96. * Get the post format video
  97. *
  98. * @param array $post_data current post data
  99. * @return array current post data modified for the video post format
  100. */
  101. private static function format_video( $post_data ) {
  102. $blog_query = isset( $GLOBALS['vamtam_blog_query'] ) ? $GLOBALS['vamtam_blog_query'] : $GLOBALS['wp_query'];
  103. if ( ! $blog_query->is_single() ) {
  104. VamtamOverrides::unlimited_image_sizes();
  105. $post_data['media'] = get_the_post_thumbnail( $post_data['p']->ID, self::get_thumb_name( $post_data ) );
  106. VamtamOverrides::limit_image_sizes();
  107. }
  108. if ( ! isset( $post_data['media'] ) || empty( $post_data['media'] ) ) {
  109. global $wp_embed;
  110. $post_data['media'] = do_shortcode( $wp_embed->run_shortcode( '[embed]' . get_post_meta( $post_data['p']->ID, 'vamtam-post-format-video-link', true ) . '[/embed]' ) );
  111. } else {
  112. $post_data['act_as_image'] = true;
  113. }
  114. return $post_data;
  115. }
  116. /**
  117. * Get the post format audio
  118. *
  119. * @param array $post_data current post data
  120. * @return array current post data modified for the audio post format
  121. */
  122. private static function format_audio( $post_data ) {
  123. global $wp_embed;
  124. $post_data['media'] = do_shortcode( $wp_embed->run_shortcode( '[embed]' . get_post_meta( $post_data['p']->ID, 'vamtam-post-format-audio-link', true ) . '[/embed]' ) );
  125. return $post_data;
  126. }
  127. /**
  128. * Get the post format quote
  129. *
  130. * @param array $post_data current post data
  131. * @return array current post data modified for the quote post format
  132. */
  133. private static function format_quote( $post_data ) {
  134. $quote = self::get_the_post_format_quote( $post_data['p'] );
  135. // Replace the existing quote in-place.
  136. if ( ! empty( $quote ) ) {
  137. $post_data['content'] = $quote;
  138. }
  139. return $post_data;
  140. }
  141. /**
  142. * Returns the correct thumbnail name for the current post
  143. *
  144. * @param array $post_data current post data as used in VamtamPostFormats::process( $post_data )
  145. * @return string thumbnail name
  146. */
  147. public static function get_thumb_name( $post_data ) {
  148. if ( $post_data['p']->post_type == 'jetpack-portfolio' ) {
  149. $columns = isset( $GLOBALS['vamtam_portfolio_column'] ) ? $GLOBALS['vamtam_portfolio_column'] : 1;
  150. $thumb_prefix = "loop-$columns";
  151. $thumb_suffix = '';
  152. } else {
  153. $blog_query = isset( $GLOBALS['vamtam_blog_query'] ) ? $GLOBALS['vamtam_blog_query'] : $GLOBALS['wp_query'];
  154. extract( self::post_layout_info() );
  155. $thumb_prefix = $blog_query->is_single() ?
  156. 'single' :
  157. (
  158. $news ?
  159. (
  160. ( $layout === 'grid' || $layout === 'mosaic' ) && ! has_post_format( 'gallery' ) ?
  161. 'normal' : 'loop'
  162. ) :
  163. 'loop'
  164. );
  165. $thumb_suffix = $news ? '-' . $columns : '-3';
  166. }
  167. return VAMTAM_THUMBNAIL_PREFIX . $thumb_prefix . $thumb_suffix;
  168. }
  169. /**
  170. * Post layout settings
  171. *
  172. * @return array filtered post layout settings
  173. */
  174. public static function post_layout_info() {
  175. global $vamtam_loop_vars;
  176. $result = array();
  177. if ( is_array( $vamtam_loop_vars ) ) {
  178. $result['show_content'] = vamtam_sanitize_bool( $vamtam_loop_vars['show_content'] );
  179. $result['show_title'] = vamtam_sanitize_bool( $vamtam_loop_vars['show_title'] );
  180. $result['show_media'] = vamtam_sanitize_bool( $vamtam_loop_vars['show_media'] );
  181. $result['news'] = vamtam_sanitize_bool( $vamtam_loop_vars['news'] );
  182. $result['columns'] = intval( $vamtam_loop_vars['columns'] );
  183. $result['layout'] = isset( $vamtam_loop_vars['layout'] ) ? $vamtam_loop_vars['layout'] : 'normal';
  184. } else {
  185. $result['show_content'] = true;
  186. $result['show_title'] = true;
  187. $result['show_media'] = true;
  188. $result['news'] = false;
  189. $result['columns'] = 1;
  190. $result['layout'] = 'normal';
  191. }
  192. return apply_filters( 'vamtam_post_layout_info', $result );
  193. }
  194. /**
  195. * Get the first [gallery] shortcode from a string
  196. *
  197. * @param string|null $content search string
  198. * @param string|null $original_content content to extract the gallery from
  199. * @param string $thumbnail_name thumbnail name for the current gallery
  200. * @return array gallery shortcode and filtered content string
  201. */
  202. public static function get_first_gallery( $content = null, $original_content = null, $thumbnail_name = 'thumbnail' ) {
  203. if ( is_null( $original_content ) )
  204. $original_content = $content;
  205. preg_match( '!\[(?:vamtam_)?gallery.*?\]!', $original_content, $matches );
  206. $gallery = '';
  207. if ( ! empty( $matches ) ) {
  208. $gallery = $matches[0];
  209. $content = trim( preg_replace( '/' . preg_quote( $matches[0], '/' ) . '/', '', $content, 1 ) );
  210. if ( shortcode_exists( 'vamtam_gallery' ) ) {
  211. if ( strpos( $gallery, 'vamtam_gallery' ) === false ) {
  212. $gallery = str_replace( '[gallery', '[vamtam_gallery', $gallery );
  213. }
  214. $gallery = preg_replace( '/size="[^"]*"/', '', $gallery );
  215. $gallery = str_replace( '[vamtam_gallery', '[vamtam_gallery size="' . $thumbnail_name . '"', $gallery );
  216. }
  217. }
  218. return array( $gallery, $content );
  219. }
  220. /**
  221. * Enable removal of first gallery in beaver-edited post
  222. */
  223. public static function block_gallery_beaver() {
  224. add_filter( 'fl_builder_before_render_shortcodes', array( __CLASS__, 'remove_first_gallery' ) );
  225. }
  226. /**
  227. * Disable removal of first gallery in beaver-edited post
  228. */
  229. public static function enable_gallery_beaver() {
  230. remove_filter( 'fl_builder_before_render_shortcodes', array( __CLASS__, 'remove_first_gallery' ) );
  231. }
  232. /**
  233. * Remove first gallery in $content
  234. *
  235. * @param string $content post content
  236. * @return string post content without first gallery
  237. */
  238. public static function remove_first_gallery( $content ) {
  239. return preg_replace( '!\[(?:vamtam_)?gallery.*?\]!', '', $content );
  240. }
  241. /**
  242. * Get a quote from the post content
  243. *
  244. *
  245. * @uses get_content_quote()
  246. *
  247. * @param object $post ( optional ) A reference to the post object, falls back to get_post().
  248. * @return string The quote html.
  249. */
  250. public static function get_the_post_format_quote( &$post = null ) {
  251. if ( empty( $post ) )
  252. $post = get_post();
  253. if ( empty( $post ) )
  254. return '';
  255. $quote = $post->post_content;
  256. $source = '';
  257. $author = get_post_meta( $post->ID, 'vamtam-post-format-quote-author', true );
  258. $link = get_post_meta( $post->ID, 'vamtam-post-format-quote-link', true );
  259. if ( ! empty( $author ) ) {
  260. VamtamOverrides::unlimited_image_sizes();
  261. $thumb = get_the_post_thumbnail( $post->ID, 'thumbnail' );
  262. VamtamOverrides::limit_image_sizes();
  263. $author = empty( $thumb ) ? $author : "$thumb <span class='quote-author'>$author</span>";
  264. $source = empty( $link ) ?
  265. $author :
  266. sprintf( '<a href="%s">%s</a>', esc_url( $link ), $author );
  267. }
  268. $quote = preg_replace( '!</?\s*blockquote.*?>!i', '', $quote );
  269. $content = vamtam_fix_shortcodes( wpautop( $quote ) );
  270. $content = VamtamTemplates::remove_outer_columns( $content );
  271. $cite = "<div class='cite'>$source</div>";
  272. return "<blockquote class='clearfix'><div class='quote-text'>" . do_shortcode( $content ) . "</div>$cite</blockquote>";
  273. }
  274. }