forked from johnbintz/mockpress
/
mockpress.php
1660 lines (1450 loc) · 42.5 KB
/
mockpress.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
/**
* Simulate enough of WordPress to test plugins and themes using PHPUnit, SimpleTest, or any other PHP unit testing framework.
* @author John Bintz
*/
$wp_test_expectations = array();
require_once('includes/cache.php');
require_once('includes/media.php');
require_once('includes/posts.php');
require_once('includes/filtering.php');
require_once('includes/comments.php');
/**
* Reset the WordPress test expectations.
*/
function _reset_wp() {
global $wp_test_expectations;
$wp_test_expectations = array(
'options' => array(),
'categories' => array(),
'post_categories' => array(),
'get_posts' => array(),
'admin_pages' => array(),
'pages' => array(),
'posts' => array(),
'comments' => array(),
'actions' => array(),
'filters' => array(),
'post_meta' => array(),
'themes' => array(),
'plugin_domains' => array(),
'enqueued_scripts' => array(),
'enqueued_styles' => array(),
'all_tags' => array(),
'post_tags' => array(),
'sidebar_widgets' => array(),
'widget_controls' => array(),
'nonce' => array(),
'wp_widgets' => array(),
'current' => array(
'is_feed' => false,
'is_home' => false
),
'plugin_data' => array(),
'theme' => array(
'posts' => array()
),
'bloginfo' => array(),
'user_capabilities' => array(),
'children' => array(),
'current_user' => null,
'users' => array(),
'user_meta' => array(),
'image_downsize' => array(),
'sites' => array(),
);
wp_cache_init();
}
/*** WordPress Test Doubles ***/
function wp_clone($object) {
return version_compare(phpversion(). '5.0', '>=') ? clone($object) : $object;
}
/** Options **/
/**
* Get an option from the WP Options table.
* @param string $key The option to retrieve.
* @return string|boolean The value of the option, or false if the key doesn't exist.
*/
function get_option($key) {
global $wp_test_expectations;
if (is_string($key)) {
if (isset($wp_test_expectations['options'][$key])) {
return maybe_unserialize($wp_test_expectations['options'][$key]);
} else {
return false;
}
} else {
return false;
}
}
/**
* Store an option in the WP Options table.
* @param string $key The option to store.
* @param string $value The value to store.
* @return boolean True if the option was updated, false otherwise.
*/
function update_option($key, $value) {
global $wp_test_expectations;
$value = maybe_serialize($value);
if (is_string($key)) {
if (!isset($wp_test_expectations['options'][$key])) {
$wp_test_expectations['options'][$key] = $value;
return true;
} else {
if ($wp_test_expectations['options'][$key] == $value) {
return false;
} else {
$wp_test_expectations['options'][$key] = $value;
return true;
}
}
} else {
return false;
}
}
/**
* Delete an option from the WP Options table.
* @param string $key The option to delete.
* @return boolean True if the option was deleted.
*/
function delete_option($key) {
global $wp_test_expectations;
if (is_string($key)) {
if (isset($wp_test_expectations['options'][$key])) {
unset($wp_test_expectations['options'][$key]);
return true;
} else {
return false;
}
} else {
return false;
}
}
function get_alloptions() {
global $wp_test_expectations;
return $wp_test_expectations['options'];
}
function wp_load_alloptions() {
return get_alloptions();
}
/** String Utility Functions **/
/**
* Remove a trailing slash from a string if it exists.
* @param string $string The string to check for trailing slashes.
* @return string The string with a trailing slash removed, if necessary.
*/
function untrailingslashit($string) {
return preg_replace('#/$#', '', $string);
}
/**
* Add a trailing slash to a string if it does not exist.
* @param string $string The string to which a trailing slash should be added.
* @return string The string with a trailing slash added, if necessary.
*/
function trailingslashit($string) {
return preg_replace('#([^/])$#', '\1/', $string);
}
function user_trailingslashit($string, $type_of_url = '') {
$which = 'untrailingslashit';
if ($permalink_structure = get_option('permalink_structure')) {
if (preg_match('#/$#', $permalink_structure) > 0) {
$which = 'trailingslashit';
}
}
return call_user_func($which, $string);
}
/**
* Get GMT string from date string.
* Currently does nothing.
* @param string $date_string The date string to convert.
* @return string The converted date string in GMT.
*/
function get_gmt_from_date($date_string) {
return $date_string;
}
/**
* Return a string that's been internationalized.
* @param string $string The string to check for i18n.
* @param string $namespace The namespace to check.
* @return string The i18n string.
*/
function __($string, $namespace = 'default') {
return $string;
}
/**
* Echo an internationalized string.
* @param string $string The string to check for i18n.
* @param string $namespace The namespace to check.
*/
function _e($string, $namespace = 'default') {
echo __($string, $namespace);
}
/**
* Return a different string if the number of items is 1.
* @deprecated Is now _n in WordPress 2.8.
* @param string $single The string to return if only one item.
* @param string $plural The string to return if not one item.
* @param string $number The number of items.
* @param string $domain The text domain.
* @return string The correct string.
*/
function __ngettext($single, $plural, $number, $domain) {
return _n($single, $plural, $number, $domain);
}
/**
* Return a different string if the number of items is 1.
* @param $single The string to return if only one item.
* @param $plural The string to return if not one item.
* @param $number The number of items.
* @param $domain The text domain.
* @return string The correct string.
*/
function _n($single, $plural, $number, $domain) {
return ($number == 1) ? $single : $plural;
}
/**
* True if the data provided was created by serialize()
* @param mixed $data The data to check.
* @return boolean True if the data was created by serialize.
*/
function is_serialized($data) {
return (@unserialize($data) !== false);
}
/**
* Try to serialize the data and return the serialized string.
* @param mixed $data The data to try to serialize.
* @return mixed The data, possibly serialized.
*/
function maybe_serialize($data) {
if (is_array($data) || is_object($data) || is_serialized($data)) {
return serialize($data);
} else {
return $data;
}
}
/**
* Try to unserialize the data and return the serialized data.
* @param mixed $data The data to try to unserialize.
* @return mixed The data, possibly unserialized.
*/
function maybe_unserialize($data) {
if (is_serialized($data)) {
if (($gm = unserialize($data)) !== false) { return $gm; }
}
return $data;
}
/** Categories **/
/**
* Add a category.
* @param int $id The category ID.
* @param object $object The category object.
* @throws Error if $id is not numeric or $category is not an object.
*/
function add_category($id, $object) {
global $wp_test_expectations;
if (is_object($object)) {
if (is_numeric($id)) {
$object->cat_ID = $object->term_id = (int)$id;
if (!isset($object->parent)) { $object->parent = 0; }
$wp_test_expectations['categories'][$id] = (object)_make_cat_compat((array)$object);
} else {
trigger_error("ID must be numeric");
}
} else {
trigger_error("Category provided must be an object");
}
}
/**
* Insert a new category.
* @param array $catarr The new category to insert.
* @return int The ID of the new category, or 0 if there was an error.
*/
function wp_insert_category($catarr) {
global $wp_test_expectations;
if (is_object($catarr)) { $catarr = (array)$catarr; }
if (is_array($catarr)) {
if (empty($catarr['cat_ID'])) {
$max_id = 1;
foreach ($wp_test_expectations['categories'] as $category) {
if (isset($category->cat_ID)) {
$max_id = max($max_id, $category->cat_ID + 1);
}
}
add_category($max_id, (object)_make_cat_compat($catarr));
return $max_id;
}
}
return 0;
}
/**
* Make category compatible with older WordPress code.
* @param array $catarr The category to modify.
* @return array The modified category.
*/
function _make_cat_compat($catarr) {
foreach (array(
'cat_name' => 'name',
'category_description' => 'description',
'category_nicename' => 'slug',
'category_parent' => 'parent'
) as $old => $new) {
if (isset($catarr[$old])) {
$catarr[$new] = $catarr[$old];
}
}
return $catarr;
}
/**
* Retrieve a category by slug.
* @param string $slug The slug of the category to retrieve.
* @return object|false The retrieved category, or false if not found.
*/
function get_category_by_slug($slug) {
global $wp_test_expectations;
foreach ($wp_test_expectations['categories'] as $id => $info) {
if ($info->slug == $slug) {
return $info;
}
}
return false;
}
/**
* Get a category.
* @param int $id The category ID to retrieve.
* @return object|WP_Error The category object, or a WP_Error object on failure.
*/
function get_category($id) {
global $wp_test_expectations;
if (!isset($wp_test_expectations['categories'])) {
return new WP_Error();
} else {
if (isset($wp_test_expectations['categories'][$id])) {
return $wp_test_expectations['categories'][$id];
}
}
}
function wp_delete_category($id) {
global $wp_test_expectations;
if (isset($wp_test_expectations['categories'][$id])) {
$ok = true;
if (($value = get_option('default_category')) !== false) {
$ok = ($value != $id);
}
if ($ok) {
unset($wp_test_expectations['categories'][$id]);
return true;
}
}
return false;
}
/**
* Get all category IDs.
* @return array All valid category IDs.
*/
function get_all_category_ids() {
global $wp_test_expectations;
return array_keys($wp_test_expectations['categories']);
}
/**
* Get a category's name.
* @param int $id The id of the category.
* @return string|null The name, or null if the category is not found.
*/
function get_cat_name($id) {
global $wp_test_expectations;
if (isset($wp_test_expectations['categories'][$id])) {
return $wp_test_expectations['categories'][$id]->name;
} else {
return null;
}
}
/**
* Set a post's categories.
* @param int $post_id The post to modify.
* @param array $categories The categories to set for this post.
*/
function wp_set_post_categories($post_id, $categories) {
global $wp_test_expectations;
if (!is_array($categories)) { $categories = array($categories); }
$wp_test_expectations['post_categories'][$post_id] = $categories;
}
/**
* Get a post's categories.
* @param int $post_id The post to query.
* @return array The categories for this post.
*/
function wp_get_post_categories($post_id = null) {
global $wp_test_expectations, $post;
if (is_null($post_id)) {
$post_id = $post->ID;
}
if (!isset($wp_test_expectations['post_categories'][$post_id])) {
return array();
} else {
return $wp_test_expectations['post_categories'][$post_id];
}
}
/**
* Get the permalink to a category.
* For MockPress's purposes, the link will look like "/category/${category_id}"
* @param int $category_id The category ID.
* @return string|WP_Error The URI or a WP_Error object upon failure.
*/
function get_category_link($category_id) {
global $wp_test_expectations;
if (isset($wp_test_expectations['categories'][$category_id])) {
return "/category/${category_id}";
} else {
return new WP_Error();
}
}
/** Tags **/
/**
* Get a post's tags.
* @param int $post_id The post to query.
* @return array The tags for the post.
*/
function wp_get_post_tags($post_id) {
global $wp_test_expectations;
if (!isset($wp_test_expectations['post_tags'][$post_id])) {
return array();
} else {
return $wp_test_expectations['post_tags'][$post_id];
}
}
/**
* Set a post's tags.
* @param int $post_id The post to modify.
* @param array $tags The tags to set for this post. Note that these should be text strings and not objects. E_USER_WARNING will be raised if you don't pass in a string.
* @raises E_USER_WARNING if an object other than a string exists in the $tags array.
*/
function wp_set_post_tags($post_id, $tags) {
global $wp_test_expectations;
$tags = (array)$tags;
foreach ($tags as $tag) {
if (!is_string($tag)) { trigger_error("All tags sent to wp_set_post_tags() need to be strings."); }
}
$wp_test_expectations['post_tags'][$post_id] = array();
foreach ($tags as $tag) {
$wp_test_expectations['post_tags'][$post_id][] = (object)array(
'name' => $tag, 'slug' => $tag
);
}
}
/**
* Set the wp_get_post_tags response for the requested post.
* You can't pass in objects for wp_set_post_tags, so if you need more information beyond name & slug, use this.
* No checking is done to ensure you're passing in sane data.
* @param int $post_id The post to modify.
* @param array $tags The tags to set.
*/
function _set_wp_post_tag_objects($post_id, $tags) {
global $wp_test_expectations;
$wp_test_expectations['post_tags'][$post_id] = $tags;
}
/**
* Set the output for get_tags()
* @param array $tags The output for get_tags()
*/
function _set_all_tags($tags) {
global $wp_test_expectations;
$wp_test_expectations['all_tags'] = $tags;
}
/**
* Get all tags within WordPress.
* @return array All the tags within WordPress.
*/
function get_tags() {
global $wp_test_expectations;
return $wp_test_expectations['all_tags'];
}
/** Pages **/
function get_pages() {
global $wp_test_expectations;
$pages = array();
if (isset($wp_test_expectations['posts'])) {
if (is_array($wp_test_expectations['posts'])) {
foreach ($wp_test_expectations['posts'] as $post) {
if (isset($post->post_type)) {
if ($post->post_type == 'page') { $pages[] = $post; }
}
}
}
}
return $pages;
}
/** Core **/
/**
* Attach a callback to an action hook.
* @param string $name The hook to attach to.
* @param callback $callback The callback to execute.
*/
function add_action($name, $callback) {
global $wp_test_expectations;
$wp_test_expectations['actions'][$name] = $callback;
}
function do_action($name) {
}
function remove_action($name, $callback) {
global $wp_test_expectations;
unset($wp_test_expectations['actions'][$name]);
}
function add_shortcode($code, $callback) {}
/**
* Attach a callback to a filter hook.
* @param string $name The hook to attach to.
* @param callback $callback The callback to execute.
*/
function add_filter($name, $callback, $priority = 10, $parameters = 2) {
global $wp_test_expectations;
if (!isset($wp_test_expectations['filters'][$name])) {
$wp_test_expectations['filters'][$name] = array();
}
if (!isset($wp_test_expectations['filters'][$name][$priority])) {
$wp_test_expectations['filters'][$name][$priority] = array();
}
$wp_test_expectations['filters'][$name][$priority] = compact('callback', 'parameter_count');
ksort($wp_test_expectations['filters'][$name]);
}
/**
* Run attached filter hooks.
* @param string $name The hook to call.
* @param mixed,... $arguments The arguments to the hooks.
* @return mixed The return value.
*/
function apply_filters() {
global $wp_test_expectations;
$parameters = func_get_args();
$name = array_shift($parameters);
if (isset($wp_test_expectations['filters'][$name])) {
// override the normal filter processing
$override = false;
if (count($wp_test_expectations['filters'][$name]) == 2) {
if ($wp_test_expectations['filters'][$name][0] === true) {
$parameters = $wp_test_expectations['filters'][$name][1];
$override = true;
}
}
if (!$override) {
foreach ($wp_test_expectations['filters'][$name] as $priority => $callbacks) {
foreach ($callbacks as $info) {
extract($info);
if (count($parameters) == $parameter_count) {
$parameters = call_user_func_array($callback, $paremeters);
} else {
throw new Exception("Got " . count($parameters) . " parameters, expected ${parameter_count} for filter ${name}, callback " . print_r($callback, true));
}
}
}
}
}
return reset($parameters);
}
/**
* Set the expected result for a particular filter.
* @param string $name The name of the filter
* @param array $result The result of the filter.
*/
function _set_filter_expectation($name, $result) {
global $wp_test_expectations;
$result = (array)$result;
$wp_test_expectations['filters'][$name] = array(true, $result);
}
/**
* Ensure that input has default values injected into it.
* @param string|array $input The input values.
* @param array $defaults The default values.
* @return array The default values with the provided input merged overtop.
*/
function wp_parse_args($input, $defaults) {
if (is_string($input)) {
parse_str($input, $r);
$input = $r;
}
if (is_array($input)) {
return array_merge($defaults, $input);
} else {
return $defaults;
}
}
/** Admin **/
/**
* Add a page to the Options menu.
*/
function add_options_page($page_title, $menu_title, $access_level, $file, $function = "") {
add_submenu_page('options-general.php', $page_title, $menu_title, $access_level, $file, $function);
}
/**
* Add a page to the main menu.
*/
function add_menu_page($page_title, $menu_title, $access_level, $file, $function, $icon) {
global $wp_test_expectations;
$parent = "";
$wp_test_expectations['admin_pages'][] = compact('parent', 'page_title', 'menu_title', 'access_level', 'file', 'function', 'icon');
return "hook name";
}
/**
* Add a page below a main page.
*/
function add_submenu_page($parent, $page_title, $menu_title, $access_level, $file, $function = "") {
global $wp_test_expectations;
$wp_test_expectations['admin_pages'][] = compact('parent', 'page_title', 'menu_title', 'access_level', 'file', 'function');
return "hook name";
}
/**
* Set whether or not a user can use the rich text editor.
* @param boolean $can True if the user can use the editor.
*/
function _set_user_can_richedit($can) {
global $wp_test_expectations;
$wp_test_expectations['user_can_richedit'] = $can;
}
/**
* Find out if the user can use the rich text editor.
* @return boolean True if the user can use the editor.
*/
function user_can_richedit() {
global $wp_test_expectations;
return $wp_test_expectations['user_can_richedit'];
}
/**
* Embed the rich text editor.
*/
function the_editor($content) {
echo $content;
}
/** Plugin **/
/**
* Get the basename of a file relative to the plugins directory.
*/
function plugin_basename($file) { return $file; }
/**
* Load the translation files for the current plugin.
* @param string $domain The text domain to load files for.
* @param string $path The path to the translation files.
*/
function load_plugin_textdomain($domain, $path) {
global $wp_test_expectations;
$wp_test_expectations['plugin_domains'][] = "${domain}-${path}";
}
/**
* Enqueue a script library to be loaded.
* @param string $script The script library to load.
*/
function wp_enqueue_script($script) {
global $wp_test_expectations;
$wp_test_expectations['enqueued_scripts'][$script] = true;
}
/**
* Determine if a script library was enqueued.
* @param string $script The script library to check.
* @return boolean True if the library was enqueued to be loaded.
*/
function _did_wp_enqueue_script($script) {
global $wp_test_expectations;
return isset($wp_test_expectations['enqueued_scripts'][$script]);
}
/**
* Enqueue a stylesheet to be loaded
* @param string $style Name of the stylesheet
* @param string|boolean $src Path to the stylesheet from the root directory of WordPress.
*/
function wp_enqueue_style($style) {
global $wp_test_expectations;
$wp_test_expectations['enqueued_styles'][$style] = true;
}
/** Nonce **/
/**
* Set up a specific valid nonce.
* @param string $name The name of the nonce.
* @param string $value The provided nonce value.
*/
function _set_valid_nonce($name, $value) {
global $wp_test_expectations;
$wp_test_expectations['nonce'][$name] = $value;
}
/**
* Get a nonce value.
* @param string $name The name of the nonce.
* @return string|boolean The nonce value, or false if no nonce found.
*/
function _get_nonce($name) {
global $wp_test_expectations;
if (isset($wp_test_expectations['nonce'][$name])) {
return $wp_test_expectations['nonce'][$name];
} else {
return false;
}
}
/**
* Create a random nonce.
* @param string $name The name of the nonce.
* @return string The nonce value.
*/
function wp_create_nonce($name) {
global $wp_test_expectations;
if (!isset($wp_test_expectations['nonce'][$name])) {
$wp_test_expectations['nonce'][$name] = md5(rand());
}
return $wp_test_expectations['nonce'][$name];
}
/**
* Verify that the provided nonce value matches.
* @param string $value The value to check.
* @param string $name The name of the nonce.
* @return boolean True if the nonce matches the provided value.
*/
function wp_verify_nonce($value, $name) {
global $wp_test_expectations;
if (isset($wp_test_expectations['nonce'][$name])) {
return $wp_test_expectations['nonce'][$name] == $value;
}
return false;
}
/**
* Create an <input /> field ready for a nonce value.
* @param string $name The name of both the nonce and the input field.
*/
function wp_nonce_field($name) {
global $wp_test_expectations;
echo "<input type=\"hidden\" name=\"${name}\" value=\"" . wp_create_nonce($name) . "\" />";
}
/** Theme **/
/**
* Get information on the specified theme.
* @param string $name The name of the theme.
* @return array|null The theme information as an array, or null if not found.
*/
function get_theme($name) {
global $wp_test_expectations;
if (isset($wp_test_expectations['themes'][$name])) {
return $wp_test_expectations['themes'][$name];
} else {
return null;
}
}
/**
* Get the name of the current theme.
* @return string The name of the current theme.
*/
function get_current_theme() {
global $wp_test_expectations;
return $wp_test_expectations['current_theme'];
}
/**
* Set the name of the current theme.
* @param string $theme The name of the current theme.
*/
function _set_current_theme($theme) {
global $wp_test_expectations;
$wp_test_expectations['current_theme'] = $theme;
}
/** Query **/
/**
* Set up the query string.
* @param string $string The query string.
*/
function _setup_query($string) {
$_SERVER['QUERY_STRING'] = $string;
}
/**
* Add an argument to the query string.
* @param string $parameter The parameter to add.
* @param string $value The value to place in the URL.
* @return string The modified query string.
*/
function add_query_arg($parameter, $value) {
$separator = (strpos($_SERVER['QUERY_STRING'], "?") === false) ? "?" : "&";
return $_SERVER['QUERY_STRING'] . $separator . $parameter . "=" . urlencode($value);
}
/**
* Get the search query from the query string.
* @return string The search query, or blank if not found.
*/
function get_search_query() {
$parts = explode("&", preg_replace("#^.*\?#", "", $_SERVER['QUERY_STRING']));
foreach ($parts as $part) {
list($param, $value) = explode("=", $part);
if ($param == "s") {
return $value;
}
}
return "";
}
/**
* Echo out the search query.
*/
function the_search_query() {
echo get_search_query();
}
/** Pre-2.8 Widgets **/
/**
* Register a widget.
* Wrapper around register_sidebar_widget.
*/
function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) {
register_sidebar_widget($id, $name, $output_callback, $options);
}
/**
* Register a widget.
*/
function register_sidebar_widget($id, $name, $output_callback = "", $options = array()) {
global $wp_test_expectations;
$wp_test_expectations['sidebar_widgets'][] = compact('id', 'name', 'output_callback', 'options');
}
/**
* Register the controls for a widget.
*/
function register_widget_control($name, $control_callback, $width = '', $height = '') {
global $wp_test_expectations;
$params = array_slice(func_get_args(), 4);
$wp_test_expectations['widget_controls'][] = compact('id', 'name', 'output_callback', 'options', 'params');
}
/** Template Tags and Theme Testing **/
/**
* Set a theme expectation.
* @param string $which The expectation to set.
* @param string $value The value to set the expectation to.
*/
function _set_theme_expectation($which, $value) {
global $wp_test_expectations;
$wp_test_expectations['theme'][$which] = $value;
}
/**
* Set the template directory.
* @param string $dir The template directory.
*/
function _set_template_directory($dir) {
global $wp_test_expectations;
$wp_test_expectations['theme']['template_directory'] = $dir;
}
/**
* Set the child theme's directory.
* @param string $dir The template directory.
*/
function _set_stylesheet_directory($dir) {
global $wp_test_expectations;
$wp_test_expectations['theme']['stylesheet_directory'] = $dir;
}
/**
* Set a 'current' expectation, such as if the current page load is an RSS feed.
* @param string $field The expectation to set.
* @param mixed $value The value of the expectation. Usually a boolean.
*/
function _set_current_option($field, $value) {
global $wp_test_expectations;
$wp_test_expectations['current'][$field] = $value;
}
/**
* True if currently in an RSS feed.
* @return boolean True if in a feed.
*/
function is_feed() {
global $wp_test_expectations;
return $wp_test_expectations['current']['is_feed'];
}
/**
* True if the current user is an admin.
* @return boolean True if an admin.
*/
function is_admin() {
global $wp_test_expectations;
return $wp_test_expectations['current']['is_admin'];
}
/**
* True if the current user is on the home page.
* @return boolean True if on home page.
*/
function is_home() {
global $wp_test_expectations;
return $wp_test_expectations['current']['is_home'];
}
/**
* True if the current post is a page.
* @return boolean True if it's a page.
*/
function is_page() {
global $post;
if (!empty($post)) {
return $post->post_type == "page";
}
return false;
}
/**
* Get plugin data (author, version, etc.)
* @param string $filepath The path to the file which contains plugin data.
*/
function get_plugin_data($filepath) {
global $wp_test_expectations;
return $wp_test_expectations['plugin_data'][$filepath];
}
/**
* Return the URL to the plugin directory that contains the provided file.
*/