Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WooCommerce Anaylytics: PHPCS #16032

Merged
merged 6 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/phpcs-whitelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion modules/woocommerce-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand All @@ -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 );
}
Expand All @@ -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 = '<script>window._wca_prevent_referrer = true;</script>';
echo "$prevent_referrer_code\r\n";
echo '<script>window._wca_prevent_referrer = true;</script>' . "\r\n";
}
$wca_code = '<script>window._wca = window._wca || [];</script>';
echo "$wca_code\r\n";
echo '<script>window._wca = window._wca || [];</script>' . "\r\n";
}


Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {

Expand All @@ -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 ) ) {
Expand All @@ -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 );
Expand Down