Plugin Directory

Changeset 1323814


Ignore:
Timestamp:
01/08/2016 08:33:46 AM (10 years ago)
Author:
hogetan
Message:

fit for old version php

Location:
add-page-from-template/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • add-page-from-template/trunk/add-page-from-template.php

    r1319194 r1323814  
    55 * Description(en): Add pages from template files.
    66 * Description: Creates virtural page from template file.
    7  * Version: 0.3
     7 * Version: 0.4
    88 * Author: Yousan_O
    99 * Author URI: http://www.l2tp.org
  • add-page-from-template/trunk/includes/class-loader.php

    r1319194 r1323814  
    4242        add_action('template_redirect', array(self::$instance, 'template_redirect'));
    4343        add_action('delete_option', array(self::$instance, 'delete_option'), 10, 1 );
    44         add_action('pre_get_posts', array(self::$instance, 'pre_get_posts'));
    4544    }
    4645
     
    4847     * queryvarsのpagenameをセット
    4948     */
    50     public function pre_get_posts() {
     49    public function setPagename($pagename) {
    5150        /** @var WP_Query */
    5251        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' );
    6093    }
    6194
     
    79112            add_rewrite_endpoint($template->getTemplateSlug(), EP_ROOT);
    80113        }
    81         flush_rewrite_rules();
    82114    }
    83115
     
    90122    }
    91123
    92 
     124    /**
     125     * 実際に表示させているのはココ!
     126     */
    93127    public function template_redirect() {
    94128        global $wp_query;
    95129        foreach($this->templates as $template) {
    96130            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);
    101138                }
    102139                exit; // Stop!
  • add-page-from-template/trunk/includes/class-option.php

    r1319194 r1323814  
    1515    const APFT_OPTION_NAME = 'apft_options';
    1616
     17
     18    public static function getDefaults() {
     19        return array(
     20            'is_aggressive' => false,
     21            'base_dir' => 'pages/',
     22        );
     23    }
     24
    1725    /**
    1826     * Start up
     
    3644            return $options[$varname];
    3745        } else {
    38             return null;
     46            $defaults = self::getDefaults();
     47            return $defaults[$varname];
    3948        }
    4049    }
  • add-page-from-template/trunk/includes/class-template.php

    r1306270 r1323814  
    1414    public $slug = '';
    1515    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 = '';
    2517
    2618    public function __construct($path) {
    2719        $this->path = $path;
    2820        $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
    3337        }
    3438        $this->status = $this->getStatus();
    3539    }
    3640
     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
    3752    private function getStatus() {
     53        // incomplete
    3854        return AP_TemplateStatus::ENABLED;
    3955    }
  • add-page-from-template/trunk/includes/class-templatesearcher.php

    r1306270 r1323814  
    1818
    1919        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'));
    2122        }
    2223
     
    2627        public static function getTemplates() {
    2728            self::setPath();
     29
    2830            if (!is_dir(self::$path)) {
    2931                return array();
     
    3133
    3234            $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                }
    3544            }
    3645            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;
    3770        }
    3871    }
  • add-page-from-template/trunk/readme.txt

    r1319194 r1323814  
    5353== Changelog ==
    5454
     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
    5558= Version 0.3 =
    5659* Fixed: Removed debug code.
Note: See TracChangeset for help on using the changeset viewer.