PluginProbe
Mark Posts / 1.2.1
Mark Posts v1.2.1
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 / class-mark-posts-admin.php

class-mark-posts-admin.php in Mark Posts 1.2.1, at admin/class-mark-posts-admin.php

579 lines 17.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Mark Posts Class.
4 *
5 * @author Michael Schoenrock <hello@michaelschoenrock.com>, Sven Hofmann <info@hofmannsven.com>
6 * @license GPL-2.0+
7 */
8
9 // If this file is called directly, abort.
10 if (!defined('WPINC')) {
11 die;
12 }
13
14 class Mark_Posts_Admin
15 {
16 /**
17 * Instance of this class.
18 *
19 * @since 1.0.0
20 *
21 * @var object
22 */
23 protected static $instance = null;
24
25 /**
26 * Slug of the plugin screen.
27 *
28 * @since 1.0.0
29 *
30 * @var string
31 */
32 protected $plugin_screen_hook_suffix = null;
33
34 /**
35 * Initialize the plugin by loading admin scripts & styles and adding a settings page and menu.
36 *
37 * @since 1.0.0
38 */
39 private function __construct()
40 {
41
42 /**
43 * Call $plugin_slug from public plugin class.
44 */
45 $plugin = Mark_Posts::get_instance();
46 $this->plugin_slug = $plugin->get_plugin_slug();
47 $get_mark_posts_setup = get_option('mark_posts_settings');
48
49 // Load admin style sheet and JavaScript
50 add_action('admin_enqueue_scripts', [$this, 'mark_posts_enqueue_admin_styles']);
51 add_action('admin_enqueue_scripts', [$this, 'mark_posts_enqueue_admin_scripts']);
52
53 // Add the options page and menu item
54 add_action('admin_menu', [$this, 'mark_posts_add_plugin_admin_menu']);
55
56 /*
57 * Add dashboard
58 *
59 * @since 1.0.8
60 */
61 if (isset($get_mark_posts_setup['mark_posts_dashboard'])):
62 $mark_posts_dashboard = $get_mark_posts_setup['mark_posts_dashboard'];
63 if (!empty($mark_posts_dashboard)) :
64 add_action('wp_dashboard_setup', [$this, 'mark_posts_dashboard_widget']);
65 endif;
66 endif;
67
68 // Add an action link pointing to the options page
69 $plugin_basename = plugin_basename(plugin_dir_path(__DIR__).$this->plugin_slug.'.php');
70 add_filter('plugin_action_links_'.$plugin_basename, [$this, 'mark_posts_add_action_links']);
71
72 // Add quick edit and bulk edit actions
73 add_action('bulk_edit_custom_box', [$this, 'mark_posts_display_quickedit_box'], 10, 2);
74 add_action('quick_edit_custom_box', [$this, 'mark_posts_display_quickedit_box'], 10, 2);
75 // Add JavaScript for quick edit and bulk edit actions
76 add_action('admin_print_scripts-edit.php', [$this, 'mark_posts_edit_scripts'], 10, 2);
77
78 // Add metabox
79 add_action('add_meta_boxes', [$this, 'mark_posts_add_meta_box']);
80 // Save action for metabox
81 add_action('save_post', [$this, 'mark_posts_save']);
82 // Save action for quick edit
83 add_action('save_post', [$this, 'mark_posts_save_quick_edit'], 10, 2);
84 // Save action for bulk edit
85 add_action('wp_ajax_mark_posts_save_bulk_edit', [$this, 'mark_posts_save_bulk_edit']);
86 // Trash action
87 add_action('trash_post', [$this, 'mark_posts_trash'], 1, 1);
88 // Delete action
89 add_action('delete_post', [$this, 'mark_posts_delete'], 10);
90
91 /**
92 * Custom admin post columns (custom post types only).
93 *
94 * @since 1.0.0
95 */
96 $mark_posts_posttypes = $get_mark_posts_setup['mark_posts_posttypes'];
97 foreach ($mark_posts_posttypes as $post_type) {
98 add_filter('manage_'.$post_type.'_posts_columns', [$this, 'mark_posts_column_head'], 10, 2);
99 add_action('manage_'.$post_type.'_posts_custom_column', [$this, 'mark_posts_column_content'], 10, 2);
100 }
101 }
102
103 /**
104 * Return an instance of this class.
105 *
106 * @since 1.0.0
107 *
108 * @return object A single instance of this class
109 */
110 public static function get_instance()
111 {
112
113 // If the single instance hasn't been set, set it now.
114 if (null == self::$instance) {
115 self::$instance = new self();
116 }
117
118 return self::$instance;
119 }
120
121 /**
122 * Register and enqueue admin-specific style sheet.
123 *
124 * @since 1.0.0
125 *
126 * @return null Return early if no settings page is registered
127 */
128 public function mark_posts_enqueue_admin_styles()
129 {
130 if (!isset($this->plugin_screen_hook_suffix)) {
131 return;
132 }
133
134 wp_enqueue_style($this->plugin_slug.'-admin-styles', plugins_url('assets/css/admin.css', __FILE__), [], WP_MARK_POSTS_VERSION);
135 }
136
137 /**
138 * Register and enqueue admin-specific JavaScript.
139 *
140 * @since 1.0.0
141 *
142 * @return null Return early if no settings page is registered
143 */
144 public function mark_posts_enqueue_admin_scripts()
145 {
146 if (!isset($this->plugin_screen_hook_suffix)) {
147 return;
148 }
149
150 global $pagenow;
151 if ($pagenow == 'options-general.php' || $pagenow == 'edit.php' || $pagenow == 'post.php') :
152 wp_enqueue_style('wp-color-picker'); // see http://make.wordpress.org/core/2012/11/30/new-color-picker-in-wp-3-5/
153 wp_enqueue_script($this->plugin_slug.'-post-list-marker', plugins_url('assets/js/markposts.js', __FILE__), ['wp-color-picker'], WP_MARK_POSTS_VERSION, true);
154 endif;
155 }
156
157 /**
158 * Register the administration menu for this plugin into the WordPress Dashboard menu.
159 *
160 * @since 1.0.0
161 */
162 public function mark_posts_add_plugin_admin_menu()
163 {
164
165 // add a settings page for this plugin to the Settings menu
166 $this->plugin_screen_hook_suffix = add_options_page(
167 __('Mark Posts', $this->plugin_slug),
168 __('Mark Posts', $this->plugin_slug),
169 'manage_options',
170 $this->plugin_slug,
171 [$this, 'mark_posts_display_plugin_admin_page']
172 );
173 }
174
175 /**
176 * Render the settings page for this plugin.
177 *
178 * @since 1.0.0
179 */
180 public function mark_posts_display_plugin_admin_page()
181 {
182 include_once 'views/admin.php';
183 }
184
185 /**
186 * Register custom dashboard widget.
187 *
188 * @since 1.0.0
189 */
190 public function mark_posts_dashboard_widget()
191 {
192 global $wp_meta_boxes;
193 $this->plugin_screen_hook_suffix = wp_add_dashboard_widget(
194 'mark_posts_info_widget',
195 'Mark Posts',
196 [$this, 'mark_posts_dashboard_info']
197 );
198 add_action('admin_enqueue_scripts', [$this, 'mark_posts_enqueue_dashboard_styles']);
199 add_action('admin_head', [$this, 'mark_posts_custom_dashboard_styles']);
200 }
201
202 /**
203 * Render the dashboard widget.
204 *
205 * @since 1.0.0
206 */
207 public function mark_posts_dashboard_info()
208 {
209 include_once 'views/dashboard.php';
210 }
211
212 /**
213 * Load additional dashboard styles.
214 *
215 * @since 1.0.0
216 */
217 public function mark_posts_enqueue_dashboard_styles()
218 {
219 wp_enqueue_style($this->plugin_slug.'-dashboard-styles', plugins_url('assets/css/dashboard.css', __FILE__), [], WP_MARK_POSTS_VERSION);
220 }
221
222 /**
223 * Build custom dashboard styles.
224 *
225 * @since 1.0.0
226 */
227 public function mark_posts_custom_dashboard_styles()
228 {
229 $marker_args = [
230 'hide_empty' => true,
231 ];
232 $markers = get_terms('marker', $marker_args);
233 echo '<style>';
234
235 foreach ($markers as $marker) :
236 echo '.mark-posts-'.$marker->slug.' a:before { color: '.$marker->description.'} ';
237 endforeach;
238
239 echo '</style>';
240 }
241
242 /**
243 * Add settings action link to the plugins page.
244 *
245 * @since 1.0.0
246 *
247 * @param $links
248 *
249 * @return array associative array of plugin action links
250 */
251 public function mark_posts_add_action_links($links)
252 {
253 // add a 'Settings' link to the front of the actions list for this plugin
254 return array_merge(
255 [
256 'settings' => '<a href="'.admin_url('options-general.php?page='.$this->plugin_slug).'">'.__('Settings', $this->plugin_slug).'</a>',
257 ],
258 $links
259 );
260 }
261
262 /**
263 * Adds a box to the main column on the edit screens.
264 *
265 * @since 1.0.0
266 */
267 public function mark_posts_add_meta_box()
268 {
269 $get_mark_posts_setup = get_option('mark_posts_settings');
270 $mark_posts_posttypes = $get_mark_posts_setup['mark_posts_posttypes'];
271
272 if (!empty($mark_posts_posttypes)) {
273 foreach ($mark_posts_posttypes as $mark_posts_posttype) {
274 add_meta_box(
275 'mark_posts_options',
276 __('Mark Posts Options', 'mark-posts'),
277 [$this, 'mark_posts_inner_meta_box'],
278 $mark_posts_posttype,
279 'side'
280 );
281 }
282 }
283 }
284
285 /**
286 * Prints the box content.
287 *
288 * @since 1.0.0
289 *
290 * @param $post Information about the post e.g. 'ID'
291 */
292 public function mark_posts_inner_meta_box($post)
293 {
294
295 // Add an nonce field so we can check for it later
296 wp_nonce_field('mark_posts_inner_meta_box', 'mark_posts_inner_meta_box_nonce');
297
298 echo '<p>'.__('Mark this post as:', 'mark-posts').'</p>';
299
300 // Get available markers as select dropdown
301 $markers = new Mark_Posts_Marker();
302 echo $markers->mark_posts_select($post->ID);
303
304 echo '<span class="mark-posts-color"></span>';
305
306 echo '<p>'.sprintf(
307 /* translators: %s: plugin settings page */
308 __('Click <a href="%s">here</a> to manage Marker categories.', 'mark-posts'),
309 esc_url('options-general.php?page=mark-posts')
310 ).'</p>';
311 }
312
313 /**
314 * Save the meta when the post is saved.
315 *
316 * @since 1.0.0
317 *
318 * @param $post_id ID of the post e.g. '1'
319 *
320 * @return mixed
321 */
322 public function mark_posts_save($post_id)
323 {
324 // Check if our nonce is set.
325 if (!isset($_POST['mark_posts_inner_meta_box_nonce'])) {
326 return $post_id;
327 }
328
329 $nonce = $_POST['mark_posts_inner_meta_box_nonce'];
330
331 // Verify that the nonce is valid.
332 if (!wp_verify_nonce($nonce, 'mark_posts_inner_meta_box')) {
333 return $post_id;
334 }
335
336 // If this is an autosave, our form has not been submitted,
337 // so we don't want to do anything.
338 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
339 return $post_id;
340 }
341
342 // Check the user's permissions.
343 if ('page' == $_POST['post_type']) {
344 if (!current_user_can('edit_page', $post_id)) {
345 return $post_id;
346 }
347 } else {
348 if (!current_user_can('edit_post', $post_id)) {
349 return $post_id;
350 }
351 }
352
353 /* OK, its safe for us to mark_posts_save the data now. */
354
355 // Sanitize the user input.
356 $mydata = sanitize_text_field($_POST['mark_posts_term_id']);
357 $myterm = get_term($mydata, 'marker');
358
359 // Update the meta field.
360 update_post_meta($post_id, 'mark_posts_term_id', $mydata);
361
362 // Update taxonomy count
363 @wp_update_term_count_now($mydata, 'marker');
364
365 // Clear transient dashboard stats
366 delete_transient('marker_posts_stats');
367 }
368
369 /**
370 * Update taxonomy count if posts get permanently deleted.
371 *
372 * @since 1.0.7
373 *
374 * @param $post_id ID of the post e.g. '1'
375 */
376 public function mark_posts_delete($post_id)
377 {
378 // Retrieve post meta value from the database
379 if (isset($post_id)) :
380 $term = get_post_meta($post_id, 'mark_posts_term_id', true);
381 if (!empty($term)) :
382 wp_set_object_terms($post_id, $term, 'marker'); else :
383 wp_set_object_terms($post_id, null, 'marker'); // clear/remove all marker from post with $post_id
384 endif;
385 // Clear transient dashboard stats
386 delete_transient('marker_posts_stats');
387 endif;
388 }
389
390 /**
391 * Update dashboard stats if posts get trashed.
392 *
393 * @since 1.0.7
394 *
395 * @param $post_id ID of the post e.g. '1'
396 */
397 public function mark_posts_trash($post_id)
398 {
399 // Clear transient dashboard stats
400 delete_transient('marker_posts_stats');
401 }
402
403 /**
404 * Custom quick edit box.
405 *
406 * @since 1.0.0
407 *
408 * @param $column_name Custom column name e.g. 'mark_posts_term_id'
409 */
410 public function mark_posts_display_quickedit_box($column_name)
411 {
412 switch ($column_name) {
413 case 'mark_posts_term_id':
414 ?>
415 <fieldset class="inline-edit-col-right mark-posts-quickedit">
416 <div class="inline-edit-col">
417 <div class="inline-edit-group">
418 <label class="inline-edit-status alignleft">
419 <span class="title"><?php _e('Marker', 'mark-posts'); ?></span>
420 <?php
421
422 // Get available markers as select dropdown
423 $markers = new Mark_Posts_Marker();
424 echo $markers->mark_posts_select();
425
426 ?>
427 </label>
428 </div>
429 </div>
430 </fieldset>
431 <?php
432 break;
433 }
434 }
435
436 /**
437 * Save quick edit.
438 *
439 * @since 1.0.0
440 *
441 * @param $post_id ID of the post e.g. '1'
442 * @param $post Information about the post e.g. 'post_type'
443 *
444 * @return mixed
445 */
446 public function mark_posts_save_quick_edit($post_id, $post)
447 {
448 // pointless if $_POST is empty (this happens on bulk edit)
449 if (empty($_POST)) {
450 return $post_id;
451 }
452
453 // verify quick edit nonce
454 if (isset($_POST['_inline_edit']) && !wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce')) {
455 return $post_id;
456 }
457
458 // don't mark_posts_save for autosave
459 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
460 return $post_id;
461 }
462
463 // dont mark_posts_save for revisions
464 if (isset($post->post_type) && $post->post_type == 'revision') {
465 return $post_id;
466 }
467
468 $mark_posts_fields = ['mark_posts_term_id'];
469
470 foreach ($mark_posts_fields as $mark_field) :
471 if (array_key_exists($mark_field, $_POST)) :
472 // update post meta
473 update_post_meta($post_id, $mark_field, $_POST[$mark_field]);
474
475 // update terms
476 $term = get_term($_POST[$mark_field], 'marker');
477 if (!empty($term->name)) :
478 wp_set_object_terms($post_id, $term->name, 'marker'); else :
479 wp_set_object_terms($post_id, null, 'marker'); // clear/remove all marker from post with $post_id
480 endif;
481
482 // Clear transient dashboard stats
483 delete_transient('marker_posts_stats');
484 endif;
485 endforeach;
486 }
487
488 /**
489 * Save bulk edit.
490 *
491 * @since 1.0.0
492 */
493 public function mark_posts_save_bulk_edit()
494 {
495
496 // we need the post IDs
497 $post_ids = (isset($_POST['post_ids']) && !empty($_POST['post_ids'])) ? $_POST['post_ids'] : null;
498
499 // if we have post IDs
500 if (!empty($post_ids) && is_array($post_ids)) {
501 $mark_posts_fields = ['mark_posts_term_id'];
502
503 foreach ($mark_posts_fields as $mark_field) :
504
505 // if it has a value, doesn't update if empty on bulk
506 if (isset($_POST[$mark_field]) && !empty($_POST[$mark_field])) {
507
508 // update for each post ID
509 foreach ($post_ids as $post_id) {
510 // update post meta
511 update_post_meta($post_id, $mark_field, $_POST[$mark_field]);
512
513 // update terms
514 $term = get_term($_POST[$mark_field], 'marker');
515 wp_set_object_terms($post_id, $term->name, 'marker');
516
517 // Clear transient dashboard stats
518 delete_transient('marker_posts_stats');
519 }
520 }
521
522 endforeach;
523 }
524 }
525
526 /**
527 * Enqueue quick edit and bulk edit script in admin footer.
528 *
529 * @since 1.0.0
530 */
531 public function mark_posts_edit_scripts()
532 {
533 wp_enqueue_script($this->plugin_slug.'-quick-bulk-edit', plugins_url('assets/js/admin-edit.js', __FILE__), ['jquery', 'inline-edit-post'], WP_MARK_POSTS_VERSION, true);
534 }
535
536 /**
537 * Set admin column.
538 *
539 * @since 1.0.0
540 *
541 * @param $columns Set custom admin column ID and name
542 *
543 * @return mixed
544 */
545 public function mark_posts_column_head($columns)
546 {
547 $columns['mark_posts_term_id'] = __('Marker', 'mark-posts');
548
549 return $columns;
550 }
551
552 /**
553 * Show column content.
554 *
555 * @since 1.0.0
556 *
557 * @param $column_name Custom column name e.g. 'mark_posts_term_id'
558 * @param $post_id ID of the post e.g. '1'
559 */
560 public function mark_posts_column_content($column_name, $post_id)
561 {
562 switch ($column_name) {
563
564 case 'mark_posts_term_id':
565 $value = get_post_meta($post_id, 'mark_posts_term_id', true);
566 if (isset($value)) {
567 $term = get_term($value, 'marker');
568 if ($term) {
569 if (isset($term->description) && isset($term->name)) {
570 echo '<div id="mark_posts_term_id-'.$post_id.'" class="mark-posts-marker" style="background:'.$term->description.'" data-val="'.$term->term_id.'" data-background="'.$term->description.'">'.$term->name.'</div>';
571 }
572 }
573 }
574 break;
575
576 }
577 }
578 }
579