| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the wp-forecast plugin for WordPress |
| 4 |
* |
| 5 |
* Copyright 2016 Hans Matzen (email : webmaster at tuxlog.de) |
| 6 |
* |
| 7 |
* @package VirtualPage |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! class_exists( 'Wpf_VirtualPage' ) ) { |
| 11 |
/** |
| 12 |
* Define class for creating virtual posts. |
| 13 |
* |
| 14 |
* This is a base class for creating virtual pages in WordPress |
| 15 |
* |
| 16 |
* @package wp-forecast |
| 17 |
*/ |
| 18 |
class Wpf_VirtualPage { |
| 19 |
/** |
| 20 |
* The Slug. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
private $slug = null; |
| 25 |
/** |
| 26 |
* The Title. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
private $title = null; |
| 31 |
/** |
| 32 |
* The Content. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
private $content = null; |
| 37 |
/** |
| 38 |
* The Author. |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
private $author = null; |
| 43 |
/** |
| 44 |
* Date of publish |
| 45 |
* |
| 46 |
* @var date |
| 47 |
*/ |
| 48 |
private $date = null; |
| 49 |
/** |
| 50 |
* Typo of post |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
private $type = null; |
| 55 |
|
| 56 |
/** |
| 57 |
* Constructor. |
| 58 |
* |
| 59 |
* @param array $args Constructor. |
| 60 |
* @throws Exception Nothing special. |
| 61 |
*/ |
| 62 |
public function __construct( $args ) { |
| 63 |
if ( ! isset( $args['slug'] ) ) { |
| 64 |
throw new Exception( 'Slug missing. Can not create virtual page' ); |
| 65 |
} |
| 66 |
|
| 67 |
$this->slug = $args['slug']; |
| 68 |
$this->title = isset( $args['title'] ) ? $args['title'] : ''; |
| 69 |
$this->content = isset( $args['content'] ) ? $args['content'] : ''; |
| 70 |
$this->author = isset( $args['author'] ) ? $args['author'] : 1; |
| 71 |
$this->date = isset( $args['date'] ) ? $args['date'] : current_time( 'mysql' ); |
| 72 |
$this->dategmt = isset( $args['date'] ) ? $args['date'] : current_time( 'mysql', 1 ); |
| 73 |
$this->type = isset( $args['type'] ) ? $args['type'] : 'page'; |
| 74 |
|
| 75 |
add_filter( 'the_posts', array( &$this, 'virtual_page' ) ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Filter to create virtual page content. |
| 80 |
* |
| 81 |
* @param array $posts Parameter array for the virtual page. |
| 82 |
*/ |
| 83 |
public function virtual_page( $posts ) { |
| 84 |
global $wp, $wp_query; |
| 85 |
|
| 86 |
if ( count( $posts ) === 0 && |
| 87 |
( strcasecmp( $wp->request, $this->slug ) === 0 || $wp->query_vars['page_id'] === $this->slug ) ) { |
| 88 |
// create a fake post intance. |
| 89 |
$post = new stdClass(); |
| 90 |
// fill properties of $post with everything a page in the database would have. |
| 91 |
$post->ID = -1; // use an illegal value for page ID. |
| 92 |
$post->post_author = $this->author; // post author id. |
| 93 |
$post->post_date = $this->date; // date of post. |
| 94 |
$post->post_date_gmt = $this->dategmt; |
| 95 |
$post->post_content = $this->content; |
| 96 |
$post->post_title = $this->title; |
| 97 |
$post->post_excerpt = ''; |
| 98 |
$post->post_status = 'publish'; |
| 99 |
$post->comment_status = 'closed'; // mark as closed for comments, since page doesn't exist. |
| 100 |
$post->ping_status = 'closed'; // mark as closed for pings, since page doesn't exist. |
| 101 |
$post->post_password = ''; // no password. |
| 102 |
$post->post_name = $this->slug; |
| 103 |
$post->to_ping = ''; |
| 104 |
$post->pinged = ''; |
| 105 |
$post->modified = $post->post_date; |
| 106 |
$post->modified_gmt = $post->post_date_gmt; |
| 107 |
$post->post_content_filtered = ''; |
| 108 |
$post->post_parent = 0; |
| 109 |
$post->guid = get_home_url( '/' . $this->slug ); |
| 110 |
$post->menu_order = 0; |
| 111 |
$post->post_tyle = $this->type; |
| 112 |
$post->post_mime_type = ''; |
| 113 |
$post->comment_count = 0; |
| 114 |
|
| 115 |
// set filter results. |
| 116 |
$posts = array( $post ); |
| 117 |
|
| 118 |
// reset wp_query properties to simulate a found page. |
| 119 |
$wp_query->is_page = true; |
| 120 |
$wp_query->is_singular = true; |
| 121 |
$wp_query->is_home = false; |
| 122 |
$wp_query->is_archive = false; |
| 123 |
$wp_query->is_category = false; |
| 124 |
unset( $wp_query->query['error'] ); |
| 125 |
$wp_query->query_vars['error'] = ''; |
| 126 |
$wp_query->is_404 = false; |
| 127 |
} |
| 128 |
|
| 129 |
return ( $posts ); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|