| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace Photonic_Plugin\Core; |
| 3 | 4 | |
| 4 | 5 | /** |
| 5 | 6 | * Class Template |
| @@ -4,14 +5,20 @@ | ||
| 4 | 5 | /** |
| 5 | 6 | * Class Template |
| 6 | 7 | * 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 | 8 | * <code>popup='page'</code> is set |
| 8 | - * | |
| 9 | 9 | */ |
| 10 | 10 | class Template { |
| 11 | 11 | public function __construct() { |
| 12 | - add_filter('the_content', [&$this, 'load_gallery'], 100, 1); | |
| 13 | - add_filter('the_title', [&$this, 'set_title'], 10, 2); | |
| 12 | + add_filter('the_content', [&$this, 'load_gallery'], 100); | |
| 13 | + add_filter('the_title', [&$this, 'set_header_title'], 10, 2); | |
| 14 | + | |
| 15 | + /* | |
| 16 | + add_filter('wp_title', [&$this, 'set_meta_title'], 10, 3); | |
| 17 | + if (current_theme_supports('title-tag')) { | |
| 18 | + add_filter('pre_get_document_title', [&$this, 'set_meta_title'], 10, 1); | |
| 19 | + } | |
| 20 | + */ | |
| 14 | 21 | } |
| 15 | 22 | |
| 16 | 23 | /** |
| 17 | 24 | * Changes the title of the template page to the title of the album being displayed. |
| @@ -19,13 +26,15 @@ | ||
| 19 | 26 | * @param $title |
| 20 | 27 | * @param $id |
| 21 | 28 | * @return string|void |
| 22 | 29 | */ |
| 23 | - function set_title($title, $id) { | |
| 30 | + public function set_header_title($title, $id = null) { | |
| 24 | 31 | 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']); | |
| 32 | + if (!empty($id)) { | |
| 33 | + if (!empty($photonic_gallery_template_page) && is_page($photonic_gallery_template_page) && 'replace-if-available' === $photonic_page_title && absint($photonic_gallery_template_page) === absint($id)) { | |
| 34 | + if (isset($_REQUEST['photonic_gallery_title'])) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 35 | + return wptexturize(stripslashes_deep(wp_kses_post($_REQUEST['photonic_gallery_title']))); // phpcs:ignore WordPress.Security.NonceVerification | |
| 36 | + } | |
| 28 | 37 | } |
| 29 | 38 | } |
| 30 | 39 | return $title; |
| 31 | 40 | } |
| @@ -30,22 +39,52 @@ | ||
| 30 | 39 | return $title; |
| 31 | 40 | } |
| 32 | 41 | |
| 33 | 42 | /** |
| 43 | + * Changes the title of the template page to the title of the album being displayed. | |
| 44 | + * | |
| 45 | + * @param $title | |
| 46 | + * @param string $seperator | |
| 47 | + * @param string $location | |
| 48 | + * @return string|void | |
| 49 | + */ | |
| 50 | + public function set_meta_title($title, $seperator = ' – ', $location = 'right') { | |
| 51 | + global $photonic_page_meta_title, $photonic_gallery_template_page; | |
| 52 | + $id = get_queried_object_id(); | |
| 53 | + | |
| 54 | + if (!empty($_REQUEST['photonic_gallery_title']) && !empty($photonic_gallery_template_page) && is_page($photonic_gallery_template_page) && 'page' !== $photonic_page_meta_title && absint($photonic_gallery_template_page) === absint($id)) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 55 | + $album_title = wptexturize(stripslashes_deep(wp_kses_post($_REQUEST['photonic_gallery_title']))); // phpcs:ignore WordPress.Security.NonceVerification | |
| 56 | + | |
| 57 | + if ('replace-if-available' === $photonic_page_meta_title) { | |
| 58 | + return $album_title; | |
| 59 | + } | |
| 60 | + elseif ('append-if-available' === $photonic_page_meta_title) { | |
| 61 | + return $title . $seperator;// . ' - ';// . $album_title; | |
| 62 | + } | |
| 63 | + elseif ('prepend-if-available' === $photonic_page_meta_title) { | |
| 64 | + return $album_title . ' – ' . $title; | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + return $title; | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 34 | 72 | * Changes the content of the template page to have the description and contents of the gallery. |
| 35 | 73 | * |
| 36 | 74 | * @param $content |
| 37 | 75 | * @return string |
| 38 | 76 | */ |
| 39 | - function load_gallery($content) { | |
| 77 | + public function load_gallery($content): string { | |
| 40 | 78 | global $photonic_gallery_template_page; |
| 41 | 79 | if (!empty($photonic_gallery_template_page) && is_page($photonic_gallery_template_page)) { |
| 42 | - if (isset($_REQUEST['photonic_gallery'])) { | |
| 80 | + // Cannot check nonce for front-end gallery, but will vet the request fully | |
| 81 | + if (isset($_REQUEST['photonic_gallery'])) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 43 | 82 | global $photonic_alternative_shortcode; |
| 44 | 83 | |
| 45 | - $shortcode_tag = $photonic_alternative_shortcode ?: 'gallery'; | |
| 46 | - $shortcode = $_REQUEST['photonic_gallery']; | |
| 47 | - $shortcode = base64_decode($shortcode); | |
| 84 | + $shortcode_tag = esc_attr($photonic_alternative_shortcode ?: 'gallery'); | |
| 85 | + $shortcode = sanitize_text_field($_REQUEST['photonic_gallery']); // phpcs:ignore WordPress.Security.NonceVerification | |
| 86 | + $shortcode = base64_decode($shortcode); // The `encode` is defined in Core.php's get_gallery_url method. We check this in the following steps. | |
| 48 | 87 | |
| 49 | 88 | // Input is coming via a URL, so we have to ensure it is safe. |
| 50 | 89 | // The input is expected to be a Photonic shortcode, so the simplest way is to strip out all instances of the Photonic |
| 51 | 90 | // shortcode and verify that the input is blank. If it is blank, then all that the input had was a Photonic shortcode. |
| @@ -52,12 +91,12 @@ | ||
| 52 | 91 | $content_without_shortcodes = strip_shortcodes($shortcode); |
| 53 | 92 | if (!empty(trim($shortcode)) && has_shortcode($shortcode, $shortcode_tag) && empty(trim($content_without_shortcodes))) { |
| 54 | 93 | // Looks good. Let's proceed. |
| 55 | 94 | global $photonic_page_content; |
| 56 | - if ($photonic_page_content == 'replace-if-available') { | |
| 95 | + if ('replace-if-available' === $photonic_page_content) { | |
| 57 | 96 | $content = do_shortcode($shortcode); |
| 58 | 97 | } |
| 59 | - else if ($photonic_page_content == 'append-if-available') { | |
| 98 | + elseif ('append-if-available' === $photonic_page_content) { | |
| 60 | 99 | $content .= do_shortcode($shortcode); |
| 61 | 100 | } |
| 62 | 101 | } |
| 63 | 102 | else { |
| @@ -73,5 +112,5 @@ | ||
| 73 | 112 | return $content; |
| 74 | 113 | } |
| 75 | 114 | } |
| 76 | 115 | |
| 77 | -new Template(); | |
| 116 | +new Template(); | |