[ 'type' => 'string', 'default' => 'post', ], 'className' => [ 'type' => 'string', ], 'postsToShow' => [ 'type' => 'number', 'default' => 5, ], 'order' => [ 'type' => 'string', 'default' => 'desc', ], 'orderBy' => [ 'type' => 'string', 'default' => 'date', ], 'align' => [ 'type' => 'string', ], ]; /** * Constructor * * @param string $name block name. */ public function __construct( string $name ) { foreach ( get_taxonomies( [ 'publicly_queryable' => true ], 'objects' ) as $taxonomy ) { $this->get_rest_base( $taxonomy ); $base = $this->get_rest_base( $taxonomy ); $this->attributes[ $base ] = [ 'type' => 'array', 'default' => [], ]; } new Matrix_Term_Query( 'advanced_posts_blocks' ); parent::__construct( $name ); } /** * Get rest Base. * * @param \WP_Taxonomy $taxonomy Taxonomy object. * * @return bool|string */ public function get_rest_base( \WP_Taxonomy $taxonomy ) { return ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; } /** * Get taxonomies with connected. * * @param array|string|\WP_Post $post_type Name of the type of taxonomy object, or an object (row from posts). * * @return \WP_Taxonomy[] */ public function get_post_type_taxonomies( $post_type ) { return array_map( 'get_taxonomy', get_object_taxonomies( $post_type ) ); } /** * Render callback * * @param array $attributes block attributes. * * @return false|string */ public function render( $attributes ) { $args = [ 'posts_per_page' => $attributes['postsToShow'], 'post_status' => 'publish', 'order' => $attributes['order'], 'orderby' => $attributes['orderBy'], 'post_type' => $attributes['postType'], 'advanced_posts_blocks' => true, ]; $post_type = $attributes['postType']; $args['tax_query'] = []; foreach ( $this->get_post_type_taxonomies( $post_type ) as $taxonomy ) { $this->get_rest_base( $taxonomy ); $base = $this->get_rest_base( $taxonomy ); $terms = array_filter( $attributes[ $base ] ); if ( ! empty( $terms ) ) { $args['tax_query'][] = array_merge( [ 'taxonomy' => $taxonomy->name, 'field' => 'term_id', 'terms' => $terms, ] ); } } $query = new \WP_Query( $args ); set_query_var( 'query', $query ); $output = $this->get_content_from_template( $attributes ); if ( $output ) { return $output; } ob_start(); load_template( dirname( __FILE__ ) . '/template.php', false ); $output = ob_get_contents(); ob_end_clean(); return $output; } }