db->prepare( 'SELECT * FROM %i WHERE `key` = %s AND `channel` = %s AND `locale` = %s LIMIT 1', $this->table( 'notification_templates' ), $key, $channel, $locale ); $row = is_string( $sql ) ? $this->db->get_row( $sql, ARRAY_A ) : null; return is_array( $row ) ? $this->hydrate( $row ) : null; } /** * {@inheritDoc} * * @param NotificationTemplate $template Template. */ public function save( NotificationTemplate $template ): void { $data = [ 'uuid' => $template->uuid()->to_string(), 'key' => $template->key(), 'channel' => $template->channel(), 'recipient' => $template->recipient()->value, 'custom_recipient' => $template->custom_recipient(), 'subject' => $template->subject(), 'body' => $template->body(), 'is_enabled' => $template->is_enabled() ? 1 : 0, 'locale' => $template->locale(), 'created_at' => $this->format_datetime( $template->created_at() ), 'updated_at' => $this->format_datetime( $template->updated_at() ), ]; if ( null === $template->id() ) { $this->db->insert( $this->table( 'notification_templates' ), $data ); $template->assign_id( $this->last_insert_id() ); return; } $this->db->update( $this->table( 'notification_templates' ), $data, [ 'id' => $template->id() ] ); } /** * {@inheritDoc} * * @param int $id Id. */ public function find_by_id( int $id ): ?NotificationTemplate { $sql = $this->db->prepare( 'SELECT * FROM %i WHERE `id` = %d LIMIT 1', $this->table( 'notification_templates' ), $id ); $row = is_string( $sql ) ? $this->db->get_row( $sql, ARRAY_A ) : null; return is_array( $row ) ? $this->hydrate( $row ) : null; } /** * {@inheritDoc} * * @return list */ public function all(): array { $table = $this->table( 'notification_templates' ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $rows = $this->db->get_results( "SELECT * FROM `{$table}` ORDER BY `key` ASC", ARRAY_A ); if ( ! is_array( $rows ) ) { return []; } $out = []; foreach ( $rows as $row ) { if ( is_array( $row ) ) { $out[] = $this->hydrate( $row ); } } return $out; } /** * Hydrate. * * @param array $row Row. */ private function hydrate( array $row ): NotificationTemplate { return new NotificationTemplate( (int) $row['id'], new Uuid( (string) $row['uuid'] ), (string) $row['key'], (string) $row['channel'], RecipientRole::from( (string) $row['recipient'] ), ( is_string( $row['custom_recipient'] ?? null ) ? (string) $row['custom_recipient'] : null ), ( is_string( $row['subject'] ?? null ) ? (string) $row['subject'] : null ), ( is_string( $row['body'] ?? null ) ? (string) $row['body'] : null ), (bool) $row['is_enabled'], (string) $row['locale'], $this->parse_datetime( (string) $row['created_at'] ), $this->parse_datetime( (string) $row['updated_at'] ), ); } }