activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
4 months ago
auth_helper.php
3 months ago
blocks_helper.php
3 months ago
booking_helper.php
3 months ago
bricks_helper.php
3 months ago
bundles_helper.php
3 months ago
calendar_helper.php
3 months ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
3 months ago
customer_import_helper.php
3 months ago
database_helper.php
3 months ago
debug_helper.php
3 months ago
defaults_helper.php
3 months ago
elementor_helper.php
3 months ago
email_helper.php
3 months ago
encrypt_helper.php
3 months ago
events_helper.php
3 months ago
form_helper.php
3 months ago
icalendar_helper.php
3 months ago
image_helper.php
3 months ago
invoices_helper.php
3 months ago
license_helper.php
3 months ago
location_helper.php
3 months ago
marketing_systems_helper.php
3 months ago
meeting_systems_helper.php
3 months ago
menu_helper.php
3 months ago
meta_helper.php
3 months ago
migrations_helper.php
3 months ago
money_helper.php
3 months ago
notifications_helper.php
3 months ago
nps_survey_helper.php
3 months ago
order_intent_helper.php
3 months ago
orders_helper.php
3 months ago
otp_helper.php
3 months ago
pages_helper.php
3 months ago
params_helper.php
3 months ago
payments_helper.php
3 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
replacer_helper.php
3 months ago
resource_helper.php
3 months ago
roles_helper.php
3 months ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
3 months ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
3 months ago
sms_helper.php
3 months ago
steps_helper.php
3 months ago
stripe_connect_helper.php
3 months ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
3 months ago
timeline_helper.php
3 months ago
transaction_helper.php
3 months ago
transaction_intent_helper.php
3 months ago
util_helper.php
3 months ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
3 months ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
analytics_helper.php
165 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly. |
| 5 | } |
| 6 | |
| 7 | class OsAnalyticsHelper { |
| 8 | |
| 9 | /** |
| 10 | * Initialize BSF Analytics. |
| 11 | * |
| 12 | * @return void |
| 13 | */ |
| 14 | public static function init() { |
| 15 | |
| 16 | add_action( 'latepoint_settings_updated', array( self::class, 'update_contribute_option' ) ); |
| 17 | |
| 18 | if ( ! class_exists( 'BSF_Analytics_Loader' ) ) { |
| 19 | require_once LATEPOINT_ABSPATH . 'lib/kit/bsf-analytics/class-bsf-analytics-loader.php'; |
| 20 | } |
| 21 | |
| 22 | if ( ! class_exists( 'Astra_Notices' ) ) { |
| 23 | require_once LATEPOINT_ABSPATH . 'lib/kit/astra-notices/class-astra-notices.php'; |
| 24 | } |
| 25 | |
| 26 | $bsf_analytics = \BSF_Analytics_Loader::get_instance(); |
| 27 | |
| 28 | $bsf_analytics->set_entity( |
| 29 | [ |
| 30 | 'latepoint' => [ |
| 31 | 'product_name' => 'LatePoint', |
| 32 | 'path' => LATEPOINT_ABSPATH . 'lib/kit/bsf-analytics', |
| 33 | 'author' => 'LatePoint', |
| 34 | 'time_to_display' => '+24 hours', |
| 35 | 'deactivation_survey' => apply_filters( |
| 36 | 'latepoint_deactivation_survey_data', |
| 37 | [ |
| 38 | [ |
| 39 | 'id' => 'deactivation-survey-latepoint', |
| 40 | 'popup_logo' => LATEPOINT_IMAGES_URL . 'logo.svg', |
| 41 | 'plugin_slug' => 'latepoint', |
| 42 | 'popup_title' => 'Quick Feedback', |
| 43 | 'support_url' => 'https://latepoint.com/support/', |
| 44 | 'popup_description' => 'If you have a moment, please share why you are deactivating LatePoint:', |
| 45 | 'show_on_screens' => [ 'plugins' ], |
| 46 | 'plugin_version' => LATEPOINT_VERSION, |
| 47 | ], |
| 48 | ] |
| 49 | ), |
| 50 | ], |
| 51 | ] |
| 52 | ); |
| 53 | |
| 54 | add_filter( 'bsf_core_stats', [ __CLASS__, 'add_latepoint_analytics_data' ] ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Toggle contribute to latepoint from general settings. |
| 59 | * |
| 60 | * @param array<mixed> $settings settings array. |
| 61 | * @return bool |
| 62 | */ |
| 63 | public static function update_contribute_option( $settings ) { |
| 64 | if ( isset( $settings['contribute_to_latepoint'] ) && 'on' === $settings['contribute_to_latepoint'] ) { |
| 65 | $enable_tracking = 'yes'; |
| 66 | } else { |
| 67 | $enable_tracking = ''; |
| 68 | } |
| 69 | |
| 70 | return update_option( 'latepoint_usage_optin', $enable_tracking ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Add LatePoint specific analytics data. |
| 75 | * |
| 76 | * @param array $stats_data Existing stats data. |
| 77 | * @return array |
| 78 | */ |
| 79 | public static function add_latepoint_analytics_data( $stats_data ) { |
| 80 | $stats_data['plugin_data']['latepoint'] = [ |
| 81 | 'free_version' => LATEPOINT_VERSION, |
| 82 | 'db_version' => LATEPOINT_DB_VERSION, |
| 83 | 'site_language' => get_locale(), |
| 84 | ]; |
| 85 | |
| 86 | $stats_data['plugin_data']['latepoint']['numeric_values'] = [ |
| 87 | 'total_bookings' => self::get_table_count( LATEPOINT_TABLE_BOOKINGS ), |
| 88 | 'total_services' => self::get_table_count( LATEPOINT_TABLE_SERVICES ), |
| 89 | 'total_agents' => self::get_table_count( LATEPOINT_TABLE_AGENTS ), |
| 90 | 'total_customers' => self::get_table_count( LATEPOINT_TABLE_CUSTOMERS ), |
| 91 | 'total_locations' => self::get_table_count( LATEPOINT_TABLE_LOCATIONS ), |
| 92 | ]; |
| 93 | |
| 94 | // Add KPI tracking data. |
| 95 | $kpi_data = self::get_kpi_tracking_data(); |
| 96 | if ( ! empty( $kpi_data ) ) { |
| 97 | $stats_data['plugin_data']['latepoint']['kpi_records'] = $kpi_data; |
| 98 | } |
| 99 | |
| 100 | return $stats_data; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get KPI tracking data for the last 2 days (excluding today). |
| 105 | * |
| 106 | * @return array KPI data organized by date. |
| 107 | */ |
| 108 | private static function get_kpi_tracking_data() { |
| 109 | $kpi_data = []; |
| 110 | $today = current_time( 'Y-m-d' ); |
| 111 | |
| 112 | for ( $i = 1; $i <= 2; $i++ ) { |
| 113 | $date = gmdate( 'Y-m-d', strtotime( $today . ' -' . $i . ' days' ) ); |
| 114 | $bookings = self::get_daily_count( LATEPOINT_TABLE_BOOKINGS, $date ); |
| 115 | $orders = self::get_daily_count( LATEPOINT_TABLE_ORDERS, $date ); |
| 116 | |
| 117 | $kpi_data[ $date ] = [ |
| 118 | 'numeric_values' => [ |
| 119 | 'bookings' => $bookings, |
| 120 | 'orders' => $orders, |
| 121 | ], |
| 122 | ]; |
| 123 | } |
| 124 | |
| 125 | return $kpi_data; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get count of rows created on a specific date. |
| 130 | * |
| 131 | * @param string $table_name Full table name. |
| 132 | * @param string $date Date in Y-m-d format. |
| 133 | * @return int |
| 134 | */ |
| 135 | private static function get_daily_count( $table_name, $date ) { |
| 136 | global $wpdb; |
| 137 | |
| 138 | $start_date = $date . ' 00:00:00'; |
| 139 | $end_date = $date . ' 23:59:59'; |
| 140 | |
| 141 | $count = $wpdb->get_var( |
| 142 | $wpdb->prepare( |
| 143 | 'SELECT COUNT(*) FROM %i WHERE created_at >= %s AND created_at <= %s', |
| 144 | $table_name, |
| 145 | $start_date, |
| 146 | $end_date |
| 147 | ) |
| 148 | ); |
| 149 | |
| 150 | return $count ? (int) $count : 0; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get total row count from a table. |
| 155 | * |
| 156 | * @param string $table_name Full table name. |
| 157 | * @return int |
| 158 | */ |
| 159 | private static function get_table_count( $table_name ) { |
| 160 | global $wpdb; |
| 161 | $count = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $table_name ) ); |
| 162 | return $count ? (int) $count : 0; |
| 163 | } |
| 164 | } |
| 165 |