1 | <?php |
||
13 | class Nav_Menu_Container extends Container { |
||
14 | |||
15 | public static $instances = array(); |
||
16 | public static $active_containers = array(); |
||
17 | public static $initialized = false; |
||
18 | |||
19 | /** |
||
20 | * Create a new nav menu item fields container |
||
21 | * |
||
22 | * @param string $id ID of the container |
||
23 | **/ |
||
24 | public function __construct( $id ) { |
||
36 | |||
37 | /** |
||
38 | * Perform instance initialization after calling setup() |
||
39 | * |
||
40 | * @param int $menu_id Used to pass the correct menu_item_id to the Container object |
||
41 | * @param bool $render Whether the container will render the fields. |
||
42 | */ |
||
43 | public function init( $menu_id = 0, $render = true ) { |
||
55 | |||
56 | /** |
||
57 | * Assign DataStore instance for use by the container fields |
||
58 | * |
||
59 | * @param object $store |
||
60 | **/ |
||
61 | public function set_datastore( Meta_Datastore $store ) { |
||
64 | |||
65 | /** |
||
66 | * Set the menu item ID the container will operate with. |
||
67 | * |
||
68 | * @param int $menu_id |
||
69 | **/ |
||
70 | public function set_menu_id( $menu_id ) { |
||
74 | |||
75 | /** |
||
76 | * Checks whether the current request is valid |
||
77 | * |
||
78 | * @return bool |
||
79 | **/ |
||
80 | public function is_valid_save() { |
||
83 | |||
84 | /** |
||
85 | * Perform save operation after successful is_valid_save() check. |
||
86 | * The call is propagated to all fields in the container. |
||
87 | **/ |
||
88 | public function save( $user_data = null ) { |
||
96 | |||
97 | /** |
||
98 | * Returns an array that holds the container data, suitable for JSON representation. |
||
99 | * This data will be available in the Underscore template and the Backbone Model. |
||
100 | * |
||
101 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
102 | * @return array |
||
103 | */ |
||
104 | public function to_json( $load ) { |
||
114 | |||
115 | /** |
||
116 | * Output the container markup |
||
117 | **/ |
||
118 | public function render() { |
||
121 | |||
122 | /** |
||
123 | * TODO: make sure the containers for nav menus are not printed everywhere |
||
124 | */ |
||
125 | public function is_valid_attach() { |
||
128 | |||
129 | /** |
||
130 | * Initialize filters. This will be executed only once |
||
131 | */ |
||
132 | public static function initialize_filters() { |
||
142 | |||
143 | /** |
||
144 | * Get containers only once, and store in instance memory. |
||
145 | */ |
||
146 | public static function get_containers() { |
||
153 | |||
154 | /** |
||
155 | * Render custom fields inside each Nav Menu entry |
||
156 | */ |
||
157 | public static function form( $item ) { |
||
160 | |||
161 | /** |
||
162 | * Setup custom walker for the Nav Menu entries |
||
163 | */ |
||
164 | public static function edit_walker() { |
||
167 | |||
168 | /** |
||
169 | * Trigger Save for all instances |
||
170 | */ |
||
171 | public static function update( $menu_id, $current_menu_item_id ) { |
||
177 | |||
178 | /** |
||
179 | * Render attribute prevents field containers showing on menu save |
||
180 | */ |
||
181 | public static function set_instance_for_id( $current_menu_item_id, $render = true ) { |
||
219 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: