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(), $this->has_default_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_item_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_item_id = 0 ) { |
48
|
|
|
$this->get_datastore()->set_id( $menu_item_id ); |
|
|
|
|
49
|
|
|
$this->load(); |
50
|
|
|
$this->_attach(); |
51
|
|
|
|
52
|
|
|
// Only the base container should register for updating/rendering |
53
|
|
|
if ( $menu_item_id === 0 ) { |
54
|
|
|
add_action( 'wp_update_nav_menu_item', array( $this, 'update' ), 10, 3 ); |
55
|
|
|
add_action( 'crb_print_carbon_container_nav_menu_fields_html', array( $this, 'form' ), 10, 5 ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Checks whether the current request is valid |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
**/ |
66
|
|
|
public function is_valid_save() { |
67
|
|
|
// rely on wp_update_nav_menu_item action not being called unless WP's nonce is not valid |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Perform checks whether the container should be attached during the current request |
73
|
|
|
* |
74
|
|
|
* @return bool True if the container is allowed to be attached |
75
|
|
|
**/ |
76
|
|
|
public function is_valid_attach() { |
77
|
|
|
global $pagenow; |
78
|
|
|
|
79
|
|
|
if ( $pagenow === 'nav-menus.php' ) { |
80
|
|
|
return true; |
81
|
|
|
} elseif ( defined( 'DOING_AJAX' ) && DOING_AJAX && $_REQUEST['action'] === 'add-menu-item' ) { |
|
|
|
|
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Perform save operation after successful is_valid_save() check. |
90
|
|
|
* The call is propagated to all fields in the container. |
91
|
|
|
**/ |
92
|
|
|
public function save( $data = null ) { |
93
|
|
|
foreach ( $this->fields as $field ) { |
94
|
|
|
$field->set_value_from_input(); |
95
|
|
|
$field->save(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
do_action( 'carbon_after_save_nav_menu', $this ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Output the container markup |
103
|
|
|
**/ |
104
|
|
|
public function render() { |
105
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/nav_menu.php'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Trigger Save for all instances |
110
|
|
|
*/ |
111
|
|
|
public function update( $menu_id, $current_menu_item_id ) { |
112
|
|
|
if ( !$this->is_valid_attach() ) { |
|
|
|
|
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$clone = $this->get_clone_for_menu_item( $current_menu_item_id ); |
117
|
|
|
$clone->_save(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Render custom fields inside each Nav Menu entry |
122
|
|
|
*/ |
123
|
|
|
public function form( $item ) { |
124
|
|
|
if ( !$this->is_valid_attach() ) { |
|
|
|
|
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$clone = $this->get_clone_for_menu_item( $item->ID ); |
129
|
|
|
$clone->render(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Create a clone of this container with it's own datastore for every menu item |
134
|
|
|
*/ |
135
|
|
|
protected function get_clone_for_menu_item( $menu_item_id ) { |
136
|
|
|
if ( !isset( $this->menu_item_instances[ $menu_item_id ] ) ) { |
|
|
|
|
137
|
|
|
$menu_item_datastore = new Nav_Menu_Datastore(); |
138
|
|
|
$menu_item_datastore->set_id( $menu_item_id ); |
139
|
|
|
$menu_item_field_prefix = $menu_item_datastore->get_garbage_prefix(); |
|
|
|
|
140
|
|
|
|
141
|
|
|
$custom_fields = array(); |
142
|
|
|
$fields = $this->get_fields(); |
143
|
|
|
foreach ( $fields as $field ) { |
144
|
|
|
$tmp_field = clone $field; |
145
|
|
|
|
146
|
|
|
$tmp_field->set_id( $menu_item_field_prefix . $tmp_field->get_id() ); |
147
|
|
|
$tmp_field->set_name( $menu_item_field_prefix . $tmp_field->get_name() ); |
148
|
|
|
$tmp_field->set_datastore( $menu_item_datastore, true ); |
149
|
|
|
|
150
|
|
|
$custom_fields[] = $tmp_field; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->menu_item_instances[ $menu_item_id ] = Container::factory( $this->type, $menu_item_field_prefix . $this->id ) |
154
|
|
|
->set_datastore( $menu_item_datastore, true ) |
155
|
|
|
->add_fields( $custom_fields ) |
156
|
|
|
->init( $menu_item_id ); |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->menu_item_instances[ $menu_item_id ]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Setup custom walker for the Nav Menu entries |
165
|
|
|
*/ |
166
|
|
|
public static function edit_walker() { |
167
|
|
|
return '\Carbon_Fields\Walker\Nav_Menu_Edit_Walker'; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.