| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File naming is acceptable |
| 2 |
/** |
| 3 |
* Analytify REST API class. |
| 4 |
* |
| 5 |
* @package WP_Analytify |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly. |
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
require_once __DIR__ . '/analytify-rest/bootstrap.php'; |
| 14 |
require_once __DIR__ . '/analytify-rest/endpoints-general.php'; |
| 15 |
require_once __DIR__ . '/analytify-rest/endpoints-content.php'; |
| 16 |
require_once __DIR__ . '/analytify-rest/endpoints-dimensions.php'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Handle Analytify REST API endpoints. |
| 20 |
* |
| 21 |
* @package WP_Analytify |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
class Analytify_Rest_API { |
| 25 |
use Analytify_Rest_Bootstrap; |
| 26 |
use Analytify_Rest_Endpoints_General; |
| 27 |
use Analytify_Rest_Endpoints_Content; |
| 28 |
use Analytify_Rest_Endpoints_Dimensions; |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Adds 'General Statistics', 'Scroll Depth Reach' sections for single post stats. |
| 33 |
* |
| 34 |
* @param array<string, mixed> $sections Sections. |
| 35 |
* @param int $post_id Post id. |
| 36 |
* @param array<int, string> $date Start and End date. |
| 37 |
* @return array<string, mixed> |
| 38 |
*/ |
| 39 |
public function single_post_sections( $sections, $post_id, $date ): array { |
| 40 |
|
| 41 |
$show_settings = $this->wp_analytify->settings->get_option( 'show_panels_back_end', 'wp-analytify-admin', array( 'show-overall-dashboard' ) ); |
| 42 |
if ( empty( $show_settings ) || ( ! in_array( 'show-overall-dashboard', $show_settings, true ) && ! in_array( 'show-scroll-depth-stats', $show_settings, true ) ) ) { |
| 43 |
return $sections; |
| 44 |
} |
| 45 |
|
| 46 |
$report = new Analytify_Report( |
| 47 |
array( |
| 48 |
'dashboard_type' => 'single_post', |
| 49 |
'start_date' => $date[0], |
| 50 |
'end_date' => $date[1], |
| 51 |
'post_id' => $post_id, |
| 52 |
) |
| 53 |
); |
| 54 |
|
| 55 |
if ( in_array( 'show-overall-dashboard', $show_settings, true ) ) { |
| 56 |
$general_stats = $report->get_general_stats(); |
| 57 |
$sections['general_stats'] = array( |
| 58 |
'title' => esc_html__( 'General Statistics', 'wp-analytify' ), |
| 59 |
'type' => 'boxes', |
| 60 |
'stats' => $general_stats['boxes'], |
| 61 |
'new_vs_returning' => $general_stats['new_vs_returning_boxes'], |
| 62 |
'device_visitors' => $general_stats['device_visitors_boxes'], |
| 63 |
// TODO: add footer. |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
if ( in_array( 'show-scroll-depth-stats', $show_settings, true ) && 'on' === $this->wp_analytify->settings->get_option( 'depth_percentage', 'wp-analytify-advanced' ) ) { |
| 68 |
|
| 69 |
$scroll_depth_stats = $report->get_scroll_depth_stats(); |
| 70 |
|
| 71 |
$sections['scroll_depth'] = array( |
| 72 |
'title' => esc_html__( 'Scroll Depth Reach', 'wp-analytify' ), |
| 73 |
'type' => 'table', |
| 74 |
'table_class' => 'analytify_bar_tables', |
| 75 |
'headers' => array( |
| 76 |
'percentage' => array( |
| 77 |
'label' => esc_html__( 'Scroll Percentage', 'wp-analytify' ), |
| 78 |
'th_class' => 'analytify_txt_left', |
| 79 |
'td_class' => '', |
| 80 |
), |
| 81 |
'events' => array( |
| 82 |
'label' => esc_html__( 'Total Reached', 'wp-analytify' ), |
| 83 |
'th_class' => '', |
| 84 |
'td_class' => 'analytify_txt_center analytify_value_row', |
| 85 |
), |
| 86 |
), |
| 87 |
'stats' => $scroll_depth_stats['stats'], |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
return $sections; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Formate 'general_statistics' footer, add labels and description. |
| 96 |
* |
| 97 |
* @param string $number Number to format. |
| 98 |
* @param array<string, mixed> $data Start and End date (unused). |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
public function general_stats_footer( $number, $data ): string { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Parameter required by filter |
| 103 |
$time_value = is_numeric( $number ) ? WPANALYTIFY_Utils::pretty_time( (float) $number ) : '0'; |
| 104 |
// translators: %s is the formatted time duration. |
| 105 |
return sprintf( __( 'Total time visitors spent on your site: %s.', 'wp-analytify' ), '<span class="analytify_red general_stats_message">' . $time_value . '</span>' ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get profile related data based on the key (option) provided. |
| 110 |
* |
| 111 |
* @param string $key Option name. |
| 112 |
* @return string|null |
| 113 |
*/ |
| 114 |
private function get_profile_info( $key ) { |
| 115 |
$dashboard_profile_id = $this->wp_analytify->settings->get_option( 'profile_for_dashboard', 'wp-analytify-profile' ); |
| 116 |
switch ( $key ) { |
| 117 |
case 'profile_id': |
| 118 |
return $dashboard_profile_id; |
| 119 |
case 'website_url': |
| 120 |
return WP_ANALYTIFY_FUNCTIONS::search_profile_info( $dashboard_profile_id, 'websiteUrl' ); |
| 121 |
default: |
| 122 |
return null; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Sets compare dates based on the start an end dates. |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
private function set_compare_dates() { |
| 132 |
$date_diff = WPANALYTIFY_Utils::calculate_date_diff( $this->start_date, $this->end_date ); |
| 133 |
if ( ! $date_diff ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
$this->compare_start_date = $date_diff['start_date']; |
| 138 |
$this->compare_end_date = $date_diff['end_date']; |
| 139 |
$this->compare_days = $date_diff['diff_days']; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Compares current stat with the previous one and returns the formatted difference. |
| 144 |
* |
| 145 |
* @param int $current_stat Current stat. |
| 146 |
* @param int $old_stat Old stat to compare with. |
| 147 |
* @param string $type Type of stat (key). |
| 148 |
* |
| 149 |
* @return array<string, mixed>|false |
| 150 |
*/ |
| 151 |
private function compare_stat( $current_stat, $old_stat, $type ) { |
| 152 |
|
| 153 |
// Check for compare dates. |
| 154 |
if ( is_null( $this->compare_start_date ) || is_null( $this->compare_end_date ) || is_null( $this->compare_days ) ) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
// So we don't divide by zero. |
| 159 |
if ( ! $old_stat || 0 === $old_stat ) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
$number = number_format( ( ( $current_stat - $old_stat ) / $old_stat ) * 100, 2 ); |
| 163 |
|
| 164 |
if ( 'bounce_rate' === $type ) { |
| 165 |
$arrow_type = ( $number < 0 ) ? 'analytify_green_inverted' : 'analytify_red_inverted'; |
| 166 |
} else { |
| 167 |
$arrow_type = ( $number > 0 ) ? 'analytify_green' : 'analytify_red'; |
| 168 |
} |
| 169 |
|
| 170 |
return array( |
| 171 |
'arrow_type' => $arrow_type, |
| 172 |
'main_text' => $number . esc_html__( '%', 'wp-analytify' ), |
| 173 |
// translators: Days. |
| 174 |
'sub_text' => sprintf( esc_html__( '%s days ago', 'wp-analytify' ), $this->compare_days ), |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns start and end date as an array to be used for GA4's get_reports() |
| 180 |
* |
| 181 |
* @return array<string, mixed> |
| 182 |
*/ |
| 183 |
private function get_dates(): array { |
| 184 |
return array( |
| 185 |
'start' => $this->start_date, |
| 186 |
'end' => $this->end_date, |
| 187 |
); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Init the instance. |
| 193 |
*/ |
| 194 |
Analytify_Rest_API::get_instance(); |
| 195 |
|