| 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 |
if ( ! _mw_adminimize_get_option_value( 'mw_adminimize_debug' ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! class_exists( 'DebugListener' ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// Buffering. |
| 95 |
ob_start(); |
| 96 |
$output = ''; |
| 97 |
$output .= 'console.info(' . json_encode( $description ) . ');'; |
| 98 |
$output .= 'console.log(' . json_encode( $data ) . ');'; |
| 99 |
|
| 100 |
echo sprintf( '<script>%s</script>', $output ); |
| 101 |
|
| 102 |
do_action( 'adminimize.log', $description, $data ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Return duplicate items from array. |
| 107 |
* |
| 108 |
* @param $array |
| 109 |
* |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
function _mw_adminimize_get_duplicate( $array ) { |
| 113 |
|
| 114 |
return array_unique( array_map( 'unserialize', array_diff_assoc( array_map( 'serialize', $array), array_map( 'serialize', array_unique( $array, SORT_REGULAR ) ) ) ), SORT_REGULAR ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get intersection of a multiple array. |
| 119 |
* |
| 120 |
* @since 2016-06-28 |
| 121 |
* |
| 122 |
* @param $array array Array with settings of all roles. |
| 123 |
* |
| 124 |
* @return array Data with only the data, there in each role active. |
| 125 |
*/ |
| 126 |
function _mw_adminimize_get_intersection( $array ) { |
| 127 |
|
| 128 |
return (array) call_user_func_array( 'array_intersect', array_values( $array ) ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Flatten a multi-dimensional array in a simple array. |
| 133 |
* |
| 134 |
* @since 2016-11-19 |
| 135 |
* |
| 136 |
* @param array $array |
| 137 |
* |
| 138 |
* @return array $flat |
| 139 |
*/ |
| 140 |
function _mw_adminimize_array_flatten( $array ) { |
| 141 |
|
| 142 |
$flat = array(); |
| 143 |
foreach ( $array as $key => $value ) { |
| 144 |
if ( is_array( $value ) ) { |
| 145 |
$flat = array_merge( $flat, _mw_adminimize_array_flatten( $value ) ); |
| 146 |
} else { |
| 147 |
$flat[ $key ] = $value; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return $flat; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Break the access to a page. |
| 156 |
* |
| 157 |
* @param string $slug Slug of each menu item. |
| 158 |
* |
| 159 |
* @return bool If the check is true, return also true as bool. |
| 160 |
*/ |
| 161 |
function _mw_adminimize_check_page_access( $slug ) { |
| 162 |
|
| 163 |
// If this default behavior is deactivated. |
| 164 |
if ( _mw_adminimize_get_option_value( 'mw_adminimize_prevent_page_access' ) ) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
$url = basename( esc_url_raw( $_SERVER['REQUEST_URI'] ) ); |
| 169 |
$url = htmlspecialchars( $url ); |
| 170 |
|
| 171 |
if ( ! isset( $url ) ) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
$uri = wp_parse_url( $url ); |
| 176 |
|
| 177 |
if ( ! isset( $uri['path'] ) ) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
// URI without query parameter, like WP core edit.php. |
| 182 |
if ( ! isset( $uri['query'] ) && strpos( $uri['path'], $slug ) !== false ) { |
| 183 |
add_action( 'load-' . $slug, '_mw_adminimize_block_page_access' ); |
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
// URL is equal the slug of WP menu. |
| 188 |
if ( $slug === $url ) { |
| 189 |
add_action( 'load-' . basename( $uri['path'] ), '_mw_adminimize_block_page_access' ); |
| 190 |
return true; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Break the access to a page. |
| 196 |
* |
| 197 |
* @wp-hook load-$page_slug |
| 198 |
*/ |
| 199 |
function _mw_adminimize_block_page_access() { |
| 200 |
|
| 201 |
$message = esc_attr__( 'Cheatin’ uh? Sorry, you are not allowed to access this site.', 'adminimize' ); |
| 202 |
$message = apply_filters( 'adminimize_nopage_access_message', $message ); |
| 203 |
wp_die( esc_html( $message ) ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Check option value string in array and get string back for active checkboxes. |
| 208 |
* |
| 209 |
* @param string $option String of option to check. |
| 210 |
* @param array $haystack Array of all options. |
| 211 |
* |
| 212 |
* @return string String for checked input box or empty string. |
| 213 |
*/ |
| 214 |
function _mw_adminimize_is_checked( $option, $haystack ) { |
| 215 |
|
| 216 |
if ( ! isset( $haystack ) ) { |
| 217 |
return ''; |
| 218 |
} |
| 219 |
|
| 220 |
if ( in_array( htmlspecialchars_decode( $option ), $haystack, true ) ) { |
| 221 |
return ' checked="checked"'; |
| 222 |
} |
| 223 |
|
| 224 |
return ''; |
| 225 |
} |