|
1
|
|
|
<?php # -*- coding: utf-8 -*- |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace MultisiteGlobalMedia; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class Thumbnail |
|
8
|
|
|
*/ |
|
9
|
|
|
class Thumbnail |
|
10
|
|
|
{ |
|
11
|
|
|
use Helper; |
|
12
|
|
|
|
|
13
|
|
|
const META_KEY_THUMBNAIL_ID = '_thumbnail_id'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var Site |
|
17
|
|
|
*/ |
|
18
|
|
|
private $site; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var SingleSwitcher |
|
22
|
|
|
*/ |
|
23
|
|
|
private $siteSwitcher; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Thumbnail constructor |
|
27
|
|
|
* |
|
28
|
|
|
* @param Site $site |
|
29
|
|
|
* @param SingleSwitcher $siteSwitcher |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Site $site, SingleSwitcher $siteSwitcher) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->site = $site; |
|
34
|
|
|
$this->siteSwitcher = $siteSwitcher; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Fires once a post has been saved. |
|
39
|
|
|
* |
|
40
|
|
|
* @since 1.5.0 |
|
41
|
|
|
* |
|
42
|
|
|
* @param int $postId Post ID. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function saveThumbnailMeta(int $postId) |
|
45
|
|
|
{ |
|
46
|
|
|
$idPrefix = $this->site->idSitePrefix(); |
|
47
|
|
|
|
|
48
|
|
|
$attachmentId = (int)filter_input( |
|
49
|
|
|
INPUT_POST, |
|
50
|
|
|
self::META_KEY_THUMBNAIL_ID, |
|
51
|
|
|
FILTER_SANITIZE_NUMBER_INT |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
if (!$attachmentId) { |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($attachmentId && $this->idPrefixIncludedInAttachmentId($attachmentId, $idPrefix)) { |
|
59
|
|
|
update_post_meta($postId, self::META_KEY_THUMBNAIL_ID, $attachmentId); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Ajax handler for retrieving HTML for the featured image. |
|
65
|
|
|
* |
|
66
|
|
|
* @since 4.6.0 |
|
67
|
|
|
* |
|
68
|
|
|
* @param int $postId |
|
69
|
|
|
* @param int $attachmentId |
|
70
|
|
|
*/ |
|
71
|
|
|
public function ajaxGetPostThumbnailHtml(int $postId, int $attachmentId) |
|
72
|
|
|
{ |
|
73
|
|
|
$idPrefix = $this->site->idSitePrefix(); |
|
74
|
|
|
|
|
75
|
|
|
$return = _wp_post_thumbnail_html($attachmentId, $postId); |
|
76
|
|
|
|
|
77
|
|
|
if (!$this->idPrefixIncludedInAttachmentId($attachmentId, $idPrefix)) { |
|
78
|
|
|
wp_send_json_success($return); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$attachmentId = $this->stripSiteIdPrefixFromAttachmentId($idPrefix, $attachmentId); |
|
82
|
|
|
|
|
83
|
|
|
$this->siteSwitcher->switchToBlog($this->site->id()); |
|
84
|
|
|
$return = _wp_post_thumbnail_html($attachmentId, $postId); |
|
85
|
|
|
$this->siteSwitcher->restoreBlog(); |
|
86
|
|
|
|
|
87
|
|
|
$post = get_post($postId); |
|
88
|
|
|
$postTypeObject = get_post_type_object($post->post_type); |
|
89
|
|
|
|
|
90
|
|
|
$return = $this->replaceRemovePostThumbnailMarkup( |
|
91
|
|
|
esc_html($postTypeObject->labels->remove_featured_image), |
|
92
|
|
|
$return |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
wp_send_json_success($return); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Filters the admin post thumbnail HTML markup to return. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $content Admin post thumbnail HTML markup. |
|
102
|
|
|
* @param int $postId Post ID. |
|
103
|
|
|
* @param string|int $attachmentId Thumbnail ID. |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
* |
|
107
|
|
|
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType |
|
108
|
|
|
*/ |
|
109
|
|
|
public function adminPostThumbnailHtml(string $content, int $postId, $attachmentId): string |
|
110
|
|
|
{ |
|
111
|
|
|
// phpcs:enable |
|
112
|
|
|
|
|
113
|
|
|
$attachmentId = (int)$attachmentId; |
|
114
|
|
|
$idPrefix = $this->site->idSitePrefix(); |
|
115
|
|
|
|
|
116
|
|
|
if (false === $this->idPrefixIncludedInAttachmentId($attachmentId, $idPrefix)) { |
|
117
|
|
|
return $content; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$post = get_post($postId); |
|
121
|
|
|
$attachmentId = $this->stripSiteIdPrefixFromAttachmentId($idPrefix, $attachmentId); |
|
122
|
|
|
|
|
123
|
|
|
$this->siteSwitcher->switchToBlog($this->site->id()); |
|
124
|
|
|
// $thumbnailId is passed instead of postId to avoid warning messages of nonexistent post object. |
|
125
|
|
|
$content = _wp_post_thumbnail_html($attachmentId, $post); |
|
126
|
|
|
$this->siteSwitcher->restoreBlog(); |
|
127
|
|
|
|
|
128
|
|
|
$search = 'value="' . $attachmentId . '"'; |
|
129
|
|
|
$replace = 'value="' . $idPrefix . $attachmentId . '"'; |
|
130
|
|
|
$content = str_replace($search, $replace, $content); |
|
131
|
|
|
|
|
132
|
|
|
$post = get_post($postId); |
|
133
|
|
|
$postTypeObject = null; |
|
134
|
|
|
|
|
135
|
|
|
$removeImageLabel = _x('Remove featured image', 'post', 'multisite-global-media'); |
|
136
|
|
|
if ($post !== null) { |
|
137
|
|
|
$postTypeObject = get_post_type_object($post->post_type); |
|
138
|
|
|
} |
|
139
|
|
|
if ($postTypeObject !== null) { |
|
140
|
|
|
$removeImageLabel = $postTypeObject->labels->remove_featured_image; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $this->replaceRemovePostThumbnailMarkup( |
|
144
|
|
|
$removeImageLabel, |
|
145
|
|
|
$content |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Filters the post thumbnail HTML. |
|
151
|
|
|
* |
|
152
|
|
|
* @since 2.9.0 |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $html The post thumbnail HTML. |
|
155
|
|
|
* @param int $postId The post ID. |
|
156
|
|
|
* @param string $attachmentId The post thumbnail ID. |
|
157
|
|
|
* @param string|array $size The post thumbnail size. Image size or array of width and height |
|
158
|
|
|
* values (in that order). Default 'post-thumbnail'. |
|
159
|
|
|
* @param string $attr Query string of attributes. |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
* |
|
163
|
|
|
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType |
|
164
|
|
|
*/ |
|
165
|
|
|
public function postThumbnailHtml( |
|
166
|
|
|
string $html, |
|
167
|
|
|
int $postId, |
|
168
|
|
|
string $attachmentId, |
|
169
|
|
|
$size, |
|
170
|
|
|
$attr |
|
171
|
|
|
): string { |
|
172
|
|
|
|
|
173
|
|
|
// phpcs:enable |
|
174
|
|
|
|
|
175
|
|
|
$attachmentId = (int)$attachmentId; |
|
176
|
|
|
$siteId = $this->site->id(); |
|
177
|
|
|
$idPrefix = $this->site->idSitePrefix(); |
|
178
|
|
|
|
|
179
|
|
|
if ($this->idPrefixIncludedInAttachmentId($attachmentId, $idPrefix)) { |
|
180
|
|
|
$attachmentId = $this->stripSiteIdPrefixFromAttachmentId($idPrefix, $attachmentId); |
|
181
|
|
|
$this->siteSwitcher->switchToBlog($siteId); |
|
182
|
|
|
$html = wp_get_attachment_image($attachmentId, $size, false, $attr); |
|
183
|
|
|
$this->siteSwitcher->restoreBlog(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $html; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Replace the remove post thumbnail markup with the image or without |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $replace |
|
193
|
|
|
* @param string $subject |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
|
|
private function replaceRemovePostThumbnailMarkup(string $replace, string $subject): string |
|
197
|
|
|
{ |
|
198
|
|
|
$search = '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail"></a></p>'; |
|
199
|
|
|
$replace = sprintf( |
|
200
|
|
|
'<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">%s</a></p>', |
|
201
|
|
|
$replace |
|
202
|
|
|
); |
|
203
|
|
|
|
|
204
|
|
|
return str_replace($search, $replace, $subject); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|