PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.0.2
WP Popular Posts v5.0.2
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / src / Helper.php
wordpress-popular-posts / src Last commit date
Activation 6 years ago Admin 6 years ago Container 6 years ago Front 6 years ago Moment 6 years ago Rest 6 years ago Widget 6 years ago Bootstrap.php 6 years ago Cache.php 6 years ago Helper.php 6 years ago I18N.php 6 years ago Image.php 6 years ago Output.php 6 years ago Query.php 6 years ago Settings.php 6 years ago Themer.php 6 years ago Translate.php 6 years ago WordPressPopularPosts.php 6 years ago deprecated.php 6 years ago template-tags.php 6 years ago
Helper.php
273 lines
1 <?php
2
3 namespace WordPressPopularPosts;
4
5 class Helper {
6
7 /**
8 * Checks for valid number.
9 *
10 * @since 2.1.6
11 * @param int number
12 * @return bool
13 */
14 public static function is_number($number)
15 {
16 return !empty($number) && is_numeric($number) && (intval($number) == floatval($number));
17 }
18
19 /**
20 * Checks for valid date.
21 *
22 * @since 4.0.0
23 * @param string $date
24 * @param string $format
25 * @return bool
26 */
27 public static function is_valid_date($date = null, $format = 'Y-m-d')
28 {
29 $d = \DateTime::createFromFormat($format, $date);
30 return $d && $d->format($format) === $date;
31 }
32
33 /**
34 * Returns an array of dates between two dates.
35 *
36 * @since 4.0.0
37 * @param string $start_date
38 * @param string $end_date
39 * @param string $format
40 * @return array|bool
41 */
42 public static function get_date_range($start_date = null, $end_date = null, $format = 'Y-m-d')
43 {
44 if (
45 self::is_valid_date($start_date, $format)
46 && self::is_valid_date($end_date, $format)
47 ) {
48 $dates = [];
49
50 $begin = new \DateTime($start_date, new \DateTimeZone(Helper::get_timezone()));
51 $end = new \DateTime($end_date, new \DateTimeZone(Helper::get_timezone()));
52
53 if ( $begin < $end ) {
54 while( $begin <= $end ) {
55 $dates[] = $begin->format($format);
56 $begin->modify('+1 day');
57 }
58 }
59 else {
60 while( $begin >= $end ) {
61 $dates[] = $begin->format($format);
62 $begin->modify('-1 day');
63 }
64 }
65
66 return $dates;
67 }
68
69 return false;
70 }
71
72 /**
73 * Returns server date.
74 *
75 * @since 2.1.6
76 * @access private
77 * @return string
78 */
79 public static function curdate()
80 {
81 return current_time('Y-m-d', false);
82 }
83
84 /**
85 * Returns mysql datetime.
86 *
87 * @since 2.1.6
88 * @access private
89 * @return string
90 */
91 public static function now()
92 {
93 return current_time('mysql');
94 }
95
96 /**
97 * Returns current timestamp.
98 *
99 * @since 5.0.2
100 * @return string
101 */
102 public static function timestamp()
103 {
104 // current_datetime() is WP 5.3+
105 return ( function_exists('current_datetime') ) ? current_datetime()->getTimestamp() : current_time('timestamp');
106 }
107
108 /**
109 * Returns site's timezone.
110 *
111 * Code borrowed from Rarst's awesome WpDateTime class: https://github.com/Rarst/wpdatetime
112 *
113 * @since 5.0.0
114 * @return string
115 */
116 public static function get_timezone()
117 {
118 $timezone_string = get_option('timezone_string');
119
120 if ( ! empty($timezone_string) ) {
121 return $timezone_string;
122 }
123
124 $offset = get_option('gmt_offset');
125 $sign = $offset < 0 ? '-' : '+';
126 $hours = (int) $offset;
127 $minutes = abs(($offset - (int) $offset) * 60);
128 $offset = sprintf('%s%02d:%02d', $sign, abs($hours), $minutes);
129
130 return $offset;
131 }
132
133 /**
134 * Returns time.
135 *
136 * @since 2.3.0
137 * @return string
138 */
139 public static function microtime_float()
140 {
141 list($msec, $sec) = explode(' ', microtime());
142 return (float) $msec + (float) $sec;
143 }
144
145 /**
146 * Merges two associative arrays recursively.
147 *
148 * @since 2.3.4
149 * @link http://www.php.net/manual/en/function.array-merge-recursive.php#92195
150 * @param array array1
151 * @param array array2
152 * @return array
153 */
154 public static function merge_array_r(array $array1, array $array2)
155 {
156 $merged = $array1;
157
158 foreach( $array2 as $key => &$value ) {
159 if ( is_array($value) && isset($merged[$key]) && is_array($merged[$key]) ) {
160 $merged[$key] = self::merge_array_r($merged[$key], $value);
161 } else {
162 $merged[$key] = $value;
163 }
164 }
165
166 return $merged;
167 }
168
169 /**
170 * Debug function.
171 *
172 * @since 3.0.0
173 * @param mixed $v variable to display with var_dump()
174 * @param mixed $v,... unlimited optional number of variables to display with var_dump()
175 */
176 public static function debug($v)
177 {
178 if ( !defined('WPP_DEBUG') || !WPP_DEBUG )
179 return;
180
181 foreach( func_get_args() as $arg ) {
182 print "<pre>";
183 var_dump($arg);
184 print "</pre>";
185 }
186 }
187
188 /**
189 * Truncates text.
190 *
191 * @since 4.0.0
192 * @param string $text
193 * @param int $length
194 * @param bool $truncate_by_words
195 * @return string
196 */
197 public static function truncate($text = '', $length = 25, $truncate_by_words = false, $more = '...')
198 {
199 if ( '' !== $text ) {
200 // Truncate by words
201 if ( $truncate_by_words ) {
202 $words = explode(" ", $text, $length + 1);
203
204 if ( count($words) > $length ) {
205 array_pop($words);
206 $text = rtrim(implode(" ", $words), ",.") . " {$more}";
207 }
208 }
209 // Truncate by characters
210 elseif ( strlen($text) > $length ) {
211 $text = rtrim(mb_substr($text, 0, $length , get_bloginfo('charset')), " ,.") . $more;
212 }
213 }
214
215 return $text;
216 }
217
218 /**
219 * Gets post/page ID if current page is singular
220 *
221 * @since 3.1.2
222 */
223 public static function is_single()
224 {
225 $trackable = [];
226 $registered_post_types = get_post_types(['public' => true], 'names');
227
228 foreach( $registered_post_types as $post_type ) {
229 $trackable[] = $post_type;
230 }
231
232 $trackable = apply_filters('wpp_trackable_post_types', $trackable);
233
234 if (
235 is_singular($trackable)
236 && !is_front_page()
237 && !is_preview()
238 && !is_trackback()
239 && !is_feed()
240 && !is_robots()
241 && !is_customize_preview()
242 ) {
243 return get_queried_object_id();
244 }
245
246 return false;
247 }
248
249 /**
250 * Adds scheme to a given URL.
251 *
252 * @since 5.0.0
253 * @param string $url
254 * @param string $scheme
255 * @return string|bool
256 */
257 static function add_scheme($url = null, $scheme = 'https://')
258 {
259 $url_args = parse_url($url);
260
261 if ( $url_args ) {
262 // No need to do anything, URL is fine
263 if ( isset($url_args['scheme']) )
264 return $url;
265 // Return URL with scheme
266 return $scheme . $url_args['host'] . $url_args['path'];
267 }
268
269 // Invalid/malformed URL
270 return false;
271 }
272 }
273