Public/Set-GhostThemeSetting.ps1
|
function Set-GhostThemeSetting { <# .SYNOPSIS Changes custom settings of the active theme of the Ghost site. .DESCRIPTION Gives the settings that are passed their value and leaves every other setting of the theme as it is. Only a setting whose value differs from the one on the site is sent, so the command can run on every push, and only those settings are returned: nothing comes back when the site has every value already. A setting is named by its key, as the theme declares it in its package.json under config.custom. Ghost checks the value: a key that the active theme does not have and a choice that is not one of the options are refused with ValidationError. A setting of the type boolean takes $true or $false. The settings belong to the theme that is active. Activate the theme first, with Enable-GhostTheme or Sync-GhostTheme. This needs a staff access token of an owner or an administrator: connect with the token of your profile page in Ghost Admin. Ghost refuses the Admin API key of an integration here, with NoPermissionError. .EXAMPLE Set-GhostThemeSetting -Setting @{ title_font = 'Elegant serif'; show_publication_cover = $false } Changes two settings and returns the ones that had another value. .EXAMPLE Set-GhostThemeSetting -Setting (Import-PowerShellDataFile -Path ./theme-settings.psd1) -WhatIf Shows which settings of the site differ from the ones in a file. .EXAMPLE Set-GhostThemeSetting -Setting @{ google_analytics_id = '' } Clears a text setting. .LINK https://docs.ghost.org/themes/custom-settings .LINK https://docs.ghost.org/admin-api #> [CmdletBinding(SupportsShouldProcess)] [OutputType('Ghost.ThemeSetting')] param( # The settings to change: key = value. [Parameter(Mandatory, Position = 0)] [ValidateNotNull()] [System.Collections.IDictionary] $Setting ) begin { Test-GhostConnection $resources = Get-GhostAPIData -Endpoint $MyInvocation.MyCommand.Name } process { try { $uri = New-URIString -Endpoint $resources.URI $current = Submit-Request -Uri $uri -Method 'Get' $current = @(Test-ReturnFormat -Result $current -Location $resources.Result) # What differs from the site. A key that the site does not know is sent as well: Ghost says what is # wrong with it. $changes = @() foreach ($key in @($Setting.Keys | Sort-Object)) { $value = $Setting[$key] $existing = $current | Where-Object { $_.key -eq $key } | Select-Object -First 1 if ($existing) { if ($existing.type -eq 'boolean') { if ($value -is [string] -and @('true', 'false') -contains $value) { $value = $value -eq 'true' } if ($value -isnot [bool]) { throw "The setting '$key' is true or false: pass `$true or `$false." } if ([bool] $existing.value -eq $value) { continue } } else { $value = [string] $value if ([string] $existing.value -ceq $value) { continue } } } $changes += [ordered]@{ key = [string] $key; value = $value } } if ($changes.Count -eq 0) { return } $body = ConvertTo-Json -InputObject @{ ($resources.Result) = @($changes) } -Depth 4 -Compress $keys = @($changes | ForEach-Object { $_['key'] }) if ($PSCmdlet.ShouldProcess(($keys -join ', '), $resources.Description)) { $result = Submit-Request -Uri $uri -Method $resources.Method -Body $body -Success $resources.Success $result = @(Test-ReturnFormat -Result $result -Location $resources.Result) $result = @($result | Where-Object { $keys -contains $_.key }) if ($result.Count -gt 0) { Set-ObjectTypeName -InputObject $result -TypeName $resources.TypeName } } } catch { $PSCmdlet.ThrowTerminatingError($_) } } } |