| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Admin\Settings\Integration; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die(); |
| 6 |
|
| 7 |
use Sellkit\Global_Checkout\Checkout as Global_Checkout; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Google Analytics and Facebook Pixel integration. |
| 11 |
* |
| 12 |
* @package Sellkit\Admin\Settings\Integration\Settings_Integration |
| 13 |
* @since 1.1.0 |
| 14 |
*/ |
| 15 |
class Settings_Integration { |
| 16 |
/** |
| 17 |
* The class instance. |
| 18 |
* |
| 19 |
* @var Object Class instance. |
| 20 |
* @since 1.1.0 |
| 21 |
*/ |
| 22 |
public static $instance = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Post data. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
* @since 1.1.0 |
| 29 |
*/ |
| 30 |
public static $post = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* Order key. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
* @since 1.1.0 |
| 37 |
*/ |
| 38 |
public static $order_key = []; |
| 39 |
|
| 40 |
/** |
| 41 |
* Localized data. |
| 42 |
* |
| 43 |
* @var array localized data. |
| 44 |
* @since 1.1.0 |
| 45 |
*/ |
| 46 |
public static $localized_data = []; |
| 47 |
|
| 48 |
/** |
| 49 |
* Class Instance. |
| 50 |
* |
| 51 |
* @since 1.1.0 |
| 52 |
* @return Sellkit_Funnel|null |
| 53 |
*/ |
| 54 |
public static function get_instance() { |
| 55 |
if ( null === self::$instance ) { |
| 56 |
self::$instance = new self(); |
| 57 |
} |
| 58 |
|
| 59 |
return self::$instance; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Class constructor. |
| 64 |
* |
| 65 |
* @since 1.1.0 |
| 66 |
*/ |
| 67 |
public function __construct() { |
| 68 |
add_action( 'wp_head', [ $this, 'google_facebook_analytics' ] ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get google analyticts scripts. |
| 73 |
* |
| 74 |
* @since 1.1.0 |
| 75 |
*/ |
| 76 |
public function google_facebook_analytics() { |
| 77 |
global $post; |
| 78 |
|
| 79 |
$step_meta = $this->get_step_meta( $post ); |
| 80 |
|
| 81 |
if ( |
| 82 |
empty( $post ) || |
| 83 |
empty( $step_meta ) || |
| 84 |
$this->is_elementor_preview() |
| 85 |
) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
self::$post = $step_meta; |
| 90 |
self::$order_key = sellkit_htmlspecialchars( INPUT_GET, 'order-key' ); |
| 91 |
|
| 92 |
if ( empty( self::$order_key ) ) { |
| 93 |
self::$order_key = sellkit_htmlspecialchars( INPUT_GET, 'key' ); |
| 94 |
} |
| 95 |
|
| 96 |
if ( ! sellkit()->has_valid_dependencies() ) { |
| 97 |
self::$order_key = null; |
| 98 |
} |
| 99 |
|
| 100 |
sellkit()->load_files( |
| 101 |
[ |
| 102 |
'admin/settings/integration/google-integration', |
| 103 |
'admin/settings/integration/facebook-integration', |
| 104 |
] |
| 105 |
); |
| 106 |
|
| 107 |
wp_localize_script( 'funnel-settings-variables', 'sellkitSettings', self::$localized_data ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get step meta. |
| 112 |
* |
| 113 |
* @param object $post post object. |
| 114 |
* @since 2.3.0 |
| 115 |
* @return array|void |
| 116 |
*/ |
| 117 |
private function get_step_meta( $post ) { |
| 118 |
if ( empty( $post ) ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
$step_meta = get_post_meta( $post->ID, 'step_data' ); |
| 123 |
|
| 124 |
if ( ! empty( $step_meta ) ) { |
| 125 |
return $step_meta; |
| 126 |
} |
| 127 |
|
| 128 |
$global_checkout_id = get_option( Global_Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, 0 ); |
| 129 |
|
| 130 |
if ( empty( $global_checkout_id ) ) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
if ( 'publish' !== get_post_status( $global_checkout_id ) ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
$global_chekout_nodes = get_post_meta( $global_checkout_id, 'nodes' ); |
| 139 |
|
| 140 |
if ( empty( $global_chekout_nodes ) ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
$global_chekout_nodes = $global_chekout_nodes[0]; |
| 145 |
$global_chekout_nodes = $this->check_global_checkout_steps( $global_chekout_nodes, $post->ID ); |
| 146 |
|
| 147 |
if ( empty( $global_chekout_nodes ) ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
return $global_chekout_nodes; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Check global checkout steps. |
| 156 |
* |
| 157 |
* @param array $global_chekout_nodes global checkout nodes. |
| 158 |
* @param int $post_id post id. |
| 159 |
* @since 2.3.0 |
| 160 |
* @return array |
| 161 |
*/ |
| 162 |
private function check_global_checkout_steps( $global_chekout_nodes, $post_id ) { |
| 163 |
$default_checkout_page = intval( get_option( 'woocommerce_checkout_page_id', 0 ) ); |
| 164 |
$default_thankyou_page = is_checkout() && ! empty( is_wc_endpoint_url( 'order-received' ) ); |
| 165 |
$is_checkout_page = is_checkout() && empty( is_wc_endpoint_url( 'order-received' ) ); |
| 166 |
|
| 167 |
$new_nodes = []; |
| 168 |
|
| 169 |
if ( $default_checkout_page !== $post_id && $default_thankyou_page ) { |
| 170 |
return []; |
| 171 |
} |
| 172 |
|
| 173 |
foreach ( $global_chekout_nodes as $node ) { |
| 174 |
if ( 'checkout' === $node['type']['key'] && ! empty( $default_checkout_page ) && $is_checkout_page ) { |
| 175 |
$new_nodes[ $default_checkout_page ] = $node; |
| 176 |
continue; |
| 177 |
} |
| 178 |
|
| 179 |
if ( 'thankyou' === $node['type']['key'] && ! empty( $default_thankyou_page ) ) { |
| 180 |
$new_nodes[ $post_id ] = $node; |
| 181 |
continue; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
if ( ! isset( $new_nodes[ $post_id ] ) ) { |
| 186 |
return []; |
| 187 |
} |
| 188 |
|
| 189 |
return $new_nodes; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Check is elementor preview. |
| 194 |
* |
| 195 |
* @since 1.1.0 |
| 196 |
*/ |
| 197 |
private function is_elementor_preview() { |
| 198 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 199 |
|
| 200 |
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) { |
| 201 |
return true; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get product data. |
| 210 |
* |
| 211 |
* @since 1.1.0 |
| 212 |
* @param array $products_list list of the products. |
| 213 |
* @param string $event event type name. |
| 214 |
*/ |
| 215 |
public function get_products_data( $products_list, $event ) { |
| 216 |
if ( empty( $products_list ) ) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
$products = []; |
| 221 |
$products_id = []; |
| 222 |
$products_name = ''; |
| 223 |
$categories_names = ''; |
| 224 |
|
| 225 |
foreach ( $products_list as $product ) { |
| 226 |
$product_data = wc_get_product( $product['product_id'] ); |
| 227 |
|
| 228 |
if ( $product_data->is_type( 'variable' ) && isset( $product['variation_id'] ) ) { |
| 229 |
$product_data = wc_get_product( $product['variation_id'] ); |
| 230 |
} |
| 231 |
|
| 232 |
if ( ! empty( $product_data ) ) { |
| 233 |
$products_id[] = (string) $product_data->get_id(); |
| 234 |
$products_name = $products_name . ', ' . $product_data->get_name(); |
| 235 |
$categories_names = $categories_names . ', ' . wp_strip_all_tags( wc_get_product_category_list( $product_data->get_id() ) ); |
| 236 |
|
| 237 |
$products[] = [ |
| 238 |
'id' => $product_data->get_id(), |
| 239 |
'name' => $product_data->get_name(), |
| 240 |
'sku' => $product_data->get_sku(), |
| 241 |
'category' => wp_strip_all_tags( wc_get_product_category_list( $product_data->get_id() ) ), |
| 242 |
'quantity' => $product['quantity'], |
| 243 |
]; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if ( 'fb' === $event ) { |
| 248 |
$fb_products = [ |
| 249 |
'cart_contents' => $products, |
| 250 |
'content_ids' => $products_id, |
| 251 |
'products_name' => $products_name, |
| 252 |
'categories_name' => $categories_names, |
| 253 |
]; |
| 254 |
|
| 255 |
return $fb_products; |
| 256 |
} |
| 257 |
|
| 258 |
return $products; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
Settings_Integration::get_instance(); |
| 263 |
|