1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Classy; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Includes multiple helper function |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
class Helper { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* |
14
|
|
|
* @param string $text |
15
|
|
|
* @param int $num_words |
16
|
|
|
* @param string|null|false $more text to appear in "Read more...". Null to use default, false to hide |
17
|
|
|
* @param string $allowed_tags |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public static function trim_words( $text, $num_words = 55, $more = null, $allowed_tags = 'p a span b i br blockquote' ) { |
21
|
|
|
if ( null === $more ) { |
22
|
|
|
$more = __( '…' ); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$original_text = $text; |
26
|
|
|
$allowed_tag_string = ''; |
27
|
|
|
|
28
|
|
|
foreach ( explode( ' ', $allowed_tags ) as $tag ) { |
29
|
|
|
$allowed_tag_string .= '<' . $tag . '>'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$text = strip_tags( $text, $allowed_tag_string ); |
|
|
|
|
33
|
|
|
|
34
|
|
|
/* translators: If your word count is based on single characters (East Asian characters), |
35
|
|
|
enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */ |
36
|
|
|
|
37
|
|
|
if ( 'characters' === _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { |
38
|
|
|
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' ); |
|
|
|
|
39
|
|
|
preg_match_all( '/./u', $text, $words_array ); |
40
|
|
|
$words_array = array_slice( $words_array[0], 0, $num_words + 1 ); |
41
|
|
|
$sep = ''; |
42
|
|
|
} else { |
43
|
|
|
$words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY ); |
44
|
|
|
$sep = ' '; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ( count( $words_array ) > $num_words ) { |
48
|
|
|
array_pop( $words_array ); |
49
|
|
|
$text = implode( $sep, $words_array ); |
|
|
|
|
50
|
|
|
$text = $text . $more; |
|
|
|
|
51
|
|
|
} else { |
52
|
|
|
$text = implode( $sep, $words_array ); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$text = self::close_tags( $text ); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
* |
64
|
|
|
* @param string $html |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public static function close_tags( $html ) { |
68
|
|
|
//put all opened tags into an array |
69
|
|
|
preg_match_all( '#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result ); |
70
|
|
|
|
71
|
|
|
$openedtags = $result[1]; |
72
|
|
|
|
73
|
|
|
//put all closed tags into an array |
74
|
|
|
preg_match_all( '#</([a-z]+)>#iU', $html, $result ); |
75
|
|
|
|
76
|
|
|
$closedtags = $result[1]; |
77
|
|
|
$len_opened = count( $openedtags ); |
78
|
|
|
|
79
|
|
|
// all tags are closed |
80
|
|
|
if ( count( $closedtags ) === $len_opened ) { |
81
|
|
|
return $html; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$openedtags = array_reverse( $openedtags ); |
85
|
|
|
|
86
|
|
|
// close tags |
87
|
|
|
for ( $i = 0; $i < $len_opened; $i++ ) { |
88
|
|
|
if ( ! in_array( $openedtags[ $i ], $closedtags, true ) ) { |
89
|
|
|
$html .= '</' . $openedtags[ $i ] . '>'; |
90
|
|
|
} else { |
91
|
|
|
unset( $closedtags[ array_search( $openedtags[ $i ], $closedtags ) ] ); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$html = str_replace( array( '</br>', '</hr>', '</wbr>' ), '', $html ); |
|
|
|
|
96
|
|
|
$html = str_replace( array( '<br>', '<hr>', '<wbr>' ), array( '<br />', '<hr />', '<wbr />' ), $html ); |
|
|
|
|
97
|
|
|
|
98
|
|
|
return $html; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Displays variable if WP_DEBUG is up |
103
|
|
|
* |
104
|
|
|
* @param mixed $arg |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public static function error_log( $arg ) { |
109
|
|
|
if ( ! WP_DEBUG ) { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
if ( is_object( $arg ) || is_array( $arg ) ) { |
113
|
|
|
$arg = print_r( $arg, true ); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
return error_log( $arg ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* |
120
|
|
|
* |
121
|
|
|
* @param string $args |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public static function paginate_links( $args = '' ) { |
125
|
|
|
$defaults = array( |
126
|
|
|
'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below) |
127
|
|
|
'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number |
128
|
|
|
'total' => 1, |
129
|
|
|
'current' => 0, |
130
|
|
|
'show_all' => false, |
131
|
|
|
'prev_next' => true, |
132
|
|
|
'prev_text' => __( '« Previous' ), |
133
|
|
|
'next_text' => __( 'Next »' ), |
134
|
|
|
'end_size' => 1, |
135
|
|
|
'mid_size' => 2, |
136
|
|
|
'type' => 'array', |
137
|
|
|
'add_args' => false, // array of query args to add |
138
|
|
|
'add_fragment' => '', |
139
|
|
|
); |
140
|
|
|
$args = wp_parse_args( $args, $defaults ); |
|
|
|
|
141
|
|
|
// Who knows what else people pass in $args |
142
|
|
|
$args['total'] = intval( (int) $args['total'] ); |
143
|
|
|
if ( $args['total'] < 2 ) { |
144
|
|
|
return array(); |
145
|
|
|
} |
146
|
|
|
$args['current'] = (int) $args['current']; |
147
|
|
|
$args['end_size'] = 0 < (int) $args['end_size'] ? (int) $args['end_size'] : 1; // Out of bounds? Make it the default. |
148
|
|
|
$args['mid_size'] = 0 <= (int) $args['mid_size'] ? (int) $args['mid_size'] : 2; |
149
|
|
|
$args['add_args'] = is_array( $args['add_args'] ) ? $args['add_args'] : false; |
150
|
|
|
$page_links = array(); |
151
|
|
|
$dots = false; |
152
|
|
|
if ( $args['prev_next'] && $args['current'] && 1 < $args['current'] ) { |
153
|
|
|
$link = str_replace( '%_%', 2 === absint( $args['current'] ) ? '' : $args['format'], $args['base'] ); |
154
|
|
|
$link = str_replace( '%#%', $args['current'] - 1, $link ); |
155
|
|
|
if ( $args['add_args'] ) { |
156
|
|
|
$link = add_query_arg( $args['add_args'], $link ); |
157
|
|
|
} |
158
|
|
|
$link .= $args['add_fragment']; |
159
|
|
|
$link = untrailingslashit( $link ); |
160
|
|
|
$page_links[] = array( |
161
|
|
|
'class' => 'prev page-numbers', |
162
|
|
|
'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
163
|
|
|
'title' => $args['prev_text'], |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
for ( $n = 1; $n <= $args['total']; $n++ ) { |
167
|
|
|
$n_display = number_format_i18n( $n ); |
168
|
|
|
if ( absint( $args['current'] ) === $n ) { |
169
|
|
|
$page_links[] = array( |
170
|
|
|
'class' => 'page-number page-numbers current', |
171
|
|
|
'title' => $n_display, |
172
|
|
|
'text' => $n_display, |
173
|
|
|
'name' => $n_display, |
174
|
|
|
'current' => true, |
175
|
|
|
); |
176
|
|
|
$dots = true; |
177
|
|
|
} else { |
178
|
|
|
if ( $args['show_all'] || ( $n <= $args['end_size'] || ( $args['current'] && $n >= $args['current'] - $args['mid_size'] && $n <= $args['current'] + $args['mid_size'] ) || $n > $args['total'] - $args['end_size'] ) ) { |
179
|
|
|
$link = str_replace( '%_%', 1 === absint( $n ) ? '' : $args['format'], $args['base'] ); |
180
|
|
|
$link = str_replace( '%#%', $n, $link ); |
181
|
|
|
$link = trailingslashit( $link ) . ltrim( $args['add_fragment'], '/' ); |
182
|
|
|
if ( $args['add_args'] ) { |
183
|
|
|
$link = rtrim( add_query_arg( $args['add_args'], $link ), '/' ); |
184
|
|
|
} |
185
|
|
|
$link = str_replace( ' ', '+', $link ); |
186
|
|
|
$link = untrailingslashit( $link ); |
187
|
|
|
$page_links[] = array( |
188
|
|
|
'class' => 'page-number page-numbers', |
189
|
|
|
'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
190
|
|
|
'title' => $n_display, |
191
|
|
|
'name' => $n_display, |
192
|
|
|
'current' => absint( $args['current'] ) === $n, |
193
|
|
|
); |
194
|
|
|
$dots = true; |
195
|
|
|
} elseif ( $dots && ! $args['show_all'] ) { |
196
|
|
|
$page_links[] = array( |
197
|
|
|
'class' => 'dots', |
198
|
|
|
'title' => __( '…' ), |
199
|
|
|
); |
200
|
|
|
$dots = false; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
if ( $args['prev_next'] && $args['current'] && ( $args['current'] < $args['total'] || -1 === intval( $args['total'] ) ) ) { |
205
|
|
|
$link = str_replace( '%_%', $args['format'], $args['base'] ); |
206
|
|
|
$link = str_replace( '%#%', $args['current'] + 1, $link ); |
207
|
|
|
if ( $args['add_args'] ) { |
208
|
|
|
$link = add_query_arg( $args['add_args'], $link ); |
209
|
|
|
} |
210
|
|
|
$link = untrailingslashit( trailingslashit( $link ) . $args['add_fragment'] ); |
211
|
|
|
$page_links[] = array( |
212
|
|
|
'class' => 'next page-numbers', |
213
|
|
|
'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
214
|
|
|
'title' => $args['next_text'], |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
return $page_links; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Converts array to object recursively |
223
|
|
|
* |
224
|
|
|
* @param array $array |
225
|
|
|
* @return object |
226
|
|
|
*/ |
227
|
|
|
public static function array_to_object( $array ) { |
228
|
|
|
$obj = new \stdClass; |
229
|
|
|
|
230
|
|
|
foreach ( $array as $k => $v ) { |
231
|
|
|
if ( strlen( $k ) ) { |
232
|
|
|
if ( is_array( $v ) ) { |
233
|
|
|
$obj->{$k} = self::array_to_object( $v ); //RECURSION |
234
|
|
|
} else { |
235
|
|
|
$obj->{$k} = $v; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $obj; |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Returns Current Archives Page Title |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
public static function get_archives_title() { |
251
|
|
|
|
252
|
|
|
$textdomain = Classy::textdomain(); |
253
|
|
|
|
254
|
|
|
$archives_title = ''; |
255
|
|
|
|
256
|
|
|
if ( is_category() ) { |
257
|
|
|
|
258
|
|
|
$archives_title = single_cat_title( '', false ); |
259
|
|
|
|
260
|
|
|
} else if ( is_tag() ) { |
261
|
|
|
|
262
|
|
|
$archives_title = 'Tag: ' . single_tag_title( '', false ); |
263
|
|
|
|
264
|
|
|
} else if ( is_author() ) { |
265
|
|
|
|
266
|
|
|
if ( have_posts() ) { |
267
|
|
|
|
268
|
|
|
the_post(); |
269
|
|
|
$archives_title = 'Author: ' . get_the_author(); |
270
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
rewind_posts(); |
274
|
|
|
|
275
|
|
|
} else if ( is_search() ) { |
276
|
|
|
|
277
|
|
|
$archives_title = sprintf( __( 'Search Results for: %s', $textdomain ), '<span>' . get_search_query() . '</span>' ); |
278
|
|
|
|
279
|
|
|
} else if ( is_archive() ) { |
280
|
|
|
|
281
|
|
|
if ( is_day() ) { |
282
|
|
|
|
283
|
|
|
$archives_title = get_the_date(); |
284
|
|
|
|
285
|
|
|
} elseif ( is_month() ) { |
286
|
|
|
|
287
|
|
|
$archives_title = get_the_date( _x( 'F Y', 'monthly archives date format', $textdomain ) ); |
288
|
|
|
|
289
|
|
|
} elseif ( is_year() ) { |
290
|
|
|
|
291
|
|
|
$archives_title = get_the_date( _x( 'Y', 'yearly archives date format', $textdomain ) ); |
292
|
|
|
|
293
|
|
|
} else { |
294
|
|
|
|
295
|
|
|
$archives_title = 'Archives'; |
296
|
|
|
|
297
|
|
|
} |
298
|
|
|
} else { |
299
|
|
|
|
300
|
|
|
$archives_title = 'Archives'; |
301
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return $archives_title; |
305
|
|
|
|
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|