first commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace PixelYourSite;
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
class GroupedEvent extends PYSEvent
|
||||
{
|
||||
private $events = array();
|
||||
public function __construct($id, $type,$category='') {
|
||||
parent::__construct($id, $type,$category);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PYSEvent $event
|
||||
*/
|
||||
public function addEvent($event) {
|
||||
$this->events[] = $event;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PYSEvent[]
|
||||
*/
|
||||
public function getEvents() {
|
||||
return $this->events;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace PixelYourSite;
|
||||
class SingleEvent extends PYSEvent{
|
||||
|
||||
public $params = array(
|
||||
);
|
||||
public $payload = array(
|
||||
'delay' => 0
|
||||
);
|
||||
|
||||
public function __construct($id,$type,$category=''){
|
||||
parent::__construct($id,$type,$category);
|
||||
$this->payload['type'] = $type;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Insert Array params for event
|
||||
* @param array $data
|
||||
*/
|
||||
function addParams($data) {
|
||||
|
||||
if(is_array($data)) {
|
||||
$this->params = array_merge($this->params,$data);
|
||||
} else {
|
||||
error_log("addParams no array ".print_r($data,true));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert additional Array data for event
|
||||
* @param array $data
|
||||
*/
|
||||
function addPayload($data) {
|
||||
if(is_array($data)) {
|
||||
$this->payload = array_merge($this->payload,$data);
|
||||
} else {
|
||||
error_log("addPayload no array ".print_r($data,true));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function getData() {
|
||||
$data = $this->payload;
|
||||
$data['params'] = sanitizeParams($this->params);
|
||||
$data['e_id'] = $this->getId();
|
||||
|
||||
$data['delay'] = isset( $this->payload['delay'] ) ? $this->payload['delay'] : 0;
|
||||
$data['ids'] = isset( $this->payload['ids'] ) ? $this->payload['ids'] : array();
|
||||
$data['hasTimeWindow'] = isset( $this->payload['hasTimeWindow'] ) ? $this->payload['hasTimeWindow'] : false;
|
||||
$data['timeWindow'] = isset( $this->payload['timeWindow'] ) ? $this->payload['timeWindow'] : 0;
|
||||
$data['pixelIds'] = isset( $this->payload['pixelIds'] ) ? $this->payload['pixelIds'] : array();
|
||||
$data['eventID'] = isset( $this->payload['eventID'] ) ? $this->payload['eventID'] : "";
|
||||
$data['woo_order'] = isset( $this->payload['woo_order'] ) ? $this->payload['woo_order'] : "";
|
||||
$data['edd_order'] = isset( $this->payload['edd_order'] ) ? $this->payload['edd_order'] : "";
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
namespace PixelYourSite;
|
||||
class EventTypes {
|
||||
static public $DYNAMIC = "dyn";
|
||||
static public $STATIC = "static";
|
||||
static public $TRIGGER = "trigger";
|
||||
}
|
||||
|
||||
abstract class PYSEvent {
|
||||
protected $id;
|
||||
protected $type;
|
||||
protected $category;
|
||||
public $args = null;
|
||||
/**
|
||||
* GroupedEvent constructor.
|
||||
* @param String $id // unique id use in js object like key
|
||||
* @param String $type // can be static(fire when open page) or dynamic (fire when some event did)
|
||||
* @param String $category // event category like woo, edd signal etc
|
||||
*/
|
||||
public function __construct($id,$type,$category=''){
|
||||
$this->id = $id;
|
||||
$this->type = $type;
|
||||
$this->category = $category;
|
||||
}
|
||||
|
||||
function getId() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
function getType() {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
function getCategory() {
|
||||
return $this->category;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
namespace PixelYourSite;
|
||||
|
||||
/*
|
||||
* Automatic Events we will fire this event in order to capture all actions like clicks, video
|
||||
views, downloads, comments, forms.
|
||||
* */
|
||||
|
||||
class EventsAutomatic extends EventsFactory {
|
||||
private static $_instance;
|
||||
|
||||
private $events = array(
|
||||
'automatic_event_form' ,
|
||||
'automatic_event_signup' ,
|
||||
'automatic_event_login' ,
|
||||
'automatic_event_download' ,
|
||||
'automatic_event_comment' ,
|
||||
|
||||
'automatic_event_scroll' ,
|
||||
'automatic_event_time_on_page' ,
|
||||
'automatic_event_search' ,
|
||||
);
|
||||
|
||||
public static function instance() {
|
||||
|
||||
if ( is_null( self::$_instance ) ) {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
|
||||
}
|
||||
|
||||
private function __construct() {
|
||||
add_filter("pys_event_factory",[$this,"register"]);
|
||||
}
|
||||
|
||||
function register($list) {
|
||||
$list[] = $this;
|
||||
return $list;
|
||||
}
|
||||
|
||||
static function getSlug() {
|
||||
return "automatic";
|
||||
}
|
||||
|
||||
function getEvents() {
|
||||
return $this->events;
|
||||
}
|
||||
|
||||
function getCount() {
|
||||
$count = 0;
|
||||
if($this->isEnabled()) {
|
||||
foreach ($this->events as $event) {
|
||||
if(PYS()->getOption($event."_enabled")) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $count;
|
||||
}
|
||||
|
||||
|
||||
function isEnabled() {
|
||||
return PYS()->getOption("automatic_events_enabled");
|
||||
}
|
||||
|
||||
// return option for js part
|
||||
function getOptions() {
|
||||
return array(
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is event ready for fire
|
||||
* @param $event
|
||||
* @return bool
|
||||
*/
|
||||
function isReadyForFire($event) {
|
||||
|
||||
if(!$this->isEnabled()) return false;
|
||||
|
||||
if(!in_array($event,$this->events)) return false;
|
||||
|
||||
switch($event) {
|
||||
|
||||
case "automatic_event_login" : {
|
||||
if($user_id = get_current_user_id()) {
|
||||
if (get_user_meta($user_id, 'pys_just_login', true)) {
|
||||
delete_user_meta($user_id, 'pys_just_login');
|
||||
return PYS()->getOption( $event."_enabled");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
case 'automatic_event_signup' : {
|
||||
if ( $user_id = get_current_user_id() ) {
|
||||
if ( get_user_meta( $user_id, 'pys_complete_registration', true ) ) {
|
||||
return PYS()->getOption( $event."_enabled");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
case 'automatic_event_search' : {
|
||||
return PYS()->getOption( $event."_enabled") && is_search();
|
||||
}
|
||||
default: {
|
||||
return PYS()->getOption( $event."_enabled");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param String $event
|
||||
* @return SingleEvent
|
||||
*/
|
||||
function getEvent($event) {
|
||||
$payload = [];
|
||||
$params = [];
|
||||
$item = new SingleEvent($event,EventTypes::$DYNAMIC,self::getSlug());
|
||||
switch ($event) {
|
||||
|
||||
|
||||
case "automatic_event_form": {
|
||||
$payload['name'] = 'Form';
|
||||
}break;
|
||||
case "automatic_event_download": {
|
||||
$payload['name'] = 'Download';
|
||||
$payload["extensions"] = PYS()->getOption( 'automatic_event_download_extensions' );
|
||||
}break;
|
||||
case "automatic_event_comment": {
|
||||
$payload['name'] = 'Comment';
|
||||
}break;
|
||||
|
||||
case "automatic_event_scroll": {
|
||||
$payload['name'] = 'PageScroll';
|
||||
$payload["scroll_percent"] = PYS()->getOption('automatic_event_scroll_value');
|
||||
}break;
|
||||
case "automatic_event_time_on_page": {
|
||||
$payload['name'] = 'TimeOnPage';
|
||||
$payload["time_on_page"] = PYS()->getOption('automatic_event_time_on_page_value');
|
||||
|
||||
}break;
|
||||
case "automatic_event_search":
|
||||
case "automatic_event_signup":
|
||||
case "automatic_event_login": {
|
||||
$item = new SingleEvent($event,EventTypes::$STATIC,self::getSlug());
|
||||
} break;
|
||||
}
|
||||
|
||||
$item->addParams($params);
|
||||
$item->addPayload($payload);
|
||||
return $item;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EventsAutomatic
|
||||
*/
|
||||
function EventsAutomatic() {
|
||||
return EventsAutomatic::instance();
|
||||
}
|
||||
|
||||
EventsAutomatic();
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
namespace PixelYourSite;
|
||||
class EventsCustom extends EventsFactory {
|
||||
private static $_instance;
|
||||
public static function instance() {
|
||||
|
||||
if ( is_null( self::$_instance ) ) {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
|
||||
}
|
||||
|
||||
static function getSlug() {
|
||||
return "custom";
|
||||
}
|
||||
|
||||
private function __construct() {
|
||||
add_filter("pys_event_factory",[$this,"register"]);
|
||||
}
|
||||
|
||||
function register($list) {
|
||||
$list[] = $this;
|
||||
return $list;
|
||||
}
|
||||
|
||||
|
||||
function getEvents(){
|
||||
return CustomEventFactory::get( 'active' );
|
||||
}
|
||||
|
||||
function getCount()
|
||||
{
|
||||
if(!$this->isEnabled()) {
|
||||
return 0;
|
||||
}
|
||||
return count($this->getEvents());
|
||||
}
|
||||
|
||||
function isEnabled()
|
||||
{
|
||||
return PYS()->getOption( 'custom_events_enabled' );
|
||||
}
|
||||
|
||||
function getOptions()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CustomEvent $event
|
||||
* @return bool
|
||||
*/
|
||||
function isReadyForFire($event)
|
||||
{
|
||||
switch ($event->getTriggerType()) {
|
||||
|
||||
case 'page_visit': {
|
||||
$triggers = $event->getPageVisitTriggers();
|
||||
return !empty( $triggers ) && compareURLs( $triggers );
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @param CustomEvent $event
|
||||
* @return PYSEvent
|
||||
*/
|
||||
function getEvent($event)
|
||||
{
|
||||
|
||||
switch ($event->getTriggerType()) {
|
||||
case 'page_visit': {
|
||||
$singleEvent = new SingleEvent('custom_event',EventTypes::$STATIC,'custom');
|
||||
$singleEvent->args = $event;
|
||||
return $singleEvent;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @return EventsCustom
|
||||
*/
|
||||
function EventsCustom() {
|
||||
return EventsCustom::instance();
|
||||
}
|
||||
|
||||
EventsCustom();
|
||||
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
namespace PixelYourSite;
|
||||
|
||||
class EventsEdd extends EventsFactory {
|
||||
private $events = array(
|
||||
//'edd_frequent_shopper', pro
|
||||
//'edd_vip_client',pro
|
||||
//'edd_big_whale',pro
|
||||
'edd_view_content',
|
||||
'edd_view_category',
|
||||
'edd_add_to_cart_on_checkout_page',
|
||||
'edd_remove_from_cart',
|
||||
'edd_initiate_checkout',
|
||||
'edd_purchase',
|
||||
'edd_add_to_cart_on_button_click'
|
||||
);
|
||||
|
||||
|
||||
private static $_instance;
|
||||
|
||||
public static function instance() {
|
||||
|
||||
if ( is_null( self::$_instance ) ) {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
|
||||
}
|
||||
|
||||
static function getSlug() {
|
||||
return "edd";
|
||||
}
|
||||
|
||||
private function __construct() {
|
||||
add_filter("pys_event_factory",[$this,"register"]);
|
||||
}
|
||||
|
||||
function register($list) {
|
||||
$list[] = $this;
|
||||
return $list;
|
||||
}
|
||||
|
||||
function getEvents() {
|
||||
return $this->events;
|
||||
}
|
||||
|
||||
function getCount()
|
||||
{
|
||||
$size = 0;
|
||||
if(!$this->isEnabled()) {
|
||||
return 0;
|
||||
}
|
||||
foreach ($this->events as $event) {
|
||||
if($this->isActive($event)){
|
||||
$size++;
|
||||
}
|
||||
}
|
||||
return $size;
|
||||
}
|
||||
|
||||
function isEnabled()
|
||||
{
|
||||
return isEddActive();
|
||||
}
|
||||
|
||||
function getOptions()
|
||||
{
|
||||
if($this->isEnabled()) {
|
||||
return array(
|
||||
'enabled' => true,
|
||||
'enabled_save_data_to_orders' => PYS()->getOption('edd_enabled_save_data_to_orders'),
|
||||
'addToCartOnButtonEnabled' => isEventEnabled( 'edd_add_to_cart_enabled' ) && PYS()->getOption( 'edd_add_to_cart_on_button_click' ),
|
||||
'addToCartOnButtonValueEnabled' => PYS()->getOption( 'edd_add_to_cart_value_enabled' ),
|
||||
'addToCartOnButtonValueOption' => PYS()->getOption( 'edd_add_to_cart_value_option' ),
|
||||
);
|
||||
} else {
|
||||
return array(
|
||||
'enabled' => false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function isReadyForFire($event)
|
||||
{
|
||||
switch ($event) {
|
||||
case 'edd_add_to_cart_on_button_click': {
|
||||
return PYS()->getOption( 'edd_add_to_cart_enabled' ) && PYS()->getOption( 'edd_add_to_cart_on_button_click' );
|
||||
}
|
||||
case 'edd_purchase': {
|
||||
if(PYS()->getOption( 'edd_purchase_enabled' ) && edd_is_success_page()) {
|
||||
/**
|
||||
* When a payment gateway used, user lands to Payment Confirmation page first, which does automatic
|
||||
* redirect to Purchase Confirmation page. We filter Payment Confirmation to avoid double Purchase event.
|
||||
*/
|
||||
if ( isset( $_GET['payment-confirmation'] ) ) {
|
||||
//@fixme: some users will not reach success page and event will not be fired
|
||||
//return;
|
||||
}
|
||||
$payment_key = getEddPaymentKey();
|
||||
$order_id = (int) edd_get_purchase_id_by_key( $payment_key );
|
||||
$status = edd_get_payment_status( $order_id );
|
||||
|
||||
// pending payment status used because we can't fire event on IPN
|
||||
if ( strtolower( $status ) != 'publish' && strtolower( $status ) != 'pending' && strtolower( $status ) != 'complete' ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
update_post_meta( $order_id, '_pys_purchase_event_fired', true );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
case 'edd_initiate_checkout': {
|
||||
return PYS()->getOption( 'edd_initiate_checkout_enabled' ) && edd_is_checkout();
|
||||
}
|
||||
case 'edd_remove_from_cart': {
|
||||
return PYS()->getOption( 'edd_remove_from_cart_enabled') && edd_is_checkout();
|
||||
}
|
||||
case 'edd_add_to_cart_on_checkout_page' : {
|
||||
return PYS()->getOption( 'edd_add_to_cart_enabled' ) && PYS()->getOption( 'edd_add_to_cart_on_checkout_page' )
|
||||
&& edd_is_checkout();
|
||||
}
|
||||
case 'edd_view_category': {
|
||||
return PYS()->getOption( 'edd_view_category_enabled' ) && is_tax( 'download_category' );
|
||||
}
|
||||
case 'edd_view_content' : {
|
||||
return PYS()->getOption( 'edd_view_content_enabled' ) && is_singular( 'download' );
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function getEvent($event)
|
||||
{
|
||||
switch ($event) {
|
||||
case 'edd_initiate_checkout':
|
||||
case 'edd_purchase':
|
||||
case 'edd_add_to_cart_on_checkout_page' :
|
||||
case 'edd_view_category':
|
||||
case 'edd_view_content':{
|
||||
return new SingleEvent($event,EventTypes::$STATIC,'edd');
|
||||
}
|
||||
|
||||
case 'edd_remove_from_cart': {
|
||||
return $this->getRemoveFromCartEvents($event);
|
||||
}
|
||||
case 'edd_add_to_cart_on_button_click': {
|
||||
|
||||
return new SingleEvent($event,EventTypes::$DYNAMIC,'edd');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function isActive($event)
|
||||
{
|
||||
switch ($event) {
|
||||
case 'edd_add_to_cart_on_button_click': {
|
||||
return PYS()->getOption( 'edd_add_to_cart_enabled' ) && PYS()->getOption( 'edd_add_to_cart_on_button_click' );
|
||||
}
|
||||
case 'edd_purchase': {
|
||||
return PYS()->getOption( 'edd_purchase_enabled' );
|
||||
}
|
||||
case 'edd_initiate_checkout': {
|
||||
return PYS()->getOption( 'edd_initiate_checkout_enabled' ) ;
|
||||
}
|
||||
case 'edd_remove_from_cart': {
|
||||
return PYS()->getOption( 'edd_remove_from_cart_enabled');
|
||||
}
|
||||
case 'edd_add_to_cart_on_checkout_page' : {
|
||||
return PYS()->getOption( 'edd_add_to_cart_enabled' ) && PYS()->getOption( 'edd_add_to_cart_on_checkout_page' );
|
||||
}
|
||||
case 'edd_view_category': {
|
||||
return PYS()->getOption( 'edd_view_category_enabled' ) ;
|
||||
}
|
||||
case 'edd_view_content' : {
|
||||
return PYS()->getOption( 'edd_view_content_enabled' ) ;
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function getRemoveFromCartEvents($eventId) {
|
||||
$events = [];
|
||||
|
||||
|
||||
foreach (edd_get_cart_contents() as $cart_item_key => $cart_item) {
|
||||
$event = new SingleEvent($eventId,EventTypes::$DYNAMIC,self::getSlug());
|
||||
$event->args = ['key'=>$cart_item_key,'item'=>$cart_item];
|
||||
$events[]=$event;
|
||||
}
|
||||
return $events;
|
||||
}
|
||||
|
||||
private function getEddCartActiveCategories($categoryPixels){
|
||||
$catIds = array();
|
||||
$keys = array_keys($categoryPixels);
|
||||
$cart = edd_get_cart_contents();
|
||||
foreach ( $cart as $cart_item_key => $cart_item ) {
|
||||
$download_id = (int) $cart_item['id'];
|
||||
$productCatIds = Facebook\HelpersCategory\getIntersectEddProduct($download_id,$keys);
|
||||
foreach ($productCatIds as $id) {
|
||||
if(!in_array($categoryPixels[$id],$catIds)) // disable duplicate pixel_id
|
||||
$catIds[]=$id;
|
||||
}
|
||||
}
|
||||
return array_unique($catIds);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EventsEdd
|
||||
*/
|
||||
function EventsEdd() {
|
||||
return EventsEdd::instance();
|
||||
}
|
||||
|
||||
EventsEdd();
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
namespace PixelYourSite;
|
||||
|
||||
class EventsFdp extends EventsFactory
|
||||
{
|
||||
private $events = array(
|
||||
'fdp_view_content',
|
||||
'fdp_view_category',
|
||||
'fdp_add_to_cart',
|
||||
'fdp_purchase',
|
||||
);
|
||||
|
||||
|
||||
private static $_instance;
|
||||
|
||||
public static function instance()
|
||||
{
|
||||
|
||||
if (is_null(self::$_instance)) {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
|
||||
}
|
||||
|
||||
static function getSlug() {
|
||||
return "fdp";
|
||||
}
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
add_filter("pys_event_factory",[$this,"register"]);
|
||||
}
|
||||
|
||||
function register($list) {
|
||||
$list[] = $this;
|
||||
return $list;
|
||||
}
|
||||
function getEvents() {
|
||||
return $this->events;
|
||||
}
|
||||
|
||||
function getCount()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
function isEnabled()
|
||||
{
|
||||
return Facebook()->enabled() && PYS()->getOption( 'fdp_enabled' );
|
||||
}
|
||||
|
||||
function getOptions()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
function isReadyForFire($event)
|
||||
{
|
||||
switch ($event) {
|
||||
case 'fdp_purchase':
|
||||
case 'fdp_add_to_cart':
|
||||
case 'fdp_view_content': {
|
||||
return is_single() && get_post_type() == 'post';
|
||||
}
|
||||
case 'fdp_view_category': {
|
||||
return is_category();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getEvent($event)
|
||||
{
|
||||
switch ($event) {
|
||||
case 'fdp_view_category':
|
||||
case 'fdp_view_content': {
|
||||
return new SingleEvent($event,EventTypes::$STATIC,'fdp');
|
||||
}
|
||||
case 'fdp_add_to_cart':
|
||||
case 'fdp_purchase': {
|
||||
return new SingleEvent($event,EventTypes::$TRIGGER,'fdp');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EventsFdp
|
||||
*/
|
||||
function EventsFdp() {
|
||||
return EventsFdp::instance();
|
||||
}
|
||||
|
||||
EventsFdp();
|
||||
@@ -0,0 +1,320 @@
|
||||
<?php
|
||||
namespace PixelYourSite;
|
||||
|
||||
|
||||
class EventsWoo extends EventsFactory {
|
||||
|
||||
private $events = array(
|
||||
//"woo_frequent_shopper",
|
||||
//"woo_vip_client",
|
||||
//"woo_big_whale",
|
||||
"woo_view_content",
|
||||
//"woo_view_content_for_category",
|
||||
"woo_view_category",
|
||||
"woo_view_item_list",
|
||||
//"woo_view_item_list_single",
|
||||
//"woo_view_item_list_search",
|
||||
//"woo_view_item_list_shop",
|
||||
//"woo_view_item_list_tag",
|
||||
"woo_add_to_cart_on_cart_page",
|
||||
//"woo_add_to_cart_on_cart_page_category",
|
||||
"woo_add_to_cart_on_checkout_page",
|
||||
//"woo_add_to_cart_on_checkout_page_category",
|
||||
"woo_initiate_checkout",
|
||||
//"woo_initiate_checkout_category",
|
||||
"woo_purchase",
|
||||
//"woo_initiate_set_checkout_option",
|
||||
//"woo_initiate_checkout_progress_f",
|
||||
//"woo_initiate_checkout_progress_l",
|
||||
//"woo_initiate_checkout_progress_e",
|
||||
//"woo_initiate_checkout_progress_o",
|
||||
"woo_remove_from_cart",
|
||||
"woo_add_to_cart_on_button_click",
|
||||
//"woo_affiliate",
|
||||
//"woo_paypal",
|
||||
//"woo_select_content_category",
|
||||
//"woo_select_content_single",
|
||||
//"woo_select_content_search",
|
||||
//"woo_select_content_shop",
|
||||
// "woo_select_content_tag",
|
||||
);
|
||||
public $doingAMP = false;
|
||||
|
||||
|
||||
private static $_instance;
|
||||
|
||||
public static function instance() {
|
||||
|
||||
if ( is_null( self::$_instance ) ) {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
|
||||
}
|
||||
|
||||
static function getSlug() {
|
||||
return "woo";
|
||||
}
|
||||
|
||||
private function __construct() {
|
||||
add_filter("pys_event_factory",[$this,"register"]);
|
||||
}
|
||||
|
||||
function register($list) {
|
||||
$list[] = $this;
|
||||
return $list;
|
||||
}
|
||||
|
||||
function getCount()
|
||||
{
|
||||
$size = 0;
|
||||
if(!$this->isEnabled()) {
|
||||
return 0;
|
||||
}
|
||||
foreach ($this->events as $event) {
|
||||
if($this->isActive($event)){
|
||||
$size++;
|
||||
}
|
||||
}
|
||||
if(PYS()->getOption( 'woo_complete_registration_enabled' ))
|
||||
$size++;
|
||||
return $size;
|
||||
}
|
||||
|
||||
function isEnabled()
|
||||
{
|
||||
return isWooCommerceActive();
|
||||
}
|
||||
|
||||
function getOptions() {
|
||||
|
||||
if($this->isEnabled()) {
|
||||
global $post;
|
||||
$data = array(
|
||||
'enabled' => true,
|
||||
'enabled_save_data_to_orders' => PYS()->getOption('woo_enabled_save_data_to_orders'),
|
||||
'addToCartOnButtonEnabled' => PYS()->getOption( 'woo_add_to_cart_enabled' ) && PYS()->getOption( 'woo_add_to_cart_on_button_click' ),
|
||||
'addToCartOnButtonValueEnabled' => PYS()->getOption( 'woo_add_to_cart_value_enabled' ),
|
||||
'addToCartOnButtonValueOption' => PYS()->getOption( 'woo_add_to_cart_value_option' ),
|
||||
'singleProductId' => isWooCommerceActive() && is_singular( 'product' ) ? $post->ID : null,
|
||||
'removeFromCartSelector' => isWooCommerceVersionGte( '3.0.0' )
|
||||
? 'form.woocommerce-cart-form .remove'
|
||||
: '.cart .product-remove .remove',
|
||||
'addToCartCatchMethod' => PYS()->getOption('woo_add_to_cart_catch_method')
|
||||
);
|
||||
|
||||
return $data;
|
||||
} else {
|
||||
return array(
|
||||
'enabled' => false,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function isReadyForFire($event)
|
||||
{
|
||||
switch ($event) {
|
||||
case 'woo_add_to_cart_on_button_click': {
|
||||
return PYS()->getOption( 'woo_add_to_cart_enabled' )
|
||||
&& PYS()->getOption( 'woo_add_to_cart_on_button_click' )
|
||||
&& PYS()->getOption('woo_add_to_cart_catch_method') == "add_cart_js"; // or use in hook
|
||||
}
|
||||
|
||||
|
||||
case 'woo_remove_from_cart': {
|
||||
return PYS()->getOption( 'woo_remove_from_cart_enabled') && is_cart();
|
||||
}
|
||||
|
||||
|
||||
case 'woo_purchase' : {
|
||||
if(PYS()->getOption( 'woo_purchase_enabled' ) && is_order_received_page() &&
|
||||
isset( $_REQUEST['key'] ) && $_REQUEST['key'] != ""
|
||||
&& empty($_REQUEST['wc-api']) // if is not api request
|
||||
) {
|
||||
$order_key = sanitize_key($_REQUEST['key']);
|
||||
$order_id = (int) wc_get_order_id_by_order_key( $order_key );
|
||||
|
||||
$order = wc_get_order($order_id);
|
||||
if(!$order) return false;
|
||||
$status = "wc-".$order->get_status("edit");
|
||||
|
||||
$disabledStatuses = (array)PYS()->getOption("woo_order_purchase_disabled_status");
|
||||
|
||||
if( in_array($status,$disabledStatuses)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
case 'woo_view_content' : {
|
||||
return PYS()->getOption( 'woo_view_content_enabled' ) && is_product();
|
||||
}
|
||||
case 'woo_view_category': {
|
||||
return PYS()->getOption( 'woo_view_category_enabled' ) && is_tax( 'product_cat' );
|
||||
}
|
||||
case 'woo_view_item_list': {
|
||||
return PYS()->getOption( 'woo_view_item_list_enabled' ) && is_tax( 'product_cat' );
|
||||
}
|
||||
case 'woo_add_to_cart_on_cart_page': {
|
||||
return PYS()->getOption( 'woo_add_to_cart_enabled' ) &&
|
||||
PYS()->getOption( 'woo_add_to_cart_on_cart_page' ) &&
|
||||
is_cart()
|
||||
&& count(WC()->cart->get_cart())>0;
|
||||
}
|
||||
case 'woo_add_to_cart_on_checkout_page': {
|
||||
return PYS()->getOption( 'woo_add_to_cart_enabled' ) && PYS()->getOption( 'woo_add_to_cart_on_checkout_page' )
|
||||
&& is_checkout() && ! is_wc_endpoint_url()
|
||||
&& count(WC()->cart->get_cart())>0;
|
||||
}
|
||||
|
||||
case 'woo_initiate_checkout': {
|
||||
return PYS()->getOption( 'woo_initiate_checkout_enabled' ) && is_checkout() && ! is_wc_endpoint_url();
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function getEvent($event)
|
||||
{
|
||||
switch ($event) {
|
||||
case 'woo_remove_from_cart':{
|
||||
return $this->getRemoveFromCartEvents($event);
|
||||
}
|
||||
|
||||
case 'woo_initiate_checkout':
|
||||
case 'woo_add_to_cart_on_checkout_page':
|
||||
case 'woo_add_to_cart_on_cart_page':
|
||||
case 'woo_view_category':
|
||||
case 'woo_view_item_list':
|
||||
case 'woo_view_content':
|
||||
return new SingleEvent($event,EventTypes::$STATIC,'woo');
|
||||
|
||||
case 'woo_add_to_cart_on_button_click':
|
||||
return new SingleEvent($event,EventTypes::$DYNAMIC,'woo');
|
||||
case 'woo_purchase' : {
|
||||
$events = array();
|
||||
$order_key = sanitize_key($_REQUEST['key']);
|
||||
$order_id = (int) wc_get_order_id_by_order_key( $order_key );
|
||||
$order = wc_get_order($order_id);
|
||||
if($order) {
|
||||
$order->update_meta_data("_pys_purchase_event_fired",true);
|
||||
$order->save();
|
||||
}
|
||||
$events[] = new SingleEvent($event,EventTypes::$STATIC,'woo');
|
||||
|
||||
// add child event complete_registration
|
||||
if(PYS()->getOption( 'woo_complete_registration_enabled' )) {
|
||||
$events[] = new SingleEvent('woo_complete_registration',EventTypes::$STATIC,'woo');
|
||||
}
|
||||
|
||||
|
||||
return $events;
|
||||
}
|
||||
}
|
||||
error_log("Not handle event ".$event);
|
||||
return null;
|
||||
}
|
||||
|
||||
private function isActive($event)
|
||||
{
|
||||
switch ($event) {
|
||||
case 'woo_add_to_cart_on_button_click': {
|
||||
return PYS()->getOption( 'woo_add_to_cart_enabled' ) && PYS()->getOption( 'woo_add_to_cart_on_button_click' );
|
||||
}
|
||||
|
||||
case 'woo_remove_from_cart': {
|
||||
return PYS()->getOption( 'woo_remove_from_cart_enabled') ;
|
||||
}
|
||||
|
||||
case 'woo_purchase' : {
|
||||
return PYS()->getOption( 'woo_purchase_enabled' );
|
||||
}
|
||||
|
||||
|
||||
case 'woo_view_content' : {
|
||||
return PYS()->getOption( 'woo_view_content_enabled' ) ;
|
||||
}
|
||||
case 'woo_view_category': {
|
||||
return PYS()->getOption( 'woo_view_category_enabled' ) ;
|
||||
}
|
||||
|
||||
case 'woo_initiate_checkout': {
|
||||
return PYS()->getOption( 'woo_initiate_checkout_enabled' );
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function getRemoveFromCartEvents($eventId) {
|
||||
$events = [];
|
||||
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
|
||||
$event = new SingleEvent($eventId,EventTypes::$DYNAMIC,self::getSlug());
|
||||
$event->args = ['key'=>$cart_item_key,'item'=>$cart_item];
|
||||
$events[]=$event;
|
||||
}
|
||||
return $events;
|
||||
}
|
||||
|
||||
private function getWooCartActiveCategories($activeIds) {
|
||||
$fireForCategory = array();
|
||||
foreach (WC()->cart->cart_contents as $cart_item_key => $cart_item) {
|
||||
$_product = wc_get_product( $cart_item['product_id'] );
|
||||
if(!$_product) continue;
|
||||
$productCat = $_product->get_category_ids();
|
||||
foreach ($activeIds as $key => $value) {
|
||||
if(in_array($key,$productCat)) {
|
||||
$fireForCategory[] = $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
return array_unique($fireForCategory);
|
||||
}
|
||||
|
||||
private function getWooOrderActiveCategories($orderId,$activeIds) {
|
||||
$order = new \WC_Order( $orderId );
|
||||
|
||||
$fireForCategory = array();
|
||||
foreach ($order->get_items() as $item) {
|
||||
$_product = wc_get_product( $item->get_product_id() );
|
||||
if(!$_product) continue;
|
||||
$productCat = $_product->get_category_ids();
|
||||
foreach ($activeIds as $key => $value) {
|
||||
if(in_array($key,$productCat)) { // fire initiate_checkout for all category pixel
|
||||
$fireForCategory[] = $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
return array_unique($fireForCategory);
|
||||
}
|
||||
/**
|
||||
* Always returns empty customer LTV-related values to make plugin compatible with PRO version.
|
||||
* Used by Pinterest add-on.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCustomerTotals($order_id = null){
|
||||
return [
|
||||
'ltv' => null,
|
||||
'avg_order_value' => null,
|
||||
'orders_count' => null,
|
||||
];
|
||||
}
|
||||
|
||||
function getEvents() {
|
||||
return $this->events;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EventsWoo
|
||||
*/
|
||||
function EventsWoo() {
|
||||
return EventsWoo::instance();
|
||||
}
|
||||
|
||||
EventsWoo();
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace PixelYourSite;
|
||||
|
||||
abstract class EventsFactory {
|
||||
|
||||
|
||||
static function getSlug(){
|
||||
return "";
|
||||
}
|
||||
abstract function getCount();
|
||||
abstract function isEnabled();
|
||||
abstract function getOptions();
|
||||
|
||||
abstract function getEvents();
|
||||
/**
|
||||
* Check is event ready for fire
|
||||
* @param $event
|
||||
* @return bool
|
||||
*/
|
||||
abstract function isReadyForFire($event);
|
||||
|
||||
/**
|
||||
* @param String $event
|
||||
* @return SingleEvent
|
||||
*/
|
||||
abstract function getEvent($event);
|
||||
|
||||
|
||||
function generateEvents() {
|
||||
if(!$this->isEnabled()) return array();
|
||||
$eventsList = array();
|
||||
foreach ($this->getEvents() as $eventName) {
|
||||
if($this->isReadyForFire($eventName)) {
|
||||
|
||||
foreach ( PYS()->getRegisteredPixels() as $pixel ) {
|
||||
$events = $this->getEvent($eventName);
|
||||
if(!is_array($events)) $events = array($events); // some type of events can return array
|
||||
|
||||
foreach ($events as $event) {
|
||||
$singleEvents = $pixel->generateEvents( $event );
|
||||
foreach ($singleEvents as $singleEvent) {
|
||||
if(!apply_filters("pys_validate_pixel_event",true,$singleEvent,$pixel)) continue;
|
||||
$eventsList[$pixel->getSlug()][] = $singleEvent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $eventsList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user