Gato GraphQL + Meta Box เดโม

ซิงโครไนซ์โพสต์ระหว่าง 2 ไซต์ รวมถึงข้อมูลเมตา Meta Box และ Slim SEO

ซิงโครไนซ์โพสต์พร้อมข้อมูล Meta Box และ Slim SEO จากไซต์ WordPress หนึ่งไปยังอีกไซต์หนึ่ง โดยใช้ Gato GraphQL for WordPress

Leonardo Losoviz
Leonardo Losoviz -
Logo
Image
Target Image
Target Image

เราสามารถซิงโครไนซ์โพสต์จากไซต์ WordPress หนึ่งไปยังอีกไซต์หนึ่งได้ รวมถึงข้อมูลเมตาที่จัดการผ่าน Meta Box หรือที่เพิ่มโดย Slim SEO (หรือปลั๊กอินอื่น ๆ)

ในเดโมนี้ เราจะใช้ GraphQL เพื่อ:

  1. ดึงโพสต์และข้อมูลเมตาทั้งหมดจากไซต์ต้นทาง
  2. สร้างโพสต์ใหม่หรืออัปเดตโพสต์ที่มีอยู่บนไซต์เป้าหมาย และคัดลอกข้อมูลโพสต์และข้อมูลเมตาจากไซต์ต้นทาง

queries นี้ต้องการ:

  • Gato GraphQL + ส่วนขยาย PRO บนไซต์ต้นทาง
  • ปลั๊กอิน Gato GraphQL ฟรีบนไซต์เป้าหมาย
  • เปิดใช้งาน Nested Mutations บน endpoint ของไซต์เป้าหมาย

เราต้องระบุตัวแปรต่อไปนี้:

  • postType: ประเภทโพสต์กำหนดเองของโพสต์ที่ต้องการซิงโครไนซ์ระหว่างไซต์
  • postSlug: สลักของโพสต์ที่ต้องการซิงโครไนซ์ระหว่างไซต์
  • downstreamServerGraphQLEndpointURL: URL ของ GraphQL endpoint บนไซต์ WordPress เป้าหมาย
  • username: ชื่อผู้ใช้ของ Application Password สำหรับการยืนยันตัวตนบนไซต์เป้าหมาย
  • appPassword: รหัสผ่านของ Application Password สำหรับการยืนยันตัวตนบนไซต์เป้าหมาย
  • update: ระบุว่าจะอัปเดตโพสต์ที่มีอยู่ (true) หรือสร้างโพสต์ใหม่ (false)

หากอัปเดตโพสต์ ตัวระบุร่วมระหว่างไซต์ต้นน้ำและปลายน้ำคือสลักของโพสต์

GraphQL queries ต้องดำเนินการบนไซต์ต้นทาง

นี่คือ GraphQL queries:

query CheckHasCustomPost($postSlug: String!, $postType: String! = post)
{
  customPost(by: { slug: $postSlug }, status: any, customPostTypes: [$postType])
    @fail(
      message: "There is no post in the upstream site with the provided slug"
      data: {
        slug: $postSlug
      }
    )
  {
    rawTitle
      @export(as: "postTitle")
    rawContent
      @export(as: "postContent")
    rawExcerpt
      @export(as: "postExcerpt")
 
    metaKeys(filter: { exclude: [
      "_thumbnail_id",
      "_edit_last",
    ] })
    meta(keys: $__metaKeys) 
      @export(as: "postMeta")
  }
 
  isMissingPostInUpstream: _isNull(value: $__customPost)
    @export(as: "isMissingPostInUpstream")
}
 
query ExportCreateCustomPostOnTargetSiteGraphQLQuery(
  $update: Boolean! = false
)
  @depends(on: "CheckHasCustomPost")
  @skip(if: $isMissingPostInUpstream)
  @skip(if: $update)
{
  query: _echo(value: """
 
mutation CreateCustomPost(
  $postType: String! = post
  $postSlug: String!
  $postTitle: String!
  $postExcerpt: String!
  $postContent: String!
  $postMeta: NullableListValueJSONObject!
) {
  createCustomPost(input: {
    customPostType: $postType
    title: $postTitle,
    excerpt: $postExcerpt,
    slug: $postSlug,
    contentAs: { html: $postContent },
    status: draft,
    meta: $postMeta,
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      __typename
      ...on CustomPost {
        customPostType
        title
        excerpt
        slug
        content
        status
      }
    }
  }
}
 
    """
  )
    @export(as: "query")
    @remove
}
 
query ExportUpdateCustomPostOnTargetSiteGraphQLQuery(
  $update: Boolean! = false
)
  @depends(on: "CheckHasCustomPost")
  @skip(if: $isMissingPostInUpstream)
  @include(if: $update)
{
  query: _echo(value: """
 
mutation UpdateCustomPost(
  $postType: String! = post
  $postSlug: String!
  $postTitle: String!
  $postContent: String!
  $postExcerpt: String!
  $postMeta: NullableListValueJSONObject!
) {
  customPost(by: { slug: $postSlug }, status: any, customPostTypes: [$postType]) {
    update(input: {
      title: $postTitle,
      excerpt: $postExcerpt,
      contentAs: { html: $postContent },
      meta: $postMeta,
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      customPost {
        __typename
        ...on CustomPost {
          customPostType
          title
          excerpt
          slug
          content
          status
        }
      }
    }
  }
}
 
    """
  )
    @export(as: "query")
    @remove
}
 
query CreateOrUpdateCustomPostOnTargetSite(
  $downstreamServerGraphQLEndpointURL: String!
  $postSlug: String!
  $username: String!
  $appPassword: String!
  $postType: String! = post
)
  @depends(on: [
    "ExportCreateCustomPostOnTargetSiteGraphQLQuery",
    "ExportUpdateCustomPostOnTargetSiteGraphQLQuery",
  ])
  @skip(if: $isMissingPostInUpstream)
{
  loginCredentials: _sprintf(
    string: "%s:%s",
    values: [$username, $appPassword]
  )
    @remove
 
  base64EncodedLoginCredentials: _strBase64Encode(
    string: $__loginCredentials
  )
    @remove
 
  loginCredentialsHeaderValue: _sprintf(
    string: "Basic %s",
    values: [$__base64EncodedLoginCredentials]
  )
    @remove
 
  _sendGraphQLHTTPRequest(
    input: {
      endpoint: $downstreamServerGraphQLEndpointURL,
      query: $query,
      variables: [
        {
          name: "postSlug",
          value: $postSlug
        },
        {
          name: "postTitle",
          value: $postTitle
        },
        {
          name: "postContent",
          value: $postContent
        },
        {
          name: "postExcerpt",
          value: $postExcerpt
        },
        {
          name: "postMeta",
          value: $postMeta
        },
        {
          name: "postType",
          value: $postType
        }
      ],
      options: {
        headers: [
          {
            name: "Authorization",
            value: $__loginCredentialsHeaderValue
          }
        ]
      }
    }
  )
}

ตัวแปรจะมีลักษณะดังนี้:

{
  "postType": "post",
  "postSlug": "hello-world",
  "downstreamServerGraphQLEndpointURL": "https://target-site.com/graphql",
  "update": false,
  "username": "admin",
  "appPassword": "{ application password, eg: cNEp BVPy QVxF eVqH lggt BTb4 }"
}

สมัครรับจดหมายข่าวของเรา

ติดตามการอัปเดตทั้งหมดของ Gato GraphQL