| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Adminimize |
| 4 |
* @subpackage Helping_Functions |
| 5 |
* @author Frank Bültge <frank@bueltge.de |
| 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 |
* @param string $slug Slug of each menu item. |
| 156 |
* |
| 157 |
* @return bool If the check is true, return also true as bool. |
| 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 FALSE; |
| 164 |
} |
| 165 |
|
| 166 |
$url = basename( esc_url_raw( $_SERVER[ 'REQUEST_URI' ] ) ); |
| 167 |
$url = htmlspecialchars( $url ); |
| 168 |
|
| 169 |
if ( ! isset( $url ) ) { |
| 170 |
return FALSE; |
| 171 |
} |
| 172 |
|
| 173 |
$uri = parse_url( $url ); |
| 174 |
|
| 175 |
if ( ! isset( $uri[ 'path' ] ) ) { |
| 176 |
return FALSE; |
| 177 |
} |
| 178 |
|
| 179 |
// URI without query parameter, like WP core edit.php. |
| 180 |
if ( ! isset( $uri[ 'query' ] ) && strpos( $uri[ 'path' ], $slug ) !== FALSE ) { |
| 181 |
add_action( 'load-' . $slug, '_mw_adminimize_block_page_access' ); |
| 182 |
return TRUE; |
| 183 |
} |
| 184 |
|
| 185 |
// URL is equal the slug of WP menu. |
| 186 |
if ( $slug === $url ) { |
| 187 |
add_action( 'load-' . basename( $uri[ 'path' ] ), '_mw_adminimize_block_page_access' ); |
| 188 |
return TRUE; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Break the access to a page. |
| 194 |
* |
| 195 |
* @wp-hook load-$page_slug |
| 196 |
*/ |
| 197 |
function _mw_adminimize_block_page_access() { |
| 198 |
|
| 199 |
$message = esc_attr__( 'Cheatin’ uh? Sorry, you are not allowed to access this site.', 'adminimize' ); |
| 200 |
$message = apply_filters( 'adminimize_nopage_access_message', $message ); |
| 201 |
wp_die( $message ); |
| 202 |
} |