@@ -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,27 +100,65 @@ 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 ' ] );
104
- $ feed_urls = array ();
103
+ $ feed_items = $ this ->fetch_feed ( $ attributes );
105
104
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
+ ) {
107
145
$ group = $ attributes ['feed ' ]['source ' ];
108
146
$ value = get_post_meta ( $ group , 'feedzy_category_feed ' , true );
109
147
$ value = trim ( $ value );
110
- $ feed_urls = !empty ( $ value ) ? explode ( ', ' , $ value ) : array ();
148
+ $ feed_urls = ! empty ( $ value ) ? explode ( ', ' , $ value ) : array ();
111
149
}
112
150
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
+ ) {
114
155
$ feed_urls = $ attributes ['feed ' ]['source ' ];
115
156
}
116
157
117
158
if ( empty ( $ feed_urls ) ) {
118
- return ' <div> ' . esc_html__ ( ' No feeds to display ' , ' feedzy-rss-feeds ' ) . ' </div> ' ;
159
+ return array () ;
119
160
}
120
161
121
- $ column_count = isset ($ attributes ['layout ' ]) && isset ($ attributes ['layout ' ]['columnCount ' ]) && !empty ($ attributes ['layout ' ]['columnCount ' ]) ? $ attributes ['layout ' ]['columnCount ' ] : 1 ;
122
-
123
162
$ default_query = array (
124
163
'max ' => 5 ,
125
164
'sort ' => 'default ' ,
@@ -147,44 +186,123 @@ public function render_callback( $attributes, $content, $block ) {
147
186
'filters ' => wp_json_encode ( $ filters ),
148
187
);
149
188
189
+ $ feed = $ this ->admin ->fetch_feed ( $ feed_urls , $ query ['refresh ' ], $ options );
190
+
150
191
$ sizes = array (
151
192
'width ' => 300 ,
152
193
'height ' => 300 ,
153
194
);
154
195
155
- $ feed = $ this ->admin ->fetch_feed ( $ feed_urls , $ query ['refresh ' ], $ options );
156
-
157
196
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
+ }
159
257
}
160
258
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 ();
162
262
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
+ }
165
274
}
166
275
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
+ }
168
284
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 );
175
286
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 () );
179
291
}
180
292
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 ;
188
306
}
189
307
190
308
/**
0 commit comments