2019-02-21 22:45:43 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce Import Tracking
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Tracks
|
|
|
|
*/
|
|
|
|
|
2020-01-29 05:21:29 +00:00
|
|
|
use Automattic\Jetpack\Constants;
|
|
|
|
|
2019-02-21 22:45:43 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class adds actions to track usage of WooCommerce Products.
|
|
|
|
*/
|
|
|
|
class WC_Products_Tracking {
|
|
|
|
/**
|
|
|
|
* Init tracking.
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function init() {
|
2020-05-07 22:39:19 +00:00
|
|
|
add_action( 'load-edit.php', array( $this, 'track_products_view' ), 10 );
|
2020-05-08 15:49:45 +00:00
|
|
|
add_action( 'load-edit-tags.php', array( $this, 'track_categories_and_tags_view' ), 10, 2 );
|
2019-03-05 00:30:01 +00:00
|
|
|
add_action( 'edit_post', array( $this, 'track_product_updated' ), 10, 2 );
|
|
|
|
add_action( 'transition_post_status', array( $this, 'track_product_published' ), 10, 3 );
|
|
|
|
add_action( 'created_product_cat', array( $this, 'track_product_category_created' ) );
|
2020-04-06 20:10:09 +00:00
|
|
|
add_action( 'add_meta_boxes_product', array( $this, 'track_product_updated_client_side' ), 10 );
|
2019-02-21 22:45:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 22:39:19 +00:00
|
|
|
/**
|
|
|
|
* Send a Tracks event when the Products page is viewed.
|
|
|
|
*/
|
|
|
|
public function track_products_view() {
|
2020-05-08 01:24:17 +00:00
|
|
|
// We only record Tracks event when no `_wp_http_referer` query arg is set, since
|
|
|
|
// when searching, the request gets sent from the browser twice,
|
|
|
|
// once with the `_wp_http_referer` and once without it.
|
|
|
|
//
|
|
|
|
// Otherwise, we would double-record the view and search events.
|
|
|
|
|
|
|
|
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification
|
2020-05-08 15:20:28 +00:00
|
|
|
if (
|
|
|
|
isset( $_GET['post_type'] )
|
2020-05-08 01:24:17 +00:00
|
|
|
&& 'product' === wp_unslash( $_GET['post_type'] )
|
2020-05-08 15:20:28 +00:00
|
|
|
&& ! isset( $_GET['_wp_http_referer'] )
|
|
|
|
) {
|
2020-05-08 01:24:17 +00:00
|
|
|
// phpcs:enable
|
|
|
|
|
2020-05-07 22:39:19 +00:00
|
|
|
WC_Tracks::record_event( 'products_view' );
|
2020-05-08 01:24:17 +00:00
|
|
|
|
2020-05-08 15:20:28 +00:00
|
|
|
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification
|
|
|
|
if (
|
|
|
|
isset( $_GET['s'] )
|
|
|
|
&& 0 < strlen( sanitize_text_field( wp_unslash( $_GET['s'] ) ) )
|
|
|
|
) {
|
|
|
|
// phpcs:enable
|
|
|
|
|
2020-05-08 01:24:17 +00:00
|
|
|
WC_Tracks::record_event( 'products_search' );
|
|
|
|
}
|
2020-05-07 22:39:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:49:45 +00:00
|
|
|
/**
|
|
|
|
* Send a Tracks event when the Products Categories and Tags page is viewed.
|
|
|
|
*/
|
|
|
|
public function track_categories_and_tags_view() {
|
|
|
|
// We only record Tracks event when no `_wp_http_referer` query arg is set, since
|
|
|
|
// when searching, the request gets sent from the browser twice,
|
|
|
|
// once with the `_wp_http_referer` and once without it.
|
|
|
|
//
|
|
|
|
// Otherwise, we would double-record the view and search events.
|
|
|
|
|
|
|
|
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification
|
|
|
|
if (
|
|
|
|
isset( $_GET['post_type'] )
|
|
|
|
&& 'product' === wp_unslash( $_GET['post_type'] )
|
|
|
|
&& isset( $_GET['taxonomy'] )
|
|
|
|
&& ! isset( $_GET['_wp_http_referer'] )
|
|
|
|
) {
|
|
|
|
$taxonomy = wp_unslash( $_GET['taxonomy'] );
|
|
|
|
// phpcs:enable
|
|
|
|
|
|
|
|
if ( 'product_cat' === $taxonomy ) {
|
|
|
|
WC_Tracks::record_event( 'categories_view' );
|
|
|
|
} elseif ( 'product_tag' === $taxonomy ) {
|
|
|
|
WC_Tracks::record_event( 'tags_view' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification
|
|
|
|
if (
|
|
|
|
isset( $_GET['s'] )
|
|
|
|
&& 0 < strlen( sanitize_text_field( wp_unslash( $_GET['s'] ) ) )
|
|
|
|
) {
|
|
|
|
// phpcs:enable
|
|
|
|
|
|
|
|
if ( 'product_cat' === $taxonomy ) {
|
|
|
|
WC_Tracks::record_event( 'categories_search' );
|
|
|
|
} elseif ( 'product_tag' === $taxonomy ) {
|
|
|
|
WC_Tracks::record_event( 'tags_search' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 22:39:19 +00:00
|
|
|
|
2019-02-21 22:45:43 +00:00
|
|
|
/**
|
2020-03-27 04:52:24 +00:00
|
|
|
* Send a Tracks event when a product is updated.
|
2019-02-21 22:45:43 +00:00
|
|
|
*
|
2019-03-05 00:30:01 +00:00
|
|
|
* @param int $product_id Product id.
|
2020-04-06 00:27:45 +00:00
|
|
|
* @param object $post WordPress post.
|
2019-02-21 22:45:43 +00:00
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_product_updated( $product_id, $post ) {
|
2019-02-21 22:45:43 +00:00
|
|
|
if ( 'product' !== $post->post_type ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$properties = array(
|
|
|
|
'product_id' => $product_id,
|
|
|
|
);
|
|
|
|
|
2019-04-08 03:37:36 +00:00
|
|
|
WC_Tracks::record_event( 'product_edit', $properties );
|
2020-03-26 00:55:31 +00:00
|
|
|
}
|
2020-03-17 01:27:57 +00:00
|
|
|
|
2020-04-06 00:27:45 +00:00
|
|
|
/**
|
|
|
|
* Track the Update button being clicked on the client side.
|
|
|
|
* This is needed because `track_product_updated` (using the `edit_post`
|
|
|
|
* hook) is called in response to a number of other triggers.
|
|
|
|
*
|
|
|
|
* @param WP_Post $post The post, not used.
|
|
|
|
*/
|
|
|
|
public function track_product_updated_client_side( $post ) {
|
|
|
|
wc_enqueue_js(
|
|
|
|
"
|
2020-04-06 01:22:24 +00:00
|
|
|
if ( $( 'h1.wp-heading-inline' ).text().trim() === '" . __( 'Edit product', 'woocommerce' ) . "') {
|
2020-04-06 00:27:45 +00:00
|
|
|
var initialStockValue = $( '#_stock' ).val();
|
|
|
|
var hasRecordedEvent = false;
|
|
|
|
|
|
|
|
$( '#publish' ).click( function() {
|
|
|
|
if ( hasRecordedEvent ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentStockValue = $( '#_stock' ).val();
|
|
|
|
var properties = {
|
|
|
|
product_type: $( '#product-type' ).val(),
|
|
|
|
is_virtual: $( '#_virtual' ).is( ':checked' ) ? 'Y' : 'N',
|
|
|
|
is_downloadable: $( '#_downloadable' ).is( ':checked' ) ? 'Y' : 'N',
|
|
|
|
manage_stock: $( '#_manage_stock' ).is( ':checked' ) ? 'Y' : 'N',
|
|
|
|
stock_quantity_update: ( initialStockValue != currentStockValue ) ? 'Y' : 'N',
|
|
|
|
};
|
|
|
|
|
|
|
|
window.wcTracks.recordEvent( 'product_update', properties );
|
|
|
|
hasRecordedEvent = true;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-22 01:34:06 +00:00
|
|
|
/**
|
|
|
|
* Send a Tracks event when a product is published.
|
|
|
|
*
|
|
|
|
* @param string $new_status New post_status.
|
|
|
|
* @param string $old_status Previous post_status.
|
|
|
|
* @param object $post WordPress post.
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_product_published( $new_status, $old_status, $post ) {
|
2019-02-22 01:34:06 +00:00
|
|
|
if (
|
|
|
|
'product' !== $post->post_type ||
|
|
|
|
'publish' !== $new_status ||
|
|
|
|
'publish' === $old_status
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$properties = array(
|
|
|
|
'product_id' => $post->ID,
|
|
|
|
);
|
|
|
|
|
|
|
|
WC_Tracks::record_event( 'product_add_publish', $properties );
|
|
|
|
}
|
2019-02-22 01:38:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a Tracks event when a product category is created.
|
|
|
|
*
|
|
|
|
* @param int $category_id Category ID.
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_product_category_created( $category_id ) {
|
2019-12-13 19:58:14 +00:00
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing
|
2019-02-22 01:38:50 +00:00
|
|
|
// Only track category creation from the edit product screen or the
|
|
|
|
// category management screen (which both occur via AJAX).
|
|
|
|
if (
|
2020-01-29 05:21:29 +00:00
|
|
|
! Constants::is_defined( 'DOING_AJAX' ) ||
|
2019-02-22 01:38:50 +00:00
|
|
|
empty( $_POST['action'] ) ||
|
|
|
|
(
|
|
|
|
// Product Categories screen.
|
|
|
|
'add-tag' !== $_POST['action'] &&
|
|
|
|
// Edit Product screen.
|
|
|
|
'add-product_cat' !== $_POST['action']
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$category = get_term( $category_id, 'product_cat' );
|
|
|
|
$properties = array(
|
|
|
|
'category_id' => $category_id,
|
|
|
|
'parent_id' => $category->parent,
|
|
|
|
'page' => ( 'add-tag' === $_POST['action'] ) ? 'categories' : 'product',
|
|
|
|
);
|
|
|
|
// phpcs:enable
|
|
|
|
|
|
|
|
WC_Tracks::record_event( 'product_category_add', $properties );
|
|
|
|
}
|
2019-02-21 22:45:43 +00:00
|
|
|
}
|