@@ -48,13 +48,15 @@ class WordPressStubs {
4848 private $ metadata ;
4949 private $ calls ;
5050 private $ stubs ;
51+ private $ filters ;
5152
5253 public function __construct ( $ vfs ) {
5354 $ GLOBALS ['wp ' ] = $ this ;
5455 $ this ->vfs = $ vfs ;
5556 $ this ->addMethod ( 'add_action ' );
5657 $ this ->addMethod ( 'do_action ' );
5758 $ this ->addMethod ( 'add_filter ' );
59+ $ this ->addMethod ( 'apply_filters ' );
5860 $ this ->addMethod ( 'register_setting ' );
5961 $ this ->addMethod ( 'add_settings_section ' );
6062 $ this ->addMethod ( 'add_settings_field ' );
@@ -76,6 +78,7 @@ public function __construct( $vfs ) {
7678 $ this ->addMethod ( 'current_user_can ' );
7779 $ this ->addMethod ( 'wp_get_attachment_metadata ' );
7880 $ this ->addMethod ( 'is_admin ' );
81+ $ this ->addMethod ( 'is_customize_preview ' );
7982 $ this ->addMethod ( 'is_plugin_active ' );
8083 $ this ->defaults ();
8184 $ this ->create_filesystem ();
@@ -91,6 +94,7 @@ public function defaults() {
9194 $ this ->admin_initFunctions = array ();
9295 $ this ->options = new WordPressOptions ();
9396 $ this ->metadata = array ();
97+ $ this ->filters = array ();
9498 $ GLOBALS ['_wp_additional_image_sizes ' ] = array ();
9599 }
96100
@@ -103,6 +107,47 @@ public function call( $method, $args ) {
103107 $ this ->admin_initFunctions [] = $ args [1 ];
104108 }
105109 }
110+ // Allow explicit stubs to override defaults/behaviors
111+ if ( isset ( $ this ->stubs [ $ method ] ) && $ this ->stubs [ $ method ] ) {
112+ return call_user_func_array ( $ this ->stubs [ $ method ], $ args );
113+ }
114+ if ( 'add_filter ' === $ method ) {
115+ $ tag = isset ( $ args [0 ] ) ? $ args [0 ] : '' ;
116+ $ function_to_add = isset ( $ args [1 ] ) ? $ args [1 ] : '' ;
117+ $ priority = isset ( $ args [2 ] ) ? intval ( $ args [2 ] ) : 10 ;
118+ $ accepted_args = isset ( $ args [3 ] ) ? intval ( $ args [3 ] ) : 1 ;
119+ if ( ! isset ( $ this ->filters [ $ tag ] ) ) {
120+ $ this ->filters [ $ tag ] = array ();
121+ }
122+ if ( ! isset ( $ this ->filters [ $ tag ][ $ priority ] ) ) {
123+ $ this ->filters [ $ tag ][ $ priority ] = array ();
124+ }
125+ $ this ->filters [ $ tag ][ $ priority ][] = array (
126+ 'function ' => $ function_to_add ,
127+ 'accepted_args ' => $ accepted_args ,
128+ );
129+ return true ;
130+ }
131+ if ( 'apply_filters ' === $ method ) {
132+ $ tag = isset ( $ args [0 ] ) ? $ args [0 ] : '' ;
133+ // $value is the first value passed to filters
134+ $ value = isset ( $ args [1 ] ) ? $ args [1 ] : null ;
135+ $ call_args = array_slice ( $ args , 1 );
136+ if ( isset ( $ this ->filters [ $ tag ] ) ) {
137+ $ priorities = array_keys ( $ this ->filters [ $ tag ] );
138+ sort ( $ priorities , SORT_NUMERIC );
139+ foreach ( $ priorities as $ priority ) {
140+ foreach ( $ this ->filters [ $ tag ][ $ priority ] as $ callback ) {
141+ $ accepted = max ( 1 , intval ( $ callback ['accepted_args ' ] ) );
142+ $ args_to_pass = array_slice ( $ call_args , 0 , $ accepted );
143+ $ returned = call_user_func_array ( $ callback ['function ' ], $ args_to_pass );
144+ // Filters should return the (possibly modified) value as first argument.
145+ $ call_args [0 ] = $ returned ;
146+ }
147+ }
148+ }
149+ return $ call_args [0 ];
150+ }
106151 if ( 'translate ' === $ method ) {
107152 return $ args [0 ];
108153 } elseif ( 'get_option ' === $ method ) {
@@ -123,8 +168,6 @@ public function call( $method, $args ) {
123168 return array ( 'basedir ' => $ this ->vfs ->url () . '/ ' . self ::UPLOAD_DIR , 'baseurl ' => '/ ' . self ::UPLOAD_DIR );
124169 } elseif ( 'is_admin ' === $ method ) {
125170 return true ;
126- } elseif ( $ this ->stubs [ $ method ] ) {
127- return call_user_func_array ( $ this ->stubs [ $ method ], $ args );
128171 }
129172 }
130173
0 commit comments