| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Admin\Settings\Integration; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die(); |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Google Analytics and Facebook Pixel integration. |
| 9 |
* |
| 10 |
* @package Sellkit\Admin\Settings\Integration\Settings_Integration |
| 11 |
* @since 1.1.0 |
| 12 |
*/ |
| 13 |
class Settings_Integration { |
| 14 |
/** |
| 15 |
* The class instance. |
| 16 |
* |
| 17 |
* @var Object Class instance. |
| 18 |
* @since 1.1.0 |
| 19 |
*/ |
| 20 |
public static $instance = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* Post data. |
| 24 |
* |
| 25 |
* @var array |
| 26 |
* @since 1.1.0 |
| 27 |
*/ |
| 28 |
public static $post = []; |
| 29 |
|
| 30 |
/** |
| 31 |
* Order key. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
* @since 1.1.0 |
| 35 |
*/ |
| 36 |
public static $order_key = []; |
| 37 |
|
| 38 |
/** |
| 39 |
* Localized data. |
| 40 |
* |
| 41 |
* @var array localized data. |
| 42 |
* @since 1.1.0 |
| 43 |
*/ |
| 44 |
public static $localized_data = []; |
| 45 |
|
| 46 |
/** |
| 47 |
* Class Instance. |
| 48 |
* |
| 49 |
* @since 1.1.0 |
| 50 |
* @return Sellkit_Funnel|null |
| 51 |
*/ |
| 52 |
public static function get_instance() { |
| 53 |
if ( null === self::$instance ) { |
| 54 |
self::$instance = new self(); |
| 55 |
} |
| 56 |
|
| 57 |
return self::$instance; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Class constructor. |
| 62 |
* |
| 63 |
* @since 1.1.0 |
| 64 |
*/ |
| 65 |
public function __construct() { |
| 66 |
add_action( 'wp_head', [ $this, 'google_facebook_analytics' ] ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get google analyticts scripts. |
| 71 |
* |
| 72 |
* @since 1.1.0 |
| 73 |
*/ |
| 74 |
public function google_facebook_analytics() { |
| 75 |
global $post; |
| 76 |
|
| 77 |
if ( |
| 78 |
empty( $post ) || |
| 79 |
empty( get_post_meta( $post->ID, 'step_data' ) ) || |
| 80 |
$this->is_elementor_preview() |
| 81 |
) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
self::$post = get_post_meta( $post->ID, 'step_data' ); |
| 86 |
self::$order_key = sellkit_htmlspecialchars( INPUT_GET, 'order-key' ); |
| 87 |
|
| 88 |
if ( ! sellkit()->has_valid_dependencies() ) { |
| 89 |
self::$order_key = null; |
| 90 |
} |
| 91 |
|
| 92 |
sellkit()->load_files( |
| 93 |
[ |
| 94 |
'admin/settings/integration/google-integration', |
| 95 |
'admin/settings/integration/facebook-integration', |
| 96 |
] |
| 97 |
); |
| 98 |
|
| 99 |
wp_localize_script( 'funnel-settings-variables', 'sellkitSettings', self::$localized_data ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Check is elementor preview. |
| 104 |
* |
| 105 |
* @since 1.1.0 |
| 106 |
*/ |
| 107 |
private function is_elementor_preview() { |
| 108 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 109 |
|
| 110 |
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) { |
| 111 |
return true; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get product data. |
| 120 |
* |
| 121 |
* @since 1.1.0 |
| 122 |
* @param array $products_list list of the products. |
| 123 |
* @param string $event event type name. |
| 124 |
*/ |
| 125 |
public function get_products_data( $products_list, $event ) { |
| 126 |
if ( empty( $products_list ) ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
$products = []; |
| 131 |
$products_id = []; |
| 132 |
$products_name = ''; |
| 133 |
$categories_names = ''; |
| 134 |
|
| 135 |
foreach ( $products_list as $product ) { |
| 136 |
$product_data = wc_get_product( $product['product_id'] ); |
| 137 |
|
| 138 |
if ( $product_data->is_type( 'variable' ) && isset( $product['variation_id'] ) ) { |
| 139 |
$product_data = wc_get_product( $product['variation_id'] ); |
| 140 |
} |
| 141 |
|
| 142 |
if ( ! empty( $product_data ) ) { |
| 143 |
$products_id[] = (string) $product_data->get_id(); |
| 144 |
$products_name = $products_name . ', ' . $product_data->get_name(); |
| 145 |
$categories_names = $categories_names . ', ' . wp_strip_all_tags( wc_get_product_category_list( $product_data->get_id() ) ); |
| 146 |
|
| 147 |
$products[] = [ |
| 148 |
'id' => $product_data->get_id(), |
| 149 |
'name' => $product_data->get_name(), |
| 150 |
'sku' => $product_data->get_sku(), |
| 151 |
'category' => wp_strip_all_tags( wc_get_product_category_list( $product_data->get_id() ) ), |
| 152 |
'quantity' => $product['quantity'], |
| 153 |
]; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if ( 'fb' === $event ) { |
| 158 |
$fb_products = [ |
| 159 |
'cart_contents' => $products, |
| 160 |
'content_ids' => $products_id, |
| 161 |
'products_name' => $products_name, |
| 162 |
'categories_name' => $categories_names, |
| 163 |
]; |
| 164 |
|
| 165 |
return $fb_products; |
| 166 |
} |
| 167 |
|
| 168 |
return $products; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
Settings_Integration::get_instance(); |
| 173 |
|