| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- /**
- * Post-format functions
- *
- * @package vamtam/consulting
- */
- /**
- * class VamtamPostFormats
- */
- class VamtamPostFormats {
- /**
- * Returns the icon denoting the current post format
- *
- * @param string $format post format name
- * @return string icon name
- */
- public static function get_post_format_icon( $format ) {
- if ( is_sticky() ) {
- return 'pushpin';
- }
- $formats = apply_filters( 'vamtam_post_format_icons', array(
- 'aside' => 'notebook',
- 'audio' => 'music2',
- 'gallery' => 'vamtam-theme-gallery',
- 'image' => 'vamtam-theme-camera',
- 'link' => 'link',
- 'quote' => 'quotes-right2',
- 'standard' => 'pencil1',
- 'status' => 'notebook',
- 'video' => 'vamtam-theme-video',
- ) );
- if ( isset( $formats[ $format ] ) ) {
- return $formats[ $format ];
- }
- return 'vamtam-theme-pencil';
- }
- /**
- * Process the data for the current post according to its format
- *
- * @param array $post_data current post data
- * @return array current post data, possibly modified for the respective post format
- */
- public static function process( $post_data ) {
- $post_data_unchanged = $post_data;
- $process_method = 'format_' . $post_data['format'];
- if ( method_exists( __CLASS__, $process_method ) ) {
- $post_data = call_user_func( array( __CLASS__, $process_method ), $post_data );
- }
- if ( isset( $post_data['media'] ) && empty( $post_data['media'] ) ) {
- unset( $post_data['media'] );
- unset( $post_data['act_as_image'] );
- $post_data['act_as_standard'] = true;
- }
- return apply_filters( 'vamtam_post_format_process', $post_data, $post_data_unchanged );
- }
- /**
- * Get the first gallery from the post content
- *
- * @param array $post_data current post data
- * @return array current post data modified for the gallery post format
- */
- private static function format_gallery( $post_data ) {
- list( $gallery, $post_data['content'] ) = self::get_first_gallery( $post_data['content'], $post_data['p']->post_content, self::get_thumb_name( $post_data ) );
- $post_data['media'] = do_shortcode( $gallery );
- return $post_data;
- }
- /**
- * Get the post format image
- *
- * @param array $post_data current post data
- * @return array current post data modified for the image post format
- */
- private static function format_image( $post_data ) {
- $blog_query = isset( $GLOBALS['vamtam_blog_query'] ) ? $GLOBALS['vamtam_blog_query'] : $GLOBALS['wp_query'];
- if ( ! $blog_query->is_single() ) {
- VamtamOverrides::unlimited_image_sizes();
- }
- $post_data['media'] = get_the_post_thumbnail( $post_data['p']->ID, self::get_thumb_name( $post_data ) );
- if ( ! $blog_query->is_single() ) {
- VamtamOverrides::limit_image_sizes();
- }
- return $post_data;
- }
- /**
- * Standard post format
- *
- * @param array $post_data current post data
- * @return array current post data modified for the image post format
- */
- private static function format_standard( $post_data ) {
- $post_data = self::format_image( $post_data );
- $post_data['act_as_image'] = true;
- return $post_data;
- }
- /**
- * Get the post format video
- *
- * @param array $post_data current post data
- * @return array current post data modified for the video post format
- */
- private static function format_video( $post_data ) {
- $blog_query = isset( $GLOBALS['vamtam_blog_query'] ) ? $GLOBALS['vamtam_blog_query'] : $GLOBALS['wp_query'];
- if ( ! $blog_query->is_single() ) {
- VamtamOverrides::unlimited_image_sizes();
- $post_data['media'] = get_the_post_thumbnail( $post_data['p']->ID, self::get_thumb_name( $post_data ) );
- VamtamOverrides::limit_image_sizes();
- }
- if ( ! isset( $post_data['media'] ) || empty( $post_data['media'] ) ) {
- global $wp_embed;
- $post_data['media'] = do_shortcode( $wp_embed->run_shortcode( '[embed]' . get_post_meta( $post_data['p']->ID, 'vamtam-post-format-video-link', true ) . '[/embed]' ) );
- } else {
- $post_data['act_as_image'] = true;
- }
- return $post_data;
- }
- /**
- * Get the post format audio
- *
- * @param array $post_data current post data
- * @return array current post data modified for the audio post format
- */
- private static function format_audio( $post_data ) {
- global $wp_embed;
- $post_data['media'] = do_shortcode( $wp_embed->run_shortcode( '[embed]' . get_post_meta( $post_data['p']->ID, 'vamtam-post-format-audio-link', true ) . '[/embed]' ) );
- return $post_data;
- }
- /**
- * Get the post format quote
- *
- * @param array $post_data current post data
- * @return array current post data modified for the quote post format
- */
- private static function format_quote( $post_data ) {
- $quote = self::get_the_post_format_quote( $post_data['p'] );
- // Replace the existing quote in-place.
- if ( ! empty( $quote ) ) {
- $post_data['content'] = $quote;
- }
- return $post_data;
- }
- /**
- * Returns the correct thumbnail name for the current post
- *
- * @param array $post_data current post data as used in VamtamPostFormats::process( $post_data )
- * @return string thumbnail name
- */
- public static function get_thumb_name( $post_data ) {
- if ( $post_data['p']->post_type == 'jetpack-portfolio' ) {
- $columns = isset( $GLOBALS['vamtam_portfolio_column'] ) ? $GLOBALS['vamtam_portfolio_column'] : 1;
- $thumb_prefix = "loop-$columns";
- $thumb_suffix = '';
- } else {
- $blog_query = isset( $GLOBALS['vamtam_blog_query'] ) ? $GLOBALS['vamtam_blog_query'] : $GLOBALS['wp_query'];
- extract( self::post_layout_info() );
- $thumb_prefix = $blog_query->is_single() ?
- 'single' :
- (
- $news ?
- (
- ( $layout === 'grid' || $layout === 'mosaic' ) && ! has_post_format( 'gallery' ) ?
- 'normal' : 'loop'
- ) :
- 'loop'
- );
- $thumb_suffix = $news ? '-' . $columns : '-3';
- }
- return VAMTAM_THUMBNAIL_PREFIX . $thumb_prefix . $thumb_suffix;
- }
- /**
- * Post layout settings
- *
- * @return array filtered post layout settings
- */
- public static function post_layout_info() {
- global $vamtam_loop_vars;
- $result = array();
- if ( is_array( $vamtam_loop_vars ) ) {
- $result['show_content'] = vamtam_sanitize_bool( $vamtam_loop_vars['show_content'] );
- $result['show_title'] = vamtam_sanitize_bool( $vamtam_loop_vars['show_title'] );
- $result['show_media'] = vamtam_sanitize_bool( $vamtam_loop_vars['show_media'] );
- $result['news'] = vamtam_sanitize_bool( $vamtam_loop_vars['news'] );
- $result['columns'] = intval( $vamtam_loop_vars['columns'] );
- $result['layout'] = isset( $vamtam_loop_vars['layout'] ) ? $vamtam_loop_vars['layout'] : 'normal';
- } else {
- $result['show_content'] = true;
- $result['show_title'] = true;
- $result['show_media'] = true;
- $result['news'] = false;
- $result['columns'] = 1;
- $result['layout'] = 'normal';
- }
- return apply_filters( 'vamtam_post_layout_info', $result );
- }
- /**
- * Get the first [gallery] shortcode from a string
- *
- * @param string|null $content search string
- * @param string|null $original_content content to extract the gallery from
- * @param string $thumbnail_name thumbnail name for the current gallery
- * @return array gallery shortcode and filtered content string
- */
- public static function get_first_gallery( $content = null, $original_content = null, $thumbnail_name = 'thumbnail' ) {
- if ( is_null( $original_content ) )
- $original_content = $content;
- preg_match( '!\[(?:vamtam_)?gallery.*?\]!', $original_content, $matches );
- $gallery = '';
- if ( ! empty( $matches ) ) {
- $gallery = $matches[0];
- $content = trim( preg_replace( '/' . preg_quote( $matches[0], '/' ) . '/', '', $content, 1 ) );
- if ( shortcode_exists( 'vamtam_gallery' ) ) {
- if ( strpos( $gallery, 'vamtam_gallery' ) === false ) {
- $gallery = str_replace( '[gallery', '[vamtam_gallery', $gallery );
- }
- $gallery = preg_replace( '/size="[^"]*"/', '', $gallery );
- $gallery = str_replace( '[vamtam_gallery', '[vamtam_gallery size="' . $thumbnail_name . '"', $gallery );
- }
- }
- return array( $gallery, $content );
- }
- /**
- * Enable removal of first gallery in beaver-edited post
- */
- public static function block_gallery_beaver() {
- add_filter( 'fl_builder_before_render_shortcodes', array( __CLASS__, 'remove_first_gallery' ) );
- }
- /**
- * Disable removal of first gallery in beaver-edited post
- */
- public static function enable_gallery_beaver() {
- remove_filter( 'fl_builder_before_render_shortcodes', array( __CLASS__, 'remove_first_gallery' ) );
- }
- /**
- * Remove first gallery in $content
- *
- * @param string $content post content
- * @return string post content without first gallery
- */
- public static function remove_first_gallery( $content ) {
- return preg_replace( '!\[(?:vamtam_)?gallery.*?\]!', '', $content );
- }
- /**
- * Get a quote from the post content
- *
- *
- * @uses get_content_quote()
- *
- * @param object $post ( optional ) A reference to the post object, falls back to get_post().
- * @return string The quote html.
- */
- public static function get_the_post_format_quote( &$post = null ) {
- if ( empty( $post ) )
- $post = get_post();
- if ( empty( $post ) )
- return '';
- $quote = $post->post_content;
- $source = '';
- $author = get_post_meta( $post->ID, 'vamtam-post-format-quote-author', true );
- $link = get_post_meta( $post->ID, 'vamtam-post-format-quote-link', true );
- if ( ! empty( $author ) ) {
- VamtamOverrides::unlimited_image_sizes();
- $thumb = get_the_post_thumbnail( $post->ID, 'thumbnail' );
- VamtamOverrides::limit_image_sizes();
- $author = empty( $thumb ) ? $author : "$thumb <span class='quote-author'>$author</span>";
- $source = empty( $link ) ?
- $author :
- sprintf( '<a href="%s">%s</a>', esc_url( $link ), $author );
- }
- $quote = preg_replace( '!</?\s*blockquote.*?>!i', '', $quote );
- $content = vamtam_fix_shortcodes( wpautop( $quote ) );
- $content = VamtamTemplates::remove_outer_columns( $content );
- $cite = "<div class='cite'>$source</div>";
- return "<blockquote class='clearfix'><div class='quote-text'>" . do_shortcode( $content ) . "</div>$cite</blockquote>";
- }
- }
|