class-wordpress-popular-posts-activator.php
8 years ago
class-wordpress-popular-posts-deactivator.php
8 years ago
class-wordpress-popular-posts-helper.php
8 years ago
class-wordpress-popular-posts-i18n.php
8 years ago
class-wordpress-popular-posts-image.php
8 years ago
class-wordpress-popular-posts-loader.php
8 years ago
class-wordpress-popular-posts-output.php
8 years ago
class-wordpress-popular-posts-query.php
8 years ago
class-wordpress-popular-posts-rest-controller.php
8 years ago
class-wordpress-popular-posts-settings.php
8 years ago
class-wordpress-popular-posts-template.php
8 years ago
class-wordpress-popular-posts-translate.php
8 years ago
class-wordpress-popular-posts-widget.php
8 years ago
class-wordpress-popular-posts.php
8 years ago
widget-form.php
8 years ago
class-wordpress-popular-posts-helper.php
215 lines
| 1 | <?php |
| 2 | |
| 3 | class WPP_Helper { |
| 4 | |
| 5 | /** |
| 6 | * Checks for valid number. |
| 7 | * |
| 8 | * @since 2.1.6 |
| 9 | * @param int number |
| 10 | * @return bool |
| 11 | */ |
| 12 | public static function is_number( $number ){ |
| 13 | return !empty($number) && is_numeric($number) && (intval($number) == floatval($number)); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Checks for valid date. |
| 18 | * |
| 19 | * @since 4.0.0 |
| 20 | * @param string $date |
| 21 | * @param string $format |
| 22 | * @return bool |
| 23 | */ |
| 24 | public static function is_valid_date( $date = null, $format = 'Y-m-d' ){ |
| 25 | $d = DateTime::createFromFormat( $format, $date ); |
| 26 | return $d && $d->format($format) === $date; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Returns an array of dates between two dates. |
| 31 | * |
| 32 | * @since 4.0.0 |
| 33 | * @param string $start_date |
| 34 | * @param string $end_date |
| 35 | * @param string $format |
| 36 | * @return array|bool |
| 37 | */ |
| 38 | public static function get_date_range( $start_date = null, $end_date = null, $format = 'Y-m-d' ) { |
| 39 | |
| 40 | if ( |
| 41 | self::is_valid_date( $start_date ) |
| 42 | && self::is_valid_date( $end_date ) |
| 43 | ) { |
| 44 | |
| 45 | $dates = array(); |
| 46 | |
| 47 | $begin = new DateTime( $start_date ); |
| 48 | $end = new DateTime( $end_date ); |
| 49 | |
| 50 | if ( $begin < $end ) { |
| 51 | while( $begin <= $end ) { |
| 52 | $dates[] = $begin->format( $format ); |
| 53 | $begin->modify('+1 day'); |
| 54 | } |
| 55 | } |
| 56 | else { |
| 57 | while( $begin >= $end ) { |
| 58 | $dates[] = $begin->format( $format ); |
| 59 | $begin->modify('-1 day'); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return $dates; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | return false; |
| 68 | |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns server date. |
| 73 | * |
| 74 | * @since 2.1.6 |
| 75 | * @access private |
| 76 | * @return string |
| 77 | */ |
| 78 | public static function curdate() { |
| 79 | return gmdate( 'Y-m-d', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns mysql datetime. |
| 84 | * |
| 85 | * @since 2.1.6 |
| 86 | * @access private |
| 87 | * @return string |
| 88 | */ |
| 89 | public static function now() { |
| 90 | return current_time( 'mysql' ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns time. |
| 95 | * |
| 96 | * @since 2.3.0 |
| 97 | * @return string |
| 98 | */ |
| 99 | public static function microtime_float() { |
| 100 | |
| 101 | list( $msec, $sec ) = explode( ' ', microtime() ); |
| 102 | |
| 103 | return (float) $msec + (float) $sec; |
| 104 | |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Merges two associative arrays recursively. |
| 109 | * |
| 110 | * @since 2.3.4 |
| 111 | * @link http://www.php.net/manual/en/function.array-merge-recursive.php#92195 |
| 112 | * @param array array1 |
| 113 | * @param array array2 |
| 114 | * @return array |
| 115 | */ |
| 116 | public static function merge_array_r( array $array1, array $array2 ) { |
| 117 | |
| 118 | $merged = $array1; |
| 119 | |
| 120 | foreach ( $array2 as $key => &$value ) { |
| 121 | |
| 122 | if ( is_array( $value ) && isset ( $merged[$key] ) && is_array( $merged[$key] ) ) { |
| 123 | $merged[$key] = self::merge_array_r( $merged[$key], $value ); |
| 124 | } else { |
| 125 | $merged[$key] = $value; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return $merged; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Debug function. |
| 135 | * |
| 136 | * @since 3.0.0 |
| 137 | * @param mixed $v variable to display with var_dump() |
| 138 | * @param mixed $v,... unlimited optional number of variables to display with var_dump() |
| 139 | */ |
| 140 | public static function debug( $v ) { |
| 141 | |
| 142 | if ( !defined('WPP_DEBUG') || !WPP_DEBUG ) |
| 143 | return; |
| 144 | |
| 145 | foreach ( func_get_args() as $arg ) { |
| 146 | |
| 147 | print "<pre>"; |
| 148 | var_dump($arg); |
| 149 | print "</pre>"; |
| 150 | |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Truncates text. |
| 157 | * |
| 158 | * @since 4.0.0 |
| 159 | * @param string $text |
| 160 | * @param int $length |
| 161 | * @param bool $truncate_by_words |
| 162 | * @return string |
| 163 | */ |
| 164 | public static function truncate( $text = '', $length = 25, $truncate_by_words = false ) { |
| 165 | |
| 166 | if ( '' !== $text ) { |
| 167 | |
| 168 | // Truncate by words |
| 169 | if ( $truncate_by_words ) { |
| 170 | |
| 171 | $words = explode( " ", $text, $length + 1 ); |
| 172 | |
| 173 | if ( count($words) > $length ) { |
| 174 | array_pop( $words ); |
| 175 | $text = rtrim( implode(" ", $words), ",." ) . " ..."; |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | // Truncate by characters |
| 180 | elseif ( strlen($text) > $length ) { |
| 181 | $text = rtrim( mb_substr($text, 0, $length , get_bloginfo('charset')), " ,." ) . "..."; |
| 182 | } |
| 183 | |
| 184 | } |
| 185 | |
| 186 | return $text; |
| 187 | |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Gets post/page ID if current page is singular |
| 192 | * |
| 193 | * @since 3.1.2 |
| 194 | */ |
| 195 | public static function is_single() { |
| 196 | |
| 197 | $trackable = array(); |
| 198 | $registered_post_types = get_post_types( array('public' => true), 'names' ); |
| 199 | |
| 200 | foreach ( $registered_post_types as $post_type ) { |
| 201 | $trackable[] = $post_type; |
| 202 | } |
| 203 | |
| 204 | $trackable = apply_filters( 'wpp_trackable_post_types', $trackable ); |
| 205 | |
| 206 | if ( is_singular( $trackable ) && !is_front_page() && !is_preview() && !is_trackback() && !is_feed() && !is_robots() && !is_customize_preview() ) { |
| 207 | return get_queried_object_id(); |
| 208 | } |
| 209 | |
| 210 | return false; |
| 211 | |
| 212 | } |
| 213 | |
| 214 | } // End WPP_Helper class |
| 215 |