1 | <?php |
||
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 ) { |
||
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( 'carbon_fields_fields_registered', array( $this, 'initialize_containers' ) ); |
||
46 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_media_browser' ), 0 ); |
||
47 | add_action( 'admin_print_footer_scripts', array( $this, 'enqueue_scripts' ), 0 ); |
||
48 | add_action( 'admin_print_footer_scripts', array( $this, 'print_json_data_script' ), 9 ); |
||
49 | add_action( 'admin_print_footer_scripts', array( $this, 'print_bootstrap_js' ), 100 ); |
||
50 | add_action( 'edit_form_after_title', array( $this, 'add_carbon_fields_meta_box_contexts' ) ); |
||
51 | |||
52 | # Enable the legacy storage service |
||
53 | \Carbon_Fields\Carbon_Fields::service( 'legacy_storage' )->enable(); |
||
54 | |||
55 | # Enable the meta query service |
||
56 | \Carbon_Fields\Carbon_Fields::service( 'meta_query' )->enable(); |
||
57 | |||
58 | # Enable the REST API service |
||
59 | \Carbon_Fields\Carbon_Fields::service( 'rest_api' )->enable(); |
||
60 | |||
61 | # Initialize sidebar manager |
||
62 | $this->sidebar_manager->boot(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Load the plugin textdomain. |
||
67 | */ |
||
68 | public function load_textdomain() { |
||
69 | $dir = \Carbon_Fields\DIR . '/languages/'; |
||
70 | $domain = 'carbon-fields'; |
||
71 | $locale = get_locale(); |
||
72 | $path = $dir . $domain . '-' . $locale . '.mo'; |
||
73 | load_textdomain( $domain, $path ); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Register containers and fields. |
||
78 | */ |
||
79 | public function trigger_fields_register() { |
||
80 | try { |
||
81 | do_action( 'carbon_fields_register_fields' ); |
||
82 | do_action( 'carbon_fields_fields_registered' ); |
||
83 | } catch ( Incorrect_Syntax_Exception $e ) { |
||
84 | $callback = ''; |
||
85 | foreach ( $e->getTrace() as $trace ) { |
||
86 | $callback .= '<br/>' . ( isset( $trace['file'] ) ? $trace['file'] . ':' . $trace['line'] : $trace['function'] . '()' ); |
||
87 | } |
||
88 | wp_die( '<h3>' . $e->getMessage() . '</h3><small>' . $callback . '</small>' ); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Initialize containers. |
||
94 | */ |
||
95 | public function initialize_containers() { |
||
96 | $this->container_repository->initialize_containers(); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Initialize the media browser. |
||
101 | */ |
||
102 | public function enqueue_media_browser() { |
||
103 | wp_enqueue_media(); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Initialize main scripts |
||
108 | */ |
||
109 | public function enqueue_scripts() { |
||
110 | $locale = get_locale(); |
||
111 | $short_locale = substr( $locale, 0, 2 ); |
||
112 | $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
||
113 | |||
114 | wp_enqueue_style( 'carbon-fields-core', \Carbon_Fields\URL . '/assets/dist/carbon.css', array(), \Carbon_Fields\VERSION ); |
||
115 | |||
116 | wp_enqueue_script( 'carbon-fields-vendor', \Carbon_Fields\URL . '/assets/dist/carbon.vendor' . $suffix . '.js', array( 'jquery' ), \Carbon_Fields\VERSION ); |
||
117 | wp_enqueue_script( 'carbon-fields-core', \Carbon_Fields\URL . '/assets/dist/carbon.core' . $suffix . '.js', array( 'carbon-fields-vendor', 'quicktags', 'editor' ), \Carbon_Fields\VERSION ); |
||
118 | wp_enqueue_script( 'carbon-fields-boot', \Carbon_Fields\URL . '/assets/dist/carbon.boot' . $suffix . '.js', array( 'carbon-fields-core' ), \Carbon_Fields\VERSION ); |
||
119 | |||
120 | wp_localize_script( 'carbon-fields-vendor', 'carbonFieldsConfig', apply_filters( 'carbon_fields_config', array( |
||
121 | 'compactInput' => \Carbon_Fields\COMPACT_INPUT, |
||
122 | 'compactInputKey' => \Carbon_Fields\COMPACT_INPUT_KEY, |
||
123 | ) ) ); |
||
124 | wp_localize_script( 'carbon-fields-vendor', 'carbonFieldsL10n', apply_filters( 'carbon_fields_l10n', array( |
||
125 | 'locale' => $locale, |
||
126 | 'shortLocale' => $short_locale, |
||
127 | |||
128 | 'container' => array( |
||
129 | 'pleaseFillTheRequiredFields' => __( 'Please fill out all required fields highlighted below.', 'carbon-fields' ), |
||
130 | 'changesMadeSaveAlert' => __( 'The changes you made will be lost if you navigate away from this page.', 'carbon-fields' ), |
||
131 | ), |
||
132 | |||
133 | 'field' => array( |
||
134 | 'geocodeZeroResults' => __( 'The address could not be found. ', 'carbon-fields' ), |
||
135 | 'geocodeNotSuccessful' => __( 'Geocode was not successful for the following reason: ', 'carbon-fields' ), |
||
136 | 'mapLocateAddress' => __( 'Locate address on the map:', 'carbon-fields' ), |
||
137 | 'minNumItemsNotReached' => __( 'Minimum number of items not reached (%s items)', 'carbon-fields' ), |
||
138 | 'maxNumItemsReached' => __( 'Maximum number of items reached (%s items)', 'carbon-fields' ), |
||
139 | |||
140 | 'complexNoRows' => __( 'There are no %s yet. Click <a href="#">here</a> to add one.', 'carbon-fields' ), |
||
141 | 'complexMinNumRowsNotReached' => __( 'Minimum number of rows not reached (%1$d %2$s)', 'carbon-fields' ), |
||
142 | 'complexMaxNumRowsExceeded' => __( 'Maximum number of rows exceeded (%1$d %2$s)', 'carbon-fields' ), |
||
143 | 'complexAddButton' => _x( 'Add %s', 'Complex field', 'carbon-fields' ), |
||
144 | 'complexCloneButton' => _x( 'Clone', 'Complex field', 'carbon-fields' ), |
||
145 | 'complexRemoveButton' => _x( 'Remove', 'Complex field', 'carbon-fields' ), |
||
146 | 'complexCollapseExpandButton' => _x( 'Collapse/Expand', 'Complex field', 'carbon-fields' ), |
||
147 | 'complexCollapseAllButton' => _x( 'Collapse All', 'Complex field groups', 'carbon-fields' ), |
||
148 | 'complexExpandAllButton' => _x( 'Expand All', 'Complex field groups', 'carbon-fields' ), |
||
149 | |||
150 | 'messageFormValidationFailed' => __( 'Please fill out all fields correctly. ', 'carbon-fields' ), |
||
151 | 'messageRequiredField' => __( 'This field is required. ', 'carbon-fields' ), |
||
152 | 'messageChooseOption' => __( 'Please choose an option. ', 'carbon-fields' ), |
||
153 | |||
154 | 'enterNameOfNewSidebar' => __( 'Please enter the name of the new sidebar:', 'carbon-fields' ), |
||
155 | |||
156 | 'selectTime' => __( 'Select Time', 'carbon-fields' ), |
||
157 | 'selectDate' => __( 'Select Date', 'carbon-fields' ), |
||
158 | |||
159 | 'associationSelectedItem' => __( '%1$d selected item', 'carbon-fields' ), |
||
160 | 'associationSelectedItems' => __( '%1$d selected items', 'carbon-fields' ), |
||
161 | 'associationSelectedItemOutOf' => __( '%1$d selected item out of %2$d', 'carbon-fields' ), |
||
162 | 'associationSelectedItemsOutOf' => __( '%1$d selected items out of %2$d', 'carbon-fields' ), |
||
163 | |||
164 | 'colorSelectColor' => __( 'Select a color', 'carbon-fields' ), |
||
165 | |||
166 | 'noOptions' => __( 'No options.', 'carbon-fields' ), |
||
167 | |||
168 | 'setShowAll' => __( 'Show all options', 'carbon-fields' ), |
||
169 | |||
170 | 'searchPlaceholder' => __( 'Search...', 'carbon-fields' ), |
||
171 | |||
172 | 'editAttachmentUrl' => _x( 'URL', 'WordPress media attachment', 'carbon-fields' ), |
||
173 | 'editAttachmentTitle' => _x( 'Title', 'WordPress media attachment', 'carbon-fields' ), |
||
174 | 'editAttachmentArtist' => _x( 'Artist', 'WordPress media attachment', 'carbon-fields' ), |
||
175 | 'editAttachmentAlbum' => _x( 'Album', 'WordPress media attachment', 'carbon-fields' ), |
||
176 | 'editAttachmentCaption' => _x( 'Caption', 'WordPress media attachment', 'carbon-fields' ), |
||
177 | 'editAttachmentAlt' => _x( 'Alt Text', 'WordPress media attachment', 'carbon-fields' ), |
||
178 | 'editAttachmentDescription' => _x( 'Description', 'WordPress media attachment', 'carbon-fields' ), |
||
179 | 'editAttachmentClose' => _x( 'Close', 'WordPress media attachment', 'carbon-fields' ), |
||
180 | 'editAttachmentSave' => _x( 'Save', 'WordPress media attachment', 'carbon-fields' ), |
||
181 | |||
182 | 'oembedNotFound' => _x( 'Not Found', 'oEmbed field', 'carbon-fields' ), |
||
183 | |||
184 | 'mediaGalleryNoFiles' => __( 'No files selected.', 'carbon-fields' ), |
||
185 | 'mediaGalleryButtonLabel' => __( 'Add File', 'carbon-fields' ), |
||
186 | 'mediaGalleryBrowserTitle' => _x( 'Files', 'WordPress Media Browser', 'carbon-fields' ), |
||
187 | 'mediaGalleryBrowserButtonLabel' => _x( 'Select File', 'WordPress Media Browser', 'carbon-fields' ), |
||
188 | |||
189 | 'fileButtonLabel' => __( 'Select File', 'carbon-fields' ), |
||
190 | 'fileBrowserTitle' => _x( 'Files', 'WordPress Media Browser', 'carbon-fields' ), |
||
191 | 'fileBrowserButtonLabel' => _x( 'Select File', 'WordPress Media Browser', 'carbon-fields' ), |
||
192 | |||
193 | 'imageButtonLabel' => __( 'Select Image', 'carbon-fields' ), |
||
194 | 'imageBrowserTitle' => _x( 'Images', 'WordPress Media Browser', 'carbon-fields' ), |
||
195 | 'imageBrowserButtonLabel' => _x( 'Select Image', 'WordPress Media Browser', 'carbon-fields' ), |
||
196 | ), |
||
197 | ) ) ); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Add custom meta box contexts |
||
202 | */ |
||
203 | public function add_carbon_fields_meta_box_contexts() { |
||
204 | global $post, $wp_meta_boxes; |
||
205 | |||
206 | $context = 'carbon_fields_after_title'; |
||
207 | foreach ( $wp_meta_boxes as $post_type => $meta_boxes ) { |
||
208 | if ( empty( $meta_boxes[ $context ] ) ) { |
||
209 | continue; |
||
210 | } |
||
211 | do_meta_boxes( get_current_screen(), $context, $post ); |
||
212 | unset( $wp_meta_boxes[ $post_type ][ $context ] ); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Retrieve containers and sidebars for use in the JS. |
||
218 | * |
||
219 | * @return array $carbon_data |
||
220 | */ |
||
221 | public function get_json_data() { |
||
222 | global $pagenow; |
||
223 | |||
224 | $carbon_data = array( |
||
225 | 'containers' => array(), |
||
226 | 'sidebars' => array(), |
||
227 | 'pagenow' => $pagenow, |
||
228 | ); |
||
229 | |||
230 | $containers = $this->container_repository->get_active_containers(); |
||
231 | |||
232 | foreach ( $containers as $container ) { |
||
233 | $container_data = $container->to_json( true ); |
||
234 | |||
235 | $carbon_data['containers'][] = $container_data; |
||
236 | } |
||
237 | |||
238 | $carbon_data['sidebars'] = Helper::get_active_sidebars(); |
||
239 | |||
240 | return $carbon_data; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Print the carbon JSON data script. |
||
245 | */ |
||
246 | public function print_json_data_script() { |
||
247 | ?> |
||
248 | <script type="text/javascript"> |
||
249 | <!--//--><![CDATA[//><!-- |
||
250 | var carbon_json = <?php echo wp_json_encode( $this->get_json_data() ); ?>; |
||
251 | //--><!]]> |
||
252 | </script> |
||
253 | <?php |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Print the bootstrap code for the fields. |
||
258 | */ |
||
259 | public function print_bootstrap_js() { |
||
268 | } |
||
269 |