PageRenderTime 22ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/adminimize/inc-setup/helping_hands.php

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