R/post.R

Defines functions post_content post_update post_browse post_retrieve

Documented in post_browse post_content post_retrieve post_update

#' Retrieve a Post
#' @param post the post ID
#' @export
post_retrieve <- function(post) {
  send_request("GET", "/posts/{post}")
}

#' View a Post in the Browser
#' @inheritParams post_retrieve
#' @export
post_browse <- function(post) {
  response <- send_request("GET", "/posts/{post}", query = list("_fields" = "link"))
  url <- response[["content"]][["link"]]
  utils::browseURL(url)
}

#' Update a Post
#' @inheritParams post_retrieve
#' @param title post title
#' @param content post content (see [post_content()] for RMarkdown)
#' @param ... Currently unused
#' @export
post_update <- function(post, ..., title = NULL, content = NULL) {
  is_post_content <- inherits(content, "wordpress_post_content")

  if (inherits(content, "wordpress_post_content")) {
    if (is.null(title)) {
      try(silent = TRUE, {
        title <- rmarkdown::yaml_front_matter(attr(content, "file"))$title
      })
    }
    content <- unclass(content)
  }

  body <- purrr::compact(
    list(
      title = title,
      content = content
    )
  )
  send_request("POST", "/posts/{post}",
    body = body,
    encode = "json",
    ...
  )
}

#' Create Post Content from RMarkdown
#' @param file path to .Rmd file
#' @export
post_content <- function(file) {
  output_file <- fs::file_temp(ext = "html")
  on.exit(unlink(output_file), add = TRUE)
  render_wordpress(file, output_file = output_file, quiet = TRUE)
  structure(
    brio::read_file(output_file),
    file = file,
    class = "wordpress_post_content"
  )
}
shunsambongi/wordpress documentation built on Aug. 19, 2020, 12:10 a.m.