| @@ -1,14 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Helper functions. | |
| 4 | - * | |
| 5 | 3 | * @package Adminimize |
| 6 | 4 | * @subpackage Helping_Functions |
| 7 | 5 | * @author Frank Bültge <[email protected] |
| 8 | 6 | * @since 2016-01-22 |
| 9 | 7 | */ |
| 10 | - | |
| 11 | 8 | if ( ! function_exists( 'add_action' ) ) { |
| 12 | 9 | die( "Hi there! I'm just a part of plugin, not much I can do when called directly." ); |
| 13 | 10 | } |
| 14 | 11 | |
| @@ -14,10 +11,10 @@ | ||
| 14 | 11 | |
| 15 | 12 | /** |
| 16 | 13 | * Recursive search in array. |
| 17 | 14 | * |
| 18 | - * @param string $needle | |
| 19 | - * @param array $haystack | |
| 15 | + * @param $needle | |
| 16 | + * @param $haystack | |
| 20 | 17 | * |
| 21 | 18 | * @return bool |
| 22 | 19 | */ |
| 23 | 20 | function _mw_adminimize_recursive_in_array( $needle, $haystack ) { |
| @@ -22,13 +19,13 @@ | ||
| 22 | 19 | */ |
| 23 | 20 | function _mw_adminimize_recursive_in_array( $needle, $haystack ) { |
| 24 | 21 | |
| 25 | 22 | if ( '' === $haystack ) { |
| 26 | - return false; | |
| 23 | + return FALSE; | |
| 27 | 24 | } |
| 28 | 25 | |
| 29 | 26 | if ( ! $haystack ) { |
| 30 | - return false; | |
| 27 | + return FALSE; | |
| 31 | 28 | } |
| 32 | 29 | |
| 33 | 30 | foreach ( $haystack as $stalk ) { |
| 34 | 31 | if ( $needle === $stalk |
| @@ -35,20 +32,20 @@ | ||
| 35 | 32 | || ( is_array( $stalk ) |
| 36 | 33 | && _mw_adminimize_recursive_in_array( $needle, $stalk ) |
| 37 | 34 | ) |
| 38 | 35 | ) { |
| 39 | - return true; | |
| 36 | + return TRUE; | |
| 40 | 37 | } |
| 41 | 38 | } |
| 42 | 39 | |
| 43 | - return false; | |
| 40 | + return FALSE; | |
| 44 | 41 | } |
| 45 | 42 | |
| 46 | 43 | /** |
| 47 | 44 | * Check if array contains all array values from another array. |
| 48 | 45 | * |
| 49 | - * @param array $array1 | |
| 50 | - * @param array $array2 | |
| 46 | + * @param $array1 | |
| 47 | + * @param $array2 | |
| 51 | 48 | * |
| 52 | 49 | * @return bool |
| 53 | 50 | */ |
| 54 | 51 | function _mw_adminimize_in_arrays( $array1, $array2 ) { |
| @@ -55,185 +52,67 @@ | ||
| 55 | 52 | |
| 56 | 53 | return (bool) count( array_intersect( $array1, $array2 ) ); |
| 57 | 54 | } |
| 58 | 55 | |
| 56 | +// Fix some badly enqueued scripts with no sense of HTTPS. | |
| 57 | +// Kudos to http://snippets.webaware.com.au/snippets/cleaning-up-wordpress-plugin-script-and-stylesheet-loads-over-ssl/ | |
| 58 | +add_action( 'wp_print_scripts', '_mw_adminimize_enqueueScriptsFix', 100 ); | |
| 59 | +add_action( 'wp_print_styles', '_mw_adminimize_enqueueStylesFix', 100 ); | |
| 60 | + | |
| 59 | 61 | /** |
| 60 | - * Check the role with the current user data. | |
| 61 | - * | |
| 62 | - * @param string $role | |
| 63 | - * | |
| 64 | - * @return bool | |
| 62 | + * Force plugins to load scripts with SSL if page is SSL. | |
| 65 | 63 | */ |
| 66 | -function _mw_adminimize_current_user_has_role( $role ) { | |
| 64 | +function _mw_adminimize_enqueueScriptsFix() { | |
| 67 | 65 | |
| 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' ) ) { | |
| 66 | + if ( is_admin() ) { | |
| 87 | 67 | return; |
| 88 | 68 | } |
| 89 | 69 | |
| 90 | - if ( ! class_exists( 'DebugListener' ) ) { | |
| 70 | + $https_values = array( NULL, 'off' ); | |
| 71 | + if ( ! isset( $_SERVER[ 'HTTPS' ] ) || in_array( $_SERVER[ 'HTTPS' ], $https_values ) ) { | |
| 91 | 72 | return; |
| 92 | 73 | } |
| 93 | 74 | |
| 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; | |
| 75 | + foreach ( (array) $GLOBALS[ 'wp_scripts' ]->registered as $script ) { | |
| 76 | + if ( FALSE !== stripos( $script->src, 'http://', 0 ) ) { | |
| 77 | + $script->src = str_replace( 'http://', 'https://', $script->src ); | |
| 148 | 78 | } |
| 149 | 79 | } |
| 150 | - | |
| 151 | - return $flat; | |
| 152 | 80 | } |
| 153 | 81 | |
| 154 | 82 | /** |
| 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. | |
| 83 | + * Force plugins to load styles with SSL if page is SSL. | |
| 160 | 84 | */ |
| 161 | -function _mw_adminimize_check_page_access( $slug ) { | |
| 85 | +function _mw_adminimize_enqueueStylesFix() { | |
| 162 | 86 | |
| 163 | - // If this default behavior is deactivated. | |
| 164 | - if ( _mw_adminimize_get_option_value( 'mw_adminimize_prevent_page_access' ) ) { | |
| 165 | - return false; | |
| 87 | + if ( is_admin() ) { | |
| 88 | + return; | |
| 166 | 89 | } |
| 167 | 90 | |
| 168 | - $url = basename( esc_url_raw( $_SERVER['REQUEST_URI'] ) ); | |
| 169 | - $url = htmlspecialchars( $url ); | |
| 170 | - | |
| 171 | - if ( ! isset( $url ) ) { | |
| 172 | - return false; | |
| 91 | + $https_values = array( NULL, 'off' ); | |
| 92 | + if ( ! isset( $_SERVER[ 'HTTPS' ] ) || in_array( $_SERVER[ 'HTTPS' ], $https_values ) ) { | |
| 93 | + return; | |
| 173 | 94 | } |
| 174 | 95 | |
| 175 | - $uri = wp_parse_url( $url ); | |
| 176 | - | |
| 177 | - if ( ! isset( $uri['path'] ) ) { | |
| 178 | - return false; | |
| 179 | - } | |
| 180 | - | |
| 181 | - // Parse the slug to extract its path and query components for comparison. | |
| 182 | - $slug_parts = wp_parse_url( $slug ); | |
| 183 | - $slug_path = isset( $slug_parts['path'] ) ? $slug_parts['path'] : $slug; | |
| 184 | - $slug_query = isset( $slug_parts['query'] ) ? $slug_parts['query'] : ''; | |
| 185 | - | |
| 186 | - // Check if the current page path matches the blocked slug path. | |
| 187 | - if ( strpos( $uri['path'], $slug_path ) !== false ) { | |
| 188 | - // If the slug has its own query params (e.g. edit.php?post_type=page), | |
| 189 | - // verify those params are present in the current request. | |
| 190 | - if ( $slug_query ) { | |
| 191 | - $request_query = isset( $uri['query'] ) ? $uri['query'] : ''; | |
| 192 | - parse_str( $slug_query, $slug_params ); | |
| 193 | - parse_str( $request_query, $request_params ); | |
| 194 | - | |
| 195 | - // All slug query params must be present in the request. | |
| 196 | - foreach ( $slug_params as $key => $value ) { | |
| 197 | - if ( ! isset( $request_params[ $key ] ) || $request_params[ $key ] !== $value ) { | |
| 198 | - return false; | |
| 199 | - } | |
| 200 | - } | |
| 96 | + foreach ( (array) $GLOBALS[ 'wp_styles' ]->registered as $script ) { | |
| 97 | + if ( FALSE !== stripos( $script->src, 'http://', 0 ) ) { | |
| 98 | + $script->src = str_replace( 'http://', 'https://', $script->src ); | |
| 201 | 99 | } |
| 202 | - | |
| 203 | - add_action( 'load-' . $uri['path'], '_mw_adminimize_block_page_access' ); | |
| 204 | - return true; | |
| 205 | 100 | } |
| 206 | 101 | } |
| 207 | 102 | |
| 208 | 103 | /** |
| 209 | - * Break the access to a page. | |
| 104 | + * Check the role with the current user data. | |
| 210 | 105 | * |
| 211 | - * @wp-hook load-$page_slug | |
| 212 | - */ | |
| 213 | -function _mw_adminimize_block_page_access() { | |
| 214 | - | |
| 215 | - $message = esc_attr__( 'Cheatin’ uh? Sorry, you are not allowed to access this site.', 'adminimize' ); | |
| 216 | - $message = apply_filters( 'adminimize_nopage_access_message', $message ); | |
| 217 | - wp_die( esc_html( $message ) ); | |
| 218 | -} | |
| 219 | - | |
| 220 | -/** | |
| 221 | - * Check option value string in array and get string back for active checkboxes. | |
| 106 | + * @param string $role | |
| 222 | 107 | * |
| 223 | - * @param string $option String of option to check. | |
| 224 | - * @param array $haystack Array of all options. | |
| 225 | - * | |
| 226 | - * @return string String for checked input box or empty string. | |
| 108 | + * @return bool | |
| 227 | 109 | */ |
| 228 | -function _mw_adminimize_is_checked( $option, $haystack ) { | |
| 110 | +function _mw_adminimize_current_user_has_role( $role ) { | |
| 229 | 111 | |
| 230 | - if ( ! isset( $haystack ) ) { | |
| 231 | - return ''; | |
| 112 | + $user = wp_get_current_user(); | |
| 113 | + if ( in_array( $role, (array) $user->roles ) ) { | |
| 114 | + return TRUE; | |
| 232 | 115 | } |
| 233 | 116 | |
| 234 | - if ( in_array( htmlspecialchars_decode( $option ), $haystack, true ) ) { | |
| 235 | - return ' checked="checked"'; | |
| 236 | - } | |
| 237 | - | |
| 238 | - return ''; | |
| 117 | + return FALSE; | |
| 239 | 118 | } |