ไลบรารี Queries
ไลบรารี Queriesซิงโครไนซ์โพสต์ระหว่างเว็บไซต์

ซิงโครไนซ์โพสต์ระหว่างเว็บไซต์

query นี้ได้รับการสาธิตในเดโม การซิงโครไนซ์โพสต์ระหว่าง 2 เว็บไซต์ รวมถึงเมตาดาต้าของ Meta Box (และ Slim SEO ด้วย)

query นี้ซิงโครไนซ์โพสต์จากเว็บไซต์ WordPress หนึ่งไปยังอีกเว็บไซต์หนึ่ง รวมถึงเมตาดาต้าด้วย

โดยจะดำเนินการต่อไปนี้:

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

query นี้ต้องการสิ่งต่อไปนี้:

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

ตัวแปรที่จำเป็นมีดังนี้:

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

หากเป็นการอัปเดตโพสต์ ตัวระบุร่วม (common identifier) ระหว่างเว็บไซต์ต้นทาง (upstream) และเว็บไซต์ปลายทาง (downstream) คือ slug ของโพสต์

query GraphQL ต้องถูกเรียกใช้บนเว็บไซต์ต้นทาง

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 }"
}