| 1 |
<?php |
| 2 |
/** |
| 3 |
* BSF analytics Helper Class File. |
| 4 |
* |
| 5 |
* @package bsf-analytics |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
if ( ! class_exists( 'BSF_Analytics_Helper' ) ) { |
| 13 |
/** |
| 14 |
* BSF analytics stat class. |
| 15 |
*/ |
| 16 |
class BSF_Analytics_Helper { |
| 17 |
|
| 18 |
/** |
| 19 |
* Check is error in the received response. |
| 20 |
* |
| 21 |
* @param object $response Received API Response. |
| 22 |
* @return array $result Error result. |
| 23 |
*/ |
| 24 |
public static function is_api_error( $response ) { |
| 25 |
|
| 26 |
$result = array( |
| 27 |
'error' => false, |
| 28 |
'error_message' => __( 'Oops! Something went wrong. Please refresh the page and try again.' ), |
| 29 |
'error_code' => 0, |
| 30 |
); |
| 31 |
|
| 32 |
if ( is_wp_error( $response ) ) { |
| 33 |
$result['error'] = true; |
| 34 |
$result['error_message'] = $response->get_error_message(); |
| 35 |
$result['error_code'] = $response->get_error_code(); |
| 36 |
} elseif ( ! empty( wp_remote_retrieve_response_code( $response ) ) && ! in_array( wp_remote_retrieve_response_code( $response ), array( 200, 201, 204 ), true ) ) { |
| 37 |
$result['error'] = true; |
| 38 |
$result['error_message'] = wp_remote_retrieve_response_message( $response ); |
| 39 |
$result['error_code'] = wp_remote_retrieve_response_code( $response ); |
| 40 |
} |
| 41 |
|
| 42 |
return $result; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get API headers |
| 47 |
* |
| 48 |
* @since 1.1.6 |
| 49 |
* @return array<string, string> |
| 50 |
*/ |
| 51 |
public static function get_api_headers() { |
| 52 |
return array( |
| 53 |
'Content-Type' => 'application/json', |
| 54 |
'Accept' => 'application/json', |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get API URL for sending analytics. |
| 60 |
* |
| 61 |
* @return string API URL. |
| 62 |
* @since 1.0.0 |
| 63 |
*/ |
| 64 |
public static function get_api_url() { |
| 65 |
return defined( 'BSF_ANALYTICS_API_BASE_URL' ) ? BSF_ANALYTICS_API_BASE_URL : 'https://analytics.brainstormforce.com/'; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Check if the current screen is allowed for the survey. |
| 70 |
* |
| 71 |
* This function checks if the current screen is one of the allowed screens for displaying the survey. |
| 72 |
* It uses the `get_current_screen` function to get the current screen information and compares it with the list of allowed screens. |
| 73 |
* |
| 74 |
* @since 1.1.6 |
| 75 |
* @return bool True if the current screen is allowed, false otherwise. |
| 76 |
*/ |
| 77 |
public static function is_allowed_screen() { |
| 78 |
|
| 79 |
// This filter allows to dynamically modify the list of allowed screens for the survey. |
| 80 |
$allowed_screens = apply_filters( 'uds_survey_allowed_screens', array( 'plugins' ) ); |
| 81 |
|
| 82 |
$current_screen = get_current_screen(); |
| 83 |
|
| 84 |
// Check if $current_screen is a valid object before accessing its properties. |
| 85 |
if ( ! is_object( $current_screen ) ) { |
| 86 |
return false; // Return false if current screen is not valid. |
| 87 |
} |
| 88 |
|
| 89 |
$screen_id = $current_screen->id; |
| 90 |
|
| 91 |
if ( ! empty( $screen_id ) && in_array( $screen_id, $allowed_screens, true ) ) { |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
return false; |
| 96 |
} |
| 97 |
} |
| 98 |
} |