Completed
Push — milestone/2_0/react-ui ( 21feb1...1c376a )
by
unknown
03:59
created

Loader::load_textdomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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