Skip to content

Instantly share code, notes, and snippets.

@bsalex
Forked from gmazzap/Controller.php
Created March 6, 2019 01:12
Show Gist options
  • Select an option

  • Save bsalex/af9929df5da1608ac18edbee6e4a7adb to your computer and use it in GitHub Desktop.

Select an option

Save bsalex/af9929df5da1608ac18edbee6e4a7adb to your computer and use it in GitHub Desktop.

Revisions

  1. @gmazzap gmazzap revised this gist Apr 12, 2016. 8 changed files with 34 additions and 18 deletions.
    4 changes: 4 additions & 0 deletions Controller.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    <?php
    namespace GM\VirtualPages;

    /**
    * @author Giuseppe Mazzapica <[email protected]>
    * @license http://opensource.org/licenses/MIT MIT
    */
    class Controller implements ControllerInterface {

    private $pages;
    4 changes: 4 additions & 0 deletions ControllerInterface.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    <?php
    namespace GM\VirtualPages;

    /**
    * @author Giuseppe Mazzapica <[email protected]>
    * @license http://opensource.org/licenses/MIT MIT
    */
    interface ControllerInterface {

    /**
    9 changes: 9 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    The MIT License (MIT)

    Copyright (c) 2016 Giuseppe Mazzapica

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    4 changes: 4 additions & 0 deletions Page.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    <?php
    namespace GM\VirtualPages;

    /**
    * @author Giuseppe Mazzapica <[email protected]>
    * @license http://opensource.org/licenses/MIT MIT
    */
    class Page implements PageInterface {

    private $url;
    4 changes: 4 additions & 0 deletions PageInterface.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    <?php
    namespace GM\VirtualPages;

    /**
    * @author Giuseppe Mazzapica <[email protected]>
    * @license http://opensource.org/licenses/MIT MIT
    */
    interface PageInterface {

    function getUrl();
    4 changes: 4 additions & 0 deletions TemplateLoader.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    <?php
    namespace GM\VirtualPages;

    /**
    * @author Giuseppe Mazzapica <[email protected]>
    * @license http://opensource.org/licenses/MIT MIT
    */
    class TemplateLoader implements TemplateLoaderInterface {

    public function init( PageInterface $page ) {
    4 changes: 4 additions & 0 deletions TemplateLoaderInterface.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    <?php
    namespace GM\VirtualPages;

    /**
    * @author Giuseppe Mazzapica <[email protected]>
    * @license http://opensource.org/licenses/MIT MIT
    */
    interface TemplateLoaderInterface {

    /**
    19 changes: 1 addition & 18 deletions gm-virtual-pages.php
    Original file line number Diff line number Diff line change
    @@ -5,24 +5,7 @@
    Description: Virtual pages made easy.
    Author: Giuseppe Mazzapica
    Author URI: http://gm.zoomlab.it
    License: GPL-3.0
    */

    /*
    Copyright (C) 2014 Giuseppe Mazzapica
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
    License: MIT
    */

    require_once 'PageInterface.php';
  2. @gmazzap gmazzap revised this gist Feb 19, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TemplateLoader.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ class TemplateLoader implements TemplateLoaderInterface {

    public function init( PageInterface $page ) {
    $this->templates = wp_parse_args(
    (array) $page->getTemplate(), array( 'page.php', 'index.php' )
    array( 'page.php', 'index.php' ), (array) $page->getTemplate()
    );
    }

  3. @gmazzap gmazzap revised this gist Sep 25, 2014. 2 changed files with 26 additions and 24 deletions.
    4 changes: 2 additions & 2 deletions TemplateLoader.php
    Original file line number Diff line number Diff line change
    @@ -15,10 +15,10 @@ public function load() {
    $filtered = apply_filters( 'template_include',
    apply_filters( 'virtual_page_template', $template )
    );
    if ( ! empty( $filtered ) && file_exists( $filtered ) ) {
    if ( empty( $filtered ) || file_exists( $filtered ) ) {
    $template = $filtered;
    }
    if ( ! empty( $template ) ) {
    if ( ! empty( $template ) &&file_exists( $template ) ) {
    require_once $template;
    }
    }
    46 changes: 24 additions & 22 deletions gm-virtual-pages.php
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,29 @@
    <?php namespace GM\VirtualPages;
    /*
    Plugin Name: GM Virtual Pages
    Plugin URI: http://wordpress.stackexchange.com/questions/162240/custom-pages-with-plugin
    Description: Virtual pages made easy.
    Author: Giuseppe Mazzapica
    Author URI: http://gm.zoomlab.it
    License: GPL-3.0
    */
    Plugin Name: GM Virtual Pages
    Plugin URI: http://wordpress.stackexchange.com/questions/162240/custom-pages-with-plugin
    Description: Virtual pages made easy.
    Author: Giuseppe Mazzapica
    Author URI: http://gm.zoomlab.it
    License: GPL-3.0
    */

    /*
    Copyright (C) 2014 Giuseppe Mazzapica
    Copyright (C) 2014 Giuseppe Mazzapica
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
    */
    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
    */

    require_once 'PageInterface.php';
    require_once 'ControllerInterface.php';
    @@ -47,11 +47,13 @@
    add_filter( 'the_permalink', function( $plink ) {
    global $post, $wp_query;
    if (
    $wp_query->is_page && isset( $wp_query->virtual_page )
    $wp_query->is_page
    && isset( $wp_query->virtual_page )
    && $wp_query->virtual_page instanceof Page
    && isset( $post->is_virtual ) && $post->is_virtual
    && isset( $post->is_virtual )
    && $post->is_virtual
    ) {
    $plink = home_url( $wp_query->virtual_page->getUrl() );
    $plink = home_url( $wp_query->virtual_page->getUrl() );
    }
    return $plink;
    } );
  4. @gmazzap gmazzap revised this gist Sep 25, 2014. 2 changed files with 22 additions and 22 deletions.
    2 changes: 1 addition & 1 deletion Controller.php
    Original file line number Diff line number Diff line change
    @@ -73,7 +73,7 @@ private function setupQuery() {
    : NULL;
    }

    private function handleExit() {
    public function handleExit() {
    exit();
    }
    }
    42 changes: 21 additions & 21 deletions Page.php
    Original file line number Diff line number Diff line change
    @@ -8,42 +8,42 @@ class Page implements PageInterface {
    private $content;
    private $template;
    private $wp_post;

    function __construct( $url, $title = 'Untitled', $template = 'page.php' ) {
    $this->url = filter_var( $url, FILTER_SANITIZE_URL );
    $this->setTitle( $title );
    $this->setTemplate( $template);
    $this->url = filter_var( $url, FILTER_SANITIZE_URL );
    $this->setTitle( $title );
    $this->setTemplate( $template);
    }

    function getUrl() {
    return $this->url;
    return $this->url;
    }

    function getTemplate() {
    return $this->template;
    return $this->template;
    }

    function getTitle() {
    return $this->title;
    return $this->title;
    }

    function setTitle( $title ) {
    $this->title = filter_var( $title, FILTER_SANITIZE_STRING );
    return $this;
    $this->title = filter_var( $title, FILTER_SANITIZE_STRING );
    return $this;
    }

    function setContent( $content ) {
    $this->content = $content;
    return $this;
    $this->content = $content;
    return $this;
    }

    function setTemplate( $template ) {
    $this->template = $template;
    return $this;
    $this->template = $template;
    return $this;
    }

    function asWpPost() {
    if ( is_null( $this->wp_post ) ) {
    if ( is_null( $this->wp_post ) ) {
    $post = array(
    'ID' => 0,
    'post_title' => $this->title,
  5. @gmazzap gmazzap revised this gist Sep 25, 2014. 4 changed files with 42 additions and 34 deletions.
    30 changes: 18 additions & 12 deletions Page.php
    Original file line number Diff line number Diff line change
    @@ -5,44 +5,50 @@ class Page implements PageInterface {

    private $url;
    private $title;
    private $content;
    private $template;
    private $wp_post;

    function __construct( $url, $title = 'Untitled', $template = 'page.php' ) {
    $this->url = filter_var( $url, FILTER_SANITIZE_URL );
    $this->setTitle( $title );
    $this->setTemplate( $template);
    $this->url = filter_var( $url, FILTER_SANITIZE_URL );
    $this->setTitle( $title );
    $this->setTemplate( $template);
    }

    function getUrl() {
    return $this->url;
    return $this->url;
    }

    function getTemplate() {
    return $this->template;
    return $this->template;
    }

    function getTitle() {
    return $this->title;
    return $this->title;
    }

    function setTitle( $title ) {
    $this->title = filter_var( $title, FILTER_SANITIZE_STRING );
    return $this;
    $this->title = filter_var( $title, FILTER_SANITIZE_STRING );
    return $this;
    }

    function setContent( $content ) {
    $this->content = $content;
    return $this;
    }

    function setTemplate( $template ) {
    $this->template = $template;
    return $this;
    $this->template = $template;
    return $this;
    }

    function asWpPost() {
    if ( is_null( $this->wp_post ) ) {
    if ( is_null( $this->wp_post ) ) {
    $post = array(
    'ID' => 0,
    'post_title' => $this->title,
    'post_name' => sanitize_title( $this->title ),
    'post_content' => '',
    'post_content' => $this->content ? : '',
    'post_excerpt' => '',
    'post_parent' => 0,
    'menu_order' => 0,
    12 changes: 7 additions & 5 deletions PageInterface.php
    Original file line number Diff line number Diff line change
    @@ -11,12 +11,14 @@ function getTitle();

    function setTitle( $title );

    function setContent( $content );

    function setTemplate( $template );

    /**
    * Get a WP_Post build using viryual Page object
    *
    * @return \WP_Post
    */
    * Get a WP_Post build using viryual Page object
    *
    * @return \WP_Post
    */
    function asWpPost();
    }
    }
    26 changes: 13 additions & 13 deletions TemplateLoaderInterface.php
    Original file line number Diff line number Diff line change
    @@ -2,17 +2,17 @@
    namespace GM\VirtualPages;

    interface TemplateLoaderInterface {
    /**
    * Setup loader for a page objects
    *
    * @param \GM\VirtualPagesPageInterface $page matched virtual page
    */
    public function init( PageInterface $page );
    /**
    * Trigger core and custom hooks to filter templates,
    * then load the found template.
    */
    public function load();

    /**
    * Setup loader for a page objects
    *
    * @param \GM\VirtualPagesPageInterface $page matched virtual page
    */
    public function init( PageInterface $page );
    /**
    * Trigger core and custom hooks to filter templates,
    * then load the found template.
    */
    public function load();
    }
    8 changes: 4 additions & 4 deletions gm-virtual-pages.php
    Original file line number Diff line number Diff line change
    @@ -47,11 +47,11 @@
    add_filter( 'the_permalink', function( $plink ) {
    global $post, $wp_query;
    if (
    $wp_query->is_page && isset( $wp_query->virtual_page )
    && $wp_query->virtual_page instanceof Page
    && isset( $post->is_virtual ) && $post->is_virtual
    $wp_query->is_page && isset( $wp_query->virtual_page )
    && $wp_query->virtual_page instanceof Page
    && isset( $post->is_virtual ) && $post->is_virtual
    ) {
    $plink = home_url( $wp_query->virtual_page->getUrl() );
    $plink = home_url( $wp_query->virtual_page->getUrl() );
    }
    return $plink;
    } );
  6. @gmazzap gmazzap created this gist Sep 25, 2014.
    79 changes: 79 additions & 0 deletions Controller.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    <?php
    namespace GM\VirtualPages;

    class Controller implements ControllerInterface {

    private $pages;
    private $loader;
    private $matched;

    function __construct( TemplateLoaderInterface $loader ) {
    $this->pages = new \SplObjectStorage;
    $this->loader = $loader;
    }

    function init() {
    do_action( 'gm_virtual_pages', $this );
    }

    function addPage( PageInterface $page ) {
    $this->pages->attach( $page );
    return $page;
    }

    function dispatch( $bool, \WP $wp ) {
    if ( $this->checkRequest() && $this->matched instanceof Page ) {
    $this->loader->init( $this->matched );
    $wp->virtual_page = $this->matched;
    do_action( 'parse_request', $wp );
    $this->setupQuery();
    do_action( 'wp', $wp );
    $this->loader->load();
    $this->handleExit();
    }
    return $bool;
    }

    private function checkRequest() {
    $this->pages->rewind();
    $path = trim( $this->getPathInfo(), '/' );
    while( $this->pages->valid() ) {
    if ( trim( $this->pages->current()->getUrl(), '/' ) === $path ) {
    $this->matched = $this->pages->current();
    return TRUE;
    }
    $this->pages->next();
    }
    }

    private function getPathInfo() {
    $home_path = parse_url( home_url(), PHP_URL_PATH );
    return preg_replace( "#^/?{$home_path}/#", '/', add_query_arg( array() ) );
    }

    private function setupQuery() {
    global $wp_query;
    $wp_query->init();
    $wp_query->is_page = TRUE;
    $wp_query->is_singular = TRUE;
    $wp_query->is_home = FALSE;
    $wp_query->found_posts = 1;
    $wp_query->post_count = 1;
    $wp_query->max_num_pages = 1;
    $posts = (array) apply_filters(
    'the_posts', array( $this->matched->asWpPost() ), $wp_query
    );
    $post = $posts[0];
    $wp_query->posts = $posts;
    $wp_query->post = $post;
    $wp_query->queried_object = $post;
    $GLOBALS['post'] = $post;
    $wp_query->virtual_page = $post instanceof \WP_Post && isset( $post->is_virtual )
    ? $this->matched
    : NULL;
    }

    private function handleExit() {
    exit();
    }
    }
    28 changes: 28 additions & 0 deletions ControllerInterface.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    <?php
    namespace GM\VirtualPages;

    interface ControllerInterface {

    /**
    * Init the controller, fires the hook that allow consumer to add pages
    */
    function init();

    /**
    * Register a page object in the controller
    *
    * @param \GM\VirtualPages\Page $page
    * @return \GM\VirtualPages\Page
    */
    function addPage( PageInterface $page );

    /**
    * Run on 'do_parse_request' and if the request is for one of the registerd
    * setup global variables, fire core hooks, requires page template and exit.
    *
    * @param boolean $bool The boolean flag value passed by 'do_parse_request'
    * @param \WP $wp The global wp object passed by 'do_parse_request'
    */
    function dispatch( $bool, \WP $wp );

    }
    68 changes: 68 additions & 0 deletions Page.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    <?php
    namespace GM\VirtualPages;

    class Page implements PageInterface {

    private $url;
    private $title;
    private $template;
    private $wp_post;

    function __construct( $url, $title = 'Untitled', $template = 'page.php' ) {
    $this->url = filter_var( $url, FILTER_SANITIZE_URL );
    $this->setTitle( $title );
    $this->setTemplate( $template);
    }

    function getUrl() {
    return $this->url;
    }

    function getTemplate() {
    return $this->template;
    }

    function getTitle() {
    return $this->title;
    }

    function setTitle( $title ) {
    $this->title = filter_var( $title, FILTER_SANITIZE_STRING );
    return $this;
    }

    function setTemplate( $template ) {
    $this->template = $template;
    return $this;
    }

    function asWpPost() {
    if ( is_null( $this->wp_post ) ) {
    $post = array(
    'ID' => 0,
    'post_title' => $this->title,
    'post_name' => sanitize_title( $this->title ),
    'post_content' => '',
    'post_excerpt' => '',
    'post_parent' => 0,
    'menu_order' => 0,
    'post_type' => 'page',
    'post_status' => 'publish',
    'comment_status' => 'closed',
    'ping_status' => 'closed',
    'comment_count' => 0,
    'post_password' => '',
    'to_ping' => '',
    'pinged' => '',
    'guid' => home_url( $this->getUrl() ),
    'post_date' => current_time( 'mysql' ),
    'post_date_gmt' => current_time( 'mysql', 1 ),
    'post_author' => is_user_logged_in() ? get_current_user_id() : 0,
    'is_virtual' => TRUE,
    'filter' => 'raw'
    );
    $this->wp_post = new \WP_Post( (object) $post );
    }
    return $this->wp_post;
    }
    }
    22 changes: 22 additions & 0 deletions PageInterface.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?php
    namespace GM\VirtualPages;

    interface PageInterface {

    function getUrl();

    function getTemplate();

    function getTitle();

    function setTitle( $title );

    function setTemplate( $template );

    /**
    * Get a WP_Post build using viryual Page object
    *
    * @return \WP_Post
    */
    function asWpPost();
    }
    25 changes: 25 additions & 0 deletions TemplateLoader.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    <?php
    namespace GM\VirtualPages;

    class TemplateLoader implements TemplateLoaderInterface {

    public function init( PageInterface $page ) {
    $this->templates = wp_parse_args(
    (array) $page->getTemplate(), array( 'page.php', 'index.php' )
    );
    }

    public function load() {
    do_action( 'template_redirect' );
    $template = locate_template( array_filter( $this->templates ) );
    $filtered = apply_filters( 'template_include',
    apply_filters( 'virtual_page_template', $template )
    );
    if ( ! empty( $filtered ) && file_exists( $filtered ) ) {
    $template = $filtered;
    }
    if ( ! empty( $template ) ) {
    require_once $template;
    }
    }
    }
    18 changes: 18 additions & 0 deletions TemplateLoaderInterface.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?php
    namespace GM\VirtualPages;

    interface TemplateLoaderInterface {

    /**
    * Setup loader for a page objects
    *
    * @param \GM\VirtualPagesPageInterface $page matched virtual page
    */
    public function init( PageInterface $page );

    /**
    * Trigger core and custom hooks to filter templates,
    * then load the found template.
    */
    public function load();
    }
    57 changes: 57 additions & 0 deletions gm-virtual-pages.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    <?php namespace GM\VirtualPages;
    /*
    Plugin Name: GM Virtual Pages
    Plugin URI: http://wordpress.stackexchange.com/questions/162240/custom-pages-with-plugin
    Description: Virtual pages made easy.
    Author: Giuseppe Mazzapica
    Author URI: http://gm.zoomlab.it
    License: GPL-3.0
    */

    /*
    Copyright (C) 2014 Giuseppe Mazzapica
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
    */

    require_once 'PageInterface.php';
    require_once 'ControllerInterface.php';
    require_once 'TemplateLoaderInterface.php';
    require_once 'Page.php';
    require_once 'Controller.php';
    require_once 'TemplateLoader.php';

    $controller = new Controller ( new TemplateLoader );

    add_action( 'init', array( $controller, 'init' ) );

    add_filter( 'do_parse_request', array( $controller, 'dispatch' ), PHP_INT_MAX, 2 );

    add_action( 'loop_end', function( \WP_Query $query ) {
    if ( isset( $query->virtual_page ) && ! empty( $query->virtual_page ) ) {
    $query->virtual_page = NULL;
    }
    } );

    add_filter( 'the_permalink', function( $plink ) {
    global $post, $wp_query;
    if (
    $wp_query->is_page && isset( $wp_query->virtual_page )
    && $wp_query->virtual_page instanceof Page
    && isset( $post->is_virtual ) && $post->is_virtual
    ) {
    $plink = home_url( $wp_query->virtual_page->getUrl() );
    }
    return $plink;
    } );