Libreria di queries
Libreria di queriesImportare un articolo da un feed RSS WordPress

Importare un articolo da un feed RSS WordPress

Questa query importa un articolo da un feed RSS WordPress, utilizzando il titolo, il contenuto e l'estratto dell'articolo.

Se l'autore con quel nome utente esiste localmente, viene utilizzato; altrimenti viene sostituito con quello definito tramite la variabile $defaultAuthorUsername.

La variabile $url riceve l'URL del feed RSS dell'articolo WordPress singolo. Di solito corrisponde all'URL dell'articolo + "/feed/rss/?withoutcomments=1". Ad esempio:

https://wordpress.com/blog/2024/07/16/wordpress-6-6/feed/rss/?withoutcomments=1
query GetPostFromRSSFeedAndExportData(
  $url: URL!
) {
  _sendHTTPRequest(input: {
    url: $url,
    method: GET
  }) {
    body
    rssJSON: _strDecodeXMLAsJSON(
      xml: $__body
      alwaysArrayTagNames: [
        "category",
      ],
    )
 
    # Fields to be imported
    authorUsername: _objectProperty(
      object: $__rssJSON,
      by: {
        path: "rss.channel.item.dc:creator"
      }
    )
      @export(as: "authorUsername")
 
    # categorySlugs: _objectProperty(
    #   object: $__rssJSON,
    #   by: {
    #     path: "rss.channel.item.category"
    #   }
    # )
 
    content:  _objectProperty(
      object: $__rssJSON,
      by: {
        path: "rss.channel.item.content:encoded"
      }
    )
      @export(as: "content")
 
    excerpt:  _objectProperty(
      object: $__rssJSON,
      by: {
        path: "rss.channel.item.description"
      }
    )
      @export(as: "excerpt")
 
    title:  _objectProperty(
      object: $__rssJSON,
      by: {
        path: "rss.channel.item.title"
      }
    )
      @export(as: "title")
  }
}
 
# If the author's username exists in this site, keep it
# Otherwise, use the default one
query CheckAuthorExistsOrChange(
  $defaultAuthorUsername: String! = "admin"
)
  @depends(on: "GetPostFromRSSFeedAndExportData")
{
  existingUserByUsername: user(by: { username: $authorUsername })
  {
    id
    username
  }
  userExists: _notNull(value: $__existingUserByUsername)
  username: _if(
    condition: $__userExists,
    then: $authorUsername,
    else: $defaultAuthorUsername
  )
    @export(as: "existingAuthorUsername")
}
 
mutation ImportPostFromWPRSSFeed
  @depends(on: "CheckAuthorExistsOrChange")
{
  createPost(input: {
    status: draft,
    authorBy: {
      username: $existingAuthorUsername
    },
    contentAs: {
      html: $content
    },
    excerpt: $excerpt
    title: $title
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      slug
      date
      status
 
      author {
        id
        username
      }
      content
      excerpt
      title
    }
  }
}