Gato GraphQL + Bricks Builder 데모

Bricks 페이지 또는 템플릿을 사이트 간에 전송하기

이 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의 모든 업데이트를 놓치지 마세요.