Changeset 3012052
- Timestamp:
- 12/19/2023 03:22:53 PM (2 years ago)
- Location:
- shinystat-analytics
- Files:
-
- 27 added
- 4 edited
-
tags/1.0.14 (added)
-
tags/1.0.14/LICENSE.txt (added)
-
tags/1.0.14/README.txt (added)
-
tags/1.0.14/admin (added)
-
tags/1.0.14/admin/class-shinystat-analytics-admin.php (added)
-
tags/1.0.14/admin/css (added)
-
tags/1.0.14/admin/css/shinystat-analytics-admin.css (added)
-
tags/1.0.14/admin/images (added)
-
tags/1.0.14/admin/images/conversions_en.png (added)
-
tags/1.0.14/admin/images/conversions_it.png (added)
-
tags/1.0.14/admin/partials (added)
-
tags/1.0.14/admin/partials/shinystat-analytics-admin-display.php (added)
-
tags/1.0.14/includes (added)
-
tags/1.0.14/includes/class-shinystat-analytics-activator.php (added)
-
tags/1.0.14/includes/class-shinystat-analytics-deactivator.php (added)
-
tags/1.0.14/includes/class-shinystat-analytics-i18n.php (added)
-
tags/1.0.14/includes/class-shinystat-analytics-loader.php (added)
-
tags/1.0.14/includes/class-shinystat-analytics-widget.php (added)
-
tags/1.0.14/includes/class-shinystat-analytics.php (added)
-
tags/1.0.14/languages (added)
-
tags/1.0.14/languages/shinystat-analytics-it_IT.mo (added)
-
tags/1.0.14/languages/shinystat-analytics-it_IT.po (added)
-
tags/1.0.14/languages/shinystat-analytics.pot (added)
-
tags/1.0.14/public (added)
-
tags/1.0.14/public/class-shinystat-analytics-public.php (added)
-
tags/1.0.14/shinystat-analytics.php (added)
-
tags/1.0.14/uninstall.php (added)
-
trunk/README.txt (modified) (7 diffs)
-
trunk/includes/class-shinystat-analytics.php (modified) (2 diffs)
-
trunk/public/class-shinystat-analytics-public.php (modified) (18 diffs)
-
trunk/shinystat-analytics.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
shinystat-analytics/trunk/README.txt
r2982691 r3012052 4 4 Tags: 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 5 5 Requires at least: 3.1.0 6 Tested up to: 6. 37 Stable tag: 1.0.1 36 Tested up to: 6.4 7 Stable tag: 1.0.14 8 8 Requires PHP: 5.6 or higher 9 9 License: GPLv2 or later … … 55 55 == Changelog == 56 56 57 = 1.0.14 = 58 * Extended rest api to get cart content and to change product quantity. 59 * Compatibily with woocommerce 8.x 60 57 61 = 1.0.13 = 58 * Update css rule to improve compatibily with yoast plugin.62 * Updated css rule to improve compatibily with yoast plugin. 59 63 60 64 = 1.0.12 = … … 68 72 69 73 = 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). 71 75 72 76 = 1.0.8 = 73 * Fix timestamp update when cart is empty in get_cart_content js function77 * Fixed timestamp update when cart is empty in get_cart_content js function 74 78 75 79 = 1.0.7 = … … 77 81 78 82 = 1.0.6 = 79 * Fix empty scripts cleaning that are in page.83 * Fixed empty scripts cleaning that are in page. 80 84 81 85 = 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. 83 87 84 88 = 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. 86 90 87 91 = 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. 93 94 94 95 = 1.0.2 = … … 105 106 == Upgrade Notice == 106 107 108 = 1.0.14 = 109 * Compatibily of shn_engage functions with woocommerce 8.x. 110 107 111 = 1.0.13 = 108 * Update style rules to improve compatibily with yoast plugin.112 * Updated style rules to improve compatibily with yoast plugin. 109 113 110 114 = 1.0.12 = … … 118 122 119 123 = 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. 121 125 122 126 = 1.0.8 = 123 * Fix timestamp update when cart is empty in get_cart_content js function127 * Fixed timestamp update when cart is empty in get_cart_content js function 124 128 125 129 = 1.0.7 = … … 127 131 128 132 = 1.0.6 = 129 Fix empty scripts cleaning that are in page.133 Fixed empty scripts cleaning that are in page. 130 134 131 135 = 1.0.5 = 132 Fix to get valid cart content when cart items are null.136 Fixed to get valid cart content when cart items are null. 133 137 134 138 = 1.0.4 = 135 Fix to get valid cart content when cart is null.139 Fixed to get valid cart content when cart is null. 136 140 137 141 = 1.0.3 = 138 Fix to apply coupon successfully also when woocommerce cart session is still not initialized.142 Fixed to apply coupon successfully also when woocommerce cart session is still not initialized. 139 143 140 144 = 1.0.2 = -
shinystat-analytics/trunk/includes/class-shinystat-analytics.php
r2883680 r3012052 70 70 $this->version = SHINYSTAT_ANALYTICS_VERSION; 71 71 } else { 72 $this->version = '1.0.1 3';72 $this->version = '1.0.14'; 73 73 } 74 74 $this->plugin_name = 'shinystat-analytics'; … … 192 192 193 193 //inside the thankyou page add the conversion call with order parameters 194 $this->loader->add_action( 'woocommerce_thankyou', $plugin_public, 'woocommerce_thankyou_send_conver tion');194 $this->loader->add_action( 'woocommerce_thankyou', $plugin_public, 'woocommerce_thankyou_send_conversion'); 195 195 196 196 //add the script to populate the shn_engage structure 197 197 $this->loader->add_action( 'wp_head', $plugin_public, 'woocommerce_cart_content' ); 198 199 //add custom endpoint to retrieve product information198 199 //add custom endpoint to retrieve cart/product information 200 200 $this->loader->add_action( 'rest_api_init', $plugin_public, 'register_shinystat_rest_route' ); 201 201 202 202 //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 205 205 //add support to amp (by using the plugin https://it.wordpress.org/plugins/amp/) 206 206 $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 4 4 * The public-facing functionality of the plugin. 5 5 * 6 * @link https://www.shinystat.com7 * @since 1.0.06 * @link https://www.shinystat.com 7 * @since 1.0.0 8 8 * 9 9 * @package Shinystat_Analytics … … 28 28 * @since 1.0.0 29 29 * @access private 30 * @var string $plugin_name The ID of this plugin.30 * @var string $plugin_name The ID of this plugin. 31 31 */ 32 32 private $plugin_name; … … 46 46 * @since 1.0.0 47 47 * @access private 48 * @var string $version The current version of this plugin.48 * @var string $version The current version of this plugin. 49 49 */ 50 50 private $version; … … 54 54 * 55 55 * @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. 58 58 */ 59 59 public function __construct( $plugin_name, $version ) { … … 119 119 * 120 120 * @since 1.0.0 121 * @param string $field Content of the field to clean121 * @param string $field Content of the field to clean 122 122 */ 123 123 function clean_field($field, $encode=true) { … … 142 142 * 143 143 * @since 1.0.0 144 * @param int $order_id The id of the processed order145 */ 146 public function woocommerce_thankyou_send_conver tion( $order_id ) {144 * @param int $order_id The id of the processed order 145 */ 146 public function woocommerce_thankyou_send_conversion( $order_id ) { 147 147 148 148 $conv_name = get_option( $this->option_prefix . '_conv_name' ); … … 216 216 } 217 217 218 218 219 /** 219 220 * Check if the request contains POST attributes to add items in the cart. … … 223 224 private function is_post_add_to_cart( ) { 224 225 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 229 234 return False; 230 235 } 236 231 237 232 238 /** … … 254 260 var shn_engage = { 255 261 256 cart_content: {},257 258 262 /** 259 263 * Apply the redirect to cart or checkout page … … 269 273 270 274 /** 271 * Call the callback function with the parameter shn_engage.cart_content275 * Call the callback function with the parameter cart_content 272 276 */ 273 277 get_cart_content: function(callback_fnc) { 274 278 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); 276 306 277 },278 279 /**280 * Set the variable shn_engage.cart_content281 */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 timestamp297 */298 update_timestamp: function() {299 300 var t = Math.floor(new Date().getTime() / 1000);301 localStorage.setItem("cart_update_ts", t);302 303 307 return t; 304 },308 }, 305 309 306 310 /** … … 416 420 417 421 /** 418 * Get the key of the cart item from the product id419 */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 /**432 422 * Set product quantity (identified by its variant id) in the cart of the current session. 433 423 * To remove a product from cart, set quantity to zero. … … 435 425 update_product_quantity: function(id, quantity=0, redirect="") { 436 426 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 } 448 442 } 449 443 450 xhr .send("cart_item_key=" + shn_engage.get_product_key_from_id(id));444 xhr_prod.send("product_id=" + id + "&quantity=" + quantity); 451 445 }, 452 446 … … 459 453 }, 460 454 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 } 461 481 462 482 } 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 } 467 487 ?> 488 489 490 shn_engage.init(); 468 491 469 492 //set shn_engage object returning selected methods … … 472 495 apply_discount: shn_engage.apply_discount, 473 496 get_cart_content: shn_engage.get_cart_content, 474 set_cart_content: shn_engage.set_cart_content,475 497 get_product_details: shn_engage.get_product_details, 476 498 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, 478 501 is_woocommerce: shn_engage.is_woocommerce, 479 502 }; 480 503 }; 504 481 505 482 506 var head = document.getElementsByTagName('head'); … … 487 511 head[0].append(s); 488 512 } 489 513 490 514 })(); 491 515 … … 494 518 <?php 495 519 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 * 506 526 * @since 1.0.0 507 527 */ 508 528 function woocommerce_add_to_cart_fragments( $fragments ) { 509 529 510 530 if ( ! function_exists('WC') ) 511 531 return $fragments; 512 532 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 513 574 $cart = WC()->cart; 514 575 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 575 727 } 576 728 … … 626 778 627 779 $account_name = get_option( $this->option_prefix . '_account_name' ); 780 628 781 629 782 if ( ! is_array( $analytics_entries ) ) { … … 649 802 650 803 651 /**652 * Get data about specified wc product id for the rest api response.653 *654 * @since 1.0.12655 */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.12710 */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 721 804 } -
shinystat-analytics/trunk/shinystat-analytics.php
r2883680 r3012052 17 17 * Plugin URI: https://wordpress.org/plugins/shinystat-analytics/ 18 18 * Description: Activate the plugin and start to use ShinyStat Web Analytics and On-site Marketing Automation tools. 19 * Version: 1.0.1 319 * Version: 1.0.14 20 20 * Author: ShinyStat 21 21 * Author URI: https://www.shinystat.com … … 36 36 * Rename this for your plugin and update it as you release new versions. 37 37 */ 38 define( 'SHINYSTAT_ANALYTICS_VERSION', '1.0.1 3' );38 define( 'SHINYSTAT_ANALYTICS_VERSION', '1.0.14' ); 39 39 40 40 /**
Note: See TracChangeset
for help on using the changeset viewer.