博客

🚀 使用新版 Gato GraphQL v2.1 自动化您的 WordPress 任务

Leonardo Losoviz
作者:Leonardo Losoviz ·

我们很高兴地宣布 Gato GraphQL v2.1 已正式发布!

借助这个新版本以及 PRO 扩展,您可以实现 GraphQL as Automator(将 GraphQL 用作自动化器),利用 GraphQL 持久化 Query 自动化 WordPress 站点中与数据相关的任务。

以下是 v2.1 中所有变更的完整列表(简短版本可在 GitHub 发布说明中查看)。

支持在调用内部 GraphQL Server 时指定要应用的 Schema Configuration

现在可以在通过内部 GraphQL Server 执行 Query 时(即直接在 PHP 应用程序内部执行,而非通过端点),指定要应用的 Schema Configuration。

此新功能增强了 Internal GraphQL Server 扩展:它现在在 GraphQLServer 类的 executeQueryexecuteQueryInFile 方法上接受 $schemaConfigurationIDOrSlug 参数,并且 executePersistedQuery 已从持久化 Query 中提取所使用的 Schema Configuration:

class GraphQLServer {
  
  public static function executeQuery(
    string $query,
    array $variables = [],
    ?string $operationName = null,
+   // Accept parameter 
+   int|string|null $schemaConfigurationIDOrSlug = null,
  ): Response {
    // ...
  }
 
  public static function executeQueryInFile(
    string $file,
    array $variables = [],
    ?string $operationName = null,
+   // Accept parameter 
+   int|string|null $schemaConfigurationIDOrSlug = null,
  ): Response {
    // ...
  }
 
  public static function executePersistedQuery(
    WP_Post|string|int $persistedQuery,
    array $variables = [],
    ?string $operationName = null,
+   // Schema Configuration is taken directly from the Persisted Query
  ): Response {
    // ...
  }

GraphQL as Automator [PRO]

上述功能也为升级 Automation 扩展奠定了基础。

Automation 扩展现在提供了一个名为 Automation Configurator 的"自动化器"用户界面(可直接通过 WordPress 编辑器使用)。自动化触发器为任意 WordPress action hook,执行的操作是运行 GraphQL 持久化 Query。

自动化规则编辑器
自动化规则编辑器

例如,在创建新文章时,自动化规则 Add comments block to new post(执行同名持久化 Query)会检查 core/comments 块是否存在,如果不存在,则将其添加到文章末尾:

自动将评论块插入新“草稿”文章的演示
自动将评论块插入新“草稿”文章的演示

以下是自动化器的一些使用场景:

  • 使用 AI 为新文章创建(并优化)特色图片
  • 文章发布时添加必要块
  • 文章更新时将所有图片来源和链接中的 http 替换为 https
  • 有新文章时向管理员发送电子邮件
  • 向收到新回复的评论用户发送电子邮件
  • [多站点] 将新文章翻译成不同语言,并将翻译后的文章添加到各站点
  • 在外部服务上执行操作(例如:自动将新文章分享到 Facebook)

由于可以直接在 wp-admin 中编写 GraphQL 持久化 Query 和自动化规则,构建一条完全定制、满足您需求的自动化流水线无需花费多少时间。

由于输入没有任何限制(GraphQL Query 可以与 WordPress 中的所有数据交互,并通过 HTTP 客户端连接到任何外部服务),您应该能够满足 100% 的需求。

(自动化插件通常能满足我们 95% 的需求,但我们经常发现某些需要的输入尚未被映射。)

插件已预先创建了若干自动化规则(状态为 draft,需更新为 publish 才能生效)。您可以直接使用它们,也可以将其作为创建自定义自动化的参考示例(此列表将在后续版本中持续扩充):

  • Add comments block to new post
  • Insert block in new post
  • Send notification email to admin when there is a new post
  • Generate a post's featured image using AI (and optimize it) if a new post has none
自动化规则
自动化规则

预定义持久化 Query「Insert block in post」

新增的持久化 GraphQL Query「Insert block in post」允许向文章中插入块。它会识别文章中指定类型的第 n 个块(默认为 wp:paragraph),并将提供的自定义块 HTML 内容插入其正后方。

Automation 扩展配合使用,此持久化 Query 可用于自动向新发布的文章插入必要块(例如:用于推广正在进行的活动的 CTA 块)。

改进

  • 当从缓存初始化服务容器失败时,回退为从内存初始化 PHP 对象 (#2638)
  • 为所有预定义持久化 Query 赋予唯一的 operationName (#2644)
  • 改进了从文章获取块时块内容存在错误的错误提示信息
  • 完成了 Automation 扩展的文档 (#2651)
  • 在预定义持久化 Query「Generate a post's featured image using AI and optimize it」中,仅在文章标题不为空时才执行逻辑 (#ec931dd)

修复

  • 修复了扩展(Access Control、Cache Control 和 Field Deprecation)所使用的多控件 JS 组件在 Schema Configuration 块上显示"undefined"的问题 (#2639)
  • 修复了预定义持久化 Query 中正则表达式替换的问题 (#2649)
  • 避免在停用/重新激活插件时重新安装插件设置数据 (#2641)
  • 处理了将 WP_Post 作为 GraphQL 变量传递给内部 GraphQL Server 时出现的错误 (#2652)

订阅我们的新闻通讯

及时了解 Gato GraphQL 的所有更新。