Plugin Directory

Changeset 2834929


Ignore:
Timestamp:
12/16/2022 08:34:24 AM (3 years ago)
Author:
shinystat
Message:

Update files in trunk folder for version 1.0.12

Location:
shinystat-analytics/trunk
Files:
4 edited

Legend:

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

    r2816928 r2834929  
    55Requires at least: 3.1.0
    66Tested up to: 6.1
    7 Stable tag: 1.0.11
     7Stable tag: 1.0.12
    88Requires PHP: 5.6 or higher
    99License: GPLv2 or later
     
    5555== Changelog ==
    5656
     57= 1.0.12 =
     58* Added function shn_engage.get_product_details that calls rest api to retrieve product information from id.
     59
    5760= 1.0.11 =
    5861* Compatibility with not defined advanced options section.
     
    98101
    99102== Upgrade Notice ==
     103
     104= 1.0.12 =
     105* Added function shn_engage.get_product_details to get product information from id.
    100106
    101107= 1.0.11 =
  • shinystat-analytics/trunk/includes/class-shinystat-analytics.php

    r2816928 r2834929  
    7070            $this->version = SHINYSTAT_ANALYTICS_VERSION;
    7171        } else {
    72             $this->version = '1.0.11';
     72            $this->version = '1.0.12';
    7373        }
    7474        $this->plugin_name = 'shinystat-analytics';
     
    197197        $this->loader->add_action( 'wp_head', $plugin_public, 'woocommerce_cart_content' );
    198198
     199        //add custom endpoint to retrieve product information
     200        $this->loader->add_action( 'rest_api_init', $plugin_public, 'register_shinystat_rest_route' );
     201
    199202        //capture call adding product to cart and update the shn_engage structure inside the page navigation
    200203        $this->loader->add_filter( 'woocommerce_add_to_cart_fragments', $plugin_public, 'woocommerce_add_to_cart_fragments', 10, 1);
  • shinystat-analytics/trunk/public/class-shinystat-analytics-public.php

    r2816928 r2834929  
    370370
    371371                        /**
     372                         * Get product details by product id
     373                         */
     374                        get_product_details: function(callback_fnc, prod_id) {
     375                           
     376                            let xhr_prod = new XMLHttpRequest();
     377                            xhr_prod.open('GET', "<?php echo get_rest_url( null, 'shinystat/v1/product/' ); ?>" + prod_id);
     378
     379                            xhr_prod.onload  = function() {
     380                                if (!!xhr_prod.responseText) {
     381                                   
     382                                    var jsonResp = JSON.parse(xhr_prod.responseText);
     383                                    if (!jsonResp)
     384                                        return;
     385
     386                                    callback_fnc(jsonResp);
     387                               
     388                                }
     389                            }
     390
     391                            xhr_prod.send();
     392                        },
     393
     394
     395                        /**
    372396                         * Add product (identified by its variant id) for input quantity
    373397                         * to the cart of the current session
     
    419443                                var jsonResp = JSON.parse(xhr.responseText);
    420444                                if (!jsonResp)
    421                                 return;
     445                                    return;
    422446
    423447                                shn_engage.add_product(id, quantity, redirect);
     
    449473                            get_cart_content:       shn_engage.get_cart_content,
    450474                            set_cart_content:       shn_engage.set_cart_content,
     475                            get_product_details:        shn_engage.get_product_details,
    451476                            add_product:            shn_engage.add_product,
    452477                            update_product_quantity:shn_engage.update_product_quantity,
     
    624649
    625650
     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
    626721}
  • shinystat-analytics/trunk/shinystat-analytics.php

    r2816928 r2834929  
    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.11
     19 * Version:           1.0.12
    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.11' );
     38define( 'SHINYSTAT_ANALYTICS_VERSION', '1.0.12' );
    3939
    4040/**
Note: See TracChangeset for help on using the changeset viewer.