| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') || exit; |
| 4 |
|
| 5 |
class Blockspare_TB_Assets |
| 6 |
{ |
| 7 |
|
| 8 |
|
| 9 |
public function blocksspare_builder_enqueue($hook_suffix) |
| 10 |
{ |
| 11 |
|
| 12 |
$expected_hook = get_plugin_page_hookname(Blockspare_TB_Admin_Menu::PAGE_SLUG, 'blockspare'); |
| 13 |
|
| 14 |
if ($hook_suffix !== $expected_hook) { |
| 15 |
return; |
| 16 |
} // Hide WordPress admin notices on Site Builder page. |
| 17 |
remove_all_actions('admin_notices'); |
| 18 |
remove_all_actions('all_admin_notices'); |
| 19 |
|
| 20 |
|
| 21 |
$this->enqueue_bundle('blocksparetb-admin', 'builder'); |
| 22 |
// Check if the user is a Pro user |
| 23 |
$is_pro = apply_filters('blockspare_is_pro_user', false); |
| 24 |
wp_localize_script( |
| 25 |
'blocksparetb-admin', |
| 26 |
'stbldrData', |
| 27 |
array( |
| 28 |
'restUrl' => esc_url_raw(rest_url('wp/v2/blocksparetb-templates')), |
| 29 |
'restRootUrl' => esc_url_raw(rest_url('blocksparetb/v1')), |
| 30 |
'nonce' => wp_create_nonce('wp_rest'), |
| 31 |
'types' => $this->get_type_terms(), |
| 32 |
'adminUrl' => esc_url_raw(admin_url('post.php')), |
| 33 |
'sitebarIcon' => BLOCKSPARE_PLUGIN_URL . 'assets/icons', |
| 34 |
'isPro' => $is_pro, |
| 35 |
'proLink' => admin_url('admin.php?page=blockspare-pricing'), |
| 36 |
) |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
private function get_type_terms() |
| 41 |
{ |
| 42 |
$types = array(); |
| 43 |
|
| 44 |
foreach (Blockspare_TB_Template_Type_Taxonomy::TYPES as $slug => $label) { |
| 45 |
$slug = (string) $slug; // See the matching note in Blockspare_TB_Template_Type_Taxonomy::seed_terms(). |
| 46 |
$term = get_term_by('slug', $slug, Blockspare_TB_Template_Type_Taxonomy::SLUG); |
| 47 |
|
| 48 |
$types[$slug] = array( |
| 49 |
'id' => $term ? (int) $term->term_id : 0, |
| 50 |
'label' => $label, |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
return $types; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
public function blocksspare_builder_enqueue_editor() |
| 59 |
{ |
| 60 |
$screen = get_current_screen(); |
| 61 |
|
| 62 |
if (! $screen || Blockspare_TB_Template_Post_Type::SLUG !== $screen->post_type) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$this->enqueue_bundle('blocksparetb-editor', 'builder_editor'); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
private function enqueue_bundle($handle, $bundle) |
| 71 |
{ |
| 72 |
|
| 73 |
$js_path = BLOCKSPARE_PLUGIN_DIR . "dist/{$bundle}.js"; |
| 74 |
|
| 75 |
if (! file_exists($js_path)) { |
| 76 |
add_action( |
| 77 |
'admin_notices', |
| 78 |
function () use ($bundle) { |
| 79 |
printf( |
| 80 |
'<div class="notice notice-error"><p>%s</p></div>', |
| 81 |
esc_html( |
| 82 |
sprintf( |
| 83 |
/* translators: %s: bundle name, e.g. "builder" or "builder_editor" */ |
| 84 |
__('Blockspare Site Builder: run `npm install && npm run build` in the plugin directory to compile the "%s" bundle.', 'blockspare'), |
| 85 |
$bundle |
| 86 |
) |
| 87 |
) |
| 88 |
); |
| 89 |
} |
| 90 |
); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$version = (string) filemtime($js_path); |
| 95 |
|
| 96 |
wp_enqueue_script( |
| 97 |
$handle, |
| 98 |
BLOCKSPARE_PLUGIN_URL . "dist/{$bundle}.js", |
| 99 |
$this->get_bundle_dependencies($bundle), |
| 100 |
$version, |
| 101 |
true |
| 102 |
); |
| 103 |
|
| 104 |
$css_path = BLOCKSPARE_PLUGIN_DIR . "dist/{$bundle}.css"; |
| 105 |
|
| 106 |
if (file_exists($css_path)) { |
| 107 |
wp_enqueue_style( |
| 108 |
$handle, |
| 109 |
BLOCKSPARE_PLUGIN_URL . "dist/{$bundle}.css", |
| 110 |
array('wp-components'), |
| 111 |
(string) filemtime($css_path) |
| 112 |
); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
private function get_bundle_dependencies($bundle) |
| 117 |
{ |
| 118 |
$common = array( |
| 119 |
'wp-element', |
| 120 |
'wp-components', |
| 121 |
'wp-data', |
| 122 |
'wp-i18n', |
| 123 |
'wp-api-fetch', |
| 124 |
'wp-url', |
| 125 |
'wp-compose', |
| 126 |
'wp-hooks', |
| 127 |
); |
| 128 |
|
| 129 |
if ('builder_editor' === $bundle) { |
| 130 |
// The editor panel bundle additionally needs the block-editor |
| 131 |
// integration packages that the dashboard bundle never touches. |
| 132 |
return array_merge( |
| 133 |
$common, |
| 134 |
array( |
| 135 |
'wp-plugins', |
| 136 |
'wp-edit-post', |
| 137 |
'wp-editor', |
| 138 |
'wp-block-editor', |
| 139 |
'wp-blocks', |
| 140 |
'wp-core-data', |
| 141 |
'wp-notices', |
| 142 |
) |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
// 'builder' (the standalone dashboard) also talks to the REST |
| 147 |
// API for CRUD on templates, so it needs wp-core-data too. |
| 148 |
return array_merge($common, array('wp-core-data')); |
| 149 |
} |
| 150 |
} |
| 151 |
|