PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.50.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.50.0
3.54.0 3.53.0 3.52.0 3.51.0 3.50.0 3.45.0 3.38.0 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.40.0 3.40.1 3.41.0 3.42.0 3.43.0 3.44.0 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 All 178 releases
simple-tags / inc / related-posts-functions.php

related-posts-functions.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.50.0, at inc/related-posts-functions.php

411 lines 14.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Fetch our TAXOPRESS Related Posts option.
5 *
6 * @return mixed
7 */
8 function taxopress_get_relatedpost_data()
9 {
10 return array_filter((array)apply_filters(
11 'taxopress_get_relatedpost_data',
12 get_option('taxopress_relatedposts', []),
13 get_current_blog_id()
14 ));
15 }
16
17 /**
18 * Get the selected relatedpost from the $_POST global.
19 *
20 * @return bool|string False on no result, sanitized relatedpost if set.
21 * @internal
22 *
23 */
24 function taxopress_get_current_relatedpost()
25 {
26
27 $relatedposts = false;
28
29 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
30 if (!empty($_GET) && isset($_GET['taxopress_relatedposts'])) {
31 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
32 $relatedposts = sanitize_text_field(wp_unslash($_GET['taxopress_relatedposts']));
33 } else {
34 $relatedposts = taxopress_get_relatedpost_data();
35 if (!empty($relatedposts)) {
36 // Will return the first array key.
37 $relatedposts = key($relatedposts);
38 }
39 }
40
41 /**
42 * Filters the current relatedpost to edit.
43 *
44 * @param string $relatedposts relatedpost slug.
45 */
46 return apply_filters('taxopress_current_relatedpost', $relatedposts);
47 }
48
49 /**
50 * Handle the save and deletion of relatedpost data.
51 */
52 function taxopress_process_relatedpost()
53 {
54
55 if (wp_doing_ajax()) {
56 return;
57 }
58
59 if (!is_admin()) {
60 return;
61 }
62
63 if (empty($_GET)) {
64 return;
65 }
66
67 if (!isset($_GET['page'])) {
68 return;
69 }
70 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
71 $page = sanitize_key(wp_unslash($_GET['page']));
72 if ('st_related_posts' !== $page) {
73 return;
74 }
75
76 if (!current_user_can('simple_tags')) {
77 return;
78 }
79
80 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
81 if (isset($_GET['new_relatedpost'])) {
82 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
83 if ((int) $_GET['new_relatedpost'] === 1) {
84 add_action('admin_notices', "taxopress_relatedposts_update_success_admin_notice");
85 add_filter('removable_query_args', 'taxopress_saved_relatedpost_filter_removable_query_args');
86 }
87 }
88
89 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
90 if (isset($_GET['deleted_relatedpost'])) {
91 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
92 if ((int) $_GET['deleted_relatedpost'] === 1) {
93 add_action('admin_notices', "taxopress_relatedposts_delete_success_admin_notice");
94 add_filter('removable_query_args', 'taxopress_deleted_relatedpost_filter_removable_query_args');
95 }
96 }
97
98
99 if (!empty($_POST) && isset($_POST['relatedpost_submit'])) {
100 $result = '';
101 if (isset($_POST['relatedpost_submit'])) {
102 check_admin_referer(
103 'taxopress_addedit_relatedpost_nonce_action',
104 'taxopress_addedit_relatedpost_nonce_field'
105 );
106 $result = taxopress_update_relatedpost($_POST);
107 }
108
109 if ($result) {
110 wp_safe_redirect(
111 add_query_arg(
112 [
113 'page' => 'st_related_posts',
114 'add' => 'new_item',
115 'action' => 'edit',
116 'taxopress_relatedposts' => $result,
117 'new_relatedpost' => 1,
118 ],
119 taxopress_admin_url('admin.php')
120 )
121 );
122
123 exit();
124 }
125 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-relatedpost') {
126 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
127 $nonce = isset($_REQUEST['_wpnonce']) ? sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])) : '';
128 if ($nonce && wp_verify_nonce($nonce, 'relatedpost-action-request-nonce')) {
129 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
130 $relatedpost_id = isset($_REQUEST['taxopress_relatedposts']) ? sanitize_text_field(wp_unslash($_REQUEST['taxopress_relatedposts'])) : '';
131 if ($relatedpost_id !== '') {
132 taxopress_action_delete_relatedpost($relatedpost_id);
133 }
134 }
135 add_filter('removable_query_args', 'taxopress_delete_relatedpost_filter_removable_query_args');
136 }
137 }
138
139
140 /**
141 * Create default related post.
142 */
143 function taxopress_create_default_related_post()
144 {
145
146 if (wp_doing_ajax()) {
147 return;
148 }
149
150 if (!is_admin()) {
151 return;
152 }
153
154 if (!current_user_can('simple_tags')) {
155 return;
156 }
157
158 if ((int)get_option('taxopress_default_relatedposts') > 0) {
159 return;
160 }
161
162 if (count(taxopress_get_relatedpost_data()) > 0) {
163 return;
164 }
165
166 $default = [];
167 $default['taxopress_related_post']['title'] = esc_html__('Related Posts', 'simple-tags');
168 $default['taxopress_related_post']['title_header'] = 'h4';
169 $default['taxopress_related_post']['before'] = '';
170 $default['taxopress_related_post']['after'] = '';
171 $default['taxopress_related_post']['post_types'] = ['post'];
172 $default['taxopress_related_post']['taxonomy'] = 'post_tag';
173 $default['taxopress_related_post']['number'] = 5;
174 $default['taxopress_related_post']['limit_days'] = 0;
175 $default['taxopress_related_post']['order'] = 'count-desc';
176 $default['taxopress_related_post']['nopoststext'] = __('No related posts.', 'simple-tags');
177 $default['taxopress_related_post']['xformat'] = '<a href="%post_permalink%" title="%post_title% (%post_date%)" style="font-size:%post_size%;color:%post_color%"><img src="%post_thumb_url%" height="200" width="200" class="custom-image-class"/><br>%post_title%<br>%post_category%</a>';
178 $default['taxopress_related_post']['format'] = 'box';
179 $default['taxopress_related_post']['default_featured_media'] = STAGS_URL . '/assets/images/taxopress-white-logo.png';
180 $default['taxopress_related_post']['smallest'] = 12;
181 $default['taxopress_related_post']['largest'] = 12;
182 $default['taxopress_related_post']['unit'] = 'pt';
183 $default['taxopress_related_post']['color'] = 1;
184 $default['taxopress_related_post']['mincolor'] = '#353535';
185 $default['taxopress_related_post']['maxcolor'] = '#000000';
186 $default['relatedpost_submit'] = 'Add Related Posts';
187 $default['cpt_tax_status'] = 'new';
188 $result = taxopress_update_relatedpost($default);
189 update_option('taxopress_default_relatedposts', $result);
190 }
191
192
193 /**
194 * Add to or update our TAXOPRESS option with new data.
195 *
196 *
197 * @param array $data Array of relatedpost data to update. Optional.
198 * @return bool|string False on failure, string on success.
199 * @internal
200 *
201 */
202 function taxopress_update_relatedpost($data = [])
203 {
204 $sanitized_data = [];
205 foreach ($data as $key => $value) {
206 if (!is_array($value)) {
207 $sanitized_data[$key] = taxopress_sanitize_text_field($value);
208 } else {
209 $new_value = [];
210 foreach ($data[$key] as $option_key => $option_value) {
211 if ($option_key === 'xformat') {
212 $new_value[$option_key] = taxopress_sanitize_text_field($option_value);
213 } else {
214 $new_value[$option_key] = taxopress_sanitize_text_field($option_value);
215 }
216 }
217 $sanitized_data[$key] = $new_value;
218 }
219 }
220 $data = $sanitized_data;
221
222 $relatedposts = taxopress_get_relatedpost_data();
223
224 $title = $data['taxopress_related_post']['title'];
225 $title = str_replace('"', '', htmlspecialchars_decode($title));
226 $title = htmlspecialchars($title, ENT_QUOTES);
227 $title = trim($title);
228 $data['taxopress_related_post']['title'] = stripslashes_deep($title);
229
230 $xformat = $data['taxopress_related_post']['xformat'];
231 $data['taxopress_related_post']['xformat'] = stripslashes_deep($xformat);
232 $data['taxopress_related_post']['embedded'] = isset($data['embedded']) ? $data['embedded'] : [];
233 $data['taxopress_related_post']['post_types'] = isset($data['post_types']) ? array_map('sanitize_key', (array) $data['post_types']) : [];
234 $data['taxopress_related_post']['taxonomy'] = isset($data['taxopress_related_post']['taxonomy']) ? sanitize_key($data['taxopress_related_post']['taxonomy']) : 'post_tag';
235
236 if (isset($data['edited_relatedpost'])) {
237 $relatedpost_id = $data['edited_relatedpost'];
238 $relatedposts[$relatedpost_id] = $data['taxopress_related_post'];
239 $success = update_option('taxopress_relatedposts', $relatedposts);
240 } else {
241 $relatedpost_id = (int)get_option('taxopress_relatedpost_ids_increament') + 1;
242 $data['taxopress_related_post']['ID'] = $relatedpost_id;
243 $relatedposts[$relatedpost_id] = $data['taxopress_related_post'];
244 $success = update_option('taxopress_relatedposts', $relatedposts);
245 $update_id = update_option('taxopress_relatedpost_ids_increament', $relatedpost_id);
246 }
247
248 return $relatedpost_id;
249 }
250
251 /**
252 * Successful update callback.
253 */
254 function taxopress_relatedposts_update_success_admin_notice()
255 {
256 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
257 echo taxopress_admin_notices_helper(esc_html__('Settings updated successfully.', 'simple-tags'));
258 }
259
260 /**
261 * Successful deleted callback.
262 */
263 function taxopress_relatedposts_delete_success_admin_notice()
264 {
265 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
266 echo taxopress_admin_notices_helper(esc_html__('Related Posts successfully deleted.', 'simple-tags'), false);
267 }
268
269 /**
270 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
271 *
272 * @link https://core.trac.wordpress.org/ticket/23367
273 *
274 * @param string[] $args Array of removable query arguments.
275 * @return string[] Updated array of removable query arguments.
276 */
277 function taxopress_saved_relatedpost_filter_removable_query_args(array $args)
278 {
279 return array_merge($args, [
280 'new_relatedpost',
281 ]);
282 }
283
284 /**
285 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
286 *
287 * @link https://core.trac.wordpress.org/ticket/23367
288 *
289 * @param string[] $args Array of removable query arguments.
290 * @return string[] Updated array of removable query arguments.
291 */
292 function taxopress_deleted_relatedpost_filter_removable_query_args(array $args)
293 {
294 return array_merge($args, [
295 'deleted_relatedpost',
296 ]);
297 }
298
299 /**
300 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
301 *
302 * @link https://core.trac.wordpress.org/ticket/23367
303 *
304 * @param string[] $args Array of removable query arguments.
305 * @return string[] Updated array of removable query arguments.
306 */
307 function taxopress_delete_relatedpost_filter_removable_query_args(array $args)
308 {
309 return array_merge($args, [
310 'action',
311 'taxopress_relatedposts',
312 '_wpnonce',
313 ]);
314 }
315
316 /**
317 * Delete our custom related post from the array of related posts.
318 * @return bool|string False on failure, string on success.
319 */
320 function taxopress_action_delete_relatedpost($relatedpost_id)
321 {
322 $relatedposts = taxopress_get_relatedpost_data();
323
324 if (array_key_exists($relatedpost_id, $relatedposts)) {
325 unset($relatedposts[$relatedpost_id]);
326 $success = update_option('taxopress_relatedposts', $relatedposts);
327 }
328
329 if (isset($success)) {
330 add_action('admin_notices', "taxopress_taxdeleted_admin_notice");
331 wp_safe_redirect(
332 add_query_arg(
333 [
334 'page' => 'st_related_posts',
335 'deleted_relatedpost' => 1,
336 ],
337 taxopress_admin_url('admin.php')
338 )
339 );
340 exit();
341 }
342 }
343
344 function taxopress_relatedposts_shortcode($atts)
345 {
346 extract(shortcode_atts([
347 'id' => 0
348 ], $atts));
349
350 $relatedpost_id = $id;
351 $relatedposts = taxopress_get_relatedpost_data();
352
353 ob_start();
354 if (array_key_exists($relatedpost_id, $relatedposts)) {
355 $related_post_array = $relatedposts[$relatedpost_id];
356 $relatedpost_arg = build_query($related_post_array);
357
358 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
359 echo SimpleTags_Client_RelatedPosts::get_related_posts($relatedpost_arg);
360 } else {
361 echo esc_html__('Related Posts not found.', 'simple-tags');
362 }
363
364 $html = ob_get_clean();
365
366 return $html;
367 }
368
369 /**
370 * Auto add related post to post content
371 *
372 * @param string $content
373 *
374 * @return string
375 */
376 function taxopress_relatedposts_the_content($content = '')
377 {
378
379 $post_tags = taxopress_get_relatedpost_data();
380
381 if (count($post_tags) > 0) {
382 foreach ($post_tags as $post_tag) {
383 // Get option
384 $embedded = (isset($post_tag['embedded']) && is_array($post_tag['embedded']) && count($post_tag['embedded']) > 0) ? $post_tag['embedded'] : false;
385
386 if (!$embedded) {
387 continue;
388 }
389
390 $marker = false;
391 if (is_feed() && in_array('feed', $embedded)) {
392 $marker = true;
393 } elseif (is_home() && in_array('homeonly', $embedded)) {
394 $marker = true;
395 } elseif (is_feed() && in_array('blogonly', $embedded)) {
396 $marker = true;
397 } elseif (is_singular() && in_array('singleonly', $embedded)) {
398 $marker = true;
399 } elseif (is_singular() && in_array(get_post_type(), $embedded)) {
400 $marker = true;
401 }
402 if (true === $marker) {
403 $relatedpost_arg = build_query($post_tag);
404 $content .= SimpleTags_Client_RelatedPosts::get_related_posts($relatedpost_arg);
405 }
406 }
407 }
408
409 return $content;
410 }
411