PluginProbe
Adminimize / 1.11.1
Adminimize v1.11.1
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.1, at inc-setup/helping_hands.php

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