1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Loader; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Container\Repository as ContainerRepository; |
6
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
7
|
|
|
use Carbon_Fields\Helper\Helper; |
8
|
|
|
use Carbon_Fields\Libraries\Sidebar_Manager\Sidebar_Manager; |
9
|
|
|
use Carbon_Fields\Pimple\Container as PimpleContainer; |
10
|
|
|
use Carbon_Fields\Service\Legacy_Storage_Service_v_1_5; |
11
|
|
|
use Carbon_Fields\Service\Meta_Query_Service; |
12
|
|
|
use Carbon_Fields\Service\REST_API_Service; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Loader and main initialization |
16
|
|
|
*/ |
17
|
|
|
class Loader { |
18
|
|
|
|
19
|
|
|
protected $sidebar_manager; |
20
|
|
|
|
21
|
|
|
protected $container_repository; |
22
|
|
|
|
23
|
|
|
public function __construct( Sidebar_Manager $sidebar_manager, ContainerRepository $container_repository ) { |
24
|
|
|
$this->sidebar_manager = $sidebar_manager; |
25
|
|
|
$this->container_repository = $container_repository; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Hook the main Carbon Fields initialization functionality. |
30
|
|
|
*/ |
31
|
|
|
public function boot() { |
32
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
33
|
|
|
throw new \Exception( 'Carbon Fields cannot be booted outside of a WordPress environment.' ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ( did_action( 'init' ) ) { |
37
|
|
|
throw new \Exception( 'Carbon Fields must be booted before the "init" WordPress action has fired.' ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
include_once( dirname( dirname( __DIR__ ) ) . '/config.php' ); |
41
|
|
|
include_once( \Carbon_Fields\DIR . '/core/functions.php' ); |
42
|
|
|
|
43
|
|
|
add_action( 'after_setup_theme', array( $this, 'load_textdomain' ), 9999 ); |
44
|
|
|
add_action( 'init', array( $this, 'trigger_fields_register' ), 0 ); |
45
|
|
|
add_action( 'rest_api_init', array( $this, 'initialize_widgets' ) ); |
46
|
|
|
add_action( 'carbon_fields_fields_registered', array( $this, 'initialize_containers' ) ); |
47
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_media_browser' ), 0 ); |
48
|
|
|
add_action( 'admin_print_footer_scripts', array( $this, 'enqueue_assets' ), 9 ); |
49
|
|
|
add_action( 'admin_print_footer_scripts', array( $this, 'initialize_ui' ), 9999 ); |
50
|
|
|
add_action( 'edit_form_after_title', array( $this, 'add_carbon_fields_meta_box_contexts' ) ); |
51
|
|
|
add_action( 'wp_ajax_carbon_fields_fetch_association_options', array( $this, 'fetch_association_options' ) ); |
52
|
|
|
|
53
|
|
|
# Enable the legacy storage service |
54
|
|
|
\Carbon_Fields\Carbon_Fields::service( 'legacy_storage' )->enable(); |
55
|
|
|
|
56
|
|
|
# Enable the meta query service |
57
|
|
|
\Carbon_Fields\Carbon_Fields::service( 'meta_query' )->enable(); |
58
|
|
|
|
59
|
|
|
# Enable the REST API service |
60
|
|
|
\Carbon_Fields\Carbon_Fields::service( 'rest_api' )->enable(); |
61
|
|
|
|
62
|
|
|
# Enable post meta revisions service |
63
|
|
|
\Carbon_Fields\Carbon_Fields::service( 'revisions' )->enable(); |
64
|
|
|
|
65
|
|
|
# Initialize sidebar manager |
66
|
|
|
$this->sidebar_manager->boot(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Load the plugin textdomain. |
71
|
|
|
*/ |
72
|
|
|
public function load_textdomain() { |
73
|
|
|
$dir = \Carbon_Fields\DIR . '/languages/'; |
74
|
|
|
$domain = 'carbon-fields'; |
75
|
|
|
$domain_ui = 'carbon-fields-ui'; |
76
|
|
|
$locale = get_locale(); |
77
|
|
|
$path = $dir . $domain . '-' . $locale . '.mo'; |
78
|
|
|
$path_ui = $dir . $domain_ui . '-' . $locale . '.mo'; |
79
|
|
|
load_textdomain( $domain, $path ); |
80
|
|
|
load_textdomain( $domain_ui, $path_ui ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Load the ui textdomain |
85
|
|
|
*/ |
86
|
|
|
public function get_ui_translations() { |
87
|
|
|
$domain ='carbon-fields-ui'; |
88
|
|
|
$translations = get_translations_for_domain( $domain ); |
89
|
|
|
|
90
|
|
|
$locale = array( |
91
|
|
|
'' => array( |
92
|
|
|
'domain' => $domain, |
93
|
|
|
'lang' => is_admin() ? get_user_locale() : get_locale(), |
94
|
|
|
), |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
if ( ! empty( $translations->headers['Plural-Forms'] ) ) { |
98
|
|
|
$locale['']['plural_forms'] = $translations->headers['Plural-Forms']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach ( $translations->entries as $msgid => $entry ) { |
102
|
|
|
$locale[ $msgid ] = $entry->translations; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $locale; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Register containers and fields. |
110
|
|
|
*/ |
111
|
|
|
public function trigger_fields_register() { |
112
|
|
|
try { |
113
|
|
|
do_action( 'carbon_fields_register_fields' ); |
114
|
|
|
do_action( 'carbon_fields_fields_registered' ); |
115
|
|
|
} catch ( Incorrect_Syntax_Exception $e ) { |
116
|
|
|
$callback = ''; |
117
|
|
|
foreach ( $e->getTrace() as $trace ) { |
118
|
|
|
$callback .= '<br/>' . ( isset( $trace['file'] ) ? $trace['file'] . ':' . $trace['line'] : $trace['function'] . '()' ); |
119
|
|
|
} |
120
|
|
|
wp_die( '<h3>' . $e->getMessage() . '</h3><small>' . $callback . '</small>' ); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Initialize containers. |
126
|
|
|
*/ |
127
|
|
|
public function initialize_containers() { |
128
|
|
|
$this->container_repository->initialize_containers(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Initialize the media browser. |
133
|
|
|
*/ |
134
|
|
|
public function enqueue_media_browser() { |
135
|
|
|
wp_enqueue_media(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the rendering context for the assets. |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
protected function get_assets_context() { |
144
|
|
|
$current_screen = get_current_screen(); |
145
|
|
|
|
146
|
|
|
if ( is_null( $current_screen ) ) { |
147
|
|
|
return 'classic'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $current_screen->is_block_editor() ? 'gutenberg' : 'classic'; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns the suffix that should be applied to the assets. |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
protected function get_assets_suffix() { |
159
|
|
|
return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Registers and enqueues a style. |
164
|
|
|
* |
165
|
|
|
* @param string $src |
166
|
|
|
* @param array $deps |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
View Code Duplication |
protected function enqueue_style( $src, $deps = array() ) { |
|
|
|
|
170
|
|
|
$suffix = $this->get_assets_suffix(); |
171
|
|
|
$context = $this->get_assets_context(); |
172
|
|
|
|
173
|
|
|
wp_enqueue_style( |
174
|
|
|
'carbon-fields-' . $src, |
175
|
|
|
\Carbon_Fields\URL . '/build/' . $context . '/' . $src . $suffix . '.css', |
176
|
|
|
$deps, |
177
|
|
|
\Carbon_Fields\VERSION |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Registers and enqueues a script. |
183
|
|
|
* |
184
|
|
|
* @param string $src |
185
|
|
|
* @param array $deps |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
View Code Duplication |
protected function enqueue_script( $src, $deps = array() ) { |
|
|
|
|
189
|
|
|
$suffix = $this->get_assets_suffix(); |
190
|
|
|
$context = $this->get_assets_context(); |
191
|
|
|
|
192
|
|
|
wp_enqueue_script( |
193
|
|
|
'carbon-fields-' . $src, |
194
|
|
|
\Carbon_Fields\URL . '/build/' . $context . '/' . $src . $suffix . '.js', |
195
|
|
|
$deps, |
196
|
|
|
\Carbon_Fields\VERSION |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Enqueues the assets. |
202
|
|
|
* |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
public function enqueue_assets() { |
206
|
|
|
global $pagenow; |
207
|
|
|
|
208
|
|
|
$this->enqueue_style( 'core' ); |
209
|
|
|
$this->enqueue_style( 'metaboxes' ); |
210
|
|
|
|
211
|
|
|
$this->enqueue_script( 'vendor', array( 'wp-polyfill', 'jquery' ) ); |
212
|
|
|
$this->enqueue_script( 'core', array( 'carbon-fields-vendor' ) ); |
213
|
|
|
$this->enqueue_script( 'metaboxes', array( 'carbon-fields-vendor', 'carbon-fields-core' ) ); |
214
|
|
|
|
215
|
|
|
if ( $this->get_assets_context() === 'gutenberg' ) { |
216
|
|
|
$this->enqueue_style( 'blocks' ); |
217
|
|
|
$this->enqueue_script( 'blocks', array( 'carbon-fields-vendor', 'carbon-fields-core' ) ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
wp_add_inline_script( 'carbon-fields-vendor', 'window.cf = window.cf || {}', 'before' ); |
221
|
|
|
wp_add_inline_script( 'carbon-fields-vendor', sprintf( 'window.cf.preloaded = %s', wp_json_encode( $this->get_json_data() ) ), 'before' ); |
222
|
|
|
|
223
|
|
|
$revisions = \Carbon_Fields\Carbon_Fields::service( 'revisions' ); |
224
|
|
|
|
225
|
|
|
wp_localize_script( 'carbon-fields-vendor', 'cf', apply_filters( 'carbon_fields_config', array( |
226
|
|
|
'config' => array( |
227
|
|
|
'locale' => $this->get_ui_translations(), |
228
|
|
|
'pagenow' => $pagenow, |
229
|
|
|
'compactInput' => \Carbon_Fields\COMPACT_INPUT, |
230
|
|
|
'compactInputKey' => \Carbon_Fields\COMPACT_INPUT_KEY, |
231
|
|
|
'revisionsInputKey' => $revisions::CHANGE_KEY, |
232
|
|
|
) |
233
|
|
|
) ) ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Trigger the initialization of the UI. |
238
|
|
|
* |
239
|
|
|
* @return void |
240
|
|
|
*/ |
241
|
|
|
public function initialize_ui() { |
242
|
|
|
?> |
243
|
|
|
<script> |
244
|
|
|
if ( typeof cf.core.initialize === 'function' ) { |
245
|
|
|
cf.core.initialize(); |
246
|
|
|
} |
247
|
|
|
</script> |
248
|
|
|
<?php |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Add custom meta box contexts |
253
|
|
|
*/ |
254
|
|
|
public function add_carbon_fields_meta_box_contexts() { |
255
|
|
|
global $post, $wp_meta_boxes; |
256
|
|
|
|
257
|
|
|
$context = 'carbon_fields_after_title'; |
258
|
|
|
do_meta_boxes( get_current_screen(), $context, $post ); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Retrieve containers and sidebars for use in the JS. |
263
|
|
|
* |
264
|
|
|
* @return array $carbon_data |
265
|
|
|
*/ |
266
|
|
|
public function get_json_data() { |
267
|
|
|
$carbon_data = array( |
268
|
|
|
'blocks' => array(), |
269
|
|
|
'containers' => array(), |
270
|
|
|
'sidebars' => array(), |
271
|
|
|
); |
272
|
|
|
|
273
|
|
|
$containers = $this->container_repository->get_active_containers(); |
274
|
|
|
|
275
|
|
|
foreach ( $containers as $container ) { |
276
|
|
|
$container_data = $container->to_json( true ); |
277
|
|
|
|
278
|
|
|
if ( is_a($container, '\\Carbon_Fields\\Container\\Block_Container', true ) ) { |
279
|
|
|
$carbon_data['blocks'][] = $container_data; |
280
|
|
|
} else { |
281
|
|
|
$carbon_data['containers'][] = $container_data; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
$carbon_data['sidebars'] = Helper::get_active_sidebars(); |
286
|
|
|
|
287
|
|
|
return $carbon_data; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Register widget containers for REST API |
292
|
|
|
* in order to be able to interract with the containers |
293
|
|
|
* |
294
|
|
|
* @access public |
295
|
|
|
* @return void |
296
|
|
|
*/ |
297
|
|
|
public function initialize_widgets() { |
298
|
|
|
global $wp_registered_widgets; |
299
|
|
|
|
300
|
|
|
// Get used (active and inactive) widgets from Carbon Fields |
301
|
|
|
$carbon_fields_widgets_ids = array_filter( array_merge( ...array_values( wp_get_sidebars_widgets() ) ), function ( $widget_id ) { |
302
|
|
|
return substr( $widget_id, 0, 14 ) === 'carbon_fields_'; |
303
|
|
|
} ); |
304
|
|
|
|
305
|
|
|
foreach ( $carbon_fields_widgets_ids as $widget_id ) { |
306
|
|
|
if ( isset( $wp_registered_widgets[ $widget_id ] ) ) { |
307
|
|
|
$widget_class = $wp_registered_widgets[ $widget_id ]['callback'][0]; |
308
|
|
|
|
309
|
|
|
$widget_class->_set( $wp_registered_widgets[ $widget_id ]['params'][0]['number'] ); |
310
|
|
|
$widget_class->register_container(); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Handle association field options fetch. |
317
|
|
|
* |
318
|
|
|
* @access public |
319
|
|
|
* |
320
|
|
|
* @return array |
321
|
|
|
*/ |
322
|
|
View Code Duplication |
public function fetch_association_options() { |
|
|
|
|
323
|
|
|
$page = isset( $_GET['page'] ) ? absint( $_GET['page'] ) : 1; |
324
|
|
|
$term = isset( $_GET['term'] ) ? sanitize_text_field( $_GET['term'] ) : ''; |
325
|
|
|
|
326
|
|
|
$container_id = $_GET['container_id']; |
327
|
|
|
$field_name = $_GET['field_name']; |
328
|
|
|
|
329
|
|
|
/** @var \Carbon_Fields\Field\Association_Field $field */ |
330
|
|
|
$field = Helper::get_field( null, $container_id, $field_name ); |
331
|
|
|
|
332
|
|
|
return wp_send_json_success( $field->get_options( array( |
333
|
|
|
'page' => $page, |
334
|
|
|
'term' => $term, |
335
|
|
|
) ) ); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.