Advertisement
Guest User

Untitled

a guest
May 4th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. /**
  2. * class CM_Permalink_Registration
  3. *
  4. * This class is responsible for creating any custom permalinks meant to be used by the site
  5. * These are hard coded routes that will return different templates included with our plugin
  6. **/
  7.  
  8. class CM_Permalink_Registration {
  9.  
  10. /**
  11. * A reference to an instance of this class.
  12. */
  13. private static $instance;
  14.  
  15. public $dependencies = null;
  16.  
  17. private $permalinks_to_register = array();
  18.  
  19. public $plugin_template_path = null;
  20. public $theme_template_path = null;
  21.  
  22.  
  23. private function __construct($dependencies){
  24.  
  25. $this->dependencies = $dependencies;
  26.  
  27. $this->plugin_template_path = $dependencies['cm_manager']->plugin_path.'core/page-templates/hard-coded/';
  28. $this->theme_template_path = get_stylesheet_directory().'/cm-plugin-templates/';
  29. $this->theme_path = get_stylesheet_directory().'/';
  30.  
  31.  
  32.  
  33. $this->permalinks_to_register = array(
  34. array(
  35. 'template_filename' => 'city-directory-page.php',
  36. 'route' => '^financial-advisor-(.+)/?$',
  37. 'rewrite' => 'index.php?location=$matches[1]',
  38. 'rewrite_tags' => array(
  39. array(
  40. 'tag' => '%location%',
  41. 'regex' => '([^&]+)'
  42. )
  43. ),
  44. 'conditional' => 'return isset($wp->query_vars["location"]) && $this->dependencies["states_cities"]->is_valid_location_url($wp->query_vars["location"]);'
  45. ),
  46. array(
  47. 'template_filename' => 'advisor-profile.php',
  48. 'route' => '^(.+)-cfp/?$',
  49. 'rewrite' => 'index.php?advisor=$matches[1]',
  50. 'rewrite_tags' => array(
  51. array(
  52. 'tag' => '%advisor%',
  53. 'regex' => '([^&]+)'
  54. )
  55. ),
  56. 'conditional' => 'return isset($wp->query_vars["advisor"]) && $this->dependencies["advisor"]->is_valid_advisor_url($wp->query_vars["advisor"]);'
  57. ),
  58. array(
  59. 'template_filename' => 'advisor-profile-editor.php',
  60. 'route' => '^edit-profile/(.+)/?$',
  61. 'rewrite' => 'index.php?edit=true&edit-advisor=$matches[1]',
  62. 'rewrite_tags' => array(
  63. array(
  64. 'tag' => '%edit-advisor%',
  65. 'regex' => '([^&]+)'
  66. )
  67. ),
  68. 'conditional' => 'return isset($wp->query_vars["edit-advisor"]) && $this->dependencies["advisor"]->is_valid_advisor_url($wp->query_vars["edit-advisor"]);'
  69. )
  70. );
  71.  
  72.  
  73. $this->register_init_hook();
  74. $this->register_template_redirect_hook();
  75. }
  76.  
  77.  
  78. /**
  79. * Returns an instance of this class.
  80. */
  81. public static function get_instance($dependencies){
  82. if( null == self::$instance ) {
  83. self::$instance = new CM_Permalink_Registration($dependencies);
  84. }
  85. return self::$instance;
  86. }
  87.  
  88.  
  89. /**
  90. * public function register_custom_permalinks()
  91. *
  92. * This function registers our new routes with wordpress by adding rewrite rules
  93. **/
  94.  
  95. public function register_custom_permalinks(){
  96. foreach($this->permalinks_to_register as $permalink){
  97. add_rewrite_rule($permalink['route'], $permalink['rewrite'], 'bottom');
  98. foreach($permalink['rewrite_tags'] as $tag){
  99. add_rewrite_tag($tag['tag'], $tag['regex']);
  100. }
  101. }
  102.  
  103. }
  104.  
  105. /**
  106. * public function deliver_custom_templates()
  107. *
  108. * 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
  109. **/
  110.  
  111. public function deliver_custom_templates($return_template){
  112. global $wp;
  113.  
  114. $found_valid_page = false;
  115.  
  116. foreach($this->permalinks_to_register as $permalink){
  117. if(eval($permalink['conditional'])){
  118. if (file_exists($this->theme_template_path . $permalink['template_filename'])) {
  119. $return_template = $this->theme_template_path . $permalink['template_filename'];
  120. }
  121. else{
  122. $return_template = $this->plugin_template_path . $permalink['template_filename'];
  123. }
  124. $found_valid_page = true;
  125. }
  126. }
  127. if($found_valid_page){
  128. include($return_template);
  129. die();
  130. }
  131. else{
  132. global $post, $wp_query;
  133. foreach($this->permalinks_to_register as $permalink){
  134. if (preg_match('&'.$permalink['route'].'&', ltrim($_SERVER['REQUEST_URI'], '/')) !== 0 && !eval($permalink['conditional'])){
  135. header("HTTP/1.0 404 Not Found - Archive Empty");
  136. require $this->theme_path.'404.php';
  137. exit;
  138. $wp_query->is_404 = true;
  139. }
  140. }
  141. }
  142. return $return_template;
  143. }
  144.  
  145.  
  146. /**
  147. * private function register_template_redirect_hook()
  148. *
  149. * This function is responsible for hooking in our register_custom_permalinks() method to the template_redirect hook
  150. **/
  151.  
  152. private function register_template_redirect_hook(){
  153. add_action("template_include", array(&$this, 'deliver_custom_templates'));
  154. }
  155.  
  156. private function register_init_hook(){
  157. add_action('init', array(&$this, 'register_custom_permalinks'));
  158. }
  159.  
  160. }
  161.  
  162. foreach($this->permalinks_to_register as $permalink){
  163. add_rewrite_rule($permalink['route'], $permalink['rewrite'], 'bottom');
  164. foreach($permalink['rewrite_tags'] as $tag){
  165. add_rewrite_tag($tag['tag'], $tag['regex']);
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement