Ders 17: Zorunlu bir blok otomatik olarak ekleme
Yeni bir gönderi oluşturulduğunda, gönderinin içeriğini doğrulamak ve değiştirmek için otomasyon özelliklerini kullanabiliriz.
Bu eğitim dersindeki query, belirli bir zorunlu bloğun gönderide mevcut olup olmadığını kontrol eder ve eksikse onu ekler.
Eksik bir blok eklemek için GraphQL query
Bu GraphQL query'nin çalışması için, endpoint'e uygulanan Şema Yapılandırması İç İçe Mutations özelliğinin etkin olmasını gerektirir
Bu GraphQL query, zorunlu wp:comments bloğunun gönderiye eklenip eklenmediğini kontrol eder. Eksikse, içeriğin altına eklenir.
Bu içeriği, insert-mandatory-comments-block-if-missing slug'ıyla bir Persisted Query olarak kaydedin:
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
}
}
}
}Persisted Query'yi çalıştırmak için hook ekleme
Internal GraphQL Server uzantısı varsayılan olarak kendi Ayarlarında tanımlanan Şema Yapılandırmasını uygular.
Bu nedenle, bu GraphQL query'nin çalışması için Internal GraphQL Server'a uygulanan Şema Yapılandırması İç İçe Mutations özelliğinin etkin olmasını gerektirir.
Bu PHP kodu, Persisted Query'yi çalıştırmak için WordPress draft_post eylemine (Internal GraphQL Server uzantısı aracılığıyla) bağlanır:
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'
);
}
);