Libreria di queries
Libreria di queriesInserire un blocco in tutti i post

Inserire un blocco in tutti i post

Questa query identifica l'n-esimo blocco di un tipo specificato ("wp:paragraph" per impostazione predefinita) in tutti i post, e inserisce il contenuto HTML del blocco personalizzato fornito subito dopo.

La variabile $injectBlockMarkup deve ricevere il markup HTML completo del blocco (con le virgolette precedute da escape per l'input JSON). Ad esempio:

<!-- mycompany:black-friday-campaign-video --><figure class=\"wp-block-video\"><video controls src=\"https://mysite.com/videos/BlackFriday2023.mp4\"></video></figure><!-- /mycompany:black-friday-campaign-video -->

Questa query richiede che l'endpoint abbia le Mutazioni annidate abilitate.

query CreateRegex(
  $searchBlockType: String! = "wp:paragraph"
  $injectAfterBlockCount: Int = 1
  $injectBlockMarkup: String!
) {
  endingBlockMarkup: _sprintf(
    string: "<!-- /%s -->",
    values: [$searchBlockType]
  )
    @remove
  endingBlockMarkupArray: _arrayPad(
    array: [],
    length: $injectAfterBlockCount,
    value: $__endingBlockMarkup
  )
    @remove
  regexString: _arrayJoin(
    array: $__endingBlockMarkupArray,
    separator: "[\\s\\S]+"
  )
    @remove
  regex: _sprintf(
    string: "#(%s)#U",
    values: [$__regexString]
  )
    @export(as: "regex")
    @remove
  replaceWith: _sprintf(
    string: "$1%s",
    values: [$injectBlockMarkup]
  )
    @export(as: "replaceWith")
    @remove
}
 
mutation InsertBlockInAllPosts
  @depends(on: "CreateRegex")
{
  posts: posts(
    pagination: { limit: -1 }
  ) {
    id
    rawContent
    adaptedRawContent: _strRegexReplace(
      in: $__rawContent,
      searchRegex: $regex,
      replaceWith: $replaceWith,
      limit: 1
    )
    update(input: {
      contentAs: { html: $__adaptedRawContent },
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        rawContent
      }
    }
  }
}