> */ public function all(): array { global $wpdb; $table = TableNames::assert_plugin_table( TableNames::collections() ); $site_id = TableNames::site_id(); $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE site_id = %d ORDER BY is_smart DESC, name ASC', $table, $site_id ), ARRAY_A ) ?: array(); $items = array_map( array( $this, 'hydrate' ), $rows ); return array_map( array( $this, 'with_count' ), $items ); } /** * @param array $item * @return array */ private function with_count( array $item ): array { $filters = isset( $item['filters'] ) && is_array( $item['filters'] ) ? $item['filters'] : array(); $result = ( new \FoldRocket\Domain\Assets\AssetRepository() )->list( array_merge( $filters, array( 'page' => 1, 'per_page' => 1, ) ) ); $item['count'] = (int) ( $result['total'] ?? 0 ); return $item; } public function delete( int $id ): bool { global $wpdb; $existing = $this->find( $id ); if ( ! $existing ) { return false; } $wpdb->delete( TableNames::collections(), array( 'id' => $id, 'site_id' => TableNames::site_id(), ) ); return true; } /** * @param array{name: string, filters?: array, isSmart?: bool} $data * @return array */ public function create( array $data ): array { global $wpdb; $name = sanitize_text_field( $data['name'] ); $filters = isset( $data['filters'] ) && is_array( $data['filters'] ) ? $data['filters'] : array(); $smart = array_key_exists( 'isSmart', $data ) ? (bool) $data['isSmart'] : false; $now = current_time( 'mysql' ); $wpdb->insert( TableNames::collections(), array( 'site_id' => TableNames::site_id(), 'name' => $name, 'filters' => wp_json_encode( $filters ), 'is_smart' => $smart ? 1 : 0, 'created_at' => $now, 'updated_at' => $now, ) ); return $this->find( (int) $wpdb->insert_id ) ?? array(); } /** * @param array{name?: string, filters?: array, isSmart?: bool} $data * @return array|null */ public function update( int $id, array $data ): ?array { global $wpdb; $existing = $this->find( $id ); if ( ! $existing ) { return null; } $update = array( 'updated_at' => current_time( 'mysql' ) ); if ( isset( $data['name'] ) ) { $update['name'] = sanitize_text_field( (string) $data['name'] ); } if ( isset( $data['filters'] ) && is_array( $data['filters'] ) ) { $update['filters'] = wp_json_encode( $data['filters'] ); } if ( array_key_exists( 'isSmart', $data ) ) { $update['is_smart'] = $data['isSmart'] ? 1 : 0; } $wpdb->update( TableNames::collections(), $update, array( 'id' => $id ) ); return $this->find( $id ); } /** @return array|null */ public function find( int $id ): ?array { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d AND site_id = %d', TableNames::assert_plugin_table( TableNames::collections() ), $id, TableNames::site_id() ), ARRAY_A ); return $row ? $this->with_count( $this->hydrate( $row ) ) : null; } /** * Seed useful default smart collections once. */ public function ensure_defaults(): void { global $wpdb; $exists = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE site_id = %d', TableNames::assert_plugin_table( TableNames::collections() ), TableNames::site_id() ) ); if ( $exists > 0 ) { return; } $defaults = array( array( 'name' => __( 'Unused media', 'foldrocket' ), 'filters' => array( 'usage_max' => 0, 'indexed_only' => true ), ), array( 'name' => __( 'Missing ALT', 'foldrocket' ), 'filters' => array( 'missing_alt' => true ), ), array( 'name' => __( 'Analyzed by AI', 'foldrocket' ), 'filters' => array( 'ai_status' => 'analyzed' ), ), array( 'name' => sprintf( /* translators: %d: size threshold in megabytes */ __( 'Large files (>%dMB)', 'foldrocket' ), PluginSettings::large_file_mb() ), 'filters' => array( 'min_size' => PluginSettings::large_file_min_bytes() ), ), ); foreach ( $defaults as $item ) { $this->create( array( 'name' => $item['name'], 'filters' => $item['filters'], 'isSmart' => true, ) ); } } /** * Drop legacy default “Exact duplicates” smart view (duplicates live on their own page). */ public function purge_legacy_exact_duplicates_view(): void { foreach ( $this->all_without_counts() as $item ) { $filters = isset( $item['filters'] ) && is_array( $item['filters'] ) ? $item['filters'] : array(); if ( ( $filters['duplicate_status'] ?? '' ) === 'exact' && ! empty( $item['isSmart'] ) ) { $this->delete( (int) $item['id'] ); } } } /** * @return list> */ private function all_without_counts(): array { global $wpdb; $table = TableNames::assert_plugin_table( TableNames::collections() ); $site_id = TableNames::site_id(); $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE site_id = %d ORDER BY is_smart DESC, name ASC', $table, $site_id ), ARRAY_A ) ?: array(); return array_map( array( $this, 'hydrate' ), $rows ); } /** * @param array $row * @return array */ private function hydrate( array $row ): array { $filters = json_decode( (string) $row['filters'], true ); $filters = is_array( $filters ) ? $filters : array(); $is_smart = (bool) $row['is_smart']; $name = (string) $row['name']; // Keep large-file smart views aligned with Settings → large_file_mb. if ( $is_smart && isset( $filters['min_size'] ) ) { $mb = PluginSettings::large_file_mb(); $filters['min_size'] = PluginSettings::large_file_min_bytes(); $name = sprintf( /* translators: %d: size threshold in megabytes */ __( 'Large files (>%dMB)', 'foldrocket' ), $mb ); } return array( 'id' => (int) $row['id'], 'name' => $name, 'filters' => $filters, 'isSmart' => $is_smart, 'createdAt' => (string) $row['created_at'], 'updatedAt' => (string) $row['updated_at'], ); } }