class-adbc-automation-endpoints.php
3 months ago
class-adbc-common-endpoints.php
4 months ago
class-adbc-cron-jobs-endpoints.php
3 months ago
class-adbc-general-cleanup-endpoints.php
3 months ago
class-adbc-info-endpoints.php
3 months ago
class-adbc-logs-endpoints.php
3 months ago
class-adbc-options-endpoints.php
3 months ago
class-adbc-post-types-endpoints.php
3 months ago
class-adbc-posts-meta-endpoints.php
3 months ago
class-adbc-settings-endpoints.php
3 months ago
class-adbc-tables-endpoints.php
3 months ago
class-adbc-transients-endpoints.php
3 months ago
class-adbc-users-meta-endpoints.php
3 months ago
class-adbc-automation-endpoints.php
282 lines
| 1 | <?php |
| 2 | // Exit if accessed directly. |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * ADBC_Automation_Endpoints class. |
| 8 | * |
| 9 | * This class defines the REST API endpoints for managing automation tasks. |
| 10 | */ |
| 11 | class ADBC_Automation_Endpoints { |
| 12 | |
| 13 | /** |
| 14 | * Get the list of all automation tasks. |
| 15 | * |
| 16 | * @return WP_REST_Response |
| 17 | */ |
| 18 | public static function list_tasks( WP_REST_Request $request ) { |
| 19 | |
| 20 | try { |
| 21 | |
| 22 | // Ensure the integrity of automation tasks only here because it will be always called before the other endpoints |
| 23 | ADBC_Admin_Init::ensure_automation_integrity(); |
| 24 | |
| 25 | $tasks = ADBC_Automation::instance()->tasks(); |
| 26 | |
| 27 | $status = sanitize_key( $request->get_param( 'status' ) ); |
| 28 | if ( ! in_array( $status, [ 'all', 'active', 'paused' ], true ) ) { |
| 29 | $status = 'all'; |
| 30 | } |
| 31 | |
| 32 | $all_count = count( $tasks ); |
| 33 | $active_count = count( array_filter( $tasks, function ($task) { |
| 34 | return $task['active'] === true; |
| 35 | } ) ); |
| 36 | $paused_count = $all_count - $active_count; |
| 37 | |
| 38 | $items = $tasks; |
| 39 | if ( $status === 'active' ) { |
| 40 | $items = array_filter( $tasks, function ($task) { |
| 41 | return $task['active'] === true; |
| 42 | } ); |
| 43 | } elseif ( $status === 'paused' ) { |
| 44 | $items = array_filter( $tasks, function ($task) { |
| 45 | return $task['active'] === false; |
| 46 | } ); |
| 47 | } |
| 48 | |
| 49 | return ADBC_Rest::success( '', [ |
| 50 | 'items' => $items, |
| 51 | 'counts' => [ |
| 52 | 'total' => $all_count, |
| 53 | 'active' => $active_count, |
| 54 | 'paused' => $paused_count, |
| 55 | ], |
| 56 | ] ); |
| 57 | |
| 58 | } catch (Throwable $e) { |
| 59 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Create a new automation task using the provided details, returns the ID of the created task. |
| 66 | * |
| 67 | * @param WP_REST_Request $request |
| 68 | * |
| 69 | * @return WP_REST_Response |
| 70 | */ |
| 71 | public static function create_task( WP_REST_Request $request ) { |
| 72 | |
| 73 | try { |
| 74 | |
| 75 | $task_details = $request->get_json_params(); |
| 76 | |
| 77 | if ( ADBC_VERSION_TYPE === 'FREE' && is_array( $task_details ) && isset( $task_details['operations'] ) && is_array( $task_details['operations'] ) ) { |
| 78 | foreach ( $task_details['operations'] as $items_type => $keep_last_config ) { |
| 79 | if ( ( $keep_last_config['type'] ?? null ) === 'items' ) { |
| 80 | return ADBC_Rest::error( |
| 81 | __( 'Cannot use retention by items in free version, please upgrade to premium.' ), |
| 82 | ADBC_Rest::BAD_REQUEST, |
| 83 | 0, |
| 84 | [ |
| 85 | "message_links" => [ |
| 86 | [ |
| 87 | "text" => __( 'Upgrade Now', 'advanced-database-cleaner' ), |
| 88 | "url" => "https://sigmaplugin.com/downloads/wordpress-advanced-database-cleaner/?utm_source=adbc_notification_msg", |
| 89 | "target" => "_blank", |
| 90 | ] |
| 91 | ] |
| 92 | ] |
| 93 | ); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | $is_valid = ADBC_Automation_Validator::validate_task_structure( $task_details ); |
| 99 | if ( $is_valid === false ) { |
| 100 | return ADBC_Rest::error( "Invalid task structure", ADBC_Rest::BAD_REQUEST ); |
| 101 | } |
| 102 | |
| 103 | $number_of_tasks = count( ADBC_Automation::instance()->tasks() ); |
| 104 | if ( ADBC_VERSION_TYPE === 'FREE' && $number_of_tasks >= 5 ) { |
| 105 | return ADBC_Rest::error( "Maximum number of tasks reached in free version. Please upgrade to premium.", ADBC_Rest::BAD_REQUEST ); |
| 106 | } |
| 107 | |
| 108 | $id = ADBC_Automation::instance()->create( $task_details ); |
| 109 | |
| 110 | if ( $id === null ) { |
| 111 | return ADBC_Rest::error( 'Failed to create task.', ADBC_Rest::INTERNAL_SERVER_ERROR ); |
| 112 | } |
| 113 | |
| 114 | return ADBC_Rest::success( '', [ 'id' => $id ] ); |
| 115 | |
| 116 | } catch (Throwable $e) { |
| 117 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Update an existing automation task using its ID and the provided details, return the updated task details. |
| 124 | * |
| 125 | * @param WP_REST_Request $request |
| 126 | * |
| 127 | * @return WP_REST_Response |
| 128 | */ |
| 129 | public static function update_task( WP_REST_Request $request ) { |
| 130 | |
| 131 | try { |
| 132 | |
| 133 | $id = sanitize_key( $request->get_param( 'id' ) ); |
| 134 | if ( empty( $id ) || ! is_string( $id ) ) { |
| 135 | return ADBC_Rest::error( 'Invalid task ID.', ADBC_Rest::BAD_REQUEST ); |
| 136 | } |
| 137 | |
| 138 | $task_details = $request->get_json_params(); |
| 139 | |
| 140 | $is_valid = ADBC_Automation_Validator::validate_task_structure( $task_details ); |
| 141 | if ( $is_valid === false ) { |
| 142 | return ADBC_Rest::error( "Invalid task structure", ADBC_Rest::BAD_REQUEST ); |
| 143 | } |
| 144 | |
| 145 | $updated_details = ADBC_Automation::instance()->update( $id, $task_details ); |
| 146 | if ( $updated_details === null ) { |
| 147 | return ADBC_Rest::error( 'Task not found or failed to update.', ADBC_Rest::NOT_FOUND ); |
| 148 | } |
| 149 | |
| 150 | return ADBC_Rest::success( '', $updated_details ); |
| 151 | |
| 152 | } catch (Throwable $e) { |
| 153 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get an automation task by its ID. |
| 160 | * |
| 161 | * @param WP_REST_Request $request |
| 162 | * |
| 163 | * @return WP_REST_Response |
| 164 | */ |
| 165 | public static function get_task( WP_REST_Request $request ) { |
| 166 | |
| 167 | try { |
| 168 | |
| 169 | $id = sanitize_key( $request->get_param( 'id' ) ); |
| 170 | if ( empty( $id ) || ! is_string( $id ) ) { |
| 171 | return ADBC_Rest::error( 'Invalid task ID.', ADBC_Rest::BAD_REQUEST ); |
| 172 | } |
| 173 | |
| 174 | $task = ADBC_Automation::instance()->get_task( $id ); |
| 175 | |
| 176 | if ( $task === null ) { |
| 177 | return ADBC_Rest::error( 'Task not found.', ADBC_Rest::NOT_FOUND ); |
| 178 | } |
| 179 | |
| 180 | return ADBC_Rest::success( '', $task ); |
| 181 | |
| 182 | } catch (Throwable $e) { |
| 183 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 184 | } |
| 185 | |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Delete an automation task using its ID. |
| 190 | * |
| 191 | * @param WP_REST_Request $request |
| 192 | * |
| 193 | * @return WP_REST_Response |
| 194 | */ |
| 195 | public static function delete_task( WP_REST_Request $request ) { |
| 196 | |
| 197 | try { |
| 198 | |
| 199 | $id = sanitize_key( $request->get_param( 'id' ) ); |
| 200 | if ( empty( $id ) || ! is_string( $id ) ) { |
| 201 | return ADBC_Rest::error( 'Invalid task ID.', ADBC_Rest::BAD_REQUEST ); |
| 202 | } |
| 203 | |
| 204 | $deleted = ADBC_Automation::instance()->delete( $id ); |
| 205 | |
| 206 | if ( $deleted === false ) { |
| 207 | return ADBC_Rest::error( 'Task not found or failed to delete.', ADBC_Rest::NOT_FOUND ); |
| 208 | } |
| 209 | |
| 210 | return ADBC_Rest::success( '', [] ); |
| 211 | |
| 212 | } catch (Throwable $e) { |
| 213 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Get the events log paginated for a specific automation task using its ID. |
| 220 | * |
| 221 | * @param WP_REST_Request $request |
| 222 | * |
| 223 | * @return WP_REST_Response |
| 224 | */ |
| 225 | public static function get_task_events_log( WP_REST_Request $request ) { |
| 226 | |
| 227 | try { |
| 228 | |
| 229 | $id = sanitize_key( $request->get_param( 'id' ) ); |
| 230 | if ( empty( $id ) || ! is_string( $id ) ) { |
| 231 | return ADBC_Rest::error( 'Invalid task ID.', ADBC_Rest::BAD_REQUEST ); |
| 232 | } |
| 233 | |
| 234 | $page = ADBC_Common_Validator::sanitize_validate_current_page( $request->get_param( 'currentPage' ) ); |
| 235 | $limit = ADBC_Common_Validator::sanitize_validate_limit( $request->get_param( 'itemsPerPage' ) ); |
| 236 | |
| 237 | $events = ADBC_Automation_Events_Log::get_events( $id, $page, $limit ); |
| 238 | |
| 239 | $total_pages = max( 1, ceil( $events['total_items'] / $limit ) ); |
| 240 | $real_current_page = min( $page, $total_pages ); |
| 241 | $events['real_current_page'] = $real_current_page; |
| 242 | |
| 243 | return ADBC_Rest::success( '', $events ); |
| 244 | |
| 245 | } catch (Throwable $e) { |
| 246 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Clear the events log for a specific automation task using its ID. |
| 253 | * |
| 254 | * @param WP_REST_Request $request |
| 255 | * |
| 256 | * @return WP_REST_Response |
| 257 | */ |
| 258 | public static function clear_task_events_log( WP_REST_Request $request ) { |
| 259 | |
| 260 | try { |
| 261 | |
| 262 | $id = sanitize_key( $request->get_param( 'id' ) ); |
| 263 | if ( empty( $id ) || ! is_string( $id ) ) { |
| 264 | return ADBC_Rest::error( 'Invalid task ID.', ADBC_Rest::BAD_REQUEST ); |
| 265 | } |
| 266 | |
| 267 | $ok = ADBC_Automation_Events_Log::clear_events( $id ); |
| 268 | |
| 269 | if ( ! $ok ) { |
| 270 | return ADBC_Rest::error( 'Task not found.', ADBC_Rest::NOT_FOUND ); |
| 271 | } |
| 272 | |
| 273 | return ADBC_Rest::success( '', [] ); |
| 274 | |
| 275 | } catch (Throwable $e) { |
| 276 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 277 | } |
| 278 | |
| 279 | } |
| 280 | |
| 281 | } |
| 282 |