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
← All changes | includes/class-wp-optimize-commands.php +333 -53 4.5.4 → 4.7.0 View file →
@@ -80,9 +80,9 @@
80 80 *
81 81 * @return WP_Error|void
82 82 */
83 83 public function download_csv() {
84 - if (!current_user_can(WP_Optimize()->capability_required())) {
84 + if (!WP_Optimize()->current_user_can()) {
85 85 return $this->request_error('insufficient_privilege');
86 86 }
87 87
88 88 WP_Optimize()->get_optimizer()->get_optimization('images')->output_csv();
@@ -95,9 +95,9 @@
95 95 * @param array $params Parameters needed for the requested action
96 96 * @return array|WP_Error
97 97 */
98 98 public function get_section($params) {
99 - if (!current_user_can(WP_Optimize()->capability_required())) {
99 + if (!WP_Optimize()->current_user_can()) {
100 100 return $this->request_error('insufficient_privilege');
101 101 }
102 102
103 103 $section_id = $params['section'];
@@ -124,9 +124,9 @@
124 124 *
125 125 * @return array|WP_Error
126 126 */
127 127 public function get_database_tabs_info() {
128 - if (!current_user_can(WP_Optimize()->capability_required())) {
128 + if (!WP_Optimize()->current_user_can()) {
129 129 return $this->request_error('insufficient_privilege');
130 130 }
131 131
132 132 return $this->get_database_tabs();
@@ -138,9 +138,9 @@
138 138 * @param array $params Parameters needed for the requested action
139 139 * @return array|WP_Error
140 140 */
141 141 public function updraft_smush_ajax($params) {
142 - if (!current_user_can(WP_Optimize()->capability_required())) {
142 + if (!WP_Optimize()->current_user_can()) {
143 143 return $this->request_error('insufficient_privilege');
144 144 }
145 145
146 146 $subaction = $params['subaction'];
@@ -187,9 +187,9 @@
187 187 * @param array $params Parameters needed for the requested action
188 188 * @return array|WP_Error
189 189 */
190 190 public function handle_ajax_requests($params) {
191 - if (!current_user_can(WP_Optimize()->capability_required())) {
191 + if (!WP_Optimize()->current_user_can()) {
192 192 return $this->request_error('insufficient_privilege');
193 193 }
194 194
195 195 $subaction = $params['subaction'];
@@ -195,9 +195,9 @@
195 195 $subaction = $params['subaction'];
196 196 $data = $params['data'];
197 197
198 198 $response = array();
199 - if (is_multisite() && !current_user_can('manage_network_options')) {
199 + if (is_multisite() && !WP_Optimize()->current_user_can('manage_network_options')) {
200 200 $allowed_multisite_commands = apply_filters('wpo_multisite_allowed_commands', array('check_server_status', 'compress_single_image', 'restore_single_image'));
201 201
202 202 if (!in_array($subaction, $allowed_multisite_commands)) {
203 203 return $this->request_error('network_admin_only');
@@ -278,9 +278,9 @@
278 278 * @param array $params Parameters needed for the requested action
279 279 * @return array|WP_Error
280 280 */
281 281 public function get_posts_list($params) {
282 - if (!current_user_can(WP_Optimize()->capability_required())) {
282 + if (!WP_Optimize()->current_user_can()) {
283 283 return $this->request_error('insufficient_privilege');
284 284 }
285 285
286 286 $response = array();
@@ -372,13 +372,18 @@
372 372 private function load_action_hooks_for_content($section_id) {
373 373 $wpo = WP_Optimize();
374 374 $wpo_admin = $wpo->get_admin_instance();
375 375
376 + // dashboard
377 + if ('dashboard' === $section_id) {
378 + add_action('wp_optimize_admin_page_WP-Optimize_dashboard', array($wpo_admin, 'output_dashboard'), 20);
379 + }
380 +
376 381 // Database
377 382 if ('database' === $section_id) {
378 - add_action('wp_optimize_admin_page_WP-Optimize_optimize', array($wpo_admin, 'output_database_optimize_tab'), 20);
379 - add_action('wp_optimize_admin_page_WP-Optimize_tables', array($wpo_admin, 'output_database_tables_tab'), 20);
380 - add_action('wp_optimize_admin_page_WP-Optimize_settings', array($wpo_admin, 'output_database_settings_tab'), 20);
383 + add_action('wp_optimize_admin_page_wpo_database_optimize', array($wpo_admin, 'output_database_optimize_tab'), 20);
384 + add_action('wp_optimize_admin_page_wpo_database_tables', array($wpo_admin, 'output_database_tables_tab'), 20);
385 + add_action('wp_optimize_admin_page_wpo_database_settings', array($wpo_admin, 'output_database_settings_tab'), 20);
381 386 }
382 387
383 388 // Images
384 389 if ('images' === $section_id) {
@@ -453,9 +458,9 @@
453 458 */
454 459 private function get_section_to_tab_name($section_id) {
455 460 $tab = '';
456 461 switch ($section_id) {
457 - case 'database':
462 + case 'dashboard':
458 463 $tab = 'WP-Optimize';
459 464 break;
460 465 default:
461 466 $tab = 'wpo_'.$section_id;
@@ -505,9 +510,9 @@
505 510 * @param ?string $message_key The key to use to query any translatable message if applicable
506 511 * @param array $additional_data Any additional data to return
507 512 * @return WP_Error
508 513 */
509 - private function request_error($code, $message_key = null, $additional_data = array()) {
514 + public function request_error($code, $message_key = null, $additional_data = array()) {
510 515 global $updraftcentral_host_plugin;
511 516
512 517 $error_message = $updraftcentral_host_plugin->retrieve_show_message($code);
513 518 if (!is_null($message_key) && $code !== $message_key) {
@@ -726,8 +731,12 @@
726 731 $optimization = $this->optimizer->get_optimization($optimization_id, $data);
727 732
728 733 $result = is_a($optimization, 'WP_Optimization') ? $optimization->do_optimization() : null;
729 734
735 + if (null !== $result) {
736 + WP_Optimize()->get_options()->update_option('last-optimized', time());
737 + }
738 +
730 739 $results = array(
731 740 'result' => $result,
732 741 'messages' => array(),
733 742 'errors' => array(),
@@ -862,9 +871,22 @@
862 871 *
863 872 * @return array
864 873 */
865 874 public function get_database_tabs() {
866 - return array_merge(array('optimizations' => $this->get_optimizations_table(), 'does_server_allow_table_optimization' => WP_Optimize()->get_server_compatibility_instance()->does_server_allow_table_optimization()), $this->get_table_list());
875 + $optimizer = $this->optimizer;
876 + $scan_pending = $optimizer->is_table_count_too_high() && !$optimizer->is_large_table_count_scan_complete();
877 +
878 + $result = array(
879 + 'optimizations' => $this->get_optimizations_table(),
880 + 'does_server_allow_table_optimization' => WP_Optimize()->get_server_compatibility_instance()->does_server_allow_table_optimization(),
881 + 'scan_pending' => $scan_pending,
882 + );
883 +
884 + if (!$scan_pending) {
885 + $result = array_merge($result, $this->get_table_list());
886 + }
887 +
888 + return $result;
867 889 }
868 890
869 891 /**
870 892 * Do action wp_optimize_after_optimizations
@@ -955,13 +977,28 @@
955 977 *
956 978 * @return array
957 979 */
958 980 public function get_gzip_compression_status() {
959 - $status = WP_Optimize()->get_gzip_compression()->is_gzip_compression_enabled(true);
981 + $status = WP_Optimize()->get_gzip_compression()->is_gzip_compression_enabled(false);
960 982 return is_wp_error($status) ? array('error' => __('We could not determine if Gzip compression is enabled.', 'wp-optimize'), 'code' => $status->get_error_code(), 'message' => $status->get_error_message()) : array('status' => $status);
961 983 }
962 984
963 985 /**
986 + * Get compression test results html formatted
987 + *
988 + * @return array
989 + */
990 + public function get_compression_test_results_html() {
991 + $results_html = WP_Optimize()->get_gzip_compression()->get_compression_test_results_html();
992 + $gzip_compression_control_panel = WP_Optimize_Admin::instance()->get_gzip_compression_control_panel();
993 +
994 + return array(
995 + 'results_html' => $results_html,
996 + 'gzip_compression_control_panel' => $gzip_compression_control_panel,
997 + );
998 + }
999 +
1000 + /**
964 1001 * Import WP-Optimize settings.
965 1002 *
966 1003 * @param array $params array with 'settings' item where 'settings' json-encoded string.
967 1004 *
@@ -1175,8 +1212,64 @@
1175 1212 );
1176 1213 }
1177 1214
1178 1215 /**
1216 + * Create background tasks and execute it in sequenced AJAX calls
1217 + *
1218 + * @return array
1219 + */
1220 + public function load_tables_in_batches() {
1221 + $optimizer = WP_Optimizer::instance();
1222 + $options = WP_Optimize()->get_options();
1223 +
1224 + $start_clean = $optimizer->maybe_restart_scan_job();
1225 +
1226 + if (true == (bool) $start_clean) {
1227 +
1228 + $tables = array();
1229 +
1230 + $tables_count = $optimizer->scan_job_init();
1231 +
1232 + $offset = 0;
1233 + while ($offset < $tables_count) {
1234 + $optimizer->add_load_tables_task($offset);
1235 +
1236 + $offset += WP_Optimizer::HIGH_TABLE_COUNT_THRESHOLD;
1237 + }
1238 + $optimizer->flush_tasks();
1239 + } else {
1240 + $tables = $options->get_option(WP_Optimizer::TABLES_LIST_OPTION_NAME);
1241 + $tables = is_array($tables) ? $tables : array();
1242 + $tables_count = $options->get_option(WP_Optimizer::TABLES_LIST_COUNT_OPTION_NAME);
1243 + $tables_count = false !== $tables_count ? $tables_count : 0;
1244 + }
1245 +
1246 + $optimizer->do_next_load_table_task();
1247 +
1248 + if (!$optimizer->is_task_queue_empty() || $start_clean) {
1249 + $continue = true;
1250 + } else {
1251 + $continue = false;
1252 + }
1253 +
1254 + $done = 0;
1255 + if (false !== $tables) {
1256 + $done = count($tables);
1257 + }
1258 +
1259 + if (false === $tables_count) {
1260 + $tables_count = 0;
1261 + }
1262 +
1263 + return array(
1264 + 'success' => true,
1265 + 'continue' => $continue,
1266 + 'total' => $tables_count,
1267 + 'done' => $done
1268 + );
1269 + }
1270 +
1271 + /**
1179 1272 * Build the HTML for the status report tab
1180 1273 *
1181 1274 * @return array
1182 1275 */
@@ -1359,9 +1452,9 @@
1359 1452 $table_info['Formatted_Data_free'] = $wp_optimize->format_size($tablestatus->Data_free);
1360 1453 $table_info['Data_should_be_cleared'] = intval($tablestatus->Data_free) > $small_overhead_size ? 1 : 0;
1361 1454 $table_info['wp_core_table'] = $tablestatus->wp_core_table;
1362 1455
1363 - $table_list[] = $table_info;
1456 + $table_list[$tablestatus->Name] = $table_info;
1364 1457
1365 1458 $row_usage += $tablestatus->Rows;
1366 1459 $data_usage += $tablestatus->Data_length;
1367 1460 $index_usage += $tablestatus->Index_length;
@@ -1412,8 +1505,160 @@
1412 1505 );
1413 1506 }
1414 1507
1415 1508 /**
1509 + * [For UpdraftCentral] Returns selected WP-Optimize toggleable settings optimized for quick access
1510 + *
1511 + * @return WP_Error|array
1512 + */
1513 + private function get_quick_settings_data() {
1514 + if (!WP_Optimize()->current_user_can()) {
1515 + return $this->request_error('insufficient_privilege');
1516 + }
1517 +
1518 + $options = WP_Optimize()->get_options();
1519 + $default_lazyload_settings = array(
1520 + 'images' => false,
1521 + 'backgrounds' => false,
1522 + 'iframes' => false,
1523 + 'youtube_preview' => false,
1524 + 'skip_classes' => '',
1525 + );
1526 +
1527 + $lazyload = wp_parse_args($options->get_option('lazyload', array()), $default_lazyload_settings);
1528 + foreach ($lazyload as $key => $value) {
1529 + if ('skip_classes' !== $key) {
1530 + $lazyload[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN);
1531 + }
1532 + }
1533 +
1534 + $trackbacks = $options->get_option('trackbacks_action', array());
1535 + $comments = $options->get_option('comments_action', array());
1536 +
1537 + $gzip_compression_enabled = WP_Optimize()->get_gzip_compression()->is_gzip_compression_enabled(false);
1538 +
1539 + $settings = array(
1540 + 'scheduled_cleanup' => 'true' === $options->get_option('schedule'),
1541 + 'backup_enabled' => 'true' === $options->get_option('enable-auto-backup', 'false'),
1542 + 'comments' => (!empty($comments) && $comments['action']) ? true : false,
1543 + 'trackbacks' => (!empty($trackbacks) && $trackbacks['action']) ? true : false,
1544 + 'autosmush' => $options->get_option('autosmush'),
1545 + 'page_caching' => WP_Optimize()->get_page_cache()->is_enabled(),
1546 + 'minify' => wp_optimize_minify_config()->get('enabled'),
1547 + 'image_dimensions' => $options->get_option('image_dimensions'),
1548 + '404_detector' => $options->get_option('404_detector'),
1549 + 'webp_conversion' => $options->get_option('webp_conversion'),
1550 + 'gzip_compression' => (!is_wp_error($gzip_compression_enabled) && $gzip_compression_enabled) ? true : false,
1551 + );
1552 +
1553 + $settings = array_map(function($value) {
1554 + return filter_var($value, FILTER_VALIDATE_BOOLEAN);
1555 + }, $settings);
1556 +
1557 + return array_merge($settings, array('lazyload' => $lazyload));
1558 + }
1559 +
1560 + /**
1561 + * [For UpdraftCentral] Saves the selected WP-Optimize toggleable settings
1562 + *
1563 + * @param array $settings Parameters required for the requested action
1564 + *
1565 + * @return WP_Error|array
1566 + */
1567 + public function save_quick_settings($settings) {
1568 + if (!WP_Optimize()->current_user_can()) {
1569 + return $this->request_error('insufficient_privilege');
1570 + }
1571 +
1572 + if (!empty($settings) && is_array($settings)) {
1573 + $settings = array_map(function($value) {
1574 + return filter_var($value, FILTER_VALIDATE_BOOLEAN);
1575 + }, $settings);
1576 + }
1577 +
1578 + try {
1579 +
1580 + if (isset($settings['enable-404-detector'])) {
1581 + WP_Optimize()->get_options()->update_option('404_detector', (int) $settings['enable-404-detector']);
1582 + }
1583 +
1584 + if (isset($settings['enable-auto-backup'])) {
1585 + WP_Optimize()->get_options()->update_option('enable-auto-backup', $settings['enable-auto-backup'] ? 'true' : 'false');
1586 +
1587 + WP_Optimize()->get_options()->update_option('enable-auto-backup-scheduled', $settings['enable-auto-backup'] ? 'true' : 'false');
1588 + }
1589 +
1590 + if (isset($settings['enable-auto-compression'])) {
1591 + Updraft_Smush_Manager()->update_smush_options(array('autosmush' => (int) $settings['enable-auto-compression']));
1592 + }
1593 +
1594 + if (isset($settings['enable-comments'])) {
1595 + $this->enable_or_disable_feature(array(
1596 + 'type' => 'comments',
1597 + 'enable' => $settings['enable-comments'],
1598 + ));
1599 + }
1600 +
1601 + if (isset($settings['enable-trackbacks'])) {
1602 + $this->enable_or_disable_feature(array(
1603 + 'type' => 'trackbacks',
1604 + 'enable' => $settings['enable-trackbacks'],
1605 + ));
1606 + }
1607 +
1608 + if (isset($settings['enable-minify'])) {
1609 + wp_optimize_minify_config()->update(array('enabled' => $settings['enable-minify']));
1610 + }
1611 +
1612 + if (isset($settings['enable-page-caching'])) {
1613 + $wpo_page_cache = WP_Optimize()->get_page_cache();
1614 +
1615 + if ($settings['enable-page-caching']) {
1616 + $wpo_page_cache->enable(true);
1617 + } else {
1618 + $wpo_page_cache->disable();
1619 + }
1620 + }
1621 +
1622 + if (isset($settings['enable-scheduled-cleanup'])) {
1623 + WP_Optimize()->get_options()->update_option('schedule', $settings['enable-scheduled-cleanup'] ? 'true' : 'false');
1624 + }
1625 +
1626 + if (isset($settings['enable-webp-conversion'])) {
1627 + WP_Optimize()->get_webp_instance()->save_webp_settings(array('webp_conversion' => $settings['enable-webp-conversion']));
1628 + }
1629 +
1630 + if (isset($settings['enable-images-dimensions'])) {
1631 + WP_Optimize()->get_options()->update_option('image_dimensions', (int) $settings['enable-images-dimensions']);
1632 + }
1633 +
1634 + if (isset($settings['enable-lazy-loading'])) {
1635 + $value = (int) $settings['enable-lazy-loading'];
1636 + $options = WP_Optimize()->get_options();
1637 +
1638 + $lazyload = $options->get_option('lazyload');
1639 + $lazy_settings = array(
1640 + 'images' => $value,
1641 + 'iframes' => $value,
1642 + 'backgrounds' => $value,
1643 + 'youtube_preview' => $value,
1644 + 'skip_classes' => !empty($lazyload['skip_classes']) ? $lazyload['skip_classes'] : '',
1645 + );
1646 + $options->save_lazy_load_settings(array('lazyload' => $lazy_settings));
1647 + }
1648 +
1649 + if (isset($settings['enable-gzip-compression'])) {
1650 + $this->enable_gzip_compression(array('enable' => $settings['enable-gzip-compression']));
1651 + }
1652 +
1653 + } catch (Throwable $e) {
1654 + return $this->request_error('quick_settings_save_error', null, array('message' => $e->getMessage(), 'settings' => $this->get_quick_settings_data()));
1655 + }
1656 +
1657 + return array('settings' => $this->get_quick_settings_data());
1658 + }
1659 +
1660 + /**
1416 1661 * Get the data of wp-optimize widget.
1417 1662 *
1418 1663 * @return array
1419 1664 */
@@ -1440,14 +1685,21 @@
1440 1685 $optimization_data['run_sort_order'] = $optimization->get_run_sort_order();
1441 1686
1442 1687 // Check if the DOM is optimize-db to generate a list of tables.
1443 1688 if ('optimize-db' == $optimization_data['dom_id']) {
1444 - $table_list = $this->optimizer->get_table_information();
1445 -
1446 - // Make sure that optimization_table_inno_db is set.
1447 - if ($table_list['inno_db_tables'] > 0 && 0 === $table_list['is_optimizable'] && 0 === $table_list['non_inno_db_tables']) {
1689 + // Disable optimization when too many tables exist but the batch scan hasn't run yet.
1690 + // Running it now would silently optimize 0 tables because get_tables() returns empty.
1691 + if ($this->optimizer->is_table_count_too_high() && !$this->optimizer->is_large_table_count_scan_complete()) {
1448 1692 $optimization_data['disable_optimization_button'] = true;
1449 1693 $optimization_data['activated'] = '';
1694 + } else {
1695 + $table_list = $this->optimizer->get_table_information();
1696 +
1697 + // Make sure that optimization_table_inno_db is set.
1698 + if ($table_list['inno_db_tables'] > 0 && 0 === $table_list['is_optimizable'] && 0 === $table_list['non_inno_db_tables']) {
1699 + $optimization_data['disable_optimization_button'] = true;
1700 + $optimization_data['activated'] = '';
1701 + }
1450 1702 }
1451 1703 }
1452 1704
1453 1705 $optimization_data['id'] = $id;
@@ -1526,9 +1778,9 @@
1526 1778 * Get all status data.
1527 1779 *
1528 1780 * @return array
1529 1781 */
1530 - private function get_all_status_data() {
1782 + public function get_all_status_data() {
1531 1783 $wp_optimize = WP_Optimize();
1532 1784
1533 1785 $retention_enabled = $this->options->get_option('retention-enabled', 'false') === 'true';
1534 1786 $retention_period = $this->options->get_option('retention-period', '2');
@@ -1537,8 +1789,9 @@
1537 1789 $revisions_retention_enabled = $this->options->get_option('revisions-retention-enabled', 'false') === 'true';
1538 1790 $revisions_retention_count = $this->options->get_option('revisions-retention-count', '2');
1539 1791
1540 1792 $last_optimized = $this->options->get_option('last-optimized', 'Never');
1793 + $last_optimized_ts = $last_optimized;
1541 1794
1542 1795 if (!empty($last_optimized) && is_numeric($last_optimized)) {
1543 1796 $last_optimized = date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $last_optimized + (get_option('gmt_offset') * HOUR_IN_SECONDS));
1544 1797 }
@@ -1543,8 +1796,9 @@
1543 1796 $last_optimized = date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $last_optimized + (get_option('gmt_offset') * HOUR_IN_SECONDS));
1544 1797 }
1545 1798
1546 1799 $scheduled_optimizations_enabled = false;
1800 + $scheduled_optimizations = array();
1547 1801
1548 1802 if (WP_Optimize::is_premium()) {
1549 1803 $wp_optimize_premium = WP_Optimize_Premium();
1550 1804 $scheduled_optimizations = $wp_optimize_premium->get_scheduled_optimizations();
@@ -1590,8 +1844,9 @@
1590 1844 }
1591 1845
1592 1846 return array(
1593 1847 'last_optimized' => $last_optimized,
1848 + 'last_optimized_ts' => $last_optimized_ts,
1594 1849 'total_cleaned' => $total_cleaned,
1595 1850 'total_cleanup_size' => $total_cleanup_size,
1596 1851 'corrupted_tables_count' => $corrupted_tables_count,
1597 1852 'admin_page_url' => $admin_page_url,
@@ -1600,8 +1855,9 @@
1600 1855 'revisions_retention_enabled' => $revisions_retention_enabled,
1601 1856 'revisions_retention_count' => $revisions_retention_count,
1602 1857 'scheduled_optimizations_enabled' => $scheduled_optimizations_enabled,
1603 1858 'next_optimization_timestamp' => $next_optimization_timestamp,
1859 + 'scheduled_optimizations' => $scheduled_optimizations
1604 1860 );
1605 1861 }
1606 1862
1607 1863 /**
@@ -1936,12 +2192,16 @@
1936 2192 'is_premium' => $is_premium,
1937 2193 'data' => array(),
1938 2194 );
1939 2195 $scheduled_optimizations_enabled = false;
2196 + $last_optimized_timestamp = (int) $this->options->get_option('last-optimized', 0);
2197 + $next_optimization_timestamp = 0;
1940 2198 $wp_optimize = WP_Optimize();
2199 + $is_udc_request = $wp_optimize->is_updraft_central_request();
1941 2200
1942 2201 if (!$is_premium) {
1943 - $enabled = 'true' === $this->options->get_option('schedule');
2202 + $schedule_enabled = $this->options->get_option('schedule');
2203 + $scheduled_optimizations_enabled = 'true' === $schedule_enabled;
1944 2204
1945 2205 $schedule_type_saved_id = $this->options->get_option('schedule-type', 'wpo_weekly');
1946 2206
1947 2207 // Backwards compatibility:
@@ -1966,15 +2226,15 @@
1966 2226 $optimizations = $this->optimizer->sort_optimizations($this->optimizer->get_optimizations());
1967 2227
1968 2228 $optimizations_data = array();
1969 2229
1970 - $does_server_allow_table_optimization = WP_Optimize()->get_server_compatibility_instance()->does_server_allow_table_optimization();
2230 + $does_server_allow_table_optimization = $wp_optimize->get_server_compatibility_instance()->does_server_allow_table_optimization();
1971 2231
1972 2232 foreach ($optimizations as $id => $optimization) {
1973 2233 if (empty($optimization->available_for_auto)) continue;
1974 2234
1975 2235 if ('optimizetables' == $id && false === $does_server_allow_table_optimization) continue;
1976 -
2236 +
1977 2237 $auto_id = $optimization->get_auto_id();
1978 2238
1979 2239 $auto_dom_id = 'wp-optimize-auto-'.$auto_id;
1980 2240
@@ -1983,33 +2243,18 @@
1983 2243 $optimizations_data[] = array(
1984 2244 'id' => $auto_id,
1985 2245 'dom_id' => $auto_dom_id,
1986 2246 'activated' => $setting_activated,
1987 - 'label' => $optimization->get_auto_option_description(),
2247 + 'label' => $optimization->settings_label(),
1988 2248 );
1989 2249 }
1990 2250
1991 - $next_optimization_timestamp = 0;
1992 -
1993 - $scheduled_optimizations_enabled = $this->options->get_option('schedule', 'false') == 'true';
1994 -
1995 - if ($scheduled_optimizations_enabled) {
1996 - $next_optimization_timestamp = apply_filters('wpo_cron_next_event', wp_next_scheduled('wpo_cron_event2'));
1997 -
1998 - if ($next_optimization_timestamp) {
1999 -
2000 - $next_optimization_timestamp = $next_optimization_timestamp + 60 * 60 * get_option('gmt_offset');
2001 - }
2002 - }
2003 -
2004 2251 $return_data['data'] = array(
2005 - 'enabled' => $enabled,
2006 2252 'schedule_options' => $schedule_options,
2007 2253 'schedule_type_saved_id' => $schedule_type_saved_id,
2008 2254 'optimizations' => $optimizations_data,
2009 - 'next_optimization' => $next_optimization_timestamp ? esc_html(gmdate(get_option('date_format') . ' ' . get_option('time_format'), $next_optimization_timestamp)) : '',
2010 2255 'premium_version_link' => $wp_optimize->premium_version_link,
2011 - 'enable_schedule' => $this->options->get_option('schedule'),
2256 + 'enable_schedule' => $schedule_enabled,
2012 2257 );
2013 2258 } else {
2014 2259 $wp_optimize_premium = WP_Optimize_Premium();
2015 2260 $auto_options = $wp_optimize_premium->get_scheduled_optimizations();
@@ -2014,10 +2259,8 @@
2014 2259 $wp_optimize_premium = WP_Optimize_Premium();
2015 2260 $auto_options = $wp_optimize_premium->get_scheduled_optimizations();
2016 2261 $auto_optimizations = $wp_optimize_premium->get_auto_optimizations();
2017 2262
2018 - $next_optimization_timestamp = 0;
2019 -
2020 2263 if (!empty($auto_options)) {
2021 2264 foreach ($auto_options as $optimization) {
2022 2265 if (isset($optimization['status']) && 1 == $optimization['status']) {
2023 2266 $scheduled_optimizations_enabled = true;
@@ -2040,10 +2283,10 @@
2040 2283 );
2041 2284 if (!isset($event['optimization'])) {
2042 2285 $event['optimization'] = array();
2043 2286 }
2044 - foreach ($event as $key => $value) {
2045 - switch ($key) {
2287 + foreach ($event as $event_key => $value) {
2288 + switch ($event_key) {
2046 2289 case 'optimization':
2047 2290 $optimization['optimization'] = array();
2048 2291 foreach ($all_provided_optimizations as $this_optimization) {
2049 2292 if (in_array($this_optimization['id'], $value)) {
@@ -2072,9 +2315,9 @@
2072 2315 $optimization['weeks'] = array();
2073 2316 foreach ($weeks as $key => $this_week_value) {
2074 2317 $this_week = array();
2075 2318 $this_week['label'] = $this_week_value;
2076 - $this_week['selected'] = $key === $value;
2319 + $this_week['selected'] = absint($key) === absint($value);
2077 2320 $this_week['value'] = $key;
2078 2321 $optimization['weeks'][] = $this_week;
2079 2322 }
2080 2323 break;
@@ -2082,9 +2325,9 @@
2082 2325 $optimization['days'] = array();
2083 2326 foreach ($week_days as $key => $this_week_day_value) {
2084 2327 $this_week_day = array();
2085 2328 $this_week_day['label'] = $this_week_day_value;
2086 - $this_week_day['selected'] = $key === $value;
2329 + $this_week_day['selected'] = absint($key) === absint($value);
2087 2330 $this_week_day['value'] = $key;
2088 2331 $optimization['days'][] = $this_week_day;
2089 2332 }
2090 2333 break;
@@ -2102,17 +2345,22 @@
2102 2345 $optimization['status'] = '1' === $value;
2103 2346 break;
2104 2347 }
2105 2348 }
2106 - $optimizations[] = $optimization;
2107 - }
2108 2349
2109 - if ($scheduled_optimizations_enabled) {
2110 - $next_optimization_timestamp = apply_filters('wpo_cron_next_event', wp_next_scheduled('wpo_cron_event2'));
2350 + // Add the optimization only if it has at least one selected optimization.
2351 + $has_selected_optimization = false;
2111 2352
2112 - if ($next_optimization_timestamp) {
2113 - $next_optimization_timestamp = $next_optimization_timestamp + 60 * 60 * get_option('gmt_offset');
2353 + if (!empty($optimization['optimization'])) {
2354 + foreach ($optimization['optimization'] as $optimization_data) {
2355 + if (true === $optimization_data['selected']) {
2356 + $has_selected_optimization = true;
2357 + break;
2358 + }
2359 + }
2114 2360 }
2361 +
2362 + if ($has_selected_optimization) $optimizations[] = $optimization;
2115 2363 }
2116 2364
2117 2365 $return_data['data'] = array(
2118 2366 'optimizations' => $optimizations,
@@ -2121,9 +2369,8 @@
2121 2369 'today_date' => gmdate("Y-m-d"),
2122 2370 'weeks' => $weeks,
2123 2371 'week_days' => $week_days,
2124 2372 'days' => $days,
2125 - 'next_optimization' => $next_optimization_timestamp ? esc_html(gmdate(get_option('date_format') . ' ' . get_option('time_format'), $next_optimization_timestamp)) : '',
2126 2373 'is_auto_innodb' => $this->options->get_option('auto-innodb'),
2127 2374 'scheduled_optimizations' => $auto_options,
2128 2375 'auto_optimizations' => $auto_optimizations,
2129 2376 'warning_url' => $wp_optimize->wp_optimize_url('https://teamupdraft.com/documentation/wp-optimize/topics/database-optimization/faqs/', __('Warning: you should read the FAQ about the risks of this operation first.', 'wp-optimize'), '', '', true)
@@ -2129,8 +2376,23 @@
2129 2376 'warning_url' => $wp_optimize->wp_optimize_url('https://teamupdraft.com/documentation/wp-optimize/topics/database-optimization/faqs/', __('Warning: you should read the FAQ about the risks of this operation first.', 'wp-optimize'), '', '', true)
2130 2377 );
2131 2378 }
2132 2379
2380 + $return_data['data']['enabled'] = $scheduled_optimizations_enabled;
2381 + $return_data['data']['next_optimization'] = '';
2382 +
2383 + if ($scheduled_optimizations_enabled) {
2384 + $next_optimization_timestamp = apply_filters('wpo_cron_next_event', wp_next_scheduled('wpo_cron_event2'));
2385 +
2386 + if ($next_optimization_timestamp) {
2387 + $return_data['data']['next_optimization'] = $is_udc_request ? (int) $next_optimization_timestamp : esc_html(gmdate(get_option('date_format') . ' ' . get_option('time_format'), $next_optimization_timestamp + 60 * 60 * get_option('gmt_offset')));
2388 + }
2389 + }
2390 +
2391 + if ($is_udc_request && $last_optimized_timestamp) {
2392 + $return_data['data']['last_optimized'] = (int) $last_optimized_timestamp;
2393 + }
2394 +
2133 2395 // Add the translations.
2134 2396 $return_data['data']['translations'] = array(
2135 2397 'scheduled_optimization' => __('Scheduled Optimization', 'wp-optimize'),
2136 2398 'next_schedule_run' => __('Next scheduled run', 'wp-optimize'),
@@ -2223,7 +2485,25 @@
2223 2485 'variables' => array(
2224 2486 /* translators: %s is the cleaned up memory size. */
2225 2487 'performance_highlights' => sprintf(__('%s saved', 'wp-optimize'), $total_cleanup_size),
2226 2488 ),
2489 + );
2490 + }
2491 +
2492 + /**
2493 + * Returns freshly rendered HTML for the dashboard data sections (top row + module grid).
2494 + *
2495 + * @return array{success: bool, top_row: string, mod_grid: string}
2496 + */
2497 + public function refresh_dashboard() {
2498 + $data = WP_Optimize_Dashboard::instance()->get_dashboard_data();
2499 +
2500 + $top_row = WP_Optimize()->include_template('dashboard/partials/top-row.php', true, $data);
2501 + $mod_grid = WP_Optimize()->include_template('dashboard/partials/mod-grid.php', true, $data);
2502 +
2503 + return array(
2504 + 'success' => true,
2505 + 'top_row' => $top_row,
2506 + 'mod_grid' => $mod_grid,
2227 2507 );
2228 2508 }
2229 2509 }