general-utilities.php
13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
/**
* General utilities, not directly related to events display, management, or organization.
*
* @category Utilities
* @package My Calendar
* @author Joe Dolson
* @license GPLv2 or later
* @link https://www.joedolson.com/my-calendar/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Switch sites in multisite environment.
*
* @return boolean
*/
function mc_switch_sites() {
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
if ( get_site_option( 'mc_multisite' ) === '2' && my_calendar_table() !== my_calendar_table( 'global' ) ) {
if ( mc_get_option( 'current_table' ) === '1' ) {
// can post to either, but is currently set to post to central table.
return true;
}
} elseif ( get_site_option( 'mc_multisite' ) === '1' && my_calendar_table() !== my_calendar_table( 'global' ) ) {
// can only post to central table.
return true;
}
}
return false;
}
/**
* Send a Tweet on approval of event
*
* @param string $prev Previous status.
* @param string $new New status.
*
* @return void
*/
function mc_tweet_approval( $prev, $new ) {
if ( function_exists( 'wpt_post_to_twitter' ) && isset( $_POST['mc_twitter'] ) && trim( $_POST['mc_twitter'] ) !== '' ) {
if ( ( 0 === (int) $prev || 2 === (int) $prev ) && 1 === (int) $new ) {
wpt_post_to_twitter( stripslashes( $_POST['mc_twitter'] ) );
}
}
}
/**
* Flatten event array; need an array that isn't multi dimensional
* Once used in upcoming events?
*
* @param array $events Array of events.
*
* @return array<int, mixed>
*/
function mc_flatten_array( $events ) {
$new_array = array();
if ( is_array( $events ) ) {
foreach ( $events as $event ) {
foreach ( $event as $e ) {
$new_array[] = $e;
}
}
}
return $new_array;
}
add_action( 'admin_menu', 'mc_add_outer_box' );
/**
* Add meta boxes
*
* @return void
*/
function mc_add_outer_box() {
add_meta_box( 'mcs_add_event', __( 'My Calendar Event', 'my-calendar' ), 'mc_add_inner_box', 'mc-events', 'side', 'high' );
}
/**
* Add inner metabox
*
* @return void
*/
function mc_add_inner_box() {
global $post;
$event_id = get_post_meta( $post->ID, '_mc_event_id', true );
if ( $event_id ) {
$url = admin_url( 'admin.php?page=my-calendar&mode=edit&event_id=' . $event_id );
$event = mc_get_first_event( $event_id );
$content = '<p><strong>' . strip_tags( $event->event_title, mc_strip_tags() ) . '</strong><br />' . $event->event_begin . ' @ ' . $event->event_time . '</p>';
if ( ! mc_is_recurring( $event ) ) {
$recur = mc_event_recur_string( $event, $event->event_begin );
$content .= wpautop( $recur );
}
if ( '' !== $event->event_label ) {
// Translators: Name of event location.
$content .= '<p>' . sprintf( __( '<strong>Location:</strong> %s', 'my-calendar' ), strip_tags( $event->event_label, mc_strip_tags() ) ) . '</p>';
}
// Translators: Event URL.
$content .= '<p>' . sprintf( __( '<a href="%s">Edit event</a>.', 'my-calendar' ), $url ) . '</p>';
echo $content;
}
}
/**
* Pass group of allowed tags to strip_tags
*
* @return string Allowed tags parseable by strip_tags.
*/
function mc_strip_tags() {
return apply_filters( 'mc_strip_tags', '<strong><em><i><b><span><br><a><time><img>' );
}
/**
* Pass group of allowed tags to strip_tags
*
* @return string Allowed tags parseable by strip_tags in admin.
*/
function mc_admin_strip_tags() {
return '<strong><em><i><b><span><a><code><pre><br>';
}
if ( ! function_exists( 'exif_imagetype' ) ) {
/**
* This is a hack for people who don't have PHP installed with exif_imagetype
*
* @param string $filename Name of file.
*
* @return string|bool type of file.
*/
function exif_imagetype( $filename ) {
if ( ! is_dir( $filename ) && ( list( $width, $height, $type, $attr ) = getimagesize( $filename ) ) !== false ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.NonVariableAssignmentFound
return $type;
}
return false;
}
}
/**
* Return default state of link expiration checkbox. Replaces option.
*
* @return bool Default false.
*/
function mc_event_link_expires() {
$return = false;
$default = get_option( 'mc_event_link_expires' ); // Option only exists prior to 3.3.0.
if ( 'true' === $default ) {
$return = true;
}
return apply_filters( 'mc_event_link_expires', $return );
}
/**
* Return default state of fifth week checkbox. Replaces option.
*
* @return bool Default true.
*/
function mc_no_fifth_week() {
$return = true;
$default = get_option( 'mc_no_fifth_week' ); // Option only exists prior to 3.3.0.
if ( 'false' === $default ) {
$return = false;
}
return apply_filters( 'mc_no_fifth_week', $return );
}
/**
* Return default state of skip holidays checkbox. Replaces option.
*
* @return bool Default false.
*/
function mc_skip_holidays() {
$return = false;
$default = get_option( 'mc_skip_holidays' ); // Option only exists before 3.3.0.
if ( 'true' === $default ) {
$return = true;
}
return apply_filters( 'mc_skip_holidays', $return );
}
/**
* Checks the contrast ratio of color & returns the optimal color to use with it.
*
* @param string $color hex value.
*
* @return string white or black hex value
*/
function mc_inverse_color( $color ) {
$color = str_replace( '#', '', $color );
if ( strlen( $color ) !== 6 ) {
return '#000000';
}
$rgb = '';
$total = 0;
$red = 0.299 * ( 255 - hexdec( substr( $color, 0, 2 ) ) );
$green = 0.587 * ( 255 - hexdec( substr( $color, 2, 2 ) ) );
$blue = 0.114 * ( 255 - hexdec( substr( $color, 4, 2 ) ) );
$luminance = 1 - ( ( $red + $green + $blue ) / 255 );
if ( $luminance < 0.5 ) {
return '#ffffff';
} else {
return '#000000';
}
}
/**
* Shift color to an acceptable alternate color. Shifts dark colors darker and light colors lighter.
*
* @param string $color Color hex.
*
* @return string New color hex
*/
function mc_shift_color( $color ) {
$color = str_replace( '#', '', $color );
$rgb = '';
$percent = ( mc_inverse_color( $color ) === '#ffffff' ) ? - 20 : 20;
$per = $percent / 100 * 255;
// Percentage to work with. Change middle figure to control color temperature.
if ( $per < 0 ) {
// DARKER.
$per = abs( $per ); // Turns Neg Number to Pos Number.
for ( $x = 0; $x < 3; $x ++ ) {
$c = hexdec( substr( $color, ( 2 * $x ), 2 ) ) - $per;
$c = ( $c < 0 ) ? 0 : dechex( $c );
$rgb .= ( strlen( $c ) < 2 ) ? '0' . $c : $c;
}
} else {
// LIGHTER.
for ( $x = 0; $x < 3; $x ++ ) {
$c = hexdec( substr( $color, ( 2 * $x ), 2 ) ) + $per;
$c = ( $c > 255 ) ? 'ff' : dechex( $c );
$rgb .= ( strlen( $c ) < 2 ) ? '0' . $c : $c;
}
}
return '#' . $rgb;
}
/**
* Convert a CSV string into an array
*
* @param string $csv Data.
* @param string $delimiter to use.
* @param string $enclosure to wrap strings.
* @param string $escape character.
* @param string $terminator end of line character.
*
* @return array
*/
function mc_csv_to_array( $csv, $delimiter = ',', $enclosure = '"', $escape = '\\', $terminator = "\n" ) {
$r = array();
$rows = explode( $terminator, trim( $csv ) );
foreach ( $rows as $row ) {
if ( trim( $row ) ) {
$values = explode( $delimiter, $row );
$r[ $values[0] ] = ( isset( $values[1] ) ) ? str_replace( array( $enclosure, $escape ), '', $values[1] ) : $values[0];
}
}
return $r;
}
/**
* Return string for HTML email types
*/
function mc_html_type() {
return 'text/html';
}
/**
* Test if a string is a properly formatted URL.
*
* @param string $url URL.
*
* @return int|false URL, if valid.
*/
function _mc_is_url( $url ) {
return preg_match( '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url );
}
/**
* Check whether a link is external
*
* @param string $link URL.
*
* @return boolean true if not on current host
*/
function mc_external_link( $link ) {
if ( ! _mc_is_url( $link ) ) {
return true; // If this is not a valid URL, consider it to be external.
}
$url = parse_url( $link );
$host = $url['host'];
$site = parse_url( get_option( 'siteurl' ) );
$known = $site['host'];
if ( false === strpos( $host, $known ) ) {
return true;
}
return false;
}
/**
* Replace newline characters in a string
*
* @param string $string Any string.
*
* @return string string without newline chars
*/
function mc_newline_replace( $string ) {
return (string) str_replace( array( "\r", "\r\n", "\n" ), '', $string );
}
/**
* Reverse the order of an array
*
* @param array $array Any array.
* @param boolean $boolean true or false arguments for array_reverse.
* @param string $order sort order to use.
*
* @return array
*/
function reverse_array( $array, $boolean, $order ) {
if ( 'desc' === $order ) {
return array_reverse( $array, $boolean );
} else {
return $array;
}
}
/**
* Debugging handler shortcut
*
* @param string $subject Text for email subject.
* @param string $body Text for email body.
* @param string $email target email (if sending via email).
*/
function mc_debug( $subject, $body, $email = '' ) {
if ( defined( 'MC_DEBUG' ) && true === MC_DEBUG ) {
if ( ! $email ) {
$email = get_option( 'admin_email' );
}
if ( defined( 'MC_DEBUG_METHOD' ) && 'email' === MC_DEBUG_METHOD ) {
wp_mail( get_option( 'admin_email' ), $subject, print_r( $body ) );
} else {
/**
* Execute a custom debug action during an mc_debug call. Runs if MC_DEBUG_METHOD is not 'email'.
*
* @hook mc_debug
*
* @param {string} $subject Subject line of email debugging message.
* @param {string} $body Body of email debugging message.
*/
do_action( 'mc_debug', $subject, $body );
}
}
}
/**
* Get users as options in a select
*
* @param string $selected Group of selected users. Comma-separated IDs.
* @param string $group Type of roles to fetch.
* @param string $return Type of return; string of select options or array.
*
* @return string|array <option> elements or an array of possible values.
*/
function mc_selected_users( $selected = '', $group = 'authors', $return = 'select' ) {
/**
* Filter the list of users used to select authors or hosts.
*
* @hook mc_custom_user_select
*
* @param {string} $output Output that should replace data.
* @param {string|int} $selected The currently selected user.
* @param {string} $group Whether this function is returning hosts or authors.
* @param {string} $return Whether this should return fully realized <option> values or an array of data.
*
* @return {string|array}
*/
$options = apply_filters( 'mc_custom_user_select', '', $selected, $group, $return );
if ( '' !== $options ) {
return $options;
}
$selected = explode( ',', $selected );
$users = mc_get_users( $group );
$values = array();
foreach ( $users as $u ) {
if ( in_array( $u->ID, $selected, true ) ) {
$checked = ' selected="selected"';
} else {
$checked = '';
}
$display_name = ( '' === $u->display_name ) ? $u->user_nicename : $u->display_name;
$options .= '<option value="' . $u->ID . '"' . $checked . ">$display_name</option>\n";
$values[] = array(
'value' => $u->ID,
'label' => $display_name,
);
}
return ( 'select' === $return ) ? $options : $values;
}
/**
* Get users.
*
* @param string $group Not used except in filters.
*
* @return array of users
*/
function mc_get_users( $group = 'authors' ) {
global $blog_id;
$users = apply_filters( 'mc_get_users', false, $group, $blog_id );
if ( $users ) {
return $users;
}
$count = count_users( 'time' );
$args = array(
'blog_id' => $blog_id,
'orderby' => 'display_name',
'fields' => array( 'ID', 'user_nicename', 'display_name' ),
);
$args = apply_filters( 'mc_filter_user_arguments', $args, $count, $group );
$users = new WP_User_Query( $args );
return $users->get_results();
}
/**
* Display an update message.
*
* @param string $message Update message.
* @param boolean $echo Echo or return. Default true (echo).
* @param boolean|string $code Message code.
*
* @return string
*/
function mc_show_notice( $message, $echo = true, $code = false ) {
if ( trim( $message ) === '' ) {
return '';
}
$message = strip_tags( apply_filters( 'mc_filter_notice', $message, $code ), mc_admin_strip_tags() );
$message = "<div class='updated'><p>$message</p></div>";
if ( $echo ) {
echo wp_kses_post( $message );
}
return $message;
}
/**
* Display an error message.
*
* @param string $message Error message.
* @param boolean $echo Echo or return. Default true (echo).
* @param boolean|string $code Message code.
*
* @return string
*/
function mc_show_error( $message, $echo = true, $code = false ) {
if ( trim( $message ) === '' ) {
return '';
}
$message = strip_tags( apply_filters( 'mc_filter_error', $message, $code ), mc_admin_strip_tags() );
$message = "<div class='error'><p>$message</p></div>";
if ( $echo ) {
echo $message;
}
return $message;
}