Нет описания

Format.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Utils_Format
  10. */
  11. class PUM_Utils_Format {
  12. /**
  13. * Removes <p></p> around URLs
  14. *
  15. * @param string $content
  16. *
  17. * @return mixed|string
  18. */
  19. public static function unwrap_urls( $content = '' ) {
  20. $content = preg_replace( "/<\\w+>((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\\+\\$,\\w]+@)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%@.\\w_]*)#?(?:[.\\!\\/\\\\w]*))?)<\\/\\w+>/", "$1\n\n", $content );
  21. $content = str_replace( "</p>", "</p>\n\n", $content );
  22. return $content;
  23. }
  24. /**
  25. * @param $time
  26. * @param string $format U|human
  27. *
  28. * @return false|int|mixed
  29. */
  30. public static function time( $time, $format = 'U' ) {
  31. if ( ! PUM_Utils_Time::is_timestamp( $time ) ) {
  32. $time = strtotime( $time );
  33. }
  34. switch ( $format ) {
  35. case 'human':
  36. case 'human-readable':
  37. return self::human_time( $time );
  38. break;
  39. default:
  40. case 'U':
  41. return $time;
  42. break;
  43. }
  44. }
  45. /**
  46. * @param int|float $number
  47. * @param string $format
  48. *
  49. * @return int|string
  50. */
  51. public static function number( $number, $format = '' ) {
  52. switch ( $format ) {
  53. default:
  54. case 'abbreviated':
  55. return self::abbreviated_number( $number );
  56. break;
  57. }
  58. }
  59. /**
  60. * Convert the timestamp to a nice time format
  61. *
  62. * @param int $time
  63. * @param int|null $current
  64. *
  65. * @return mixed
  66. */
  67. public static function human_time( $time, $current = null ) {
  68. if ( empty( $current ) ) {
  69. $current = time();
  70. }
  71. $diff = (int) abs( $current - $time );
  72. if ( $diff < 60 ) {
  73. $since = sprintf( __( '%ss', 'popup-maker' ), $diff );
  74. } else if ( $diff < HOUR_IN_SECONDS ) {
  75. $mins = round( $diff / MINUTE_IN_SECONDS );
  76. if ( $mins <= 1 ) {
  77. $mins = 1;
  78. }
  79. $since = sprintf( __( '%smin', 'popup-maker' ), $mins );
  80. } elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
  81. $hours = round( $diff / HOUR_IN_SECONDS );
  82. if ( $hours <= 1 ) {
  83. $hours = 1;
  84. }
  85. $since = sprintf( __( '%shr', 'popup-maker' ), $hours );
  86. } elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
  87. $days = round( $diff / DAY_IN_SECONDS );
  88. if ( $days <= 1 ) {
  89. $days = 1;
  90. }
  91. $since = sprintf( __( '%sd', 'popup-maker' ), $days );
  92. } elseif ( $diff < MONTH_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
  93. $weeks = round( $diff / WEEK_IN_SECONDS );
  94. if ( $weeks <= 1 ) {
  95. $weeks = 1;
  96. }
  97. $since = sprintf( __( '%sw', 'popup-maker' ), $weeks );
  98. } else {
  99. $since = '';
  100. }
  101. return apply_filters( 'pum_human_time_diff', $since, $diff, $time, $current );
  102. }
  103. /**
  104. * K, M number formatting
  105. *
  106. * @param int|float $n
  107. * @param string $point
  108. * @param string $sep
  109. *
  110. * @return int|string
  111. */
  112. public static function abbreviated_number( $n, $point = '.', $sep = ',' ) {
  113. if ( $n < 0 ) {
  114. return 0;
  115. }
  116. if ( $n < 10000 ) {
  117. return number_format( $n, 0, $point, $sep );
  118. }
  119. $d = $n < 1000000 ? 1000 : 1000000;
  120. $f = round( $n / $d, 1 );
  121. return number_format( $f, $f - intval( $f ) ? 1 : 0, $point, $sep ) . ( $d == 1000 ? 'K' : 'M' );
  122. }
  123. /**
  124. * Strips line breaks, tabs & carriage returns from html.
  125. *
  126. * Used to prevent WP from adding <br> and <p> tags.
  127. *
  128. * @param string $string
  129. *
  130. * @return mixed
  131. */
  132. public static function strip_white_space( $string = '' ) {
  133. return str_replace( array( "\t", "\r", "\n" ), '', $string );
  134. }
  135. }