@@ -49,6 +49,7 @@ private function __construct() {
49
49
$ this ->version = Feedzy_Rss_Feeds::get_version ();
50
50
$ this ->admin = Feedzy_Rss_Feeds::instance ()->get_admin ();
51
51
add_action ( 'init ' , array ( $ this , 'register_block ' ) );
52
+ add_action ( 'rest_api_init ' , array ( $ this , 'register_fetch_feed_endpoint ' ) );
52
53
add_filter ( 'feedzy_loop_item ' , array ( $ this , 'apply_magic_tags ' ), 10 , 3 );
53
54
}
54
55
@@ -99,8 +100,44 @@ public function register_block() {
99
100
* @return string The block content.
100
101
*/
101
102
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 ) {
104
141
$ feed_urls = array ();
105
142
106
143
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 ) {
110
147
$ feed_urls = ! empty ( $ value ) ? explode ( ', ' , $ value ) : array ();
111
148
}
112
149
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
+ ) {
114
154
$ feed_urls = $ attributes ['feed ' ]['source ' ];
115
155
}
116
156
117
157
if ( empty ( $ feed_urls ) ) {
118
- return ' <div> ' . esc_html__ ( ' No feeds to display ' , ' feedzy-rss-feeds ' ) . ' </div> ' ;
158
+ return array () ;
119
159
}
120
160
121
- $ column_count = isset ( $ attributes ['layout ' ] ) && isset ( $ attributes ['layout ' ]['columnCount ' ] ) && ! empty ( $ attributes ['layout ' ]['columnCount ' ] ) ? $ attributes ['layout ' ]['columnCount ' ] : 1 ;
122
-
123
161
$ default_query = array (
124
162
'max ' => 5 ,
125
163
'sort ' => 'default ' ,
@@ -147,46 +185,123 @@ public function render_callback( $attributes, $content, $block ) {
147
185
'filters ' => wp_json_encode ( $ filters ),
148
186
);
149
187
188
+ $ feed = $ this ->admin ->fetch_feed ( $ feed_urls , $ query ['refresh ' ], $ options );
189
+
150
190
$ sizes = array (
151
191
'width ' => 300 ,
152
192
'height ' => 300 ,
153
193
);
154
194
155
- $ feed = $ this ->admin ->fetch_feed ( $ feed_urls , $ query ['refresh ' ], $ options );
156
-
157
195
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
+ }
159
256
}
160
257
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 ();
162
261
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
+ }
165
273
}
166
274
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
+ }
168
283
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 );
175
285
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 () );
179
290
}
180
291
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 ;
190
305
}
191
306
192
307
/**
0 commit comments