PluginProbe
Adminimize / 1.11.6
Adminimize v1.11.6
1.11.14 1.11.13 1.11.1 1.11.10 1.11.11 1.11.12 1.11.2 1.11.3 1.11.4 1.11.5 1.11.6 1.11.7 1.11.8 1.11.9 1.3 1.4.7 1.6 1.7.12 1.7.13 1.7.14 1.7.15 1.7.16 1.7.17 1.7.18 1.7.19 All 53 releases
adminimize / inc-setup / helping_hands.php

helping_hands.php in Adminimize 1.11.6, at inc-setup/helping_hands.php

230 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helper functions.
4 *
5 * @package Adminimize
6 * @subpackage Helping_Functions
7 * @author Frank Bültge <frank@bueltge.de
8 * @since 2016-01-22
9 */
10
11 if ( ! function_exists( 'add_action' ) ) {
12 die( "Hi there! I'm just a part of plugin, not much I can do when called directly." );
13 }
14
15 /**
16 * Recursive search in array.
17 *
18 * @param string $needle
19 * @param array $haystack
20 *
21 * @return bool
22 */
23 function _mw_adminimize_recursive_in_array( $needle, $haystack ) {
24
25 if ( '' === $haystack ) {
26 return false;
27 }
28
29 if ( ! $haystack ) {
30 return false;
31 }
32
33 foreach ( $haystack as $stalk ) {
34 if ( $needle === $stalk
35 || ( is_array( $stalk )
36 && _mw_adminimize_recursive_in_array( $needle, $stalk )
37 )
38 ) {
39 return true;
40 }
41 }
42
43 return false;
44 }
45
46 /**
47 * Check if array contains all array values from another array.
48 *
49 * @param array $array1
50 * @param array $array2
51 *
52 * @return bool
53 */
54 function _mw_adminimize_in_arrays( $array1, $array2 ) {
55
56 return (bool) count( array_intersect( $array1, $array2 ) );
57 }
58
59 /**
60 * Check the role with the current user data.
61 *
62 * @param string $role
63 *
64 * @return bool
65 */
66 function _mw_adminimize_current_user_has_role( $role ) {
67
68 $user = wp_get_current_user();
69 if ( in_array( $role, (array) $user->roles, true ) ) {
70 return true;
71 }
72
73 return false;
74 }
75
76 /**
77 * Simple helper to debug to the console of the browser.
78 * Set WP_DEBUG_DISPLAY in your wp-config.php to true for view debug messages inside the console.
79 *
80 * @param string | array | object
81 * @param string $description
82 *
83 * @return string|void
84 */
85 function _mw_adminimize_debug( $data, $description = '' ) {
86
87 // Don't run on export data of Adminimize settings.
88 if ( isset( $_POST['_mw_adminimize_export'] ) ) {
89 return;
90 }
91
92 if ( ! _mw_adminimize_get_option_value( 'mw_adminimize_debug' ) ) {
93 return;
94 }
95
96 if ( '' === $description ) {
97 $description = 'Debug in Console via Adminimize Plugin:';
98 }
99
100 // Buffering to solve problems with WP core, header() etc.
101 ob_start();
102 $output = 'console.info(' . json_encode( $description ) . ');';
103 $output .= 'console.log(' . json_encode( $data ) . ');';
104 $output = sprintf( '<script>%s</script>', $output );
105
106 echo $output;
107 }
108
109 /**
110 * Return duplicate items from array.
111 *
112 * @param $array
113 *
114 * @return array
115 */
116 function _mw_adminimize_get_duplicate( $array ) {
117
118 return array_unique( array_map( 'unserialize', array_diff_assoc( array_map( 'serialize', $array), array_map( 'serialize', array_unique( $array, SORT_REGULAR ) ) ) ), SORT_REGULAR );
119 }
120
121 /**
122 * Get intersection of a multiple array.
123 *
124 * @since 2016-06-28
125 *
126 * @param $array array Array with settings of all roles.
127 *
128 * @return array Data with only the data, there in each role active.
129 */
130 function _mw_adminimize_get_intersection( $array ) {
131
132 return (array) call_user_func_array( 'array_intersect', $array );
133 }
134
135 /**
136 * Flatten a multi-dimensional array in a simple array.
137 *
138 * @since 2016-11-19
139 *
140 * @param array $array
141 *
142 * @return array $flat
143 */
144 function _mw_adminimize_array_flatten( $array ) {
145
146 $flat = array();
147 foreach ( $array as $key => $value ) {
148 if ( is_array( $value ) ) {
149 $flat = array_merge( $flat, _mw_adminimize_array_flatten( $value ) );
150 } else {
151 $flat[ $key ] = $value;
152 }
153 }
154
155 return $flat;
156 }
157
158 /**
159 * Break the access to a page.
160 *
161 * @param string $slug Slug of each menu item.
162 *
163 * @return bool If the check is true, return also true as bool.
164 */
165 function _mw_adminimize_check_page_access( $slug ) {
166
167 // If this default behavior is deactivated.
168 if ( _mw_adminimize_get_option_value( 'mw_adminimize_prevent_page_access' ) ) {
169 return false;
170 }
171
172 $url = basename( esc_url_raw( $_SERVER['REQUEST_URI'] ) );
173 $url = htmlspecialchars( $url );
174
175 if ( ! isset( $url ) ) {
176 return false;
177 }
178
179 $uri = wp_parse_url( $url );
180
181 if ( ! isset( $uri['path'] ) ) {
182 return false;
183 }
184
185 // URI without query parameter, like WP core edit.php.
186 if ( ! isset( $uri['query'] ) && strpos( $uri['path'], $slug ) !== false ) {
187 add_action( 'load-' . $slug, '_mw_adminimize_block_page_access' );
188 return true;
189 }
190
191 // URL is equal the slug of WP menu.
192 if ( $slug === $url ) {
193 add_action( 'load-' . basename( $uri['path'] ), '_mw_adminimize_block_page_access' );
194 return true;
195 }
196 }
197
198 /**
199 * Break the access to a page.
200 *
201 * @wp-hook load-$page_slug
202 */
203 function _mw_adminimize_block_page_access() {
204
205 $message = esc_attr__( 'Cheatin&#8217; uh? Sorry, you are not allowed to access this site.', 'adminimize' );
206 $message = apply_filters( 'adminimize_nopage_access_message', $message );
207 wp_die( esc_html( $message ) );
208 }
209
210 /**
211 * Check option value string in array and get string back for active checkboxes.
212 *
213 * @param string $option String of option to check.
214 * @param array $haystack Array of all options.
215 *
216 * @return string String for checked input box or empty string.
217 */
218 function _mw_adminimize_is_checked( $option, $haystack ) {
219
220 if ( ! isset( $haystack ) ) {
221 return '';
222 }
223
224 if ( in_array( htmlspecialchars_decode( $option ), $haystack, true ) ) {
225 return ' checked="checked"';
226 }
227
228 return '';
229 }
230