Skip to content

Commit 1857f56

Browse files
committed
better cache stuff, minor code improvements
1 parent bf44641 commit 1857f56

File tree

4 files changed

+93
-37
lines changed

4 files changed

+93
-37
lines changed

php/Video.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Nextgenthemes\ARVE;
66

7+
use WP_Error;
78
use WP_HTML_Tag_Processor;
89
use function Nextgenthemes\WP\get_url_arg;
910
use function Nextgenthemes\WP\apply_attr;
@@ -167,11 +168,6 @@ public function build_video() {
167168

168169
private function oembed_data_errors(): void {
169170

170-
if ( isset( $this->oembed_data->youtube_api_error ) ) {
171-
update_option( 'arve_youtube_api_error', $this->oembed_data->youtube_api_error );
172-
unset( $this->oembed_data->youtube_api_error );
173-
}
174-
175171
unset( $this->oembed_data->arve_error ); // ignore old errors.
176172

177173
foreach ( (array) $this->oembed_data as $key => $value ) {

php/fn-oembed.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ function filter_embed_oembed_html( $cache, string $url, array $attr, ?int $post_
8888
$oembed_data = extract_oembed_data( $cache );
8989

9090
if ( $oembed_data ) {
91-
9291
$a['url'] = $url;
9392
$a['oembed_data'] = $oembed_data;
9493
$a['origin_data'] = array(
@@ -117,12 +116,11 @@ function cache_is_old_enough( object $oembed_data ): bool {
117116

118117
function delete_oembed_caches_when_missing_data( object $oembed_data ): array {
119118

120-
$pro_active = function_exists( __NAMESPACE__ . '\Pro\oembed_data' );
121-
$result = [];
122-
$url = $oembed_data->arve_url ?? false;
123-
$provider = $oembed_data->provider ?? false;
124-
$cachetime = $oembed_data->arve_cachetime ?? false;
125-
$yt_api_error = $oembed_data->youtube_api_error ?? '';
119+
$pro_active = function_exists( __NAMESPACE__ . '\Pro\oembed_data' );
120+
$result = [];
121+
$url = $oembed_data->arve_url ?? false;
122+
$provider = $oembed_data->provider ?? false;
123+
$cachetime = $oembed_data->arve_cachetime ?? false;
126124

127125
if ( ! $provider || ! $cachetime ) {
128126
$result['delete_oembed_cache_for_provider_or_cachetime'] = delete_oembed_cache( $url );

vendor/nextgenthemes/wp-settings/includes/WP/Settings.php

+66-22
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
namespace Nextgenthemes\WP;
66

7+
use WP_Error;
8+
use WP_REST_Request;
9+
use WP_REST_Response;
710
use function wp_interactivity_data_wp_context as data_wp_context;
811

912
class Settings {
1013
/**
1114
* The slug of the parent menu under which the settings menu will appear.
1215
*/
13-
private string $menu_parent_slug = 'options-general.php';
16+
private string $menu_parent_slug;
1417

1518
private string $menu_title;
1619
private string $settings_page_title;
@@ -97,7 +100,7 @@ class Settings {
97100

98101
public function __construct( array $args ) {
99102

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';
101104
$this->base_url = trailingslashit( $args['base_url'] );
102105
$this->base_path = trailingslashit( $args['base_path'] );
103106
$this->plugin_file = $args['plugin_file'] ?? null;
@@ -249,7 +252,7 @@ public function register_rest_route(): void {
249252
'permission_callback' => function () {
250253
return current_user_can( 'manage_options' );
251254
},
252-
'callback' => function ( \WP_REST_Request $request ): \WP_REST_Response {
255+
'callback' => function ( WP_REST_Request $request ): WP_REST_Response {
253256
$this->save_options( $request->get_params() );
254257
return rest_ensure_response( __( 'Options saved', 'advanced-responsive-video-embedder' ) );
255258
},
@@ -288,12 +291,13 @@ public function register_rest_route(): void {
288291
'permission_callback' => function () {
289292
return current_user_can( 'manage_options' );
290293
},
291-
'callback' => function ( \WP_REST_Request $request ) {
294+
/** @return WP_Error|WP_REST_Response */
295+
'callback' => function ( WP_REST_Request $request ) {
292296

293297
$p = $request->get_params();
294298

295299
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 ) );
297301
}
298302

299303
$options = $this->get_options();
@@ -310,15 +314,60 @@ public function register_rest_route(): void {
310314

311315
register_rest_route(
312316
$this->rest_namespace,
313-
'/delete-oembed-cache',
317+
'/delete-caches',
314318
array(
315319
'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+
),
316347
'permission_callback' => function () {
317-
#return true;
318348
return current_user_can( 'manage_options' );
319349
},
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+
}
322371
},
323372
)
324373
);
@@ -497,18 +546,13 @@ class="button button-secondary"
497546

498547
public function register_setting_page(): void {
499548

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+
);
513557
}
514558
}

vendor/nextgenthemes/wp-settings/src/settings.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,18 @@ function setupInteractivityApi() {
171171
}
172172
} );
173173
},
174-
deleteOembedCache: () => {
175-
actions.restCall( '/delete-oembed-cache', { delete: true } );
174+
deleteCaches: () => {
175+
const context = getContext< clearCacheContext >();
176+
177+
console.log( context );
178+
179+
actions.restCall( '/delete-caches', {
180+
type: context.type,
181+
prefix: context.prefix,
182+
like: context.like,
183+
not_like: context.type,
184+
delete_option: context.delete_option,
185+
} );
176186
},
177187
// debounced version created later
178188
saveOptionsReal: () => {
@@ -383,7 +393,7 @@ interface storeInterface {
383393
inputChange: ( event: Event ) => void;
384394
checkboxChange: ( event: Event ) => void;
385395
selectImage: () => void;
386-
deleteOembedCache: () => void;
396+
deleteCaches: () => void;
387397
eddLicenseAction: () => void;
388398
resetOptionsSection: () => void;
389399
restCall: (
@@ -411,6 +421,14 @@ interface optionContext {
411421
activeTabs: { [ key: string ]: boolean };
412422
}
413423

424+
interface clearCacheContext {
425+
type: string;
426+
like: string;
427+
not_like: string;
428+
prefix: string;
429+
delete_option: string;
430+
}
431+
414432
interface configInterface {
415433
restUrl: string;
416434
nonce: string;

0 commit comments

Comments
 (0)