PluginProbe
Mark Posts / trunk
Mark Posts vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 All 28 releases
mark-posts / admin / views / admin.php

admin.php in Mark Posts trunk, at admin/views/admin.php

381 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Represents the view for the administration dashboard.
4 *
5 * This includes the header, options, and other information that should provide
6 * The User Interface to the end user.
7 *
8 * @author Michael Schoenrock <hello@michaelschoenrock.com>, Sven Hofmann <info@hofmannsven.com>
9 * @license GPL-2.0+
10 */
11
12 // If this file is called directly, abort.
13 if (! defined('WPINC')) {
14 exit;
15 }
16
17 /**
18 * Declare default colors.
19 *
20 * @since 1.0.1
21 */
22 function mark_posts_get_default_colors()
23 {
24 return ['#96D754', '#FFFA74', '#FF7150', '#9ABADC', '#FFA74C', '#158A61'];
25 }
26
27 /**
28 * Get marker terms.
29 *
30 * @since 1.0.1
31 */
32 function mark_posts_get_marker_terms()
33 {
34 return get_terms([
35 'taxonomy' => 'marker',
36 'orderby' => 'id',
37 'hide_empty' => false,
38 ]);
39 }
40
41 /**
42 * Misc functions.
43 *
44 * @since 1.0.0
45 *
46 * @updated 1.0.8
47 */
48 function mark_posts_misc_functions()
49 {
50 // mark all posts
51 if (
52 ! isset($_GET['mark-all-posts-term-id'], $_GET['_wpnonce'])
53 || ! wp_verify_nonce($_GET['_wpnonce'], 'mark-posts-initial')
54 ) {
55 return;
56 }
57 $term_id = (int) $_GET['mark-all-posts-term-id'];
58
59 // set color only for selected post types
60 foreach (get_option('mark_posts_settings')['mark_posts_posttypes'] as $post_type) {
61 // get all posts
62 $all_posts = new WP_Query([
63 'posts_per_page' => -1,
64 'post_type' => $post_type,
65 ]);
66
67 foreach ($all_posts->posts as $post) {
68 // Get the term.
69 $myterm = get_term($term_id, 'marker');
70 // Update the meta field.
71 update_post_meta($post->ID, 'mark_posts_term_id', $term_id);
72 // Update taxonomy count.
73 wp_set_object_terms($post->ID, $myterm->name, 'marker');
74 }
75 }
76
77 // remove the URL parameters after setting the marker.
78 ?>
79 <script>
80 (() => {
81 window.history.replaceState({}, '', '<?php echo esc_url(admin_url('options-general.php?page=mark-posts')) ?>');
82 })();
83 </script>
84 <?php
85 echo mark_posts_display_settings_updated();
86 }
87
88 /**
89 * Save form data.
90 *
91 * @since 1.0.0
92 */
93 function mark_posts_validate_form()
94 {
95 if (! isset($_POST['submit'], $_POST['_wpnonce'])) {
96 return;
97 }
98
99 if (! wp_verify_nonce($_POST['_wpnonce'], 'mark_posts_save_settings')) {
100 return;
101 }
102
103 // get options from db
104 $get_mark_posts_settings = get_option('mark_posts_settings');
105
106 // update post type settings
107 $get_mark_posts_settings['mark_posts_posttypes'] = array_map('sanitize_text_field', $_POST['markertypes'] ?? []);
108
109 // update dashboard settings
110 $get_mark_posts_settings['mark_posts_dashboard'] = array_map('sanitize_text_field', $_POST['markerdashboard'] ?? []);
111
112 // save options
113 update_option('mark_posts_settings', $get_mark_posts_settings);
114
115 // news markers
116 $markers = array_map(static function (string $marker) {
117 return trim(sanitize_text_field($marker));
118 }, explode(',', $_POST['markers'] ?? ''));
119 $default_colors = mark_posts_get_default_colors();
120
121 foreach ($markers as $marker) {
122 wp_insert_term($marker, 'marker', [
123 'name' => $marker,
124 'slug' => sanitize_title($marker),
125 'description' => $default_colors[array_rand($default_colors)], // assign default color
126 ]);
127 }
128
129 // update markers
130 $term_ids = array_map('intval', $_POST['term_ids'] ?? []);
131 $colors = array_map('sanitize_text_field', $_POST['colors'] ?? []);
132 $markers = array_map(static function (string $marker) {
133 return trim(sanitize_text_field($marker));
134 }, $_POST['markernames'] ?? []);
135 foreach ($markers as $index => $marker) {
136 wp_update_term($term_ids[$index], 'marker', [
137 'name' => $marker,
138 'slug' => sanitize_title($marker),
139 'description' => $colors[$index],
140 ]);
141 }
142
143 // delete markers
144 foreach (array_map('intval', $_POST['delete'] ?? []) as $term_id) {
145 wp_delete_term($term_id, 'marker');
146 }
147
148 echo mark_posts_display_settings_updated();
149
150 // Clear transient dashboard stats
151 delete_transient('marker_posts_stats');
152 }
153
154 /**
155 * Show update notice.
156 *
157 * @since 1.0.0
158 */
159 function mark_posts_display_settings_updated(): string
160 {
161 return '<div id="message" class="updated"><p>'.esc_html__('Settings saved', 'mark-posts').'</p></div>';
162 }
163
164 /**
165 * List of excluded post types.
166 *
167 * @since 1.2.1
168 *
169 * @docs https://github.com/hofmannsven/mark-posts/wiki/Reset-Custom-Post-Types
170 */
171 function mark_posts_excluded_post_types(): array
172 {
173 return apply_filters('mark_posts_excluded_post_types', [
174 'acf-field',
175 'acf-field-group',
176 'acf-post-type',
177 'acf-taxonomy',
178 'attachment',
179 'custom_css',
180 'customize_changeset',
181 'itsec-dash-card',
182 'itsec-dashboard',
183 'nav_menu_item',
184 'oembed_cache',
185 'revision',
186 'tcb_content_template',
187 'tcb_symbol',
188 'td_nm_notification',
189 'tve_form_type',
190 'tve_lead_1c_signup',
191 'tve_lead_2s_lightbox',
192 'tve_lead_group',
193 'tve_lead_shortcode',
194 'user_request',
195 'visibility_preset',
196 'wp_block',
197 'wp_font_face',
198 'wp_font_family',
199 'wp_global_styles',
200 'wp_navigation',
201 'wp_template',
202 'wp_template_part',
203 'wpcf7_contact_form',
204 ]);
205 }
206
207 /**
208 * Get the readable name of the custom post type.
209 *
210 *
211 * @since 1.2.3
212 */
213 function mark_posts_get_post_type_name(string $key): string
214 {
215 $cpt = get_post_type_object($key);
216
217 return $cpt instanceof \WP_Post_Type ? $cpt->labels->name : ucfirst($key);
218 }
219
220 /**
221 * Get all available post types.
222 *
223 * @since 1.0.0
224 */
225 function mark_posts_get_all_types()
226 {
227 $active_post_types = get_option('mark_posts_settings')['mark_posts_posttypes'] ?? [];
228 $excluded_post_types = mark_posts_excluded_post_types();
229
230 foreach (get_post_types() as $post_type) {
231 // Filter excluded post types.
232 if (in_array($post_type, $excluded_post_types, true)) {
233 continue;
234 }
235 ?>
236 <p>
237 <label>
238 <input name="markertypes[]" type="checkbox" value="<?php esc_attr_e($post_type) ?>" <?php checked(in_array($post_type, $active_post_types, true)) ?>>
239 <?php esc_html_e(mark_posts_get_post_type_name($post_type)) ?>
240 </label>
241 </p>
242 <?php
243 }
244 }
245
246 /**
247 * Get dashboard widget setup.
248 *
249 * @since 1.0.8
250 */
251 function mark_posts_dashboard()
252 {
253 $option = get_option('mark_posts_settings');
254 echo '<p><input name="markerdashboard[]" type="checkbox" value="dashboard"';
255 if (! empty($option['mark_posts_dashboard'])) {
256 echo ' checked="checked"';
257 }
258 echo ' /> '.esc_html__('Dashboard Widget', 'mark-posts').'</p>';
259 }
260
261 /**
262 * Display all settings.
263 *
264 * @since 1.0.0
265 */
266 function mark_posts_show_settings()
267 {
268 // get default colors
269 $default_colors = mark_posts_get_default_colors(); ?>
270 <form method="post" action="">
271 <?php
272
273 wp_nonce_field('mark_posts_save_settings');
274
275 // Get Marker terms from DB
276 $markers_terms = mark_posts_get_marker_terms();
277
278 if (! empty($markers_terms)) {
279 ?>
280 <h3 class="title"><?php esc_html_e('Markers', 'mark-posts') ?></h3>
281 <table class="form-table">
282 <tbody>
283 <?php
284 $i = 0;
285 foreach ($markers_terms as $marker_term) {
286 $color = $marker_term->description;
287 if ($color === '') {
288 if (! isset($default_colors[$i++])) {
289 $i = 0; // reset pointer to 0 start over
290 }
291 $color = $default_colors[$i];
292 }
293 ?>
294 <tr valign="top">
295 <td scope="row">
296 <input type="text" name="markernames[]" value="<?php esc_html_e($marker_term->name) ?>">
297 </td>
298 <td width="130">
299 <input type="text" name="colors[]" value="<?php esc_attr_e($color) ?>" class="my-color-field" data-default-color="<?php esc_attr_e($color) ?>"/>
300 </td>
301 <td>
302 <input type="checkbox" name="delete[]" id="delete_<?php echo (int) $marker_term->term_id ?>" value="<?php echo (int) $marker_term->term_id ?>">
303 <label for="delete_<?php echo (int) $marker_term->term_id ?>">
304 <?php esc_html_e('delete', 'mark-posts') ?>
305 </label>
306 <a href="javascript:void(0);" class="mark-posts-initial" data-confirm-msg="<?php esc_attr_e('Do you really want to mark all posts with this marker? Note: This will override all your previous set markers. This will only effect the enabled post types.', 'mark-posts') ?>" data-term-id="<?php echo (int) $marker_term->term_id ?>">
307 <?php esc_html_e('Mark all posts with this marker', 'mark-posts') ?>
308 </a>
309 </td>
310 <input type="hidden" name="term_ids[]" value="<?php echo (int) $marker_term->term_id ?>"/>
311 </tr>
312 <?php } ?>
313 </tbody>
314 </table>
315
316 <?php submit_button(); ?>
317
318 <hr/>
319 <?php } ?>
320
321 <h3 class="title"><?php esc_html_e('Add new Markers', 'mark-posts') ?></h3>
322
323 <p>
324 <?php esc_html_e('Add new marker (please separate them by comma):', 'mark-posts') ?>
325 </p>
326
327 <textarea class="js-add-markers" name="markers" style="width:60%;height:120px;"></textarea>
328
329 <div class="new-markers">
330 <span class="js-new-markers-intro"><?php esc_html_e('Markers to add:', 'mark-posts'); ?></span>
331 <span class="js-new-markers"></span>
332 </div>
333
334 <?php submit_button(); ?>
335
336 <hr/>
337 <h3 class="title"><?php _e('Enable/Disable Markers', 'mark-posts'); ?></h3>
338
339 <p>
340 <?php esc_html_e('Enable/Disable markers for specific post types:', 'mark-posts') ?>
341 </p>
342
343 <?php
344 mark_posts_get_all_types();
345 submit_button();
346 ?>
347
348 <hr/>
349 <h3 class="title"><?php esc_html_e('Enable/Disable Dashboard Widget', 'mark-posts') ?></h3>
350
351 <?php
352 mark_posts_dashboard();
353 submit_button();
354 ?>
355
356 </form>
357
358 <?php
359 }
360
361 ?>
362
363 <div class="wrap">
364
365 <?php mark_posts_misc_functions() ?>
366
367 <?php mark_posts_validate_form(); ?>
368
369 <h2><?php esc_html_e('Mark Posts Options', 'mark-posts'); ?></h2>
370
371 <?php mark_posts_show_settings(); ?>
372
373 <div class="mark-posts-copy">
374 <hr/>
375 Mark Posts | Version: <?php echo WP_MARK_POSTS_VERSION; ?> | &copy; <?php echo date('Y'); ?>
376 <a href="http://www.aliquit.de" target="_blank" rel="noopener">Michael Schoenrock</a>,
377 <a href="https://hofmannsven.com" target="_blank" rel="noopener">Sven Hofmann</a>
378 </div>
379
380 </div>
381