Plugin Directory

Changeset 3086929


Ignore:
Timestamp:
05/15/2024 08:20:18 AM (19 months ago)
Author:
konamiman
Message:

PR 9: Add a dismissable admin notice indicating that the Legacy REST API is not compatible with HPOS

Location:
woocommerce-legacy-rest-api/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • woocommerce-legacy-rest-api/trunk/includes/class-wc-legacy-rest-api-plugin.php

    r3022200 r3086929  
    1616     * Plugin initialization, to be invoked inside the woocommerce_init hook.
    1717     */
    18     private static function init() {
    19         require_once __DIR__ . '/legacy/class-wc-legacy-api.php';
    20         require_once __DIR__ . '/class-wc-api.php';
     18    public static function on_woocommerce_init() {
     19        if( ! self::legacy_api_still_in_woocommerce() ) {
     20            require_once __DIR__ . '/legacy/class-wc-legacy-api.php';
     21            require_once __DIR__ . '/class-wc-api.php';
    2122
    22         WC()->api = new WC_API();
    23         WC()->api->init();
    24         WC()->api->add_endpoint();
     23            WC()->api = new WC_API();
     24            WC()->api->init();
     25            WC()->api->add_endpoint();
     26        }
     27
     28        if( ! self::maybe_add_hpos_incompatibility_admin_notice() ) {
     29            self::maybe_remove_hpos_incompatibility_admin_notice();
     30        }
    2531    }
    2632
     
    5056     */
    5157    public static function on_plugin_activated() {
     58        if( ! self::woocommerce_is_active() ) {
     59            return;
     60        }
     61       
    5262        if( ! self::legacy_api_still_in_woocommerce() ) {
    5363            require_once __DIR__ . '/legacy/class-wc-legacy-api.php';
     
    6070
    6171    /**
     72     * Add the "legacy REST API and HPOS are incompatible" admin notice if needed.
     73     *
     74     * @returns bool True if the notice has been added, false otherwise.
     75     */
     76    private static function maybe_add_hpos_incompatibility_admin_notice() {
     77        if( ! self::hpos_is_enabled() || self::user_has_dismissed_admin_notice( 'legacy_rest_api_is_incompatible_with_hpos' ) ) {
     78            return false;
     79        }
     80   
     81        if ( ! WC_Admin_Notices::has_notice( 'legacy_rest_api_is_incompatible_with_hpos' ) ) {
     82            $features_page_url = admin_url( 'admin.php?page=wc-settings&tab=advanced&section=features' );
     83            WC_Admin_Notices::add_custom_notice(
     84                'legacy_rest_api_is_incompatible_with_hpos',
     85                sprintf(
     86                    wpautop( __( '⚠ <b>The Legacy REST API plugin and HPOS are both active on this site.</b><br/><br/>Please be aware that the WooCommerce Legacy REST API is <b>not</b> compatible with HPOS. <a target="_blank" href="%s">Manage features</a>', 'woocommerce-legacy-rest-api' ) ),
     87                    $features_page_url
     88                )
     89            );
     90
     91            return true;
     92        }
     93
     94        return false;
     95    }
     96
     97    /**
     98     * Check if HPOS is currently in use.
     99     *
     100     * @returns bool True if HPOS is currently in use.
     101     */
     102    private function hpos_is_enabled(): bool {
     103        return class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
     104    }
     105
     106    /**
     107     * Remove the "legacy REST API and HPOS are incompatible" admin notice if needed.
     108     */
     109    private static function maybe_remove_hpos_incompatibility_admin_notice() {
     110        if ( WC_Admin_Notices::has_notice( 'legacy_rest_api_is_incompatible_with_hpos' ) && ! self::hpos_is_enabled() ) {
     111            self::remove_notice( 'legacy_rest_api_is_incompatible_with_hpos' );
     112        }
     113    }
     114
     115    /**
    62116     * Act on plugin deactivation/uninstall.
    63117     */
    64118    public static function on_plugin_deactivated() {
     119        if( ! self::woocommerce_is_active() ) {
     120            return;
     121        }
     122
    65123        if( ! self::legacy_api_still_in_woocommerce() ) {
    66124            update_option( 'woocommerce_api_enabled', 'no' );
    67125            flush_rewrite_rules();
     126        }
     127
     128        if ( WC_Admin_Notices::has_notice( 'legacy_rest_api_is_incompatible_with_hpos' ) ) {
     129            self::remove_notice( 'legacy_rest_api_is_incompatible_with_hpos' );
    68130        }
    69131    }
     
    75137        if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
    76138            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', self::$plugin_filename, false );
    77         }
    78     }
    79 
    80      /**
    81      * Handler for the before_woocommerce_init hook, needed to initialize the plugin.
    82      */
    83     public static function on_woocommerce_init() {
    84         if( ! self::legacy_api_still_in_woocommerce() ) {
    85             self::init();
    86139        }
    87140    }
     
    102155        return $all_plugins;
    103156    }
     157
     158    /**
     159     * Check if WooCommerce itself is active in the site.
     160     */
     161    private static function woocommerce_is_active() {
     162        return class_exists( 'WooCommerce' );
     163    }
     164
     165    /**
     166     * Check if the current user has dismissed an admin notice.
     167     *
     168     * @param string $notice_id Id of the notice.
     169     */
     170    private static function user_has_dismissed_admin_notice( $notice_id ) {
     171        return '' !== get_user_meta( get_current_user_id(), "dismissed_${notice_id}_notice", true );
     172    }
    104173}
  • woocommerce-legacy-rest-api/trunk/includes/legacy/api/v1/class-wc-api-server.php

    r3022200 r3086929  
    279279        // Normalise the endpoints
    280280        foreach ( $endpoints as $route => &$handlers ) {
    281             if ( count( $handlers ) <= 2 && isset( $handlers[1] ) && ! is_array( $handlers[1] ) ) {
     281            if ( is_array( $handlers ) && count( $handlers ) <= 2 && isset( $handlers[1] ) && ! is_array( $handlers[1] ) ) {
    282282                $handlers = array( $handlers );
    283283            }
  • woocommerce-legacy-rest-api/trunk/includes/legacy/api/v2/class-wc-api-server.php

    r3022200 r3086929  
    277277        // Normalise the endpoints
    278278        foreach ( $endpoints as $route => &$handlers ) {
    279             if ( count( $handlers ) <= 2 && isset( $handlers[1] ) && ! is_array( $handlers[1] ) ) {
     279            if ( is_array( $handlers ) && count( $handlers ) <= 2 && isset( $handlers[1] ) && ! is_array( $handlers[1] ) ) {
    280280                $handlers = array( $handlers );
    281281            }
  • woocommerce-legacy-rest-api/trunk/includes/legacy/api/v3/class-wc-api-server.php

    r3022200 r3086929  
    277277        // Normalise the endpoints
    278278        foreach ( $endpoints as $route => &$handlers ) {
    279             if ( count( $handlers ) <= 2 && isset( $handlers[1] ) && ! is_array( $handlers[1] ) ) {
     279            if ( is_array( $handlers ) && count( $handlers ) <= 2 && isset( $handlers[1] ) && ! is_array( $handlers[1] ) ) {
    280280                $handlers = array( $handlers );
    281281            }
  • woocommerce-legacy-rest-api/trunk/readme.txt

    r3022200 r3086929  
    55Tested up to: 6.3
    66Requires PHP: 7.4
    7 Stable tag: 1.0.1
     7Stable tag: 1.0.2
    88License: GPLv3
    99License URI: https://www.gnu.org/licenses/gpl-3.0.html
     
    4141- Replace the text domain for human-readable strings from 'woocommerce' to 'woocommerce-legacy-rest-api'.
    4242- Add sanitization for data received via query string arguments and the $_SERVER array.
     43
     44
     45= 1.0.2 2024-05-01
     46
     47Add a dismissable admin notice indicating that the Legacy REST API is not compatible with HPOS.
     48The notice will appear if the orders table is (or has been) selected as the orders data store in the WooCommerce features settings page,
     49and will disappear when that ceases to be true. Once the notice is dismissed it will never appear again.
  • woocommerce-legacy-rest-api/trunk/woocommerce-legacy-rest-api.php

    r3022200 r3086929  
    44 * Plugin URI: https://github.com/woocommerce/woocommerce-legacy-rest-api
    55 * Description: The legacy WooCommerce REST API, which used to be part of WooCommerce itself but is removed as of WooCommerce 9.0.
    6  * Version: 1.0.1
     6 * Version: 1.0.2
    77 * Author: WooCommerce
    88 * Author URI: https://woocommerce.com
Note: See TracChangeset for help on using the changeset viewer.