Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * class CM_Permalink_Registration
- *
- * This class is responsible for creating any custom permalinks meant to be used by the site
- * These are hard coded routes that will return different templates included with our plugin
- **/
- class CM_Permalink_Registration {
- /**
- * A reference to an instance of this class.
- */
- private static $instance;
- public $dependencies = null;
- private $permalinks_to_register = array();
- public $plugin_template_path = null;
- public $theme_template_path = null;
- private function __construct($dependencies){
- $this->dependencies = $dependencies;
- $this->plugin_template_path = $dependencies['cm_manager']->plugin_path.'core/page-templates/hard-coded/';
- $this->theme_template_path = get_stylesheet_directory().'/cm-plugin-templates/';
- $this->theme_path = get_stylesheet_directory().'/';
- $this->permalinks_to_register = array(
- array(
- 'template_filename' => 'city-directory-page.php',
- 'route' => '^financial-advisor-(.+)/?$',
- 'rewrite' => 'index.php?location=$matches[1]',
- 'rewrite_tags' => array(
- array(
- 'tag' => '%location%',
- 'regex' => '([^&]+)'
- )
- ),
- 'conditional' => 'return isset($wp->query_vars["location"]) && $this->dependencies["states_cities"]->is_valid_location_url($wp->query_vars["location"]);'
- ),
- array(
- 'template_filename' => 'advisor-profile.php',
- 'route' => '^(.+)-cfp/?$',
- 'rewrite' => 'index.php?advisor=$matches[1]',
- 'rewrite_tags' => array(
- array(
- 'tag' => '%advisor%',
- 'regex' => '([^&]+)'
- )
- ),
- 'conditional' => 'return isset($wp->query_vars["advisor"]) && $this->dependencies["advisor"]->is_valid_advisor_url($wp->query_vars["advisor"]);'
- ),
- array(
- 'template_filename' => 'advisor-profile-editor.php',
- 'route' => '^edit-profile/(.+)/?$',
- 'rewrite' => 'index.php?edit=true&edit-advisor=$matches[1]',
- 'rewrite_tags' => array(
- array(
- 'tag' => '%edit-advisor%',
- 'regex' => '([^&]+)'
- )
- ),
- 'conditional' => 'return isset($wp->query_vars["edit-advisor"]) && $this->dependencies["advisor"]->is_valid_advisor_url($wp->query_vars["edit-advisor"]);'
- )
- );
- $this->register_init_hook();
- $this->register_template_redirect_hook();
- }
- /**
- * Returns an instance of this class.
- */
- public static function get_instance($dependencies){
- if( null == self::$instance ) {
- self::$instance = new CM_Permalink_Registration($dependencies);
- }
- return self::$instance;
- }
- /**
- * public function register_custom_permalinks()
- *
- * This function registers our new routes with wordpress by adding rewrite rules
- **/
- public function register_custom_permalinks(){
- foreach($this->permalinks_to_register as $permalink){
- add_rewrite_rule($permalink['route'], $permalink['rewrite'], 'bottom');
- foreach($permalink['rewrite_tags'] as $tag){
- add_rewrite_tag($tag['tag'], $tag['regex']);
- }
- }
- }
- /**
- * public function deliver_custom_templates()
- *
- * This function hooks into the template redirect hook and checks to see if the entered URL meets the specified conditions to display a custom template
- **/
- public function deliver_custom_templates($return_template){
- global $wp;
- $found_valid_page = false;
- foreach($this->permalinks_to_register as $permalink){
- if(eval($permalink['conditional'])){
- if (file_exists($this->theme_template_path . $permalink['template_filename'])) {
- $return_template = $this->theme_template_path . $permalink['template_filename'];
- }
- else{
- $return_template = $this->plugin_template_path . $permalink['template_filename'];
- }
- $found_valid_page = true;
- }
- }
- if($found_valid_page){
- include($return_template);
- die();
- }
- else{
- global $post, $wp_query;
- foreach($this->permalinks_to_register as $permalink){
- if (preg_match('&'.$permalink['route'].'&', ltrim($_SERVER['REQUEST_URI'], '/')) !== 0 && !eval($permalink['conditional'])){
- header("HTTP/1.0 404 Not Found - Archive Empty");
- require $this->theme_path.'404.php';
- exit;
- $wp_query->is_404 = true;
- }
- }
- }
- return $return_template;
- }
- /**
- * private function register_template_redirect_hook()
- *
- * This function is responsible for hooking in our register_custom_permalinks() method to the template_redirect hook
- **/
- private function register_template_redirect_hook(){
- add_action("template_include", array(&$this, 'deliver_custom_templates'));
- }
- private function register_init_hook(){
- add_action('init', array(&$this, 'register_custom_permalinks'));
- }
- }
- foreach($this->permalinks_to_register as $permalink){
- add_rewrite_rule($permalink['route'], $permalink['rewrite'], 'bottom');
- foreach($permalink['rewrite_tags'] as $tag){
- add_rewrite_tag($tag['tag'], $tag['regex']);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement