Plugin Directory

Changeset 1378328


Ignore:
Timestamp:
03/24/2016 10:13:30 PM (9 years ago)
Author:
oleg2tor
Message:

add: Check for WP REST API Plugin is activated

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-api-v2-woocommerce-endpoints/trunk/wp-api-woocommerce-endpoints.php

    r1377468 r1378328  
    44Description: WP REST API (V2) Modifications for Woocommerce endpoints.
    55Author: Oleg Kostin
    6 Version: 1.0
     6Version: 1.0.1
    77Author URI: http://pmr.io
    88*/
     
    1515if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) :
    1616
    17     if ( ! function_exists ( 'wp_rest_is_shop_init' ) ) :
     17    /**
     18     * Check if WP REST API is active
     19     **/
     20    if ( in_array( 'rest-api/plugin.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) :
    1821
    19         /**
    20          * Init JSON REST API is_shop endpoint.
    21          *
    22          * @since 1.0.0
    23          */
    24         function wp_rest_is_shop_init() {
    25             register_rest_field(
    26                 'page',
    27                 'is_shop',
    28                 array(
    29                     'get_callback' => 'wp_rest_is_shop',
    30                 )
    31             );
    32         }
    3322
    34         /**
    35          * Handler for updating page data with is_shop.
    36          *
    37          * @since 1.0.0
    38          *
    39          * @param array $object The object from the response
    40          * @param string $field_name Name of field
    41          * @param WP_REST_Request $request Current request
    42          *
    43          * @return bool
    44          */
    45         function wp_rest_is_shop( $object, $field_name, $request ) {
    46             return wc_get_page_id( 'shop' ) === $object['id'];
    47         }
     23        if ( ! function_exists ( 'wp_rest_is_shop_init' ) ) :
    4824
    49         add_action( 'init', 'wp_rest_is_shop_init' );
     25            /**
     26             * Init JSON REST API is_shop endpoint.
     27             *
     28             * @since 1.0.0
     29             */
     30            function wp_rest_is_shop_init() {
     31                register_rest_field(
     32                    'page',
     33                    'is_shop',
     34                    array(
     35                        'get_callback' => 'wp_rest_is_shop',
     36                    )
     37                );
     38            }
     39
     40            /**
     41             * Handler for updating page data with is_shop.
     42             *
     43             * @since 1.0.0
     44             *
     45             * @param array $object The object from the response
     46             * @param string $field_name Name of field
     47             * @param WP_REST_Request $request Current request
     48             *
     49             * @return bool
     50             */
     51            function wp_rest_is_shop( $object, $field_name, $request ) {
     52                return wc_get_page_id( 'shop' ) === $object['id'];
     53            }
     54
     55            add_action( 'init', 'wp_rest_is_shop_init' );
     56
     57        endif;
     58
     59        if ( ! function_exists ( 'wp_rest_is_cart_init' ) ) :
     60
     61            /**
     62             * Init JSON REST API is_cart endpoint.
     63             *
     64             * @since 1.0.0
     65             */
     66            function wp_rest_is_cart_init() {
     67                register_rest_field(
     68                    'page',
     69                    'is_cart',
     70                    array(
     71                        'get_callback' => 'wp_rest_is_cart',
     72                    )
     73                );
     74            }
     75
     76            /**
     77             * Handler for updating page data with is_cart.
     78             *
     79             * @since 1.0.0
     80             *
     81             * @param array $object The object from the response
     82             * @param string $field_name Name of field
     83             * @param WP_REST_Request $request Current request
     84             *
     85             * @return bool
     86             */
     87            function wp_rest_is_cart( $object, $field_name, $request ) {
     88                return wc_get_page_id( 'cart' ) === $object['id'] || defined( 'WOOCOMMERCE_CART' );
     89            }
     90
     91            add_action( 'init', 'wp_rest_is_cart_init' );
     92
     93        endif;
     94
     95        if ( ! function_exists ( 'wp_rest_is_checkout_init' ) ) :
     96
     97            /**
     98             * Init JSON REST API is_checkout endpoint.
     99             *
     100             * @since 1.0.0
     101             */
     102            function wp_rest_is_checkout_init() {
     103                register_rest_field(
     104                    'page',
     105                    'is_checkout',
     106                    array(
     107                        'get_callback' => 'wp_rest_is_checkout',
     108                    )
     109                );
     110            }
     111
     112            /**
     113             * Handler for updating page data with is_checkout.
     114             *
     115             * @since 1.0.0
     116             *
     117             * @param array $object The object from the response
     118             * @param string $field_name Name of field
     119             * @param WP_REST_Request $request Current request
     120             *
     121             * @return bool
     122             */
     123            function wp_rest_is_checkout( $object, $field_name, $request ) {
     124                return wc_get_page_id( 'checkout' ) === $object['id'];
     125            }
     126
     127            add_action( 'init', 'wp_rest_is_checkout_init' );
     128
     129        endif;
     130
     131        if ( ! function_exists ( 'wp_rest_is_account_page_init' ) ) :
     132
     133            /**
     134             * Init JSON REST API is_account_page endpoint.
     135             *
     136             * @since 1.0.0
     137             */
     138            function wp_rest_is_account_page_init() {
     139                register_rest_field(
     140                    'page',
     141                    'is_account_page',
     142                    array(
     143                        'get_callback' => 'wp_rest_is_account_page',
     144                    )
     145                );
     146            }
     147
     148            /**
     149             * Handler for updating page data with is_account_page.
     150             *
     151             * @since 1.0.0
     152             *
     153             * @param array $object The object from the response
     154             * @param string $field_name Name of field
     155             * @param WP_REST_Request $request Current request
     156             *
     157             * @return bool
     158             */
     159            function wp_rest_is_account_page( $object, $field_name, $request ) {
     160                return wc_get_page_id( 'myaccount' ) === $object['id'];
     161            }
     162
     163            add_action( 'init', 'wp_rest_is_account_page_init' );
     164
     165        endif;
     166
    50167
    51168    endif;
    52169
    53     if ( ! function_exists ( 'wp_rest_is_cart_init' ) ) :
    54 
    55         /**
    56          * Init JSON REST API is_cart endpoint.
    57          *
    58          * @since 1.0.0
    59          */
    60         function wp_rest_is_cart_init() {
    61             register_rest_field(
    62                 'page',
    63                 'is_cart',
    64                 array(
    65                     'get_callback' => 'wp_rest_is_cart',
    66                 )
    67             );
    68         }
    69 
    70         /**
    71          * Handler for updating page data with is_cart.
    72          *
    73          * @since 1.0.0
    74          *
    75          * @param array $object The object from the response
    76          * @param string $field_name Name of field
    77          * @param WP_REST_Request $request Current request
    78          *
    79          * @return bool
    80          */
    81         function wp_rest_is_cart( $object, $field_name, $request ) {
    82             return wc_get_page_id( 'cart' ) === $object['id'] || defined( 'WOOCOMMERCE_CART' );
    83         }
    84 
    85         add_action( 'init', 'wp_rest_is_cart_init' );
    86 
    87     endif;
    88 
    89     if ( ! function_exists ( 'wp_rest_is_checkout_init' ) ) :
    90 
    91         /**
    92          * Init JSON REST API is_checkout endpoint.
    93          *
    94          * @since 1.0.0
    95          */
    96         function wp_rest_is_checkout_init() {
    97             register_rest_field(
    98                 'page',
    99                 'is_checkout',
    100                 array(
    101                     'get_callback' => 'wp_rest_is_checkout',
    102                 )
    103             );
    104         }
    105 
    106         /**
    107          * Handler for updating page data with is_checkout.
    108          *
    109          * @since 1.0.0
    110          *
    111          * @param array $object The object from the response
    112          * @param string $field_name Name of field
    113          * @param WP_REST_Request $request Current request
    114          *
    115          * @return bool
    116          */
    117         function wp_rest_is_checkout( $object, $field_name, $request ) {
    118             return wc_get_page_id( 'checkout' ) === $object['id'];
    119         }
    120 
    121         add_action( 'init', 'wp_rest_is_checkout_init' );
    122 
    123     endif;
    124 
    125     if ( ! function_exists ( 'wp_rest_is_account_page_init' ) ) :
    126 
    127         /**
    128          * Init JSON REST API is_account_page endpoint.
    129          *
    130          * @since 1.0.0
    131          */
    132         function wp_rest_is_account_page_init() {
    133             register_rest_field(
    134                 'page',
    135                 'is_account_page',
    136                 array(
    137                     'get_callback' => 'wp_rest_is_account_page',
    138                 )
    139             );
    140         }
    141 
    142         /**
    143          * Handler for updating page data with is_account_page.
    144          *
    145          * @since 1.0.0
    146          *
    147          * @param array $object The object from the response
    148          * @param string $field_name Name of field
    149          * @param WP_REST_Request $request Current request
    150          *
    151          * @return bool
    152          */
    153         function wp_rest_is_account_page( $object, $field_name, $request ) {
    154             return wc_get_page_id( 'myaccount' ) === $object['id'];
    155         }
    156 
    157         add_action( 'init', 'wp_rest_is_account_page_init' );
    158 
    159     endif;
    160170
    161171endif;
Note: See TracChangeset for help on using the changeset viewer.