Skip to content

Commit

Permalink
update typerocket and admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindees committed Jun 7, 2019
1 parent 591c9a7 commit 1575c05
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 58 deletions.
21 changes: 18 additions & 3 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,25 @@ function trDevIconSearch() {
};

$rules = function() {
echo '<h3><i class="tr-icon-link"></i> ' . __('Rewrite Rules') . '</h3>';
echo '<h3><i class="tr-icon-link"></i> ' . __('Rewrite Rules & Routes') . '</h3>';
$rules = get_option('rewrite_rules');
$routes = \TypeRocket\Http\Routes::$routes;
if($routes && $rules) {
echo "<p><strong>TypeRocket Routes</strong>. TypeRocket loads custom routes at run time.</p>";
echo '<table class="wp-list-table widefat fixed striped">';
echo "<thead><tr><th>" . __('Match') . "</th><th>" . __('Vars') . "</th><th>" . __('Trailing Slash') . "</th><th>" . __('Request') . "</th></tr></thead>";
foreach ($routes as $route) {
/** @var \TypeRocket\Http\Route $route */
$methods = json_encode($route->methods);
$vars = json_encode($route->match[1]);
$slash = $route->addTrailingSlash ? 'yes' : '';
echo "<tr><td>{$route->match[0]}</td><td>{$vars}</td><td>{$slash}</td><td>{$methods}</td></tr>";
}
echo '</table>';
}

if(!empty($rules)) {
echo "<p>If you are using TypeRocket custom routes they will not appear in this list. TypeRocket detects custom routes on the fly.</p>";
echo "<p><strong>WordPress Rewrite Rules</strong>. WordPress rewrite rules are loaded from the database.</p>";
echo '<table class="wp-list-table widefat fixed striped">';
echo "<thead><tr><th>" . __('Rewrite Rule') . "</th><th>" . __('Match') . "</th></tr></thead>";
foreach ($rules as $rule => $match) {
Expand All @@ -106,6 +121,6 @@ function trDevIconSearch() {

$tabs = tr_tabs();
$tabs->addTab(__('Icons'), $icons)
->addTab(__('Rewrite Rules'), $rules)
->addTab(__('Rewrites & Routes'), $rules)
->addTab(__('Configure'), $configure)
->render('box');
2 changes: 1 addition & 1 deletion typerocket/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"typerocket/core": "4.0.*",
"typerocket/plugin-seo": "4.0.*",
"typerocket/plugin-theme-options": "4.0.*",
"typerocket/plugin-builder": "4.0.*",
"typerocket/plugin-builder": "4.0.*"
},
"autoload": {
"psr-4": {
Expand Down
54 changes: 6 additions & 48 deletions typerocket/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions typerocket/vendor/typerocket/core/src/Elements/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,30 @@ public function useAjax() {
/**
* Use TypeRocket Rest to submit form
*
* @param null|string $resource override resource name useful for setting custom post type IDs etc.
* @return Form $this
*/
public function useJson()
public function useJson($resource = null)
{
if( $this->useAjax === null ) {
$this->useAjax();
}

$this->formUrl = home_url('/', get_http_protocol() ) . 'tr_json_api/v1/' . $this->resource . '/' . $this->itemId;
$the_resource = $this->resource;

if($this->model instanceof WPPost) {
$pt = $this->model->getPostType();
$the_resource = $the_resource != $pt ? $pt : $the_resource;
}

if($this->model instanceof WPTerm) {
$tx = $this->model->getTaxonomy();
$the_resource = $the_resource != $tx ? $tx : $the_resource;
}

$the_resource = $resource ?? $the_resource;

$this->formUrl = home_url('/', get_http_protocol() ) . 'tr_json_api/v1/' . $the_resource . '/' . $this->itemId;

return $this;
}
Expand Down
4 changes: 4 additions & 0 deletions typerocket/vendor/typerocket/core/src/Http/Rewrites/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function __construct()
$this->middleware_fallback = 'term';
$this->middleware = $this->resource;
$this->handler = $obj[3];
} elseif($obj = Registry::getCustomResource($this->resource)) {
$this->middleware_fallback = '';
$this->middleware = $this->resource;
$this->handler = $obj[3];
}

if ( apply_filters( 'tr_rest_api_load', true, $this->resource, $this->item ) ) {
Expand Down
12 changes: 8 additions & 4 deletions typerocket/vendor/typerocket/core/src/Http/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,14 @@ private function runRoute($path = null, $handle = null, $wilds = null)
$this->vars = $wilds;
$addSlash = $this->match[1]->addTrailingSlash ?? true;
$path = self::$request->getPath();
$endsInSlash = Str::ends('/', $path );

if( $addSlash && ! Str::ends('/', $path ) && self::$request->isGet() ) {
if( $addSlash && ! $endsInSlash && self::$request->isGet() ) {
wp_redirect( $path . '/' );
die();
} elseif( ! $addSlash && $endsInSlash && self::$request->isGet() ) {
wp_redirect( rtrim($path, '/') );
die();
}

if (is_callable($handle->do)) {
Expand Down Expand Up @@ -190,7 +194,7 @@ public function detectRoute()
$requestPath = ltrim($path, '/');
$routesRegistered = $this->getRegisteredRoutes();

list($match, $args) = $this->matchRoute($requestPath, $routesRegistered);
list($match, $args) = $this->matchRoute(rtrim($requestPath, '/'), $routesRegistered);

if($match) {
$this->match = [$requestPath, $match[2], $args];
Expand All @@ -216,7 +220,7 @@ public function redirect_canonical($redirect_url, $requested_url)
}

/**
* @param $uri path to match
* @param string $uri path to match
* @param array $routes list of routes
*
* @return array
Expand All @@ -227,7 +231,7 @@ public function matchRoute($uri, $routes) {

$regex = ['#^(?'];
foreach ($routes as $i => $route) {
$slash = $route[2]->addTrailingSlash ? '/?' : '';
$slash = $route[2]->addTrailingSlash ? '\/?$' : '';
$regex[] = rtrim($route[0], '/') . $slash . '(*MARK:'.$i.')';
}
$regex = implode('|', $regex) . ')$#x';
Expand Down
23 changes: 23 additions & 0 deletions typerocket/vendor/typerocket/core/src/Register/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Registry
'post_tag' => ['tag', 'tags', null, null]
];

public static $customs = [];

/**
* Add a post type resource
*
Expand Down Expand Up @@ -60,6 +62,27 @@ public static function addTaxonomyResource($id, $resource = []) {
self::$taxonomies[$id] = array_pad($resource, 4, null);
}

/**
* Add a custom resource
*
* @param string $id custom resource id
* @param array $resource resource name ex. posts, pages, books
*/
public static function addCustomResource($id, $resource = []) {
self::$customs[$id] = array_pad($resource, 4, null);
}

/**
* Get the custom resource
*
* @param $id
*
* @return null
*/
public static function getCustomResource($id) {
return self::$customs[$id] ?? null;
}

/**
* Add Registrable objects to collection
*
Expand Down

0 comments on commit 1575c05

Please sign in to comment.