Skip to content

Commit efe83af

Browse files
authored
Merge pull request #271 from ProgressPlanner/filip/aaa-option-optimizer
2 parents 9944070 + 8f85333 commit efe83af

5 files changed

Lines changed: 241 additions & 2 deletions

File tree

.github/workflows/phpunit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
matrix:
2222
include:
2323
- php_version: '8.2'
24-
wp_version: '6.2'
24+
wp_version: '6.7'
2525
multisite: false
2626

2727
- php_version: '8.2'

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
= 1.10.0 =
2+
3+
Added these recommendations from Ravi:
4+
5+
* Reduce number of autoloaded options
6+
17
= 1.9.0 =
28

39
In this release we've added an integration with the **All In One Seo** plugin so you’ll now see personalized suggestions based on your current SEO configuration.

classes/suggested-tasks/class-tasks-manager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Progress_Planner\Suggested_Tasks\Providers\Fewer_Tags;
3434
use Progress_Planner\Suggested_Tasks\Providers\Remove_Terms_Without_Posts;
3535
use Progress_Planner\Suggested_Tasks\Providers\Update_Term_Description;
36+
use Progress_Planner\Suggested_Tasks\Providers\Reduce_Autoloaded_Options;
3637
use Progress_Planner\Suggested_Tasks\Providers\Unpublished_Content;
3738
use Progress_Planner\Suggested_Tasks\Providers\Collaborator;
3839
use Progress_Planner\Suggested_Tasks\Providers\Select_Timezone;
@@ -74,6 +75,7 @@ public function __construct() {
7475
new Permalink_Structure(),
7576
new Php_Version(),
7677
new Search_Engine_Visibility(),
78+
new Reduce_Autoloaded_Options(),
7779
new User_Tasks(),
7880
new Email_Sending(),
7981
new Set_Valuable_Post_Types(),

classes/suggested-tasks/providers/class-improve-pdf-handling.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class Improve_Pdf_Handling extends Tasks_Interactive {
1919
*/
2020
protected const IS_ONBOARDING_TASK = false;
2121

22-
2322
/**
2423
* The minimum number of PDF files.
2524
*
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
<?php
2+
/**
3+
* Add tasks for settings saved.
4+
*
5+
* @package Progress_Planner
6+
*/
7+
8+
namespace Progress_Planner\Suggested_Tasks\Providers;
9+
10+
/**
11+
* Add tasks to check if WP debug is enabled.
12+
*/
13+
class Reduce_Autoloaded_Options extends Tasks_Interactive {
14+
15+
/**
16+
* Whether the task is an onboarding task.
17+
*
18+
* @var bool
19+
*/
20+
protected const IS_ONBOARDING_TASK = false;
21+
22+
/**
23+
* The provider type.
24+
*
25+
* @var string
26+
*/
27+
const CATEGORY = 'maintenance';
28+
29+
/**
30+
* The provider ID.
31+
*
32+
* @var string
33+
*/
34+
const PROVIDER_ID = 'reduce-autoloaded-options';
35+
36+
/**
37+
* The popover ID.
38+
*
39+
* @var string
40+
*/
41+
const POPOVER_ID = 'reduce-autoloaded-options';
42+
43+
/**
44+
* Whether the task is dismissable.
45+
*
46+
* @var bool
47+
*/
48+
protected $is_dismissable = true;
49+
50+
/**
51+
* The number of autoloaded options.
52+
*
53+
* @var int
54+
*/
55+
private $autoloaded_options_count = null;
56+
57+
/**
58+
* The plugin active state.
59+
*
60+
* @var bool
61+
*/
62+
private $is_plugin_active = null;
63+
64+
/**
65+
* Threshold for the number of autoloaded options.
66+
*
67+
* @var int
68+
*/
69+
private $autoloaded_options_threshold = 500;
70+
71+
/**
72+
* The task priority.
73+
*
74+
* Tasks are ordered from lowest to highest priority value (0 = highest priority, 100 = lowest priority).
75+
* Use the PRIORITY_* constants defined in this class for consistency.
76+
*
77+
* @var int
78+
*/
79+
protected $priority = 50;
80+
81+
/**
82+
* The plugin path.
83+
*
84+
* @var string
85+
*/
86+
private $plugin_path = 'aaa-option-optimizer/aaa-option-optimizer.php';
87+
88+
/**
89+
* Initialize the task provider.
90+
*
91+
* @return void
92+
*/
93+
public function init() {
94+
$this->url = \admin_url( '/plugin-install.php?tab=search&s=aaa+option+optimizer' );
95+
96+
/**
97+
* Filter the autoloaded options threshold.
98+
*
99+
* @param int $threshold The threshold.
100+
*
101+
* @return int
102+
*/
103+
$this->autoloaded_options_threshold = (int) \apply_filters( 'progress_planner_reduce_autoloaded_options_threshold', $this->autoloaded_options_threshold );
104+
}
105+
106+
/**
107+
* Get the title.
108+
*
109+
* @return string
110+
*/
111+
public function get_title() {
112+
return \esc_html__( 'Reduce number of autoloaded options', 'progress-planner' );
113+
}
114+
115+
/**
116+
* Check if the task condition is satisfied.
117+
* (bool) true means that the task condition is satisfied, meaning that we don't need to add the task or task was completed.
118+
*
119+
* @return bool
120+
*/
121+
public function should_add_task() {
122+
// If the plugin is active, we don't need to add the task.
123+
if ( $this->is_plugin_active() ) {
124+
return false;
125+
}
126+
127+
return $this->get_autoloaded_options_count() > $this->autoloaded_options_threshold;
128+
}
129+
130+
/**
131+
* Check if the task is completed.
132+
*
133+
* @param string $task_id The task ID.
134+
*
135+
* @return bool
136+
*/
137+
public function is_task_completed( $task_id = '' ) {
138+
return $this->is_plugin_active() || $this->get_autoloaded_options_count() <= $this->autoloaded_options_threshold;
139+
}
140+
141+
/**
142+
* Check if the plugin is active.
143+
*
144+
* @return bool
145+
*/
146+
protected function is_plugin_active() {
147+
148+
if ( null === $this->is_plugin_active ) {
149+
if ( ! \function_exists( 'get_plugins' ) ) {
150+
// @phpstan-ignore-next-line requireOnce.fileNotFound
151+
require_once ABSPATH . 'wp-admin/includes/plugin.php';
152+
}
153+
154+
$plugins = get_plugins();
155+
$this->is_plugin_active = isset( $plugins[ $this->plugin_path ] ) && is_plugin_active( $this->plugin_path );
156+
}
157+
158+
return $this->is_plugin_active;
159+
}
160+
161+
/**
162+
* Get the number of autoloaded options.
163+
*
164+
* @return int
165+
*/
166+
protected function get_autoloaded_options_count() {
167+
global $wpdb;
168+
169+
if ( null === $this->autoloaded_options_count ) {
170+
$autoload_values = \wp_autoload_values_to_autoload();
171+
$placeholders = implode( ',', array_fill( 0, count( $autoload_values ), '%s' ) );
172+
173+
// phpcs:disable WordPress.DB
174+
$this->autoloaded_options_count = $wpdb->get_var(
175+
$wpdb->prepare( "SELECT COUNT(*) FROM `{$wpdb->options}` WHERE autoload IN ( $placeholders )", $autoload_values ) // @phpstan-ignore-line property.nonObject
176+
);
177+
178+
}
179+
180+
return $this->autoloaded_options_count;
181+
}
182+
183+
/**
184+
* Get the popover instructions.
185+
*
186+
* @return void
187+
*/
188+
public function print_popover_instructions() {
189+
echo '<p>';
190+
\printf(
191+
// translators: %d is the number of autoloaded options.
192+
\esc_html__( 'There are %d autoloaded options. If you don\'t need them, consider reducing them by installing the "AAA Option Optimizer" plugin.', 'progress-planner' ),
193+
(int) $this->get_autoloaded_options_count(),
194+
);
195+
echo '</p>';
196+
}
197+
198+
/**
199+
* Print the popover input field for the form.
200+
*
201+
* @return void
202+
*/
203+
public function print_popover_form_contents() {
204+
?>
205+
<?php if ( ! \is_multisite() && \current_user_can( 'install_plugins' ) ) : ?>
206+
<prpl-install-plugin
207+
data-plugin-name="AAA Option Optimizer"
208+
data-plugin-slug="aaa-option-optimizer"
209+
data-action="<?php echo \progress_planner()->get_plugin_installer()->is_plugin_installed( 'aaa-option-optimizer' ) ? 'activate' : 'install'; ?>"
210+
data-provider-id="<?php echo \esc_attr( self::PROVIDER_ID ); ?>"
211+
></prpl-install-plugin>
212+
<?php endif; ?>
213+
<?php
214+
}
215+
216+
/**
217+
* Add task actions specific to this task.
218+
*
219+
* @param array $data The task data.
220+
* @param array $actions The existing actions.
221+
*
222+
* @return array
223+
*/
224+
public function add_task_actions( $data = [], $actions = [] ) {
225+
$actions[] = [
226+
'priority' => 10,
227+
'html' => '<a href="#" class="prpl-tooltip-action-text" role="button" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover()">' . \esc_html__( 'Reduce', 'progress-planner' ) . '</a>',
228+
];
229+
230+
return $actions;
231+
}
232+
}

0 commit comments

Comments
 (0)