| 1 |
<?php |
| 2 |
namespace Photonic_Plugin\Core; |
| 3 |
|
| 4 |
/** |
| 5 |
* Class Template |
| 6 |
* Used for cases where clicking on an album link opens a Photonic gallery on its own page. This is triggered when the shortcode attribute |
| 7 |
* <code>popup='page'</code> is set |
| 8 |
* |
| 9 |
*/ |
| 10 |
class Template { |
| 11 |
public function __construct() { |
| 12 |
add_filter('the_content', [&$this, 'load_gallery'], 100, 1); |
| 13 |
add_filter('the_title', [&$this, 'set_title'], 10, 2); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Changes the title of the template page to the title of the album being displayed. |
| 18 |
* |
| 19 |
* @param $title |
| 20 |
* @param $id |
| 21 |
* @return string|void |
| 22 |
*/ |
| 23 |
function set_title($title, $id) { |
| 24 |
global $photonic_page_title, $photonic_gallery_template_page; |
| 25 |
if (!empty($photonic_gallery_template_page) && is_page($photonic_gallery_template_page) && $photonic_page_title == 'replace-if-available' && $id == $photonic_gallery_template_page) { |
| 26 |
if (isset($_REQUEST['photonic_gallery_title'])) { |
| 27 |
return esc_attr($_REQUEST['photonic_gallery_title']); |
| 28 |
} |
| 29 |
} |
| 30 |
return $title; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Changes the content of the template page to have the description and contents of the gallery. |
| 35 |
* |
| 36 |
* @param $content |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
function load_gallery($content) { |
| 40 |
global $photonic_gallery_template_page; |
| 41 |
if (!empty($photonic_gallery_template_page) && is_page($photonic_gallery_template_page)) { |
| 42 |
if (isset($_REQUEST['photonic_gallery'])) { |
| 43 |
global $photonic_alternative_shortcode; |
| 44 |
|
| 45 |
$shortcode_tag = $photonic_alternative_shortcode ?: 'gallery'; |
| 46 |
$shortcode = $_REQUEST['photonic_gallery']; |
| 47 |
$shortcode = base64_decode($shortcode); |
| 48 |
|
| 49 |
// Input is coming via a URL, so we have to ensure it is safe. |
| 50 |
// The input is expected to be a Photonic shortcode, so the simplest way is to strip out all instances of the Photonic |
| 51 |
// shortcode and verify that the input is blank. If it is blank, then all that the input had was a Photonic shortcode. |
| 52 |
$content_without_shortcodes = strip_shortcodes($shortcode); |
| 53 |
if (!empty(trim($shortcode)) && has_shortcode($shortcode, $shortcode_tag) && empty(trim($content_without_shortcodes))) { |
| 54 |
// Looks good. Let's proceed. |
| 55 |
global $photonic_page_content; |
| 56 |
if ($photonic_page_content == 'replace-if-available') { |
| 57 |
$content = do_shortcode($shortcode); |
| 58 |
} |
| 59 |
else if ($photonic_page_content == 'append-if-available') { |
| 60 |
$content .= do_shortcode($shortcode); |
| 61 |
} |
| 62 |
} |
| 63 |
else { |
| 64 |
// Input looks funny. Stay safe and exit |
| 65 |
$content .= esc_html__('You are trying to display a gallery, but no gallery was found corresponding to the input.', 'photonic'); |
| 66 |
} |
| 67 |
} |
| 68 |
else { |
| 69 |
// Input is blank. Show nothing and exit |
| 70 |
$content .= esc_html__('You are trying to display a gallery, but no input was provided.', 'photonic'); |
| 71 |
} |
| 72 |
} |
| 73 |
return $content; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
new Template(); |