can_structure_folders() ) { return true; } if ( ! $this->can_manage() || ! $request->has_param( 'color' ) ) { return false; } foreach ( array( 'name', 'parentId', 'sortOrder' ) as $key ) { if ( $request->has_param( $key ) ) { return false; } } return true; } /** * Full job administration remains site-wide and administrator-only. */ public function can_manage_jobs(): bool { return current_user_can( 'manage_options' ); } public function can_edit_requested_attachment( WP_REST_Request $request ): bool { $id = (int) $request->get_param( 'id' ); return $id > 0 && 'attachment' === get_post_type( $id ) && current_user_can( 'edit_post', $id ); } /** * Library mutation on one attachment: general library access plus ownership. */ public function can_manage_requested_attachment( WP_REST_Request $request ): bool { return $this->can_manage() && $this->can_edit_requested_attachment( $request ); } /** * AI mutation on one attachment: AI access plus ownership. */ public function can_run_ai_on_requested_attachment( WP_REST_Request $request ): bool { return $this->can_run_ai() && $this->can_edit_requested_attachment( $request ); } /** * Assign or detach only attachments this user is allowed to edit. */ public function can_edit_requested_media( WP_REST_Request $request ): bool { if ( ! $this->can_manage() ) { return false; } $attachment_ids = $request->get_param( 'attachmentIds' ); $ids = array(); if ( is_array( $attachment_ids ) ) { foreach ( $attachment_ids as $attachment_id ) { $attachment_id = (int) $attachment_id; if ( $attachment_id > 0 ) { $ids[] = $attachment_id; } } } $asset_ids = $request->get_param( 'assetIds' ); if ( is_array( $asset_ids ) && $asset_ids ) { $wanted = array(); foreach ( $asset_ids as $asset_id ) { $asset_id = (int) $asset_id; if ( $asset_id > 0 ) { $wanted[] = $asset_id; } } $resolved = $this->attachment_ids_for_assets( $wanted ); if ( count( $resolved ) !== count( $wanted ) ) { return false; } $ids = array_merge( $ids, $resolved ); } if ( ! $ids ) { return false; } foreach ( $ids as $attachment_id ) { if ( ! current_user_can( 'edit_post', $attachment_id ) ) { return false; } } return true; } /** * @param list $asset_ids * @return list */ private function attachment_ids_for_assets( array $asset_ids ): array { global $wpdb; $asset_ids = array_values( array_unique( array_filter( array_map( 'intval', $asset_ids ) ) ) ); if ( ! $asset_ids ) { return array(); } $table = \FoldRocket\Infrastructure\Database\TableNames::assert_plugin_table( \FoldRocket\Infrastructure\Database\TableNames::assets() ); $placeholders = implode( ', ', array_fill( 0, count( $asset_ids ), '%d' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Placeholder count matches the id list. $sql = "SELECT attachment_id FROM %i WHERE id IN ( {$placeholders} )"; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- Custom asset table; placeholder count matches the id list. $rows = $wpdb->get_col( $wpdb->prepare( $sql, array_merge( array( $table ), $asset_ids ) ) ); $out = array(); foreach ( (array) $rows as $attachment_id ) { $attachment_id = (int) $attachment_id; if ( $attachment_id > 0 ) { $out[] = $attachment_id; } } return $out; } public function can_upload(): bool { return current_user_can( 'upload_files' ); } public function can_run_ai(): bool { return current_user_can( 'frkt_run_ai' ) || current_user_can( 'frkt_run_ai' ) || current_user_can( 'manage_options' ); } /** * AI work on a set of attachments: AI capability plus edit_post on each id. */ public function can_run_ai_on_requested_attachments( WP_REST_Request $request ): bool { if ( ! $this->can_run_ai() ) { return false; } $ids = $request->get_param( 'attachmentIds' ); if ( ! is_array( $ids ) || ! $ids ) { return false; } foreach ( $ids as $id ) { $id = (int) $id; if ( $id < 1 || ! current_user_can( 'edit_post', $id ) ) { return false; } } return true; } /** * Applying bulk ALT writes metadata. Require AI access and edit_post on every item. */ public function can_apply_bulk_alt( WP_REST_Request $request ): bool { if ( ! $this->can_run_ai() ) { return false; } $items = $request->get_param( 'items' ); if ( ! is_array( $items ) || ! $items ) { return false; } foreach ( $items as $item ) { if ( ! is_array( $item ) ) { return false; } $id = (int) ( $item['attachmentId'] ?? $item['attachment_id'] ?? 0 ); if ( $id < 1 || ! current_user_can( 'edit_post', $id ) ) { return false; } } return true; } /** * Deleting media requires delete access on every concrete attachment. * Missing attachments may still be pruned from the plugin index by users * with the general delete capability. */ public function can_delete_requested_attachments( WP_REST_Request $request ): bool { if ( ! $this->can_delete() ) { return false; } $ids = $request->get_param( 'attachmentIds' ); if ( ! is_array( $ids ) || ! $ids ) { return false; } foreach ( $ids as $id ) { $id = (int) $id; if ( $id < 1 ) { return false; } if ( get_post( $id ) && ! current_user_can( 'delete_post', $id ) ) { return false; } } return true; } /** * License endpoints only — settings cap without requiring an active license. */ public function can_license(): bool { return current_user_can( 'frkt_manage_settings' ) || current_user_can( 'frkt_manage_settings' ) || current_user_can( 'manage_options' ); } public function can_settings(): bool { return $this->can_license(); } public function can_delete(): bool { return current_user_can( 'frkt_delete_media' ) || current_user_can( 'frkt_delete_media' ) || current_user_can( 'delete_posts' ); } public function is_licensed(): bool { if ( ! class_exists( \FoldRocket\Commercial\LicenseBridge::class ) ) { return false; } return \FoldRocket\Commercial\LicenseBridge::is_active(); } /** * Soft license check — prefer require_feature() for Pro capabilities. */ public function require_license(): ?WP_Error { if ( $this->is_licensed() ) { return null; } return $this->error( 'license_required', __( 'Activate your FoldRocket license key to unlock Pro features.', 'foldrocket' ), 402 ); } /** * Gate REST behind Features toggles and Free/Pro entitlement. */ public function require_feature( string $feature_id ): ?WP_Error { if ( FeatureRegistry::is_available( $feature_id ) ) { return null; } if ( ! FeatureRegistry::is_entitled( $feature_id ) ) { if ( class_exists( \FoldRocket\Commercial\ProFeatureError::class ) ) { return \FoldRocket\Commercial\ProFeatureError::make( $feature_id ); } return $this->error( 'feature_disabled', sprintf( /* translators: %s: feature id */ __( 'The “%s” feature is disabled in Features settings.', 'foldrocket' ), $feature_id ), 403 ); } return $this->error( 'feature_disabled', sprintf( /* translators: %s: feature id */ __( 'The “%s” feature is disabled in Features settings.', 'foldrocket' ), $feature_id ), 403 ); } /** * @param array $details */ public function error( string $code, string $message, int $status = 400, array $details = array(), bool $retryable = false ): WP_Error { return new WP_Error( $code, $message, array( 'status' => $status, 'details' => $details, 'retryable' => $retryable, ) ); } /** * Prefer validation messages; hide internal/provider details from the REST client. */ public function exception_error( string $code, \Throwable $e, string $fallback, int $status = 500, bool $retryable = false ): WP_Error { $message = $e instanceof \InvalidArgumentException ? $e->getMessage() : $fallback; if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG && ! ( $e instanceof \InvalidArgumentException ) ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( sprintf( '[FoldRocket] %s: %s', $code, $e->getMessage() ) ); } return $this->error( $code, $message, $status, array(), $retryable ); } public function int_param( WP_REST_Request $request, string $key, int $default = 0 ): int { return (int) $request->get_param( $key ) ?: $default; } }