\d+)/track', array( 'methods' => 'POST', 'permission_callback' => '__return_true', 'callback' => array( __CLASS__, 'rest_track' ), ) ); } /** Track a view or a start. */ public static function rest_track( \WP_REST_Request $req ) { $id = (int) $req['id']; $event = (string) $req->get_param( 'event' ); if ( ! in_array( $event, array( 'view', 'start' ), true ) || ! Form::exists( $id ) ) { return rest_ensure_response( array( 'ok' => false ) ); } if ( current_user_can( 'fwren_manage_forms' ) || empty( Form::settings( $id )['analytics']['enabled'] ) ) { return rest_ensure_response( array( 'ok' => true ) ); } $key = 'fwren_tr_' . md5( Submission::ip() . $id . $event . gmdate( 'Ymd' ) ); if ( (int) get_transient( $key ) > 30 ) { return rest_ensure_response( array( 'ok' => true ) ); } set_transient( $key, (int) get_transient( $key ) + 1, DAY_IN_SECONDS ); self::bump( $id, 'view' === $event ? 'views' : 'starts' ); return rest_ensure_response( array( 'ok' => true ) ); } /** Server-side views are not counted (page caches would hide them); the browser reports. */ public static function track_view( $form_id ) {} /** Count a submission. */ public static function track_submission( $form_id ) { self::bump( $form_id, 'submissions' ); } /** Increment a daily counter. */ private static function bump( $form_id, $col ) { global $wpdb; $t = Install::table( 'stats' ); $day = current_time( 'Y-m-d' ); $wpdb->query( $wpdb->prepare( "INSERT INTO $t (form_id, day, $col) VALUES (%d, %s, 1) ON DUPLICATE KEY UPDATE $col = $col + 1", $form_id, $day ) ); // phpcs:ignore } /** @return array views, starts, submissions totals. */ public static function totals( $form_id = 0, $days = 0 ) { global $wpdb; $t = Install::table( 'stats' ); $where = array( '1=1' ); if ( $form_id ) { $where[] = $wpdb->prepare( 'form_id = %d', $form_id ); } if ( $days ) { $where[] = $wpdb->prepare( 'day >= %s', gmdate( 'Y-m-d', current_time( 'timestamp' ) - $days * DAY_IN_SECONDS ) ); // phpcs:ignore } $row = $wpdb->get_row( "SELECT SUM(views) views, SUM(starts) starts, SUM(submissions) submissions FROM $t WHERE " . implode( ' AND ', $where ), ARRAY_A ); // phpcs:ignore return array( 'views' => (int) ( $row['views'] ?? 0 ), 'starts' => (int) ( $row['starts'] ?? 0 ), 'submissions' => (int) ( $row['submissions'] ?? 0 ), ); } /** @return array Daily series for the last N days: day => [views, starts, submissions]. */ public static function series( $form_id = 0, $days = 30 ) { global $wpdb; $t = Install::table( 'stats' ); $from = gmdate( 'Y-m-d', current_time( 'timestamp' ) - ( $days - 1 ) * DAY_IN_SECONDS ); // phpcs:ignore $where = $wpdb->prepare( 'day >= %s', $from ) . ( $form_id ? $wpdb->prepare( ' AND form_id = %d', $form_id ) : '' ); $rows = $wpdb->get_results( "SELECT day, SUM(views) views, SUM(starts) starts, SUM(submissions) submissions FROM $t WHERE $where GROUP BY day", ARRAY_A ); // phpcs:ignore $map = array(); foreach ( $rows as $r ) { $map[ $r['day'] ] = $r; } $out = array(); for ( $i = $days - 1; $i >= 0; $i-- ) { $d = gmdate( 'Y-m-d', current_time( 'timestamp' ) - $i * DAY_IN_SECONDS ); // phpcs:ignore $out[ $d ] = array( 'views' => (int) ( $map[ $d ]['views'] ?? 0 ), 'starts' => (int) ( $map[ $d ]['starts'] ?? 0 ), 'submissions' => (int) ( $map[ $d ]['submissions'] ?? 0 ), ); } return $out; } }