This repository has been archived by the owner on Dec 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
1209 lines (1069 loc) · 34.1 KB
/
functions.php
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Copyright (c) Adrien Foulon - 2018.
* Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*/
//namespace Tofandel;
if ( defined( 'WPP_MUPLUGIN' ) ) {
function wpp_before_theme_loaded( $root ) {
//Add a hook after the taxonomies and wp global variables are declared but before theme is loaded
do_action( 'before_theme_loaded' );
remove_filter( 'theme_root', 'wpp_before_theme_loaded' );
return $root;
}
add_filter( 'theme_root', 'wpp_before_theme_loaded' );
} else {
define( 'WPP_MUPLUGIN', false );
}
if ( ! function_exists( 'wpp_array_insert_after' ) ) {
/**
* Insert a value or key/value pair after a specific key in an array. If key doesn't exist, value is appended
* to the end of the array.
*
* @since 1.9
*
* @param array $array
* @param string $key
* @param array $new
*
* @return array
*/
function wpp_array_insert_after( array $array, $key, array $new ) {
$keys = array_keys( $array );
$index = array_search( $key, $keys );
$pos = false === $index ? count( $array ) : $index + 1;
return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );
}
}
if ( ! function_exists( 'wpp_admin_notice' ) ) {
/**
* @param $text
* @param string $type
*/
function wpp_admin_notice( $text, $type = 'success' ) {
add_action( 'admin_notices', function () use ( $text, $type ) {
echo "<div class='notice notice-$type is-dismissible'>
<p>$text</p>
</div>";
} );
}
}
if ( ! function_exists( 'wpp_remove_domain_from_url' ) ) {
/**
* @param $url
*
* @return mixed
*/
function wpp_remove_domain_from_url( $url ) {
if ( preg_match( '#(?:(?:https?:)?\/\/)[^\/]*(\/.*)?#', $url, $matches ) ) {
return $matches[ 1 ];
}
return $url;
}
}
if ( ! function_exists( 'wpp_get_domain_from_url' ) ) {
/**
* @param $url
* @param bool $scheme
*
* @return string
*/
function wpp_get_domain_from_url( $url, $scheme = false ) {
if ( preg_match( '#(https?:\/\/)?([^\/]*?)\/.*#', $url, $matches ) ) {
return $scheme ? $matches[ 1 ] . $matches[ 2 ] : $matches[ 2 ];
}
return $url;
}
}
if ( ! function_exists( 'wpp_has_shortcode' ) ) {
/**
* @param string $string
* @param string|array $shortcode
*
* @return bool
*/
function wpp_has_shortcode( $string, $shortcode ) {
global $shortcode_tags;
$old = $shortcode_tags;
if ( is_array( $shortcode ) ) {
$shortcode_tags = array_flip( $shortcode );
foreach ( $shortcode as $sh ) {
if ( has_shortcode( $string, $sh ) ) {
$shortcode_tags = $old;
return true;
}
}
} else {
$shortcode_tags = array( $shortcode => '' );
if ( has_shortcode( $string, $shortcode ) ) {
$shortcode_tags = $old;
return true;
}
}
$shortcode_tags = $old;
return false;
}
}
if ( ! function_exists( 'wpp_unique_slug' ) ) {
/**
* @param string $string The string to slugifiy
* @param bool $prepend Can be (and should) be used as a vendor name to separate slugs
*
* @param array $other_slugs
*
* @return string
*/
function wpp_unique_slug( $string, $prepend = false, $other_slugs = array() ) {
static $slugs = array();
$slugs = array_unique( array_merge( $slugs, $other_slugs ) );
$string = ( $prepend ? wpp_slugify( $prepend ) . '-' : '' ) . wpp_slugify( $string );
if ( ! in_array( $string, $slugs ) ) {
$slugs[] = $string;
return $string;
}
/** @noinspection PhpStatementHasEmptyBodyInspection */
for ( $i = 2; in_array( $string . $i, $slugs ); $i ++ ) {
;
}
$slugs[] = $string . $i;
return $string . $i;
}
}
if ( ! function_exists( 'wpp_get_editable_users' ) ) {
/**
* @param array $args
*
* @return array|mixed
*/
function wpp_get_editable_users( $args = array() ) {
static $users;
if ( ! isset( $users ) ) {
$roles = get_editable_roles();
$args = array_merge( $args, array( 'role__in' => array_keys( $roles ) ) );
$args = wpp_apply_filters( 'wpp_get_editable_users_args', $args );
if ( empty( $args[ 'role__in' ] ) ) {
$args[ 'include' ] = array_merge( wp_get_users_with_no_role(), array( get_current_user_id() ) );
}
$users_t = get_users( $args );
$users = array();
foreach ( $users_t as $user ) {
$users[ $user->ID ] = $user;
}
$user = wp_get_current_user();
$users = array_replace( array( $user->ID => $user ), $users );
$users = wpp_apply_filters( 'wpp_get_editable_users', $users );
}
return $users;
}
}
if ( ! function_exists( 'wpp_apply_filter' ) ) {
/**
* @param $filter
* @param $value
*
* @return mixed
*/
function wpp_apply_filters( $filter, $value ) {
if ( ! has_filter( $filter ) ) {
return $value;
} else {
$args = func_get_args();
return call_user_func_array( 'apply_filters', $args );
}
}
}
if ( ! function_exists( 'get_post_transient' ) ) {
/**
* @param int $post_id
* @param string $transient
*
* @return mixed
*/
function get_post_transient( $post_id, $transient ) {
return get_object_transient( 'post', $post_id, $transient );
}
}
if ( ! function_exists( 'set_post_transient' ) ) {
/**
* @param int $post_id
* @param string $transient
* @param mixed $value
* @param int $expiration
*
* @return bool
*/
function set_post_transient( $post_id, $transient, $value, $expiration = 0 ) {
return set_object_transient( 'post', $post_id, $transient, $value, $expiration );
}
}
if ( ! function_exists( 'delete_post_transient' ) ) {
/**
* @param int $post_id
* @param string $transient
*
* @return bool
*/
function delete_post_transient( $post_id, $transient ) {
return delete_object_transient( 'post', $post_id, $transient );
}
}
if ( ! function_exists( 'delete_expired_post_transients' ) ) {
/**
* @param bool $force_db
*/
function delete_expired_post_transients( $force_db = true ) {
delete_expired_object_transients( 'post', $force_db );
}
}
if ( ! function_exists( 'get_user_transient' ) ) {
/**
* @param int $user_id
* @param string $transient
*
* @return mixed
*/
function get_user_transient( $user_id, $transient ) {
return get_object_transient( 'user', $user_id, $transient );
}
}
if ( ! function_exists( 'set_user_transient' ) ) {
/**
* @param int $user_id
* @param string $transient
* @param mixed $value
* @param int $expiration
*
* @return bool
*/
function set_user_transient( $user_id, $transient, $value, $expiration = 0 ) {
return set_object_transient( 'user', $user_id, $transient, $value, $expiration );
}
}
if ( ! function_exists( 'delete_user_transient' ) ) {
/**
* @param int $user_id
* @param string $transient
*
* @return bool
*/
function delete_user_transient( $user_id, $transient ) {
return delete_object_transient( 'user', $user_id, $transient );
}
}
if ( ! function_exists( 'delete_expired_user_transients' ) ) {
/**
* @param bool $force_db
*/
function delete_expired_user_transients( $force_db = true ) {
delete_expired_object_transients( 'user', $force_db );
}
}
if ( ! function_exists( 'get_object_transient' ) ) {
/**
* Get the value of a transient.
*
* If the transient does not exist, does not have a value, or has expired,
* then the return value will be false.
*
* @since 2.8.0
*
* @param string $object
* @param int $object_id
* @param string $transient Transient name. Expected to not be SQL-escaped.
*
* @return mixed Value of transient.
*/
function get_object_transient( $object = null, $object_id, $transient ) {
if ( ! isset( $object ) ) {
return get_transient( $transient );
}
/**
* Filters the value of an existing transient.
*
* The dynamic portion of the hook name, `$transient`, refers to the transient name.
*
* Passing a truthy value to the filter will effectively short-circuit retrieval
* of the transient, returning the passed value instead.
*
* @since 2.8.0
* @since 4.4.0 The `$transient` parameter was added
*
* @param mixed $pre_transient The default value to return if the transient does not exist.
* Any value other than false will short-circuit the retrieval
* of the transient, and return the returned value.
* @param string $transient Transient name.
*/
$pre = wpp_apply_filters( "pre_{$object}_transient_{$transient}", false, $transient, $object_id );
if ( false !== $pre ) {
return $pre;
}
if ( wp_using_ext_object_cache() ) {
$value = wp_cache_get( $object_id . '_' . $transient, "{$object}_transient" );
} else {
$transient_option = '_transient_' . $transient;
$transient_timeout = '_transient_timeout_' . $transient;
$timeout = get_metadata( $object, $object_id, $transient_timeout, true );
if ( ! empty( $timeout ) && $timeout < time() ) {
delete_metadata( $object, $object_id, $transient_option );
delete_metadata( $object, $object_id, $transient_timeout );
$value = false;
}
if ( ! isset( $value ) ) {
$value = get_metadata( $object, $object_id, $transient_option, true );
}
}
/**
* Filters an existing transient's value.
*
* The dynamic portion of the hook name, `$transient`, refers to the transient name.
*
* @since 2.8.0
* @since 4.4.0 The `$transient` parameter was added
*
* @param mixed $value Value of transient.
* @param string $transient Transient name.
*/
return wpp_apply_filters( "{$object}_transient_{$transient}", $value, $transient );
}
}
if ( ! function_exists( 'set_object_transient' ) ) {
/**
* Set/update the value of a transient.
*
* You do not need to serialize values. If the value needs to be serialized, then
* it will be serialized before it is set.
*
* @since 2.8.0
*
* @param string $object
* @param int $object_id
* @param string $transient Transient name. Expected to not be SQL-escaped. Must be
* 172 characters or fewer in length.
* @param mixed $value Transient value. Must be serializable if non-scalar.
* Expected to not be SQL-escaped.
* @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
*
* @return bool False if value was not set and true if value was set.
*/
function set_object_transient( $object = null, $object_id, $transient, $value, $expiration = 0 ) {
if ( ! isset( $object ) ) {
set_transient( $transient, $value, $expiration );
}
$expiration = (int) $expiration;
/**
* Filters a specific transient before its value is set.
*
* The dynamic portion of the hook name, `$transient`, refers to the transient name.
*
* @since 3.0.0
* @since 4.2.0 The `$expiration` parameter was added.
* @since 4.4.0 The `$transient` parameter was added.
*
* @param mixed $value New value of transient.
* @param int $expiration Time until expiration in seconds.
* @param string $transient Transient name.
*/
$value = wpp_apply_filters( "pre_set_{$object}_transient_{$transient}", $value, $expiration, $transient );
/**
* Filters the expiration for a transient before its value is set.
*
* The dynamic portion of the hook name, `$transient`, refers to the transient name.
*
* @since 4.4.0
*
* @param int $expiration Time until expiration in seconds. Use 0 for no expiration.
* @param mixed $value New value of transient.
* @param string $transient Transient name.
*/
$expiration = wpp_apply_filters( "expiration_of_{$object}_transient_{$transient}", $expiration, $value, $transient );
if ( wp_using_ext_object_cache() ) {
$result = wp_cache_set( $object_id . '_' . $transient, $value, "{$object}_transient", $expiration );
} else {
$transient_timeout = '_transient_timeout_' . $transient;
$transient_option = '_transient_' . $transient;
if ( false === get_metadata( $object, $object_id, $transient_option, true ) ) {
if ( $expiration ) {
add_metadata( $object, $object_id, $transient_timeout, time() + $expiration, true );
}
$result = add_metadata( $object, $object_id, $transient_option, $value, true );
} else {
// If expiration is requested, but the transient has no timeout option,
// delete, then re-create transient rather than update.
$update = true;
if ( $expiration ) {
if ( false === get_metadata( $object, $object_id, $transient_timeout, true ) ) {
delete_metadata( $object, $object_id, $transient_option );
add_metadata( $object, $object_id, $transient_timeout, time() + $expiration, true );
$result = add_metadata( $object, $object_id, $transient_option, $value, true );
$update = false;
} else {
update_metadata( $object, $object_id, $transient_timeout, time() + $expiration );
}
}
if ( $update ) {
$result = update_metadata( $object, $object_id, $transient_option, $value );
}
}
}
if ( isset( $result ) && $result ) {
/**
* Fires after the value for a specific transient has been set.
*
* The dynamic portion of the hook name, `$transient`, refers to the transient name.
*
* @since 3.0.0
* @since 3.6.0 The `$value` and `$expiration` parameters were added.
* @since 4.4.0 The `$transient` parameter was added.
*
* @param mixed $value Transient value.
* @param int $expiration Time until expiration in seconds.
* @param string $transient The name of the transient.
*/
do_action( "set_{$object}_transient_{$transient}", $value, $expiration, $transient );
/**
* Fires after the value for a transient has been set.
*
* @since 3.0.0
* @since 3.6.0 The `$value` and `$expiration` parameters were added.
*
* @param string $transient The name of the transient.
* @param mixed $value Transient value.
* @param int $expiration Time until expiration in seconds.
*/
do_action( "setted_{$object}_transient", $transient, $value, $expiration );
return $result;
}
return false;
}
}
if ( ! function_exists( 'delete_object_transient' ) ) {
/**
* Delete a transient.
*
* @since 2.8.0
*
* @param string $object
* @param int $object_id
* @param string $transient Transient name. Expected to not be SQL-escaped.
*
* @return bool true if successful, false otherwise
*/
function delete_object_transient( $object = null, $object_id, $transient ) {
if ( ! isset( $object ) ) {
delete_transient( $transient );
}
/**
* Fires immediately before a specific transient is deleted.
*
* The dynamic portion of the hook name, `$transient`, refers to the transient name.
*
* @since 3.0.0
*
* @param string $transient Transient name.
*/
do_action( "delete_{$object}_transient_{$transient}", $transient, $object_id );
if ( wp_using_ext_object_cache() ) {
$result = wp_cache_delete( $transient, "{$object}_transient" );
} else {
$option_timeout = '_transient_timeout_' . $transient;
$option = '_transient_' . $transient;
$result = delete_metadata( $object, $object_id, $option );
if ( $result ) {
delete_metadata( $object, $object_id, $option_timeout );
}
}
if ( $result ) {
/**
* Fires after a transient is deleted.
*
* @since 3.0.0
*
* @param string $transient Deleted transient name.
*/
do_action( "deleted_{$object}_transient", $transient );
}
return $result;
}
}
if ( ! function_exists( 'delete_expired_object_transients' ) ) {
/**
* Deletes all expired transients.
*
* The multi-table delete syntax is used to delete the transient record
* from table a, and the corresponding transient_timeout record from table b.
*
* @since 4.9.0
*
* @param string $object
* @param bool $force_db Optional. Force cleanup to run against the database even when an external object cache is used.
*/
function delete_expired_object_transients( $object = 'user', $force_db = false ) {
global $wpdb;
if ( ! $force_db && wp_using_ext_object_cache() ) {
return;
}
$wpdb->query( $wpdb->prepare(
"DELETE a, b FROM {$wpdb->{$object.'meta'}} a, {$wpdb->{$object.'meta'}} b
WHERE a.meta_key LIKE %s
AND a.meta_key NOT LIKE %s
AND b.meta_key = CONCAT( '_transient_timeout_', SUBSTRING( a.meta_key, 12 ) )
AND b.meta_value < %d",
$wpdb->esc_like( '_transient_' ) . '%',
$wpdb->esc_like( '_transient_timeout_' ) . '%',
time()
) );
$wpdb->query( $wpdb->prepare(
"DELETE FROM {$wpdb->options}
WHERE option_name LIKE %s",
$wpdb->esc_like( '_transient_' ) . '%'
) );
}
}
if ( ! function_exists( 'wpp_edit_user' ) ) {
/**
* Edit user settings based on contents of $_POST
*
* Used on user-edit.php and profile.php to manage and process user options, passwords etc.
*
* @since 2.0.0
*
* @param int $user_id Optional. User ID.
*
* @return int|WP_Error user id of the updated user
*/
function wpp_edit_user( $user_id = 0 ) {
$wp_roles = wp_roles();
$user = new stdClass;
if ( $user_id ) {
$update = true;
$user->ID = (int) $user_id;
$userdata = get_userdata( $user_id );
$user->user_login = wp_slash( $userdata->user_login );
} else {
$update = false;
}
if ( ! $update && isset( $_POST[ 'user_login' ] ) ) {
$user->user_login = sanitize_user( $_POST[ 'user_login' ], true );
}
$pass1 = $pass2 = '';
if ( isset( $_POST[ 'pass1' ] ) ) {
$pass1 = $_POST[ 'pass1' ];
}
if ( isset( $_POST[ 'pass2' ] ) ) {
$pass2 = $_POST[ 'pass2' ];
}
$errors = new WP_Error();
if ( isset( $_POST[ 'role' ] ) ) {
$new_role = sanitize_text_field( $_POST[ 'role' ] );
$potential_role = isset( $wp_roles->role_objects[ $new_role ] ) ? $wp_roles->role_objects[ $new_role ] : false;
// Don't let anyone with 'edit_users' (admins) edit their own role to something without it.
// Multisite super admins can freely edit their blog roles -- they possess all caps.
if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || $user_id != get_current_user_id() || ( $potential_role && $potential_role->has_cap( 'edit_users' ) ) ) {
$user->role = $new_role;
}
// If the new role isn't editable by the logged-in user die with error
$editable_roles = get_editable_roles();
if ( ! empty( $new_role ) && empty( $editable_roles[ $new_role ] ) ) {
$errors->add( 'role', __( 'Sorry, you are not allowed to give users that role.' ) );
return $errors;
}
}
if ( isset( $_POST[ 'email' ] ) ) {
$user->user_email = sanitize_text_field( wp_unslash( $_POST[ 'email' ] ) );
}
if ( isset( $_POST[ 'url' ] ) ) {
if ( empty ( $_POST[ 'url' ] ) || $_POST[ 'url' ] == 'http://' ) {
$user->user_url = '';
} else {
$user->user_url = esc_url_raw( $_POST[ 'url' ] );
$protocols = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) );
$user->user_url = preg_match( '/^(' . $protocols . '):/is', $user->user_url ) ? $user->user_url : 'http://' . $user->user_url;
}
}
if ( isset( $_POST[ 'first_name' ] ) ) {
$user->first_name = sanitize_text_field( $_POST[ 'first_name' ] );
}
if ( isset( $_POST[ 'last_name' ] ) ) {
$user->last_name = sanitize_text_field( $_POST[ 'last_name' ] );
}
if ( isset( $_POST[ 'nickname' ] ) ) {
$user->nickname = sanitize_text_field( $_POST[ 'nickname' ] );
}
if ( isset( $_POST[ 'display_name' ] ) ) {
$user->display_name = sanitize_text_field( $_POST[ 'display_name' ] );
}
if ( isset( $_POST[ 'description' ] ) ) {
$user->description = trim( $_POST[ 'description' ] );
}
foreach ( wp_get_user_contact_methods( $user ) as $method => $name ) {
if ( isset( $_POST[ $method ] ) ) {
$user->$method = sanitize_text_field( $_POST[ $method ] );
}
}
if ( $update ) {
$user->rich_editing = isset( $_POST[ 'rich_editing' ] ) && 'false' === $_POST[ 'rich_editing' ] ? 'false' : 'true';
$user->syntax_highlighting = isset( $_POST[ 'syntax_highlighting' ] ) && 'false' === $_POST[ 'syntax_highlighting' ] ? 'false' : 'true';
$user->admin_color = isset( $_POST[ 'admin_color' ] ) ? sanitize_text_field( $_POST[ 'admin_color' ] ) : 'fresh';
$user->show_admin_bar_front = isset( $_POST[ 'admin_bar_front' ] ) ? 'true' : 'false';
$user->locale = '';
if ( isset( $_POST[ 'locale' ] ) ) {
$locale = sanitize_text_field( $_POST[ 'locale' ] );
if ( 'site-default' === $locale ) {
$locale = '';
} elseif ( '' === $locale ) {
$locale = 'en_US';
} elseif ( ! in_array( $locale, get_available_languages(), true ) ) {
$locale = '';
}
$user->locale = $locale;
}
}
$user->comment_shortcuts = isset( $_POST[ 'comment_shortcuts' ] ) && 'true' == $_POST[ 'comment_shortcuts' ] ? 'true' : '';
$user->use_ssl = 0;
if ( ! empty( $_POST[ 'use_ssl' ] ) ) {
$user->use_ssl = 1;
}
/* checking that username has been typed */
if ( $user->user_login == '' ) {
$errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ) );
}
/* checking that nickname has been typed */
if ( $update && empty( $user->nickname ) ) {
$errors->add( 'nickname', __( '<strong>ERROR</strong>: Please enter a nickname.' ) );
}
/**
* Fires before the password and confirm password fields are checked for congruity.
*
* @since 1.5.1
*
* @param string $user_login The username.
* @param string $pass1 The password (passed by reference).
* @param string $pass2 The confirmed password (passed by reference).
*/
do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) );
// Check for blank password when adding a user.
if ( ! $update && empty( $pass1 ) ) {
$errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter a password.' ), array( 'form-field' => 'pass1' ) );
}
// Check for "\" in password.
if ( false !== strpos( wp_unslash( $pass1 ), "\\" ) ) {
$errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
}
// Checking the password has been typed twice the same.
if ( ( $update || ! empty( $pass1 ) ) && $pass1 != $pass2 ) {
$errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in both password fields.' ), array( 'form-field' => 'pass1' ) );
}
if ( ! empty( $pass1 ) ) {
$user->user_pass = $pass1;
}
if ( ! $update && isset( $_POST[ 'user_login' ] ) && ! validate_username( $_POST[ 'user_login' ] ) ) {
$errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
}
if ( ! $update && username_exists( $user->user_login ) ) {
$errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) );
}
/** This filter is documented in wp-includes/user.php */
$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
if ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ) ) ) {
$errors->add( 'invalid_username', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) );
}
/* checking email address */
if ( empty( $user->user_email ) ) {
$errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an email address.' ), array( 'form-field' => 'email' ) );
} elseif ( ! is_email( $user->user_email ) ) {
$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ), array( 'form-field' => 'email' ) );
} elseif ( ( $owner_id = email_exists( $user->user_email ) ) && ( ! $update || ( $owner_id != $user->ID ) ) ) {
$errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ), array( 'form-field' => 'email' ) );
}
/**
* Fires before user profile update errors are returned.
*
* @since 2.8.0
*
* @param WP_Error $errors WP_Error object (passed by reference).
* @param bool $update Whether this is a user update.
* @param stdClass $user User object (passed by reference).
*/
do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) );
if ( $errors->get_error_codes() ) {
return $errors;
}
if ( $update ) {
$user_id = wp_update_user( $user );
} else {
$user_id = wp_insert_user( $user );
$notify = isset( $_POST[ 'send_user_notification' ] ) ? 'user' : '';
if ( ! empty( $notify ) ) {
/**
* Fires after a new user has been created.
*
* @since 4.4.0
*
* @param int $user_id ID of the newly created user.
* @param string $notify Type of notification that should happen. See wp_send_new_user_notifications()
* for more information on possible values.
*/
do_action( 'edit_user_created_user', $user_id, $notify );
}
}
return $user_id;
}
}
if ( ! defined( 'A_MIN_IN_S' ) ) {
define( 'A_MIN_IN_S', 60 * 60 );
}
if ( ! defined( 'A_DAY_IN_S' ) ) {
define( 'A_DAY_IN_S', A_MIN_IN_S * 24 );
}
if ( ! defined( 'A_WEEK_IN_S' ) ) {
define( 'A_WEEK_IN_S', A_DAY_IN_S * 7 );
}
if ( ! defined( 'A_MONTH_IN_S' ) ) {
define( 'A_MONTH_IN_S', A_DAY_IN_S * 30 );
}
if ( ! defined( 'A_YEAR_IN_S' ) ) {
define( 'A_YEAR_IN_S', A_DAY_IN_S * 365 );
}
if ( ! function_exists( 'wpp_comp' ) ) {
function wpp_comp( $a, $b ) {
if ( PHP_MAJOR_VERSION >= 7 ) {
return $a <=> $b;
} else {
if ( $a == $b ) {
return 0;
}
return ( ( $a < $b ) * 2 ) - 1;
}
}
}
if ( ! function_exists( 'wpp_is_float' ) ) {
function wpp_is_float( $val ) {
if ( ! is_scalar( $val ) || empty( $val ) ) {
return false;
}
return wpp_is_integer( $val ) || @is_float( $val + 0 );
}
}
if ( ! function_exists( 'wpp_is_integer' ) ) {
function wpp_is_integer( $val ) {
if ( ! is_scalar( $val ) || is_bool( $val ) ) {
return false;
}
return is_float( $val ) ? false : preg_match( '~^((?:\+|-)?[0-9]+)$~', $val );
}
}
if ( ! function_exists( 'wpp_is_date' ) ) {
function wpp_is_date( $val ) {
return (bool) strtotime( $val );
}
}
if ( ! function_exists( 'wpp_is_plugin_active' ) ) {
function wpp_is_plugin_active( $plugin ) {
return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || wpp_is_plugin_active_for_network( $plugin );
}
}
if ( ! function_exists( 'wpp_is_plugin_active_for_network' ) ) {
/**
* Check whether the plugin is active for the entire network.
*
* Only plugins installed in the plugins/ folder can be active.
*
* Plugins in the mu-plugins/ folder can't be "activated," so this function will
* return false for those plugins.
*
* @since 3.0.0
*
* @param string $plugin Path to the main plugin file from plugins directory.
*
* @return bool True, if active for the network, otherwise false.
*/
function wpp_is_plugin_active_for_network( $plugin ) {
if ( ! is_multisite() ) {
return false;
}
$plugins = get_site_option( 'active_sitewide_plugins' );
if ( isset( $plugins[ $plugin ] ) ) {
return true;
}
return false;
}
}
if ( ! function_exists( 'wpp_slugify' ) ) {
/**
* @param $string
* @param bool $slashes
*
* @return mixed|null|string|string[]
*/
function wpp_slugify( $string, $slashes = true ) {
//Lower case everything
$string = mb_strtolower( $string );
$normalizeChars = array(
'š' => 's',
'ž' => 'z',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'ã' => 'a',
'ä' => 'a',
'å' => 'a',
'æ' => 'ae',
'ç' => 'c',
'è' => 'e',
'é' => 'e',
'ê' => 'e',
'ë' => 'e',
'ì' => 'i',
'í' => 'i',
'î' => 'i',
'ï' => 'i',
'ð' => 'o',
'ñ' => 'n',
'ń' => 'n',
'ò' => 'o',
'ó' => 'o',
'ô' => 'o',
'õ' => 'o',
'ö' => 'o',
'ø' => 'o',
'ù' => 'u',
'ú' => 'u',
'û' => 'u',
'ü' => 'u',
'ý' => 'y',
'þ' => 'b',
'ÿ' => 'y',
'ƒ' => 'f',
'ă' => 'a',
'ș' => 's',
'ț' => 't',
'œ' => 'oe',
'+' => 'plus',
' ' => '-',
);
if ( $slashes ) {
$normalizeChars[ '/' ] = '-';
}
$string = strtr( $string, $normalizeChars );
//Make alphanumeric (removes all other characters)
$string = preg_replace( "/[^a-z0-9_" . ( $slashes ? '' : '\/' ) . "-]/", "", $string );
//Clean up multiple dashes or whitespaces
$string = preg_replace( "/[\s-]+/", "-", $string );
//Convert whitespaces and underscore to dash
//$string = preg_replace( "/[\s_]/", "-", $string );
return $string;
}
}
if ( ! function_exists( 'wpp_order_by' ) ) {
/**
* Order a multidimensional array from a subelement set specified as a path "foo.bar.orderValue"
*
* @param array $array
* @param string $path
* @param bool $keep_keys
*
* @return array
* @throws Exception
*/
function wpp_order_by( $array, $path, $keep_keys = false ) {
if ( empty( $array ) ) {
return array();
}
if ( ! is_array( $array ) ) {
throw new \Exception( 'The "orderBy" filter can only be used on array (' . gettype( $array ) . ' given)' );
}
$path = explode( '.', $path );
$c = count( $path );
// Sort the multidimensional array
( $keep_keys ? 'uasort' : 'usort' )( $array, function ( $a, $b ) use ( $path, $c ) {
$v1 = $a;
for ( $i = 0; $i < $c; $i ++ ) {
$k = $path[ $i ];
if ( isset( $v1[ $k ] ) ) {
$v1 = $v1[ $k ];
} else {
$v1 = '0';