Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.16% covered (danger)
30.16%
19 / 63
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Scheduled_Updates_Admin
30.16% covered (danger)
30.16%
19 / 63
25.00% covered (danger)
25.00%
1 / 4
103.21
0.00% covered (danger)
0.00%
0 / 1
 add_scheduled_updates_context
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
30
 add_scheduled_updates_view
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 show_scheduled_updates
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
 get_scheduled_update_text
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Scheduled Updates Admin.
4 *
5 * @package automattic/scheduled-updates
6 */
7
8namespace Automattic\Jetpack;
9
10/**
11 * Class Scheduled_Updates_Admin.
12 *
13 * Contains all the wp-admin-related functionality for scheduled updates.
14 */
15class Scheduled_Updates_Admin {
16
17    /**
18     * Add context for scheduled updates in the Plugins list table.
19     *
20     * @param array $plugins An array of plugins.
21     * @return array
22     */
23    public static function add_scheduled_updates_context( $plugins ) {
24        if ( ! function_exists( 'wp_get_scheduled_events' ) ) {
25            require_once __DIR__ . '/pluggable.php';
26        }
27
28        $events = wp_get_scheduled_events( Scheduled_Updates::PLUGIN_CRON_HOOK );
29
30        if ( ! empty( $events ) ) {
31            if ( ! empty( $_REQUEST['plugin_status'] ) && 'scheduled-updates' === $_REQUEST['plugin_status'] ) { // phpcs:ignore WordPress.Security.NonceVerification
32                $GLOBALS['status'] = 'scheduled-updates'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
33            }
34
35            /*
36             * Get the unique list of plugins that are part of scheduled updates.
37             *
38             * Example:
39             *  $scheduled = array(
40             *      'rest-api-console/rest-api-console.php'     => 0,
41             *      'wordpress-importer/wordpress-importer.php' => 1,
42             *      'wp-last-login/wp-last-login.php'           => 2,
43             *  );
44             */
45            $scheduled = array_flip(
46                array_unique(
47                    array_merge(
48                        ...array_values(
49                            wp_list_pluck( $events, 'args' )
50                        )
51                    )
52                )
53            );
54
55            // Removing from the auto-update-disabled list since they are scheduled.
56            $plugins['auto-update-disabled'] = array_diff_key( (array) $plugins['auto-update-disabled'], $scheduled );
57            $plugins['scheduled-updates']    = array_intersect_key( (array) $plugins['all'], $scheduled );
58        }
59
60        return $plugins;
61    }
62
63    /**
64     * Add a view for scheduled updates in the Plugins list table.
65     *
66     * @param array $views An array of available views.
67     * @return array
68     */
69    public static function add_scheduled_updates_view( $views ) {
70        global $totals;
71
72        if ( ! empty( $totals['scheduled-updates'] ) ) {
73            $views['scheduled-updates'] = sprintf(
74                '<a href="%1$s" class="%2$s">%3$s <span class="count">(%4$s)</span></a>',
75                esc_url( add_query_arg( array( 'plugin_status' => 'scheduled-updates' ), 'plugins.php' ) ),
76                isset( $_REQUEST['plugin_status'] ) && 'scheduled-updates' === $_REQUEST['plugin_status'] ? 'current' : '', // phpcs:ignore WordPress.Security.NonceVerification
77                __( 'Scheduled Updates', 'jetpack-scheduled-updates' ),
78                number_format_i18n( $totals['scheduled-updates'] )
79            );
80        }
81
82        return $views;
83    }
84
85    /**
86     * Filters the HTML of the auto-updates setting for each plugin in the Plugins list table.
87     *
88     * @param string $html        The HTML of the plugin's auto-update column content,
89     *                            including toggle auto-update action links and
90     *                            time to next update.
91     * @param string $plugin_file Path to the plugin file relative to the plugin directory.
92     */
93    public static function show_scheduled_updates( $html, $plugin_file ) {
94        if ( ! function_exists( 'wp_get_scheduled_events' ) ) {
95            require_once __DIR__ . '/pluggable.php';
96        }
97
98        $events = wp_get_scheduled_events( Scheduled_Updates::PLUGIN_CRON_HOOK );
99
100        $schedules = array();
101        foreach ( $events as $event ) {
102            if ( in_array( $plugin_file, $event->args, true ) ) {
103                $schedules[] = $event;
104            }
105        }
106
107        // Plugin is not part of an update schedule.
108        if ( empty( $schedules ) ) {
109            return $html;
110        }
111
112        $text = array_map( array( __CLASS__, 'get_scheduled_update_text' ), $schedules );
113
114        $html  = '<p style="margin: 0 0 8px">' . implode( '<br>', $text ) . '</p>';
115        $html .= sprintf(
116            '<a href="%1$s">%2$s</a>',
117            esc_url( 'https://wordpress.com/plugins/scheduled-updates/' . ( new Status() )->get_site_suffix() ),
118            esc_html__( 'Edit', 'jetpack-scheduled-updates' )
119        );
120
121        return $html;
122    }
123
124    /**
125     * Get the text for a scheduled update.
126     *
127     * @param object $schedule The scheduled update.
128     * @return string
129     */
130    public static function get_scheduled_update_text( $schedule ) {
131        if ( DAY_IN_SECONDS === $schedule->interval ) {
132            $html = sprintf(
133            /* translators: %s is the time of day. Daily at 10 am. */
134                esc_html__( 'Daily at %s.', 'jetpack-scheduled-updates' ),
135                wp_date( get_option( 'time_format' ), $schedule->timestamp )
136            );
137        } else {
138            // Not getting smart about passing in weekdays makes it easier to translate.
139            $weekdays = array(
140                /* translators: %s is the time of day. Mondays at 10 am. */
141                1 => __( 'Mondays at %s.', 'jetpack-scheduled-updates' ),
142                /* translators: %s is the time of day. Tuesdays at 10 am. */
143                2 => __( 'Tuesdays at %s.', 'jetpack-scheduled-updates' ),
144                /* translators: %s is the time of day. Wednesdays at 10 am. */
145                3 => __( 'Wednesdays at %s.', 'jetpack-scheduled-updates' ),
146                /* translators: %s is the time of day. Thursdays at 10 am. */
147                4 => __( 'Thursdays at %s.', 'jetpack-scheduled-updates' ),
148                /* translators: %s is the time of day. Fridays at 10 am. */
149                5 => __( 'Fridays at %s.', 'jetpack-scheduled-updates' ),
150                /* translators: %s is the time of day. Saturdays at 10 am. */
151                6 => __( 'Saturdays at %s.', 'jetpack-scheduled-updates' ),
152                /* translators: %s is the time of day. Sundays at 10 am. */
153                7 => __( 'Sundays at %s.', 'jetpack-scheduled-updates' ),
154            );
155
156            $html = sprintf(
157                $weekdays[ (int) wp_date( 'N', $schedule->timestamp ) ],
158                wp_date( get_option( 'time_format' ), $schedule->timestamp )
159            );
160        }
161
162        return $html;
163    }
164}