Completed
Push — 193-feature/delete-post-revisi... ( 4c008c...2cafa6 )
by Rajan
141:08 queued 137:21
created

BD_Util::get_cron_schedules()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 5
eloc 18
c 2
b 2
f 0
nc 5
nop 0
dl 0
loc 27
rs 8.439

1 Method

Rating   Name   Duplication   Size   Complexity  
B BD_Util::display_post_type_status() 0 26 6
1
<?php
2
/**
3
 * Utility classes and functions.
4
 *
5
 * @author     Sudar
6
 *
7
 * @package    BulkDelete\Util
8
 */
9
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
10
11
/**
12
 * Utility class.
13
 *
14
 * Ideally most of the functions should be inside the `BulkDelete\Util` and not as static functions.
15
 */
16
class BD_Util {
17
	// Meta boxes
18
	const VISIBLE_POST_BOXES     = 'metaboxhidden_toplevel_page_bulk-delete-posts';
19
	const VISIBLE_PAGE_BOXES     = 'metaboxhidden_bulk-delete_page_bulk-delete-pages';
20
	const VISIBLE_USER_BOXES     = 'metaboxhidden_bulk-delete_page_bulk-delete-users';
21
22
	/**
23
	 * Check whether the meta box in posts page is hidden or not.
24
	 *
25
	 * @static
26
	 * @access public
27
	 *
28
	 * @param string $box The name of the box
29
	 *
30
	 * @return bool True if the box is hidden, False otherwise
31
	 */
32
	public static function is_posts_box_hidden( $box ) {
33
		$hidden_boxes = self::get_posts_hidden_boxes();
34
35
		return is_array( $hidden_boxes ) && in_array( $box, $hidden_boxes );
36
	}
37
38
	/**
39
	 * Get the list of hidden boxes in posts page.
40
	 *
41
	 * @static
42
	 * @access public
43
	 *
44
	 * @return array The list of hidden meta boxes
45
	 */
46
	public static function get_posts_hidden_boxes() {
47
		$current_user = wp_get_current_user();
48
49
		return get_user_meta( $current_user->ID, self::VISIBLE_POST_BOXES, true );
50
	}
51
52
	/**
53
	 * Check whether the meta box in pages page is hidden or not.
54
	 *
55
	 * @since  5.0
56
	 * @static
57
	 * @access public
58
	 *
59
	 * @param string $box The name of the box to check
60
	 *
61
	 * @return bool True if the box is hidden, False otherwise
62
	 */
63
	public static function is_pages_box_hidden( $box ) {
64
		$hidden_boxes = self::get_pages_hidden_boxes();
65
66
		return is_array( $hidden_boxes ) && in_array( $box, $hidden_boxes );
67
	}
68
69
	/**
70
	 * Get the list of hidden boxes in posts page.
71
	 *
72
	 * @since  5.0
73
	 * @static
74
	 * @access public
75
	 *
76
	 * @return the array of hidden meta boxes
77
	 */
78
	public static function get_pages_hidden_boxes() {
79
		$current_user = wp_get_current_user();
80
81
		return get_user_meta( $current_user->ID, self::VISIBLE_PAGE_BOXES, true );
82
	}
83
84
	/**
85
	 * Check whether the meta box in users page is hidden or not.
86
	 *
87
	 * @static
88
	 * @access public
89
	 *
90
	 * @param string $box The name of the box to check
91
	 *
92
	 * @return bool True if the box is hidden, False otherwise
93
	 */
94
	public static function is_users_box_hidden( $box ) {
95
		$hidden_boxes = self::get_users_hidden_boxes();
96
97
		return is_array( $hidden_boxes ) && in_array( $box, $hidden_boxes );
98
	}
99
100
	/**
101
	 * Get the list of hidden boxes in users page.
102
	 *
103
	 * @static
104
	 * @access public
105
	 *
106
	 * @return array The array of hidden meta boxes
107
	 */
108
	public static function get_users_hidden_boxes() {
109
		$current_user = wp_get_current_user();
110
111
		return get_user_meta( $current_user->ID, self::VISIBLE_USER_BOXES, true );
112
	}
113
114
	/**
115
	 * Generate display name from post type and status.
116
	 *
117
	 * @static
118
	 *
119
	 * @param string $str
120
	 *
121
	 * @return string Label
122
	 */
123
	public static function display_post_type_status( $str ) {
124
		$type_status = self::split_post_type_status( $str );
125
126
		$status = $type_status['status'];
127
		$type   = $type_status['type'];
128
		$label  = '';
129
130
		switch ( $status ) {
131
			case 'private':
132
				$label = $type . ' - Private Posts';
133
				break;
134
			case 'future':
135
				$label = $type . ' - Scheduled Posts';
136
				break;
137
			case 'draft':
138
				$label = $type . ' - Draft Posts';
139
				break;
140
			case 'pending':
141
				$label = $type . ' - Pending Posts';
142
				break;
143
			case 'publish':
144
				$label = $type . ' - Published Posts';
145
				break;
146
		}
147
148
		return $label;
149
	}
150
151
	/**
152
	 * Split post type and status.
153
	 *
154
	 * @static
155
	 * @access public
156
	 *
157
	 * @param string $str
158
	 *
159
	 * @return array
160
	 */
161
	public static function split_post_type_status( $str ) {
162
		$type_status = array();
163
164
		$str_arr = explode( '-', $str );
165
166
		if ( count( $str_arr ) > 1 ) {
167
			$type_status['status'] = end( $str_arr );
168
			$type_status['type']   = implode( '-', array_slice( $str_arr, 0, -1 ) );
169
		} else {
170
			$type_status['status'] = 'publish';
171
			$type_status['type']   = $str;
172
		}
173
174
		return $type_status;
175
	}
176
}
177