PluginProbe ʕ •ᴥ•ʔ
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.3
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.3
2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.13.0 1.13.1 1.13.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.2 2.6.0
sureforms / inc / abilities / analytics / get-form-analytics.php
sureforms / inc / abilities / analytics Last commit date
get-form-analytics.php 5 months ago
get-form-analytics.php
188 lines
1 <?php
2 /**
3 * Get Form Analytics Ability.
4 *
5 * @package sureforms
6 * @since 2.6.0
7 */
8
9 namespace SRFM\Inc\Abilities\Analytics;
10
11 use SRFM\Inc\Abilities\Abstract_Ability;
12 use SRFM\Inc\Database\Tables\Entries;
13 use SRFM\Inc\Helper;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Get_Form_Analytics ability class.
21 *
22 * Retrieves form submission analytics for a given date range.
23 *
24 * @since 2.6.0
25 */
26 class Get_Form_Analytics extends Abstract_Ability {
27 /**
28 * Maximum number of analytics results to return.
29 *
30 * @since 2.6.0
31 */
32 public const MAX_RESULTS = 10000;
33
34 /**
35 * Constructor.
36 *
37 * @since 2.6.0
38 */
39 public function __construct() {
40 $this->id = 'sureforms/get-form-analytics';
41 $this->label = __( 'Get Form Analytics', 'sureforms' );
42 $this->description = __( 'Retrieve form submission analytics data for a specified date range, optionally filtered by form ID.', 'sureforms' );
43 $this->capability = 'manage_options';
44 }
45
46 /**
47 * {@inheritDoc}
48 *
49 * @since 2.6.0
50 */
51 public function get_annotations() {
52 return [
53 'readonly' => true,
54 'destructive' => false,
55 'idempotent' => true,
56 'priority' => 1.0,
57 'openWorldHint' => false,
58 ];
59 }
60
61 /**
62 * {@inheritDoc}
63 *
64 * @since 2.6.0
65 */
66 public function get_input_schema() {
67 return [
68 'type' => 'object',
69 'additionalProperties' => false,
70 'properties' => [
71 'date_from' => [
72 'type' => 'string',
73 'description' => __( 'Start date for analytics range (Y-m-d).', 'sureforms' ),
74 ],
75 'date_to' => [
76 'type' => 'string',
77 'description' => __( 'End date for analytics range (Y-m-d).', 'sureforms' ),
78 ],
79 'form_id' => [
80 'type' => 'integer',
81 'description' => __( 'Optional form ID to filter analytics by.', 'sureforms' ),
82 ],
83 ],
84 'required' => [ 'date_from', 'date_to' ],
85 ];
86 }
87
88 /**
89 * {@inheritDoc}
90 *
91 * @since 2.6.0
92 */
93 public function get_output_schema() {
94 return [
95 'type' => 'object',
96 'properties' => [
97 'submissions' => [
98 'type' => 'array',
99 'items' => [
100 'type' => 'object',
101 'properties' => [
102 'created_at' => [ 'type' => 'string' ],
103 ],
104 ],
105 ],
106 'total_count' => [ 'type' => 'integer' ],
107 'truncated' => [ 'type' => 'boolean' ],
108 'form_id' => [ 'type' => [ 'integer', 'null' ] ],
109 'date_from' => [ 'type' => 'string' ],
110 'date_to' => [ 'type' => 'string' ],
111 ],
112 ];
113 }
114
115 /**
116 * Execute the get-form-analytics ability.
117 *
118 * @param array<string,mixed> $input Validated input data.
119 * @since 2.6.0
120 * @return array<string,mixed>|\WP_Error
121 */
122 public function execute( $input ) {
123 $date_from = sanitize_text_field( Helper::get_string_value( $input['date_from'] ?? '' ) );
124 $date_to = sanitize_text_field( Helper::get_string_value( $input['date_to'] ?? '' ) );
125 $form_id = ! empty( $input['form_id'] ) ? absint( Helper::get_integer_value( $input['form_id'] ) ) : 0;
126
127 if ( empty( $date_from ) || empty( $date_to ) ) {
128 return new \WP_Error(
129 'srfm_missing_dates',
130 __( 'Both date_from and date_to are required.', 'sureforms' ),
131 [ 'status' => 400 ]
132 );
133 }
134
135 if ( ! Helper::validate_date( $date_from ) || ! Helper::validate_date( $date_to ) ) {
136 return new \WP_Error(
137 'srfm_invalid_date_format',
138 __( 'Dates must be in Y-m-d format.', 'sureforms' ),
139 [ 'status' => 400 ]
140 );
141 }
142
143 $where = [
144 [
145 [
146 'key' => 'created_at',
147 'value' => $date_from,
148 'compare' => '>=',
149 ],
150 [
151 'key' => 'created_at',
152 'value' => $date_to,
153 'compare' => '<=',
154 ],
155 ],
156 ];
157
158 if ( $form_id ) {
159 $where[0][] = [
160 'key' => 'form_id',
161 'value' => $form_id,
162 'compare' => '=',
163 ];
164 }
165
166 $submissions = Entries::get_instance()->get_results(
167 $where,
168 'created_at',
169 [ 'ORDER BY created_at DESC', 'LIMIT ' . ( self::MAX_RESULTS + 1 ) ]
170 );
171
172 $truncated = count( $submissions ) > self::MAX_RESULTS;
173
174 if ( $truncated ) {
175 $submissions = array_slice( $submissions, 0, self::MAX_RESULTS );
176 }
177
178 return [
179 'submissions' => $submissions,
180 'total_count' => count( $submissions ),
181 'truncated' => $truncated,
182 'form_id' => $form_id ? $form_id : null,
183 'date_from' => $date_from,
184 'date_to' => $date_to,
185 ];
186 }
187 }
188