Gato GraphQL + Bricks Builder デモ

BricksのページまたはテンプレートをサイトURLで転送する

このWordPressサイトからBricksのページまたはテンプレートを取得し、ダウンストリームのWordPressサイトに複製します

Leonardo Losoviz
Leonardo Losoviz -
Logo
Image
Target Image

Gato GraphQLBricks拡張機能を組み合わせることで、このWordPressサイトからBricksのページまたはテンプレートをエクスポートし、ダウンストリームのWordPressサイトに複製することができます。

このクエリは、このWordPressサイトからBricksのページまたはテンプレートをダウンストリームのWordPressサイトへエクスポートします。

ダウンストリームのウェブサイトには、Gato GraphQLプラグイン(無料版)がインストールされている必要があります。また、「Nested mutations」が有効になったエンドポイントを公開している必要があります。

両サイトとも、対応するカスタム投稿タイプへのアクセスを許可する必要があります(bricks_templateおよび/またはpage)。

$updateパラメーターは、ダウンストリームサイトでページ/テンプレートを作成するか更新するかを指定します。

ページ/テンプレートを更新する場合、アップストリームサイトとダウンストリームサイト間の共通識別子はスラッグです。

以下の変数を指定する必要があります。

  • postSlug: 転送するBricksページまたはテンプレートのスラッグ
  • downstreamServerGraphQLEndpointURL: ダウンストリームWordPressサイトのGraphQLエンドポイントURL
  • update: ダウンストリームサイトでページ/テンプレートを作成する(false)か更新する(true)か
  • username: ダウンストリームサイトでの認証に使用するユーザー名
  • appPassword: ダウンストリームサイトでの認証に使用するアプリケーションパスワード

以下がGraphQLクエリです。

query GetPostData(
  $postTypes: [String!]! = ["bricks_template", "page"]
  $postSlug: String!
) {
  customPost(by: { slug: $postSlug }, customPostTypes: $postTypes, status: any)
    @fail(
      message: "There is no Bricks page or template in the upstream site with the provided slug"
      data: {
        slug: $postSlug
      }
    )
  {
    rawTitle
      @export(as: "postTitle")
    rawContent
      @export(as: "postContent")
    rawExcerpt
      @export(as: "postExcerpt")
    status
      @export(as: "postStatus")
    customPostType
      @export(as: "postType")
    metaKeys(filter: { exclude: ["_edit_last", "_edit_lock", "_pingme", "_encloseme", "_trackbackme", "enclosure", "_thumbnail_id", "_wp_trash_meta_status", "_wp_trash_meta_time", "_wp_desired_post_slug", "_wp_old_slug", "_wp_old_date"] })
    meta(keys: $__metaKeys) 
      @export(as: "postMeta")
  }
 
  isMissingPostInUpstream: _isNull(value: $__customPost)
    @export(as: "isMissingPostInUpstream")
}
 
query ExportDownstreamGraphQLQuery
  @depends(on: "GetPostData")
  @skip(if: $isMissingPostInUpstream)
{
  query: _echo(value: """
 
mutation UpdatePost(
  $update: Boolean! = false
  $postSlug: String!
  $postTitle: String!
  $postContent: String!
  $postExcerpt: String!
  $postStatus: CustomPostStatusEnum!
  $postType: String!
  $postMeta: JSONObject!
) {
  customPost(by: { slug: $postSlug }, customPostTypes: [$postType], status: any)
    @include(if: $update)
  {
    id
    update(input: {
      title: $postTitle,
      contentAs: { html: $postContent },
      excerpt: $postExcerpt,
      status: $postStatus,
      meta: $postMeta
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      ...on GenericCustomPostUpdateMutationPayload {
        customPost {
          ...CustomPostData
        }
      }
      ...on PostUpdateMutationPayload {
        post {
          ...CustomPostData
        }
      }
      ...on PageUpdateMutationPayload {
        page {
          ...CustomPostData
        }
      }
    }
  }
 
  createCustomPost(input: {
    title: $postTitle,
    slug: $postSlug,
    contentAs: { html: $postContent },
    excerpt: $postExcerpt,
    status: $postStatus,
    customPostType: $postType,
    meta: $postMeta
  })
    @skip(if: $update)
  {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      ...CustomPostData
    }
  }
}
 
fragment CustomPostData on CustomPost {
  id
  title
  slug
  content
  excerpt
  status
  meta(keys: [
    "_bricks_editor_mode",
    "_bricks_template_type",
    "_bricks_page_content_2",
    "_bricks_page_header_2",
    "_bricks_page_footer_2",        
  ])
}
    """
  )
    @export(as: "query")
    @remove
}
 
query ExportPostToWPSite(
  $downstreamServerGraphQLEndpointURL: String!
  $update: Boolean! = false
  $username: String!
  $appPassword: String!
  $postSlug: String!
)
  @depends(on: "ExportDownstreamGraphQLQuery")
  @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: "update",
          value: $update
        },
        {
          name: "postType",
          value: $postType
        },
        {
          name: "postSlug",
          value: $postSlug
        },
        {
          name: "postTitle",
          value: $postTitle
        },
        {
          name: "postContent",
          value: $postContent
        },
        {
          name: "postExcerpt",
          value: $postExcerpt
        },
        {
          name: "postStatus",
          value: $postStatus
        },
        {
          name: "postMeta",
          value: $postMeta
        }
      ],
      options: {
        headers: [
          {
            name: "Authorization",
            value: $__loginCredentialsHeaderValue
          }
        ]
      }
    }
  )
}

変数は以下のように指定します。

{
  "postSlug": "my-bricks-page",
  "downstreamServerGraphQLEndpointURL": "https://downstream-site.com/graphql",
  "update": false,
  "username": "admin",
  "appPassword": "your-app-password"
}

ニュースレターを購読する

Gato GraphQL のすべてのアップデートを把握しましょう。