Skip to content

Commit

Permalink
Adds v0.0.1 of the plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
pbredenberg committed Oct 28, 2018
1 parent 725f8a8 commit 965ab7f
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# WordPress Router API

Simple WordPress plugin to provide a router-like interface with applications consuming the WordPress REST API.

## Installing

Simply copy the project directory into `wp-content/plugins` and activate it via the plugins admin page, or drop `wordpress-router-api.php` into the `wp-content/mu-plugins` directory.

## Usage

The plugin exposes a single route to the API at the following URL via a GET request:

`http://MYWORDPRESSURL/wp-json/wp-router-api/v1/by/path`

It accept a single paramater named `path`.

Pass the whole path of a post you'd like to retrieve:

`http://MYWORDPRESSURL/wp-json/wp-router-api/v1/by/path?=/path/to/post`

The response will be a standard WordPress post object, with the following additional properties:

* `children`: An array of the returned posts children (in case you're unaware, that includes attachments).
* `fields`: An array of custom fields associated with the post (any data stored as an array will be unserialized).

## Technical Notes

Tested with WordPress v4.9.8, PHP 7.1.6
2 changes: 2 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
// You know the drill
88 changes: 88 additions & 0 deletions wordpress-router-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/*
* Plugin Name: WordPress Router API
* Version: 0.0.1
* Plugin URI: http://brodmountain.studio
* Description: A plugin to expose WordPress router-like functionality to the WP REST API.
* Author: Paul Bredenberg
* Author URI: http://brodmountain.studio
* Requires at least: 4.9.8
* Tested up to: 4.9.8
*
* Text Domain: wordpress-plugin-template
*
* @package WordPress
* @author Paul Bredenberg
* @since 0.0.1
*/

if ( ! defined( 'ABSPATH' ) ) exit;

class WordPressRouterAPI {

/**
* Constructor function
*/
public function __construct () {
add_action( 'rest_api_init', array( $this, 'init' ));
}

/**
* Init method in which custom REST endpoints can be routed to their
* related method.
*/
public function init() {
register_rest_route( 'wp-router-api/v1', '/by/path',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_object_by_path'),
)
);
}

public function get_object_by_path($request) {
$parameters = $request->get_query_params();
$path = $parameters['path'];

return array (
'parameters' => $parameters,
'path' => $path,
'post' => $this->get_obj_by_path($path),
);
}

/**
* Accepts a string with the provided path and returns a post object,
* or null if not found.
*/
private function get_obj_by_path($path) {
$object = null;
$page = get_page_by_path($path);
// If a page is not found, iterate through the current sites registered post types
// to attempt a match for the provided path.
if (!$page) {
foreach(get_post_types() as $post_type) {
$page = get_page_by_path(basename( untrailingslashit($path) ), OBJECT, $post_type);
if ($page) {
$object = $page;
}
}
} else {
$object = $page;
}
// Get this posts children.
$object->children = get_children($object->ID);
// For good measure lets get all the custom fields too.
$object->fields = get_post_custom($object->ID);
return $object;
}
}

if ( ! function_exists ( 'wp_router_api_init' ) ) {

function wp_router_api_init() {
$initializer = new WordPressRouterAPI();
}

add_action( 'init', 'wp_router_api_init' );
}

0 comments on commit 965ab7f

Please sign in to comment.