Plugin Directory

Changeset 3012052


Ignore:
Timestamp:
12/19/2023 03:22:53 PM (2 years ago)
Author:
shinystat
Message:

Upload v 1.0.14. Compatibility of shn_engage functions with wc 8.x . Added rest api to get cart content and update product quantity in cart.

Location:
shinystat-analytics
Files:
27 added
4 edited

Legend:

Unmodified
Added
Removed
  • shinystat-analytics/trunk/README.txt

    r2982691 r3012052  
    44Tags: analytics, counter, statistics, marketing, tracking, visits, optimize, seo, uniques, visitors, stats, conversions, engagement, recommendation, web analytics, marketing automation, ecommerce, ecommerce tracking, javascript error tracking, surveys, artificial intelligence
    55Requires at least: 3.1.0
    6 Tested up to: 6.3
    7 Stable tag: 1.0.13
     6Tested up to: 6.4
     7Stable tag: 1.0.14
    88Requires PHP: 5.6 or higher
    99License: GPLv2 or later
     
    5555== Changelog ==
    5656
     57= 1.0.14 =
     58* Extended rest api to get cart content and to change product quantity.
     59* Compatibily with woocommerce 8.x
     60
    5761= 1.0.13 =
    58 * Update css rule to improve compatibily with yoast plugin.
     62* Updated css rule to improve compatibily with yoast plugin.
    5963
    6064= 1.0.12 =
     
    6872
    6973= 1.0.9 =
    70 * Update timestamp in shn_engage.cart_content when add-to-cart is done by entire page refresh (not by ajax request wc-add-to-cart).
     74* Updated timestamp in shn_engage.cart_content when add-to-cart is done by entire page refresh (not by ajax request wc-add-to-cart).
    7175
    7276= 1.0.8 =
    73 * Fix timestamp update when cart is empty in get_cart_content js function
     77* Fixed timestamp update when cart is empty in get_cart_content js function
    7478
    7579= 1.0.7 =
     
    7781
    7882= 1.0.6 =
    79 * Fix empty scripts cleaning that are in page.
     83* Fixed empty scripts cleaning that are in page.
    8084
    8185= 1.0.5 =
    82 * Fix to update shn_engage.get_cart_content function result when cart and its items are null.
     86* Fixed to update shn_engage.get_cart_content function result when cart and its items are null.
    8387
    8488= 1.0.4 =
    85 * Fix to update shn_engage.get_cart_content function result when cart is null.
     89* Fixed to update shn_engage.get_cart_content function result when cart is null.
    8690
    8791= 1.0.3 =
    88 * Fix nonce to apply-coupon.
    89   For the application of coupon code to the current session, make an
    90   additional request to the server to get a valid nonce. When the cart session
    91   is still not initialized at the time of page request the computed nonce is
    92   not valid to apply coupon with the wc-ajax request.
     92* Fixed nonce to apply-coupon.
     93  For the application of coupon code to the current session, made an additional request to the server to get a valid nonce. When the cart session is still not initialized at the time of page request the computed nonce is not valid to apply coupon with the wc-ajax request.
    9394
    9495= 1.0.2 =
     
    105106== Upgrade Notice ==
    106107
     108= 1.0.14 =
     109* Compatibily of shn_engage functions with woocommerce 8.x.
     110
    107111= 1.0.13 =
    108 * Update style rules to improve compatibily with yoast plugin.
     112* Updated style rules to improve compatibily with yoast plugin.
    109113
    110114= 1.0.12 =
     
    118122
    119123= 1.0.9 =
    120 * Update timestamp in shn_engage.cart_content when add-to-cart is done by page refresh.
     124* Updated timestamp in shn_engage.cart_content when add-to-cart is done by page refresh.
    121125
    122126= 1.0.8 =
    123 * Fix timestamp update when cart is empty in get_cart_content js function
     127* Fixed timestamp update when cart is empty in get_cart_content js function
    124128
    125129= 1.0.7 =
     
    127131
    128132= 1.0.6 =
    129 Fix empty scripts cleaning that are in page.
     133Fixed empty scripts cleaning that are in page.
    130134
    131135= 1.0.5 =
    132 Fix to get valid cart content when cart items are null.
     136Fixed to get valid cart content when cart items are null.
    133137
    134138= 1.0.4 =
    135 Fix to get valid cart content when cart is null.
     139Fixed to get valid cart content when cart is null.
    136140
    137141= 1.0.3 =
    138 Fix to apply coupon successfully also when woocommerce cart session is still not initialized.
     142Fixed to apply coupon successfully also when woocommerce cart session is still not initialized.
    139143
    140144= 1.0.2 =
  • shinystat-analytics/trunk/includes/class-shinystat-analytics.php

    r2883680 r3012052  
    7070            $this->version = SHINYSTAT_ANALYTICS_VERSION;
    7171        } else {
    72             $this->version = '1.0.13';
     72            $this->version = '1.0.14';
    7373        }
    7474        $this->plugin_name = 'shinystat-analytics';
     
    192192
    193193        //inside the thankyou page add the conversion call with order parameters
    194         $this->loader->add_action( 'woocommerce_thankyou', $plugin_public, 'woocommerce_thankyou_send_convertion');
     194        $this->loader->add_action( 'woocommerce_thankyou', $plugin_public, 'woocommerce_thankyou_send_conversion');
    195195
    196196        //add the script to populate the shn_engage structure
    197197        $this->loader->add_action( 'wp_head', $plugin_public, 'woocommerce_cart_content' );
    198 
    199         //add custom endpoint to retrieve product information
     198 
     199        //add custom endpoint to retrieve cart/product information
    200200        $this->loader->add_action( 'rest_api_init', $plugin_public, 'register_shinystat_rest_route' );
    201201
    202202        //capture call adding product to cart and update the shn_engage structure inside the page navigation
    203         $this->loader->add_filter( 'woocommerce_add_to_cart_fragments', $plugin_public, 'woocommerce_add_to_cart_fragments', 10, 1);
    204 
     203        $this->loader->add_filter( 'woocommerce_add_to_cart_fragments', $plugin_public, 'woocommerce_add_to_cart_fragments', 10, 1);   
     204       
    205205        //add support to amp (by using the plugin https://it.wordpress.org/plugins/amp/)
    206206        $this->loader->add_filter( 'amp_post_template_analytics', $plugin_public, 'amp_add_shinystat_analytics' );
  • shinystat-analytics/trunk/public/class-shinystat-analytics-public.php

    r2834929 r3012052  
    44 * The public-facing functionality of the plugin.
    55 *
    6  * @link      https://www.shinystat.com
    7  * @since      1.0.0
     6 * @link    https://www.shinystat.com
     7 * @since   1.0.0
    88 *
    99 * @package    Shinystat_Analytics
     
    2828     * @since    1.0.0
    2929     * @access   private
    30      * @var      string    $plugin_name    The ID of this plugin.
     30     * @var string    $plugin_name    The ID of this plugin.
    3131     */
    3232    private $plugin_name;
     
    4646     * @since    1.0.0
    4747     * @access   private
    48      * @var      string    $version    The current version of this plugin.
     48     * @var string    $version    The current version of this plugin.
    4949     */
    5050    private $version;
     
    5454     *
    5555     * @since    1.0.0
    56      * @param      string    $plugin_name       The name of the plugin.
    57      * @param      string    $version    The version of this plugin.
     56     * @param   string    $plugin_name       The name of the plugin.
     57     * @param   string    $version    The version of this plugin.
    5858     */
    5959    public function __construct( $plugin_name, $version ) {
     
    119119     *
    120120     * @since    1.0.0
    121      * @param      string    $field       Content of the field to clean
     121     * @param   string    $field       Content of the field to clean
    122122     */
    123123    function clean_field($field, $encode=true) {
     
    142142     *   
    143143     * @since    1.0.0
    144      * @param      int    $order_id       The id of the processed order
    145      */
    146     public function woocommerce_thankyou_send_convertion( $order_id ) {
     144     * @param   int    $order_id       The id of the processed order
     145     */
     146    public function woocommerce_thankyou_send_conversion( $order_id ) {
    147147
    148148        $conv_name = get_option( $this->option_prefix . '_conv_name' );
     
    216216    }
    217217
     218                           
    218219    /**
    219220     * Check if the request contains POST attributes to add items in the cart.
     
    223224    private function is_post_add_to_cart( ) {
    224225        global $_POST;
    225 
    226         if ( isset($_POST["add-to-cart"]) && isset($_POST["quantity"]) && is_numeric($_POST["quantity"]) )
    227             return True;
    228 
     226       
     227        if ( isset($_POST["add-to-cart"]) || ( isset($_GET['wc-ajax']) && $_GET['wc-ajax'] == 'add_to_cart' ) ) {
     228       
     229            if ( isset($_POST["quantity"]) && is_numeric($_POST["quantity"]) )
     230                return True;
     231
     232        }
     233       
    229234        return False;
    230235    }
     236
    231237
    232238    /**
     
    254260                    var shn_engage  = {
    255261
    256                         cart_content: {},
    257    
    258262                        /**
    259263                         * Apply the redirect to cart or checkout page
     
    269273
    270274                        /**
    271                          * Call the callback function with the parameter shn_engage.cart_content
     275                         * Call the callback function with the parameter cart_content
    272276                         */
    273277                        get_cart_content: function(callback_fnc) {
    274278
    275                             callback_fnc(shn_engage.cart_content, "completed");
     279                            let xhr_prod = new XMLHttpRequest();
     280                            xhr_prod.open('GET', "<?php echo get_rest_url( null, 'shinystat/v1/cart' ); ?>" );
     281
     282                            xhr_prod.onload  = function() {
     283                                if (!!xhr_prod.responseText) {
     284                                   
     285                                    var jsonResp = JSON.parse(xhr_prod.responseText);
     286                                    if (!jsonResp)
     287                                        return;
     288
     289                                    jsonResp.cart_update_ts = localStorage.getItem("cart_update_ts");
     290                                    callback_fnc(jsonResp, "completed");
     291                               
     292                                }
     293                            }
     294
     295                            xhr_prod.send();
     296                        },
     297
     298
     299                        /**
     300                         * Set local storage cart_update_ts with current timestamp
     301                         */
     302                        update_timestamp: function( ) {
     303
     304                            var t  = Math.floor(new Date().getTime() / 1000);
     305                            localStorage.setItem("cart_update_ts", t);
    276306                           
    277                         },
    278 
    279                         /**
    280                          * Set the variable shn_engage.cart_content
    281                          */
    282                         set_cart_content: function(cart_content) {
    283 
    284                             if (!!shn_engage.cart_content &&
    285                                 typeof(shn_engage.cart_content.item_count) !== "undefined" &&
    286                                 shn_engage.cart_content.item_count != cart_content.item_count) {
    287                                    
    288                                     cart_content.cart_update_ts = shn_engage.update_timestamp() + "";
    289                             }
    290 
    291                             shn_engage.cart_content = cart_content;
    292 
    293                         },
    294 
    295                                 /**
    296                                  * Set local storage cart_update_ts with current timestamp
    297                                  */
    298                                 update_timestamp: function() {
    299            
    300                                         var t  = Math.floor(new Date().getTime() / 1000);
    301                                         localStorage.setItem("cart_update_ts", t);
    302                                    
    303307                            return t;
    304                                 },
     308                        },
    305309
    306310                        /**
     
    416420
    417421                        /**
    418                          * Get the key of the cart item from the product id
    419                          */
    420                         get_product_key_from_id(product_id) {
    421                             var key;
    422 
    423                             shn_engage.cart_content.items.forEach(function(prod) {
    424                                 if (prod["id"] == product_id)
    425                                     key = prod["key"];
    426                             });
    427 
    428                             return key;
    429                         },
    430 
    431                         /**
    432422                        * Set product quantity (identified by its variant id) in the cart of the current session.
    433423                        * To remove a product from cart, set quantity to zero.
     
    435425                        update_product_quantity: function(id, quantity=0, redirect="") {
    436426
    437                             let xhr = new XMLHttpRequest();
    438                             xhr.open('POST', woocommerce_params.wc_ajax_url.replace( '%%endpoint%%', 'remove_from_cart' ), );
    439 
    440                             xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    441 
    442                             xhr.onload  = function() {
    443                                 var jsonResp = JSON.parse(xhr.responseText);
    444                                 if (!jsonResp)
    445                                     return;
    446 
    447                                 shn_engage.add_product(id, quantity, redirect);
     427                            let xhr_prod = new XMLHttpRequest();
     428                            xhr_prod.open('POST', "<?php echo get_rest_url( null, 'shinystat/v1/set_product_quantity/' ); ?>");
     429                           
     430                            xhr_prod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     431
     432                            xhr_prod.onload  = function() {
     433                                if (!!xhr_prod.responseText) {
     434                                   
     435                                    var jsonResp = JSON.parse(xhr_prod.responseText);
     436                                    if (!jsonResp)
     437                                        return;
     438
     439                                    shn_engage.apply_redirect(redirect);
     440                               
     441                                }
    448442                            }
    449443
    450                             xhr.send("cart_item_key=" + shn_engage.get_product_key_from_id(id));
     444                            xhr_prod.send("product_id=" + id + "&quantity=" + quantity);
    451445                        },
    452446
     
    459453                        },
    460454
     455                        /**
     456                         * Get site-id from a page element and call add_event_listeners
     457                         *
     458                         */
     459                         init: function() {
     460
     461                             function addListener(e, t, n) {     
     462                                 window.jQuery && window.jQuery(e).bind ? window.jQuery(e).bind(t, n) :
     463                                     e.addEventListener ? e.addEventListener(t, n) :
     464                                     e.attachEvent && e.attachEvent('on' + t, n);   
     465                             }
     466
     467                             //listen wc-blocks_added_to_cart event to update cart timestamp
     468                             function detect_cart_change() {
     469                                setTimeout ( function() {
     470                                    addListener(document.body, 'wc-blocks_added_to_cart', function() {
     471                                        shn_engage.update_timestamp( )
     472                                    });
     473                                }, 2000);
     474                             }
     475
     476                             if (document.readyState == "complete")
     477                                detect_cart_change();
     478                             else
     479                                addListener(window, 'load', detect_cart_change());
     480                         }
    461481
    462482                    }
    463                
    464 <?php                   if ($update_cart_timestamp) {
    465 ?>                  shn_engage.update_timestamp();
    466 <?php                   }
     483
     484<?php                   if ( $update_cart_timestamp ) {
     485                        echo "\t\t\t\t\tshn_engage.update_timestamp( )";
     486                    }                                   
    467487?>
     488
     489
     490                    shn_engage.init();
    468491                   
    469492                    //set shn_engage object returning selected methods
     
    472495                            apply_discount:         shn_engage.apply_discount,
    473496                            get_cart_content:       shn_engage.get_cart_content,
    474                             set_cart_content:       shn_engage.set_cart_content,
    475497                            get_product_details:        shn_engage.get_product_details,
    476498                            add_product:            shn_engage.add_product,
    477                             update_product_quantity:shn_engage.update_product_quantity,
     499                            update_product_quantity:    shn_engage.update_product_quantity,
     500                            update_timestamp:       shn_engage.update_timestamp,
    478501                            is_woocommerce:         shn_engage.is_woocommerce,
    479502                        };
    480503                    };
     504
    481505
    482506                    var head = document.getElementsByTagName('head');
     
    487511                        head[0].append(s);
    488512                    }
    489                
     513
    490514                })();
    491515       
     
    494518        <?php
    495519
    496 
    497     }
    498 
    499 
    500     /**
    501      * Get the cart content and set the variable through shn_engage.set_cart_content
    502      * function.
    503      * This function is set when the cart is updated to keep information
    504      * inside shn_engage correct.
    505      *
     520    }
     521
     522
     523    /**
     524     * Update timestamp when it is an ajax post add-cart request
     525     *
    506526     * @since    1.0.0
    507527     */
    508528    function woocommerce_add_to_cart_fragments( $fragments ) {
    509        
     529
    510530        if ( ! function_exists('WC') )
    511531            return $fragments;
    512532
     533        $update_cart_timestamp = $this->is_post_add_to_cart();
     534       
     535
     536        $script = '<script type="text/javascript" id="shn-engage-cart-update">';
     537       
     538        if ( $update_cart_timestamp )
     539            $script .= 'shn_engage.update_timestamp( )';
     540
     541        $script .= '</script>';
     542
     543               
     544
     545        $fragments['script#shn-engage-cart-update'] = $script;
     546 
     547        return $fragments;
     548
     549    }
     550
     551
     552    /**
     553     * Set product quantity in the cart for the current session by using
     554     * post parameters product_id and quantity.
     555     *
     556     * @since    1.0.14
     557     */
     558    public function set_product_quantity( ) {
     559        global $_POST;
     560
     561        $product_id = $_POST['product_id'];
     562        $quantity = $_POST['quantity'];
     563
     564        if ( ! is_numeric($product_id) )
     565            return ['code' => 'parameter_value_not_valid_product_id', 'message' => 'product_id is not a numeric value'];
     566       
     567        if ( ! is_numeric($quantity) )
     568            return ['code' => 'parameter_value_not_valid_quantity', 'message' => 'quantity is not a numeric value'];
     569
     570
     571        if ( ! function_exists('WC') )
     572            return ['code' => 'wc_not_found', 'message' => ''];
     573
    513574        $cart = WC()->cart;
    514575
    515 
    516         ob_start();
    517         ?>
    518        
    519             <script type="text/javascript" id="shn-engage-cart-update">
    520        
    521                 (function() {
    522                
    523                     var cart_content = {
    524                         item_count: <?php echo ($cart != null) ? $cart->get_cart_contents_count() : 0; ?>,
    525                         total_price: <?php echo ($cart != null) ? (($cart->subtotal - $cart->get_discount_total()) * 100) : 0; ?>,
    526                         total_discount: <?php echo ($cart != null) ? ($cart->get_discount_total() * 100) : 0;  ?>,
    527                         currency: "<?php echo get_woocommerce_currency(); ?>",
    528                         cart_update_ts: localStorage.getItem("cart_update_ts"),
    529                         items: [],
    530                     }
    531        
    532                     <?php
    533                     $cart_items = ($cart != null) ? $cart->get_cart() : [];
    534                     foreach ( $cart_items as $cart_item_key => $cart_item ) {
    535                         $product = $cart_item['data'];
    536                         $product_json = json_decode($product, true);
    537                     ?>
    538                
    539                         cart_content.items.push( {
    540                             id:         <?php echo $product_json['id']; ?>,
    541                             quantity:       <?php echo $cart_item['quantity']; ?>,
    542                             key:            "<?php echo $this->clean_field($cart_item['key']); ?>",
    543                             handle:         "<?php echo $this->clean_field($product_json['slug']); ?>",
    544                             product_title:      "<?php echo $this->clean_field($product_json['name']); ?>",
    545                             product_type:       "<?php echo $this->clean_field($product_json['description']); ?>",
    546                             url:            "<?php echo $this->clean_field($product->get_permalink( $cart_item )) ?>",
    547                             compare_at_price:   <?php echo ( ($product_json['regular_price'] != '') ? $product_json['regular_price'] : 0 ) * 100; ?>,
    548                             price:          <?php echo $product_json['price'] * 100; ?>,
    549                             discounted_price:   <?php echo (int) ($cart->get_discount_total() * $product_json['price'] * 100 / ($cart->subtotal)) ?>,
    550                             options_with_values:    [],
    551                         } );
    552 
    553 
    554                         <?php
    555                         foreach ( $product_json['attributes'] as $name => $value ) {
    556                         ?>
    557                             cart_content.items[cart_content.items.length - 1].options_with_values.push( {
    558                                 name:       "<?php echo $this->clean_field($name); ?>",
    559                                 value:      "<?php echo $this->clean_field(is_array($value)?'':$value); ?>",
    560                             } );
    561                         <?php
    562                         }
    563                     }
    564                     ?>
    565        
    566                     shn_engage.set_cart_content(cart_content);
    567 
    568                 })();
    569             </script>
    570            
    571         <?php
    572        
    573         $fragments['script#shn-engage-cart-update'] = ob_get_clean();
    574         return $fragments;
     576        $product_key = null;
     577        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
     578            if ( $cart_item['product_id'] == $product_id || $cart_item['variation_id'] == $product_id ) {
     579                $product_key = $cart_item_key;
     580                break;
     581            }
     582        }
     583
     584        if ( ! $product_key )
     585            return ['code' => 'product_not_found', 'message' => 'product id does not correspond to any product in cart'];
     586
     587        $cart->set_quantity($product_key, $quantity);
     588
     589        return ['code' => 'successfull', 'message' => ''];
     590    }
     591       
     592       
     593    /**
     594     * Get data about specified wc product id for the rest api response.
     595     *
     596     * @since    1.0.12
     597     */
     598    public function get_product_details( $data ) {
     599       
     600        if ( ! function_exists('WC') )
     601            return ['code' => 'wc_not_found', 'message' => ''];
     602
     603        $product = wc_get_product( $data['id'] );
     604
     605        if ( ! $product )
     606            return ['code' => 'product_not_found', 'message' => 'product id does not correspond to any product'];
     607
     608
     609        $attributes = [];
     610        foreach ( $product->get_attributes() as $attribute ) {
     611            if ( is_string($attribute) ) {
     612                $attributes[] = $attribute;
     613            } else {
     614                $attribute_data = $attribute->get_data();
     615                $value = $attribute_data['value'];
     616                $attributes[] = [
     617                    'name' => $this->clean_field( $attribute_data['name'] ),
     618                    'value' => $this->clean_field( is_array($value) ? '' : $value ),
     619                ];
     620            }
     621        }
     622
     623        $categories = [];
     624        $terms = get_the_terms( $product->get_id(), 'product_cat' );
     625        if ( is_array($terms) ) {
     626            foreach( $terms as $term ) {
     627                $categories[] = $this->clean_field( $term->name );
     628            }
     629        }
     630
     631        return [
     632            'id' => $product->get_id(),
     633            'product_title' => $this->clean_field( $product->get_name() ),
     634            'product_type' => $this->clean_field( $product->get_description() ),
     635            'handle' => $this->clean_field( $product->get_slug() ),
     636            'url' => $this->clean_field( $product->get_permalink( $product->get_id() ) ),
     637            'image' => $this->clean_field( wp_get_attachment_url( $product->get_image_id() ) ),
     638            'categories' => $categories,
     639            'price' => $product->price * 100,
     640            'compare_at_price' => ( ($product->regular_price != '') ? $product->regular_price : 0 ) * 100,
     641            'options_with_values' => $attributes,
     642        ];
     643   
     644    }
     645
     646    /**
     647     * Get the wc cart content.
     648     *
     649     * @since    1.0.14
     650     */
     651    function get_cart_content( ) {
     652       
     653        if ( ! function_exists('WC') )
     654            return [];
     655
     656        $cart = WC()->cart;
     657
     658
     659        $cart_content = [
     660            'item_count' => ($cart != null) ? $cart->get_cart_contents_count() : 0,
     661            'total_price' => ($cart != null) ? (($cart->subtotal - $cart->get_discount_total()) * 100) : 0,
     662            'total_discount' => ($cart != null) ? ($cart->get_discount_total() * 100) : 0,
     663            'currency' => get_woocommerce_currency(),
     664            'cart_update_ts' => null,
     665            'items' => [],
     666        ];
     667
     668
     669        $cart_items = ($cart != null) ? $cart->get_cart() : [];
     670        foreach ( $cart_items as $cart_item_key => $cart_item ) {
     671            $product = $cart_item['data'];
     672            $product_json = json_decode($product, true);
     673            $attributes = [];
     674            foreach ( $product_json['attributes'] as $name => $value ) {
     675                $attributes[] = [
     676                    'name' =>   $this->clean_field($name),
     677                    'value' =>      $this->clean_field(is_array($value) ? '' : $value),
     678                ];
     679            }
     680     
     681            $cart_content["items"][] = [
     682                'id' =>                     $product_json['id'],
     683                'quantity' =>               $cart_item['quantity'],
     684                'key' =>                    $this->clean_field($cart_item['key']),
     685                'handle' =>                 $this->clean_field($product_json['slug']),
     686                'product_title' =>          $this->clean_field($product_json['name']),
     687                'product_type' =>           $this->clean_field($product_json['description']),
     688                'url' =>                    $this->clean_field($product->get_permalink( $cart_item )),
     689                'compare_at_price' =>       ( ($product_json['regular_price'] != '') ? $product_json['regular_price'] : 0 ) * 100,
     690                'price' =>                  $product_json['price'] * 100,
     691                'discounted_price' =>       (int) ($cart->get_discount_total() * $product_json['price'] * 100 / ($cart->subtotal)),
     692                'options_with_values' =>    $attributes,
     693            ];
     694        }
     695
     696
     697        return $cart_content;
     698    }
     699
     700
     701
     702    /**
     703     * Register rest route to get data about specified wc product.
     704     * The route is /?rest_route=/shinystat/v1/cart
     705     * The route is /?rest_route=/shinystat/v1/product/{prod_id}
     706     * The route is /?rest_route=/shinystat/v1/set_product_quantity
     707     *
     708     * @since    1.0.12
     709     */
     710    public function register_shinystat_rest_route() {
     711   
     712        register_rest_route( 'shinystat/v1', 'product/(?P<id>\d+)', [
     713            'methods' => [ 'GET' ],
     714            'callback' => array($this, 'get_product_details')
     715        ]);
     716
     717        register_rest_route( 'shinystat/v1', 'cart', [
     718            'methods' => [ 'GET' ],
     719            'callback' => array($this, 'get_cart_content')
     720        ]);
     721       
     722        register_rest_route( 'shinystat/v1', 'set_product_quantity', [
     723            'methods' => [ 'POST' ],
     724            'callback' => array($this, 'set_product_quantity')
     725        ]);
     726
    575727    }
    576728
     
    626778
    627779        $account_name = get_option( $this->option_prefix . '_account_name' );
     780
    628781
    629782        if ( ! is_array( $analytics_entries ) ) {
     
    649802
    650803
    651     /**
    652      * Get data about specified wc product id for the rest api response.
    653      *
    654      * @since    1.0.12
    655      */
    656     public function get_product_details( $data ) {
    657        
    658         if ( ! function_exists('WC') )
    659             return ['code' => 'wc_not_found', 'message' => ''];
    660 
    661         $product = wc_get_product( $data['id'] );
    662 
    663         if ( ! $product )
    664             return ['code' => 'product_not_found', 'message' => 'product id does not correspond to any product'];
    665 
    666 
    667         $attributes = [];
    668         foreach ( $product->get_attributes() as $attribute ) {
    669             if ( is_string($attribute) ) {
    670                 $attributes[] = $attribute;
    671             } else {
    672                 $attribute_data = $attribute->get_data();
    673                 $value = $attribute_data['value'];
    674                 $attributes[] = [
    675                     'name' => $this->clean_field( $attribute_data['name'] ),
    676                     'value' => $this->clean_field( is_array($value) ? '' : $value ),
    677                 ];
    678             }
    679         }
    680 
    681         $categories = [];
    682         $terms = get_the_terms( $product->get_id(), 'product_cat' );
    683         if ( is_array($terms) ) {
    684             foreach( $terms as $term ) {
    685                 $categories[] = $this->clean_field( $term->name );
    686             }
    687         }
    688 
    689         return [
    690             'id' => $product->get_id(),
    691             'product_title' => $this->clean_field( $product->get_name() ),
    692             'product_type' => $this->clean_field( $product->get_description() ),
    693             'handle' => $this->clean_field( $product->get_slug() ),
    694             'url' => $this->clean_field( $product->get_permalink( $product->get_id() ) ),
    695             'image' => $this->clean_field( wp_get_attachment_url( $product->get_image_id() ) ),
    696             'categories' => $categories,
    697             'price' => $product->price * 100,
    698             'compare_at_price' => ( ($product->regular_price != '') ? $product->regular_price : 0 ) * 100,
    699             'options_with_values' => $attributes,
    700         ];
    701    
    702     }
    703 
    704 
    705     /**
    706      * Register rest route to get data about specified wc product.
    707      * The route is /?rest_route=/shinystat/v1/product/{prod_id}
    708      *
    709      * @since    1.0.12
    710      */
    711     public function register_shinystat_rest_route() {
    712    
    713         register_rest_route( 'shinystat/v1', 'product/(?P<id>\d+)', [
    714             'methods' => [ 'GET' ],
    715             'callback' => array($this, 'get_product_details')
    716         ]);
    717 
    718     }
    719 
    720 
    721804}
  • shinystat-analytics/trunk/shinystat-analytics.php

    r2883680 r3012052  
    1717 * Plugin URI:        https://wordpress.org/plugins/shinystat-analytics/
    1818 * Description:       Activate the plugin and start to use ShinyStat Web Analytics and On-site Marketing Automation tools.
    19  * Version:           1.0.13
     19 * Version:           1.0.14
    2020 * Author:            ShinyStat
    2121 * Author URI:        https://www.shinystat.com
     
    3636 * Rename this for your plugin and update it as you release new versions.
    3737 */
    38 define( 'SHINYSTAT_ANALYTICS_VERSION', '1.0.13' );
     38define( 'SHINYSTAT_ANALYTICS_VERSION', '1.0.14' );
    3939
    4040/**
Note: See TracChangeset for help on using the changeset viewer.