PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / includes / dashboard / class-wp-optimize-dashboard-database.php

class-wp-optimize-dashboard-database.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.7.0, at includes/dashboard/class-wp-optimize-dashboard-database.php

168 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) die('No direct access allowed');
3
4 if (!class_exists('WP_Optimize_Dashboard_Database')) :
5 /**
6 * Collects all data needed to render the Database dashboard card.
7 */
8 class WP_Optimize_Dashboard_Database implements WP_Optimize_Dashboard_Interface {
9
10 /**
11 * @return WpoDashboardDatabaseData
12 */
13 public function collect(): array {
14 $wp_optimize_commands = new WP_Optimize_Commands();
15 $status_data = $wp_optimize_commands->get_all_status_data();
16
17 $scheduled = $status_data['scheduled_optimizations_enabled'];
18 $raw_schedules = (is_array($status_data['scheduled_optimizations'])) ? $status_data['scheduled_optimizations'] : array();
19 $active_schedules = array_filter($raw_schedules, function ($s) {
20 return is_array($s) && isset($s['status']) && is_scalar($s['status']) && 1 === absint($s['status']);
21 });
22 $schedule_count = $scheduled ? count($active_schedules) : 0;
23
24 $last_schedule_type = '';
25 if (!empty($active_schedules)) {
26 $last_event = end($active_schedules);
27 if (is_array($last_event) && !empty($last_event['schedule_type'])) {
28 $schedule_type = $last_event['schedule_type'];
29 $last_schedule_type = is_scalar($schedule_type) ? ucfirst(substr((string) $schedule_type, 4)) : '';
30 }
31 }
32
33 if ('' === $last_schedule_type && $scheduled) {
34 $schedule_type_id = WP_Optimize()->get_options()->get_option('schedule-type', 'wpo_weekly');
35 if ('wpo_otherweekly' === $schedule_type_id) $schedule_type_id = 'wpo_fortnightly';
36 $schedule_types = WP_Optimize::get_schedule_types();
37 $last_schedule_type = isset($schedule_types[$schedule_type_id]) ? $schedule_types[$schedule_type_id] : '';
38 }
39
40 $schedules_summary = $this->build_schedules_summary($scheduled, $active_schedules, $last_schedule_type);
41
42 $next_ts = $status_data['next_optimization_timestamp'];
43 $next_human = (is_int($next_ts) && $next_ts > 0) ? date_i18n('F j, Y g:i A', $next_ts) : __('Not scheduled', 'wp-optimize');
44 $last_run = is_numeric($status_data['last_optimized_ts']) ? (int) $status_data['last_optimized_ts'] : 0;
45 $last_cleaned_bytes = floatval(WP_Optimize()->get_options()->get_option('last-run-cleaned-bytes', '0'));
46 $last_cleaned_bytes_formatted = size_format((int) $last_cleaned_bytes, 1) ?: '';
47 $last_cleaned_human = $last_cleaned_bytes > 0 ? $last_cleaned_bytes_formatted : '';
48 $last_run_human = $last_run > 0 ? WP_Optimize_Utils::human_time_diff_or_never($last_run) : '';
49
50 return array(
51 'active' => $scheduled || !empty($last_run),
52 'label' => __('Database', 'wp-optimize'),
53 'scheduled' => (bool) $scheduled,
54 'schedule_count' => $schedule_count,
55 'schedule_label' => $last_schedule_type,
56 'schedules_summary' => $schedules_summary,
57 'next_run_human' => $next_human,
58 'backup_before_cleanup' => 'true' === WP_Optimize()->get_options()->get_option('enable-auto-backup-scheduled', 'false'),
59 'updraftplus_active' => class_exists('UpdraftPlus'),
60 'last_run_human' => $last_run_human,
61 'last_cleaned_human' => $last_cleaned_human,
62 );
63 }
64
65 /**
66 * Builds up to 2 schedule summary rows for the dashboard card.
67 *
68 * @param bool $scheduled
69 * @param array<mixed, mixed> $active_schedules
70 * @param string $free_label Fallback label for the free-tier single schedule.
71 * @return array<int, array{frequency: string, tasks_label: string}>
72 */
73 private function build_schedules_summary(bool $scheduled, array $active_schedules, string $free_label): array {
74 if (!$scheduled) return array();
75
76 $schedule_types = WP_Optimize::get_schedule_types();
77
78 // Premium: use actual schedule entries.
79 if (!empty($active_schedules)) {
80 $task_labels = $this->get_optimization_labels();
81 $rows = array();
82
83 foreach (array_values($active_schedules) as $schedule) {
84 if (count($rows) >= 2) break;
85 if (!is_array($schedule)) continue;
86
87 $type_id = isset($schedule['schedule_type']) && is_string($schedule['schedule_type']) ? $schedule['schedule_type'] : '';
88 $frequency = isset($schedule_types[$type_id]) ? (string) $schedule_types[$type_id] : ucfirst(substr($type_id, 4));
89
90 $task_ids = isset($schedule['optimization']) && is_array($schedule['optimization']) ? $schedule['optimization'] : array();
91 $names = array();
92 foreach ($task_ids as $id) {
93 if (!is_string($id)) continue;
94 $names[] = isset($task_labels[$id]) ? $task_labels[$id] : $id;
95 }
96
97 $rows[] = array(
98 'frequency' => $frequency,
99 'tasks_label' => $this->format_task_list($names),
100 );
101 }
102 return $rows;
103 }
104
105 // Free: single schedule — read enabled tasks from the 'auto' option.
106 $auto_options = WP_Optimize()->get_options()->get_option('auto');
107 $task_labels = $this->get_optimization_labels();
108 $enabled_names = array();
109 if (is_array($auto_options)) {
110 foreach ($auto_options as $auto_id => $value) {
111 if (!is_scalar($value) || 'true' !== (string) $value) continue;
112 if (!is_string($auto_id)) continue;
113 $enabled_names[] = isset($task_labels[$auto_id]) ? $task_labels[$auto_id] : $auto_id;
114 }
115 }
116 return array(
117 array(
118 'frequency' => $free_label,
119 'tasks_label' => $this->format_task_list($enabled_names),
120 ),
121 );
122 }
123
124 /**
125 * Returns a map of ID → human label.
126 * Indexes by both the file-based ID (used by premium schedules) and get_auto_id()
127 * (used by the free 'auto' option), so both lookup paths resolve correctly.
128 *
129 * @return array<string, string>
130 */
131 private function get_optimization_labels(): array {
132 $optimizer = WP_Optimize()->get_optimizer();
133 $optimizations = $optimizer->get_optimizations();
134 $map = array();
135 foreach ($optimizations as $id => $opt) {
136 if (!is_object($opt)) continue;
137 $label = method_exists($opt, 'settings_label') ? (string) $opt->settings_label() : (string) $id;
138 $map[(string) $id] = $label;
139 $auto_id = method_exists($opt, 'get_auto_id') ? (string) $opt->get_auto_id() : (string) $id;
140 $map[$auto_id] = $label;
141 }
142 return $map;
143 }
144
145 /**
146 * Formats up to 3 task names; appends "..." if more exist.
147 *
148 * @param string[] $names
149 * @return string
150 */
151 private function format_task_list(array $names): string {
152 if (empty($names)) return '';
153 $shown = array_slice($names, 0, 3);
154 $label = '';
155 $count = count($shown);
156 if (1 === $count) {
157 $label = $shown[0];
158 } elseif (2 === $count) {
159 $label = $shown[0] . ' & ' . $shown[1];
160 } else {
161 $label = $shown[0] . ', ' . $shown[1] . ' & ' . $shown[2];
162 }
163 if (count($names) > 3) $label .= ' ...';
164 return $label;
165 }
166 }
167 endif;
168