| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file contains the class in charge of our ghost posts. |
| 4 |
* |
| 5 |
* This is based on the IT_Exchange_Casper class in iThemes Exchange. |
| 6 |
* |
| 7 |
* @version 1.0.0 |
| 8 |
* @package Charitable/Classes/Charitable_Ghost_Page |
| 9 |
* @author David Bisset |
| 10 |
* @copyright Copyright (c) 2022, WP Charitable LLC |
| 11 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 12 |
*/ |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
if ( ! class_exists( 'Charitable_Ghost_Page' ) ) : |
| 20 |
|
| 21 |
/** |
| 22 |
* Charitable_Ghost_Page class. |
| 23 |
* |
| 24 |
* It is used when viewing a frontend Charitable view other than a campaign. |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
*/ |
| 28 |
class Charitable_Ghost_Page { |
| 29 |
|
| 30 |
/** |
| 31 |
* @var string $current_view The current Charitable frontend view. Should never be campaign. |
| 32 |
* @since 1.0.0 |
| 33 |
*/ |
| 34 |
private $current_view; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var array $args |
| 38 |
* @since 1.0.0 |
| 39 |
*/ |
| 40 |
private $args; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var WP_Query $wp_query |
| 44 |
* @since 1.0.0 |
| 45 |
*/ |
| 46 |
private $wp_query; |
| 47 |
|
| 48 |
/** |
| 49 |
* Constructor. Sets $current_view and $wp_query properties. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* |
| 53 |
* @param string $current_view |
| 54 |
* @param array $args |
| 55 |
*/ |
| 56 |
public function __construct( $current_view, $args = array() ) { |
| 57 |
if ( 'campaign' == $current_view ) { |
| 58 |
throw new Exception( 'Charitable_Ghost_Page should not be constructed when $current_view is "campaign".' ); |
| 59 |
} |
| 60 |
|
| 61 |
$this->current_view = $current_view; |
| 62 |
|
| 63 |
$defaults = array( |
| 64 |
'title' => '', |
| 65 |
'content' => '' |
| 66 |
); |
| 67 |
|
| 68 |
$this->args = wp_parse_args( $args, $defaults ); |
| 69 |
$this->modify_wp_query(); |
| 70 |
|
| 71 |
/* Remove wpautop filter on Ghost pages */ |
| 72 |
remove_filter( 'the_content', 'wpautop' ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Modifies the WP Query. |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
private function modify_wp_query() { |
| 83 |
$wp_query = $GLOBALS['wp_query']; |
| 84 |
|
| 85 |
$wp_query->posts_per_page = 1; |
| 86 |
$wp_query->nopaging = true; |
| 87 |
$wp_query->post_count = 1; |
| 88 |
|
| 89 |
// If we don't have a post, load an empty one |
| 90 |
if ( empty( $wp_query->post ) ) { |
| 91 |
$wp_query->post = new WP_Post( new stdClass() ); |
| 92 |
} |
| 93 |
|
| 94 |
$wp_query->post->ID = 0; |
| 95 |
$wp_query->post->post_date = current_time( 'mysql' ); |
| 96 |
$wp_query->post->post_date_gmt = current_time( 'mysql', 1 ); |
| 97 |
$wp_query->post->post_content = $this->args['content']; |
| 98 |
$wp_query->post->post_title = $this->args['title']; |
| 99 |
$wp_query->post->post_excerpt = ''; |
| 100 |
$wp_query->post->post_status = 'publish'; |
| 101 |
$wp_query->post->comment_status = false; |
| 102 |
$wp_query->post->ping_status = false; |
| 103 |
$wp_query->post->post_password = ''; |
| 104 |
$wp_query->post->post_name = 'charitable-ghost-' . $this->current_view; |
| 105 |
$wp_query->post->to_ping = ''; |
| 106 |
$wp_query->post->pinged = ''; |
| 107 |
$wp_query->post->post_modified = $wp_query->post->post_date; |
| 108 |
$wp_query->post->post_modified_gmt = $wp_query->post->post_date_gmt; |
| 109 |
$wp_query->post->post_content_filtered = ''; |
| 110 |
$wp_query->post->post_parent = 0; |
| 111 |
$wp_query->post->guid = get_home_url() . '/' . $this->get_guid(); |
| 112 |
$wp_query->post->menu_order = 0; |
| 113 |
$wp_query->post->post_type = 'page'; |
| 114 |
$wp_query->post->post_mime_type = ''; |
| 115 |
$wp_query->post->comment_count = 0; |
| 116 |
$wp_query->post->filter = 'raw'; |
| 117 |
|
| 118 |
$wp_query->posts = array( $wp_query->post ); |
| 119 |
$wp_query->found_posts = 1; |
| 120 |
$wp_query->is_single = false; //false -- so comments_template() doesn't add comments |
| 121 |
$wp_query->is_preview = false; |
| 122 |
$wp_query->is_page = true; //false -- so comments_template() doesn't add comments |
| 123 |
$wp_query->is_archive = false; |
| 124 |
$wp_query->is_date = false; |
| 125 |
$wp_query->is_year = false; |
| 126 |
$wp_query->is_month = false; |
| 127 |
$wp_query->is_day = false; |
| 128 |
$wp_query->is_time = false; |
| 129 |
$wp_query->is_author = false; |
| 130 |
$wp_query->is_category = false; |
| 131 |
$wp_query->is_tag = false; |
| 132 |
$wp_query->is_tax = false; |
| 133 |
$wp_query->is_search = false; |
| 134 |
$wp_query->is_feed = false; |
| 135 |
$wp_query->is_comment_feed = false; |
| 136 |
$wp_query->is_trackback = false; |
| 137 |
$wp_query->is_home = false; |
| 138 |
$wp_query->is_404 = false; |
| 139 |
$wp_query->is_comments_popup = false; |
| 140 |
$wp_query->is_paged = false; |
| 141 |
$wp_query->is_admin = false; |
| 142 |
$wp_query->is_attachment = false; |
| 143 |
$wp_query->is_singular = true; |
| 144 |
$wp_query->is_posts_page = false; |
| 145 |
$wp_query->is_post_type_archive = false; |
| 146 |
|
| 147 |
$GLOBALS['wp_query'] = $wp_query; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Creates a guid based on the current view. |
| 152 |
* |
| 153 |
* @since 1.0.0 |
| 154 |
* |
| 155 |
* @return string |
| 156 |
*/ |
| 157 |
private function get_guid() { |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
endif; |