create-form.php
1 month ago
delete-form.php
5 months ago
duplicate-form.php
5 months ago
form-field-schema.php
2 months ago
form-metadata.php
1 month ago
get-form-stats.php
5 months ago
get-form.php
5 months ago
get-shortcode.php
5 months ago
list-forms.php
2 months ago
update-form.php
1 month ago
get-form-stats.php
203 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Get Form Stats Ability. |
| 4 | * |
| 5 | * @package sureforms |
| 6 | * @since 2.5.2 |
| 7 | */ |
| 8 | |
| 9 | namespace SRFM\Inc\Abilities\Forms; |
| 10 | |
| 11 | use SRFM\Inc\Abilities\Abstract_Ability; |
| 12 | use SRFM\Inc\Database\Tables\Entries as EntriesTable; |
| 13 | use SRFM\Inc\Helper; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; // Exit if accessed directly. |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Get_Form_Stats ability class. |
| 21 | * |
| 22 | * Retrieves submission statistics for a specific form or all forms. |
| 23 | * |
| 24 | * @since 2.5.2 |
| 25 | */ |
| 26 | class Get_Form_Stats extends Abstract_Ability { |
| 27 | /** |
| 28 | * Constructor. |
| 29 | * |
| 30 | * @since 2.5.2 |
| 31 | */ |
| 32 | public function __construct() { |
| 33 | $this->id = 'sureforms/get-form-stats'; |
| 34 | $this->label = __( 'Get Form Statistics', 'sureforms' ); |
| 35 | $this->description = __( 'Get submission statistics for a specific SureForms form or all forms. Returns total entries, unread, read, and trash counts.', 'sureforms' ); |
| 36 | $this->capability = 'manage_options'; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * {@inheritDoc} |
| 41 | * |
| 42 | * @since 2.5.2 |
| 43 | */ |
| 44 | public function get_annotations() { |
| 45 | return [ |
| 46 | 'readonly' => true, |
| 47 | 'destructive' => false, |
| 48 | 'idempotent' => true, |
| 49 | 'priority' => 1.0, |
| 50 | 'openWorldHint' => false, |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * {@inheritDoc} |
| 56 | * |
| 57 | * @since 2.5.2 |
| 58 | */ |
| 59 | public function get_input_schema() { |
| 60 | return [ |
| 61 | 'type' => 'object', |
| 62 | 'additionalProperties' => false, |
| 63 | 'properties' => [ |
| 64 | 'form_id' => [ |
| 65 | 'type' => 'integer', |
| 66 | 'description' => __( 'The ID of the form to get stats for. Use 0 or omit for all forms.', 'sureforms' ), |
| 67 | 'default' => 0, |
| 68 | ], |
| 69 | ], |
| 70 | ]; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * {@inheritDoc} |
| 75 | * |
| 76 | * @since 2.5.2 |
| 77 | */ |
| 78 | public function get_output_schema() { |
| 79 | return [ |
| 80 | 'type' => 'object', |
| 81 | 'properties' => [ |
| 82 | 'form_id' => [ 'type' => 'integer' ], |
| 83 | 'form_title' => [ 'type' => 'string' ], |
| 84 | 'form_status' => [ 'type' => 'string' ], |
| 85 | 'total_entries' => [ 'type' => 'integer' ], |
| 86 | 'unread_count' => [ 'type' => 'integer' ], |
| 87 | 'read_count' => [ 'type' => 'integer' ], |
| 88 | 'trash_count' => [ 'type' => 'integer' ], |
| 89 | ], |
| 90 | ]; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Execute the get-form-stats ability. |
| 95 | * |
| 96 | * @param array<string,mixed> $input Validated input data. |
| 97 | * @since 2.5.2 |
| 98 | * @return array<string,mixed>|\WP_Error |
| 99 | */ |
| 100 | public function execute( $input ) { |
| 101 | $form_id = Helper::get_integer_value( $input['form_id'] ?? 0 ); |
| 102 | |
| 103 | // If a specific form is requested, validate it exists. |
| 104 | $form_title = ''; |
| 105 | $form_status = ''; |
| 106 | |
| 107 | if ( $form_id > 0 ) { |
| 108 | $post = get_post( $form_id ); |
| 109 | |
| 110 | if ( ! $post || SRFM_FORMS_POST_TYPE !== $post->post_type ) { |
| 111 | return new \WP_Error( |
| 112 | 'srfm_form_not_found', |
| 113 | __( 'Form not found.', 'sureforms' ), |
| 114 | [ 'status' => 404 ] |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | $form_title = $post->post_title; |
| 119 | $form_status = $post->post_status; |
| 120 | } |
| 121 | |
| 122 | $entries_table = EntriesTable::get_instance(); |
| 123 | |
| 124 | // Build form_id condition. |
| 125 | $form_condition = []; |
| 126 | if ( $form_id > 0 ) { |
| 127 | $form_condition = [ |
| 128 | [ |
| 129 | 'key' => 'form_id', |
| 130 | 'compare' => '=', |
| 131 | 'value' => $form_id, |
| 132 | ], |
| 133 | ]; |
| 134 | } |
| 135 | |
| 136 | // Total entries (excluding trash). |
| 137 | $total_where = []; |
| 138 | $total_where[] = [ |
| 139 | [ |
| 140 | 'key' => 'status', |
| 141 | 'compare' => '!=', |
| 142 | 'value' => 'trash', |
| 143 | ], |
| 144 | ]; |
| 145 | if ( ! empty( $form_condition ) ) { |
| 146 | $total_where[] = $form_condition; |
| 147 | } |
| 148 | $total_entries = $entries_table->get_total_count( $total_where ); |
| 149 | |
| 150 | // Unread count. |
| 151 | $unread_where = []; |
| 152 | $unread_where[] = [ |
| 153 | [ |
| 154 | 'key' => 'status', |
| 155 | 'compare' => '=', |
| 156 | 'value' => 'unread', |
| 157 | ], |
| 158 | ]; |
| 159 | if ( ! empty( $form_condition ) ) { |
| 160 | $unread_where[] = $form_condition; |
| 161 | } |
| 162 | $unread_count = $entries_table->get_total_count( $unread_where ); |
| 163 | |
| 164 | // Read count. |
| 165 | $read_where = []; |
| 166 | $read_where[] = [ |
| 167 | [ |
| 168 | 'key' => 'status', |
| 169 | 'compare' => '=', |
| 170 | 'value' => 'read', |
| 171 | ], |
| 172 | ]; |
| 173 | if ( ! empty( $form_condition ) ) { |
| 174 | $read_where[] = $form_condition; |
| 175 | } |
| 176 | $read_count = $entries_table->get_total_count( $read_where ); |
| 177 | |
| 178 | // Trash count. |
| 179 | $trash_where = []; |
| 180 | $trash_where[] = [ |
| 181 | [ |
| 182 | 'key' => 'status', |
| 183 | 'compare' => '=', |
| 184 | 'value' => 'trash', |
| 185 | ], |
| 186 | ]; |
| 187 | if ( ! empty( $form_condition ) ) { |
| 188 | $trash_where[] = $form_condition; |
| 189 | } |
| 190 | $trash_count = $entries_table->get_total_count( $trash_where ); |
| 191 | |
| 192 | return [ |
| 193 | 'form_id' => $form_id, |
| 194 | 'form_title' => $form_title, |
| 195 | 'form_status' => $form_status, |
| 196 | 'total_entries' => $total_entries, |
| 197 | 'unread_count' => $unread_count, |
| 198 | 'read_count' => $read_count, |
| 199 | 'trash_count' => $trash_count, |
| 200 | ]; |
| 201 | } |
| 202 | } |
| 203 |