=' ) ) { add_filter( 'block_categories_all', [ $this, 'register_advanced_blog_post_block_category' ], 99999, 2 ); } else { add_filter( 'block_categories', [ $this, 'register_advanced_blog_post_block_category' ], 99999, 2 ); } // Enqueue Inline style on render block add_filter( 'render_block', [ $this, 'generate_inline_style_on_render_block' ], 10, 2 ); // enqueue editor assets add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] ); // enqueue assets for frontend add_action( 'enqueue_block_assets', [ $this, 'enqueue_assets' ] ); // Add filter for comment count add_filter('rest_prepare_post', [ $this, 'bwdabpb_add_comment_count_to_rest_response' ], 10, 3); } /** * Enqueue Editor Assets */ public function enqueue_editor_assets(){ wp_enqueue_script( 'bwdabpb-blocks-global-js', BWDABPB_ADVANCED_BLOG_POST_ADMIN_URL . '/dist/global.js', [], BWDABPB_ADVANCED_BLOG_POST_VERSION, true ); } /** * Enqueue Assets */ public function enqueue_assets(){ wp_enqueue_script( 'bwdabpb-blocks-frontend', BWDABPB_ADVANCED_BLOG_POST_ADMIN_URL . './includes/assets/js/main.js', [], BWDABPB_ADVANCED_BLOG_POST_VERSION, true); } /** * Blocks Category */ public function register_advanced_blog_post_block_category( $categories, $post ) { return array_merge( [ [ 'slug' => 'bwdabpb-advanced-post-blocks', 'title' => __( 'Advanced Blog Post Block', 'advanced-blog-post-block' ) ], ], $categories ); } /** * Load Blocks */ public function register_blocks() { // get all blocks from includes/blocks/blocks.php require_once BWDABPB_ADVANCED_BLOG_POST_DIR_PATH . './includes/blocks/blocks.php'; // Register Blocks if( ! empty( $bwdabpb_advanced_blog_post_blocks ) && is_array( $bwdabpb_advanced_blog_post_blocks ) ) { foreach( $bwdabpb_advanced_blog_post_blocks as $block ) { $this->register_single_block( $block ); } } } /** * Register Single Block */ public function register_single_block( $block ) { register_block_type( BWDABPB_ADVANCED_BLOG_POST_DIR_PATH . './build/blocks/' . $block['name'], [ 'render_callback' => [ $this, 'bwdabpb_advanced_blog_post_render_callback'] ] ); } /** * Add Comment Count to REST API Response for a Post */ public function bwdabpb_add_comment_count_to_rest_response($data, $post, $context) { $data->data['comment_count'] = get_comments_number($post->ID); return $data; } /** * Render callback for the BWD Advanced Post Block. */ public function bwdabpb_advanced_blog_post_render_callback ($attributes, $content){ $selectedCategory = isset($attributes['selectedCategory']) ? $attributes['selectedCategory'] : []; $categoryIds = array_column($selectedCategory, 'value'); $selectedTag = isset($attributes['selectedTag']) ? $attributes['selectedTag'] : []; $tagIds = array_column($selectedTag, 'value'); $selectedAuthor = isset($attributes['selectedAuthor']) ? $attributes['selectedAuthor'] : []; $authorIds = array_column($selectedAuthor, 'value'); $selectCustomPost = isset($attributes['includePosts']) ? $attributes['includePosts'] : []; $includeId = array_column($selectCustomPost, 'value'); $removePost = isset($attributes['excludePosts']) ? $attributes['excludePosts'] : []; $excludeId = array_column($removePost, 'value'); // Building the query arguments based on attributes $args = [ 'post_type' => 'post', "posts_per_page" => isset($attributes['numberOfPost']) ? $attributes['numberOfPost'] : 4, 'category__in' => $categoryIds, "order" => isset($attributes['order']) ? $attributes['order'] : '', "orderby" => isset($attributes['orderBy']) ? $attributes['orderBy'] : '', 'author__in' => $authorIds, 'tag__in' => $tagIds, 'post__in' => $includeId, 'post__not_in' => $excludeId, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1, ]; // Creating a new WP_Query instance $query = new WP_Query($args); // Extracting additional attributes $style = isset( $attributes['style'] ) ? $attributes['style'] : 'style-1'; $container = isset( $attributes['containerWidth'] ) ? $attributes['containerWidth'] : 'container'; $author_prefix = isset( $attributes['authorPrefix'] ) ? $attributes['authorPrefix'] : 'Written By'; $read_time_suffix = isset( $attributes['readTimeSuffix'] ) ? $attributes['readTimeSuffix'] : 'Min Read'; $show_header = isset( $attributes['showHeading'] ) ? $attributes['showHeading'] : false; $show_sub_header = isset( $attributes['showSubHeading'] ) ? $attributes['showSubHeading'] : false; $show_separator = isset( $attributes['showSeparator'] ) ? $attributes['showSeparator'] : false; $show_img_overlay = isset( $attributes['showImageOverlay'] ) ? $attributes['showImageOverlay'] : false; $header_title = isset( $attributes['headerTitle'] ) ? $attributes['headerTitle'] : 'Header Title Here'; $sub_heading = isset( $attributes['subHeading'] ) ? $attributes['subHeading'] : 'Sub Title Here'; $show_featured_image = isset( $attributes['showFeaturedImage'] ) ? $attributes['showFeaturedImage'] : true; $show_title = isset( $attributes['showTitle'] ) ? $attributes['showTitle'] : true; $title_tag = isset( $attributes['titleTag'] ) ? $attributes['titleTag'] : 'h2'; $show_excerpt = isset( $attributes['showExcerpt'] ) ? $attributes['showExcerpt'] : true; $show_excerpt_content = isset( $attributes['showExcerptContent'] ) ? $attributes['showExcerptContent'] : true; $show_read_more = isset( $attributes['showReadMore'] ) ? $attributes['showReadMore'] : true; $show_date = isset( $attributes['showDate'] ) ? $attributes['showDate'] : true; $show_tags = isset($attributes['showTags']) ? $attributes['showTags'] : false; $show_comments = isset($attributes['showComments']) ? $attributes['showComments'] : false; $show_category = isset( $attributes['showCategory'] ) ? $attributes['showCategory'] : true; $excerpt_length = isset($attributes['wordsExcerpt']) ? absint($attributes['wordsExcerpt']) : 10; $title_length = isset($attributes['titleLength']) ? absint($attributes['titleLength']) : 5; $tail_indicator = isset( $attributes['wordsTailIndicator'] ) ? $attributes['wordsTailIndicator'] : '.....'; $author_icon = isset( $attributes['authorIcon'] ) ? $attributes['authorIcon'] : 'gravatar'; $show_pagination = isset( $attributes['showPagination'] ) ? $attributes['showPagination'] : false; $pagi_prev_text = isset( $attributes['pagiPrevText'] ) ? $attributes['pagiPrevText'] : 'Previous'; $pagi_next_text = isset( $attributes['pagiNextText'] ) ? $attributes['pagiNextText'] : 'Next'; $read_more_text = isset( $attributes['readMoreText'] ) ? $attributes['readMoreText'] : 'Read More'; $item_animation = isset( $attributes['animation'] ) ? $attributes['animation'] : ''; $customCss = isset($attributes['customCSS']) ? $attributes['customCSS'] : ''; $custom_id = isset($attributes['id']) ? $attributes['id'] : ''; $date_format = isset( $attributes['customDate'] ) ? $attributes['customDate'] : 'F j, Y'; // Get block wrapper attributes $blocks_props = get_block_wrapper_attributes([ 'class' => $attributes['uniqueId'] . ' ' . $style . ' ' . $container . ' bwdabpb-responsive', ]); // Start output buffering ob_start(); // Ensure that the custom CSS is not empty if (!empty($customCss)) { echo ''; } // Building the main markup $markup = '
'; // Post Header if ($show_header) { $markup .= '
'; $markup .= '

' . esc_html($header_title) . '

'; if ($show_sub_header) { $markup .= '

' . esc_html($sub_heading) . '

'; } if ($show_separator) { $markup .= '
'; } $markup .= '
'; } $markup .= '
'; // Loop through the query results if( $query->have_posts() ) { while( $query->have_posts() ) { $query->the_post(); // Get post categories $categories = get_the_category(); // Get post permalink $permalink = get_permalink(); // Start building the post item markup $markup .= '
'; $markup .= '
'; // Featured image handling if ($show_featured_image && has_post_thumbnail()) { $featured_image = get_the_post_thumbnail(); $markup .= '' . $featured_image . ''; } else { // Placeholder image if featured image is not available $placeholder_image = plugin_dir_url(__FILE__) . 'assets/img/bwd-placeholder.jpg'; $markup .= ''; } $markup .= '
'; // Post content section $markup .= '
'; $markup .= ''; // Post title if ($show_title) { $title = wp_trim_words(get_the_title(), $title_length, ''); $title = rtrim($title, '.'); $markup .= '<' . esc_html($title_tag) . ' class="bwdabpb-post-title">' . esc_html($title) . ''; } // Post excerpt if ($show_excerpt) { $markup .= '
'; if ($show_excerpt_content) { $excerpt_source = get_the_content(); } else { $excerpt_source = get_the_excerpt(); } $excerpt = wp_trim_words($excerpt_source, $excerpt_length, ''); $markup .= '

' . wp_kses_post($excerpt) . esc_html($tail_indicator) . '

'; $markup .= '
'; } // Read more link if ($show_read_more) { $markup .= '' . esc_html($read_more_text) . ''; } $markup .= '
'; // Post categories if ($show_category && $categories) { $markup .= ''; } $markup .= ''; $markup .= '
'; } // Reset post data after the loop wp_reset_postdata(); } $markup .= '
'; // Pagination if ($show_pagination) { $pagination_args = array( 'total' => $query->max_num_pages, 'prev_text' => ''. esc_html($pagi_prev_text) .'', 'next_text' => ''. esc_html($pagi_next_text) .'', 'type' => 'list', ); $pagination_links = paginate_links($pagination_args); // Sanitize and echo the pagination links echo wp_kses_post($pagination_links); } $markup .= '
'; // End output buffering and return the markup $markup .= ob_get_clean(); return $markup; } /** * Register Inline Style */ function generate_inline_style_on_render_block($block_content, $block ) { if (isset($block['blockName']) && str_contains($block['blockName'], 'bwdabpb/')) { if (isset($block['attrs']['blockStyle'])) { $style = $block['attrs']['blockStyle']; $handle = isset( $block['attrs']['uniqueId'] ) ? $block['attrs']['uniqueId'] : 'advanced-blog-post-block'; // convert style array to string if ( is_array($style) ) { $style = implode(' ', $style); } // minify style to remove extra space $style = preg_replace( '/\s+/', ' ', $style ); wp_register_style( $handle, false, [], BWDABPB_ADVANCED_BLOG_POST_VERSION ); wp_enqueue_style( $handle ); wp_add_inline_style( $handle, $style ); } } return $block_content; } } new BWDABPB_ADVANCED_BLOG_POST_BLOCKS_LOADER();