-
Notifications
You must be signed in to change notification settings - Fork 5
/
functions.php
3643 lines (3243 loc) · 112 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
/**
* Main WordPress API
*
* @package WordPress
*/
/**
* Converts MySQL DATETIME field to user specified date format.
*
* If $dateformatstring has 'G' value, then gmmktime() function will be used to
* make the time. If $dateformatstring is set to 'U', then mktime() function
* will be used to make the time.
*
* The $translate will only be used, if it is set to true and it is by default
* and if the $wp_locale object has the month and weekday set.
*
* @since 0.71
*
* @param string $dateformatstring Either 'G', 'U', or php date format.
* @param string $mysqlstring Time from mysql DATETIME field.
* @param bool $translate Optional. Default is true. Will switch format to locale.
* @return string Date formated by $dateformatstring or locale (if available).
*/
function mysql2date( $dateformatstring, $mysqlstring, $translate = true ) {
global $wp_locale;
$m = $mysqlstring;
if ( empty( $m ) )
return false;
if( 'G' == $dateformatstring ) {
return strtotime( $m . ' +0000' );
}
$i = strtotime( $m );
if( 'U' == $dateformatstring )
return $i;
if ( $translate)
return date_i18n( $dateformatstring, $i );
else
return date( $dateformatstring, $i );
}
/**
* Retrieve the current time based on specified type.
*
* The 'mysql' type will return the time in the format for MySQL DATETIME field.
* The 'timestamp' type will return the current timestamp.
*
* If $gmt is set to either '1' or 'true', then both types will use GMT time.
* if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
*
* @since 1.0.0
*
* @param string $type Either 'mysql' or 'timestamp'.
* @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false.
* @return int|string String if $type is 'gmt', int if $type is 'timestamp'.
*/
function current_time( $type, $gmt = 0 ) {
switch ( $type ) {
case 'mysql':
return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
break;
case 'timestamp':
return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * 3600 );
break;
}
}
/**
* Retrieve the date in localized format, based on timestamp.
*
* If the locale specifies the locale month and weekday, then the locale will
* take over the format for the date. If it isn't, then the date format string
* will be used instead.
*
* @since 0.71
*
* @param string $dateformatstring Format to display the date.
* @param int $unixtimestamp Optional. Unix timestamp.
* @param bool $gmt Optional, default is false. Whether to convert to GMT for time.
* @return string The date, translated if locale specifies it.
*/
function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
global $wp_locale;
$i = $unixtimestamp;
// Sanity check for PHP 5.1.0-
if ( false === $i || intval($i) < 0 ) {
if ( ! $gmt )
$i = current_time( 'timestamp' );
else
$i = time();
// we should not let date() interfere with our
// specially computed timestamp
$gmt = true;
}
// store original value for language with untypical grammars
// see http://core.trac.wordpress.org/ticket/9396
$req_format = $dateformatstring;
$datefunc = $gmt? 'gmdate' : 'date';
if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
$datemonth = $wp_locale->get_month( $datefunc( 'm', $i ) );
$datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
$dateweekday = $wp_locale->get_weekday( $datefunc( 'w', $i ) );
$dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
$datemeridiem = $wp_locale->get_meridiem( $datefunc( 'a', $i ) );
$datemeridiem_capital = $wp_locale->get_meridiem( $datefunc( 'A', $i ) );
$dateformatstring = ' '.$dateformatstring;
$dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
$dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
$dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
$dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
$dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
$dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
}
$j = @$datefunc( $dateformatstring, $i );
// allow plugins to redo this entirely for languages with untypical grammars
$j = apply_filters('date_i18n', $j, $req_format, $i, $gmt);
return $j;
}
/**
* Convert number to format based on the locale.
*
* @since 2.3.0
*
* @param mixed $number The number to convert based on locale.
* @param int $decimals Precision of the number of decimal places.
* @return string Converted number in string format.
*/
function number_format_i18n( $number, $decimals = null ) {
global $wp_locale;
// let the user override the precision only
$decimals = ( is_null( $decimals ) ) ? $wp_locale->number_format['decimals'] : intval( $decimals );
$num = number_format( $number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
// let the user translate digits from latin to localized language
return apply_filters( 'number_format_i18n', $num );
}
/**
* Convert number of bytes largest unit bytes will fit into.
*
* It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts
* number of bytes to human readable number by taking the number of that unit
* that the bytes will go into it. Supports TB value.
*
* Please note that integers in PHP are limited to 32 bits, unless they are on
* 64 bit architecture, then they have 64 bit size. If you need to place the
* larger size then what PHP integer type will hold, then use a string. It will
* be converted to a double, which should always have 64 bit length.
*
* Technically the correct unit names for powers of 1024 are KiB, MiB etc.
* @link http://en.wikipedia.org/wiki/Byte
*
* @since 2.3.0
*
* @param int|string $bytes Number of bytes. Note max integer size for integers.
* @param int $decimals Precision of number of decimal places.
* @return bool|string False on failure. Number string on success.
*/
function size_format( $bytes, $decimals = null ) {
$quant = array(
// ========================= Origin ====
'TB' => 1099511627776, // pow( 1024, 4)
'GB' => 1073741824, // pow( 1024, 3)
'MB' => 1048576, // pow( 1024, 2)
'kB' => 1024, // pow( 1024, 1)
'B ' => 1, // pow( 1024, 0)
);
foreach ( $quant as $unit => $mag )
if ( doubleval($bytes) >= $mag )
return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
return false;
}
/**
* Get the week start and end from the datetime or date string from mysql.
*
* @since 0.71
*
* @param string $mysqlstring Date or datetime field type from mysql.
* @param int $start_of_week Optional. Start of the week as an integer.
* @return array Keys are 'start' and 'end'.
*/
function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
$my = substr( $mysqlstring, 0, 4 ); // Mysql string Year
$mm = substr( $mysqlstring, 8, 2 ); // Mysql string Month
$md = substr( $mysqlstring, 5, 2 ); // Mysql string day
$day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day.
$weekday = date( 'w', $day ); // The day of the week from the timestamp
$i = 86400; // One day
if( !is_numeric($start_of_week) )
$start_of_week = get_option( 'start_of_week' );
if ( $weekday < $start_of_week )
$weekday = 7 - $start_of_week - $weekday;
while ( $weekday > $start_of_week ) {
$weekday = date( 'w', $day );
if ( $weekday < $start_of_week )
$weekday = 7 - $start_of_week - $weekday;
$day -= 86400;
$i = 0;
}
$week['start'] = $day + 86400 - $i;
$week['end'] = $week['start'] + 604799;
return $week;
}
/**
* Unserialize value only if it was serialized.
*
* @since 2.0.0
*
* @param string $original Maybe unserialized original, if is needed.
* @return mixed Unserialized data can be any type.
*/
function maybe_unserialize( $original ) {
if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
return @unserialize( $original );
return $original;
}
/**
* Check value to find if it was serialized.
*
* If $data is not an string, then returned value will always be false.
* Serialized data is always a string.
*
* @since 2.0.5
*
* @param mixed $data Value to check to see if was serialized.
* @return bool False if not serialized and true if it was.
*/
function is_serialized( $data ) {
// if it isn't a string, it isn't serialized
if ( !is_string( $data ) )
return false;
$data = trim( $data );
if ( 'N;' == $data )
return true;
if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
return false;
switch ( $badions[1] ) {
case 'a' :
case 'O' :
case 's' :
if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
return true;
break;
case 'b' :
case 'i' :
case 'd' :
if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
return true;
break;
}
return false;
}
/**
* Check whether serialized data is of string type.
*
* @since 2.0.5
*
* @param mixed $data Serialized data
* @return bool False if not a serialized string, true if it is.
*/
function is_serialized_string( $data ) {
// if it isn't a string, it isn't a serialized string
if ( !is_string( $data ) )
return false;
$data = trim( $data );
if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings
return true;
return false;
}
/**
* Retrieve option value based on setting name.
*
* If the option does not exist or does not have a value, then the return value
* will be false. This is useful to check whether you need to install an option
* and is commonly used during installation of plugin options and to test
* whether upgrading is required.
*
* You can "short-circuit" the retrieval of the option from the database for
* your plugin or core options that aren't protected. You can do so by hooking
* into the 'pre_option_$option' with the $option being replaced by the option
* name. You should not try to override special options, but you will not be
* prevented from doing so.
*
* There is a second filter called 'option_$option' with the $option being
* replaced with the option name. This gives the value as the only parameter.
*
* If the option was serialized, when the option was added and, or updated, then
* it will be unserialized, when it is returned.
*
* @since 1.5.0
* @package WordPress
* @subpackage Option
* @uses apply_filters() Calls 'pre_option_$optionname' false to allow
* overwriting the option value in a plugin.
* @uses apply_filters() Calls 'option_$optionname' with the option name value.
*
* @param string $setting Name of option to retrieve. Should already be SQL-escaped
* @return mixed Value set for the option.
*/
function get_option( $setting, $default = false ) {
global $wpdb;
// Allow plugins to short-circuit options.
$pre = apply_filters( 'pre_option_' . $setting, false );
if ( false !== $pre )
return $pre;
// prevent non-existent options from triggering multiple queries
$notoptions = wp_cache_get( 'notoptions', 'options' );
if ( isset( $notoptions[$setting] ) )
return $default;
$alloptions = wp_load_alloptions();
if ( isset( $alloptions[$setting] ) ) {
$value = $alloptions[$setting];
} else {
$value = wp_cache_get( $setting, 'options' );
if ( false === $value ) {
if ( defined( 'WP_INSTALLING' ) )
$suppress = $wpdb->suppress_errors();
// expected_slashed ($setting)
$row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1" );
if ( defined( 'WP_INSTALLING' ) )
$wpdb->suppress_errors($suppress);
if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
$value = $row->option_value;
wp_cache_add( $setting, $value, 'options' );
} else { // option does not exist, so we must cache its non-existence
$notoptions[$setting] = true;
wp_cache_set( 'notoptions', $notoptions, 'options' );
return $default;
}
}
}
// If home is not set use siteurl.
if ( 'home' == $setting && '' == $value )
return get_option( 'siteurl' );
if ( in_array( $setting, array('siteurl', 'home', 'category_base', 'tag_base') ) )
$value = untrailingslashit( $value );
return apply_filters( 'option_' . $setting, maybe_unserialize( $value ) );
}
/**
* Protect WordPress special option from being modified.
*
* Will die if $option is in protected list. Protected options are 'alloptions'
* and 'notoptions' options.
*
* @since 2.2.0
* @package WordPress
* @subpackage Option
*
* @param string $option Option name.
*/
function wp_protect_special_option( $option ) {
$protected = array( 'alloptions', 'notoptions' );
if ( in_array( $option, $protected ) )
die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
}
/**
* Print option value after sanitizing for forms.
*
* @uses attr Sanitizes value.
* @since 1.5.0
* @package WordPress
* @subpackage Option
*
* @param string $option Option name.
*/
function form_option( $option ) {
echo esc_attr(get_option( $option ) );
}
/**
* Retrieve all autoload options or all options, if no autoloaded ones exist.
*
* This is different from wp_load_alloptions() in that this function does not
* cache its results and will retrieve all options from the database every time
*
* it is called.
*
* @since 1.0.0
* @package WordPress
* @subpackage Option
* @uses apply_filters() Calls 'pre_option_$optionname' hook with option value as parameter.
* @uses apply_filters() Calls 'all_options' on options list.
*
* @return array List of all options.
*/
function get_alloptions() {
global $wpdb;
$show = $wpdb->hide_errors();
if ( !$options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
$options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
$wpdb->show_errors($show);
foreach ( (array) $options as $option ) {
// "When trying to design a foolproof system,
// never underestimate the ingenuity of the fools :)" -- Dougal
if ( in_array( $option->option_name, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) )
$option->option_value = untrailingslashit( $option->option_value );
$value = maybe_unserialize( $option->option_value );
$all_options->{$option->option_name} = apply_filters( 'pre_option_' . $option->option_name, $value );
}
return apply_filters( 'all_options', $all_options );
}
/**
* Loads and caches all autoloaded options, if available or all options.
*
* This is different from get_alloptions(), in that this function will cache the
* options and will return the cached options when called again.
*
* @since 2.2.0
* @package WordPress
* @subpackage Option
*
* @return array List all options.
*/
function wp_load_alloptions() {
global $wpdb;
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( !$alloptions ) {
$suppress = $wpdb->suppress_errors();
if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
$wpdb->suppress_errors($suppress);
$alloptions = array();
foreach ( (array) $alloptions_db as $o )
$alloptions[$o->option_name] = $o->option_value;
wp_cache_add( 'alloptions', $alloptions, 'options' );
}
return $alloptions;
}
/**
* Update the value of an option that was already added.
*
* You do not need to serialize values, if the value needs to be serialize, then
* it will be serialized before it is inserted into the database. Remember,
* resources can not be serialized or added as an option.
*
* If the option does not exist, then the option will be added with the option
* value, but you will not be able to set whether it is autoloaded. If you want
* to set whether an option autoloaded, then you need to use the add_option().
*
* Before the option is updated, then the filter named
* 'pre_update_option_$option_name', with the $option_name as the $option_name
* parameter value, will be called. The hook should accept two parameters, the
* first is the new value and the second is the old value. Whatever is
* returned will be used as the new value.
*
* After the value has been updated the action named 'update_option_$option_name'
* will be called. This action receives two parameters the first being the old
* value and the second the new value.
*
* @since 1.0.0
* @package WordPress
* @subpackage Option
*
* @param string $option_name Option name. Expected to not be SQL-escaped
* @param mixed $newvalue Option value.
* @return bool False if value was not updated and true if value was updated.
*/
function update_option( $option_name, $newvalue ) {
global $wpdb;
wp_protect_special_option( $option_name );
$safe_option_name = esc_sql( $option_name );
$newvalue = sanitize_option( $option_name, $newvalue );
$oldvalue = get_option( $safe_option_name );
$newvalue = apply_filters( 'pre_update_option_' . $option_name, $newvalue, $oldvalue );
// If the new and old values are the same, no need to update.
if ( $newvalue === $oldvalue )
return false;
if ( false === $oldvalue ) {
add_option( $option_name, $newvalue );
return true;
}
$notoptions = wp_cache_get( 'notoptions', 'options' );
if ( is_array( $notoptions ) && isset( $notoptions[$option_name] ) ) {
unset( $notoptions[$option_name] );
wp_cache_set( 'notoptions', $notoptions, 'options' );
}
$_newvalue = $newvalue;
$newvalue = maybe_serialize( $newvalue );
do_action( 'update_option', $option_name, $oldvalue, $newvalue );
$alloptions = wp_load_alloptions();
if ( isset( $alloptions[$option_name] ) ) {
$alloptions[$option_name] = $newvalue;
wp_cache_set( 'alloptions', $alloptions, 'options' );
} else {
wp_cache_set( $option_name, $newvalue, 'options' );
}
$wpdb->update($wpdb->options, array('option_value' => $newvalue), array('option_name' => $option_name) );
if ( $wpdb->rows_affected == 1 ) {
do_action( "update_option_{$option_name}", $oldvalue, $_newvalue );
do_action( 'updated_option', $option_name, $oldvalue, $_newvalue );
return true;
}
return false;
}
/**
* Add a new option.
*
* You do not need to serialize values, if the value needs to be serialize, then
* it will be serialized before it is inserted into the database. Remember,
* resources can not be serialized or added as an option.
*
* You can create options without values and then add values later. Does not
* check whether the option has already been added, but does check that you
* aren't adding a protected WordPress option. Care should be taken to not name
* options, the same as the ones which are protected and to not add options
* that were already added.
*
* The filter named 'add_option_$optionname', with the $optionname being
* replaced with the option's name, will be called. The hook should accept two
* parameters, the first is the option name, and the second is the value.
*
* @package WordPress
* @subpackage Option
* @since 1.0.0
* @link http://alex.vort-x.net/blog/ Thanks Alex Stapleton
*
* @param string $name Option name to add. Expects to NOT be SQL escaped.
* @param mixed $value Optional. Option value, can be anything.
* @param mixed $deprecated Optional. Description. Not used anymore.
* @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
* @return null returns when finished.
*/
function add_option( $name, $value = '', $deprecated = '', $autoload = 'yes' ) {
global $wpdb;
wp_protect_special_option( $name );
$safe_name = esc_sql( $name );
$value = sanitize_option( $name, $value );
// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
$notoptions = wp_cache_get( 'notoptions', 'options' );
if ( !is_array( $notoptions ) || !isset( $notoptions[$name] ) )
if ( false !== get_option( $safe_name ) )
return;
$value = maybe_serialize( $value );
$autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
do_action( 'add_option', $name, $value );
if ( 'yes' == $autoload ) {
$alloptions = wp_load_alloptions();
$alloptions[$name] = $value;
wp_cache_set( 'alloptions', $alloptions, 'options' );
} else {
wp_cache_set( $name, $value, 'options' );
}
// This option exists now
$notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
if ( is_array( $notoptions ) && isset( $notoptions[$name] ) ) {
unset( $notoptions[$name] );
wp_cache_set( 'notoptions', $notoptions, 'options' );
}
$wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $name, $value, $autoload ) );
do_action( "add_option_{$name}", $name, $value );
do_action( 'added_option', $name, $value );
return;
}
/**
* Removes option by name and prevents removal of protected WordPress options.
*
* @package WordPress
* @subpackage Option
* @since 1.2.0
*
* @param string $name Option name to remove.
* @return bool True, if succeed. False, if failure.
*/
function delete_option( $name ) {
global $wpdb;
wp_protect_special_option( $name );
// Get the ID, if no ID then return
// expected_slashed ($name)
$option = $wpdb->get_row( "SELECT autoload FROM $wpdb->options WHERE option_name = '$name'" );
if ( is_null($option) )
return false;
do_action( 'delete_option', $name );
// expected_slashed ($name)
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$name'" );
if ( 'yes' == $option->autoload ) {
$alloptions = wp_load_alloptions();
if ( isset( $alloptions[$name] ) ) {
unset( $alloptions[$name] );
wp_cache_set( 'alloptions', $alloptions, 'options' );
}
} else {
wp_cache_delete( $name, 'options' );
}
do_action( 'deleted_option', $name );
return true;
}
/**
* Delete a transient
*
* @since 2.8.0
* @package WordPress
* @subpackage Transient
*
* @param string $transient Transient name. Expected to not be SQL-escaped
* @return bool true if successful, false otherwise
*/
function delete_transient($transient) {
global $_wp_using_ext_object_cache, $wpdb;
if ( $_wp_using_ext_object_cache ) {
return wp_cache_delete($transient, 'transient');
} else {
$transient = '_transient_' . esc_sql($transient);
return delete_option($transient);
}
}
/**
* Get the value of a transient
*
* If the transient does not exist or does not have a value, then the return value
* will be false.
*
* @since 2.8.0
* @package WordPress
* @subpackage Transient
*
* @param string $transient Transient name. Expected to not be SQL-escaped
* @return mixed Value of transient
*/
function get_transient($transient) {
global $_wp_using_ext_object_cache, $wpdb;
$pre = apply_filters( 'pre_transient_' . $transient, false );
if ( false !== $pre )
return $pre;
if ( $_wp_using_ext_object_cache ) {
$value = wp_cache_get($transient, 'transient');
} else {
$transient_option = '_transient_' . esc_sql($transient);
// If option is not in alloptions, it is not autoloaded and thus has a timeout
$alloptions = wp_load_alloptions();
if ( !isset( $alloptions[$transient_option] ) ) {
$transient_timeout = '_transient_timeout_' . esc_sql($transient);
if ( get_option($transient_timeout) < time() ) {
delete_option($transient_option);
delete_option($transient_timeout);
return false;
}
}
$value = get_option($transient_option);
}
return apply_filters('transient_' . $transient, $value);
}
/**
* Set/update the value of a transient
*
* You do not need to serialize values, if the value needs to be serialize, then
* it will be serialized before it is set.
*
* @since 2.8.0
* @package WordPress
* @subpackage Transient
*
* @param string $transient Transient name. Expected to not be SQL-escaped
* @param mixed $value Transient value.
* @param int $expiration Time until expiration in seconds, default 0
* @return bool False if value was not set and true if value was set.
*/
function set_transient($transient, $value, $expiration = 0) {
global $_wp_using_ext_object_cache, $wpdb;
if ( $_wp_using_ext_object_cache ) {
return wp_cache_set($transient, $value, 'transient', $expiration);
} else {
$transient_timeout = '_transient_timeout_' . $transient;
$transient = '_transient_' . $transient;
$safe_transient = esc_sql($transient);
if ( false === get_option( $safe_transient ) ) {
$autoload = 'yes';
if ( 0 != $expiration ) {
$autoload = 'no';
add_option($transient_timeout, time() + $expiration, '', 'no');
}
return add_option($transient, $value, '', $autoload);
} else {
if ( 0 != $expiration )
update_option($transient_timeout, time() + $expiration);
return update_option($transient, $value);
}
}
}
/**
* Saves and restores user interface settings stored in a cookie.
*
* Checks if the current user-settings cookie is updated and stores it. When no
* cookie exists (different browser used), adds the last saved cookie restoring
* the settings.
*
* @package WordPress
* @subpackage Option
* @since 2.7.0
*/
function wp_user_settings() {
if ( ! is_admin() )
return;
if ( defined('DOING_AJAX') )
return;
if ( ! $user = wp_get_current_user() )
return;
$settings = get_user_option( 'user-settings', $user->ID, false );
if ( isset( $_COOKIE['wp-settings-' . $user->ID] ) ) {
$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) {
if ( $cookie == $settings )
return;
$last_time = (int) get_user_option( 'user-settings-time', $user->ID, false );
$saved = isset( $_COOKIE['wp-settings-time-' . $user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user->ID] ) : 0;
if ( $saved > $last_time ) {
update_user_option( $user->ID, 'user-settings', $cookie, false );
update_user_option( $user->ID, 'user-settings-time', time() - 5, false );
return;
}
}
}
setcookie( 'wp-settings-' . $user->ID, $settings, time() + 31536000, SITECOOKIEPATH );
setcookie( 'wp-settings-time-' . $user->ID, time(), time() + 31536000, SITECOOKIEPATH );
$_COOKIE['wp-settings-' . $user->ID] = $settings;
}
/**
* Retrieve user interface setting value based on setting name.
*
* @package WordPress
* @subpackage Option
* @since 2.7.0
*
* @param string $name The name of the setting.
* @param string $default Optional default value to return when $name is not set.
* @return mixed the last saved user setting or the default value/false if it doesn't exist.
*/
function get_user_setting( $name, $default = false ) {
$all = get_all_user_settings();
return isset($all[$name]) ? $all[$name] : $default;
}
/**
* Add or update user interface setting.
*
* Both $name and $value can contain only ASCII letters, numbers and underscores.
* This function has to be used before any output has started as it calls setcookie().
*
* @package WordPress
* @subpackage Option
* @since 2.8.0
*
* @param string $name The name of the setting.
* @param string $value The value for the setting.
* @return bool true if set successfully/false if not.
*/
function set_user_setting( $name, $value ) {
if ( headers_sent() )
return false;
$all = get_all_user_settings();
$name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
if ( empty($name) )
return false;
$all[$name] = $value;
return wp_set_all_user_settings($all);
}
/**
* Delete user interface settings.
*
* Deleting settings would reset them to the defaults.
* This function has to be used before any output has started as it calls setcookie().
*
* @package WordPress
* @subpackage Option
* @since 2.7.0
*
* @param mixed $names The name or array of names of the setting to be deleted.
* @return bool true if deleted successfully/false if not.
*/
function delete_user_setting( $names ) {
if ( headers_sent() )
return false;
$all = get_all_user_settings();
$names = (array) $names;
foreach ( $names as $name ) {
if ( isset($all[$name]) ) {
unset($all[$name]);
$deleted = true;
}
}
if ( isset($deleted) )
return wp_set_all_user_settings($all);
return false;
}
/**
* Retrieve all user interface settings.
*
* @package WordPress
* @subpackage Option
* @since 2.7.0
*
* @return array the last saved user settings or empty array.
*/
function get_all_user_settings() {
global $_updated_user_settings;
if ( ! $user = wp_get_current_user() )
return array();
if ( isset($_updated_user_settings) && is_array($_updated_user_settings) )
return $_updated_user_settings;
$all = array();
if ( isset($_COOKIE['wp-settings-' . $user->ID]) ) {
$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
if ( $cookie && strpos($cookie, '=') ) // the '=' cannot be 1st char
parse_str($cookie, $all);
} else {
$option = get_user_option('user-settings', $user->ID);
if ( $option && is_string($option) )
parse_str( $option, $all );
}
return $all;
}
/**
* Private. Set all user interface settings.
*
* @package WordPress
* @subpackage Option
* @since 2.8.0
*
*/
function wp_set_all_user_settings($all) {
global $_updated_user_settings;
if ( ! $user = wp_get_current_user() )
return false;
$_updated_user_settings = $all;
$settings = '';
foreach ( $all as $k => $v ) {
$v = preg_replace( '/[^A-Za-z0-9_]+/', '', $v );
$settings .= $k . '=' . $v . '&';
}
$settings = rtrim($settings, '&');
update_user_option( $user->ID, 'user-settings', $settings, false );
update_user_option( $user->ID, 'user-settings-time', time(), false );
return true;
}
/**
* Delete the user settings of the current user.
*
* @package WordPress
* @subpackage Option
* @since 2.7.0
*/
function delete_all_user_settings() {
if ( ! $user = wp_get_current_user() )
return;
update_user_option( $user->ID, 'user-settings', '', false );
setcookie('wp-settings-' . $user->ID, ' ', time() - 31536000, SITECOOKIEPATH);
}
/**
* Serialize data, if needed.
*
* @since 2.0.5
*
* @param mixed $data Data that might be serialized.
* @return mixed A scalar data
*/
function maybe_serialize( $data ) {
if ( is_array( $data ) || is_object( $data ) )
return serialize( $data );
if ( is_serialized( $data ) )
return serialize( $data );
return $data;
}
/**
* Retrieve post title from XMLRPC XML.
*
* If the title element is not part of the XML, then the default post title from
* the $post_default_title will be used instead.
*
* @package WordPress
* @subpackage XMLRPC
* @since 0.71
*
* @global string $post_default_title Default XMLRPC post title.
*
* @param string $content XMLRPC XML Request content
* @return string Post title
*/
function xmlrpc_getposttitle( $content ) {
global $post_default_title;
if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) {
$post_title = $matchtitle[1];
} else {
$post_title = $post_default_title;
}
return $post_title;
}
/**
* Retrieve the post category or categories from XMLRPC XML.
*