WC_Helper_Updater

WC_Helper_Updater Class


Description Description

Contains the logic to fetch available updates and hook into Core’s update routines to serve WooCommerce.com-provided packages.


Source Source

File: includes/admin/helper/class-wc-helper-updater.php

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
class WC_Helper_Updater {
 
    /**
     * Loads the class, runs on init.
     */
    public static function load() {
        add_action( 'pre_set_site_transient_update_plugins', array( __CLASS__, 'transient_update_plugins' ), 21, 1 );
        add_action( 'pre_set_site_transient_update_themes', array( __CLASS__, 'transient_update_themes' ), 21, 1 );
        add_action( 'upgrader_process_complete', array( __CLASS__, 'upgrader_process_complete' ) );
        add_action( 'upgrader_pre_download', array( __CLASS__, 'block_expired_updates' ), 10, 2 );
    }
 
    /**
     * Runs in a cron thread, or in a visitor thread if triggered
     * by _maybe_update_plugins(), or in an auto-update thread.
     *
     * @param object $transient The update_plugins transient object.
     *
     * @return object The same or a modified version of the transient.
     */
    public static function transient_update_plugins( $transient ) {
        $update_data = self::get_update_data();
 
        foreach ( WC_Helper::get_local_woo_plugins() as $plugin ) {
            if ( empty( $update_data[ $plugin['_product_id'] ] ) ) {
                continue;
            }
 
            $data     = $update_data[ $plugin['_product_id'] ];
            $filename = $plugin['_filename'];
 
            $item = array(
                'id'             => 'woocommerce-com-' . $plugin['_product_id'],
                'slug'           => 'woocommerce-com-' . $data['slug'],
                'plugin'         => $filename,
                'new_version'    => $data['version'],
                'url'            => $data['url'],
                'package'        => $data['package'],
                'upgrade_notice' => $data['upgrade_notice'],
            );
 
            // We don't want to deliver a valid upgrade package when their subscription has expired.
            // To avoid the generic "no_package" error that empty strings give, we will store an
            // indication of expiration for the `upgrader_pre_download` filter to error on.
            if ( ! self::_has_active_subscription( $plugin['_product_id'] ) ) {
                $item['package'] = 'woocommerce-com-expired-' . $plugin['_product_id'];
            }
 
            if ( version_compare( $plugin['Version'], $data['version'], '<' ) ) {
                $transient->response[ $filename ] = (object) $item;
                unset( $transient->no_update[ $filename ] );
            } else {
                $transient->no_update[ $filename ] = (object) $item;
                unset( $transient->response[ $filename ] );
            }
        }
 
        return $transient;
    }
 
    /**
     * Runs on pre_set_site_transient_update_themes, provides custom
     * packages for WooCommerce.com-hosted extensions.
     *
     * @param object $transient The update_themes transient object.
     *
     * @return object The same or a modified version of the transient.
     */
    public static function transient_update_themes( $transient ) {
        $update_data = self::get_update_data();
 
        foreach ( WC_Helper::get_local_woo_themes() as $theme ) {
            if ( empty( $update_data[ $theme['_product_id'] ] ) ) {
                continue;
            }
 
            $data = $update_data[ $theme['_product_id'] ];
            $slug = $theme['_stylesheet'];
 
            $item = array(
                'theme'       => $slug,
                'new_version' => $data['version'],
                'url'         => $data['url'],
                'package'     => '',
            );
 
            if ( self::_has_active_subscription( $theme['_product_id'] ) ) {
                $item['package'] = $data['package'];
            }
 
            if ( version_compare( $theme['Version'], $data['version'], '<' ) ) {
                $transient->response[ $slug ] = $item;
            } else {
                unset( $transient->response[ $slug ] );
                $transient->checked[ $slug ] = $data['version'];
            }
        }
 
        return $transient;
    }
 
    /**
     * Get update data for all extensions.
     *
     * Scans through all subscriptions for the connected user, as well
     * as all Woo extensions without a subscription, and obtains update
     * data for each product.
     *
     * @return array Update data {product_id => data}
     */
    public static function get_update_data() {
        $payload = array();
 
        // Scan subscriptions.
        foreach ( WC_Helper::get_subscriptions() as $subscription ) {
            $payload[ $subscription['product_id'] ] = array(
                'product_id' => $subscription['product_id'],
                'file_id'    => '',
            );
        }
 
        // Scan local plugins which may or may not have a subscription.
        foreach ( WC_Helper::get_local_woo_plugins() as $data ) {
            if ( ! isset( $payload[ $data['_product_id'] ] ) ) {
                $payload[ $data['_product_id'] ] = array(
                    'product_id' => $data['_product_id'],
                );
            }
 
            $payload[ $data['_product_id'] ]['file_id'] = $data['_file_id'];
        }
 
        // Scan local themes.
        foreach ( WC_Helper::get_local_woo_themes() as $data ) {
            if ( ! isset( $payload[ $data['_product_id'] ] ) ) {
                $payload[ $data['_product_id'] ] = array(
                    'product_id' => $data['_product_id'],
                );
            }
 
            $payload[ $data['_product_id'] ]['file_id'] = $data['_file_id'];
        }
 
        return self::_update_check( $payload );
    }
 
    /**
     * Run an update check API call.
     *
     * The call is cached based on the payload (product ids, file ids). If
     * the payload changes, the cache is going to miss.
     *
     * @param array $payload Information about the plugin to update.
     * @return array Update data for each requested product.
     */
    private static function _update_check( $payload ) {
        ksort( $payload );
        $hash = md5( wp_json_encode( $payload ) );
 
        $cache_key = '_woocommerce_helper_updates';
        $data      = get_transient( $cache_key );
        if ( false !== $data ) {
            if ( hash_equals( $hash, $data['hash'] ) ) {
                return $data['products'];
            }
        }
 
        $data = array(
            'hash'     => $hash,
            'updated'  => time(),
            'products' => array(),
            'errors'   => array(),
        );
 
        $request = WC_Helper_API::post(
            'update-check',
            array(
                'body'          => wp_json_encode( array( 'products' => $payload ) ),
                'authenticated' => true,
            )
        );
 
        if ( wp_remote_retrieve_response_code( $request ) !== 200 ) {
            $data['errors'][] = 'http-error';
        } else {
            $data['products'] = json_decode( wp_remote_retrieve_body( $request ), true );
        }
 
        set_transient( $cache_key, $data, 12 * HOUR_IN_SECONDS );
        return $data['products'];
    }
 
    /**
     * Check for an active subscription.
     *
     * Checks a given product id against all subscriptions on
     * the current site. Returns true if at least one active
     * subscription is found.
     *
     * @param int $product_id The product id to look for.
     *
     * @return bool True if active subscription found.
     */
    private static function _has_active_subscription( $product_id ) {
        if ( ! isset( $auth ) ) {
            $auth = WC_Helper_Options::get( 'auth' );
        }
 
        if ( ! isset( $subscriptions ) ) {
            $subscriptions = WC_Helper::get_subscriptions();
        }
 
        if ( empty( $auth['site_id'] ) || empty( $subscriptions ) ) {
            return false;
        }
 
        // Check for an active subscription.
        foreach ( $subscriptions as $subscription ) {
            if ( $subscription['product_id'] != $product_id ) {
                continue;
            }
 
            if ( in_array( absint( $auth['site_id'] ), $subscription['connections'] ) ) {
                return true;
            }
        }
 
        return false;
    }
 
    /**
     * Get the number of products that have updates.
     *
     * @return int The number of products with updates.
     */
    public static function get_updates_count() {
        $cache_key = '_woocommerce_helper_updates_count';
        $count     = get_transient( $cache_key );
        if ( false !== $count ) {
            return $count;
        }
 
        // Don't fetch any new data since this function in high-frequency.
        if ( ! get_transient( '_woocommerce_helper_subscriptions' ) ) {
            return 0;
        }
 
        if ( ! get_transient( '_woocommerce_helper_updates' ) ) {
            return 0;
        }
 
        $count       = 0;
        $update_data = self::get_update_data();
 
        if ( empty( $update_data ) ) {
            set_transient( $cache_key, $count, 12 * HOUR_IN_SECONDS );
            return $count;
        }
 
        // Scan local plugins.
        foreach ( WC_Helper::get_local_woo_plugins() as $plugin ) {
            if ( empty( $update_data[ $plugin['_product_id'] ] ) ) {
                continue;
            }
 
            if ( version_compare( $plugin['Version'], $update_data[ $plugin['_product_id'] ]['version'], '<' ) ) {
                $count++;
            }
        }
 
        // Scan local themes.
        foreach ( WC_Helper::get_local_woo_themes() as $theme ) {
            if ( empty( $update_data[ $theme['_product_id'] ] ) ) {
                continue;
            }
 
            if ( version_compare( $theme['Version'], $update_data[ $theme['_product_id'] ]['version'], '<' ) ) {
                $count++;
            }
        }
 
        set_transient( $cache_key, $count, 12 * HOUR_IN_SECONDS );
        return $count;
    }
 
    /**
     * Return the updates count markup.
     *
     * @return string Updates count markup, empty string if no updates avairable.
     */
    public static function get_updates_count_html() {
        $count = self::get_updates_count();
        if ( ! $count ) {
            return '';
        }
 
        $count_html = sprintf( '<span class="update-plugins count-%d"><span class="update-count">%d</span></span>', $count, number_format_i18n( $count ) );
        return $count_html;
    }
 
    /**
     * Flushes cached update data.
     */
    public static function flush_updates_cache() {
        delete_transient( '_woocommerce_helper_updates' );
        delete_transient( '_woocommerce_helper_updates_count' );
        delete_site_transient( 'update_plugins' );
        delete_site_transient( 'update_themes' );
    }
 
    /**
     * Fires when a user successfully updated a theme or a plugin.
     */
    public static function upgrader_process_complete() {
        delete_transient( '_woocommerce_helper_updates_count' );
    }
 
    /**
     * Hooked into the upgrader_pre_download filter in order to better handle error messaging around expired
     * plugin updates. Initially we were using an empty string, but the error message that no_package
     * results in does not fit the cause.
     *
     * @since 4.1.0
     * @param bool   $reply Holds the current filtered response.
     * @param string $package The path to the package file for the update.
     * @return false|WP_Error False to proceed with the update as normal, anything else to be returned instead of updating.
     */
    public static function block_expired_updates( $reply, $package ) {
        // Don't override a reply that was set already.
        if ( false !== $reply ) {
            return $reply;
        }
 
        // Only for packages with expired subscriptions.
        if ( 0 !== strpos( $package, 'woocommerce-com-expired-' ) ) {
            return false;
        }
 
        return new WP_Error(
            'woocommerce_subscription_expired',
            sprintf(
                // translators: %s: URL of WooCommerce.com subscriptions tab.
                __( 'Please visit the <a href="%s" target="_blank">subscriptions page</a> and renew to continue receiving updates.', 'woocommerce' ),
                esc_url( admin_url( 'admin.php?page=wc-addons&section=helper' ) )
            )
        );
    }
}

Top ↑

Methods Methods


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.