Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
feat(rest-api): expose settings on the rest api
Browse files Browse the repository at this point in the history
fixes #87
  • Loading branch information
Eduardo Campaña committed Jan 16, 2019
1 parent e943d58 commit bc15236
Showing 1 changed file with 23 additions and 57 deletions.
80 changes: 23 additions & 57 deletions wp-pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,14 @@ function wp_api_get_latest($p) {

// Register our own routes.
function rest_routes() {
register_rest_route('wp-pwa/v1', '/siteid/', array(
register_rest_route('frontity/v1', '/info/', array(
'methods' => 'GET',
'callback' => array($this, 'get_site_id')
'callback' => array($this, 'get_info'),
));
register_rest_route('wp-pwa/v1', '/discover/', array(
register_rest_route('frontity/v1', '/discover/', array(
'methods' => 'GET',
'callback' => array($this, 'discover_url')
));
register_rest_route('wp-pwa/v1', '/plugin-version/', array(
'methods' => 'GET',
'callback' => array($this, 'get_plugin_version')
));
register_rest_route('wp-pwa/v1', '/site-info/', array(
'methods' => 'GET',
'callback' => array($this, 'get_site_info')
));
register_rest_route('wp/v2', '/latest/', array(
'methods' => 'GET',
'callback' => array($this, 'latest_general_endpoint')
Expand All @@ -240,6 +232,26 @@ function rest_routes() {
));
}

// Get plugin info from the database. Used in the REST API.
function get_info() {
$plugin = array(
'version' => $this->plugin_version,
'settings' => get_option("frontity_settings"),
);

$site = array(
'locale' => get_locale(),
'timezone' => get_option('timezone_string'),
'gmt_offset' => intval(get_option('gmt_offset')),
'per_page' => intval(get_option('posts_per_page')),
);

return array(
'plugin' => $plugin,
'site' => $site,
);
}

// Get latest info of each custom post type.
function get_latest_from_cpt($cpts) {
$result = array();
Expand Down Expand Up @@ -579,52 +591,6 @@ function() {
);
}

// Get site id from the database. Used in the REST API.
function get_site_id() {
$settings = get_option("frontity_settings");

if (isset($settings["site_id"])) {
$site_id = $settings["site_id"];
} else {
$site_id = null;
}

return array('siteId' => $site_id);
}

// Populates plugin version in REST API.
function get_plugin_version() {
return array('plugin_version' => $this->plugin_version);
}

// Populates certain info in the REST API.
function get_site_info() {
$homepage_title = get_bloginfo('name');
$homepage_metadesc = get_bloginfo('description');
$homepage_url = get_bloginfo('url');
$per_page = get_option('posts_per_page');

$site_info = array(
'homepage_title' => $homepage_title,
'homepage_metadesc' => $homepage_metadesc,
'homepage_url' => $homepage_url,
'per_page' => $per_page
);

if (has_filter('get_site_info')) {
$site_info = apply_filters('get_site_info', $site_info);
}

return array(
'home' => array(
'title' => $site_info['homepage_title'],
'description' => $site_info['homepage_metadesc'],
'url' => $site_info['homepage_url']
),
'perPage' => $site_info['per_page']
);
}

// Our first implementation of url discovery.
function discover_url($request) {
$first_folder = $request['first_folder'];
Expand Down

0 comments on commit bc15236

Please sign in to comment.