Skip to content

Commit 1dfac4e

Browse files
feat: connect the Tags with Loop
1 parent e1ad8b4 commit 1dfac4e

File tree

6 files changed

+244
-251
lines changed

6 files changed

+244
-251
lines changed

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

Lines changed: 150 additions & 32 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,27 +100,65 @@ 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'] );
104-
$feed_urls = array();
103+
$feed_items = $this->fetch_feed( $attributes );
105104

106-
if ( isset( $attributes['feed']['type'] ) && 'group' === $attributes['feed']['type'] && isset( $attributes['feed']['source'] ) && is_numeric( $attributes['feed']['source'] ) ) {
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( array(
132+
'class' => 'feedzy-loop-columns-' . $column_count,
133+
) ),
134+
$loop
135+
);
136+
}
137+
138+
public function fetch_feed( $attributes ) {
139+
$feed_urls = array();
140+
141+
if (
142+
isset( $attributes['feed']['type'] ) && 'group' === $attributes['feed']['type'] &&
143+
isset( $attributes['feed']['source'] ) && is_numeric( $attributes['feed']['source'] )
144+
) {
107145
$group = $attributes['feed']['source'];
108146
$value = get_post_meta( $group, 'feedzy_category_feed', true );
109147
$value = trim( $value );
110-
$feed_urls = !empty( $value ) ? explode( ',', $value ) : array();
148+
$feed_urls = ! empty( $value ) ? explode( ',', $value ) : array();
111149
}
112150

113-
if ( isset( $attributes['feed']['type'] ) && 'url' === $attributes['feed']['type'] && isset( $attributes['feed']['source'] ) && is_array( $attributes['feed']['source'] ) ) {
151+
if (
152+
isset( $attributes['feed']['type'] ) && 'url' === $attributes['feed']['type'] &&
153+
isset( $attributes['feed']['source'] ) && is_array( $attributes['feed']['source'] )
154+
) {
114155
$feed_urls = $attributes['feed']['source'];
115156
}
116157

117158
if ( empty( $feed_urls ) ) {
118-
return '<div>' . esc_html__( 'No feeds to display', 'feedzy-rss-feeds' ) . '</div>';
159+
return array();
119160
}
120161

121-
$column_count = isset($attributes['layout']) && isset($attributes['layout']['columnCount']) && !empty($attributes['layout']['columnCount']) ? $attributes['layout']['columnCount'] : 1;
122-
123162
$default_query = array(
124163
'max' => 5,
125164
'sort' => 'default',
@@ -147,44 +186,123 @@ public function render_callback( $attributes, $content, $block ) {
147186
'filters' => wp_json_encode( $filters ),
148187
);
149188

189+
$feed = $this->admin->fetch_feed( $feed_urls, $query['refresh'], $options );
190+
150191
$sizes = array(
151192
'width' => 300,
152193
'height' => 300,
153194
);
154195

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

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

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

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

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-
};
285+
ray( 'attributes', $attributes );
175286

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 );
287+
$feed_items = $this->fetch_feed( $attributes );
288+
289+
if ( is_wp_error( $feed_items) ) {
290+
wp_send_json_error( $feed_items->get_error_message() );
179291
}
180292

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

190308
/**

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

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

92-
return wp_kses_post( $feed_item[ $attributes['tag'] ] );
92+
$wrapper_attributes = get_block_wrapper_attributes();
93+
94+
return sprintf(
95+
'<div %1$s>%2$s</div>',
96+
$wrapper_attributes,
97+
wp_kses_post( $feed_item[ $attributes['tag'] ] )
98+
);
9399
}
94100

95101
/**

0 commit comments

Comments
 (0)