PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / trunk
GenerateBlocks vtrunk
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 weeks 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
199 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 * Store our block name.
25 *
26 * @var string $block_name The block name.
27 */
28 public static $block_name = 'generateblocks/looper';
29
30 /**
31 * Sanitize a loop item's data for security.
32 *
33 * @param array|object $loop_item The loop item to sanitize.
34 * @return array|object The sanitized loop item.
35 */
36 public static function sanitize_loop_item( $loop_item ) {
37
38 $disallowed_keys = GenerateBlocks_Meta_Handler::DISALLOWED_KEYS ?? [];
39
40 foreach ( $disallowed_keys as $key ) {
41
42 if ( is_object( $loop_item ) ) {
43 if ( isset( $loop_item->$key ) ) {
44 unset( $loop_item->$key );
45 }
46 } elseif ( is_array( $loop_item ) ) {
47 if ( isset( $loop_item[ $key ] ) ) {
48 unset( $loop_item[ $key ] );
49 }
50 }
51 }
52
53 return $loop_item;
54 }
55
56 /**
57 * Render the repeater items for the Looper block.
58 *
59 * @param array $attributes Block attributes.
60 * @param WP_Block $block The block instance.
61 * @return string The rendered content.
62 */
63 public static function render_loop_items( $attributes, $block ) {
64 $query_data = $block->context['generateblocks/queryData']['data'] ?? null;
65 $query_type = $block->context['generateblocks/queryData']['type'] ?? null;
66 $output = '';
67
68 if ( GenerateBlocks_Block_Query::TYPE_WP_QUERY === $query_type ) {
69 $output = self::render_wp_query( $query_data, $attributes, $block );
70 }
71
72 /**
73 * Allow users to filter the looper rendering of loop items.
74 *
75 * @param string $output The block output.
76 * @param string $query_type The query type.
77 * @param array|object $query_data The query data.
78 * @param array $attributes Block attributes.
79 * @param WP_Block $block The block instance.
80 */
81 $output = apply_filters( 'generateblocks_looper_render_loop_items', $output, $query_type, $query_data, $block, $attributes );
82
83 return $output;
84 }
85
86 /**
87 * Render the repeater items for the Looper block.
88 *
89 * @param WP_Query $query The WP_Query instance.
90 * @param array $attributes Block attributes.
91 * @param WP_Block $block The block instance.
92 * @return string The rendered content.
93 */
94 public static function render_wp_query( $query, $attributes, $block ) {
95 $query_id = $block->context['generateblocks/queryData']['id'] ?? null;
96 $page_key = $query_id ? 'query-' . $query_id . '-page' : 'query-page';
97 $per_page = $query->query_vars['posts_per_page'] ?? get_option( 'posts_per_page', 10 );
98 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; // phpcs:ignore -- No data processing happening.
99 $page_index = $page - 1; // Zero based index for pages.
100 $offset = $page_index * $per_page;
101 $content = '';
102 $inner_blocks = $block->parsed_block['innerBlocks'] ?? [];
103
104 if ( empty( $inner_blocks ) ) {
105 return '';
106 }
107
108 // Fallback to support preview in Elements.
109 if ( ! $query ) {
110 return (
111 new WP_Block(
112 $inner_blocks[0],
113 array(
114 'postType' => 'post',
115 'postId' => 0,
116 'generateblocks/queryType' => GenerateBlocks_Block_Query::TYPE_WP_QUERY,
117 'generateblocks/loopIndex' => 1,
118 'generateblocks/loopItem' => [ 'ID' => 0 ],
119 )
120 )
121 )->render( array( 'dynamic' => false ) );
122 }
123
124 if ( $query->have_posts() ) {
125 while ( $query->have_posts() ) {
126 $query->the_post();
127 global $post;
128 // Get the current index of the Loop.
129 foreach ( $inner_blocks as $inner_block ) {
130 $content .= (
131 new WP_Block(
132 $inner_block,
133 array(
134 'postType' => get_post_type(),
135 'postId' => get_the_ID(),
136 'generateblocks/queryType' => GenerateBlocks_Block_Query::TYPE_WP_QUERY,
137 'generateblocks/loopIndex' => $offset + $query->current_post + 1,
138 'generateblocks/loopItem' => self::sanitize_loop_item( $post ),
139 )
140 )
141 )->render();
142 }
143 }
144
145 wp_reset_postdata();
146 }
147
148 return $content;
149 }
150
151 /**
152 * Render the Query block.
153 *
154 * @param array $attributes The block attributes.
155 * @param string $block_content The block content.
156 * @param array $block The block.
157 */
158 public static function render_block( $attributes, $block_content, $block ) {
159 $loop_items = self::render_loop_items( $attributes, $block );
160
161 if ( ! $loop_items ) {
162 return '';
163 }
164
165 $html_attributes = generateblocks_get_processed_html_attributes( $block_content );
166
167 // If our processing returned nothing, let's try to build our attributes from the block attributes.
168 if ( empty( $html_attributes ) ) {
169 $html_attributes = generateblocks_get_backup_html_attributes( 'gb-looper', $attributes );
170 }
171
172 $tag_name = $attributes['tagName'] ?? 'div';
173
174 // Add styles to this block if needed.
175 $output = generateblocks_maybe_add_block_css(
176 '',
177 [
178 'class_name' => __CLASS__,
179 'attributes' => $attributes,
180 'block_ids' => self::$block_ids,
181 ]
182 );
183
184 $output .= sprintf(
185 '<%1$s %2$s>%3$s</%1$s>',
186 $tag_name,
187 generateblocks_attr(
188 'looper',
189 $html_attributes,
190 $attributes,
191 $block
192 ),
193 $loop_items
194 );
195
196 return $output;
197 }
198 }
199