-
Notifications
You must be signed in to change notification settings - Fork 34
/
exporter-helper.php
217 lines (183 loc) · 6.73 KB
/
exporter-helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Campaign Exporter Helper Functions
*
* @package P4MT
*/
/**
* Parse the post content and return all attachment ids used in blocks.
*
* @param string $content The content to parse.
* @return array All attachments used in the blocks.
*/
function get_attachments_used_in_content(string $content): array
{
$blocks = parse_blocks($content);
$attachment_ids = [];
foreach ($blocks as $block) {
// Fetch the attachement id/s from block fields.
switch ($block['blockName']) {
case 'core/media-text':
$attachment_ids[] = $block['attrs']['mediaId'] ?? '';
$attachment_ids[] = $block['attrs']['mediaLink'] ?? '';
$attachment_ids[] = $block['attrs']['mediaType'] ?? '';
break;
case 'core/image':
case 'planet4-blocks/happypoint':
$attachment_ids[] = $block['attrs']['id'] ?? '';
break;
case 'planet4-blocks/gallery':
if (isset($block['attrs']['multiple_image'])) {
$multiple_images = explode(',', $block['attrs']['multiple_image']);
$attachment_ids = array_merge($attachment_ids, $multiple_images);
}
break;
case 'planet4-blocks/carousel-header':
if (isset($block['attrs']['slides'])) {
foreach ($block['attrs']['slides'] as $slide) {
$attachment_ids[] = $slide['image'];
}
}
break;
case 'planet4-blocks/columns':
if (isset($block['attrs']['columns'])) {
foreach ($block['attrs']['columns'] as $column) {
$attachment_ids[] = $column['attachment'] ?? '';
}
}
break;
case 'planet4-blocks/take-action-boxout':
$attachment_ids[] = $block['attrs']['background_image'] ?? '';
break;
}
}
return $attachment_ids;
}
/**
* Returns all attachment ids from campaign post content.
*
* @param array $post_ids Post IDs.
* @return array $post_ids Post IDs.
*/
function get_campaign_attachments(array $post_ids): array
{
global $wpdb;
if (empty($post_ids)) {
return [];
}
$sql = '
SELECT id
FROM %1$s
WHERE post_type = \'attachment\'
AND post_parent IN (' . generate_list_placeholders($post_ids, 2) . ')';
$prepared_sql = $wpdb->prepare(
$sql, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
array_merge([ $wpdb->posts ], $post_ids)
);
$results = $wpdb->get_results($prepared_sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$attachment_ids = array_map(
function ($result) {
return $result->id;
},
$results
);
/**
* Post thumbnails
*/
$sql = '
SELECT meta_value
FROM %1$s
WHERE ( meta_key = \'_thumbnail_id\' OR meta_key = \'background_image_id\' )
AND post_id IN(' . generate_list_placeholders($post_ids, 2) . ')';
$prepared_sql = $wpdb->prepare(
$sql, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
array_merge([ $wpdb->postmeta ], $post_ids)
);
$results = $wpdb->get_results($prepared_sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$attachment_ids = array_merge(
$attachment_ids,
array_map(
function ($result) {
return $result->meta_value;
},
$results
)
);
$sql = '
SELECT post_content
FROM %1$s
WHERE ID IN(' . generate_list_placeholders($post_ids, 2) . ')
AND post_content REGEXP \'((wp-image-|wp-att-)[0-9][0-9]*)|gallery_block_style|wp\:planet4\-blocks|href=|src=\'';// phpcs:ignore Generic.Files.LineLength.MaxExceeded
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$prepared_sql = $wpdb->prepare($sql, array_merge([ $wpdb->posts ], $post_ids));
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$results = $wpdb->get_results($prepared_sql);
foreach ((array) $results as $text) {
$text = $text->post_content;
$attachment_ids = array_merge($attachment_ids, get_attachments_used_in_content($text));
}
$attachment_ids = array_unique($attachment_ids);
sort($attachment_ids);
// The post ids are reordered as sort all attachment ids first
// and then append the post id to array.(Added for simplification of import process).
$attachment_ids = array_diff($attachment_ids, $post_ids);
$post_ids = array_merge($attachment_ids, $post_ids);
return $post_ids;
}
/**
* Wrap strings in nested CDATA tags.
*
* @param string|null $str String to replace.
*/
function p4_px_single_post_cdata(?string $str): string
{
if (seems_utf8($str) === false) {
$str = mb_convert_encoding($str, 'UTF-8');
}
$str = '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $str) . ']]>';
return $str;
}
/**
* Get the site url.
*/
function p4_px_single_post_site_url(): string
{
if (is_multisite()) {
return network_home_url();
}
return get_bloginfo_rss('url');
}
/**
* Get the Campaign authors.
*
* @param array $post_ids Tag object.
*/
function p4_px_single_post_authors_list(array $post_ids): void
{
global $wpdb;
$post_ids = array_map('intval', $post_ids); // santize the post_ids manually.
$post_ids = array_filter($post_ids); // strip ones that didn't validate.
$authors = [];
$sql = 'SELECT DISTINCT post_author
FROM %1$s
WHERE ID IN(' . generate_list_placeholders($post_ids, 2) . ') AND post_status != \'auto-draft\'';
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$prepared_sql = $wpdb->prepare($sql, array_merge([ $wpdb->posts ], $post_ids));
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$results = $wpdb->get_results($prepared_sql);
foreach ((array) $results as $result) {
$authors[] = get_userdata($result->post_author);
}
$authors = array_filter($authors);
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
foreach ($authors as $author) {
echo "\t<wp:author>";
echo '<wp:author_id>' . $author->ID . '</wp:author_id>';
echo '<wp:author_login>' . $author->user_login . '</wp:author_login>';
echo '<wp:author_email>' . $author->user_email . '</wp:author_email>';
echo '<wp:author_display_name>' . p4_px_single_post_cdata($author->display_name) . '</wp:author_display_name>';
echo '<wp:author_first_name>' . p4_px_single_post_cdata($author->user_firstname) . '</wp:author_first_name>';
echo '<wp:author_last_name>' . p4_px_single_post_cdata($author->user_lastname) . '</wp:author_last_name>';
echo "</wp:author>\n";
}
}