prefix . 'abientot_' . $this->table_suffix; } /** * Finds a row by ID. * * @param int $id Row ID. * @return array|null */ public function find( int $id ): ?array { global $wpdb; if ( $id <= 0 ) { return null; } $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $this->table(), $id ), ARRAY_A ); return is_array( $row ) ? $row : null; } /** * Inserts a row and returns its ID. * * @param array $data Row data. */ public function insert( array $data ): int { global $wpdb; $wpdb->insert( $this->table(), $data ); return (int) $wpdb->insert_id; } /** * Updates a row by ID. * * @param int $id Row ID. * @param array $data Row data. */ public function update( int $id, array $data ): bool { global $wpdb; if ( $id <= 0 ) { return false; } return false !== $wpdb->update( $this->table(), $data, array( 'id' => $id ), null, array( '%d' ) ); } /** * Deletes a row by ID. * * @param int $id Row ID. */ public function delete( int $id ): bool { global $wpdb; if ( $id <= 0 ) { return false; } return false !== $wpdb->delete( $this->table(), array( 'id' => $id ), array( '%d' ) ); } /** * Returns rows with simple pagination. * * @param int $limit Result limit. * @param int $offset Result offset. * @return array> */ public function all( int $limit = 100, int $offset = 0 ): array { global $wpdb; $limit = max( 1, min( 500, $limit ) ); $offset = max( 0, $offset ); $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i ORDER BY id DESC LIMIT %d OFFSET %d', $this->table(), $limit, $offset ), ARRAY_A ); return is_array( $rows ) ? $rows : array(); } /** * Finds the first row matching a column/value pair. * * @param string $column Column name. * @param mixed $value Column value. * @return array|null */ protected function find_by( string $column, $value ): ?array { global $wpdb; if ( ! in_array( $column, $this->allowed_lookup_columns(), true ) ) { return null; } $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE %i = %s LIMIT 1', $this->table(), $column, (string) $value ), ARRAY_A ); return is_array( $row ) ? $row : null; } /** * Lookup columns accepted for dynamic repository helpers. * * @return string[] */ protected function allowed_lookup_columns(): array { return array( 'id', 'slug', 'email', 'phone', 'is_default', 'status' ); } }