| 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 |
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 |
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 |
global $wp_version; |
| 208 |
|
| 209 |
$this->enqueue_style( 'core' ); |
| 210 |
$this->enqueue_style( 'metaboxes' ); |
| 211 |
|
| 212 |
$this->enqueue_script( 'vendor', array( 'wp-polyfill', 'jquery', 'lodash' ) ); |
| 213 |
$this->enqueue_script( 'core', array( 'carbon-fields-vendor' ) ); |
| 214 |
$this->enqueue_script( 'metaboxes', array( 'carbon-fields-vendor', 'carbon-fields-core' ) ); |
| 215 |
|
| 216 |
if ( $this->get_assets_context() === 'gutenberg' ) { |
| 217 |
$this->enqueue_style( 'blocks' ); |
| 218 |
$this->enqueue_script( 'blocks', array( 'carbon-fields-vendor', 'carbon-fields-core' ) ); |
| 219 |
} |
| 220 |
|
| 221 |
wp_add_inline_script( 'carbon-fields-vendor', 'window.cf = window.cf || {}', 'before' ); |
| 222 |
wp_add_inline_script( 'carbon-fields-vendor', sprintf( 'window.cf.preloaded = %s', wp_json_encode( $this->get_json_data() ) ), 'before' ); |
| 223 |
|
| 224 |
$revisions = \Carbon_Fields\Carbon_Fields::service( 'revisions' ); |
| 225 |
|
| 226 |
wp_localize_script( 'carbon-fields-vendor', 'cf', apply_filters( 'carbon_fields_config', array( |
| 227 |
'config' => array( |
| 228 |
'locale' => $this->get_ui_translations(), |
| 229 |
'pagenow' => $pagenow, |
| 230 |
'compactInput' => \Carbon_Fields\COMPACT_INPUT, |
| 231 |
'compactInputKey' => \Carbon_Fields\COMPACT_INPUT_KEY, |
| 232 |
'revisionsInputKey' => $revisions::CHANGE_KEY, |
| 233 |
'wp_version' => $wp_version, |
| 234 |
) |
| 235 |
) ) ); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Trigger the initialization of the UI. |
| 240 |
* |
| 241 |
* @return void |
| 242 |
*/ |
| 243 |
public function initialize_ui() { |
| 244 |
?> |
| 245 |
<script> |
| 246 |
if ( typeof cf.core.initialize === 'function' ) { |
| 247 |
cf.core.initialize(); |
| 248 |
} |
| 249 |
</script> |
| 250 |
<?php |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Add custom meta box contexts |
| 255 |
*/ |
| 256 |
public function add_carbon_fields_meta_box_contexts() { |
| 257 |
global $post, $wp_meta_boxes; |
| 258 |
|
| 259 |
$context = 'carbon_fields_after_title'; |
| 260 |
do_meta_boxes( get_current_screen(), $context, $post ); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Retrieve containers and sidebars for use in the JS. |
| 265 |
* |
| 266 |
* @return array $carbon_data |
| 267 |
*/ |
| 268 |
public function get_json_data() { |
| 269 |
$carbon_data = array( |
| 270 |
'blocks' => array(), |
| 271 |
'containers' => array(), |
| 272 |
'sidebars' => array(), |
| 273 |
); |
| 274 |
|
| 275 |
$containers = $this->container_repository->get_active_containers(); |
| 276 |
|
| 277 |
foreach ( $containers as $container ) { |
| 278 |
$container_data = $container->to_json( true ); |
| 279 |
|
| 280 |
if ( is_a($container, '\\Carbon_Fields\\Container\\Block_Container', true ) ) { |
| 281 |
$carbon_data['blocks'][] = $container_data; |
| 282 |
} else { |
| 283 |
$carbon_data['containers'][] = $container_data; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
$carbon_data['sidebars'] = Helper::get_active_sidebars(); |
| 288 |
|
| 289 |
return $carbon_data; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Register widget containers for REST API |
| 294 |
* in order to be able to interract with the containers |
| 295 |
* |
| 296 |
* @access public |
| 297 |
* @return void |
| 298 |
*/ |
| 299 |
public function initialize_widgets() { |
| 300 |
global $wp_registered_widgets; |
| 301 |
|
| 302 |
// Get used (active and inactive) widgets from Carbon Fields |
| 303 |
$carbon_fields_widgets_ids = array_filter( array_merge( ...array_values( wp_get_sidebars_widgets() ) ), function ( $widget_id ) { |
| 304 |
return substr( $widget_id, 0, 14 ) === 'carbon_fields_'; |
| 305 |
} ); |
| 306 |
|
| 307 |
foreach ( $carbon_fields_widgets_ids as $widget_id ) { |
| 308 |
if ( isset( $wp_registered_widgets[ $widget_id ] ) ) { |
| 309 |
$widget_class = $wp_registered_widgets[ $widget_id ]['callback'][0]; |
| 310 |
|
| 311 |
$widget_class->_set( $wp_registered_widgets[ $widget_id ]['params'][0]['number'] ); |
| 312 |
$widget_class->register_container(); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Handle association field options fetch. |
| 319 |
* |
| 320 |
* @access public |
| 321 |
* |
| 322 |
* @return array |
| 323 |
*/ |
| 324 |
public function fetch_association_options() { |
| 325 |
$page = isset( $_GET['page'] ) ? absint( $_GET['page'] ) : 1; |
| 326 |
$term = isset( $_GET['term'] ) ? sanitize_text_field( $_GET['term'] ) : ''; |
| 327 |
|
| 328 |
$container_id = $_GET['container_id']; |
| 329 |
$field_name = $_GET['field_name']; |
| 330 |
|
| 331 |
/** @var \Carbon_Fields\Field\Association_Field $field */ |
| 332 |
$field = Helper::get_field( null, $container_id, $field_name ); |
| 333 |
|
| 334 |
return wp_send_json_success( $field->get_options( array( |
| 335 |
'page' => $page, |
| 336 |
'term' => $term, |
| 337 |
) ) ); |
| 338 |
} |
| 339 |
} |
| 340 |
|