'GET', 'callback' => array( $this, 'summary' ), 'permission_callback' => array( $this, 'can_import' ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'import' ), 'permission_callback' => array( $this, 'can_import' ), 'args' => array( 'offset' => array( 'type' => 'integer', 'required' => false, 'default' => 0, 'sanitize_callback' => 'absint', ), ), ), ) ); register_rest_route( Router::NAMESPACE, '/folder-imports/wp-media-folder', array( array( 'methods' => 'GET', 'callback' => array( $this, 'wp_media_folder_summary' ), 'permission_callback' => array( $this, 'can_import' ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'wp_media_folder_import' ), 'permission_callback' => array( $this, 'can_import' ), 'args' => array( 'offset' => array( 'type' => 'integer', 'required' => false, 'default' => 0, 'sanitize_callback' => 'absint', ), ), ), ) ); register_rest_route( Router::NAMESPACE, '/folder-imports/filebird', array( array( 'methods' => 'GET', 'callback' => array( $this, 'filebird_summary' ), 'permission_callback' => array( $this, 'can_import' ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'filebird_import' ), 'permission_callback' => array( $this, 'can_import' ), 'args' => array( 'offset' => array( 'type' => 'integer', 'required' => false, 'default' => 0, 'sanitize_callback' => 'absint', ), ), ), ) ); register_rest_route( Router::NAMESPACE, '/folder-imports/real-media-library', array( array( 'methods' => 'GET', 'callback' => array( $this, 'real_media_summary' ), 'permission_callback' => array( $this, 'can_import' ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'real_media_import' ), 'permission_callback' => array( $this, 'can_import' ), 'args' => array( 'offset' => array( 'type' => 'integer', 'required' => false, 'default' => 0, 'sanitize_callback' => 'absint', ), ), ), ) ); } public function can_import(): bool { return $this->can_structure_folders(); } public function summary(): WP_REST_Response|WP_Error { if ( $denied = $this->gate() ) { return $denied; } $summary = ( new HappyFilesImport() )->summary(); return new WP_REST_Response( array( 'source' => 'happyfiles', 'available' => $summary['available'], 'folders' => $summary['folders'], 'files' => $summary['files'], ) ); } public function import( WP_REST_Request $request ): WP_REST_Response|WP_Error { if ( $denied = $this->gate() ) { return $denied; } try { $result = ( new HappyFilesImport() )->import_batch( (int) $request->get_param( 'offset' ) ); } catch ( \Throwable $e ) { return $this->exception_error( 'import_failed', $e, __( 'Could not import the folders. Please try again.', 'foldrocket' ), 500, true ); } return new WP_REST_Response( $result ); } public function real_media_summary(): WP_REST_Response|WP_Error { if ( $denied = $this->gate() ) { return $denied; } $summary = ( new RealMediaLibraryImport() )->summary(); return new WP_REST_Response( array( 'source' => 'real-media-library', 'available' => $summary['available'], 'folders' => $summary['folders'], 'files' => $summary['files'], ) ); } public function real_media_import( WP_REST_Request $request ): WP_REST_Response|WP_Error { if ( $denied = $this->gate() ) { return $denied; } try { $result = ( new RealMediaLibraryImport() )->import_batch( (int) $request->get_param( 'offset' ) ); } catch ( \Throwable $e ) { return $this->exception_error( 'import_failed', $e, __( 'Could not import the folders. Please try again.', 'foldrocket' ), 500, true ); } return new WP_REST_Response( $result ); } public function filebird_summary(): WP_REST_Response|WP_Error { if ( $denied = $this->gate() ) { return $denied; } $summary = ( new FileBirdImport() )->summary(); return new WP_REST_Response( array( 'source' => 'filebird', 'available' => $summary['available'], 'folders' => $summary['folders'], 'files' => $summary['files'], ) ); } public function filebird_import( WP_REST_Request $request ): WP_REST_Response|WP_Error { if ( $denied = $this->gate() ) { return $denied; } try { $result = ( new FileBirdImport() )->import_batch( (int) $request->get_param( 'offset' ) ); } catch ( \Throwable $e ) { return $this->exception_error( 'import_failed', $e, __( 'Could not import the folders. Please try again.', 'foldrocket' ), 500, true ); } return new WP_REST_Response( $result ); } public function wp_media_folder_summary(): WP_REST_Response|WP_Error { if ( $denied = $this->gate() ) { return $denied; } $summary = ( new WpMediaFolderImport() )->summary(); return new WP_REST_Response( array( 'source' => 'wp-media-folder', 'available' => $summary['available'], 'folders' => $summary['folders'], 'files' => $summary['files'], ) ); } public function wp_media_folder_import( WP_REST_Request $request ): WP_REST_Response|WP_Error { if ( $denied = $this->gate() ) { return $denied; } try { $result = ( new WpMediaFolderImport() )->import_batch( (int) $request->get_param( 'offset' ) ); } catch ( \Throwable $e ) { return $this->exception_error( 'import_failed', $e, __( 'Could not import the folders. Please try again.', 'foldrocket' ), 500, true ); } return new WP_REST_Response( $result ); } private function gate(): ?WP_Error { if ( $denied = $this->require_feature( 'folders' ) ) { return $denied; } return $this->require_feature( 'folder_import' ); } }