[
'label' => esc_html__( 'List View 1', 'bayleaf' ),
'support' => [ 'thumbnailtitle', 'excerpt', 'media', 'ialign' ],
],
'grid-view1' => [
'label' => esc_html__( 'Grid View 1', 'bayleaf' ),
'support' => [ 'thumbnailtitle', 'media', 'multicol', 'imgcrop' ],
],
'grid-view2' => [
'label' => esc_html__( 'Grid View 2', 'bayleaf' ),
'support' => [ 'thumbnailtitle', 'category', 'media' ],
],
'grid-view3' => [
'label' => esc_html__( 'Grid View 3', 'bayleaf' ),
'support' => [ 'thumbnailtitle', 'category', 'media', 'multicol', 'imgcrop' ],
],
'slider1' => [
'label' => esc_html__( 'Slider 1', 'bayleaf' ),
'support' => [ 'thumbnailtitle', 'category', 'media' ],
],
'slider2' => [
'label' => esc_html__( 'Slider 2', 'bayleaf' ),
'support' => [ 'thumbnailtitle', 'media', 'excerpt' ],
],
];
}
/**
* Add classes to widget's main wrapper.
*
* @param str $classes Comma separated widget classes.
* @param array $widget_data {
* Current widget's data to generate customized output.
* @type str $widget_id Widget ID.
* @type int $widget_pos Widget position in widgetlayer widget-area.
* @type array $instance Current widget instance settings.
* @type str $id_base Widget ID base.
* }
* @return array Widget classes.
*/
public function widget_classes( $classes, $widget_data ) {
$instance = $widget_data[2];
if ( isset( $instance['styles'] ) && false !== strpos( $instance['styles'], 'grid' ) ) {
$classes[] = 'posts-grid';
}
return $classes;
}
/**
* Register widget display posts entry wrapper classes.
*
* @param str $classes Comma separated entry posts classes.
* @param array $instance Settings for the current widget instance.
* @return array Entry posts classes.
*/
public function wrapper_classes( $classes, $instance ) {
$classes[] = 'index-view';
if ( false !== strpos( $instance['styles'], 'grid' ) ) {
$classes[] = 'flex-wrapper';
$classes[] = 'dp-grid';
} elseif ( false !== strpos( $instance['styles'], 'list' ) ) {
$classes[] = 'dp-list';
}
if ( in_array( $instance['styles'], [ 'slider1', 'slider2' ], true ) ) {
$classes[] = 'slider-wrapper';
$classes[] = 'widescreen';
$classes[] = 'dp-list';
}
$classes[] = $instance['image_crop'];
return $classes;
}
/**
* Register widget display posts entry classes.
*
* @param str $classes Comma separated entry posts classes.
* @param array $instance Settings for the current widget instance.
* @return str Entry posts classes.
*/
public function entry_classes( $classes, $instance ) {
if ( false !== strpos( $instance['styles'], 'grid' ) ) {
if ( 'grid-view2' === $instance['styles'] ) {
$classes[] = 'entry fw-tab-6 fw-tabr-6';
} else {
$classes[] = 'entry fw-tab-6 fw-tabr-4';
}
}
return $classes;
}
/**
* Display widget content to front-end.
*
* @param array $args Widget display arguments.
* @param array $instance Settings for the current widget instance.
*/
public function dp_entry( $args, $instance ) {
$display = $this->get_display_map( $instance['styles'] );
$display = $this->filter_display_map( $display, $instance );
if ( ! empty( $display ) ) {
echo '
';
$this->dp_display_entry( $display, $instance );
echo '
';
}
}
/**
* Get args for displaying elements for specific dp style.
*
* @param str $style Style for this widget instance.
* @return array
*/
public function get_display_map( $style ) {
/*
* Default element display instructions.
* Instructions array to display particular HTML element as per given sequence.
*/
$display_map = apply_filters(
'bayleaf_dp_style_args',
[
'list-view1' => [ 'thumbnail-medium', [ 'title', 'excerpt' ] ],
'grid-view1' => [ 'thumbnail-medium', [ 'title' ] ],
'grid-view2' => [ 'thumbnail-medium', [ 'category', 'title' ] ],
'grid-view3' => [ 'thumbnail-medium', [ 'category', 'title' ] ],
'slider1' => [ 'thumbnail-large', [ 'category', 'title', 'excerpt' ] ],
'slider2' => [ 'thumbnail-large', [ [ 'title', 'excerpt' ] ] ],
]
);
return isset( $display_map[ $style ] ) ? $display_map[ $style ] : [];
}
/**
* Display widget content to front-end.
*
* @param array $items content to be displayed.
* @param array $args Widget display arguments.
*/
public function filter_display_map( $items, $args ) {
if ( ! isset( $args['style_sup'] ) || ! $args['style_sup'] ) {
return $items;
}
// Compatibility with older version.
if ( 'slider1' === $args['styles'] ) {
$args['style_sup'][] = 'excerpt';
}
foreach ( $items as $key => $item ) {
if ( is_array( $item ) ) {
$items[ $key ] = $this->filter_display_map( $item, $args );
} else {
$unset = true;
if ( in_array( $item, $args['style_sup'], true ) ) {
$unset = false;
} elseif ( false !== strpos( $item, 'thumbnail' ) || 'title' === $item ) {
if ( in_array( 'thumbnailtitle', $args['style_sup'], true ) ) {
$unset = false;
}
}
if ( $unset ) {
unset( $items[ $key ] );
}
}
}
return $items;
}
/**
* Add items to widget title area.
*
* @param array $after_title Items before closing of widget title.
* @param array $instance Settings for the current widget instance.
* @return str
*/
public function dp_wid_title( $after_title, $instance ) {
$link_html = '';
// Change only if theme specific after_title args has not been altered.
if ( '' !== $after_title ) {
return $after_title;
}
if ( $instance['taxonomy'] && ! empty( $instance['terms'] ) ) {
foreach ( $instance['terms'] as $cur_term ) {
$term_link = get_term_link( $cur_term, $instance['taxonomy'] );
if ( ! is_wp_error( $term_link ) ) {
$link_html = sprintf( '%2$s %3$s', esc_url( $term_link ), esc_html__( 'View All', 'bayleaf' ), bayleaf_get_icon( array( 'icon' => 'long-arrow-right' ) ) );
break;
}
}
}
return '' . $link_html . '';
}
/**
* Display entry content to front-end.
*
* @param array $display_args Content display arguments.
* @param array $instance Settings for the current widget instance.
*/
public function dp_display_entry( $display_args, $instance ) {
$style = $instance['styles'];
$fetch = isset( $instance['fetch_media'] ) ? $instance['fetch_media'] : false;
foreach ( $display_args as $args ) {
if ( is_array( $args ) ) {
bayleaf_markup( 'sub-entry', [ [ [ $this, 'dp_display_entry' ], $args, $instance ] ] );
} else {
switch ( $args ) {
case 'title':
$this->title();
break;
case 'date':
$this->date();
break;
case 'ago':
$this->ago();
break;
case 'author':
$this->author();
break;
case 'content':
$this->content();
break;
case 'excerpt':
$this->excerpt( $instance );
break;
case 'category':
$this->category();
break;
case 'meta':
$this->meta();
break;
case 'meta-alt':
$this->meta_alt();
break;
case 'thumbnail-small':
$this->featured( 'thumbnail', $style, $fetch );
break;
case 'thumbnail-medium':
$this->featured( 'bayleaf-medium', $style, $fetch );
break;
case 'thumbnail-large':
$this->featured( 'bayleaf-large', $style, $fetch );
break;
case 'no-thumb':
$this->featured( false, $style, $fetch );
break;
default:
do_action( 'bayleaf_display_dp_item', $args );
break;
}
}
}
}
/**
* Enqueue scripts and styles to admin.
*
* @since 1.0.0
*/
public function enqueue_admin() {
$screen = get_current_screen();
if ( ! in_array( $screen->id, array( 'page', 'widgets', 'customize' ), true ) ) {
return;
}
wp_enqueue_style(
'bayleaf_display_posts_admin_style',
get_template_directory_uri() . '/add-on/display-posts/admin/displayposts.css',
array(),
BAYLEAF_THEME_VERSION,
'all'
);
wp_enqueue_script(
'bayleaf_display_posts_admin_js',
get_template_directory_uri() . '/add-on/display-posts/admin/displayposts.js',
[ 'jquery' ],
BAYLEAF_THEME_VERSION,
true
);
}
/**
* Display post entry title.
*
* @since 1.0.0
*/
public function title() {
if ( get_the_title() ) {
the_title(
sprintf(
''
);
}
}
/**
* Display post entry date.
*
* @since 1.0.0
*/
public function date() {
printf(
'',
esc_attr( get_the_date( DATE_W3C ) ),
esc_html( get_the_date( 'M j, Y' ) )
);
}
/**
* Display human readable post entry date.
*
* @since 1.0.0
*/
public function ago() {
$time = sprintf(
/* translators: %s: human-readable time difference */
esc_html_x( '%s ago', 'human-readable time difference', 'bayleaf' ),
esc_html( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) )
);
printf( '%s
', $time ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Display post entry author.
*
* @since 1.0.0
*/
public function author() {
printf(
'',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_html( get_the_author_meta( 'display_name' ) )
);
}
/**
* Display post featured content.
*
* @since 1.0.0
*
* @param str $size Thumbanil Size.
* @param str $style Current display post style.
* @param str $fetch Media type to be fetched from post content.
*/
public function featured( $size, $style = '', $fetch = false ) {
if ( bayleaf_get_mod( 'bayleaf_thumbnail_placeholder', 'none' ) || has_post_thumbnail() ) {
if ( $style && false !== strpos( $style, 'slider' ) ) {
$featured_content = [
[ [ $this, 'thumbnail' ], $size ],
];
} else {
$featured_content = [
[ 'bayleaf_get_template_partial', 'template-parts/meta', 'meta-permalink' ],
[ [ $this, 'thumbnail' ], $size ],
];
}
if ( $fetch ) {
$featured_content = apply_filters( 'bayleaf_dp_fetaured_content', $featured_content, $fetch, $style );
}
bayleaf_markup( 'dp-featured-content', $featured_content );
}
}
/**
* Display post entry thumbnail.
*
* @since 1.0.0
*
* @param str $size Thumbanil Size.
*/
public function thumbnail( $size ) {
if ( ! has_post_thumbnail() ) {
return;
}
$class = '';
$id = get_post_thumbnail_id();
$imgmeta = wp_get_attachment_metadata( $id );
if ( isset( $imgmeta['width'] ) && isset( $imgmeta['height'] ) ) {
if ( $imgmeta['width'] > $imgmeta['height'] ) {
$class = 'landscape';
} else {
$class = 'portrait';
}
}
if ( $size ) {
echo '';
the_post_thumbnail( $size );
echo '
';
}
}
/**
* Display post content.
*
* @since 1.0.0
*/
public function content() {
echo '';
the_content();
echo '
';
}
/**
* Display post content.
*
* @since 1.0.0
*
* @param array $instance Settings array for current widget instance.
*/
public function excerpt( $instance ) {
// Short circuit filter.
$check = apply_filters( 'bayleaf_display_posts_excerpt', false, $instance );
if ( false !== $check ) {
return;
}
$style = $instance['styles'];
$text = get_the_content( '' );
$text = wp_strip_all_tags( strip_shortcodes( $text ) );
$text = str_replace( ']]>', ']]>', $text );
// Return if not post content.
if ( ! $text ) {
return;
}
// Default value of excerpt length. Backward styles compatibility.
if ( in_array( $style, [ 'list-view1', 'slider2' ], true ) ) {
$len = 55;
} else {
$len = 20;
}
/**
* Filters the number of words in an excerpt.
*
* @since 1.0.0
*
* @param int $number The number of words.
* @param arr $instance Settings for current widget instance.
*/
$excerpt_length = apply_filters( 'bayleaf_dp_excerpt_length', $len, $instance );
// Generate excerpt teaser text and link.
$exrpt_url = esc_url( get_permalink() );
$exrpt_text = esc_html__( 'Continue Reading', 'bayleaf' );
$exrpt_text = apply_filters( 'bayleaf_dp_excerpt_text', $exrpt_text, $instance );
$exrpt_title = get_the_title();
if ( 0 === strlen( $exrpt_title ) ) {
$screen_reader = '';
} else {
$screen_reader = sprintf( '%s', $exrpt_title );
}
$excerpt_teaser = $exrpt_text ? sprintf( '%2$s %3$s
', $exrpt_url, $exrpt_text, $screen_reader ) : '';
/**
* Filters the string in the "more" link displayed after a trimmed excerpt.
*
* @since 1.0.0
*
* @param string $more_string The string shown within the more link.
* @param arr $instance Settings for current widget instance.
*/
$excerpt_more = apply_filters( 'bayleaf_dp_excerpt_more', ' ' . $excerpt_teaser, $instance );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
printf( '%s
', $text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Modify display post's excerpt length.
*
* @since 1.0.0
*
* @param int $length Excerpt length.
* @param arr $instance Settings for current widget instance.
* @return int Excerpt length.
*/
public function excerpt_length( $length, $instance ) {
$style = $instance['styles'];
if ( 'slider1' === $style ) {
$length = 0;
}
if ( 'slider2' === $style ) {
$length = 25;
}
return $length;
}
/**
* Modify display post's excerpt teaser text.
*
* @since 1.0.0
*
* @param str $text Excerpt teaser text.
* @param arr $instance Settings for current widget instance.
* @return int Excerpt teaser.
*/
public function excerpt_text( $text, $instance ) {
$style = $instance['styles'];
if ( 'slider1' === $style ) {
$text = esc_html__( 'Read More', 'bayleaf' );
}
return $text;
}
/**
* Display slider navigation.
*
* @since 1.0.0
*
* @param array $args Settings & args for the current widget instance.
*/
public function navigate( $args ) {
$instance = $args['instance'];
$query = $args['query'];
if ( 1 >= $query->post_count ) {
return;
}
if ( ! in_array( $instance['styles'], [ 'slider1', 'slider2' ], true ) ) {
return;
}
$navigation = sprintf(
'',
bayleaf_get_icon( [ 'icon' => 'angle-left' ] ),
esc_html__( 'Previous Slide', 'bayleaf' )
);
$navigation .= sprintf(
'',
bayleaf_get_icon( [ 'icon' => 'angle-right' ] ),
esc_html__( 'Next Slide', 'bayleaf' )
);
if ( 'slider2' === $instance['styles'] ) {
echo $navigation; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
printf( '%s
', $navigation ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
/**
* Display post categories.
*
* @since 1.0.0
*/
public function category() {
echo '';
the_category( ', ' );
echo '
';
}
/**
* Display post meta.
*
* @since 1.0.0
*/
public function meta() {
echo '';
$this->author();
esc_html_e( 'on', 'bayleaf' );
$this->date();
echo '
';
}
/**
* Display alternative post meta markup.
*
* @since 1.0.0
*/
public function meta_alt() {
echo '';
}
/**
* Register the custom Widget.
*
* @since 1.0.0
*/
public function register_custom_widget() {
require_once get_template_directory() . '/add-on/display-posts/class-instance-counter.php';
require_once get_template_directory() . '/add-on/display-posts/class-display-posts-widget.php';
register_widget( 'bayleaf\Display_Posts_Widget' );
}
}
Display_Posts::init();