PageRenderTime 27ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/mega_main_menu/framework/common.php

https://gitlab.com/kidaa/portfolio
PHP | 386 lines | 292 code | 19 blank | 75 comment | 116 complexity | 094d0d52da4e6d6fc6c836a12a5df75b MD5 | raw file
  1. <?php
  2. /**
  3. * Tools Functions
  4. * @package MegaMain
  5. * @subpackage MegaMain
  6. * @since mm 1.0
  7. */
  8. if ( !class_exists( 'mm_common' ) ) {
  9. class mm_common {
  10. /**
  11. * The function return a newline (set any value in second argument to block newline)
  12. * and the number of tabs specified in the first argument.
  13. * Function has a small name and is available in any theme file.
  14. * @return $ntab - newline and tabs sumbols (example:\n\t\t).
  15. */
  16. public static function ntab( $number_of_tabs = 0, $newline = 'true' ) {
  17. $ntab = ( $newline === 'true' ) ? "\n" : "";
  18. for ($i = 0; $number_of_tabs > $i; $i++) {
  19. $ntab .= "\t";
  20. }
  21. return $ntab;
  22. }
  23. /**
  24. * The function return empty string (required for some other functions with "preg_replace_callback").
  25. */
  26. public static function return_empty_string( $variable ) {
  27. return '';
  28. }
  29. /**
  30. * The function return a vareable where only numbers.
  31. * @return $only_numbers - vareable where only numbers.
  32. */
  33. public static function get_only_numbers( $variable = false ) {
  34. $only_numbers = preg_replace_callback( '([^0-9,.]{1,})', array( 'mm_common', 'return_empty_string' ), $variable );
  35. return $only_numbers;
  36. }
  37. /**
  38. * The function return url if url is valid or return false if url is no valid.
  39. * @return $url.
  40. */
  41. public static function is_url( $variable = false ) {
  42. if( filter_var( $variable, FILTER_VALIDATE_URL ) === FALSE ) {
  43. return false;
  44. } else {
  45. return true;
  46. }
  47. }
  48. /**
  49. * The function return true if url exist or return false if url no exist.
  50. * @return boolean value.
  51. */
  52. public static function url_exist( $url = false ) {
  53. if ( !( $url = @parse_url( $url ) ) ) {
  54. return false;
  55. }
  56. $url['port'] = ( !isset( $url['port'] ) ) ? 80 : (int) $url['port'];
  57. $url['path'] = ( !empty( $url['path'] ) ) ? $url['path'] : '/';
  58. $url['path'] .= ( isset( $url['query'] ) ) ? "?$url[query]" : '';
  59. if ( isset( $url['host'] ) && $url['host'] != @gethostbyname( $url['host'] ) ) {
  60. if ( PHP_VERSION >= 5 ) {
  61. $headers = @implode( '', @get_headers( "$url[scheme]://$url[host]:$url[port]$url[path]" ) );
  62. } else {
  63. if ( !( $fp = @fsockopen( $url['host'], $url['port'], $errno, $errstr, 10 ) ) ) {
  64. return false;
  65. }
  66. fputs( $fp, "HEAD $url[path] HTTP/1.1\r\nHost: $url[host]\r\n\r\n" );
  67. $headers = fread( $fp, 4096 );
  68. fclose( $fp );
  69. }
  70. return (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
  71. }
  72. return false;
  73. }
  74. /**
  75. * The function return new unique script id on page.
  76. * @return integer value.
  77. */
  78. public static function script_id ( $name_space = 'mm_script_id', $reset = false ) {
  79. global $$name_space;
  80. if ( $reset === true || !isset( $$name_space ) || $$name_space === false ) {
  81. $GLOBALS[ $name_space ] = 0;
  82. }
  83. $GLOBALS[ $name_space ] ++;
  84. return $GLOBALS[ $name_space ];
  85. }
  86. /**
  87. * The function return excerpted text.
  88. * @return integer value.
  89. */
  90. public static function excerpt( $text = '', $length = 200, $ellipsis = '...' ) {
  91. $out = $text;
  92. if ( is_string( $text ) && strlen( $text ) > (int)$length ) {
  93. $snip = substr( strip_tags( preg_replace_callback ( "/\[(.*)\]/i", array( 'mm_common', 'return_empty_string' ), $text ) ), 0, $length );
  94. $out = substr( $snip, 0, strrpos( $snip, ' ' ) ) . $ellipsis;
  95. }
  96. return $out;
  97. }
  98. /**
  99. * The function return full content of the file from URL.
  100. * @return integer value.
  101. */
  102. public static function get_url_content( $url ) {
  103. $out = false;
  104. // cURL
  105. if ( ( $out == false ) ) {
  106. if ( function_exists( 'curl_init' ) ) {
  107. $ch = curl_init();
  108. curl_setopt( $ch, CURLOPT_URL, $url );
  109. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  110. $curl_info = curl_getinfo( $ch );
  111. if ( ( $curl_info[ 'http_code' ] == 400 ) || ( $curl_info[ 'http_code' ] == 302 ) || ( $curl_info[ 'http_code' ] == 303 ) ) {
  112. $out = false;
  113. } else {
  114. $out = curl_exec( $ch );
  115. }
  116. curl_close( $ch );
  117. } else {
  118. $out = false;
  119. }
  120. }
  121. // file_get_contents
  122. if ( ( $out == false ) ) {
  123. if ( ini_get( 'allow_url_fopen' ) && function_exists( 'file_get_contents' ) ) {
  124. $out = file_get_contents( $url );
  125. } else {
  126. $out = false;
  127. }
  128. }
  129. // fopen, fread, fclose
  130. if ( ( $out == false ) ) {
  131. if ( ( ini_get( 'allow_url_fopen' ) && function_exists('fopen') && function_exists('fread') ) || ( is_readable( $url ) ) ) {
  132. $handle = fopen( $url, "rb" );
  133. if ( $handle !== false ) {
  134. $out = fread( $handle, filesize( $url ) );
  135. fclose( $handle );
  136. } else {
  137. $out = false;
  138. }
  139. } else {
  140. $out = false;
  141. }
  142. }
  143. if ( empty( $out ) ) {
  144. $out = false;
  145. }
  146. return $out;
  147. }
  148. /**
  149. * The function return array with full list of the post types and their taxonomies.
  150. * @return integer value.
  151. */
  152. public static function get_all_taxonomies () {
  153. global $mm_get_all_taxonomies;
  154. $out = array();
  155. if ( isset( $mm_get_all_taxonomies ) && $mm_get_all_taxonomies !== false ) {
  156. $out = $mm_get_all_taxonomies;
  157. } else {
  158. $post_types = get_post_types( $args = array( 'public' => true ), 'names' );
  159. unset( $post_types['attachment'] );
  160. foreach ( $post_types as $post_types_key => $single_post_type ) {
  161. $out[ ucfirst( $single_post_type ) ] = $single_post_type;
  162. }
  163. foreach ( $out as $post_types_key => $single_post_type ) {
  164. $tax_obj[ $single_post_type ] = get_object_taxonomies( $single_post_type );
  165. $tax = $tax_obj;
  166. foreach ( $tax[ $single_post_type ] as $tax_type_key => $tax_type ) {
  167. $terms_obj[ $single_post_type ] = get_terms( $tax_type, '&orderby=name&hide_empty=0' );
  168. $terms = $terms_obj;
  169. foreach ( $terms[ $single_post_type ] as $terms_key => $terms_value ) {
  170. $out[ ucfirst( $single_post_type ) . ' &nbsp; -&gt; &nbsp; ' . $terms_value->name ] = $single_post_type . '/' . $terms_value->taxonomy . '=' . $terms_value->slug;
  171. }
  172. }
  173. }
  174. asort( $out );
  175. $GLOBALS['mm_get_all_taxonomies'] = $out;
  176. }
  177. return $out;
  178. }
  179. /**
  180. * The function return CSS font properties in a string.
  181. * @return string value.
  182. */
  183. public static function css_font( $args = array() ) {
  184. $args = wp_parse_args($args, $defaults = array( 'font_family' => '', 'font_color' => '', 'font_size' => '', 'font_weight' => '' ) );
  185. extract( $args );
  186. $font = '';
  187. if ( $font_family != '' && $font_family != false ) {
  188. if ( $font_family == 'Inherit' ) {
  189. $font .= "font-family: inherit;\n";
  190. } else {
  191. $font .= "font-family: " . $font_family . ", '" . $font_family . "';\n";
  192. }
  193. }
  194. if ( $font_color != '' && $font_color != false ) {
  195. $font .= "color: " . $font_color . ";\n";
  196. }
  197. if ( $font_size != '' && $font_size != false ) {
  198. $font .= "font-size: " . $font_size . "px;\n";
  199. }
  200. if ( $font_weight != '' && $font_weight != false ) {
  201. $font .= "font-weight: " . $font_weight . ";\n";
  202. }
  203. return $font;
  204. }
  205. /**
  206. * The function return CSS gradient properties in a string.
  207. * @return string value.
  208. */
  209. public static function css_gradient( $args = array() ) {
  210. $args = wp_parse_args($args, $defaults = array( 'color1' => 'transparent', 'color2' => 'transparent', 'start' => '0', 'end' => '100', 'orientation' => 'top' ) );
  211. extract( $args );
  212. $color1 = ( $color1 == '' || $color1 == false ) ? 'transparent' : $color1;
  213. $color2 = ( $color2 == '' || $color2 == false ) ? 'transparent' : $color2;
  214. $gradient = '';
  215. if ( ( $color1 == $color2 ) || ( $color2 != 'transparent' ) ) {
  216. $gradient .= 'background: ' . $color1 . ';';
  217. }
  218. if ( ( $color1 != 'transparent' ) || ( $color2 != 'transparent' ) ) {
  219. if ( $color1 != $color2 ) {
  220. if ( $orientation == 'radial' ) {
  221. $orient1 = 'radial-gradient(center, ellipse cover';
  222. $orient2 = 'radial, center center, 0px, center center, 100%';
  223. $orient3 = 'radial-gradient(ellipse at center';
  224. } else if ( $orientation == 'left' || $orientation == 'horizontal' ) {
  225. $orient1 = 'linear-gradient(left';
  226. $orient2 = 'linear, left top, right top';
  227. $orient3 = 'linear-gradient(to right';
  228. } else {
  229. $orient1 = 'linear-gradient(top';
  230. $orient2 = 'linear, left top, left bottom';
  231. $orient3 = 'linear-gradient(to bottom';
  232. }
  233. $gradient .= "background: -moz-" . $orient1 . ", " . $color1 . " " . $start . "%, " . $color2 . " " . $end . "%);\n";
  234. $gradient .= "background: -webkit-" . $orient1 . ", " . $color1 . " " . $start . "%, " . $color2 . " " . $end . "%);\n";
  235. $gradient .= "background: -o-" . $orient1 . ", " . $color1 . " " . $start . "%, " . $color2 . " " . $end . "%);\n";
  236. $gradient .= "background: -ms-" . $orient1 . ", " . $color1 . " " . $start . "%, " . $color2 . " " . $end . "%);\n";
  237. $gradient .= "background: -webkit-gradient(" . $orient2 . ", color-stop(" . $start . "%, " . $color1 . "), color-stop(" . $end . "%," . $color2 . "));\n";
  238. $gradient .= "background: " . $orient3 . ", " . $color1 . " " . $start . "%, " . $color2 . " " . $end . "%);\n";
  239. $gradient .= "-ms-filter: \"progid:DXImageTransform.Microsoft.gradient( startColorstr='" . $color1 . "', endColorstr='" . $color2 . "',GradientType=0 )\";\n";
  240. }
  241. }
  242. return $gradient;
  243. }
  244. /**
  245. * The function return CSS bg_image properties in a string.
  246. * @return string value.
  247. */
  248. public static function css_bg_image( $args = array() ) {
  249. $args = wp_parse_args($args, $defaults = array( 'background_image' => '', 'background_repeat' => 'repeat-x', 'background_position' => 'center right', 'background_attachment' => 'fixed', 'background_size' => '' ) );
  250. extract( $args );
  251. $bg_image = '';
  252. if ( $background_image != '' && $background_image != false ) {
  253. $bg_image .= "background-image: url('" . $background_image . "');\n";
  254. $bg_image .= "background-repeat: " . $background_repeat . ";\n";
  255. $bg_image .= "background-position: " . $background_position . ";\n";
  256. $bg_image .= "background-attachment: " . $background_attachment . ";\n";
  257. $bg_image .= "background-size: " . $background_size . ";\n";
  258. }
  259. return $bg_image;
  260. }
  261. /**
  262. * The function convert any color type to RGB(A) value.
  263. * @return string value.
  264. */
  265. public static function hex2rgba( $color = '#000000', $transparency = 1 ) {
  266. if( strlen( $color ) == 4 ) {
  267. $color = str_replace( "#", "", $color );
  268. $r = hexdec( substr( $color,0,1 ).substr( $color,0,1 ) );
  269. $g = hexdec( substr( $color,1,1 ).substr( $color,1,1 ) );
  270. $b = hexdec( substr( $color,2,1 ).substr( $color,2,1 ) );
  271. $rgba = ( $transparency == 1 ? 'rgb' : 'rgba' ) . '(' . $r . ', ' . $g . ', ' . $b . ( $transparency == 1 ? '' : ', ' . $transparency ) . ')';
  272. } elseif ( strlen( $color ) == 7 ) {
  273. $color = str_replace( "#", "", $color );
  274. $r = hexdec( substr( $color,0,2 ) );
  275. $g = hexdec( substr( $color,2,2 ) );
  276. $b = hexdec( substr( $color,4,2 ) );
  277. $rgba = ( $transparency == 1 ? 'rgb' : 'rgba' ) . '(' . $r . ', ' . $g . ', ' . $b . ( $transparency == 1 ? '' : ', ' . $transparency ) . ')';
  278. } elseif ( strripos( $color, 'rgb' ) !== false ) {
  279. $color = str_replace( array( ' ', 'rgba', 'rgb', '(', ')', ), array( '', '', '', '', '', ), $color );
  280. $rgba_array = explode( ',', $color );
  281. $r = $rgba_array[ 0 ];
  282. $g = $rgba_array[ 1 ];
  283. $b = $rgba_array[ 2 ];
  284. $rgba = ( $transparency == 1 ? 'rgb' : 'rgba' ) . '(' . $r . ', ' . $g . ', ' . $b . ( $transparency == 1 ? '' : ', ' . $transparency ) . ')';
  285. } else {
  286. $rgba = $color;
  287. }
  288. return $rgba; // returns an array with the rgb values
  289. }
  290. /**
  291. * The function convert RGB(A) color type to HEX value.
  292. * @return string value.
  293. */
  294. public static function rgba2hex( $color = 'rgba(0,0,0,0)' ) {
  295. $out = "";
  296. $only_numbers = str_replace(
  297. array( 'rgb', 'a', '(', ')', ';', ' ' ),
  298. array( '', '', '', '', '', '' ),
  299. $color
  300. );
  301. $rgb_array = explode( ',', $only_numbers );
  302. if ( isset( $rgb_array[ 0 ] ) && isset( $rgb_array[ 1 ] ) && isset( $rgb_array[ 2 ] ) ) {
  303. $temp = base_convert( $rgb_array[ 0 ], 10, 16 );
  304. $out .= ( $rgb_array[ 0 ] < 16) ? ( "0" . $temp ) : $temp;
  305. $temp = base_convert( $rgb_array[ 1 ], 10, 16 );
  306. $out .= ( $rgb_array[ 1 ] < 16) ? ( "0" . $temp ) : $temp;
  307. $temp = base_convert( $rgb_array[ 2 ], 10, 16 );
  308. $out .= ( $rgb_array[ 2 ] < 16) ? ( "0" . $temp ) : $temp;
  309. // Package and away we go!
  310. return '#' . $out;
  311. } else {
  312. return $color;
  313. }
  314. }
  315. /**
  316. * The function do transpose of array.
  317. * @return string value.
  318. */
  319. function transpose_array( $array = array() ) {
  320. $transposed_array = array();
  321. if ( is_array( $array ) ) {
  322. // Find maximum number of cells among all rows. $max_columns
  323. $max_columns = 0;
  324. foreach ( $array as $row_key => $row ) {
  325. if ( is_object( $row ) ) {
  326. $row = get_object_vars( $row );
  327. }
  328. if ( is_array( $row ) ) {
  329. $columns_number_in_row = count( $row );
  330. if ( $max_columns < $columns_number_in_row ) {
  331. $max_columns = $columns_number_in_row;
  332. }
  333. }
  334. }
  335. // Add null value for each empty cell.
  336. foreach ( $array as $row_key => $row ) {
  337. if ( is_object( $row ) ) {
  338. $row = get_object_vars( $row );
  339. }
  340. if ( is_array( $row ) ) {
  341. $columns_number_in_row = count( $row );
  342. } else {
  343. $columns_number_in_row = 0;
  344. }
  345. if ( $max_columns > $columns_number_in_row ) {
  346. // $difference = $max_columns - $columns_number_in_row;
  347. for ( $i = $columns_number_in_row; $i <= $columns_number_in_row; $i++ ) {
  348. $row[ $i ] = null;
  349. }
  350. $array[ $row_key ] = $row;
  351. }
  352. }
  353. // Transpose.
  354. foreach ( $array as $row_key => $row ) {
  355. foreach ( $row as $column_key => $cell ) {
  356. if ( empty( $cell ) ) {
  357. $cell = null;
  358. }
  359. $transposed_array[ $column_key ][ $row_key ] = $cell;
  360. }
  361. }
  362. return $transposed_array;
  363. }
  364. }
  365. }
  366. }
  367. ?>