Schema-Tutorial
Schema-TutorialLektion 17: Automatisches Hinzufügen eines Pflichtbausteins

Lektion 17: Automatisches Hinzufügen eines Pflichtbausteins

Immer wenn ein neuer Beitrag erstellt wird, können wir die Automatisierungsfunktionen nutzen, um den Inhalt des Beitrags zu validieren und zu ändern.

Die Query in dieser Tutorial-Lektion prüft, ob ein bestimmter Pflichtbaustein im Beitrag vorhanden ist, und fügt ihn hinzu, falls er fehlt.

GraphQL-Query zum Hinzufügen eines fehlenden Bausteins

Damit diese GraphQL-Query funktioniert, muss die Schema-Konfiguration des Endpoints Verschachtelte Mutations aktiviert haben

Diese GraphQL-Query prüft, ob der Pflichtbaustein wp:comments bereits zum Beitrag hinzugefügt wurde. Falls er fehlt, wird er am Ende des Inhalts eingefügt.

Speichere diesen Inhalt als Persisted Query mit dem Slug insert-mandatory-comments-block-if-missing:

query CheckIfCommentsBlockExists($postId: ID!) {
  posts(
    filter: {
      ids: [$postId]
      search: "\"<!-- /wp:comments -->\""
    }
  ) {
    id
  }
  blockExists: _notEmpty(value: $__posts)
    @export(as: "blockExists")
}
 
mutation MaybeInsertCommentsBlock($postId: ID!)
  @depends(on: "CheckIfCommentsBlockExists")
  @skip(if: $blockExists)
{
  post(by: { id: $postId }) {
    id
    rawContent
    adaptedRawContent: _strAppend(
      after: $__rawContent
      append: """
 
<!-- wp:comments -->
<div class="wp-block-comments"><!-- wp:comments-title /-->
 
<!-- wp:comment-template -->
<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column {"width":"40px"} -->
<div class="wp-block-column" style="flex-basis:40px"><!-- wp:avatar {"size":40,"style":{"border":{"radius":"20px"}}} /--></div>
<!-- /wp:column -->
 
<!-- wp:column -->
<div class="wp-block-column"><!-- wp:comment-author-name {"fontSize":"small"} /-->
 
<!-- wp:group {"style":{"spacing":{"margin":{"top":"0px","bottom":"0px"}}},"layout":{"type":"flex"}} -->
<div class="wp-block-group" style="margin-top:0px;margin-bottom:0px"><!-- wp:comment-date {"fontSize":"small"} /-->
 
<!-- wp:comment-edit-link {"fontSize":"small"} /--></div>
<!-- /wp:group -->
 
<!-- wp:comment-content /-->
 
<!-- wp:comment-reply-link {"fontSize":"small"} /--></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->
<!-- /wp:comment-template -->
 
<!-- wp:comments-pagination -->
<!-- wp:comments-pagination-previous /-->
 
<!-- wp:comments-pagination-numbers /-->
 
<!-- wp:comments-pagination-next /-->
<!-- /wp:comments-pagination -->
 
<!-- wp:post-comments-form /--></div>
<!-- /wp:comments -->   
 
      """
    )
    update(input: {
      contentAs: { html: $__adaptedRawContent },
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        rawContent
      }
    }
  }
}

Hook hinzufügen, um die Persisted Query auszuführen

Die Erweiterung Internal GraphQL Server wendet standardmäßig die Schema-Konfiguration an, die in den eigenen Einstellungen definiert ist.

Damit diese GraphQL-Query funktioniert, muss die Schema-Konfiguration, die auf den Internal GraphQL Server angewendet wird, Verschachtelte Mutations aktiviert haben.

Dieser PHP-Code hängt sich in die WordPress-Action draft_post ein, um die Persisted Query auszuführen (über die Erweiterung Internal GraphQL Server):

use GatoGraphQL\InternalGraphQLServer\GraphQLServer;
use WP_Post;
 
add_action(
  'draft_post',
  function (int $postID): void {
    GraphQLServer::executePersistedQuery(
      'insert-mandatory-comments-block-if-missing',
      [
        'postId' => $postID,
      ],
      'MaybeInsertCommentsBlock'
    );
  }
);