| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bulk Get Entries Ability. |
| 4 |
* |
| 5 |
* @package sureforms |
| 6 |
* @since 2.5.2 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\Inc\Abilities\Entries; |
| 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 |
* Bulk_Get_Entries ability class. |
| 21 |
* |
| 22 |
* Retrieves detailed information about multiple form submission entries |
| 23 |
* in a single call, including parsed form data with decoded labels. |
| 24 |
* |
| 25 |
* @since 2.5.2 |
| 26 |
*/ |
| 27 |
class Bulk_Get_Entries extends Abstract_Ability { |
| 28 |
use Entry_Parser; |
| 29 |
|
| 30 |
/** |
| 31 |
* Maximum number of entries that can be fetched in a single call. |
| 32 |
* |
| 33 |
* @since 2.5.2 |
| 34 |
*/ |
| 35 |
public const MAX_ENTRIES = 50; |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor. |
| 39 |
* |
| 40 |
* @since 2.5.2 |
| 41 |
*/ |
| 42 |
public function __construct() { |
| 43 |
$this->id = 'sureforms/bulk-get-entries'; |
| 44 |
$this->label = __( 'Bulk Get Entry Details', 'sureforms' ); |
| 45 |
$this->description = __( 'Retrieve detailed information about multiple SureForms form submission entries in a single call, including all submitted field data with labels.', 'sureforms' ); |
| 46 |
$this->capability = 'manage_options'; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* {@inheritDoc} |
| 51 |
* |
| 52 |
* @since 2.5.2 |
| 53 |
*/ |
| 54 |
public function get_annotations() { |
| 55 |
return [ |
| 56 |
'readonly' => true, |
| 57 |
'destructive' => false, |
| 58 |
'idempotent' => true, |
| 59 |
'priority' => 1.0, |
| 60 |
'openWorldHint' => false, |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* {@inheritDoc} |
| 66 |
* |
| 67 |
* @since 2.5.2 |
| 68 |
*/ |
| 69 |
public function get_input_schema() { |
| 70 |
return [ |
| 71 |
'type' => 'object', |
| 72 |
'additionalProperties' => false, |
| 73 |
'properties' => [ |
| 74 |
'entry_ids' => [ |
| 75 |
'type' => 'array', |
| 76 |
'items' => [ 'type' => 'integer' ], |
| 77 |
'description' => __( 'Array of entry IDs to retrieve (max 50).', 'sureforms' ), |
| 78 |
], |
| 79 |
], |
| 80 |
'required' => [ 'entry_ids' ], |
| 81 |
]; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* {@inheritDoc} |
| 86 |
* |
| 87 |
* @since 2.5.2 |
| 88 |
*/ |
| 89 |
public function get_output_schema() { |
| 90 |
return [ |
| 91 |
'type' => 'object', |
| 92 |
'properties' => [ |
| 93 |
'entries' => [ |
| 94 |
'type' => 'array', |
| 95 |
'items' => [ |
| 96 |
'type' => 'object', |
| 97 |
'properties' => [ |
| 98 |
'id' => [ 'type' => 'integer' ], |
| 99 |
'form_id' => [ 'type' => 'integer' ], |
| 100 |
'form_name' => [ 'type' => 'string' ], |
| 101 |
'status' => [ 'type' => 'string' ], |
| 102 |
'created_at' => [ 'type' => 'string' ], |
| 103 |
'form_data' => [ |
| 104 |
'type' => 'array', |
| 105 |
'items' => [ |
| 106 |
'type' => 'object', |
| 107 |
'properties' => [ |
| 108 |
'label' => [ 'type' => 'string' ], |
| 109 |
'value' => [], |
| 110 |
'block_name' => [ 'type' => 'string' ], |
| 111 |
], |
| 112 |
], |
| 113 |
], |
| 114 |
'submission_info' => [ 'type' => 'object' ], |
| 115 |
'user' => [ 'type' => [ 'object', 'null' ] ], |
| 116 |
], |
| 117 |
], |
| 118 |
], |
| 119 |
'errors' => [ |
| 120 |
'type' => 'array', |
| 121 |
'items' => [ |
| 122 |
'type' => 'object', |
| 123 |
'properties' => [ |
| 124 |
'entry_id' => [ 'type' => 'integer' ], |
| 125 |
'message' => [ 'type' => 'string' ], |
| 126 |
], |
| 127 |
], |
| 128 |
], |
| 129 |
], |
| 130 |
]; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Execute the bulk-get-entries ability. |
| 135 |
* |
| 136 |
* @param array<string,mixed> $input Validated input data. |
| 137 |
* @since 2.5.2 |
| 138 |
* @return array<string,mixed>|\WP_Error |
| 139 |
*/ |
| 140 |
public function execute( $input ) { |
| 141 |
$entry_ids = $input['entry_ids'] ?? []; |
| 142 |
|
| 143 |
if ( ! is_array( $entry_ids ) || empty( $entry_ids ) ) { |
| 144 |
return new \WP_Error( |
| 145 |
'srfm_invalid_entry_ids', |
| 146 |
__( 'entry_ids must be a non-empty array.', 'sureforms' ), |
| 147 |
[ 'status' => 400 ] |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
if ( count( $entry_ids ) > self::MAX_ENTRIES ) { |
| 152 |
return new \WP_Error( |
| 153 |
'srfm_too_many_entry_ids', |
| 154 |
/* translators: %d is the maximum number of entries allowed. */ |
| 155 |
sprintf( __( 'Maximum %d entry IDs allowed per request.', 'sureforms' ), self::MAX_ENTRIES ), |
| 156 |
[ 'status' => 400 ] |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
$entry_ids = array_map( 'absint', $entry_ids ); |
| 161 |
|
| 162 |
$entries = []; |
| 163 |
$errors = []; |
| 164 |
|
| 165 |
foreach ( $entry_ids as $entry_id ) { |
| 166 |
$entry_id = Helper::get_integer_value( $entry_id ); |
| 167 |
$entry = EntriesTable::get( $entry_id ); |
| 168 |
|
| 169 |
if ( empty( $entry ) ) { |
| 170 |
$errors[] = [ |
| 171 |
'entry_id' => $entry_id, |
| 172 |
'message' => __( 'Entry not found.', 'sureforms' ), |
| 173 |
]; |
| 174 |
continue; |
| 175 |
} |
| 176 |
|
| 177 |
$parsed = $this->parse_entry( $entry ); |
| 178 |
$entries[] = array_merge( [ 'id' => $entry_id ], $parsed ); |
| 179 |
} |
| 180 |
|
| 181 |
return [ |
| 182 |
'entries' => $entries, |
| 183 |
'errors' => $errors, |
| 184 |
]; |
| 185 |
} |
| 186 |
} |
| 187 |
|