Refactors gutenberg blocs rgistration strategy. #560.

This commit is contained in:
mateuswetah 2021-06-09 13:54:11 -03:00
parent 8a873c58d6
commit 6a350d54e4
4 changed files with 94 additions and 320 deletions

View File

@ -2,6 +2,19 @@
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// Blocks that to not have a theme script, render the same content always
const TAINACAN_BLOCKS = [
'items-list' => [],
'collections-list' => [],
'search-bar' => [ 'has_theme_script' => true ],
'facets-list' => [ 'has_theme_script' => true ],
'dynamic-items-list' => [ 'has_theme_script' => true ],
'carousel-items-list' => [ 'has_theme_script' => true ],
'carousel-terms-list' => [ 'has_theme_script' => true ],
'carousel-collections-list' => [ 'has_theme_script' => true ],
'terms-list' => [ 'extra_editor_script_deps' => array('undescore') ],
];
tainacan_blocks_initialize();
function tainacan_blocks_initialize() {
@ -20,75 +33,58 @@ function tainacan_blocks_initialize() {
function register_tainacan_blocks_add_gutenberg_blocks() {
tainacan_blocks_get_common_styles();
tainacan_blocks_register_category_icon();
tainacan_blocks_register_tainacan_terms_list();
tainacan_blocks_register_tainacan_search_bar();
tainacan_blocks_register_tainacan_items_list();
tainacan_blocks_register_tainacan_facets_list();
tainacan_blocks_register_tainacan_faceted_search();
tainacan_blocks_register_tainacan_collections_list();
tainacan_blocks_register_tainacan_dynamic_items_list();
tainacan_blocks_register_tainacan_carousel_items_list();
tainacan_blocks_register_tainacan_carousel_terms_list();
foreach(TAINACAN_BLOCKS as $block_slug => $block_options) {
tainacan_blocks_register_block($block_slug, $block_options);
}
tainacan_blocks_register_tainacan_item_submission_form();
tainacan_blocks_register_tainacan_carousel_collections_list();
tainacan_blocks_register_tainacan_faceted_search();
}
function unregister_tainacan_blocks() {
global $post;
foreach(TAINACAN_BLOCKS as $block_slug => $block_options) {
if ( !has_block('tainacan/' . $block_slug) || !is_singular() ) {
wp_deregister_script($block_slug);
wp_deregister_style($block_slug);
if ( isset($block_options['has_theme_script']) && $block_options['has_theme_script'] )
wp_deregister_script($block_slug . '-theme');
}
}
if(!$post) return;
$not_allowed = apply_filters('posts-names-to-unregister-tainacan-blocks', []);
$current_page = $post->post_name;
if ( in_array($current_page, $not_allowed) ) {
wp_deregister_script('items-list');
wp_deregister_script('terms-list');
wp_deregister_script('facets-list');
wp_deregister_script('search-bar');
foreach(TAINACAN_BLOCKS as $block_slug => $block_options) {
wp_deregister_script($block_slug);
wp_deregister_style($block_slug);
if ( isset($block_options['has_theme_script']) && $block_options['has_theme_script'] )
wp_deregister_script($block_slug . '-theme');
if (function_exists('unregister_block_type'))
unregister_block_type('tainacan/' . $block_slug);
}
wp_deregister_script('faceted-search');
wp_deregister_script('tainacan-search');
wp_deregister_script('collections-list');
wp_deregister_script('facets-list-theme');
wp_deregister_script('dynamic-items-list');
wp_deregister_script('carousel-items-list');
wp_deregister_script('carousel-terms-list');
wp_deregister_script('item-submission-form');
wp_deregister_script('google-recaptcha-script');
wp_deregister_script('search-bar-theme-script');
wp_deregister_script('tainacan-item-submission');
wp_deregister_script('dynamic-items-list-theme');
wp_deregister_script('carousel-collections-list');
wp_deregister_script('carousel-items-list-theme');
wp_deregister_script('carousel-terms-list-theme');
wp_deregister_script('carousel-collections-list-theme');
wp_deregister_script('tainacan-blocks-register-category-icon');
wp_deregister_style('terms-list');
wp_deregister_style('items-list');
wp_deregister_style('search-bar');
wp_deregister_style('facets-list');
wp_deregister_style('faceted-search');
wp_deregister_style('collections-list');
wp_deregister_style('dynamic-items-list');
wp_deregister_style('carousel-items-list');
wp_deregister_style('carousel-terms-list');
wp_deregister_style('item-submission-form');
wp_deregister_style('carousel-collections-list');
wp_deregister_style('tainacan-blocks-common-styles');
wp_deregister_style('tainacan-blocks-register-category-icon');
if (function_exists('unregister_block_type')) {
unregister_block_type('tainacan/terms-list');
unregister_block_type('tainacan/items-list');
unregister_block_type('tainacan/search-bar');
unregister_block_type('tainacan/facets-list');
unregister_block_type('tainacan/faceted-search');
unregister_block_type('tainacan/collections-list');
unregister_block_type('tainacan/dynamic-items-list');
unregister_block_type('tainacan/carousel-items-list');
unregister_block_type('tainacan/carousel-terms-list');
unregister_block_type('tainacan/item-submission-form');
unregister_block_type('tainacan/carousel-collections-list');
}
}
}
@ -106,118 +102,52 @@ function tainacan_blocks_register_categories($categories, $post){
);
}
function tainacan_blocks_register_tainacan_terms_list(){
/*
* Registers a 'generic' Tainacan Block, according to the TAINACAN_BLOCKs array
*
*/
function tainacan_blocks_register_block($block_slug, $options = []) {
global $TAINACAN_BASE_URL;
// Creates Register params based on registered scripts and styles
$register_params = [];
// Defines dependencies for editor script
$editor_script_deps = array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor');
if ( isset($options['extra_editor_script_deps']) )
array_merge($editor_script_deps, $options['extra_editor_script_deps']);
// Registers Editor Script
wp_register_script(
'terms-list',
$TAINACAN_BASE_URL . '/assets/js/block_terms_list.js',
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor', 'underscore')
$block_slug,
$TAINACAN_BASE_URL . '/assets/js/block_' . str_replace('-', '_' , $block_slug) . '.js',
$editor_script_deps
);
wp_set_script_translations($block_slug, 'tainacan');
$register_params['editor_script'] = $block_slug;
wp_set_script_translations('terms-list', 'tainacan');
wp_register_style(
'terms-list',
$TAINACAN_BASE_URL . '/assets/css/tainacan-gutenberg-block-terms-list.css',
array('wp-edit-blocks', 'tainacan-blocks-common-styles')
);
if (function_exists('register_block_type')) {
register_block_type( 'tainacan/terms-list', array(
'editor_script' => 'terms-list',
'style' => 'terms-list'
) );
}
}
function tainacan_blocks_register_tainacan_facets_list(){
global $TAINACAN_BASE_URL;
// Registers Theme-side Script, if necessary
if ( isset($options['has_theme_script']) && $options['has_theme_script'] ) {
wp_enqueue_script(
'facets-list-theme',
$TAINACAN_BASE_URL . '/assets/js/block_facets_list_theme.js',
array('wp-components')
$block_slug . '-theme',
$TAINACAN_BASE_URL . '/assets/js/block_' . str_replace('-', '_' , $block_slug) . '_theme.js',
array('wp-i18n')
);
wp_set_script_translations('facets-list-theme', 'tainacan');
wp_register_script(
'facets-list',
$TAINACAN_BASE_URL . '/assets/js/block_facets_list.js',
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor')
);
wp_set_script_translations('facets-list', 'tainacan');
wp_set_script_translations($block_slug . '-theme', 'tainacan');
$register_params['script'] = $block_slug;
}
// Registers style (works for both editor and theme side)
wp_register_style(
'facets-list',
$TAINACAN_BASE_URL . '/assets/css/tainacan-gutenberg-block-facets-list.css',
$block_slug,
$TAINACAN_BASE_URL . '/assets/css/tainacan-gutenberg-block-' . $block_slug . '.css',
array('wp-edit-blocks', 'tainacan-blocks-common-styles')
);
$register_params['style'] = $block_slug;
if (function_exists('register_block_type')) {
register_block_type( 'tainacan/facets-list', array(
'editor_script' => 'facets-list',
'style' => 'facets-list',
'script' => 'facets-list-theme'
) );
}
}
function tainacan_blocks_register_tainacan_items_list(){
global $TAINACAN_BASE_URL;
wp_register_script(
'items-list',
$TAINACAN_BASE_URL . '/assets/js/block_items_list.js',
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor')
);
wp_register_style(
'items-list',
$TAINACAN_BASE_URL . '/assets/css/tainacan-gutenberg-block-items-list.css',
array('wp-edit-blocks', 'tainacan-blocks-common-styles')
);
wp_set_script_translations('items-list', 'tainacan');
if (function_exists('register_block_type')) {
register_block_type( 'tainacan/items-list', array(
'editor_script' => 'items-list',
'style' => 'items-list'
) );
}
}
function tainacan_blocks_register_tainacan_dynamic_items_list(){
global $TAINACAN_BASE_URL;
wp_enqueue_script(
'dynamic-items-list-theme',
$TAINACAN_BASE_URL . '/assets/js/block_dynamic_items_list_theme.js',
array('wp-components', 'wp-i18n')
);
wp_set_script_translations('dynamic-items-list-theme', 'tainacan');
wp_register_script(
'dynamic-items-list',
$TAINACAN_BASE_URL . '/assets/js/block_dynamic_items_list.js',
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor')
);
wp_set_script_translations('dynamic-items-list', 'tainacan');
wp_register_style(
'dynamic-items-list',
$TAINACAN_BASE_URL . '/assets/css/tainacan-gutenberg-block-dynamic-items-list.css',
array('wp-edit-blocks', 'tainacan-blocks-common-styles')
);
if (function_exists('register_block_type')) {
register_block_type( 'tainacan/dynamic-items-list', array(
'editor_script' => 'dynamic-items-list',
'style' => 'dynamic-items-list',
'script' => 'dynamic-items-list-theme'
) );
}
// Registers the new block
if (function_exists('register_block_type'))
register_block_type( 'tainacan/' . $block_slug, $register_params );
}
function tainacan_blocks_register_tainacan_item_submission_form() {
@ -299,158 +229,6 @@ function tainacan_blocks_register_tainacan_faceted_search(){
}
}
function tainacan_blocks_register_tainacan_carousel_items_list(){
global $TAINACAN_BASE_URL;
wp_enqueue_script(
'carousel-items-list-theme',
$TAINACAN_BASE_URL . '/assets/js/block_carousel_items_list_theme.js',
array('wp-components')
);
wp_set_script_translations('carousel-items-list-theme', 'tainacan');
wp_register_script(
'carousel-items-list',
$TAINACAN_BASE_URL . '/assets/js/block_carousel_items_list.js',
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor')
);
wp_set_script_translations('carousel-items-list', 'tainacan');
wp_register_style(
'carousel-items-list',
$TAINACAN_BASE_URL . '/assets/css/tainacan-gutenberg-block-carousel-items-list.css',
array('wp-edit-blocks', 'tainacan-blocks-common-styles')
);
if (function_exists('register_block_type')) {
register_block_type( 'tainacan/carousel-items-list', array(
'editor_script' => 'carousel-items-list',
'style' => 'carousel-items-list',
'script' => 'carousel-items-list-theme'
) );
}
}
function tainacan_blocks_register_tainacan_carousel_terms_list(){
global $TAINACAN_BASE_URL;
wp_enqueue_script(
'carousel-terms-list-theme',
$TAINACAN_BASE_URL . '/assets/js/block_carousel_terms_list_theme.js',
array('wp-components')
);
wp_set_script_translations('carousel-terms-list-theme', 'tainacan');
wp_register_script(
'carousel-terms-list',
$TAINACAN_BASE_URL . '/assets/js/block_carousel_terms_list.js',
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor')
);
wp_set_script_translations('carousel-terms-list', 'tainacan');
wp_register_style(
'carousel-terms-list',
$TAINACAN_BASE_URL . '/assets/css/tainacan-gutenberg-block-carousel-terms-list.css',
array('wp-edit-blocks', 'tainacan-blocks-common-styles')
);
if (function_exists('register_block_type')) {
register_block_type( 'tainacan/carousel-terms-list', array(
'editor_script' => 'carousel-terms-list',
'style' => 'carousel-terms-list',
'script' => 'carousel-terms-list-theme'
) );
}
}
function tainacan_blocks_register_tainacan_search_bar(){
global $TAINACAN_BASE_URL;
wp_enqueue_script(
'search-bar-theme-script',
$TAINACAN_BASE_URL . '/assets/js/block_search_bar_script.js',
array('wp-components')
);
wp_register_script(
'search-bar',
$TAINACAN_BASE_URL . '/assets/js/block_search_bar.js',
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor')
);
wp_set_script_translations('search-bar-list', 'tainacan');
wp_register_style(
'search-bar',
$TAINACAN_BASE_URL . '/assets/css/tainacan-gutenberg-block-search-bar.css',
array('wp-edit-blocks', 'tainacan-blocks-common-styles')
);
if (function_exists('register_block_type')) {
register_block_type( 'tainacan/search-bar', array(
'editor_script' => 'search-bar',
'style' => 'search-bar'
) );
}
}
function tainacan_blocks_register_tainacan_collections_list(){
global $TAINACAN_BASE_URL;
wp_register_script(
'collections-list',
$TAINACAN_BASE_URL . '/assets/js/block_collections_list.js',
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor')
);
wp_register_style(
'collections-list',
$TAINACAN_BASE_URL . '/assets/css/tainacan-gutenberg-block-collections-list.css',
array('wp-edit-blocks', 'tainacan-blocks-common-styles')
);
wp_set_script_translations('collections-list', 'tainacan');
if (function_exists('register_block_type')) {
register_block_type( 'tainacan/collections-list', array(
'editor_script' => 'collections-list',
'style' => 'collections-list'
) );
}
}
function tainacan_blocks_register_tainacan_carousel_collections_list(){
global $TAINACAN_BASE_URL;
wp_enqueue_script(
'carousel-collections-list-theme',
$TAINACAN_BASE_URL . '/assets/js/block_carousel_collections_list_theme.js',
array('wp-components')
);
wp_set_script_translations('carousel-collections-list-theme', 'tainacan');
wp_register_script(
'carousel-collections-list',
$TAINACAN_BASE_URL . '/assets/js/block_carousel_collections_list.js',
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor')
);
wp_set_script_translations('carousel-collections-list', 'tainacan');
wp_register_style(
'carousel-collections-list',
$TAINACAN_BASE_URL . '/assets/css/tainacan-gutenberg-block-carousel-collections-list.css',
array('wp-edit-blocks', 'tainacan-blocks-common-styles')
);
if (function_exists('register_block_type')) {
register_block_type( 'tainacan/carousel-collections-list', array(
'editor_script' => 'carousel-collections-list',
'style' => 'carousel-collections-list',
'script' => 'carousel-collections-list-theme'
) );
}
}
function tainacan_blocks_get_plugin_js_settings(){
global $TAINACAN_BASE_URL;
global $wp_version;
@ -471,16 +249,12 @@ function tainacan_blocks_get_plugin_js_settings(){
function tainacan_blocks_add_plugin_settings() {
$settings = tainacan_blocks_get_plugin_js_settings();
wp_localize_script( 'terms-list', 'tainacan_blocks', $settings );
wp_localize_script( 'items-list', 'tainacan_blocks', $settings );
wp_localize_script( 'dynamic-items-list', 'tainacan_blocks', $settings );
wp_localize_script( 'carousel-items-list', 'tainacan_blocks', $settings );
wp_localize_script( 'carousel-terms-list', 'tainacan_blocks', $settings );
wp_localize_script( 'search-bar', 'tainacan_blocks', $settings );
foreach(TAINACAN_BLOCKS as $block_slug => $block_options) {
wp_localize_script( $block_slug, 'tainacan_blocks', $settings );
}
// The faceded search block also uses this settings for checking gutenberg version
wp_localize_script( 'faceted-search', 'tainacan_blocks', $settings );
wp_localize_script( 'collections-list', 'tainacan_blocks', $settings );
wp_localize_script( 'carousel-collections-list', 'tainacan_blocks', $settings );
wp_localize_script( 'facets-list', 'tainacan_blocks', $settings );
}
function tainacan_blocks_add_plugin_admin_settings() {

View File

@ -3,47 +3,47 @@
var path4 = el( 'path', {
d: 'm 11.27478,8.73457 c 1.688519,0.44936 3.333166,1.08401 4.86423,1.92899 C 17.304261,9.35093 18.355283,7.77611 18.494128,5.97784 18.606604,4.68205 18.01681,3.38652 17.0326,2.55334 16.089243,1.69008 14.73247,1.35339 13.487742,1.61188 11.913591,1.91741 10.544771,2.85442 9.3686674,3.90688 10.203337,5.42539 10.830349,7.06078 11.27478,8.73457 Z',
fill: '#a5cdd7',
'stroke-width': '0.10421441'
'strokeWidth': '0.10421441'
} );
var path6 = el( 'path', {
d: 'm 3.5149247,13.04091 c -0.5624592,1.90814 -0.6353706,3.39919 -0.6353706,4.12908 0.7186979,0 2.2185893,-0.073 4.1351172,-0.63604 C 6.4105484,16.06473 5.7855936,15.5121 5.1606389,14.86563 4.5148523,14.24001 3.9732249,13.62482 3.5149247,13.04091 Z',
fill: '#7eb8c5',
'stroke-width': '0.10421441'
'strokeWidth': '0.10421441'
} );
var path8 = el( 'path', {
d: 'M 5.8168414,8.09852 C 6.8215597,6.56512 8.0188478,5.1569 9.3686674,3.91731 8.5297232,2.4271 7.4026813,0.96168 5.7810893,0.28584 4.58159,-0.22797 3.1675523,-0.04964 2.0663954,0.62209 0.92867872,1.26113 0.19971131,2.4895 0.06863414,3.77612 -0.13858888,5.62222 0.46606606,7.44579 1.23384,9.09951 1.0651187,8.75778 1.7833517,8.82097 2.0003231,8.67455 3.2414236,8.32179 4.5283868,8.14061 5.8168414,8.09852 Z',
fill: '#a5cdd7',
'stroke-width': '0.10421441'
'strokeWidth': '0.10421441'
} );
var path10 = el( 'path', {
d: 'm 16.13901,10.66356 c -1.249149,1.3653 -2.67355,2.5697 -4.218445,3.58688 -0.06202,1.5577 -0.310603,3.11914 -0.843688,4.58787 -0.37182,-0.15288 0.329219,0.15846 0.448759,0.20875 1.660074,0.69791 3.525206,1.21087 5.323239,0.78852 1.262782,-0.28388 2.330378,-1.22057 2.820096,-2.41411 0.535186,-1.16136 0.465064,-2.56242 -0.169187,-3.67073 -0.750803,-1.35882 -2.035466,-2.33364 -3.360774,-3.08718 z',
fill: '#a5cdd7',
'stroke-width': '0.10421441'
'strokeWidth': '0.10421441'
} );
var path12 = el( 'path', {
d: 'm 1.1817604,8.94311 c 0.5559379,1.47805 1.3636245,2.85447 2.3331643,4.0978 0.5011915,-1.75438 1.3032057,-3.41792 2.3019167,-4.94239 -1.5733879,0.0545 -3.1521005,0.3044 -4.635081,0.84459 z',
fill: '#7eb8c5',
'stroke-width': '0.10421441'
'strokeWidth': '0.10421441'
} );
var path14 = el( 'path', {
d: 'M 11.27478,8.73457 C 9.4936359,8.26776 7.6479688,8.03766 5.8064254,8.0881 4.8150842,9.6169 4.0070258,11.27568 3.5045088,13.03048 c 0.884194,1.1273 1.9090682,2.14007 2.990078,3.07711 0.2863716,0.30027 0.6037275,0.52578 1.000007,0.26756 1.5571457,-0.50924 3.0386822,-1.23846 4.4155572,-2.12471 0.07412,-1.8572 -0.162523,-3.72061 -0.635371,-5.51587 z',
fill: '#57a2b1',
'stroke-width': '0.10421441'
'strokeWidth': '0.10421441'
} );
var path16 = el( 'path', {
d: 'M 11.27478,8.73457 C 10.962303,7.53547 10.514417,6.26338 9.8894631,4.9183 9.7228085,4.56378 9.545738,4.21969 9.3686674,3.90688 9.1603492,4.09457 8.9624469,4.29268 8.7541286,4.49079 7.5458828,5.70032 6.577203,6.92028 5.8168414,8.0881 7.3583964,8.0464 9.2020128,8.19237 11.27478,8.73457 Z',
fill: '#7eb8c5',
'stroke-width': '0.10421441'
'strokeWidth': '0.10421441'
} );
var path18 = el( 'path', {
d: 'm 11.920565,14.25044 c -1.514496,0.987 -3.1645362,1.78138 -4.9058937,2.27308 1.2327086,0.96216 2.5971002,1.76354 4.0622057,2.31479 0.522219,-1.47166 0.784125,-3.02968 0.843688,-4.58787 z',
fill: '#7eb8c5',
'stroke-width': '0.10421441'
'strokeWidth': '0.10421441'
} );
var path20 = el( 'path', {
d: 'm 15.545303,11.28918 c 0.17414,-0.20567 0.492354,-0.44146 0.562428,-0.64318 -1.504933,-0.82786 -3.118912,-1.45272 -4.776742,-1.89667 -0.114463,0.006 0.05623,0.30009 0.05428,0.4239 0.398631,1.65875 0.593032,3.37096 0.524879,5.07721 1.318664,-0.84526 2.528852,-1.85373 3.635153,-2.96126 z',
fill: '#7eb8c5',
'stroke-width': '0.10421441'
'strokeWidth': '0.10421441'
} );
var g1 = el('g', { transform: 'translate(-85.490815,-209.14412)' });
var g2 = el('g', { transform: 'translate(-85.490815,-209.14412)' });

View File

@ -22,7 +22,7 @@ module.exports = {
block_carousel_items_list_theme: './src/views/gutenberg-blocks/tainacan-items/carousel-items-list/carousel-items-list-theme.js',
block_search_bar: './src/views/gutenberg-blocks/tainacan-items/search-bar/index.js',
block_search_bar_script: './src/views/gutenberg-blocks/tainacan-items/search-bar/search-bar-theme-script.js',
block_search_bar_theme: './src/views/gutenberg-blocks/tainacan-items/search-bar/search-bar-theme.js',
block_collections_list: './src/views/gutenberg-blocks/tainacan-collections/collections-list/index.js',