Completed
Push — milestone/2_0/react-ui ( 4b546d...e2e0c4 )
by
unknown
09:02
created

Loader::enqueue_scripts()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 33
nc 4
nop 0
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Loader;
4
5
use Carbon_Fields\Pimple\Container as PimpleContainer;
6
use Carbon_Fields\Container\Repository as ContainerRepository;
7
use Carbon_Fields\Service\Legacy_Storage_Service_v_1_5;
8
use Carbon_Fields\Service\Meta_Query_Service;
9
use Carbon_Fields\Service\REST_API_Service;
10
use Carbon_Fields\Libraries\Sidebar_Manager\Sidebar_Manager;
11
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
12
13
/**
14
 * Loader and main initialization
15
 */
16
class Loader {
17
18
	protected $sidebar_manager;
19
20
	protected $container_repository;
21
22
	public function __construct( Sidebar_Manager $sidebar_manager, ContainerRepository $container_repository ) {
23
		$this->sidebar_manager = $sidebar_manager;
24
		$this->container_repository = $container_repository;
25
	}
26
27
	/**
28
	 * Hook the main Carbon Fields initialization functionality.
29
	 */
30
	public function boot() {
31
		if ( ! defined( 'ABSPATH' ) ) {
32
			throw new \Exception( 'Carbon Fields cannot be booted outside of a WordPress environment.' );
33
		}
34
35
		if ( did_action( 'init' ) ) {
36
			throw new \Exception( 'Carbon Fields must be booted before the "init" WordPress action has fired.' );
37
		}
38
39
		include_once( dirname( dirname( __DIR__ ) ) . '/config.php' );
40
		include_once( \Carbon_Fields\DIR . '/core/functions.php' );
41
42
		add_action( 'after_setup_theme', array( $this, 'load_textdomain' ), 9999 );
43
		add_action( 'init', array( $this, 'trigger_fields_register' ), 0 );
44
		add_action( 'carbon_fields_fields_registered', array( $this, 'initialize_containers' ) );
45
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_media_browser' ), 0 );
46
		add_action( 'admin_print_footer_scripts', array( $this, 'enqueue_scripts' ), 0 );
47
		add_action( 'admin_print_footer_scripts', array( $this, 'print_json_data_script' ), 9 );
48
		add_action( 'admin_print_footer_scripts', array( $this, 'print_bootstrap_js' ), 100 );
49
		add_action( 'edit_form_after_title', array( $this, 'add_carbon_fields_meta_box_contexts' ) );
50
51
		# Enable the legacy storage service
52
		\Carbon_Fields\Carbon_Fields::service( 'legacy_storage' )->enable();
53
54
		# Enable the meta query service
55
		\Carbon_Fields\Carbon_Fields::service( 'meta_query' )->enable();
56
57
		# Enable the REST API service
58
		\Carbon_Fields\Carbon_Fields::service( 'rest_api' )->enable();
59
60
		# Initialize sidebar manager
61
		$this->sidebar_manager->boot();
62
	}
63
64
	/**
65
	 * Load the plugin textdomain.
66
	 */
67
	public function load_textdomain() {
68
		$dir = \Carbon_Fields\DIR . '/languages/';
69
		$domain = 'carbon-fields';
70
		$locale = get_locale();
71
		$path = $dir . $domain . '-' . $locale . '.mo';
72
		load_textdomain( $domain, $path );
73
	}
74
75
	/**
76
	 * Register containers and fields.
77
	 */
78
	public function trigger_fields_register() {
79
		try {
80
			do_action( 'carbon_fields_register_fields' );
81
			do_action( 'carbon_fields_fields_registered' );
82
		} catch ( Incorrect_Syntax_Exception $e ) {
83
			$callback = '';
84
			foreach ( $e->getTrace() as $trace ) {
85
				$callback .= '<br/>' . ( isset( $trace['file'] ) ? $trace['file'] . ':' . $trace['line'] : $trace['function'] . '()' );
86
			}
87
			wp_die( '<h3>' . $e->getMessage() . '</h3><small>' . $callback . '</small>' );
88
		}
89
	}
90
91
	/**
92
	 * Initialize containers.
93
	 */
94
	public function initialize_containers() {
95
		$this->container_repository->initialize_containers();
96
	}
97
98
	/**
99
	 * Initialize the media browser.
100
	 */
101
	public function enqueue_media_browser() {
102
		wp_enqueue_media();
103
	}
104
105
	/**
106
	 * Initialize main scripts
107
	 */
108
	public function enqueue_scripts() {
109
		$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
110
111
		wp_enqueue_style( 'carbon-fields-core', \Carbon_Fields\URL . '/assets/dist/carbon.css', array(), \Carbon_Fields\VERSION );
112
113
		wp_enqueue_script( 'carbon-fields-vendor', \Carbon_Fields\URL . '/assets/dist/carbon.vendor' . $suffix . '.js', array( 'jquery' ), \Carbon_Fields\VERSION );
114
		wp_enqueue_script( 'carbon-fields-core', \Carbon_Fields\URL . '/assets/dist/carbon.core' . $suffix . '.js', array( 'carbon-fields-vendor', 'quicktags', 'editor' ), \Carbon_Fields\VERSION );
115
		wp_enqueue_script( 'carbon-fields-boot', \Carbon_Fields\URL . '/assets/dist/carbon.boot' . $suffix . '.js', array( 'carbon-fields-core' ), \Carbon_Fields\VERSION );
116
117
		wp_localize_script( 'carbon-fields-vendor', 'carbonFieldsL10n', apply_filters( 'carbon_fields_l10n', array(
118
			'container' => array(
119
				'pleaseFillTheRequiredFields' => __( 'Please fill out all required fields highlighted below.', 'carbon-fields' ),
120
				'changesMadeSaveAlert' => __( 'The changes you made will be lost if you navigate away from this page.', 'carbon-fields' ),
121
			),
122
			'field' => array(
123
				'geocodeZeroResults' => __( 'The address could not be found. ', 'carbon-fields' ),
124
				'geocodeNotSuccessful' => __( 'Geocode was not successful for the following reason: ', 'carbon-fields' ),
125
				'mapLocateAddress' => __( 'Locate address on the map:', 'carbon-fields' ),
126
				'maxNumItemsReached' => __( 'Maximum number of items reached (%s items)', 'carbon-fields' ),
127
128
				'complexNoRows' => __( 'There are no %s yet. Click <a href="#">here</a> to add one.', 'carbon-fields' ),
129
				'complexMinNumRowsNotReached' => __( 'Minimum number of rows not reached (%1$d %2$s)', 'carbon-fields' ),
130
				'complexAddButton' => __( 'Add %s', 'carbon-fields' ),
131
				'complexCloneButton' => __( 'Clone', 'carbon-fields' ),
132
				'complexRemoveButton' => __( 'Remove', 'carbon-fields' ),
133
				'complexCollapseExpandButton' => __( 'Collapse/Expand', 'carbon-fields' ),
134
135
				'messageFormValidationFailed' => __( 'Please fill out all fields correctly. ', 'carbon-fields' ),
136
				'messageRequiredField' => __( 'This field is required. ', 'carbon-fields' ),
137
				'messageChooseOption' => __( 'Please choose an option. ', 'carbon-fields' ),
138
139
				'enterNameOfNewSidebar' => __( 'Please enter the name of the new sidebar:', 'carbon-fields' ),
140
141
				'selectTime' => __( 'Select Time', 'carbon-fields' ),
142
				'selectDate' => __( 'Select Date', 'carbon-fields' ),
143
144
				'associationOutOf' => __( 'out of', 'carbon-fields' ),
145
				'associationSelectedItem' => __( 'selected item', 'carbon-fields' ),
146
				'associationSelectedItems' => __( 'selected items', 'carbon-fields' ),
147
148
				'colorSelectColor' => __( 'Select a color', 'carbon-fields' ),
149
150
				'noOptions' => __( 'No options.', 'carbon-fields' ),
151
152
				'setShowAll' => __( 'Show all options', 'carbon-fields' ),
153
			),
154
		) ) );
155
	}
156
157
	/**
158
	 * Add custom meta box contexts
159
	 */
160
	public function add_carbon_fields_meta_box_contexts() {
161
		global $post, $wp_meta_boxes;
162
163
		$context = 'carbon_fields_after_title';
164
		foreach ( $wp_meta_boxes as $post_type => $meta_boxes ) {
165
			if ( empty( $meta_boxes[ $context ] ) ) {
166
				continue;
167
			}
168
			do_meta_boxes( get_current_screen(), $context, $post );
169
			unset( $wp_meta_boxes[ $post_type ][ $context ] );
170
		}
171
	}
172
173
	/**
174
	 * Retrieve containers and sidebars for use in the JS.
175
	 *
176
	 * @return array $carbon_data
177
	 */
178
	public function get_json_data() {
179
		global $wp_registered_sidebars;
180
		global $pagenow;
181
182
		$carbon_data = array(
183
			'containers' => array(),
184
			'sidebars' => array(),
185
			'pagenow' => $pagenow,
186
		);
187
188
		$containers = $this->container_repository->get_active_containers();
189
190
		foreach ( $containers as $container ) {
191
			$container_data = $container->to_json( true );
192
193
			$carbon_data['containers'][] = $container_data;
194
		}
195
196
		foreach ( $wp_registered_sidebars as $sidebar ) {
197
			// Check if we have inactive sidebars
198
			if ( isset( $sidebar['class'] ) && strpos( $sidebar['class'], 'inactive-sidebar' ) !== false ) {
199
				continue;
200
			}
201
202
			$carbon_data['sidebars'][] = array(
203
				'name' => $sidebar['name'],
204
				'id'   => $sidebar['id'],
205
			);
206
		}
207
208
		return $carbon_data;
209
	}
210
211
	/**
212
	 * Print the carbon JSON data script.
213
	 */
214
	public function print_json_data_script() {
215
		?>
216
<script type="text/javascript">
217
<!--//--><![CDATA[//><!--
218
var carbon_json = <?php echo wp_json_encode( $this->get_json_data() ); ?>;
219
//--><!]]>
220
</script>
221
		<?php
222
	}
223
224
	/**
225
	 * Print the bootstrap code for the fields.
226
	 */
227
	public function print_bootstrap_js() {
228
		?>
229
		<script type="text/javascript">
230
			if (window['carbon.boot'] && typeof window['carbon.boot'].default === 'function') {
231
				window['carbon.boot'].default();
232
			}
233
		</script>
234
		<?php
235
	}
236
}
237