class.jetpack-sync-module-updates.php
| 1 | <?php |
| 2 | |
| 3 | class Jetpack_Sync_Module_Updates extends Jetpack_Sync_Module { |
| 4 | |
| 5 | const UPDATES_CHECKSUM_OPTION_NAME = 'jetpack_updates_sync_checksum'; |
| 6 | |
| 7 | private $old_wp_version = null; |
| 8 | |
| 9 | function name() { |
| 10 | return 'updates'; |
| 11 | } |
| 12 | |
| 13 | public function init_listeners( $callable ) { |
| 14 | global $wp_version; |
| 15 | $this->old_wp_version = $wp_version; |
| 16 | add_action( 'set_site_transient_update_plugins', array( $this, 'validate_update_change' ), 10, 3 ); |
| 17 | add_action( 'set_site_transient_update_themes', array( $this, 'validate_update_change' ), 10, 3 ); |
| 18 | add_action( 'set_site_transient_update_core', array( $this, 'validate_update_change' ), 10, 3 ); |
| 19 | |
| 20 | add_action( 'jetpack_update_plugins_change', $callable ); |
| 21 | add_action( 'jetpack_update_themes_change', $callable ); |
| 22 | add_action( 'jetpack_update_core_change', $callable ); |
| 23 | |
| 24 | add_filter( 'jetpack_sync_before_enqueue_jetpack_update_plugins_change', array( |
| 25 | $this, |
| 26 | 'filter_update_keys', |
| 27 | ), 10, 2 ); |
| 28 | add_filter( 'jetpack_sync_before_enqueue_upgrader_process_complete', array( |
| 29 | $this, |
| 30 | 'filter_upgrader_process_complete', |
| 31 | ), 10, 2 ); |
| 32 | |
| 33 | add_action( 'automatic_updates_complete', $callable ); |
| 34 | |
| 35 | |
| 36 | if ( is_multisite() ) { |
| 37 | add_filter( 'pre_update_site_option_wpmu_upgrade_site', array ( $this, 'update_core_network_event' ), 10, 2 ); |
| 38 | add_action( 'jetpack_sync_core_update_network', $callable, 10, 3 ); |
| 39 | } |
| 40 | |
| 41 | // Send data when update completes |
| 42 | add_action( '_core_updated_successfully', array( $this, 'update_core' ) ); |
| 43 | add_action( 'jetpack_sync_core_reinstalled_successfully', $callable ); |
| 44 | add_action( 'jetpack_sync_core_autoupdated_successfully', $callable, 10, 2 ); |
| 45 | add_action( 'jetpack_sync_core_updated_successfully', $callable, 10, 2 ); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | public function init_full_sync_listeners( $callable ) { |
| 50 | add_action( 'jetpack_full_sync_updates', $callable ); |
| 51 | } |
| 52 | |
| 53 | public function init_before_send() { |
| 54 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_updates', array( $this, 'expand_updates' ) ); |
| 55 | add_filter( 'jetpack_sync_before_send_jetpack_update_themes_change', array( $this, 'expand_themes' ) ); |
| 56 | } |
| 57 | |
| 58 | public function update_core_network_event( $wp_db_version, $old_wp_db_version ) { |
| 59 | global $wp_version; |
| 60 | /** |
| 61 | * Sync event for when core wp network updates to a new db version |
| 62 | * |
| 63 | * @since 5.0.0 |
| 64 | * |
| 65 | * @param int $wp_db_version the latest wp_db_version |
| 66 | * @param int $old_wp_db_version previous wp_db_version |
| 67 | * @param string $wp_version the latest wp_version |
| 68 | * |
| 69 | */ |
| 70 | do_action( 'jetpack_sync_core_update_network', $wp_db_version, $old_wp_db_version, $wp_version ); |
| 71 | return $wp_db_version; |
| 72 | } |
| 73 | |
| 74 | public function update_core( $new_wp_version ) { |
| 75 | global $pagenow; |
| 76 | |
| 77 | if ( isset( $_GET[ 'action' ] ) && 'do-core-reinstall' === $_GET[ 'action' ] ) { |
| 78 | /** |
| 79 | * Sync event that fires when core reinstall was successful |
| 80 | * |
| 81 | * @since 5.0.0 |
| 82 | * |
| 83 | * @param string $new_wp_version the updated WordPress version |
| 84 | */ |
| 85 | do_action( 'jetpack_sync_core_reinstalled_successfully', $new_wp_version ); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // Core was autoudpated |
| 90 | if ( 'update-core.php' !== $pagenow ) { |
| 91 | /** |
| 92 | * Sync event that fires when core autoupdate was successful |
| 93 | * |
| 94 | * @since 5.0.0 |
| 95 | * |
| 96 | * @param string $new_wp_version the updated WordPress version |
| 97 | * @param string $old_wp_version the previous WordPress version |
| 98 | */ |
| 99 | do_action( 'jetpack_sync_core_autoupdated_successfully', $new_wp_version, $this->old_wp_version ); |
| 100 | return; |
| 101 | } |
| 102 | /** |
| 103 | * Sync event that fires when core update was successful |
| 104 | * |
| 105 | * @since 5.0.0 |
| 106 | * |
| 107 | * @param string $new_wp_version the updated WordPress version |
| 108 | * @param string $old_wp_version the previous WordPress version |
| 109 | */ |
| 110 | do_action( 'jetpack_sync_core_updated_successfully', $new_wp_version, $this->old_wp_version ); |
| 111 | return; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | |
| 116 | public function get_update_checksum( $update, $transient ) { |
| 117 | $updates = array(); |
| 118 | $no_updated = array(); |
| 119 | switch ( $transient ) { |
| 120 | case 'update_plugins': |
| 121 | if ( ! empty( $update->response ) && is_array( $update->response ) ) { |
| 122 | foreach ( $update->response as $plugin_slug => $response ) { |
| 123 | if ( ! empty( $plugin_slug ) && isset( $response->new_version ) ) { |
| 124 | $updates[] = array( $plugin_slug => $response->new_version ); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | if ( ! empty( $update->no_update ) ) { |
| 129 | $no_updated = array_keys( $update->no_update ); |
| 130 | } |
| 131 | |
| 132 | if ( ! isset( $no_updated[ 'jetpack/jetpack.php' ] ) && isset( $updates[ 'jetpack/jetpack.php' ] ) ) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | break; |
| 137 | case 'update_themes': |
| 138 | if ( ! empty( $update->response ) && is_array( $update->response ) ) { |
| 139 | foreach ( $update->response as $theme_slug => $response ) { |
| 140 | if ( ! empty( $theme_slug ) && isset( $response['new_version'] ) ) { |
| 141 | $updates[] = array( $theme_slug => $response['new_version'] ); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if ( ! empty( $update->checked ) ) { |
| 147 | $no_updated = $update->checked; |
| 148 | } |
| 149 | |
| 150 | break; |
| 151 | case 'update_core': |
| 152 | if ( ! empty( $update->updates ) && is_array( $update->updates ) ) { |
| 153 | foreach ( $update->updates as $response ) { |
| 154 | if( ! empty( $response->response ) && $response->response === 'latest' ) { |
| 155 | continue; |
| 156 | } |
| 157 | if ( ! empty( $response->response ) && isset( $response->packages->full ) ) { |
| 158 | $updates[] = array( $response->response => $response->packages->full ); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if ( ! empty( $update->version_checked ) ) { |
| 164 | $no_updated = $update->version_checked; |
| 165 | } |
| 166 | |
| 167 | if ( empty( $updates ) ) { |
| 168 | return false; |
| 169 | } |
| 170 | break; |
| 171 | |
| 172 | } |
| 173 | if ( empty( $updates ) && empty( $no_updated ) ) { |
| 174 | return false; |
| 175 | } |
| 176 | return $this->get_check_sum( array( $no_updated, $updates ) ); |
| 177 | } |
| 178 | |
| 179 | public function validate_update_change( $value, $expiration, $transient ) { |
| 180 | |
| 181 | $new_checksum = $this->get_update_checksum( $value, $transient ); |
| 182 | if ( false === $new_checksum ) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | $checksums = get_option( self::UPDATES_CHECKSUM_OPTION_NAME, array() ); |
| 187 | |
| 188 | if ( isset( $checksums[ $transient ] ) && $checksums[ $transient ] === $new_checksum ) { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | $checksums[ $transient ] = $new_checksum; |
| 193 | |
| 194 | update_option( self::UPDATES_CHECKSUM_OPTION_NAME, $checksums ); |
| 195 | /** |
| 196 | * jetpack_{$transient}_change |
| 197 | * jetpack_update_plugins_change |
| 198 | * jetpack_update_themes_change |
| 199 | * jetpack_update_core_change |
| 200 | * |
| 201 | * @since 5.1.0 |
| 202 | * |
| 203 | * @param array containing info that tells us what needs updating |
| 204 | * |
| 205 | */ |
| 206 | do_action( "jetpack_{$transient}_change", $value ); |
| 207 | } |
| 208 | |
| 209 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
| 210 | /** |
| 211 | * Tells the client to sync all updates to the server |
| 212 | * |
| 213 | * @since 4.2.0 |
| 214 | * |
| 215 | * @param boolean Whether to expand updates (should always be true) |
| 216 | */ |
| 217 | do_action( 'jetpack_full_sync_updates', true ); |
| 218 | |
| 219 | // The number of actions enqueued, and next module state (true == done) |
| 220 | return array( 1, true ); |
| 221 | } |
| 222 | |
| 223 | public function estimate_full_sync_actions( $config ) { |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | function get_full_sync_actions() { |
| 228 | return array( 'jetpack_full_sync_updates' ); |
| 229 | } |
| 230 | |
| 231 | public function get_all_updates() { |
| 232 | return array( |
| 233 | 'core' => get_site_transient( 'update_core' ), |
| 234 | 'plugins' => get_site_transient( 'update_plugins' ), |
| 235 | 'themes' => get_site_transient( 'update_themes' ), |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | // removes unnecessary keys from synced updates data |
| 240 | function filter_update_keys( $args ) { |
| 241 | $updates = $args[0]; |
| 242 | |
| 243 | if ( isset( $updates->no_update ) ) { |
| 244 | unset( $updates->no_update ); |
| 245 | } |
| 246 | |
| 247 | return $args; |
| 248 | } |
| 249 | |
| 250 | function filter_upgrader_process_complete( $args ) { |
| 251 | array_shift( $args ); |
| 252 | |
| 253 | return $args; |
| 254 | } |
| 255 | |
| 256 | public function expand_updates( $args ) { |
| 257 | if ( $args[0] ) { |
| 258 | return $this->get_all_updates(); |
| 259 | } |
| 260 | |
| 261 | return $args; |
| 262 | } |
| 263 | |
| 264 | public function expand_themes( $args ) { |
| 265 | if ( ! isset( $args[0], $args[0]->response ) ) { |
| 266 | return $args; |
| 267 | } |
| 268 | if ( ! is_array( $args[0]->response ) ) { |
| 269 | trigger_error( 'Warning: Not an Array as expected but -> ' . wp_json_encode( $args[0]->response ) . ' instead', E_USER_WARNING ); |
| 270 | return $args; |
| 271 | } |
| 272 | foreach ( $args[0]->response as $stylesheet => &$theme_data ) { |
| 273 | $theme = wp_get_theme( $stylesheet ); |
| 274 | $theme_data['name'] = $theme->name; |
| 275 | } |
| 276 | return $args; |
| 277 | } |
| 278 | |
| 279 | public function reset_data() { |
| 280 | delete_option( self::UPDATES_CHECKSUM_OPTION_NAME ); |
| 281 | } |
| 282 | } |
| 283 |