block_slug` property
*/
protected function set_block_slug() {
$this->block_slug = 'posts-grid';
}
/**
* Set the attributes required on the server side.
*/
protected function set_attributes() {
$this->attributes = array(
'style' => array(
'type' => 'string',
'default' => 'grid',
),
'columns' => array(
'type' => 'number',
'default' => 3,
),
'template' => array(
'type' => 'object',
'default' => array(
'category',
'title',
'meta',
'description',
),
),
'categories' => array(
'type' => 'array',
'items' => array(
'type' => 'object',
),
),
'postsToShow' => array(
'type' => 'number',
'default' => 5,
),
'order' => array(
'type' => 'string',
'default' => 'desc',
),
'orderBy' => array(
'type' => 'string',
'default' => 'date',
),
'offset' => array(
'type' => 'number',
'default' => 0,
),
'imageSize' => array(
'type' => 'string',
'default' => 'full',
),
'imageBoxShadow' => array(
'type' => 'boolean',
'default' => true,
),
'displayFeaturedImage' => array(
'type' => 'boolean',
'default' => true,
),
'displayCategory' => array(
'type' => 'boolean',
'default' => true,
),
'displayTitle' => array(
'type' => 'boolean',
'default' => true,
),
'titleTag' => array(
'type' => 'string',
'default' => 'h5',
),
'displayMeta' => array(
'type' => 'boolean',
'default' => true,
),
'displayDescription' => array(
'type' => 'boolean',
'default' => true,
),
'excerptLength' => array(
'type' => 'number',
'default' => '200',
),
'displayDate' => array(
'type' => 'boolean',
'default' => true,
),
'displayAuthor' => array(
'type' => 'boolean',
'default' => true,
),
);
}
/**
* Block render function for server-side.
*
* This method will pe passed to the render_callback parameter and it will output
* the server side output of the block.
*
* @param array $attributes Blocks attrs.
*
* @return mixed|string
*/
protected function render( $attributes ) {
$categories = 0;
if ( isset( $attributes['categories'] ) ) {
$cats = array();
foreach ( $attributes['categories'] as $category ) {
array_push( $cats, $category['id'] );
}
$categories = join( ', ', $cats );
}
$recent_posts = wp_get_recent_posts(
array(
'numberposts' => $attributes['postsToShow'],
'post_status' => 'publish',
'order' => $attributes['order'],
'orderby' => $attributes['orderBy'],
'offset' => $attributes['offset'],
'category' => $categories,
'suppress_filters' => false,
)
);
$list_items_markup = '';
foreach ( $recent_posts as $post ) {
$id = $post['ID'];
$size = isset( $attributes['imageSize'] ) ? $attributes['imageSize'] : 'medium';
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), $size );
$category = get_the_category( $id );
$list_items_markup .= '
';
if ( isset( $attributes['displayFeaturedImage'] ) && $attributes['displayFeaturedImage'] ) {
if ( $thumbnail ) {
$list_items_markup .= sprintf(
'
',
esc_url( get_the_permalink( $id ) ),
wp_get_attachment_image( get_post_thumbnail_id( $id ), $size ),
esc_html( get_the_title( $id ) )
);
}
}
$list_items_markup .= '
';
foreach ( $attributes['template'] as $element ) {
if ( 'category' === $element ) {
if ( isset( $attributes['displayCategory'] ) && isset( $category[0] ) && $attributes['displayCategory'] ) {
$list_items_markup .= sprintf(
'
%1$s',
esc_html( $category[0]->cat_name )
);
}
}
if ( 'title' === $element ) {
if ( isset( $attributes['displayTitle'] ) && $attributes['displayTitle'] ) {
$list_items_markup .= sprintf(
'<%1$s class="wp-block-themeisle-blocks-posts-grid-post-title">
%3$s%1$s>',
esc_attr( $attributes['titleTag'] ),
esc_url( get_the_permalink( $id ) ),
esc_html( get_the_title( $id ) )
);
}
}
if ( 'meta' === $element ) {
if ( ( isset( $attributes['displayMeta'] ) && $attributes['displayMeta'] ) && ( ( isset( $attributes['displayDate'] ) && $attributes['displayDate'] ) || ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) ) ) {
$list_items_markup .= '
';
if ( isset( $attributes['displayDate'] ) && $attributes['displayDate'] ) {
$list_items_markup .= sprintf(
'%1$s ',
__( 'on', 'themeisle-companion' ),
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( 'j F, Y', $id ) )
);
}
if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
$list_items_markup .= sprintf(
'%1$s %2$s',
__( 'by', 'themeisle-companion' ),
get_the_author_meta( 'display_name', get_post_field( 'post_author', $id ) )
);
}
$list_items_markup .= '
';
}
}
if ( 'description' === $element ) {
if ( ( isset( $attributes['excerptLength'] ) && $attributes['excerptLength'] > 0 ) && ( isset( $attributes['displayDescription'] ) && $attributes['displayDescription'] ) ) {
$list_items_markup .= sprintf(
'
%1$s
',
$this->get_excerpt_by_id( $id, $attributes['excerptLength'] )
);
}
}
}
$list_items_markup .= '
';
}
$class = 'wp-block-themeisle-blocks-posts-grid';
if ( isset( $attributes['className'] ) ) {
$class .= ' ' . esc_attr( $attributes['className'] );
}
if ( isset( $attributes['align'] ) ) {
$class .= ' align' . $attributes['align'];
}
if ( isset( $attributes['grid'] ) && true === $attributes['grid'] ) {
$class .= ' is-grid';
}
if ( isset( $attributes['style'] ) ) {
$class .= ' is-' . $attributes['style'];
}
if ( ( isset( $attributes['style'] ) && 'grid' === $attributes['style'] ) || ( isset( $attributes['grid'] ) && true === $attributes['grid'] ) ) {
$class .= ' wp-block-themeisle-blocks-posts-grid-columns-' . $attributes['columns'];
}
if ( isset( $attributes['imageBoxShadow'] ) && true === $attributes['imageBoxShadow'] ) {
$class .= ' has-shadow';
}
$block_content = sprintf(
'%2$s
',
esc_attr( $class ),
$list_items_markup
);
return $block_content;
}
/**
* Get post excerpt
*
* @param int $post_id Post id.
* @param int $excerpt_length Excerpt size.
*
* @return string
*/
protected function get_excerpt_by_id( $post_id, $excerpt_length = 200 ) {
$excerpt = get_the_excerpt( $post_id );
if ( strlen( $excerpt ) > $excerpt_length ) {
$excerpt = substr( $excerpt, 0, $excerpt_length ) . '…';
}
return $excerpt;
}
}