diff --git a/bin/phpcs-whitelist.js b/bin/phpcs-whitelist.js index 58a8a1ba002c3..edc21fa502f6b 100644 --- a/bin/phpcs-whitelist.js +++ b/bin/phpcs-whitelist.js @@ -58,6 +58,7 @@ module.exports = [ 'modules/widgets/contact-info.php', 'modules/widgets/eu-cookie-law/widget-amp.php', 'modules/widgets/social-icons.php', + 'modules/woocommerce-analytics/', 'modules/woocommerce-analytics.php', 'modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php', 'modules/wpcom-tos/wpcom-tos.php', diff --git a/modules/woocommerce-analytics.php b/modules/woocommerce-analytics.php index 69fe6b6008dc9..cf7623548bdb7 100644 --- a/modules/woocommerce-analytics.php +++ b/modules/woocommerce-analytics.php @@ -17,6 +17,6 @@ * Load module functionality. */ function jetpack_load_woocommerce_analytics() { - require_once dirname( __FILE__ ) . '/woocommerce-analytics/wp-woocommerce-analytics.php'; + require_once dirname( __FILE__ ) . '/woocommerce-analytics/class-jetpack-woocommerce-analytics.php'; } jetpack_load_woocommerce_analytics(); diff --git a/modules/woocommerce-analytics/wp-woocommerce-analytics.php b/modules/woocommerce-analytics/class-jetpack-woocommerce-analytics.php similarity index 78% rename from modules/woocommerce-analytics/wp-woocommerce-analytics.php rename to modules/woocommerce-analytics/class-jetpack-woocommerce-analytics.php index d078af40e7dc5..57d511ebcf83b 100644 --- a/modules/woocommerce-analytics/wp-woocommerce-analytics.php +++ b/modules/woocommerce-analytics/class-jetpack-woocommerce-analytics.php @@ -9,7 +9,7 @@ exit; } -require_once plugin_basename( 'classes/wp-woocommerce-analytics-universal.php' ); +require_once plugin_basename( 'classes/class-jetpack-woocommerce-analytics-universal.php' ); /** * Class Jetpack_WooCommerce_Analytics @@ -37,28 +37,28 @@ class Jetpack_WooCommerce_Analytics { * * @return bool */ - public static function shouldTrackStore() { + public static function should_track_store() { /** * Make sure WooCommerce is installed and active * * This action is documented in https://docs.woocommerce.com/document/create-a-plugin */ - if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', Jetpack::get_active_plugins() ) ) ) { + if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', Jetpack::get_active_plugins() ), true ) ) { return false; } - // Tracking only Site pages + // Tracking only Site pages. if ( is_admin() ) { return false; } - // Don't track site admins - if ( is_user_logged_in() && in_array( 'administrator', wp_get_current_user()->roles ) ) { + // Don't track site admins. + if ( is_user_logged_in() && in_array( 'administrator', wp_get_current_user()->roles, true ) ) { return false; } - // Make sure Jetpack is installed and active + // Make sure Jetpack is installed and active. if ( ! Jetpack::is_active() ) { return false; } - // Ensure the WooCommerce class exists and is a valid version + // Ensure the WooCommerce class exists and is a valid version. $minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' ); if ( ! $minimum_woocommerce_active ) { return false; @@ -72,14 +72,14 @@ public static function shouldTrackStore() { * @return void */ private function __construct() { - $analytics = new Jetpack_WooCommerce_Analytics_Universal(); + self::$analytics = new Jetpack_WooCommerce_Analytics_Universal(); } /** * Function to instantiate our class and make it a singleton */ public static function get_instance() { - if ( ! self::shouldTrackStore() ) { + if ( ! self::should_track_store() ) { return; } if ( ! self::$instance ) { diff --git a/modules/woocommerce-analytics/classes/wp-woocommerce-analytics-universal.php b/modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-universal.php similarity index 92% rename from modules/woocommerce-analytics/classes/wp-woocommerce-analytics-universal.php rename to modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-universal.php index 36adeb41b2623..4e2c6a4031d0b 100644 --- a/modules/woocommerce-analytics/classes/wp-woocommerce-analytics-universal.php +++ b/modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-universal.php @@ -24,19 +24,19 @@ class Jetpack_WooCommerce_Analytics_Universal { * Jetpack_WooCommerce_Analytics_Universal constructor. */ public function __construct() { - // loading _wca + // loading _wca. add_action( 'wp_head', array( $this, 'wp_head_top' ), 1 ); - // add to carts from non-product pages or lists (search, store etc.) + // add to carts from non-product pages or lists -- search, store etc. add_action( 'wp_head', array( $this, 'loop_session_events' ), 2 ); // loading s.js. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_tracking_script' ) ); - // Capture cart events + // Capture cart events. add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 ); - // single product page view + // single product page view. add_action( 'woocommerce_after_single_product', array( $this, 'capture_product_view' ) ); add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) ); @@ -50,7 +50,7 @@ public function __construct() { // Send events after checkout block. add_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after', array( $this, 'checkout_process' ) ); - // order confirmed + // order confirmed. add_action( 'woocommerce_thankyou', array( $this, 'order_process' ), 10, 1 ); add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart_via_quantity' ), 10, 1 ); } @@ -60,11 +60,9 @@ public function __construct() { */ public function wp_head_top() { if ( is_cart() || is_checkout() || is_checkout_pay_page() || is_order_received_page() || is_add_payment_method_page() ) { - $prevent_referrer_code = ''; - echo "$prevent_referrer_code\r\n"; + echo '' . "\r\n"; } - $wca_code = ''; - echo "$wca_code\r\n"; + echo '' . "\r\n"; } @@ -222,7 +220,7 @@ public function remove_from_cart_attributes( $url, $key ) { /** * Gather relevant product information * - * @param array $product product + * @param array $product product. * @return array */ public function get_product_details( $product ) { @@ -338,29 +336,33 @@ public function get_user_id() { } /** - * @param $cart_item_key - * @param $product_id - * @param $quantity - * @param $variation_id - * @param $variation - * @param $cart_item_data + * Track adding items to the cart. + * + * @param string $cart_item_key Cart item key. + * @param int $product_id Product added to cart. + * @param int $quantity Quantity added to cart. + * @param int $variation_id Product variation. + * @param array $variation Variation attributes.. + * @param array $cart_item_data Other cart data. */ public function capture_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { $referer_postid = isset( $_SERVER['HTTP_REFERER'] ) ? url_to_postid( $_SERVER['HTTP_REFERER'] ) : 0; - // if the referring post is not a product OR the product being added is not the same as post - // (eg. related product list on single product page) then include a product view event + // if the referring post is not a product OR the product being added is not the same as post. + // (eg. related product list on single product page) then include a product view event. $product_by_referer_postid = wc_get_product( $referer_postid ); if ( ! $product_by_referer_postid instanceof WC_Product || (int) $product_id !== $referer_postid ) { $this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_product_view' ); } - // add cart event to the session data + // add cart event to the session data. $this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_add_to_cart' ); } /** - * @param $product_id - * @param $quantity - * @param $event + * Track in-session data. + * + * @param int $product_id Product ID. + * @param int $quantity Quantity. + * @param string $event Fired event. */ public function capture_event_in_session_data( $product_id, $quantity, $event ) { @@ -369,9 +371,9 @@ public function capture_event_in_session_data( $product_id, $quantity, $event ) return; } - $quantity = ( $quantity == 0 ) ? 1 : $quantity; + $quantity = ( 0 === $quantity ) ? 1 : $quantity; - // check for existing data + // check for existing data. if ( is_object( WC()->session ) ) { $data = WC()->session->get( 'wca_session_data' ); if ( empty( $data ) || ! is_array( $data ) ) { @@ -381,14 +383,14 @@ public function capture_event_in_session_data( $product_id, $quantity, $event ) $data = array(); } - // extract new event data + // extract new event data. $new_data = array( 'event' => $event, 'product_id' => (string) $product_id, 'quantity' => (string) $quantity, ); - // append new data + // append new data. $data[] = $new_data; WC()->session->set( 'wca_session_data', $data );