| 1 |
<?php |
| 2 |
namespace Photonic_Plugin; |
| 3 |
|
| 4 |
use stdClass; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class Photonic_Template |
| 8 |
* Used for cases where clicking on an album link opens a Photonic gallery on its own page. This is triggered when the shortcode attribute |
| 9 |
* <code>popup='page'</code> is set |
| 10 |
* |
| 11 |
* This functionality is work in progress and has not been tested. Hence this file is not loaded as a part of Photonic for now. |
| 12 |
* |
| 13 |
* @package Photonic_Plugin |
| 14 |
*/ |
| 15 |
class Photonic_Template { |
| 16 |
public function __construct() { |
| 17 |
// Future functionality... will be added in the next version. |
| 18 |
// add_filter('the_posts', array($this, 'check_and_load_template')); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Internally registers pages we want to fake. Array key is the slug under which it is being available from the frontend |
| 23 |
* @return mixed |
| 24 |
*/ |
| 25 |
private static function get_fake_pages() { |
| 26 |
//http://example.com/fakepage1 |
| 27 |
$fake_pages['photonic-gallery'] = array( |
| 28 |
'title' => __('Photonic Gallery', 'photonic'), |
| 29 |
'content' => 'This is dummy content in a fake page' |
| 30 |
); |
| 31 |
return $fake_pages; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Fakes get posts result |
| 36 |
* |
| 37 |
* @param $posts |
| 38 |
* |
| 39 |
* @return array|null |
| 40 |
*/ |
| 41 |
public function check_and_load_template($posts) { |
| 42 |
global $wp, $wp_query, $photonic_gallery_page; |
| 43 |
|
| 44 |
$fake_pages = self::get_fake_pages(); |
| 45 |
|
| 46 |
$gallery_page = strtolower($photonic_gallery_page); |
| 47 |
if ($gallery_page === strtolower($wp->request) || |
| 48 |
(isset($wp->query_vars['page_id']) && strtolower($wp->query_vars['page_id']) === $gallery_page)) { |
| 49 |
$posts = null; |
| 50 |
$posts[] = self::create_gallery_page($fake_pages[$gallery_page]); |
| 51 |
$wp_query->is_page = true; |
| 52 |
$wp_query->is_singular = true; |
| 53 |
$wp_query->is_home = false; |
| 54 |
$wp_query->is_archive = false; |
| 55 |
$wp_query->is_category = false; |
| 56 |
$wp_query->photonic_template = $wp->request; |
| 57 |
//Longer permalink structures may not match the fake post slug and cause a 404 error so we catch the error here |
| 58 |
unset($wp_query->query["error"]); |
| 59 |
$wp_query->query_vars["error"] = ""; |
| 60 |
$wp_query->is_404 = false; |
| 61 |
} |
| 62 |
|
| 63 |
return $posts; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Creates virtual fake page |
| 68 |
* |
| 69 |
* @param $page |
| 70 |
* |
| 71 |
* @return stdClass |
| 72 |
*/ |
| 73 |
private static function create_gallery_page($page) { |
| 74 |
global $photonic_alternative_shortcode, $photonic_gallery_page; |
| 75 |
$gallery_page = strtolower($photonic_gallery_page); |
| 76 |
$shortcode_tag = $photonic_alternative_shortcode ?: 'gallery'; |
| 77 |
$post = new stdClass; |
| 78 |
$post->post_author = 1; |
| 79 |
$post->post_name = $gallery_page; |
| 80 |
$post->guid = get_bloginfo('wpurl') . '/' . $gallery_page; |
| 81 |
|
| 82 |
if (isset($_REQUEST['photonic_gallery'])) { |
| 83 |
$shortcode = $_REQUEST['photonic_gallery']; |
| 84 |
$shortcode = base64_decode($shortcode); |
| 85 |
|
| 86 |
// Input is coming via a URL, so we have to ensure it is safe. |
| 87 |
// The input is expected to be a Photonic shortcode, so the simplest way is to strip out all instances of the Photonic |
| 88 |
// shortcode and verify that the input is blank. If it is blank, then all that the input had was a Photonic shortcode. |
| 89 |
$content_without_shortcodes = strip_shortcodes($shortcode); |
| 90 |
if (!empty(trim($shortcode)) && has_shortcode($shortcode, $shortcode_tag) && empty(trim($content_without_shortcodes))) { |
| 91 |
// Looks good. Let's proceed. |
| 92 |
$post->post_title = $page['title']; |
| 93 |
$post->post_content = $shortcode; |
| 94 |
} |
| 95 |
else { |
| 96 |
// Input looks funny. Stay safe and exit |
| 97 |
$post->post_title = esc_html__('Nothing found!', 'photonic'); |
| 98 |
$post->post_content = esc_html__('You are trying to display a gallery, but no gallery was found corresponding to the input.', 'photonic'); |
| 99 |
} |
| 100 |
} |
| 101 |
else { |
| 102 |
// Input is blank. Show nothing and exit |
| 103 |
$post->post_title = esc_html__('Nothing found!', 'photonic'); |
| 104 |
$post->post_content = esc_html__('You are trying to display a gallery, but no input was provided.', 'photonic'); |
| 105 |
} |
| 106 |
|
| 107 |
$post->ID = -1729; // Arbitrary number; picking Ramanujan's number :-) |
| 108 |
$post->post_status = 'static'; |
| 109 |
$post->comment_status = 'closed'; |
| 110 |
$post->ping_status = 'closed'; |
| 111 |
$post->comment_count = 0; |
| 112 |
$post->post_date = current_time('mysql'); |
| 113 |
$post->post_date_gmt = current_time('mysql', 1); |
| 114 |
|
| 115 |
return $post; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
new Photonic_Template(); |