helper_adapter = $helper_adapter; $this->option_adapter = $option_adapter; } /** * Registers the REST API routes for the BackWPup plugin. * * This method is responsible for defining the routes that the plugin * exposes via the WordPress REST API. Each route should be registered * with its corresponding callback and permissions. * * @return void */ public function register_routes(): void { register_rest_route( 'backwpup/v1', '/storagelistcompact', [ 'methods' => 'GET', 'callback' => [ $this, 'get_storage_list_compact' ], 'permission_callback' => [ $this, 'has_permission' ], ] ); } /** * Checks if the current user has the necessary permissions to perform the action. * * @return bool True if the user has permission, false otherwise. */ public function has_permission(): bool { return current_user_can( 'backwpup' ); } /** * Get the storage list compact. * * @return WP_HTTP_Response * @throws Exception If there is not jobs sets. */ public function get_storage_list_compact() { $status = 200; try { $first_job_id = get_site_option( Plugin::FIRST_JOB_ID, false ); if ( false === $first_job_id ) { throw new Exception( __( 'No backup jobs set.', 'backwpup' ) ); } $storage_destination = $this->option_adapter->get( $first_job_id, 'destinations', [] ); $html = $this->helper_adapter->component( 'storage-list-compact', [ 'storages' => $storage_destination ] ); } catch ( Exception $e ) { $status = 500; $html = $this->helper_adapter->component( 'alerts/info', [ 'type' => 'alert', 'font' => 'xs', "content" => __($e->getMessage(), 'backwpup'), //phpcs:ignore ] ); } $response = new WP_HTTP_Response( $html, $status, [ 'Content-Type' => 'text/html' ] ); return rest_ensure_response( $response ); } }