Skip to content

Commit 3d70475

Browse files
authored
Cleanup script registering (#85)
* Cleanup manager scripts * Cleanup runtime scripts * Standardize script enqueueing
1 parent 9f46a55 commit 3d70475

29 files changed

+592
-878
lines changed

includes/manager/class-content-model-loader.php

+24-170
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function get_instance() {
3838
* Initializes the Content_Model_Loader class.
3939
*
4040
* Checks if the current user has the capability to manage options.
41-
* If they do, it registers the post type and enqueues the attribute binder.
41+
* If they do, it registers the post type and enqueues the manager scripts.
4242
*
4343
* @return void
4444
*/
@@ -48,11 +48,8 @@ private function __construct() {
4848
}
4949

5050
$this->register_post_type();
51-
$this->maybe_enqueue_the_attribute_binder();
52-
$this->maybe_enqueue_the_cpt_settings_panel();
53-
$this->maybe_enqueue_the_fields_ui();
54-
$this->maybe_enqueue_content_model_length_restrictor();
55-
$this->maybe_enqueue_the_defaut_value_placeholder();
51+
52+
add_action( 'enqueue_block_editor_assets', array( $this, 'maybe_enqueue_scripts' ) );
5653

5754
add_action( 'save_post', array( $this, 'map_template_to_bindings_api_signature' ), 99, 2 );
5855

@@ -149,178 +146,35 @@ private function register_post_type() {
149146
}
150147

151148
/**
152-
* Conditionally enqueues the attribute binder script for the block editor.
153-
*
154-
* Checks if the current post is of the correct type before enqueueing the script.
149+
* Enqueue the helper scripts if opening the content model manager.
155150
*
156151
* @return void
157152
*/
158-
private function maybe_enqueue_the_attribute_binder() {
159-
add_action(
160-
'enqueue_block_editor_assets',
161-
function () {
162-
global $post;
163-
164-
if ( ! $post || Content_Model_Manager::POST_TYPE_NAME !== $post->post_type ) {
165-
return;
166-
}
167-
168-
$register_attribute_binder_js = include CONTENT_MODEL_PLUGIN_PATH . '/includes/manager/dist/register-attribute-binder.asset.php';
169-
170-
wp_enqueue_script(
171-
'content-model/attribute-binder',
172-
CONTENT_MODEL_PLUGIN_URL . '/includes/manager/dist/register-attribute-binder.js',
173-
$register_attribute_binder_js['dependencies'],
174-
$register_attribute_binder_js['version'],
175-
true
176-
);
177-
178-
wp_add_inline_script(
179-
'content-model/attribute-binder',
180-
'window.BINDINGS_KEY = "' . Content_Model_Loader::BINDINGS_KEY . '";',
181-
'before'
182-
);
183-
184-
wp_add_inline_script(
185-
'content-model/attribute-binder',
186-
'window.BLOCK_VARIATION_NAME_ATTR = "' . Content_Model_Block::BLOCK_VARIATION_NAME_ATTR . '";',
187-
'before'
188-
);
189-
}
190-
);
191-
}
153+
public function maybe_enqueue_scripts() {
154+
global $post;
192155

193-
/**
194-
* Conditionally enqueues the CPT settings script for the content model editor.
195-
*
196-
* Checks if the current post is of the correct type before enqueueing the script.
197-
*
198-
* @return void
199-
*/
200-
private function maybe_enqueue_the_cpt_settings_panel() {
201-
add_action(
202-
'enqueue_block_editor_assets',
203-
function () {
204-
global $post;
205-
206-
if ( ! $post || Content_Model_Manager::POST_TYPE_NAME !== $post->post_type ) {
207-
return;
208-
}
209-
210-
$asset_file = include CONTENT_MODEL_PLUGIN_PATH . 'includes/manager/dist/cpt-settings-panel.asset.php';
211-
212-
wp_register_script(
213-
'data-types/cpt-settings-panel',
214-
CONTENT_MODEL_PLUGIN_URL . '/includes/manager/dist/cpt-settings-panel.js',
215-
$asset_file['dependencies'],
216-
$asset_file['version'],
217-
true
218-
);
219-
220-
wp_localize_script(
221-
'data-types/cpt-settings-panel',
222-
'contentModelFields',
223-
array(
224-
'postType' => Content_Model_Manager::POST_TYPE_NAME,
225-
)
226-
);
227-
228-
wp_enqueue_script( 'data-types/cpt-settings-panel' );
229-
}
230-
);
231-
}
156+
if ( ! $post || Content_Model_Manager::POST_TYPE_NAME !== $post->post_type ) {
157+
return;
158+
}
232159

160+
$asset_file = include CONTENT_MODEL_PLUGIN_PATH . '/includes/manager/dist/manager.asset.php';
233161

234-
/**
235-
* Conditionally enqueues the fields UI script for the block editor.
236-
*
237-
* Checks if the current post is of the correct type before enqueueing the script.
238-
*
239-
* @return void
240-
*/
241-
private function maybe_enqueue_the_fields_ui() {
242-
add_action(
243-
'enqueue_block_editor_assets',
244-
function () {
245-
global $post;
246-
247-
if ( ! $post || Content_Model_Manager::POST_TYPE_NAME !== $post->post_type ) {
248-
return;
249-
}
250-
251-
$asset_file = include CONTENT_MODEL_PLUGIN_PATH . 'includes/manager/dist/fields-ui.asset.php';
252-
253-
wp_register_script(
254-
'data-types/fields-ui',
255-
CONTENT_MODEL_PLUGIN_URL . '/includes/manager/dist/fields-ui.js',
256-
$asset_file['dependencies'],
257-
$asset_file['version'],
258-
true
259-
);
260-
261-
wp_localize_script(
262-
'data-types/fields-ui',
263-
'contentModelFields',
264-
array(
265-
'postType' => Content_Model_Manager::POST_TYPE_NAME,
266-
)
267-
);
268-
269-
wp_enqueue_script( 'data-types/fields-ui' );
270-
}
271-
);
272-
}
273-
274-
/**
275-
* Conditionally enqueues the content model length restrictor script for the block editor.
276-
*
277-
* Checks if the current post is of the correct type before enqueueing the script.
278-
*
279-
* @return void
280-
*/
281-
private function maybe_enqueue_content_model_length_restrictor() {
282-
add_action(
283-
'enqueue_block_editor_assets',
284-
function () {
285-
global $post;
286-
287-
if ( ! $post || Content_Model_Manager::POST_TYPE_NAME !== $post->post_type ) {
288-
return;
289-
}
290-
291-
$content_model_length_restrictor_js = include CONTENT_MODEL_PLUGIN_PATH . '/includes/manager/dist/content-model-title-length-restrictor.asset.php';
292-
293-
wp_enqueue_script(
294-
'content-model/length-restrictor',
295-
CONTENT_MODEL_PLUGIN_URL . '/includes/manager/dist/content-model-title-length-restrictor.js',
296-
$content_model_length_restrictor_js['dependencies'],
297-
$content_model_length_restrictor_js['version'],
298-
true
299-
);
300-
}
162+
wp_enqueue_script(
163+
'content-model/manager',
164+
CONTENT_MODEL_PLUGIN_URL . '/includes/manager/dist/manager.js',
165+
$asset_file['dependencies'],
166+
$asset_file['version'],
167+
true
301168
);
302-
}
303169

304-
private function maybe_enqueue_the_defaut_value_placeholder() {
305-
add_action(
306-
'enqueue_block_editor_assets',
307-
function () {
308-
global $post;
309-
310-
if ( ! $post || Content_Model_Manager::POST_TYPE_NAME !== $post->post_type ) {
311-
return;
312-
}
313-
314-
$content_model_length_restrictor_js = include CONTENT_MODEL_PLUGIN_PATH . '/includes/manager/dist/default-value-placeholder-changer.asset.php';
315-
316-
wp_enqueue_script(
317-
'content-model/default-value-placeholder-changer',
318-
CONTENT_MODEL_PLUGIN_URL . '/includes/manager/dist/default-value-placeholder-changer.js',
319-
$content_model_length_restrictor_js['dependencies'],
320-
$content_model_length_restrictor_js['version'],
321-
true
322-
);
323-
}
170+
wp_localize_script(
171+
'content-model/manager',
172+
'contentModelData',
173+
array(
174+
'BINDINGS_KEY' => self::BINDINGS_KEY,
175+
'BLOCK_VARIATION_NAME_ATTR' => Content_Model_Block::BLOCK_VARIATION_NAME_ATTR,
176+
'POST_TYPE_NAME' => Content_Model_Manager::POST_TYPE_NAME,
177+
)
324178
);
325179
}
326180

includes/manager/manager.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { registerAttributeBinder } from './src/register-attribute-binder';
2+
import { registerContentModelLengthRestrictor } from './src/register-content-model-title-length-restrictor';
3+
import { registerCPTSettingsPanel } from './src/register-cpt-settings-panel';
4+
import { registerDefaultValuePlaceholderChanger } from './src/register-default-value-placeholder-changer';
5+
import { registerFieldsUI } from './src/register-fields-ui';
6+
7+
registerAttributeBinder();
8+
registerCPTSettingsPanel();
9+
registerFieldsUI();
10+
registerContentModelLengthRestrictor();
11+
registerDefaultValuePlaceholderChanger();

0 commit comments

Comments
 (0)