Skip to content

Commit

Permalink
Merge pull request #53 from torounit/fix/block-attr-for-query
Browse files Browse the repository at this point in the history
add block attribute and WP_Block instance `advanced_posts_blocks_posts_query`
  • Loading branch information
torounit authored Jul 13, 2023
2 parents 91283bc + e5a0faf commit 2b2aa30
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/blocks/children/class-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ class Renderer extends \Advanced_Posts_Blocks\Blocks\Renderer {
* Render callback
*
* @param array $attributes block attributes.
* @param string $block_content block content.
* @param \WP_Block $block_instance block instance.
*
* @return string
*/
public function render( array $attributes ) : string {
public function render( array $attributes, string $block_content, \WP_Block $block_instance ) : string {
$args = array(
'posts_per_page' => $attributes['postsToShow'],
'post_status' => 'publish',
Expand All @@ -38,7 +40,7 @@ public function render( array $attributes ) : string {
'post_type' => $attributes['postType'] ? $attributes['postType'] : get_post_type(),
);

$this->setup_query( $args );
$this->setup_query( $args, $attributes, $block_instance );

if ( ! $this->query->found_posts ) {
return '';
Expand Down
16 changes: 12 additions & 4 deletions src/blocks/class-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public function __construct() {
* Register Block Type.
*/
protected function register() {
$block = register_block_type( dirname( PLUGIN_FILE ) . '/build/blocks/' . $this->dirname );
$block = register_block_type( dirname( PLUGIN_FILE ) . '/build/blocks/' . $this->dirname );
if ( ! $block ) {
return;
}
$this->name = $block->name;
$block->set_props(
array(
Expand All @@ -94,10 +97,12 @@ public function get_attributes(): array {
* Render callback
*
* @param array $attributes block attributes.
* @param string $block_content block content.
* @param \WP_Block $block_instance block instance.
*
* @return string
*/
abstract public function render( array $attributes ): string;
abstract public function render( array $attributes, string $block_content, \WP_Block $block_instance ): string;

/**
* Get html class names.
Expand Down Expand Up @@ -248,19 +253,22 @@ protected function get_content_from_default_template( string $name ) {
* Create \WP_Query and set $query.
*
* @param array $args URL query string or array of vars.
* @param array $attributes Block attributes.
* @param \WP_Block|null $block_instance Block instance.
* @param string $query_var query var.
*/
protected function setup_query( array $args, string $query_var = 'query' ) {
protected function setup_query( array $args, array $attributes = array(), \WP_Block $block_instance = null, string $query_var = 'query' ) {
/**
* Filters query arguments.
*
* @param array $args query arguments.
* @param string $name block name.
* @param array $attributes block attributes.
* @param \WP_Block $block_instance Block instance.
*
* @since 0.6.0
*/
$args = apply_filters( 'advanced_posts_blocks_posts_query', $args, $this->name, $this->attributes );
$args = apply_filters( 'advanced_posts_blocks_posts_query', $args, $this->name, $attributes, $block_instance );
$this->query = new WP_Query( $args );
$this->set_template_args( $query_var, $this->query );
}
Expand Down
6 changes: 4 additions & 2 deletions src/blocks/post/class-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ class Renderer extends \Advanced_Posts_Blocks\Blocks\Renderer {
* Render callback
*
* @param array $attributes block attributes.
* @param string $block_content block content.
* @param \WP_Block $block_instance block instance.
*
* @return string
*/
public function render( array $attributes ) : string {
public function render( array $attributes, string $block_content, \WP_Block $block_instance ) : string {
if ( empty( $attributes['postId'] ) ) {
return '';
}
Expand All @@ -37,7 +39,7 @@ public function render( array $attributes ) : string {
'post_type' => $attributes['postType'],
);

$this->setup_query( $args );
$this->setup_query( $args, $attributes, $block_instance );

if ( ! $this->query->found_posts ) {
return '';
Expand Down
6 changes: 4 additions & 2 deletions src/blocks/posts/class-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ public function get_post_type_taxonomies( $post_type ): array {
* Render callback
*
* @param array $attributes block attributes.
* @param string $block_content block content.
* @param \WP_Block $block_instance block instance.
*
* @return string|null
*/
public function render( array $attributes ): string {
public function render( array $attributes, string $block_content, \WP_Block $block_instance ): string {
$args = array(
'posts_per_page' => $attributes['postsToShow'],
'post_status' => 'publish',
Expand Down Expand Up @@ -151,7 +153,7 @@ public function render( array $attributes ): string {
}
}

$this->setup_query( $args );
$this->setup_query( $args, $attributes, $block_instance );

if ( ! $this->query->found_posts ) {
return '';
Expand Down

0 comments on commit 2b2aa30

Please sign in to comment.