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
|
|
|
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 ) { |
25
|
|
|
// Reset the registered fields array, this is required so we can have fields with same names |
26
|
|
|
self::$registered_field_names = array(); |
27
|
|
|
|
28
|
|
|
$id = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $id ) ) ); |
29
|
|
|
|
30
|
|
|
$this->id = $id; |
31
|
|
|
|
32
|
|
|
$this->store = new Nav_Menu_Datastore(); |
33
|
|
|
|
34
|
|
|
self::initialize_filters(); |
35
|
|
|
} |
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 ) { |
44
|
|
|
$this->set_menu_id( $menu_id ); |
45
|
|
|
|
46
|
|
|
$this->load(); |
47
|
|
|
$this->_attach(); |
48
|
|
|
|
49
|
|
|
if ( ! empty( $menu_id ) && $render === true ) { |
50
|
|
|
$this->render(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the menu item ID the container will operate with. |
58
|
|
|
* |
59
|
|
|
* @param int $menu_id |
60
|
|
|
**/ |
61
|
|
|
public function set_menu_id( $menu_id ) { |
62
|
|
|
$this->menu_id = $menu_id; |
63
|
|
|
$this->store->set_id( $menu_id ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks whether the current request is valid |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
**/ |
71
|
|
|
public function is_valid_save() { |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Perform save operation after successful is_valid_save() check. |
77
|
|
|
* The call is propagated to all fields in the container. |
78
|
|
|
**/ |
79
|
|
|
public function save( $user_data = null ) { |
80
|
|
|
foreach ( $this->fields as $field ) { |
81
|
|
|
$field->set_value_from_input(); |
82
|
|
|
$field->save(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
do_action( 'carbon_after_save_nav_menu', $this ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns an array that holds the container data, suitable for JSON representation. |
90
|
|
|
* This data will be available in the Underscore template and the Backbone Model. |
91
|
|
|
* |
92
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function to_json( $load ) { |
96
|
|
|
$carbon_data = parent::to_json( false ); |
97
|
|
|
|
98
|
|
|
// Sends the menu_id to javascript |
99
|
|
|
$carbon_data = array_merge( $carbon_data, array( |
100
|
|
|
'menu_id' => $this->menu_id, |
101
|
|
|
) ); |
102
|
|
|
|
103
|
|
|
return $carbon_data; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Output the container markup |
108
|
|
|
**/ |
109
|
|
|
public function render() { |
110
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/nav_menu.php'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* TODO: make sure the containers for nav menus are not printed everywhere |
115
|
|
|
*/ |
116
|
|
|
public function is_valid_attach() { |
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Initialize filters. This will be executed only once |
122
|
|
|
*/ |
123
|
|
|
public static function initialize_filters() { |
124
|
|
|
if ( self::$initialized ) { |
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$self = 'Carbon_Fields\Container\Nav_Menu_Container'; |
129
|
|
|
add_action( 'crb_print_carbon_container_nav_menu_fields_html', array( $self, 'form' ), 10, 5 ); |
130
|
|
|
add_filter( 'wp_edit_nav_menu_walker', array( $self, 'edit_walker' ), 10, 2 ); |
131
|
|
|
add_action( 'wp_update_nav_menu_item', array( $self, 'update' ), 10, 3 ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get containers only once, and store in instance memory. |
136
|
|
|
*/ |
137
|
|
|
public static function get_containers() { |
138
|
|
|
if ( empty( self::$active_containers ) ) { |
139
|
|
|
self::$active_containers = Container::get_active_containers(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return self::$active_containers; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Render custom fields inside each Nav Menu entry |
147
|
|
|
*/ |
148
|
|
|
public static function form( $item ) { |
149
|
|
|
self::set_instance_for_id( $item->ID, true ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Setup custom walker for the Nav Menu entries |
154
|
|
|
*/ |
155
|
|
|
public static function edit_walker() { |
156
|
|
|
return 'Carbon_Fields\Walker\Nav_Menu_Edit_Walker'; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Trigger Save for all instances |
161
|
|
|
*/ |
162
|
|
|
public static function update( $menu_id, $current_menu_item_id ) { |
|
|
|
|
163
|
|
|
$instance = self::set_instance_for_id( $current_menu_item_id, false ); |
164
|
|
|
$instance->_save(); |
165
|
|
|
|
166
|
|
|
return $instance; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Render attribute prevents field containers showing on menu save |
171
|
|
|
*/ |
172
|
|
|
public static function set_instance_for_id( $current_menu_item_id, $render = true ) { |
173
|
|
|
$active_containers = self::get_containers(); |
174
|
|
|
$suffix = '-' . $current_menu_item_id; |
175
|
|
|
|
176
|
|
|
foreach ( $active_containers as $container ) { |
177
|
|
|
if ( $container->type != 'Nav_Menu' ) { |
178
|
|
|
continue; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$custom_fields = array(); |
182
|
|
|
$fields = $container->get_fields(); |
183
|
|
|
|
184
|
|
|
foreach ( $fields as $field ) { |
185
|
|
|
$tmp_field = clone $field; |
186
|
|
|
|
187
|
|
|
// Setup Public properties |
188
|
|
|
$tmp_field->current_menu_item_id = $current_menu_item_id; |
189
|
|
|
$tmp_field->initial_name = $tmp_field->get_name(); |
190
|
|
|
|
191
|
|
|
// Setup Field ID and Name |
192
|
|
|
$tmp_field->set_id( $tmp_field->get_id() . $suffix ); |
193
|
|
|
$tmp_field->set_name( $tmp_field->get_name() . $suffix ); |
194
|
|
|
|
195
|
|
|
// Update Datastore instance |
196
|
|
|
$new_datastore = new Nav_Menu_Datastore(); |
197
|
|
|
$new_datastore->set_id( $current_menu_item_id ); |
198
|
|
|
$tmp_field->set_datastore( $new_datastore ); |
199
|
|
|
|
200
|
|
|
$custom_fields[] = $tmp_field; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
self::$instances[ $current_menu_item_id ] = Container::factory( 'nav_menu', $container->id . $suffix ) |
204
|
|
|
->add_fields( $custom_fields ) |
205
|
|
|
->init( $current_menu_item_id, $render ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return self::$instances[ $current_menu_item_id ]; |
209
|
|
|
} |
210
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.