R/url.R

Defines functions rest_url rest_url_root wordpress_url

wordpress_url <- function() {
  wordpress_envvar("URL")
}

rest_url_root <- function(url = wordpress_url()) {
  root <- xml2::read_html(url)
  xpath <- "//link[@rel='https://api.w.org/']"
  link_node <- xml2::xml_find_first(root, xpath)
  xml2::xml_attr(link_node, "href")
}

rest_url <- function(
  path,
  namespace = "/wp/v2",
  ...,
  .url = wordpress_url(),
  .envir = parent.frame()
) {
  parsed <- httr::parse_url(rest_url_root(.url))

  base_path <- parsed$query$rest_route %||% parsed$path
  path <- glue::glue(path, .envir = .envir)
  full_path <- gsub("//+", "/", paste(base_path, namespace, path, sep = "/"))

  if (!is.null(parsed$query$rest_route)) {
    parsed$query$rest_route <- full_path
  } else {
    parsed$path <- full_path
  }
  httr::build_url(parsed)
}
shunsambongi/wordpress documentation built on Aug. 19, 2020, 12:10 a.m.