views
5 months ago
class-wc-helper-admin.php
4 months ago
class-wc-helper-api.php
1 year ago
class-wc-helper-compat.php
5 years ago
class-wc-helper-options.php
3 years ago
class-wc-helper-orders-api.php
2 years ago
class-wc-helper-sanitization.php
1 year ago
class-wc-helper-subscriptions-api.php
4 months ago
class-wc-helper-updater.php
2 months ago
class-wc-helper.php
4 months ago
class-wc-plugin-api-updater.php
1 year ago
class-wc-product-usage-notice.php
1 year ago
class-wc-woo-helper-connection.php
4 months ago
class-wc-woo-update-manager-plugin.php
2 years ago
class-wc-plugin-api-updater.php
111 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Updates the Product API response from WP.org. |
| 4 | * |
| 5 | * @class WC_Plugin_Api_Updater |
| 6 | * @package WooCommerce\Admin\Helper |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Class WC_Plugin_Api_Updater |
| 13 | */ |
| 14 | class WC_Plugin_Api_Updater { |
| 15 | |
| 16 | /** |
| 17 | * Loads the class, runs on init. |
| 18 | */ |
| 19 | public static function load() { |
| 20 | add_filter( 'plugins_api', array( __CLASS__, 'plugins_api' ), 20, 3 ); |
| 21 | add_filter( 'themes_api', array( __CLASS__, 'themes_api' ), 20, 3 ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Plugin information callback for Woo extensions. |
| 26 | * |
| 27 | * @param object $response The response core needs to display the modal. |
| 28 | * @param string $action The requested plugins_api() action. |
| 29 | * @param object $args Arguments passed to plugins_api(). |
| 30 | * |
| 31 | * @return object An updated $response. |
| 32 | */ |
| 33 | public static function plugins_api( $response, $action, $args ) { |
| 34 | if ( 'plugin_information' !== $action ) { |
| 35 | return $response; |
| 36 | } |
| 37 | |
| 38 | return self::override_products_api_response( $response, $action, $args ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Theme information callback for Woo themes. |
| 43 | * |
| 44 | * @param object $response The response core needs to display the modal. |
| 45 | * @param string $action The requested themes_api() action. |
| 46 | * @param object $args Arguments passed to themes_api(). |
| 47 | */ |
| 48 | public static function themes_api( $response, $action, $args ) { |
| 49 | if ( 'theme_information' !== $action ) { |
| 50 | return $response; |
| 51 | } |
| 52 | |
| 53 | return self::override_products_api_response( $response, $action, $args ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Override the products API to fetch data from the Helper API if it's a Woo product. |
| 58 | * |
| 59 | * @param object $response The response core needs to display the modal. |
| 60 | * @param string $action The requested action. |
| 61 | * @param object $args Arguments passed to the API. |
| 62 | */ |
| 63 | public static function override_products_api_response( $response, $action, $args ) { |
| 64 | if ( empty( $args->slug ) ) { |
| 65 | return $response; |
| 66 | } |
| 67 | |
| 68 | // Only for slugs that start with woocommerce-com-. |
| 69 | if ( 0 !== strpos( $args->slug, 'woocommerce-com-' ) ) { |
| 70 | return $response; |
| 71 | } |
| 72 | |
| 73 | $clean_slug = str_replace( 'woocommerce-com-', '', $args->slug ); |
| 74 | |
| 75 | // Look through update data by slug. |
| 76 | $update_data = WC_Helper_Updater::get_update_data(); |
| 77 | $products = wp_list_filter( $update_data, array( 'slug' => $clean_slug ) ); |
| 78 | |
| 79 | if ( empty( $products ) ) { |
| 80 | return $response; |
| 81 | } |
| 82 | |
| 83 | $product_id = array_keys( $products ); |
| 84 | $product_id = array_shift( $product_id ); |
| 85 | $is_site_connected = WC_Helper::is_site_connected(); |
| 86 | $endpoint = add_query_arg( |
| 87 | array( 'product_id' => absint( $product_id ) ), |
| 88 | 'info' |
| 89 | ); |
| 90 | // Fetch the product information from the Helper API. |
| 91 | $request = WC_Helper_API::get( |
| 92 | $endpoint, |
| 93 | array( 'authenticated' => $is_site_connected ) |
| 94 | ); |
| 95 | |
| 96 | // If we tried to authenticate and failed, try again without authentication. |
| 97 | if ( is_wp_error( $request ) && $is_site_connected ) { |
| 98 | $request = WC_Helper_API::get( $endpoint ); |
| 99 | } |
| 100 | |
| 101 | $results = json_decode( wp_remote_retrieve_body( $request ), true ); |
| 102 | if ( ! empty( $results ) ) { |
| 103 | $response = (object) $results; |
| 104 | } |
| 105 | |
| 106 | return $response; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | WC_Plugin_Api_Updater::load(); |
| 111 |