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