PluginProbe
Pixelavo – Server Side Tracking & Pixel + AI Ads Tools / 1.2.2
Pixelavo – Server Side Tracking & Pixel + AI Ads Tools v1.2.2
1.5.5 1.5.4 trunk 1.0.0 1.0.1 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 All 47 releases
pixelavo / includes / helper-functions.php

helper-functions.php in Pixelavo – Server Side Tracking & Pixel + AI Ads Tools 1.2.2, at includes/helper-functions.php

561 lines 17.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Clean array data.
5 * @param [array] $var
6 * @return mixed
7 */
8 function pixelavo_data_clean( $var ) {
9 if ( is_array( $var ) ) {
10 return array_map( 'pixelavo_data_clean', $var );
11 } else {
12 return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
13 }
14 }
15
16 /**
17 * Get Options Value.
18 * @param [type] $key
19 * @param [type] $section
20 * @param mixed $default
21 * @return mixed
22 */
23 function pixelavo_get_option( $key, $section, $default = false ){
24 $options = get_option( $section );
25 if ( isset( $options[$key] ) ) {
26 $value = $options[$key];
27 }else{
28 $value = $default;
29 }
30 return apply_filters( 'pixelavo_get_option_' . $key, $value, $key, $default );
31 }
32
33 /**
34 * Get Option value Section wise.
35 * @param array $registered_settings
36 * @return void
37 */
38 function pixelavo_get_options( $registered_settings = [] ) {
39 if( ! is_array( $registered_settings ) ){
40 return;
41 }
42 $settings = [];
43 $options = [];
44 foreach ( $registered_settings as $section_key => $setting_section ) {
45 foreach ( $setting_section as $setting ) {
46 $default = isset( $setting['std'] ) ? $setting['std'] : ( isset( $setting['default'] ) ? $setting['default'] : '' );
47 $options[ $setting['id'] ] = pixelavo_get_option( $setting['id'], $section_key, $default );
48 }
49 $settings[$section_key] = $options;
50 $options = [];
51 }
52 return apply_filters( 'pixelavo_get_settings', $settings );
53
54 }
55
56 /**
57 * Get all page list.
58 * @return array an `array` of all pages with `id` and `title`
59 */
60 function pixelavo_page_list() {
61 $page_list = [];
62 foreach(get_posts( ['post_type' => 'page', 'posts_per_page' => -1, 'fields' => 'ids'] ) as $id){
63 $page_list[] = [ 'id' => $id, 'text' => 'Page - ' . get_the_title( $id ) ];
64 }
65 $page_list[] = [ 'id' => 'all_posts', 'text' => 'All Post Page' ];
66 foreach(get_posts( ['post_type' => 'post', 'posts_per_page' => -1, 'fields' => 'ids'] ) as $id){
67 $page_list[] = [ 'id' => $id, 'text' => 'Post - ' . get_the_title( $id ) ];
68 }
69 if(pixelavo_is_woocommerce_active()){
70 $page_list[] = [ 'id' => 'all_products', 'text' => 'All Product Page' ];
71 foreach(get_posts( ['post_type' => 'product', 'posts_per_page' => -1, 'fields' => 'ids'] ) as $id){
72 $page_list[] = [ 'id' => $id, 'text' => 'Product - ' . get_the_title( $id ) ];
73 }
74 }
75 if(pixelavo_is_edd_active()){
76 $page_list[] = [ 'id' => 'all_downloads', 'text' => 'All Download Page' ];
77 foreach(get_posts( ['post_type' => 'download', 'posts_per_page' => -1, 'fields' => 'ids'] ) as $id){
78 $page_list[] = [ 'id' => $id, 'text' => 'Download - ' . get_the_title( $id ) ];
79 }
80 }
81 if(pixelavo_is_woocommerce_active()){
82 $page_list[] = [ 'id' => 'product_cat', 'text' => 'All Product Category Page' ];
83 foreach(get_terms(['taxonomy' => 'product_cat']) as $term) {
84 $page_list[] = [ 'id' => 'term'.$term->term_id, 'text' => 'Category - ' . $term->name ];
85 }
86 }
87 if(pixelavo_is_edd_active()){
88 $page_list[] = [ 'id' => 'download_category', 'text' => 'All Download Category Page' ];
89 foreach(get_terms(['taxonomy' => 'download_category']) as $term) {
90 $page_list[] = [ 'id' => 'term'.$term->term_id, 'text' => 'Category - ' . $term->name ];
91 }
92 }
93 $page_list[] = [ 'id' => 'category', 'text' => 'All Post Category Page' ];
94 foreach(get_terms(['taxonomy' => 'category']) as $term){
95 $page_list[] = [ 'id' => 'term'.$term->term_id, 'text' => 'Category - ' . $term->name ];
96 }
97 if(pixelavo_is_woocommerce_active()){
98 $page_list[] = [ 'id' => 'product_tag', 'text' => 'All Product Tag Page' ];
99 foreach(get_terms(['taxonomy' => 'product_tag']) as $term) {
100 $page_list[] = [ 'id' => 'term'.$term->term_id, 'text' => 'Tag - ' . $term->name ];
101 }
102 }
103 if(pixelavo_is_edd_active()){
104 $page_list[] = [ 'id' => 'download_tag', 'text' => 'All Download Tag Page' ];
105 foreach(get_terms(['taxonomy' => 'download_tag']) as $term) {
106 $page_list[] = [ 'id' => 'term'.$term->term_id, 'text' => 'Tag - ' . $term->name ];
107 }
108 }
109 $page_list[] = [ 'id' => 'post_tag', 'text' => 'All Post Tag Page' ];
110 foreach(get_terms(['taxonomy' => 'post_tag']) as $term ){
111 $page_list[] = [ 'id' => 'term'.$term->term_id, 'text' => 'Tag - ' . $term->name ];
112 }
113 return $page_list;
114 }
115
116 /**
117 * Get all editable roles.
118 */
119 function pixelavo_editable_roles() {
120 $roles = [];
121 foreach (wp_roles()->get_names() as $key => $value) {
122 $roles[] = [
123 'id' => $key,
124 'text' => $value
125 ];
126 }
127 return $roles;
128 }
129
130 /**
131 * Get all product categories.
132 */
133 function get_product_categories() {
134 $cats = get_terms([
135 'taxonomy' => 'product_cat',
136 'hide_empty' => false
137 ]);
138 if(!is_array($cats)) {
139 $cats = [];
140 }
141 $values = [];
142 foreach ($cats as $cat) {
143 $values[] = [
144 'id' => $cat->term_id,
145 'text' => $cat->name
146 ];
147 }
148 return $values;
149 }
150 /**
151 * Get all product tags.
152 */
153 function get_product_tags() {
154 $tags = get_terms([
155 'taxonomy' => 'product_tag',
156 'hide_empty' => false
157 ]);
158 if(!is_array($tags)) {
159 $tags = [];
160 }
161 $values = [];
162 foreach ($tags as $tag) {
163 $values[] = [
164 'id' => $tag->term_id,
165 'text' => $tag->name
166 ];
167 }
168 return $values;
169 }
170
171 /**
172 * Get all product categories.
173 */
174 function get_download_categories() {
175 $cats = get_terms([
176 'taxonomy' => 'download_category',
177 'hide_empty' => false
178 ]);
179 if(!is_array($cats)) {
180 $cats = [];
181 }
182 $values = [];
183 foreach ($cats as $cat) {
184 $values[] = [
185 'id' => $cat->term_id,
186 'text' => $cat->name
187 ];
188 }
189 return $values;
190 }
191 /**
192 * Get all product tags.
193 */
194 function get_download_tags() {
195 $tags = get_terms([
196 'taxonomy' => 'download_tag',
197 'hide_empty' => false
198 ]);
199 if(!is_array($tags)) {
200 $tags = [];
201 }
202 $values = [];
203 foreach ($tags as $tag) {
204 $values[] = [
205 'id' => $tag->term_id,
206 'text' => $tag->name
207 ];
208 }
209 return $values;
210 }
211
212 /**
213 * Get all pixel list.
214 * @param int|null $limit
215 * @return array
216 */
217 function pixelavo_get_pixel_list($limit) {
218 $pixel_option = get_option('pixelavo_pixels_list', []);
219 if(empty($pixel_option)) {
220 return $pixel_option;
221 }
222 if($limit) {
223 return pixelavo_data_clean(array_slice($pixel_option['pixel_lists'], 0, $limit));
224 }
225 return pixelavo_data_clean($pixel_option['pixel_lists']);
226 }
227
228 /**
229 * Check if any pixel status is active or not..
230 * @return boolean `true` if any pixel status is active else 'false`
231 */
232 function pixelavo_pixel_status() {
233 $pixel_list = pixelavo_get_pixel_list(apply_filters('pixelavo_pixels_limit', 1));
234 foreach ($pixel_list as $pixel) {
235 if($pixel['active'] == 'on' && pixelavo_check_pixel_page($pixel)) {
236 return true;
237 }
238 }
239 return false;
240 }
241
242 /**
243 * Check pixel page list.
244 * @param array $pixel
245 * @return bool Return `true` If allpages is selected or if specificpage is selected and current page `ID` exist in the specificpage array.
246 */
247 function pixelavo_check_pixel_page ($pixel) {
248 $page_id = get_queried_object_id();
249 if( function_exists('is_shop') && is_shop()) {
250 $page_id = wc_get_page_id('shop');
251 }
252 if (
253 $pixel['pixel_page'] == 'allpages' ||
254 (
255 $pixel['pixel_page'] == 'specificpage' &&
256 !empty($pixel['specificpage_list']) &&
257 in_array($page_id, $pixel['specificpage_list']) ||
258 in_array('term'.$page_id, $pixel['specificpage_list']) ||
259 (in_array('all_posts', $pixel['specificpage_list']) && get_post_type($page_id) == 'post' ) ||
260 (in_array('all_products', $pixel['specificpage_list']) && get_post_type($page_id) == 'product' ) ||
261 (in_array('all_downloads', $pixel['specificpage_list']) && get_post_type($page_id) == 'download' ) ||
262 (is_archive() && !empty(get_queried_object()->taxonomy) && in_array(get_queried_object()->taxonomy, $pixel['specificpage_list']) )
263 )
264 ) {
265 return true;
266 }
267 return false;
268 }
269
270 /**
271 * Check if current user role is included in `Exclude Roles`
272 * @return bool If current user role exist in `Exclude Roles` return `true` else `false`
273 */
274 function pixelavo_check_exclude_roles() {
275 $current_user = wp_get_current_user();
276 $user_roles = $current_user->roles;
277
278 $settings = get_option('pixelavo_settings', []);
279 $exclude_roles = array_key_exists('exclude_roles', $settings) ? $settings['exclude_roles'] : [];
280
281 if(count(array_intersect($user_roles, $exclude_roles))) {
282 return true;
283 }
284 return false;
285 }
286
287 /**
288 * Check if a event exists and it's enable.
289 * @param string $event Event name as a `string` in lowercase and word separate with `_`
290 * @return bool Return `true` if exists and enable otherwise `false`
291 */
292 function pixelavo_pixel_event_exists ($event) {
293 $pixel_event = pixelavo_get_option($event, 'pixelavo_events');
294 if($pixel_event && $pixel_event == 'on') {
295 return true;
296 }
297 return false;
298 }
299
300 /**
301 * Check if a Easy Digital Downloads event exists and it's enable.
302 * @param string $event Event name as a `string` in lowercase and word separate with `_`
303 * @return bool Return `true` if exists and enable otherwise `false`
304 */
305 function pixelavo_edd_event_exists ($event) {
306 $pixel_event = pixelavo_get_option($event, 'pixelavo_edd_events');
307 if($pixel_event && $pixel_event == 'on') {
308 return true;
309 }
310 return false;
311 }
312
313 /**
314 * Check if a custom event exists
315 * @param string $event Event name as a `string` in lowercase
316 * @return bool Return `true` if exists and enable otherwise `false`
317 */
318 function pixelavo_custom_event_exists ($target) {
319 $events = pixelavo_get_option('custom_events_lists', 'pixelavo_custom_events');
320 foreach ($events as $event) {
321 if(in_array($target, $event)) {
322 return true;
323 }
324 }
325 return false;
326 }
327
328 /**
329 * Get all Custom Events
330 * @return array
331 */
332 function pixelavo_get_custom_events () {
333 $custom_events = get_option('pixelavo_custom_events', []);
334 if(empty($custom_events)) {
335 return $custom_events;
336 }
337 return pixelavo_data_clean($custom_events['custom_events_lists']);
338 }
339
340 /**
341 * Prepare Custom Event Data
342 * @param array $params
343 * @return array
344 */
345 function prepare_custom_event_data ($params) {
346 $data = [];
347 if(!empty($params)) {
348 foreach ($params as $item) {
349 if(!empty($item)) {
350 $data[$item['name']] = $item['value'];
351 }
352 }
353 }
354 return $data;
355 }
356
357 /**
358 * Check if Other/Extra Event Exists
359 * @param string $event Event name as a `string` in lowercase
360 * @return bool Return `true` if exists and enable otherwise `false`
361 */
362 function pixelavo_other_event_exists ($event) {
363 $pixel_event = pixelavo_get_option($event, 'pixelavo_other_events');
364 if($pixel_event && $pixel_event === 'on') {
365 return true;
366 }
367 return false;
368 }
369 /**
370 * Get all Other/Extra Events
371 * @return array
372 */
373 function pixelavo_get_other_events () {
374 $other_events = get_option('pixelavo_other_events', []);
375 if(empty($other_events)) {
376 return $other_events;
377 }
378 return pixelavo_data_clean($other_events);
379 }
380 /**
381 * Get Event Common data
382 * @return array
383 */
384 function pixelavo_get_event_common_data() {
385 global $wp, $post;
386 $user = wp_get_current_user();
387 $post_type = get_post_type();
388 $data = [
389 'event_url' => home_url(add_query_arg([], $wp->request)),
390 ];
391 if($post_type) {
392 $data['post_type'] = $post_type;
393 }
394 if(is_singular( 'post' )) {
395 $data['page_title'] = $post->post_title;
396 $data['post_id'] = $post->ID;
397 } elseif( is_singular( 'page' ) || is_home()) {
398 $data['post_type'] = 'page';
399 $data['post_id'] = is_home() ? null : $post->ID;
400 $data['page_title'] = is_home() == true ? get_bloginfo( 'name' ) : $post->post_title;
401 } elseif (function_exists('is_shop') && is_shop()) {
402 $page_id = (int) wc_get_page_id( 'shop' );
403 $data['post_type'] = 'page';
404 $data['post_id'] = $page_id;
405 $data['page_title'] = get_the_title( $page_id );
406 } elseif ( is_category() ) {
407 $cat = get_query_var( 'cat' );
408 $term = get_category( $cat );
409 $data['post_type'] = 'category';
410 $data['post_id'] = $cat;
411 $data['page_title'] = $term->name;
412 } elseif ( is_tag() ) {
413 $slug = get_query_var( 'tag' );
414 $term = get_term_by( 'slug', $slug, 'post_tag' );
415 $data['post_type'] = 'tag';
416 if($term) {
417 $data['post_id'] = $term->term_id;
418 $data['page_title'] = $term->name;
419 }
420 } elseif (is_tax()) {
421 $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
422 $data['post_type'] = get_query_var( 'taxonomy' );
423 if ( $term ) {
424 $data['post_id'] = $term->term_id;
425 $data['page_title'] = $term->name;
426 }
427 } elseif(is_archive()){
428 $data['page_title'] = get_the_archive_title();
429 $data['post_type'] = 'archive';
430 } elseif ($post_type == 'product' || $post_type == 'download' ) {
431 $data['page_title'] = $post->post_title;
432 $data['post_id'] = $post->ID;
433 } elseif(is_search()) {
434 $data['page_title'] = 'Search';
435 } elseif(is_404()) {
436 $data['page_title'] = 'Page Not Found';
437 } else {
438 if(!empty($post)) {
439 $data['page_title'] = $post->post_title;
440 $data['post_id'] = $post->ID;
441 }
442 }
443 $data['plugin'] = 'Pixelavo';
444 $data['user_role'] = implode( ',', $user->roles );
445 return $data;
446 }
447
448 /**
449 * Check if woocommerce is installed
450 */
451 function pixelavo_is_woocommerce_install () {
452 $installed_plugins = get_plugins();
453 return isset($installed_plugins['woocommerce/woocommerce.php']);
454 }
455
456 /**
457 * Check if woocommerce is active
458 */
459 function pixelavo_is_woocommerce_active () {
460 return is_plugin_active( 'woocommerce/woocommerce.php');
461 }
462
463 /**
464 * Check if Easy Digital Downloads is active
465 */
466 function pixelavo_is_edd_active () {
467 return is_plugin_active( 'easy-digital-downloads/easy-digital-downloads.php');
468 }
469
470 /**
471 * Get client ip address.
472 * @return string
473 */
474 function pixelavo_get_client_ip() {
475 $ip = '';
476 if (isset($_SERVER['HTTP_CLIENT_IP']))
477 $ip = $_SERVER['HTTP_CLIENT_IP'];
478 else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
479 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
480 else if (isset($_SERVER['HTTP_X_FORWARDED']))
481 $ip = $_SERVER['HTTP_X_FORWARDED'];
482 else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
483 $ip = $_SERVER['HTTP_FORWARDED_FOR'];
484 else if (isset($_SERVER['HTTP_FORWARDED']))
485 $ip = $_SERVER['HTTP_FORWARDED'];
486 else if (isset($_SERVER['REMOTE_ADDR']))
487 $ip = $_SERVER['REMOTE_ADDR'];
488 else
489 $ip = 'UNKNOWN';
490 return sanitize_text_field($ip);
491 }
492
493 /**
494 * Pixel browser event.
495 * Inject pixel browser event script into website to run using `wp_footer` hook.
496 * @param string $event_name
497 * @param array $data
498 * @param string $eventID
499 * @param bool $customEvent
500 * @return void
501 */
502 function pixelavo_run_browser_event($event_name, $data, $event_id) {
503 echo "<script>fbq('track', '".esc_js($event_name)."', ".wp_json_encode($data).", {eventID: '".esc_js($event_id)."'});</script>";
504 }
505
506 /**
507 * Pixel conversions api.
508 * Check if conversion api is exist and active.
509 * Prepare all necessary data and send request to facebook pixel for server event.
510 * @param string $eventName
511 * @param array $data
512 * @param string $eventID
513 * @return void
514 */
515 function pixelavo_run_conversions_api($eventName, $data, $eventID = '') {
516 global $wp;
517
518 $current_url = home_url(add_query_arg(array(), $wp->request));
519 $pixels = pixelavo_get_pixel_list(apply_filters('pixelavo_pixels_limit', 1));
520 $client_ip = pixelavo_get_client_ip();
521 $client_user_agent = sanitize_text_field($_SERVER['HTTP_USER_AGENT']);
522
523 $advanced_matching_hashed = apply_filters('pixelavo_advanced_matching_hashed', []);
524 $advanced_matching = [];
525 foreach ($advanced_matching_hashed as $key => $value) {
526 $advanced_matching[$key] = [$value];
527 }
528 $user_data = array_merge($advanced_matching, [
529 "client_ip_address" => $client_ip,
530 "client_user_agent" => $client_user_agent
531 ]);
532
533 if(!empty($pixels)) {
534 foreach ($pixels as $pixel) {
535 if(
536 array_key_exists('conversion_api_status', $pixel) && $pixel['conversion_api_status'] == 'on' &&
537 pixelavo_check_pixel_page($pixel) && array_key_exists('access_token', $pixel) && !empty($pixel['access_token'])
538 ) {
539 $url = "https://graph.facebook.com/v15.0/{$pixel['pixel_id']}/events?access_token={$pixel['access_token']}";
540 wp_remote_post($url, [
541 'method' => 'POST',
542 'body' => [
543 "data" => [
544 [
545 "event_name" => $eventName,
546 "event_time" => time(),
547 "action_source" => "website",
548 "event_source_url" => $current_url,
549 "event_id" => $eventID,
550 "user_data" => $user_data,
551 "custom_data" => $data,
552 ]
553 ],
554 "test_event_code" => isset($pixel['event_code']) ? $pixel['event_code'] : ''
555 ],
556 ]);
557
558 }
559 }
560 }
561 }