Metabox
1 week ago
views
1 week ago
Dashboard.php
1 week ago
DashboardTracker.php
1 week ago
DashboardTrackingData.php
1 week ago
DashboardTrackingReceiver.php
1 week ago
FSIE.php
1 week ago
FSPro.php
1 week ago
FSWalkthrough.php
1 week ago
FSWalkthroughPL.php
1 week ago
Metabox.php
1 week ago
Video.php
1 week ago
WooCommerceABC.php
1 week ago
WooCommerceABCPL.php
1 week ago
DashboardTrackingReceiver.php
140 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Dashboard tracking transport. |
| 4 | * |
| 5 | * @package WPDesk\FS\Info |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types=1 ); |
| 9 | |
| 10 | namespace WPDesk\FS\Info; |
| 11 | |
| 12 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 13 | |
| 14 | /** |
| 15 | * Receives dashboard counter increments from Heartbeat and AJAX. |
| 16 | */ |
| 17 | final class DashboardTrackingReceiver implements Hookable { |
| 18 | |
| 19 | const AJAX_ACTION = 'flexible_shipping_dashboard_tracking'; |
| 20 | const PACKET_KEY = 'flexible_shipping_dashboard'; |
| 21 | |
| 22 | const MAX_VISIBLE_TIME_SECONDS = 300; |
| 23 | const SCREEN_ID = 'woocommerce_page_wc-settings'; |
| 24 | const SECTION_ID = 'flexible_shipping_info'; |
| 25 | |
| 26 | /** |
| 27 | * @var DashboardTrackingData |
| 28 | */ |
| 29 | private DashboardTrackingData $tracking_data; |
| 30 | |
| 31 | public function __construct( DashboardTrackingData $tracking_data ) { |
| 32 | $this->tracking_data = $tracking_data; |
| 33 | } |
| 34 | |
| 35 | public function hooks(): void { |
| 36 | add_filter( 'heartbeat_received', [ $this, 'receive_heartbeat' ], 10, 3 ); |
| 37 | add_action( 'wp_ajax_' . self::AJAX_ACTION, [ $this, 'receive_ajax' ] ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param array<string, mixed> $response Heartbeat response. |
| 42 | * @param array<string, mixed> $data Heartbeat request data. |
| 43 | * @param string $screen_id Current screen ID. |
| 44 | * |
| 45 | * @return array<string, mixed> |
| 46 | */ |
| 47 | public function receive_heartbeat( array $response, array $data, string $screen_id ): array { |
| 48 | if ( self::SCREEN_ID === $screen_id && $this->request_is_allowed() && isset( $data[ self::PACKET_KEY ] ) ) { |
| 49 | $this->save_packet( $data[ self::PACKET_KEY ] ); |
| 50 | } |
| 51 | |
| 52 | return $response; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @internal |
| 57 | */ |
| 58 | public function receive_ajax(): void { |
| 59 | check_ajax_referer( self::AJAX_ACTION ); |
| 60 | |
| 61 | if ( ! $this->request_is_allowed() ) { |
| 62 | wp_send_json_error(); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | $packet = json_decode( sanitize_text_field( wp_unslash( $_POST['events'] ?? '' ) ), true ); |
| 67 | if ( ! $this->save_packet( $packet ) ) { |
| 68 | wp_send_json_error(); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | wp_send_json_success(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param mixed $packet Packet to validate and save. |
| 77 | */ |
| 78 | private function save_packet( $packet ): bool { |
| 79 | if ( ! is_array( $packet ) || [] === $packet ) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | $allowed = $this->allowed_fields(); |
| 84 | foreach ( $packet as $field => $increment ) { |
| 85 | if ( ! in_array( $field, $allowed, true ) || ! is_int( $increment ) || $increment < 0 ) { |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if ( ( $packet['visible_time_seconds'] ?? 0 ) > self::MAX_VISIBLE_TIME_SECONDS ) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | $this->tracking_data->increase( |
| 95 | $packet, |
| 96 | wpdesk_is_plugin_active( 'flexible-shipping-pro/flexible-shipping-pro.php' ) |
| 97 | ); |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | private function request_is_allowed(): bool { |
| 103 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | $query = parse_url( (string) wp_get_referer(), PHP_URL_QUERY ); |
| 108 | parse_str( is_string( $query ) ? $query : '', $parameters ); |
| 109 | |
| 110 | return self::SECTION_ID === ( $parameters['section'] ?? '' ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @return string[] |
| 115 | */ |
| 116 | private function allowed_fields(): array { |
| 117 | return [ |
| 118 | 'views_count', |
| 119 | 'visible_time_seconds', |
| 120 | 'navigation_click_count', |
| 121 | 'reached_end_count', |
| 122 | 'create_method_click_count', |
| 123 | 'extensions_click_count', |
| 124 | 'onboarding.step_1_reached_count', |
| 125 | 'onboarding.step_2_reached_count', |
| 126 | 'onboarding.step_3_reached_count', |
| 127 | 'onboarding.step_4_reached_count', |
| 128 | 'onboarding.step_5_reached_count', |
| 129 | 'onboarding.step_6_reached_count', |
| 130 | 'onboarding.completed_count', |
| 131 | 'onboarding.restart_count', |
| 132 | 'onboarding.add_zone_click_count', |
| 133 | 'onboarding.add_method_click_count', |
| 134 | 'onboarding.open_cart_click_count', |
| 135 | 'onboarding.activate_license_click_count', |
| 136 | 'onboarding.check_extensions_click_count', |
| 137 | ]; |
| 138 | } |
| 139 | } |
| 140 |