$wrap_text,
'imgSize' => $img_size,
);
}
/**
* Renders the `tiptip/category-posts-block` on server.
*
* @see WP_Widget_Archives
*
* @param array $attributes The block attributes.
*
* @return string Returns the post content with archives added.
*/
function render_category_posts_block( $attributes ) {
global $attr, $before_title, $after_title;
$attr = $attributes;
// Get HTML
$widget = new Widget();
$instance = build_block_instance( $attributes );
$instance = upgrade_settings( $instance );
$block_id = get_block_loadmore_id( $attributes );
store_block_loadmore_settings( $block_id, $attributes );
$widget->number = $block_id;
$current_post_id = '';
if ( is_singular() ) {
$current_post_id = get_the_ID();
}
$items = $widget->get_elements_HTML( $instance, $current_post_id, 0, 0 );
$ret = '';
// Id of the wrapper element. $widget->number has to keep its 'block-' prefix
// because loadMoreHTML() relies on it, so use a separate variable for the DOM id.
$dom_id = WIDGET_BASE_ID . '-' . $block_id;
if ( ( 'nothing' === $instance['no_match_handling'] ) || ! empty( $items ) ) {
$ret = $widget->titleHTML( $before_title, $after_title, $instance );
$ret .= '
' . implode( $items ) . '
';
// Load more only if we think we have more items.
if ( count( $items ) === (int) $instance['num'] ) {
$ret .= $widget->loadMoreHTML( $instance );
}
$ret .= $widget->footerHTML( $instance );
// The 'cpwp-wrap-text' class the excerpt-lines CSS relies on, and the
// 'cpwp-wrap-text-stage' wrapper, are added by this script at runtime.
// Without it the excerpt lines and image ratio settings have no effect.
if ( isset( $instance['template'] ) && preg_match( '/%thumb%|%excerpt%/', $instance['template'] ) ) {
wp_enqueue_script( 'jquery' ); // Just in case the theme or other plugins did not enqueue it.
add_action(
'wp_footer',
function () use ( $dom_id, $instance ) {
equal_cover_content_height( $dom_id, $instance );
},
100
);
// Gutenberg Editor does not run wp_footer, so we also need to enqueue the script in the admin footer.
add_action(
'admin_footer',
function () use ( $dom_id, $instance ) {
equal_cover_content_height( $dom_id, $instance );
},
100
);
}
// Everything above hooks into wp_footer/admin_footer, which never run for the
// REST request the editor preview is built from - and a ';
}
} elseif ( 'text' === $instance['no_match_handling'] ) {
$ret = $widget->titleHTML( $before_title, $after_title, $instance );
$ret .= '' . wp_kses_post( $instance['no_match_text'] ) . '';
$ret .= $widget->footerHTML( $instance );
}
if ( '' === $ret ) {
return ''; // Nothing to show, so no style island either.
}
// The CSS rules depend on the settings of this specific block, so they can not be
// part of the static stylesheet. wp_head has long been sent by the time a block is
// rendered, so the rules are emitted as a style island right here.
$css = '';
static $styled = array(); // Identical attributes produce an identical id, emit once.
if ( ! isset( $styled[ $dom_id ] ) ) {
$styled[ $dom_id ] = true;
$virtual = new Virtual_Widget( $dom_id, WIDGET_BASE_ID . '-block', $instance );
$rules = array();
$virtual->getCSSRules( CONTEXT_BLOCK, $rules );
foreach ( $rules as $group ) {
$css .= implode( "\n", $group ) . "\n";
}
}
// get_block_wrapper_attributes() is what actually applies the block-supports
// classes (e.g. "aligncenter" from the toolbar's alignment option) to a
// dynamic block's markup - core never adds them on its own, they only ever
// end up in $attributes and are otherwise silently dropped by a hardcoded
// wrapper like the one this used to build.
$wrapper_attributes = get_block_wrapper_attributes(
array(
'id' => $dom_id,
'class' => WIDGET_BASE_ID . '-block',
)
);
return ( '' !== $css ? '' : '' ) .
'' . $ret . '
';
}
/**
* Registers all block assets so that they can be enqueued through the block editor
* in the corresponding context.
*
* @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/applying-styles-with-stylesheets/
*/
function category_posts_block_init() {
$dir = __DIR__;
$script_asset_path = "$dir/build/index.asset.php";
if ( ! file_exists( $script_asset_path ) ) {
throw new \Error(
'You need to run `npm start` or `npm run build` for the "tiptip/category-posts-block" block first.'
);
}
$index_js = 'build/index.js';
$script_asset = require( $script_asset_path );
wp_register_script(
'tiptip-category-posts-block-editor',
plugins_url( $index_js, __FILE__ ),
$script_asset['dependencies'],
$script_asset['version']
);
wp_set_script_translations( 'tiptip-category-posts-block-editor', 'category-posts' );
register_block_type_from_metadata(
__DIR__,
array(
'render_callback' => __NAMESPACE__ . '\render_category_posts_block',
)
);
}
add_action( 'init', __NAMESPACE__ . '\category_posts_block_init' );