Skip to content

Commit 242d018

Browse files
authored
Merge pull request #2829 from Strategy11/omit_changelog_from_autoloaded_option_with_plugin_api_data
Omit changelog from autoloaded option with plugin api data and exclude invalid plugins
2 parents 8ee64a3 + d9cc216 commit 242d018

File tree

3 files changed

+113
-7
lines changed

3 files changed

+113
-7
lines changed

classes/models/FrmAddon.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,21 @@ public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
195195

196196
$item_id = $this->download_id;
197197

198-
if ( ! $item_id ) {
198+
if ( $item_id ) {
199+
$api = new FrmFormApi( $this->license );
200+
201+
// Force new API info so we can pull changelog data.
202+
// Change log data is intentionally omitted from the cached API response
203+
// to help reduce the size of the autoloaded option.
204+
$api->force_api_request();
205+
$plugins = $api->get_api_info();
206+
207+
if ( $plugins ) {
208+
$_data = $plugins[ $item_id ];
209+
}
210+
}
211+
212+
if ( empty( $plugins ) ) {
199213
$_data = array(
200214
'name' => $this->plugin_name,
201215
'excerpt' => '',
@@ -205,10 +219,6 @@ public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
205219
'low' => 'https://ps.w.org/formidable/assets/banner-1544x500.png',
206220
),
207221
);
208-
} else {
209-
$api = new FrmFormApi( $this->license );
210-
$plugins = $api->get_api_info();
211-
$_data = $plugins[ $item_id ];
212222
}
213223

214224
$_data['sections'] = array(

classes/models/FrmFormApi.php

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ class FrmFormApi {
2727
*/
2828
protected $new_days = 90;
2929

30+
/**
31+
* If true, calls to get_api_info will bypass cache.
32+
* This is set true by calling force_api_request.
33+
*
34+
* @var bool
35+
*/
36+
protected $force = false;
37+
3038
/**
3139
* @since 3.06
3240
*
@@ -84,6 +92,18 @@ public function get_cache_key() {
8492
return $this->cache_key;
8593
}
8694

95+
/**
96+
* Flag the force property as true, so the next API request bypasses cache.
97+
* This is used to pull API data for change logs, which are excluded from the cached data.
98+
*
99+
* @since x.x
100+
*
101+
* @return void
102+
*/
103+
public function force_api_request() {
104+
$this->force = true;
105+
}
106+
87107
/**
88108
* @since 3.06
89109
*
@@ -96,7 +116,12 @@ public function get_api_info() {
96116
$url .= '?l=' . urlencode( base64_encode( $this->license ) );
97117
}
98118

99-
$addons = $this->get_cached();
119+
if ( $this->force ) {
120+
$addons = false;
121+
$this->force = false;
122+
} else {
123+
$addons = $this->get_cached();
124+
}
100125

101126
if ( is_array( $addons ) ) {
102127
return $addons;
@@ -344,6 +369,8 @@ protected function get_cached_option() {
344369
* @return void
345370
*/
346371
protected function set_cached( $addons ) {
372+
$addons = $this->reduce_addon_data_before_caching( $addons );
373+
347374
$data = array(
348375
'timeout' => strtotime( $this->get_cache_timeout( $addons ), time() ),
349376
'value' => wp_json_encode( $addons ),
@@ -359,6 +386,74 @@ protected function set_cached( $addons ) {
359386
}
360387
}
361388

389+
/**
390+
* Remove certain add-on API data that we don't need to cache.
391+
* This is to help keep the option data (which is auto-loaded) small.
392+
*
393+
* @since x.x
394+
*
395+
* @param array $addons
396+
*
397+
* @return array
398+
*/
399+
private function reduce_addon_data_before_caching( $addons ) {
400+
if ( is_subclass_of( $this, 'FrmFormApi' ) ) {
401+
// We only want to modify FrmFormApi. Leave the other APIs alone for now.
402+
return $addons;
403+
}
404+
405+
$reduced_addons = array();
406+
407+
foreach ( $addons as $key => $addon ) {
408+
if ( ! is_array( $addon ) ) {
409+
$reduced_addons[ $key ] = $addon;
410+
continue;
411+
}
412+
413+
if ( ! $this->should_include_addon_in_cached_data( $addon ) ) {
414+
continue;
415+
}
416+
417+
if ( isset( $addon['changelog'] ) ) {
418+
unset( $addon['changelog'], $addon['banners'] );
419+
}
420+
421+
$reduced_addons[ $key ] = $addon;
422+
}
423+
424+
return $reduced_addons;
425+
}
426+
427+
/**
428+
* @since x.x
429+
*
430+
* @param array $addon
431+
*
432+
* @return bool True if the add-on should be included in cached data.
433+
*/
434+
private function should_include_addon_in_cached_data( $addon ) {
435+
if ( isset( $addon['version'] ) && '' === $addon['version'] ) {
436+
// If version is set but blank, the plugin is not actually live.
437+
return false;
438+
}
439+
440+
if ( isset( $addon['categories'] ) ) {
441+
if ( 'views' === $addon['slug'] ) {
442+
// Legacy views has no categories set, but we should still
443+
// include it in cache since it is a valid add-on.
444+
return true;
445+
}
446+
447+
$categories_are_empty = ! $addon['categories'] || $addon['categories'] === array( 'Strategy11' );
448+
449+
if ( $categories_are_empty ) {
450+
return false;
451+
}
452+
}
453+
454+
return true;
455+
}
456+
362457
/**
363458
* If the last check was a a rate limit, we'll need to check again sooner.
364459
*

tests/cypress/e2e/Applications/validateApplicationsPage.cy.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ describe( 'Applications page', () => {
2727
t.includes( 'the most advanced wordpress form builder' ) ||
2828
t.includes( 'more than just a wordpress form builder' ) ||
2929
t.includes( 'get more done in less time with better wordpress forms' ) ||
30-
t.includes( 'power your wordpress site like never before' )
30+
t.includes( 'power your wordpress site like never before' ) ||
31+
t.includes( 'stop patching together plugins. start building what you actually need.' )
3132
);
3233
} );
3334
} );

0 commit comments

Comments
 (0)