|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon_Fields\Datastore\Meta_Datastore; |
|
6
|
|
|
use Carbon_Fields\Datastore\Nav_Menu_Datastore; |
|
7
|
|
|
use Carbon_Fields\Walker\Nav_Menu_Edit_Walker; |
|
8
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Nav menu item fields container class. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Nav_Menu_Container extends Container { |
|
14
|
|
|
/** |
|
15
|
|
|
* Array of container clones for every menu item |
|
16
|
|
|
* |
|
17
|
|
|
* @see init() |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $menu_item_instances = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Create a new nav menu item fields container |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $id ID of the container |
|
|
|
|
|
|
26
|
|
|
**/ |
|
27
|
|
|
public function __construct( $title ) { |
|
28
|
|
|
parent::__construct( $title ); |
|
29
|
|
|
|
|
30
|
|
|
if ( ! $this->get_datastore() ) { |
|
31
|
|
|
$this->set_datastore( new Nav_Menu_Datastore() ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Register the custom edit walker only once |
|
35
|
|
|
$callable = array( get_class(), 'edit_walker' ); |
|
36
|
|
|
if ( !has_filter( 'wp_edit_nav_menu_walker', $callable ) ) { |
|
|
|
|
|
|
37
|
|
|
add_filter( 'wp_edit_nav_menu_walker', $callable, 10, 2 ); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Perform instance initialization after calling setup() |
|
43
|
|
|
* |
|
44
|
|
|
* @param int $menu_id Used to pass the correct menu_item_id to the Container object |
|
45
|
|
|
* @param bool $render Whether the container will render the fields. |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
|
|
public function init( $menu_id = 0 ) { |
|
48
|
|
|
$this->set_menu_id( $menu_id ); |
|
49
|
|
|
|
|
50
|
|
|
$this->load(); |
|
51
|
|
|
$this->_attach(); |
|
52
|
|
|
|
|
53
|
|
|
// Only the base container should register for updating/rendering |
|
54
|
|
|
if ( $menu_id === 0 ) { |
|
55
|
|
|
add_action( 'wp_update_nav_menu_item', array( $this, 'update' ), 10, 3 ); |
|
56
|
|
|
add_action( 'crb_print_carbon_container_nav_menu_fields_html', array( $this, 'form' ), 10, 5 ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set the menu item ID the container will operate with. |
|
64
|
|
|
* |
|
65
|
|
|
* @param int $menu_id |
|
66
|
|
|
**/ |
|
67
|
|
|
public function set_menu_id( $menu_id ) { |
|
68
|
|
|
$this->menu_id = $menu_id; |
|
69
|
|
|
$this->store->set_id( $menu_id ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Checks whether the current request is valid |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
**/ |
|
77
|
|
|
public function is_valid_save() { |
|
78
|
|
|
// rely on wp_update_nav_menu_item action not being called unless WP's nonce is not valid |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns an array that holds the container data, suitable for JSON representation. |
|
84
|
|
|
* This data will be available in the Underscore template and the Backbone Model. |
|
85
|
|
|
* |
|
86
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
public function to_json( $load ) { |
|
90
|
|
|
$carbon_data = parent::to_json( false ); |
|
91
|
|
|
|
|
92
|
|
|
// Sends the menu_id to javascript |
|
93
|
|
|
$carbon_data = array_merge( $carbon_data, array( |
|
94
|
|
|
'menu_id' => $this->menu_id, |
|
95
|
|
|
) ); |
|
96
|
|
|
|
|
97
|
|
|
return $carbon_data; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Perform checks whether the container should be attached during the current request |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool True if the container is allowed to be attached |
|
104
|
|
|
**/ |
|
105
|
|
|
public function is_valid_attach() { |
|
106
|
|
|
global $pagenow; |
|
107
|
|
|
|
|
108
|
|
|
if ( $pagenow === 'nav-menus.php' ) { |
|
109
|
|
|
return true; |
|
110
|
|
|
} elseif ( defined( 'DOING_AJAX' ) && DOING_AJAX && $_REQUEST['action'] === 'add-menu-item' ) { |
|
|
|
|
|
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Perform save operation after successful is_valid_save() check. |
|
119
|
|
|
* The call is propagated to all fields in the container. |
|
120
|
|
|
**/ |
|
121
|
|
|
public function save( $data = null ) { |
|
122
|
|
|
foreach ( $this->fields as $field ) { |
|
123
|
|
|
$field->set_value_from_input(); |
|
124
|
|
|
$field->save(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
do_action( 'carbon_after_save_nav_menu', $this ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Output the container markup |
|
132
|
|
|
**/ |
|
133
|
|
|
public function render() { |
|
134
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/nav_menu.php'; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Trigger Save for all instances |
|
139
|
|
|
*/ |
|
140
|
|
|
public function update( $menu_id, $current_menu_item_id ) { |
|
141
|
|
|
if ( !$this->is_valid_attach() ) { |
|
|
|
|
|
|
142
|
|
|
return; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$clone = $this->get_clone_for_menu_item( $current_menu_item_id ); |
|
146
|
|
|
$clone->_save(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Render custom fields inside each Nav Menu entry |
|
151
|
|
|
*/ |
|
152
|
|
|
public function form( $item ) { |
|
153
|
|
|
if ( !$this->is_valid_attach() ) { |
|
|
|
|
|
|
154
|
|
|
return; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$clone = $this->get_clone_for_menu_item( $item->ID ); |
|
158
|
|
|
$clone->render(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Create a clone of this container with it's own datastore for every menu item |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function get_clone_for_menu_item( $menu_item_id ) { |
|
165
|
|
|
if ( !isset( $this->menu_item_instances[ $menu_item_id ] ) ) { |
|
|
|
|
|
|
166
|
|
|
$suffix = '-menu-item-' . $menu_item_id; |
|
167
|
|
|
$fields = $this->get_fields(); |
|
168
|
|
|
$menu_item_datastore = new Nav_Menu_Datastore(); |
|
169
|
|
|
$menu_item_datastore->set_id( $menu_item_id ); |
|
170
|
|
|
$custom_fields = array(); |
|
171
|
|
|
|
|
172
|
|
|
foreach ( $fields as $field ) { |
|
173
|
|
|
$tmp_field = clone $field; |
|
174
|
|
|
|
|
175
|
|
|
// Hacky: preserve the original field name as we append the menu item id to it to avoid field name collision in requests |
|
176
|
|
|
$tmp_field->nav_menu_datastore_field_name = $tmp_field->get_name(); |
|
177
|
|
|
|
|
178
|
|
|
$tmp_field->set_id( $tmp_field->get_id() . $suffix ); |
|
179
|
|
|
$tmp_field->set_name( $tmp_field->get_name() . $suffix ); |
|
180
|
|
|
|
|
181
|
|
|
$tmp_field->set_datastore( $menu_item_datastore ); |
|
182
|
|
|
|
|
183
|
|
|
$custom_fields[] = $tmp_field; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$this->menu_item_instances[ $menu_item_id ] = Container::factory( $this->type, $this->id . $suffix ) |
|
187
|
|
|
->set_datastore( $menu_item_datastore ) |
|
188
|
|
|
->add_fields( $custom_fields ) |
|
189
|
|
|
->init( $menu_item_id ); |
|
190
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $this->menu_item_instances[ $menu_item_id ]; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Setup custom walker for the Nav Menu entries |
|
198
|
|
|
*/ |
|
199
|
|
|
public static function edit_walker() { |
|
200
|
|
|
return '\Carbon_Fields\Walker\Nav_Menu_Edit_Walker'; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function add_fields( $fields ) { |
|
204
|
|
|
foreach ( $fields as $field ) { |
|
205
|
|
|
if ( is_a( $field, 'Carbon_Fields\\Field\\Complex_Field' ) ) { |
|
206
|
|
|
Incorrect_Syntax_Exception::raise( get_class() . ' does not support Complex_Field, yet.' ); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return parent::add_fields( $fields ); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.