PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/cm-pop-up-banners/shared/functions.php

https://gitlab.com/thisishayat/itv-2016
PHP | 626 lines | 521 code | 44 blank | 61 comment | 84 complexity | d5a09c01fdb3945c635e20fe266231e6 MD5 | raw file
  1. <?php
  2. if ( !function_exists( 'cminds_parse_php_info' ) ) {
  3. function cminds_parse_php_info() {
  4. $obstartresult = ob_start();
  5. if ( $obstartresult ) {
  6. $phpinforesult = phpinfo( INFO_MODULES );
  7. if ( $phpinforesult == FALSE ) {
  8. return array();
  9. }
  10. $s = ob_get_clean();
  11. } else {
  12. return array();
  13. }
  14. $s = strip_tags( $s, '<h2><th><td>' );
  15. $s = preg_replace( '/<th[^>]*>([^<]+)<\/th>/', "<info>\\1</info>", $s );
  16. $s = preg_replace( '/<td[^>]*>([^<]+)<\/td>/', "<info>\\1</info>", $s );
  17. $vTmp = preg_split( '/(<h2>[^<]+<\/h2>)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE );
  18. $vModules = array();
  19. for ( $i = 1; $i < count( $vTmp ); $i++ ) {
  20. if ( preg_match( '/<h2>([^<]+)<\/h2>/', $vTmp[ $i ], $vMat ) ) {
  21. $vName = trim( $vMat[ 1 ] );
  22. $vTmp2 = explode( "\n", $vTmp[ $i + 1 ] );
  23. foreach ( $vTmp2 AS $vOne ) {
  24. $vPat = '<info>([^<]+)<\/info>';
  25. $vPat3 = "/$vPat\s*$vPat\s*$vPat/";
  26. $vPat2 = "/$vPat\s*$vPat/";
  27. if ( preg_match( $vPat3, $vOne, $vMat ) ) { // 3cols
  28. $vModules[ $vName ][ trim( $vMat[ 1 ] ) ] = array( trim( $vMat[ 2 ] ), trim( $vMat[ 3 ] ) );
  29. } elseif ( preg_match( $vPat2, $vOne, $vMat ) ) { // 2cols
  30. $vModules[ $vName ][ trim( $vMat[ 1 ] ) ] = trim( $vMat[ 2 ] );
  31. }
  32. }
  33. }
  34. }
  35. return $vModules;
  36. }
  37. }
  38. if ( !function_exists( 'cminds_file_exists_remote' ) ) {
  39. /**
  40. * Checks whether remote file exists
  41. * @param type $url
  42. * @return boolean
  43. */
  44. function cminds_file_exists_remote( $url ) {
  45. if ( !function_exists( 'curl_version' ) ) {
  46. return false;
  47. }
  48. $curl = curl_init( $url );
  49. curl_setopt( $curl, CURLOPT_NOBODY, true );
  50. /*
  51. * Don't wait more than 5s for a file
  52. */
  53. curl_setopt( $curl, CURLOPT_TIMEOUT, 5 );
  54. //Check connection only
  55. $result = curl_exec( $curl );
  56. //Actual request
  57. $ret = false;
  58. if ( $result !== false ) {
  59. $statusCode = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
  60. //Check HTTP status code
  61. if ( $statusCode == 200 ) {
  62. $ret = true;
  63. }
  64. }
  65. curl_close( $curl );
  66. return $ret;
  67. }
  68. }
  69. if ( !function_exists( 'cminds_sort_WP_posts_by_title_length' ) ) {
  70. function cminds_sort_WP_posts_by_title_length( $a, $b ) {
  71. $sortVal = 0;
  72. if ( property_exists( $a, 'post_title' ) && property_exists( $b, 'post_title' ) ) {
  73. $sortVal = strlen( $b->post_title ) - strlen( $a->post_title );
  74. }
  75. return $sortVal;
  76. }
  77. }
  78. if ( !function_exists( 'cminds_strip_only' ) ) {
  79. /**
  80. * Strips just one tag
  81. * @param type $str
  82. * @param type $tags
  83. * @param type $stripContent
  84. * @return type
  85. */
  86. function cminds_strip_only( $str, $tags, $stripContent = false ) {
  87. $content = '';
  88. if ( !is_array( $tags ) ) {
  89. $tags = (strpos( $str, '>' ) !== false ? explode( '>', str_replace( '<', '', $tags ) ) : array( $tags ));
  90. if ( end( $tags ) == '' ) {
  91. array_pop( $tags );
  92. }
  93. }
  94. foreach ( $tags as $tag ) {
  95. if ( $stripContent ) {
  96. $content = '(.+</' . $tag . '[^>]*>|)';
  97. }
  98. $str = preg_replace( '#</?' . $tag . '[^>]*>' . $content . '#is', '', $str );
  99. }
  100. return $str;
  101. }
  102. }
  103. if ( !function_exists( 'cminds_truncate' ) ) {
  104. /**
  105. * From: http://stackoverflow.com/a/2398759/2107024
  106. * @param type $text
  107. * @param type $length
  108. * @param type $ending
  109. * @param type $exact
  110. * @param type $considerHtml
  111. * @return string
  112. */
  113. function cminds_truncate( $text, $length = 100, $ending = '...', $exact = false, $considerHtml = true ) {
  114. if ( is_array( $ending ) ) {
  115. extract( $ending );
  116. }
  117. if ( $considerHtml ) {
  118. if ( mb_strlen( preg_replace( '/<.*?>/', '', $text ) ) <= $length ) {
  119. return $text;
  120. }
  121. $totalLength = mb_strlen( $ending );
  122. $openTags = array();
  123. $truncate = '';
  124. $tags = array(); //inistialize empty array
  125. preg_match_all( '/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER );
  126. foreach ( $tags as $tag ) {
  127. if ( !preg_match( '/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[ 2 ] ) ) {
  128. $closeTag = array();
  129. if ( preg_match( '/<[\w]+[^>]*>/s', $tag[ 0 ] ) ) {
  130. array_unshift( $openTags, $tag[ 2 ] );
  131. } else if ( preg_match( '/<\/([\w]+)[^>]*>/s', $tag[ 0 ], $closeTag ) ) {
  132. $pos = array_search( $closeTag[ 1 ], $openTags );
  133. if ( $pos !== false ) {
  134. array_splice( $openTags, $pos, 1 );
  135. }
  136. }
  137. }
  138. $truncate .= $tag[ 1 ];
  139. $contentLength = mb_strlen( preg_replace( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[ 3 ] ) );
  140. if ( $contentLength + $totalLength > $length ) {
  141. $left = $length - $totalLength;
  142. $entitiesLength = 0;
  143. $entities = array();
  144. if ( preg_match_all( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[ 3 ], $entities, PREG_OFFSET_CAPTURE ) ) {
  145. foreach ( $entities[ 0 ] as $entity ) {
  146. if ( $entity[ 1 ] + 1 - $entitiesLength <= $left ) {
  147. $left--;
  148. $entitiesLength += mb_strlen( $entity[ 0 ] );
  149. } else {
  150. break;
  151. }
  152. }
  153. }
  154. $truncate .= mb_substr( $tag[ 3 ], 0, $left + $entitiesLength );
  155. break;
  156. } else {
  157. $truncate .= $tag[ 3 ];
  158. $totalLength += $contentLength;
  159. }
  160. if ( $totalLength >= $length ) {
  161. break;
  162. }
  163. }
  164. } else {
  165. if ( mb_strlen( $text ) <= $length ) {
  166. return $text;
  167. } else {
  168. $truncate = mb_substr( $text, 0, $length - strlen( $ending ) );
  169. }
  170. }
  171. if ( !$exact ) {
  172. $spacepos = mb_strrpos( $truncate, ' ' );
  173. if ( isset( $spacepos ) ) {
  174. if ( $considerHtml ) {
  175. $bits = mb_substr( $truncate, $spacepos );
  176. $droppedTags = array();
  177. preg_match_all( '/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER );
  178. if ( !empty( $droppedTags ) ) {
  179. foreach ( $droppedTags as $closingTag ) {
  180. if ( !in_array( $closingTag[ 1 ], $openTags ) ) {
  181. array_unshift( $openTags, $closingTag[ 1 ] );
  182. }
  183. }
  184. }
  185. }
  186. $truncate = mb_substr( $truncate, 0, $spacepos );
  187. }
  188. }
  189. $truncate .= $ending;
  190. if ( $considerHtml ) {
  191. foreach ( $openTags as $tag ) {
  192. $truncate .= '</' . $tag . '>';
  193. }
  194. }
  195. return $truncate;
  196. }
  197. }
  198. if ( !function_exists( 'cminds_show_message' ) ) {
  199. /**
  200. * Generic function to show a message to the user using WP's
  201. * standard CSS classes to make use of the already-defined
  202. * message colour scheme.
  203. *
  204. * @param $message The message you want to tell the user.
  205. * @param $errormsg If true, the message is an error, so use
  206. * the red message style. If false, the message is a status
  207. * message, so use the yellow information message style.
  208. */
  209. function cminds_show_message( $message, $errormsg = false ) {
  210. if ( $errormsg ) {
  211. echo '<div id="message" class="error">';
  212. } else {
  213. echo '<div id="message" class="updated fade">';
  214. }
  215. echo "<p><strong>$message</strong></p></div>";
  216. }
  217. }
  218. if ( !function_exists( 'cminds_units2bytes' ) ) {
  219. /**
  220. * Converts the Apache memory values to number of bytes ini_get('upload_max_filesize') or ini_get('post_max_size')
  221. * @param type $str
  222. * @return type
  223. */
  224. function cminds_units2bytes( $str ) {
  225. $units = array( 'B', 'K', 'M', 'G', 'T' );
  226. $unit = preg_replace( '/[0-9]/', '', $str );
  227. $unitFactor = array_search( strtoupper( $unit ), $units );
  228. if ( $unitFactor !== false ) {
  229. return preg_replace( '/[a-z]/i', '', $str ) * pow( 2, 10 * $unitFactor );
  230. }
  231. }
  232. }
  233. if ( !function_exists( 'cminds_paginate_links' ) ) {
  234. function cminds_paginate_links( $args = '' ) {
  235. $defaults = array(
  236. 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
  237. 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number
  238. 'total' => 1,
  239. 'current' => 0,
  240. 'show_all' => false,
  241. 'prev_next' => true,
  242. 'prev_text' => __( '&laquo; Previous' ),
  243. 'next_text' => __( 'Next &raquo;' ),
  244. 'end_size' => 1,
  245. 'mid_size' => 2,
  246. 'type' => 'plain',
  247. 'add_args' => false, // array of query args to add
  248. 'add_fragment' => '',
  249. 'before_page_number' => '',
  250. 'after_page_number' => '',
  251. 'link_class' => ''
  252. );
  253. $args = wp_parse_args( $args, $defaults );
  254. extract( $args, EXTR_SKIP );
  255. // Who knows what else people pass in $args
  256. $total = (int) $total;
  257. if ( $total < 2 )
  258. return;
  259. $current = (int) $current;
  260. $end_size = 0 < (int) $end_size ? (int) $end_size : 1; // Out of bounds? Make it the default.
  261. $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2;
  262. $add_args = is_array( $add_args ) ? $add_args : false;
  263. $r = '';
  264. $page_links = array();
  265. $n = 0;
  266. $dots = false;
  267. if ( $prev_next && $current && 1 < $current ) :
  268. $link = str_replace( '%_%', 2 == $current ? '' : $format, $base );
  269. $link = str_replace( '%#%', $current - 1, $link );
  270. if ( $add_args )
  271. $link = esc_url( add_query_arg( $add_args, $link ) );
  272. $link .= $add_fragment;
  273. /**
  274. * Filter the paginated links for the given archive pages.
  275. *
  276. * @since 3.0.0
  277. *
  278. * @param string $link The paginated link URL.
  279. */
  280. $page_links[] = '<a class="prev page-numbers ' . $link_class . '" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $prev_text . '</a>';
  281. endif;
  282. for ( $n = 1; $n <= $total; $n++ ) :
  283. if ( $n == $current ) :
  284. $page_links[] = '<span class="page-numbers current ' . $link_class . '">' . $before_page_number . number_format_i18n( $n ) . $after_page_number . "</span>";
  285. $dots = true;
  286. else :
  287. if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
  288. $link = str_replace( '%_%', 1 == $n ? '' : $format, $base );
  289. $link = str_replace( '%#%', $n, $link );
  290. if ( $add_args )
  291. $link = esc_url( add_query_arg( $add_args, $link ) );
  292. $link .= $add_fragment;
  293. /** This filter is documented in wp-includes/general-template.php */
  294. $page_links[] = '<a class="page-numbers ' . $link_class . '" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $before_page_number . number_format_i18n( $n ) . $after_page_number . '</a>';
  295. $dots = true;
  296. elseif ( $dots && !$show_all ) :
  297. $page_links[] = '<span class="page-numbers dots">' . __( '&hellip;' ) . '</span>';
  298. $dots = false;
  299. endif;
  300. endif;
  301. endfor;
  302. if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) :
  303. $link = str_replace( '%_%', $format, $base );
  304. $link = str_replace( '%#%', $current + 1, $link );
  305. if ( $add_args )
  306. $link = esc_url( add_query_arg( $add_args, $link ) );
  307. $link .= $add_fragment;
  308. /** This filter is documented in wp-includes/general-template.php */
  309. $page_links[] = '<a class="next page-numbers ' . $link_class . '" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $next_text . '</a>';
  310. endif;
  311. switch ( $type ) :
  312. case 'array' :
  313. return $page_links;
  314. break;
  315. case 'list' :
  316. $r .= "<ul class='page-numbers'>\n\t<li>";
  317. $r .= join( "</li>\n\t<li>", $page_links );
  318. $r .= "</li>\n</ul>\n";
  319. break;
  320. default :
  321. $r = join( "\n", $page_links );
  322. break;
  323. endswitch;
  324. return $r;
  325. }
  326. }
  327. if ( !function_exists( 'cmpopfly_cminds_dropdown' ) ) {
  328. /**
  329. * Retrieve or display list of pages as a dropdown (select list).
  330. *
  331. * @since 2.1.0
  332. *
  333. * @param array|string $args Optional. Override default arguments.
  334. * @return string HTML content, if not displaying.
  335. */
  336. function cmpopfly_cminds_dropdown( $args = '' ) {
  337. $defaults = array(
  338. 'depth' => 0,
  339. 'child_of' => 0,
  340. 'selected' => 0,
  341. 'echo' => 1,
  342. 'name' => 'page_id',
  343. 'id' => '',
  344. 'show_option_none' => '',
  345. 'show_option_no_change' => '',
  346. 'option_none_value' => '',
  347. 'add_posts' => 1
  348. );
  349. $r = wp_parse_args( $args, $defaults );
  350. $pages = get_pages( $r );
  351. $output = '';
  352. // Back-compat with old system where both id and name were based on $name argument
  353. if ( empty( $r[ 'id' ] ) ) {
  354. $r[ 'id' ] = $r[ 'name' ];
  355. }
  356. if ( !empty( $pages ) ) {
  357. $output = "<select name='" . esc_attr( $r[ 'name' ] ) . "' id='" . esc_attr( $r[ 'id' ] ) . "'>\n";
  358. if ( $r[ 'show_option_no_change' ] ) {
  359. $selected = ( '-1' == $r[ 'selected' ] ) ? ' selected="selected"' : '';
  360. $output .= "\t<option value=\"-1\" {$selected} >" . $r[ 'show_option_no_change' ] . "</option>\n";
  361. }
  362. if ( $r[ 'show_option_none' ] ) {
  363. $selected = ( esc_attr( $r[ 'option_none_value' ] ) == $r[ 'selected' ] ) ? ' selected="selected"' : '';
  364. $output .= "\t<option value=\"" . esc_attr( $r[ 'option_none_value' ] ) . '" ' . $selected . ' >' . $r[ 'show_option_none' ] . "</option>\n";
  365. }
  366. $output .= '<optgroup label="Pages">';
  367. $output .= walk_page_dropdown_tree( $pages, $r[ 'depth' ], $r );
  368. $output .= '</optgroup>';
  369. if ( $r[ 'add_posts' ] ) {
  370. $postsArgs = array(
  371. 'posts_per_page' => 10000,
  372. 'post_type' => 'post',
  373. 'fields' => 'ids',
  374. );
  375. $query = new WP_Query( $postsArgs );
  376. $posts = $query->get_posts();
  377. if ( !empty( $posts ) ) {
  378. $output .= '<optgroup label="Posts">';
  379. $output .= cmpopfly_walk_post_dropdown_tree( $posts, $r[ 'depth' ], $r );
  380. $output .= '</optgroup>';
  381. }
  382. }
  383. if ( $r[ 'custom_post_types' ] ) {
  384. $post_types = array();
  385. // by removing those two if conditions this part of the code can handle above pages and post functionality
  386. if ( ($key = array_search( 'post', $post_types )) !== false ) {
  387. unset( $post_types[ $key ] );
  388. }
  389. if ( ($key = array_search( 'page', $post_types )) !== false ) {
  390. unset( $post_types[ $key ] );
  391. }
  392. foreach ( $post_types as $post_type ) {
  393. $postsArgs = array(
  394. 'posts_per_page' => 10000,
  395. 'post_type' => $post_type,
  396. 'fields' => 'ids',
  397. );
  398. $query = new WP_Query( $postsArgs );
  399. $posts = $query->get_posts();
  400. if ( !empty( $posts ) ) {
  401. $output .= '<optgroup label="' . $post_type . '">';
  402. $output .= cmpopfly_walk_post_dropdown_tree( $posts, $r[ 'depth' ], $r );
  403. $output .= '</optgroup>';
  404. }
  405. }
  406. }
  407. $output .= "</select>\n";
  408. }
  409. /**
  410. * Filter the HTML output of a list of pages as a drop down.
  411. *
  412. * @since 2.1.0
  413. *
  414. * @param string $output HTML output for drop down list of pages.
  415. */
  416. $html = apply_filters( 'wp_dropdown_pages', $output );
  417. if ( $r[ 'echo' ] ) {
  418. echo $html;
  419. }
  420. return $html;
  421. }
  422. }
  423. if ( !class_exists( 'Cmpopfly_Walker_PostDropdown' ) ) {
  424. class Cmpopfly_Walker_PostDropdown extends Walker {
  425. /**
  426. * @see Walker::$tree_type
  427. * @since 2.1.0
  428. * @var string
  429. */
  430. var $tree_type = 'post';
  431. /**
  432. * @see Walker::$db_fields
  433. * @since 2.1.0
  434. * @todo Decouple this
  435. * @var array
  436. */
  437. var $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
  438. /**
  439. * @see Walker::start_el()
  440. * @since 2.1.0
  441. *
  442. * @param string $output Passed by reference. Used to append additional content.
  443. * @param object $page Page data object.
  444. * @param int $depth Depth of page in reference to parent pages. Used for padding.
  445. * @param array $args Uses 'selected' argument for selected page to set selected HTML attribute for option element.
  446. */
  447. function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {
  448. $pad = str_repeat( '&nbsp;', $depth * 3 );
  449. if ( !is_a( $object, 'WP_Post' ) ) {
  450. $object = get_post( $object );
  451. }
  452. $value = ((isset( $args[ 'jump_to' ] ) && $args[ 'jump_to' ]) ? get_permalink( $object->ID ) : $object->ID);
  453. $output .= "\t<option class=\"level-$depth\" value=\"$value\"";
  454. if ( $object->ID == $args[ 'selected' ] )
  455. $output .= ' selected="selected"';
  456. $output .= '>';
  457. $title = esc_html( $object->post_title );
  458. $output .= "$pad$title";
  459. $output .= "</option>\n";
  460. }
  461. /**
  462. * Display array of elements hierarchically.
  463. *
  464. * Does not assume any existing order of elements.
  465. *
  466. * $max_depth = -1 means flatly display every element.
  467. * $max_depth = 0 means display all levels.
  468. * $max_depth > 0 specifies the number of display levels.
  469. *
  470. * @since 2.1.0
  471. *
  472. * @param array $elements An array of elements.
  473. * @param int $max_depth The maximum hierarchical depth.
  474. * @return string The hierarchical item output.
  475. */
  476. public function walk( $elements, $max_depth ) {
  477. $args = array_slice( func_get_args(), 2 );
  478. $output = '';
  479. //invalid parameter or nothing to walk
  480. if ( $max_depth < -1 || empty( $elements ) ) {
  481. return $output;
  482. }
  483. $parent_field = $this->db_fields[ 'parent' ];
  484. // flat display
  485. if ( -1 == $max_depth ) {
  486. $empty_array = array();
  487. foreach ( $elements as $e )
  488. $this->display_element( $e, $empty_array, 1, 0, $args, $output );
  489. return $output;
  490. }
  491. /*
  492. * Need to display in hierarchical order.
  493. * Separate elements into two buckets: top level and children elements.
  494. * Children_elements is two dimensional array, eg.
  495. * Children_elements[10][] contains all sub-elements whose parent is 10.
  496. */
  497. $top_level_elements = array();
  498. $children_elements = array();
  499. foreach ( $elements as $e ) {
  500. if ( !is_a( $e, 'WP_Post' ) ) {
  501. $e = get_post( $e );
  502. }
  503. if ( 0 == $e->$parent_field )
  504. $top_level_elements[] = $e;
  505. else
  506. $children_elements[ $e->$parent_field ][] = $e;
  507. }
  508. /*
  509. * When none of the elements is top level.
  510. * Assume the first one must be root of the sub elements.
  511. */
  512. if ( empty( $top_level_elements ) ) {
  513. $first = array_slice( $elements, 0, 1 );
  514. $root = $first[ 0 ];
  515. $top_level_elements = array();
  516. $children_elements = array();
  517. foreach ( $elements as $e ) {
  518. if ( !is_a( $e, 'WP_Post' ) ) {
  519. $e = get_post( $e );
  520. }
  521. if ( $root->$parent_field == $e->$parent_field )
  522. $top_level_elements[] = $e;
  523. else
  524. $children_elements[ $e->$parent_field ][] = $e;
  525. }
  526. }
  527. foreach ( $top_level_elements as $e )
  528. $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
  529. /*
  530. * If we are displaying all levels, and remaining children_elements is not empty,
  531. * then we got orphans, which should be displayed regardless.
  532. */
  533. if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
  534. $empty_array = array();
  535. foreach ( $children_elements as $orphans )
  536. foreach ( $orphans as $op )
  537. $this->display_element( $op, $empty_array, 1, 0, $args, $output );
  538. }
  539. return $output;
  540. }
  541. }
  542. }
  543. if ( !function_exists( 'cmpopfly_walk_post_dropdown_tree' ) ) {
  544. function cmpopfly_walk_post_dropdown_tree() {
  545. $args = func_get_args();
  546. if ( empty( $args[ 2 ][ 'walker' ] ) ) { // the user's options are the third parameter
  547. $walker = new Cmpopfly_Walker_PostDropdown;
  548. } else {
  549. $walker = $args[ 2 ][ 'walker' ];
  550. }
  551. return call_user_func_array( array( &$walker, 'walk' ), $args );
  552. }
  553. }