4
4
5
5
namespace Nextgenthemes \WP ;
6
6
7
+ use WP_Error ;
8
+ use WP_REST_Request ;
9
+ use WP_REST_Response ;
7
10
use function wp_interactivity_data_wp_context as data_wp_context ;
8
11
9
12
class Settings {
10
13
/**
11
14
* The slug of the parent menu under which the settings menu will appear.
12
15
*/
13
- private string $ menu_parent_slug = ' options-general.php ' ;
16
+ private string $ menu_parent_slug ;
14
17
15
18
private string $ menu_title ;
16
19
private string $ settings_page_title ;
@@ -97,7 +100,7 @@ class Settings {
97
100
98
101
public function __construct ( array $ args ) {
99
102
100
- $ this ->menu_parent_slug = $ args ['menu_parent_slug ' ] ?? $ this -> menu_parent_slug ;
103
+ $ this ->menu_parent_slug = $ args ['menu_parent_slug ' ] ?? ' options-general.php ' ;
101
104
$ this ->base_url = trailingslashit ( $ args ['base_url ' ] );
102
105
$ this ->base_path = trailingslashit ( $ args ['base_path ' ] );
103
106
$ this ->plugin_file = $ args ['plugin_file ' ] ?? null ;
@@ -249,7 +252,7 @@ public function register_rest_route(): void {
249
252
'permission_callback ' => function () {
250
253
return current_user_can ( 'manage_options ' );
251
254
},
252
- 'callback ' => function ( \ WP_REST_Request $ request ): \ WP_REST_Response {
255
+ 'callback ' => function ( WP_REST_Request $ request ): WP_REST_Response {
253
256
$ this ->save_options ( $ request ->get_params () );
254
257
return rest_ensure_response ( __ ( 'Options saved ' , 'advanced-responsive-video-embedder ' ) );
255
258
},
@@ -288,12 +291,13 @@ public function register_rest_route(): void {
288
291
'permission_callback ' => function () {
289
292
return current_user_can ( 'manage_options ' );
290
293
},
291
- 'callback ' => function ( \WP_REST_Request $ request ) {
294
+ /** @return WP_Error|WP_REST_Response */
295
+ 'callback ' => function ( WP_REST_Request $ request ) {
292
296
293
297
$ p = $ request ->get_params ();
294
298
295
299
if ( ! in_array ( $ p ['edd_action ' ], array ( 'activate_license ' , 'deactivate_license ' , 'check_license ' ), true ) ) {
296
- return new \ WP_Error ( 'invalid_action ' , 'Invalid action ' , array ( 'status ' => 500 ) );
300
+ return new WP_Error ( 'invalid_action ' , 'Invalid action ' , array ( 'status ' => 500 ) );
297
301
}
298
302
299
303
$ options = $ this ->get_options ();
@@ -310,15 +314,60 @@ public function register_rest_route(): void {
310
314
311
315
register_rest_route (
312
316
$ this ->rest_namespace ,
313
- '/delete-oembed-cache ' ,
317
+ '/delete-caches ' ,
314
318
array (
315
319
'methods ' => 'POST ' ,
320
+ 'args ' => array (
321
+ 'type ' => array (
322
+ 'required ' => true ,
323
+ 'type ' => 'string ' ,
324
+ 'default ' => '' ,
325
+ ),
326
+ 'like ' => array (
327
+ 'required ' => false ,
328
+ 'type ' => 'string ' ,
329
+ 'default ' => '' ,
330
+ ),
331
+ 'not_like ' => array (
332
+ 'required ' => false ,
333
+ 'type ' => 'string ' ,
334
+ 'default ' => '' ,
335
+ ),
336
+ 'prefix ' => array (
337
+ 'required ' => false ,
338
+ 'type ' => 'string ' ,
339
+ 'default ' => '' ,
340
+ ),
341
+ 'delete_option ' => array (
342
+ 'required ' => false ,
343
+ 'type ' => 'string ' ,
344
+ 'default ' => '' ,
345
+ ),
346
+ ),
316
347
'permission_callback ' => function () {
317
- #return true;
318
348
return current_user_can ( 'manage_options ' );
319
349
},
320
- 'callback ' => function (): \WP_REST_Response {
321
- return rest_ensure_response ( \Nextgenthemes \ARVE \delete_oembed_cache () );
350
+ /** @return WP_Error|WP_REST_Response */
351
+ 'callback ' => function ( WP_REST_Request $ request ) {
352
+
353
+ $ p = $ request ->get_params ();
354
+
355
+ if ( ! empty ( $ p ['delete_option ' ] ) ) {
356
+ delete_option ( $ p ['delete_option ' ] );
357
+ // just do this silently and continue to so we can clear caches at the same time.
358
+ }
359
+
360
+ switch ( $ p ['type ' ] ) {
361
+ case 'oembed ' :
362
+ return rest_ensure_response ( \Nextgenthemes \ARVE \delete_oembed_cache ( $ p ['like ' ], $ p ['not_like ' ] ) );
363
+ case 'transients ' :
364
+ return rest_ensure_response ( \Nextgenthemes \ARVE \delete_transients ( $ p ['prefix ' ], $ p ['like ' ] ) );
365
+ case 'flush_object_cache ' :
366
+ case 'wp_cache_flush ' :
367
+ return rest_ensure_response ( wp_cache_flush () );
368
+ default :
369
+ return ( new WP_Error ( 'invalid_type ' , 'Invalid type ' , array ( 'status ' => 500 ) ) );
370
+ }
322
371
},
323
372
)
324
373
);
@@ -497,18 +546,13 @@ class="button button-secondary"
497
546
498
547
public function register_setting_page (): void {
499
548
500
- $ parent_slug = $ this ->menu_parent_slug ;
501
- // The HTML Document title for our settings page.
502
- $ page_title = $ this ->settings_page_title ;
503
- // The menu item title for our settings page.
504
- $ menu_title = $ this ->menu_title ;
505
- // The user permission required to view our settings page.
506
- $ capability = 'manage_options ' ;
507
- // The URL slug for our settings page.
508
- $ menu_slug = $ this ->slugged_namespace ;
509
- // The callback function for rendering our settings page HTML.
510
- $ callback = array ( $ this , 'print_admin_page ' );
511
-
512
- add_submenu_page ( $ parent_slug , $ page_title , $ menu_title , $ capability , $ menu_slug , $ callback );
549
+ add_submenu_page (
550
+ $ this ->menu_parent_slug ,
551
+ $ this ->settings_page_title , // The HTML Document title for our settings page.
552
+ $ this ->menu_title ,
553
+ 'manage_options ' , // The user permission required to view our settings page.
554
+ $ this ->slugged_namespace , // The URL slug for our settings page.
555
+ array ( $ this , 'print_admin_page ' ) // The callback function for rendering our settings page HTML.
556
+ );
513
557
}
514
558
}
0 commit comments