MS_Helper_Utility

This Helper creates additional utility functions.

Defined (1)

The class is defined in the following location(s).

/app/helper/class-ms-helper-utility.php  
  1. class MS_Helper_Utility extends MS_Helper { 
  2.  
  3. /** 
  4. * Implements a multi-dimensional array_intersect_assoc. 
  5. * The standard array_intersect_assoc does the intersection based on string 
  6. * values of the keys. 
  7. * We need to find a way to recursively check multi-dimensional arrays. 
  8. * Note that we are not passing values here but references. 
  9. * @since 1.0.0 
  10. * @param mixed $arr1 First array to intersect. 
  11. * @param mixed $arr2 Second array to intersect. 
  12. * @return array Combined array. 
  13. */ 
  14. public static function array_intersect_assoc_deep( &$arr1, &$arr2 ) { 
  15. // If not arrays, at least associate the strings. 
  16. // If 1 argument is an array and the other not throw error. 
  17. if ( ! is_array( $arr1 ) && ! is_array( $arr2 ) ) { 
  18. $arr1 = (string) $arr1; 
  19. $arr2 = (string) $arr2; 
  20. return $arr1 == $arr2 ? $arr1 : false; 
  21. } elseif ( is_array( $arr1 ) !== is_array( $arr2 ) ) { 
  22. MS_Helper_Debug::log( 
  23. 'WARNING: MS_Helper_Utility::array_intersect_assoc_deep() - ' . 
  24. 'Both params need to be of same type (array or string).',  
  25. true 
  26. ); 
  27. return false; 
  28.  
  29. $intersections = array_intersect( 
  30. array_keys( $arr1 ),  
  31. array_keys( $arr2 ) 
  32. ); 
  33.  
  34. $assoc_array = array(); 
  35.  
  36. // Time to recursively run through the arrays 
  37. foreach ( $intersections as $key ) { 
  38. $result = MS_Helper_Utility::array_intersect_assoc_deep( 
  39. $arr1[ $key ],  
  40. $arr2[ $key ] 
  41. ); 
  42.  
  43. if ( $result ) { 
  44. $assoc_array[ $key ] = $result; 
  45.  
  46. return apply_filters( 
  47. 'ms_helper_utility_array_intersect_assoc_deep',  
  48. $assoc_array,  
  49. $arr1,  
  50. $arr2 
  51. ); 
  52.  
  53. /** 
  54. * Get the current page URL. 
  55. * @since 1.0.0 
  56. * @return string The URL. 
  57. */ 
  58. public static function get_current_url( $force_ssl = false ) { 
  59. static $Url = null; 
  60.  
  61. if ( null === $Url ) { 
  62. $Url = 'http://'; 
  63.  
  64. if ( $force_ssl ) { 
  65. $Url = 'https://'; 
  66. } elseif ( isset( $_SERVER['HTTPS'] ) && 'on' == $_SERVER['HTTPS'] ) { 
  67. $Url = 'https://'; 
  68.  
  69. $Url .= $_SERVER['SERVER_NAME']; 
  70. if ( $_SERVER['SERVER_PORT'] != '80' ) { 
  71. $Url .= ':' . $_SERVER['SERVER_PORT']; 
  72. $Url .= $_SERVER['REQUEST_URI']; 
  73.  
  74. $Url = apply_filters( 
  75. 'ms_helper_utility_get_current_url',  
  76. $Url 
  77. ); 
  78.  
  79. return $Url; 
  80.  
  81. /** 
  82. * Replace http protocol to https 
  83. * @since 1.0.0 
  84. * @param string $url the original url 
  85. * @return string The changed url. 
  86. */ 
  87. public static function get_ssl_url( $url ) { 
  88. //TODO: There is a Wordpress function that does the same thing... 
  89. // Remove this function and replace with WP core function. 
  90. return apply_filters( 
  91. 'ms_helper_utility_get_ssl_url',  
  92. preg_replace( '|^http://|', 'https://', $url ),  
  93. $url 
  94. ); 
  95.  
  96. /** 
  97. * Returns user IP address. 
  98. * @since 1.0.0 
  99. * @static 
  100. * @access protected 
  101. * @return string Remote IP address on success, otherwise FALSE. 
  102. */ 
  103. protected static function get_remote_ip() { 
  104. $flag = ! WP_DEBUG ? FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE : null; 
  105. $keys = array( 
  106. 'HTTP_CLIENT_IP',  
  107. 'HTTP_X_FORWARDED_FOR',  
  108. 'HTTP_X_FORWARDED',  
  109. 'HTTP_X_CLUSTER_CLIENT_IP',  
  110. 'HTTP_FORWARDED_FOR',  
  111. 'HTTP_FORWARDED',  
  112. 'REMOTE_ADDR',  
  113. ); 
  114.  
  115. $remote_ip = false; 
  116. foreach ( $keys as $key ) { 
  117. if ( array_key_exists( $key, $_SERVER ) === true ) { 
  118. foreach ( array_filter( array_map( 'trim', explode( ', ', $_SERVER[$key] ) ) ) as $ip ) { 
  119. if ( filter_var( $ip, FILTER_VALIDATE_IP, $flag ) !== false ) { 
  120. $remote_ip = $ip; 
  121. break; 
  122.  
  123. return $remote_ip; 
  124.  
  125. /** 
  126. * Register a post type. 
  127. * @since 1.0.0 
  128. * @param string $post_type Post type ID. 
  129. * @param array $args Post type details. 
  130. */ 
  131. public static function register_post_type( $post_type, $args = null ) { 
  132. $defaults = array( 
  133. 'public' => false,  
  134. 'has_archive' => false,  
  135. 'publicly_queryable' => false,  
  136. 'supports' => false,  
  137. 'hierarchical' => false,  
  138. ); 
  139.  
  140. $args = wp_parse_args( $args, $defaults ); 
  141.  
  142. register_post_type( $post_type, $args ); 
  143.  
  144. /** 
  145. * Transforms the $key value into a color index. The same key will always 
  146. * return the same color 
  147. * @since 1.0.0 
  148. * @param string $key Some name/ID value 
  149. * @return string HTML color code (#123456) 
  150. */ 
  151. public static function color_index( $key ) { 
  152. static $Colors = array(); 
  153. $key = strtolower( $key ); 
  154.  
  155. if ( ! isset( $Colors[$key] ) ) { 
  156. $base_hash = md5( $key ); 
  157.  
  158. $h = hexdec( substr( $base_hash, 0, 2 ) ) + 
  159. hexdec( substr( $base_hash, 10, 2 ) ) + 
  160. hexdec( substr( $base_hash, 20, 2 ) ); 
  161.  
  162. $s = 35 + ( hexdec( substr( $base_hash, 1, 2 ) ) % 15 ); 
  163.  
  164. $l = 45 + ( hexdec( substr( $base_hash, 2, 2 ) ) % 15 ); 
  165.  
  166. $Colors[$key] = self::hsl2web( $h, $s / 100, $l / 100 ); 
  167.  
  168. return $Colors[$key]; 
  169.  
  170. /** 
  171. * Takes Hue/Saturation/Lightness color definition and returns a hex color code 
  172. * @since 1.0.0 
  173. * @param float $h 
  174. * @param float $s 
  175. * @param float $l 
  176. * @return string 
  177. */ 
  178. static public function hsl2web( $h, $s, $l ) { 
  179. $r = 0; $g = 0; $b = 0; 
  180.  
  181. $h %= 360; 
  182. $c = ( 1 - abs( 2 * $l - 1 ) ) * $s; 
  183. $x = $c * ( 1 - abs( fmod( ( $h / 60 ), 2 ) - 1 ) ); 
  184. $m = $l - ( $c / 2 ); 
  185.  
  186. if ( $h < 60 ) { 
  187. $r = $c; $g = $x; $b = 0; 
  188. } else if ( $h < 120 ) { 
  189. $r = $x; $g = $c; $b = 0; 
  190. } else if ( $h < 180 ) { 
  191. $r = 0; $g = $c; $b = $x; 
  192. } else if ( $h < 240 ) { 
  193. $r = 0; $g = $x; $b = $c; 
  194. } else if ( $h < 300 ) { 
  195. $r = $x; $g = 0; $b = $c; 
  196. } else { 
  197. $r = $c; $g = 0; $b = $x; 
  198.  
  199. $r = str_pad( dechex( ( $r + $m ) * 255 ), 2, '0', STR_PAD_LEFT ); 
  200. $g = str_pad( dechex( ( $g + $m ) * 255 ), 2, '0', STR_PAD_LEFT ); 
  201. $b = str_pad( dechex( ( $b + $m ) * 255 ), 2, '0', STR_PAD_LEFT ); 
  202.  
  203. return '#' . $r . $g . $b; 
  204.  
  205. /** 
  206. * Determine if the user currently is on the specified URL 
  207. * @since 1.0.0 
  208. * @param string $url The URL to check 
  209. * @return bool 
  210. */ 
  211. static public function is_current_url( $url, $ignore = array( 'paged' ) ) { 
  212. static $Cur_url = null; 
  213.  
  214. if ( null === $Cur_url ) { 
  215. $query_string = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY ); 
  216. parse_str( $query_string, $Cur_url ); 
  217. foreach ( $ignore as $param ) { unset( $Cur_url[$param] ); } 
  218.  
  219. $query_string = parse_url( $url, PHP_URL_QUERY ); 
  220. parse_str( $query_string, $params ); 
  221. foreach ( $ignore as $param ) { unset( $params[$param] ); } 
  222.  
  223. return ( $params == $Cur_url ); 
  224.  
  225. /** 
  226. * Returns the *correct* home-url for front-end pages. 
  227. * By default the home_url() function ignores the is_ssl() flag when it's 
  228. * called from the admin-dashboard. So when redirecting from dashboard to 
  229. * the front-page it will usually always redirect to http:// even when the 
  230. * front-end is on https:// - this function fixes this. 
  231. * @since 1.0.1.2 
  232. * @param string $path Argument passed to the home_url() function. 
  233. * @return string The correct URL for a front-end page. 
  234. */ 
  235. static public function home_url( $path = '' ) { 
  236. return self::get_home_url( null, $path ); 
  237.  
  238. /** 
  239. * Returns the *correct* home-url for front-end pages of a given site. 
  240. * {@see description of home_url above for details} 
  241. * @since 1.0.1.2 
  242. * @param int $blog_id Blog-ID; by default the current blog is used. 
  243. * @param string $path Argument passed to the home_url() function. 
  244. * @return string The correct URL for a front-end page. 
  245. */ 
  246. static public function get_home_url( $blog_id = null, $path = '' ) { 
  247. $schema = is_ssl() ? 'https' : 'http'; 
  248. $url = get_home_url( $blog_id, $path, $schema ); 
  249.  
  250. return apply_filters( 
  251. 'ms_helper_home_url',  
  252. $url,  
  253. $blog_id,  
  254. $path,  
  255. $schema 
  256. );