PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.2
GenerateBlocks v2.0.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / includes / blocks / class-looper.php
generateblocks / includes / blocks Last commit date
class-block.php 1 year ago class-button-container.php 2 years ago class-button.php 2 years ago class-container.php 2 years ago class-element.php 1 year ago class-grid.php 2 years ago class-headline.php 2 years ago class-image.php 2 years ago class-loop-item.php 1 year ago class-looper.php 1 year ago class-media.php 1 year ago class-query-loop.php 3 years ago class-query-no-results.php 1 year ago class-query-page-numbers.php 1 year ago class-query.php 1 year ago class-shape.php 1 year ago class-text.php 1 year ago
class-looper.php
192 lines
1 <?php
2 /**
3 * Handles the Element block.
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * The Element block.
14 */
15 class GenerateBlocks_Block_Looper extends GenerateBlocks_Block {
16 /**
17 * Keep track of all blocks of this type on the page.
18 *
19 * @var array $block_ids The current block id.
20 */
21 protected static $block_ids = [];
22
23 /**
24 * Sanitize a loop item's data for security.
25 *
26 * @param array|object $loop_item The loop item to sanitize.
27 * @return array|object The sanitized loop item.
28 */
29 public static function sanitize_loop_item( $loop_item ) {
30
31 $disallowed_keys = GenerateBlocks_Meta_Handler::DISALLOWED_KEYS ?? [];
32
33 foreach ( $disallowed_keys as $key ) {
34
35 if ( is_object( $loop_item ) ) {
36 if ( isset( $loop_item->$key ) ) {
37 unset( $loop_item->$key );
38 }
39 } elseif ( is_array( $loop_item ) ) {
40 if ( isset( $loop_item[ $key ] ) ) {
41 unset( $loop_item[ $key ] );
42 }
43 }
44 }
45
46 return $loop_item;
47 }
48
49 /**
50 * Render the repeater items for the Looper block.
51 *
52 * @param array $attributes Block attributes.
53 * @param WP_Block $block The block instance.
54 * @return string The rendered content.
55 */
56 public static function render_loop_items( $attributes, $block ) {
57 $query_data = $block->context['generateblocks/queryData']['data'] ?? null;
58 $query_type = $block->context['generateblocks/queryData']['type'] ?? null;
59 $output = '';
60
61 if ( GenerateBlocks_Block_Query::TYPE_WP_QUERY === $query_type ) {
62 $output = self::render_wp_query( $query_data, $attributes, $block );
63 }
64
65 /**
66 * Allow users to filter the looper rendering of loop items.
67 *
68 * @param string $output The block output.
69 * @param string $query_type The query type.
70 * @param array|object $query_data The query data.
71 * @param array $attributes Block attributes.
72 * @param WP_Block $block The block instance.
73 */
74 $output = apply_filters( 'generateblocks_looper_render_loop_items', $output, $query_type, $query_data, $block, $attributes );
75
76 return $output;
77 }
78
79 /**
80 * Render the repeater items for the Looper block.
81 *
82 * @param WP_Query $query The WP_Query instance.
83 * @param array $attributes Block attributes.
84 * @param WP_Block $block The block instance.
85 * @return string The rendered content.
86 */
87 public static function render_wp_query( $query, $attributes, $block ) {
88 $query_id = $block->context['generateblocks/queryData']['id'] ?? null;
89 $page_key = $query_id ? 'query-' . $query_id . '-page' : 'query-page';
90 $per_page = $query->query_vars['posts_per_page'] ?? get_option( 'posts_per_page', 10 );
91 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; // phpcs:ignore -- No data processing happening.
92 $page_index = $page - 1; // Zero based index for pages.
93 $offset = $page_index * $per_page;
94 $content = '';
95 $inner_blocks = $block->parsed_block['innerBlocks'] ?? [];
96
97 if ( empty( $inner_blocks ) ) {
98 return '';
99 }
100
101 // Fallback to support preview in Elements.
102 if ( ! $query ) {
103 return (
104 new WP_Block(
105 $inner_blocks[0],
106 array(
107 'postType' => 'post',
108 'postId' => 0,
109 'generateblocks/queryType' => GenerateBlocks_Block_Query::TYPE_WP_QUERY,
110 'generateblocks/loopIndex' => 1,
111 'generateblocks/loopItem' => [ 'ID' => 0 ],
112 )
113 )
114 )->render( array( 'dynamic' => false ) );
115 }
116
117 if ( $query->have_posts() ) {
118 while ( $query->have_posts() ) {
119 $query->the_post();
120 global $post;
121 // Get the current index of the Loop.
122 foreach ( $inner_blocks as $inner_block ) {
123 $content .= (
124 new WP_Block(
125 $inner_block,
126 array(
127 'postType' => get_post_type(),
128 'postId' => get_the_ID(),
129 'generateblocks/queryType' => GenerateBlocks_Block_Query::TYPE_WP_QUERY,
130 'generateblocks/loopIndex' => $offset + $query->current_post + 1,
131 'generateblocks/loopItem' => self::sanitize_loop_item( $post ),
132 )
133 )
134 )->render();
135 }
136 }
137
138 wp_reset_postdata();
139 }
140
141 return $content;
142 }
143
144 /**
145 * Render the Query block.
146 *
147 * @param array $attributes The block attributes.
148 * @param string $block_content The block content.
149 * @param array $block The block.
150 */
151 public static function render_block( $attributes, $block_content, $block ) {
152 $loop_items = self::render_loop_items( $attributes, $block );
153
154 if ( ! $loop_items ) {
155 return '';
156 }
157
158 $html_attributes = generateblocks_get_processed_html_attributes( $block_content );
159
160 // If our processing returned nothing, let's try to build our attributes from the block attributes.
161 if ( empty( $html_attributes ) ) {
162 $html_attributes = generateblocks_get_backup_html_attributes( 'gb-looper', $attributes );
163 }
164
165 $tag_name = $attributes['tagName'] ?? 'div';
166
167 // Add styles to this block if needed.
168 $output = generateblocks_maybe_add_block_css(
169 '',
170 [
171 'class_name' => __CLASS__,
172 'attributes' => $attributes,
173 'block_ids' => self::$block_ids,
174 ]
175 );
176
177 $output .= sprintf(
178 '<%1$s %2$s>%3$s</%1$s>',
179 $tag_name,
180 generateblocks_attr(
181 'looper',
182 $html_attributes,
183 $attributes,
184 $block
185 ),
186 $loop_items
187 );
188
189 return $output;
190 }
191 }
192