| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace RyanHellyer\UniqueHeaders; |
| 6 |
|
| 7 |
use RyanHellyer\UniqueHeaders\Vendor\Inpsyde\Modularity\Module\ExecutableModule; |
| 8 |
use RyanHellyer\UniqueHeaders\Vendor\Psr\Container\ContainerInterface; |
| 9 |
|
| 10 |
class DisplayModule implements ExecutableModule |
| 11 |
{ |
| 12 |
private AttachmentHelper $attachmentHelper; |
| 13 |
private string $name = 'custom-header-image'; |
| 14 |
private string $nameUnderscores; |
| 15 |
|
| 16 |
/** @var array<string, string> */ |
| 17 |
private array $taxonomies = []; |
| 18 |
|
| 19 |
public function __construct(AttachmentHelper $attachmentHelper) |
| 20 |
{ |
| 21 |
$this->attachmentHelper = $attachmentHelper; |
| 22 |
$this->nameUnderscores = str_replace('-', '_', $this->name); |
| 23 |
} |
| 24 |
|
| 25 |
public function id(): string |
| 26 |
{ |
| 27 |
return 'unique-headers-display'; |
| 28 |
} |
| 29 |
|
| 30 |
public function run(ContainerInterface $container): bool |
| 31 |
{ |
| 32 |
add_filter('theme_mod_header_image', [$this, 'postHeaderImageFilter'], 20); |
| 33 |
add_filter('wp_calculate_image_srcset', [$this, 'headerSrcsetFilter'], 20, 5); |
| 34 |
add_filter('theme_mod_header_image_data', [$this, 'postModifyHeaderImageData']); |
| 35 |
|
| 36 |
if (function_exists('get_term_meta')) { |
| 37 |
$this->taxonomies = get_taxonomies(['public' => true]); |
| 38 |
add_action('admin_init', [$this, 'addTaxonomyFields']); |
| 39 |
add_filter('theme_mod_header_image', [$this, 'taxonomyHeaderImageFilter'], 5); |
| 40 |
add_filter('theme_mod_header_image_data', [$this, 'taxonomyModifyHeaderImageData']); |
| 41 |
} |
| 42 |
|
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
public function postHeaderImageFilter(string $url): string |
| 47 |
{ |
| 48 |
if (!is_single() && !is_page() && !is_home()) { |
| 49 |
return $url; |
| 50 |
} |
| 51 |
|
| 52 |
$postId = is_home() ? (int) get_option('page_for_posts') : (int) get_the_ID(); |
| 53 |
$attachmentId = $this->attachmentHelper->getId($postId, $this->nameUnderscores); |
| 54 |
|
| 55 |
if ($attachmentId) { |
| 56 |
$url = $this->attachmentHelper->getSrc($attachmentId); |
| 57 |
} |
| 58 |
|
| 59 |
return $url; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @param object|null $data |
| 64 |
* @return object|null |
| 65 |
*/ |
| 66 |
public function postModifyHeaderImageData($data) |
| 67 |
{ |
| 68 |
if (!is_single() && !is_page() && !is_home()) { |
| 69 |
return $data; |
| 70 |
} |
| 71 |
|
| 72 |
$postId = is_home() ? (int) get_option('page_for_posts') : (int) get_the_ID(); |
| 73 |
|
| 74 |
return $this->setAttachmentData($data, $this->attachmentHelper->getId($postId, $this->nameUnderscores)); |
| 75 |
} |
| 76 |
|
| 77 |
public function taxonomyHeaderImageFilter(string $url): string |
| 78 |
{ |
| 79 |
$taxId = $this->getCurrentTaxonomyId(); |
| 80 |
if (!$taxId) { |
| 81 |
return $url; |
| 82 |
} |
| 83 |
|
| 84 |
$attachmentId = $this->getTaxonomyAttachmentId($taxId); |
| 85 |
if ($attachmentId) { |
| 86 |
return esc_url($this->attachmentHelper->getSrc($attachmentId)); |
| 87 |
} |
| 88 |
|
| 89 |
$legacy = get_metadata('taxonomy', $taxId, 'taxonomy-header-image', true); |
| 90 |
if (is_string($legacy) && $legacy !== '') { |
| 91 |
return esc_url($legacy); |
| 92 |
} |
| 93 |
|
| 94 |
return $url; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @param object|null $data |
| 99 |
* @return object|null |
| 100 |
*/ |
| 101 |
public function taxonomyModifyHeaderImageData($data) |
| 102 |
{ |
| 103 |
if (!is_tag() && !is_tax() && !is_category()) { |
| 104 |
return $data; |
| 105 |
} |
| 106 |
|
| 107 |
$taxId = $this->getCurrentTaxonomyId(); |
| 108 |
if (!$taxId) { |
| 109 |
return $data; |
| 110 |
} |
| 111 |
|
| 112 |
return $this->setAttachmentData($data, $this->getTaxonomyAttachmentId($taxId)); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @param array<int, array{url: string}>|string $srcset |
| 117 |
* @param array<int, int> $sizeArray |
| 118 |
* @param string $imageSrc |
| 119 |
* @param array<string, mixed> $imageMeta |
| 120 |
* @param int $attachmentId |
| 121 |
* @return array<int, array{url: string}>|string |
| 122 |
*/ |
| 123 |
public function headerSrcsetFilter($srcset, $sizeArray, $imageSrc, $imageMeta, int $attachmentId = 0) |
| 124 |
{ |
| 125 |
if ( |
| 126 |
!isset(get_custom_header()->attachment_id) |
| 127 |
|| get_custom_header()->attachment_id !== $attachmentId |
| 128 |
) { |
| 129 |
return $srcset; |
| 130 |
} |
| 131 |
|
| 132 |
if (!is_array($srcset)) { |
| 133 |
return $srcset; |
| 134 |
} |
| 135 |
|
| 136 |
$currentUrl = $this->getCurrentHeaderUrl(); |
| 137 |
if ($currentUrl === '') { |
| 138 |
return $srcset; |
| 139 |
} |
| 140 |
|
| 141 |
foreach ($srcset as $size => $set) { |
| 142 |
$srcset[$size]['url'] = $currentUrl; |
| 143 |
} |
| 144 |
|
| 145 |
return $srcset; |
| 146 |
} |
| 147 |
|
| 148 |
public function addTaxonomyFields(): void |
| 149 |
{ |
| 150 |
if (!is_admin()) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
foreach ($this->taxonomies as $taxonomy) { |
| 155 |
add_action($taxonomy . '_edit_form_fields', [$this, 'extraFields'], 1); |
| 156 |
add_action('edit_' . $taxonomy, [$this, 'storeTaxonomyData']); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
public function storeTaxonomyData(): void |
| 161 |
{ |
| 162 |
if ( |
| 163 |
!isset($_POST[$this->name . '-nonce']) |
| 164 |
|| !isset($_POST[$this->name . '-id']) |
| 165 |
|| !isset($_POST['tag_ID']) |
| 166 |
) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
$nonce = sanitize_key(wp_unslash($_POST[$this->name . '-nonce'])); |
| 171 |
if (!wp_verify_nonce($nonce, $this->name)) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
$tagId = absint(wp_unslash($_POST['tag_ID'])); |
| 176 |
|
| 177 |
if (!current_user_can('edit_term', $tagId)) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
$attachmentId = absint(wp_unslash($_POST[$this->name . '-id'])); |
| 182 |
|
| 183 |
update_term_meta($tagId, 'taxonomy-header-image', $attachmentId); |
| 184 |
} |
| 185 |
|
| 186 |
public function extraFields(\WP_Term $term): void |
| 187 |
{ |
| 188 |
$tagId = $term->term_id; |
| 189 |
$attachmentId = $this->getTaxonomyAttachmentId($tagId); |
| 190 |
|
| 191 |
if ($attachmentId) { |
| 192 |
$url = $this->attachmentHelper->getSrc($attachmentId); |
| 193 |
$title = $this->attachmentHelper->getTitle($attachmentId); |
| 194 |
} else { |
| 195 |
$legacy = get_metadata('taxonomy', $tagId, 'taxonomy-header-image', true); |
| 196 |
$url = is_string($legacy) ? $legacy : ''; |
| 197 |
$title = ''; |
| 198 |
$attachmentId = $legacy; |
| 199 |
} |
| 200 |
|
| 201 |
$name = $this->name; |
| 202 |
?> |
| 203 |
<tr valign="top"> |
| 204 |
<th scope="row"><?php echo esc_html__('Upload header image', 'unique-headers'); ?></th> |
| 205 |
<td> |
| 206 |
<div id="unique-header" class="postbox"> |
| 207 |
<div class="inside"> |
| 208 |
<?php require __DIR__ . '/../views/meta-box.php'; ?> |
| 209 |
</div> |
| 210 |
</div> |
| 211 |
</td> |
| 212 |
</tr> |
| 213 |
<?php |
| 214 |
} |
| 215 |
|
| 216 |
private function getCurrentHeaderUrl(): string |
| 217 |
{ |
| 218 |
$postId = 0; |
| 219 |
|
| 220 |
if (is_home()) { |
| 221 |
$postId = (int) get_option('page_for_posts'); |
| 222 |
} elseif (is_single() || is_page()) { |
| 223 |
$postId = (int) get_the_ID(); |
| 224 |
} |
| 225 |
|
| 226 |
if ($postId) { |
| 227 |
$attachmentId = $this->attachmentHelper->getId($postId, $this->nameUnderscores); |
| 228 |
if ($attachmentId) { |
| 229 |
return $this->attachmentHelper->getSrc($attachmentId); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
$taxId = $this->getCurrentTaxonomyId(); |
| 234 |
if ($taxId) { |
| 235 |
$attachmentId = $this->getTaxonomyAttachmentId($taxId); |
| 236 |
if ($attachmentId) { |
| 237 |
return $this->attachmentHelper->getSrc($attachmentId); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
return ''; |
| 242 |
} |
| 243 |
|
| 244 |
private function getCurrentTaxonomyId(): int |
| 245 |
{ |
| 246 |
if (is_category()) { |
| 247 |
return (int) get_query_var('cat'); |
| 248 |
} |
| 249 |
|
| 250 |
if (is_tag() || is_tax()) { |
| 251 |
foreach ($this->taxonomies as $taxonomy) { |
| 252 |
if ($taxonomy === 'category') { |
| 253 |
continue; |
| 254 |
} |
| 255 |
|
| 256 |
$slug = $taxonomy === 'post_tag' ? get_query_var('tag') : get_query_var($taxonomy); |
| 257 |
|
| 258 |
if ($slug) { |
| 259 |
$term = get_term_by('slug', $slug, $taxonomy); |
| 260 |
if (isset($term->term_id)) { |
| 261 |
return (int) $term->term_id; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
private function getTaxonomyAttachmentId(int $taxId): int |
| 271 |
{ |
| 272 |
$attachmentId = get_term_meta($taxId, 'taxonomy-header-image', true); |
| 273 |
|
| 274 |
if (is_numeric($attachmentId)) { |
| 275 |
return (int) $attachmentId; |
| 276 |
} |
| 277 |
|
| 278 |
$legacy = get_metadata('taxonomy', $taxId, 'taxonomy-header-image', true); |
| 279 |
|
| 280 |
return is_numeric($legacy) ? (int) $legacy : 0; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* @param object|null $data |
| 285 |
* @return object|null |
| 286 |
*/ |
| 287 |
private function setAttachmentData($data, int $attachmentId) |
| 288 |
{ |
| 289 |
if (!$attachmentId) { |
| 290 |
return $data; |
| 291 |
} |
| 292 |
|
| 293 |
if ($data === null) { |
| 294 |
$data = (object) null; |
| 295 |
} |
| 296 |
|
| 297 |
if (is_object($data)) { |
| 298 |
$data->attachment_id = $attachmentId; |
| 299 |
$data->width = $this->attachmentHelper->getDimensions($attachmentId, 'width'); |
| 300 |
$data->height = $this->attachmentHelper->getDimensions($attachmentId, 'height'); |
| 301 |
} |
| 302 |
|
| 303 |
return $data; |
| 304 |
} |
| 305 |
} |
| 306 |
|