-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
725f8a8
commit 965ab7f
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?php | ||
// You know the drill |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
} |