Changeset 1323814
- Timestamp:
- 01/08/2016 08:33:46 AM (10 years ago)
- Location:
- add-page-from-template/trunk
- Files:
-
- 6 edited
-
add-page-from-template.php (modified) (1 diff)
-
includes/class-loader.php (modified) (4 diffs)
-
includes/class-option.php (modified) (2 diffs)
-
includes/class-template.php (modified) (1 diff)
-
includes/class-templatesearcher.php (modified) (3 diffs)
-
readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
add-page-from-template/trunk/add-page-from-template.php
r1319194 r1323814 5 5 * Description(en): Add pages from template files. 6 6 * Description: Creates virtural page from template file. 7 * Version: 0. 37 * Version: 0.4 8 8 * Author: Yousan_O 9 9 * Author URI: http://www.l2tp.org -
add-page-from-template/trunk/includes/class-loader.php
r1319194 r1323814 42 42 add_action('template_redirect', array(self::$instance, 'template_redirect')); 43 43 add_action('delete_option', array(self::$instance, 'delete_option'), 10, 1 ); 44 add_action('pre_get_posts', array(self::$instance, 'pre_get_posts'));45 44 } 46 45 … … 48 47 * queryvarsのpagenameをセット 49 48 */ 50 public function pre_get_posts() {49 public function setPagename($pagename) { 51 50 /** @var WP_Query */ 52 51 global $wp_query; 53 global $wp_rewrite; 54 $request_url = $_SERVER['REQUEST_URI']; 55 $request_url =user_trailingslashit($request_url); 56 // remove slash "/hoge/" => "hoge" 57 $slug = preg_replace('#^/?([^/]+)/?$#', '\\1', $request_url); 58 $wp_query->set('pagename', $slug); 59 return $wp_query; 52 $wp_query->set('pagename', $pagename); 53 } 54 55 private function setGlobalQuery() { 56 /** @var WP_Query */ 57 global $wp_query; 58 $wp_query->is_home = false; 59 // 他にも必要そうなものがあればココでセットしていく 60 // $wp_query->set('is_home', false)は動かない 61 } 62 63 private function setGlobalPost() { 64 /** @var WP_Post */ 65 global $post; 66 67 /** @see get_default_post_to_edit() */ 68 /** @var stdClass */ 69 $postObj = new stdClass; 70 $postObj->ID = 1; 71 $postObj->post_author = ''; 72 $postObj->post_date = ''; 73 $postObj->post_date_gmt = ''; 74 $postObj->post_password = ''; 75 $postObj->post_name = ''; 76 $postObj->post_type = 'page'; 77 $postObj->post_status = 'publish'; 78 $postObj->to_ping = ''; 79 $postObj->pinged = ''; 80 $postObj->comment_status = get_default_comment_status( 'page' ); 81 $postObj->ping_status = get_default_comment_status( 'page', 'pingback' ); 82 $postObj->post_pingback = get_option( 'default_pingback_flag' ); 83 $postObj->post_category = get_option( 'default_category' ); 84 $postObj->page_template = 'default'; 85 $postObj->post_parent = 0; 86 $postObj->menu_order = 0; 87 $post = new WP_Post( $postObj ); 88 89 // set filter 90 // @see get_post() 91 // void '$_post = $_post->filter( $filter );' returns null 92 $post = sanitize_post( $post, 'raw' ); 60 93 } 61 94 … … 79 112 add_rewrite_endpoint($template->getTemplateSlug(), EP_ROOT); 80 113 } 81 flush_rewrite_rules();82 114 } 83 115 … … 90 122 } 91 123 92 124 /** 125 * 実際に表示させているのはココ! 126 */ 93 127 public function template_redirect() { 94 128 global $wp_query; 95 129 foreach($this->templates as $template) { 96 130 if (isset($wp_query->query[$template->getTemplateSlug()])) { 97 $template = $template->path; 98 apply_filters("page_template", $template); 99 if ($template = apply_filters('template_include', $template)) { 100 include($template); 131 $templatePath = apply_filters("page_template", $template->path); 132 //add_action('pre_get_posts', array(self::$instance, 'pre_get_posts')); 133 $this->setPagename($template->pagename); 134 $this->setGlobalPost(); 135 $this->setGlobalQuery(); 136 if ($templatePath = apply_filters('template_include', $templatePath)) { 137 include($templatePath); 101 138 } 102 139 exit; // Stop! -
add-page-from-template/trunk/includes/class-option.php
r1319194 r1323814 15 15 const APFT_OPTION_NAME = 'apft_options'; 16 16 17 18 public static function getDefaults() { 19 return array( 20 'is_aggressive' => false, 21 'base_dir' => 'pages/', 22 ); 23 } 24 17 25 /** 18 26 * Start up … … 36 44 return $options[$varname]; 37 45 } else { 38 return null; 46 $defaults = self::getDefaults(); 47 return $defaults[$varname]; 39 48 } 40 49 } -
add-page-from-template/trunk/includes/class-template.php
r1306270 r1323814 14 14 public $slug = ''; 15 15 public $status = NULL; 16 17 public function getTemplateSlug() { 18 $pattern = '/.*\/page-(?P<slug>.*)\.php$/'; 19 if (preg_match($pattern, $this->path, $match)) { 20 return $match['slug']; 21 } else { // nothing to mach 22 23 } 24 } 16 public $pagename = ''; 25 17 26 18 public function __construct($path) { 27 19 $this->path = $path; 28 20 $this->filename = basename($path); 29 if ( $this->filename ) { // page-hoge.php => hoge 30 $pattern = '/^page-(.*)\.php$/'; 31 $replacement = '${1}'; 32 $this->slug = preg_replace($pattern, $replacement, $this->filename); 21 $pattern = '#^(?<path>.*)page-(?<unislug>[^\.]+)\.php$#'; 22 23 // abspath => relpath 24 $fullBaseDir = untrailingslashit(get_stylesheet_directory().DIRECTORY_SEPARATOR. 25 AP_Option::get_('base_dir').DIRECTORY_SEPARATOR); 26 $fullBaseDir = preg_replace('#/+#','/', $fullBaseDir); // remove duplicate slash 27 28 $relpath = str_replace($fullBaseDir, '', $this->path); 29 if ( !empty($this->filename) && 30 !empty($relpath) && 31 preg_match($pattern, $relpath, $match)) { // page-hoge.php => hoge 32 $slug = stripslashes($match['path'] . $match['unislug']); 33 $this->slug = preg_replace('#^/#', '', $slug); 34 $this->pagename = $match['unislug']; 35 } else { 36 throw new Exception('Invalid Template Path'); // no one shows, just return 33 37 } 34 38 $this->status = $this->getStatus(); 35 39 } 36 40 41 /** 42 * Returns a template slug name. 43 * pages/page-hoge.php => hoge 44 * pages/hoge/page-fuga.php => fuga 45 * 46 * @return string 47 */ 48 public function getTemplateSlug() { 49 return $this->slug; 50 } 51 37 52 private function getStatus() { 53 // incomplete 38 54 return AP_TemplateStatus::ENABLED; 39 55 } -
add-page-from-template/trunk/includes/class-templatesearcher.php
r1306270 r1323814 18 18 19 19 private static function setPath() { 20 self::$path = get_stylesheet_directory().'/pages/'; 20 self::$path = (get_stylesheet_directory() . DIRECTORY_SEPARATOR . 21 AP_Option::get_('base_dir')); 21 22 } 22 23 … … 26 27 public static function getTemplates() { 27 28 self::setPath(); 29 28 30 if (!is_dir(self::$path)) { 29 31 return array(); … … 31 33 32 34 $rets = array(); 33 foreach(glob(self::$path.'page-*.php') as $file) { 34 $rets[] = new AP_Template($file); 35 $basedir_path = preg_replace('#/+$#', '', self::$path); // remove last slash 36 $files = self::filesToArray($basedir_path); 37 foreach ($files as $file) { 38 try { 39 $template = new AP_Template($file); 40 $rets[] = $template; 41 } catch (Exception $e){ 42 // do nothing. just invalid filename. 43 } 35 44 } 36 45 return $rets; 46 } 47 48 /** 49 * recursive search. 50 * Return only file. 51 * 52 * @param $dir 53 * @return string[] 54 */ 55 private static function filesToArray($dir) 56 { 57 $result = array(); 58 $cdir = scandir($dir); 59 foreach ( $cdir as $key => $value ){ 60 if (!in_array($value, array('.', '..'))) { 61 if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) { 62 $result = array_merge($result, 63 static::filesToArray($dir . DIRECTORY_SEPARATOR . $value)); 64 } else { 65 $result[] = $dir . DIRECTORY_SEPARATOR .$value; 66 } 67 } 68 } 69 return $result; 37 70 } 38 71 } -
add-page-from-template/trunk/readme.txt
r1319194 r1323814 53 53 == Changelog == 54 54 55 = Version 0.4 = 56 * Fixed: APFT Overrides global $post. Now we can use function/tags which refers to global $post such as get_post_type(). 57 55 58 = Version 0.3 = 56 59 * Fixed: Removed debug code.
Note: See TracChangeset
for help on using the changeset viewer.