'post',
'tag' => $tag,
'posts_per_page' => 3,
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'post_status' => 'publish',
), $tag );
self::$posts = new WP_Query( $args );
}
// Output
return self::$posts;
} // /get_posts
/**
* Sets featured post fallback media.
*
* @since 1.0.0
*
* @param string $context
*
* @return void
*/
public static function set_image( $context = '' ) {
// Requirements check
if ( 'featured' !== $context ) {
return;
}
// Processing
if ( doing_action( 'tha_content_while_before' ) ) {
add_filter( 'michelle/entry/media/get_image_size', __CLASS__ . '::get_image_size' );
add_filter( 'pre/michelle/entry/media/display', __CLASS__ . '::get_media_fallback' );
} else {
remove_filter( 'pre/michelle/entry/media/display', __CLASS__ . '::get_media_fallback' );
remove_filter( 'michelle/entry/media/get_image_size', __CLASS__ . '::get_image_size' );
}
} // /set_image
/**
* Gets featured post fallback media image size.
*
* @since 1.0.0
*
* @return string
*/
public static function get_image_size(): string {
// Output
/**
* Filters featured posts image size.
*
* @since 1.0.0
*
* @param string $image_size
*/
return (string) apply_filters( 'michelle/loop/featured_posts/get_image_size', 'large' );
} // /get_image_size
/**
* Gets featured post fallback media.
*
* @since 1.0.0
*
* @param mixed $pre
*
* @return mixed
*/
public static function get_media_fallback( $pre ) {
// Requirements check
if ( has_post_thumbnail() ) {
return $pre;
}
// Variables
$image_id = absint( Mod::get( 'featured_posts_image' ) );
// Processing
if ( ! empty( $image_id ) ) {
return
'
';
}
// Output
return $pre;
} // /get_media_fallback
/**
* Theme options.
*
* @since 1.0.0
*
* @param array $options
*
* @return array
*/
public static function options( array $options ): array {
// Variables
$tags = get_tags( array(
'hide_empty' => false,
'fields' => 'slugs',
) );
if ( is_wp_error( $tags ) ) {
$tags = array();
}
// Processing
$options[ 400 . 'posts' . 200 ] = array(
'type' => 'html',
'content' =>
''
. esc_html__( 'Featured posts', 'michelle' )
. '
',
);
$options[ 400 . 'posts' . 210 ] = array(
'type' => 'text',
'id' => 'featured_posts_tag',
'label' => esc_html__( 'Featured posts tag slug', 'michelle' ),
'description' => esc_html__( '3 latest posts assigned to this tag will be displayed as featured posts on blog page.', 'michelle' ) . ' (' . esc_html__( 'Open tags manager in a new window now →', 'michelle' ) . ')',
'default' => '',
'datalist' => $tags,
'sanitize_callback' => 'esc_attr',
'input_attrs' => array(
'placeholder' => esc_attr_x( 'featured', 'Form field placeholder. URL slug, no diacritic.', 'michelle' ),
),
);
$options[ 400 . 'posts' . 220 ] = array(
'type' => 'checkbox',
'id' => 'featured_posts_remove_from_blog',
'label' => esc_html__( 'Remove from blog posts', 'michelle' ),
'description' => esc_html__( 'Makes sure the featured posts will be removed from posts list on blog page.', 'michelle' ),
'default' => false,
);
$options[ 400 . 'posts' . 230 ] = array(
'type' => 'image',
'id' => 'featured_posts_image',
'label' => esc_html__( 'Fallback image', 'michelle' ),
'description' => esc_html__( 'This image will be displayed when post has no featured image set.', 'michelle' ),
'return' => 'id',
'default' => 0,
);
// Output
return $options;
} // /options
/**
* Setup customizer partial refresh pointers.
*
* @since 1.0.0
*
* @param WP_Customize_Manager $wp_customize
*
* @return void
*/
public static function option_pointers( WP_Customize_Manager $wp_customize ) {
// Processing
$wp_customize->selective_refresh->add_partial( 'featured_posts_tag', array(
'selector' => '.featured-posts-section',
) );
} // /option_pointers
/**
* Remove featured posts from blog posts list.
*
* This has to be enabled in theme options.
*
* @since 1.0.0
*
* @param WP_Query $query
*
* @return void
*/
public static function pre_get_posts( WP_Query $query ) {
// Requirements check
if (
! $query->is_home()
|| ! $query->is_main_query()
|| empty( Mod::get( 'featured_posts_remove_from_blog' ) )
) {
return;
}
// Variables
$featured = self::get_posts();
// Processing
if ( ! empty( $featured ) ) {
$query->set( 'post__not_in', wp_list_pluck( $featured->posts, 'ID' ) );
}
} // /pre_get_posts
}