Permalink
Browse files

Added a function get_custom_route() that returns matched custom route…

… and the template it points to
  • Loading branch information...
Nomafin committed Jan 8, 2019
1 parent 94a951a commit 1861b1c9c3c1274864e8ed9e0d3efcc624e4f7fd
Showing with 38 additions and 1 deletion.
  1. +5 −0 CHANGELOG.md
  2. +33 −1 dustpress.php
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.20.0] - 2019-01-08

### Added
- A function `get_custom_route()` that returns matched custom route and the template it points to.

## [1.19.1] - 2019-01-07

### Changed
@@ -6,7 +6,7 @@
Author: Miika Arponen & Ville Siltala / Geniem Oy
Author URI: http://www.geniem.com
License: GPLv3
Version: 1.19.0
Version: 1.20.0
*/
final class DustPress {
@@ -41,6 +41,9 @@
// Registered custom ajax functions
private $ajax_functions;
// Custom routes
private $custom_routes = [];
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new DustPress();
@@ -257,6 +260,33 @@ public function create_instance() {
}
}
/**
* This function returns the model name of current custom route, or false if we are not on a custom route.
*
* @type function
* @date 8/1/2019
* @since 1.20.0
*
* @param N/A
* @return string|boolean
*/
public function get_custom_route() {
global $wp_query;
$custom_route = $wp_query->get( 'dustpress_custom_route' );
if ( ! empty( $custom_route ) ) {
if ( isset( $this->custom_routes[ $custom_route ] ) ) {
return [
'template' => $custom_route,
'route' => $this->custom_routes[ $custom_route ],
];
}
}
return false;
}
/**
* This function gets current template's filename and returns without extension or WP-template prefixes such as page- or single-.
*
@@ -1608,6 +1638,8 @@ private function add_core_paths() {
* @return void
*/
public function register_custom_route( $route, $template ) {
$this->custom_routes[ $template ] = $route;
add_action( 'init', function() use ( $route, $template ) {
add_rewrite_rule( '(' . $route . ')(\/(.+))?\/?$', 'index.php?dustpress_custom_route=' . $template . '&dustpress_custom_route_route=$matches[1]&dustpress_custom_route_parameters=$matches[3]', 'top' );
}, 30);

0 comments on commit 1861b1c

Please sign in to comment.