Skip to content

Commit ab48944

Browse files
feat: connect the Tags with Loop
1 parent 64be81d commit ab48944

File tree

6 files changed

+240
-250
lines changed

6 files changed

+240
-250
lines changed

includes/gutenberg/feedzy-rss-feeds-loop-block.php

Lines changed: 146 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ private function __construct() {
4949
$this->version = Feedzy_Rss_Feeds::get_version();
5050
$this->admin = Feedzy_Rss_Feeds::instance()->get_admin();
5151
add_action( 'init', array( $this, 'register_block' ) );
52+
add_action( 'rest_api_init', array( $this, 'register_fetch_feed_endpoint' ) );
5253
add_filter( 'feedzy_loop_item', array( $this, 'apply_magic_tags' ), 10, 3 );
5354
}
5455

@@ -99,8 +100,44 @@ public function register_block() {
99100
* @return string The block content.
100101
*/
101102
public function render_callback( $attributes, $content, $block ) {
102-
$content = empty( $content ) ? ( $attributes['innerBlocksContent'] ?? '' ) : $content;
103-
$is_preview = isset( $attributes['innerBlocksContent'] ) && ! empty( $attributes['innerBlocksContent'] );
103+
$feed_items = $this->fetch_feed( $attributes );
104+
105+
if ( empty( $feed_items ) ) {
106+
return '<div>' . esc_html__( 'No feeds to display', 'feedzy-rss-feeds' ) . '</div>';
107+
}
108+
109+
if ( is_wp_error( $feed_items ) ) {
110+
return '<div>' . $feed_items->get_error_message() . '</div>';
111+
}
112+
113+
$content = empty( $content ) ? ( $attributes['innerBlocksContent'] ?? '' ) : $content;
114+
$loop = '';
115+
$column_count = isset( $attributes['layout'] ) && isset( $attributes['layout']['columnCount'] ) && ! empty( $attributes['layout']['columnCount'] ) ? $attributes['layout']['columnCount'] : 1;
116+
117+
foreach ( $feed_items as $key => $item ) {
118+
// Reference https://github.com/WordPress/gutenberg/blob/b3cda428abe895a0a97c0a6df0e0cf5c925d9208/packages/block-library/src/post-template/index.php#L113-L125
119+
$filter_block_context = static function ( $context ) use ( $item ) {
120+
$context['feedzy-rss-feeds/feedItem'] = $item;
121+
return $context;
122+
};
123+
124+
add_filter( 'render_block_context', $filter_block_context, 1 );
125+
$loop .= apply_filters( 'feedzy_loop_item', $content, $item, $block );
126+
remove_filter( 'render_block_context', $filter_block_context, 1 );
127+
}
128+
129+
return sprintf(
130+
'<div %1$s>%2$s</div>',
131+
get_block_wrapper_attributes(
132+
array(
133+
'class' => 'feedzy-loop-columns-' . $column_count,
134+
)
135+
),
136+
$loop
137+
);
138+
}
139+
140+
public function fetch_feed( $attributes ) {
104141
$feed_urls = array();
105142

106143
if ( isset( $attributes['feed']['type'] ) && 'group' === $attributes['feed']['type'] && isset( $attributes['feed']['source'] ) && is_numeric( $attributes['feed']['source'] ) ) {
@@ -110,16 +147,17 @@ public function render_callback( $attributes, $content, $block ) {
110147
$feed_urls = ! empty( $value ) ? explode( ',', $value ) : array();
111148
}
112149

113-
if ( isset( $attributes['feed']['type'] ) && 'url' === $attributes['feed']['type'] && isset( $attributes['feed']['source'] ) && is_array( $attributes['feed']['source'] ) ) {
150+
if (
151+
isset( $attributes['feed']['type'] ) && 'url' === $attributes['feed']['type'] &&
152+
isset( $attributes['feed']['source'] ) && is_array( $attributes['feed']['source'] )
153+
) {
114154
$feed_urls = $attributes['feed']['source'];
115155
}
116156

117157
if ( empty( $feed_urls ) ) {
118-
return '<div>' . esc_html__( 'No feeds to display', 'feedzy-rss-feeds' ) . '</div>';
158+
return array();
119159
}
120160

121-
$column_count = isset( $attributes['layout'] ) && isset( $attributes['layout']['columnCount'] ) && ! empty( $attributes['layout']['columnCount'] ) ? $attributes['layout']['columnCount'] : 1;
122-
123161
$default_query = array(
124162
'max' => 5,
125163
'sort' => 'default',
@@ -147,46 +185,123 @@ public function render_callback( $attributes, $content, $block ) {
147185
'filters' => wp_json_encode( $filters ),
148186
);
149187

188+
$feed = $this->admin->fetch_feed( $feed_urls, $query['refresh'], $options );
189+
150190
$sizes = array(
151191
'width' => 300,
152192
'height' => 300,
153193
);
154194

155-
$feed = $this->admin->fetch_feed( $feed_urls, $query['refresh'], $options );
156-
157195
if ( isset( $feed->error ) && ! empty( $feed->error ) ) {
158-
return '<div>' . esc_html__( 'An error occurred while fetching the feed.', 'feedzy-rss-feeds' ) . '</div>';
196+
return new \WP_Error( 'invalid_feed', __( 'No feeds to display', 'feedzy-rss-feeds' ) );
197+
}
198+
199+
return apply_filters( 'feedzy_get_feed_array', array(), $options, $feed, implode( ',', $feed_urls ), $sizes );
200+
}
201+
202+
public function register_fetch_feed_endpoint() {
203+
register_rest_route(
204+
'feedzy/v1',
205+
'/loop/feed',
206+
array(
207+
'methods' => 'GET',
208+
'callback' => array( $this, 'get_feed_items' ),
209+
'permission_callback' => function () {
210+
return is_user_logged_in();
211+
},
212+
)
213+
);
214+
}
215+
216+
/**
217+
* Get the feed items.
218+
*
219+
* @param WP_REST_Request $request
220+
* @return void
221+
*/
222+
public function get_feed_items( $request ) {
223+
// Get query parameters
224+
$params = $request->get_query_params();
225+
226+
ray( $params );
227+
228+
// Extract attributes
229+
$attributes = array();
230+
231+
// Sanitize feed data
232+
if ( isset( $params['feed'] ) ) {
233+
$attributes['feed'] = array();
234+
235+
// Sanitize feed type
236+
if ( isset( $params['feed']['type'] ) ) {
237+
$attributes['feed']['type'] = sanitize_text_field( $params['feed']['type'] );
238+
}
239+
240+
// Sanitize feed source
241+
if ( isset( $params['feed']['source'] ) ) {
242+
if ( is_array( $params['feed']['source'] ) ) {
243+
// For URL type - array of URLs
244+
$attributes['feed']['source'] = array();
245+
foreach ( $params['feed']['source'] as $url ) {
246+
$clean_url = esc_url_raw( urldecode( $url ) );
247+
if ( $clean_url ) {
248+
$attributes['feed']['source'][] = $clean_url;
249+
}
250+
}
251+
} else {
252+
// For group type - numeric ID
253+
$attributes['feed']['source'] = absint( $params['feed']['source'] );
254+
}
255+
}
159256
}
160257

161-
$feed_items = apply_filters( 'feedzy_get_feed_array', array(), $options, $feed, implode( ',', $feed_urls ), $sizes );
258+
// Sanitize query parameters
259+
if ( isset( $params['query'] ) ) {
260+
$attributes['query'] = array();
162261

163-
if ( empty( $feed_items ) ) {
164-
return '<div>' . esc_html__( 'No items to display.', 'feedzy-rss-feeds' ) . '</div>';
262+
if ( isset( $params['query']['max'] ) ) {
263+
$attributes['query']['max'] = absint( $params['query']['max'] );
264+
}
265+
266+
if ( isset( $params['query']['sort'] ) ) {
267+
$attributes['query']['sort'] = sanitize_text_field( $params['query']['sort'] );
268+
}
269+
270+
if ( isset( $params['query']['refresh'] ) ) {
271+
$attributes['query']['refresh'] = sanitize_text_field( $params['query']['refresh'] );
272+
}
165273
}
166274

167-
$loop = '';
275+
// Sanitize conditions/filters
276+
if ( isset( $params['conditions'] ) ) {
277+
$attributes['conditions'] = array();
278+
// Add specific sanitization for conditions based on your needs
279+
foreach ( $params['conditions'] as $key => $condition ) {
280+
$attributes['conditions'][ sanitize_key( $key ) ] = sanitize_text_field( $condition );
281+
}
282+
}
168283

169-
foreach ( $feed_items as $key => $item ) {
170-
// Reference https://github.com/WordPress/gutenberg/blob/b3cda428abe895a0a97c0a6df0e0cf5c925d9208/packages/block-library/src/post-template/index.php#L113-L125
171-
$filter_block_context = static function ( $context ) use ( $item ) {
172-
$context['feedzy-rss-feeds/feedItem'] = $item;
173-
return $context;
174-
};
284+
ray( 'attributes', $attributes );
175285

176-
add_filter( 'render_block_context', $filter_block_context, 1 );
177-
$loop .= apply_filters( 'feedzy_loop_item', $content, $item, $block );
178-
remove_filter( 'render_block_context', $filter_block_context, 1 );
286+
$feed_items = $this->fetch_feed( $attributes );
287+
288+
if ( is_wp_error( $feed_items) ) {
289+
wp_send_json_error( $feed_items->get_error_message() );
179290
}
180291

181-
return sprintf(
182-
'<div %1$s>%2$s</div>',
183-
get_block_wrapper_attributes(
184-
array(
185-
'class' => 'feedzy-loop-columns-' . $column_count,
186-
)
187-
),
188-
$loop
189-
);
292+
foreach ($feed_items as &$item) {
293+
if ( ! is_array( $item ) ) {
294+
continue;
295+
}
296+
297+
foreach ( $item as $key => $value ) {
298+
if ( is_array( $value ) || is_object( $value ) ) {
299+
unset( $item[ $key ] );
300+
}
301+
}
302+
}
303+
304+
return $feed_items;
190305
}
191306

192307
/**

includes/gutenberg/feedzy-rss-feeds-tag-block.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ public function render_callback( $attributes, $content, $block ) {
8585
return '';
8686
}
8787

88-
return wp_kses_post( $feed_item[ $attributes['tag'] ] );
88+
$wrapper_attributes = get_block_wrapper_attributes();
89+
90+
return sprintf(
91+
'<div %1$s>%2$s</div>',
92+
$wrapper_attributes,
93+
wp_kses_post( $feed_item[ $attributes['tag'] ] )
94+
);
8995
}
9096

9197
/**

0 commit comments

Comments
 (0)