From b53cc0d0e5701220eb49576b84feb2b118c36770 Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Sun, 15 Jan 2023 22:12:53 -0300 Subject: [PATCH 1/3] Adds query loop block variations. #764. --- .../class-tainacan-gutenberg-block.php | 29 ++++++++++- .../js/tainacan-blocks-query-variations.js | 48 +++++++++++++++++++ webpack.common.js | 1 + 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js diff --git a/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php b/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php index f9fc0ee31..1c6a84d2f 100644 --- a/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php +++ b/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php @@ -82,6 +82,7 @@ function tainacan_blocks_register_and_enqueue_all_blocks() { if ( is_admin() ) { tainacan_blocks_get_category_icon_script(); tainacan_blocks_get_common_editor_styles(); + tainacan_blocks_get_variations_script(); } // May be needed outside the editor, if server side render is used foreach(TAINACAN_BLOCKS as $block_slug => $block_options) { @@ -199,6 +200,12 @@ function tainacan_blocks_get_plugin_js_settings(){ global $TAINACAN_BASE_URL; global $wp_version; + $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); + $collections = $Tainacan_Collections->fetch( [], 'OBJECT' ); + foreach ( $collections as $col ) { + $cpts[$col->get_db_identifier()] = $col->get_name(); + } + $settings = [ 'wp_version' => $wp_version, 'root' => esc_url_raw( rest_url() ) . 'tainacan/v2', @@ -206,7 +213,8 @@ function tainacan_blocks_get_plugin_js_settings(){ 'base_url' => $TAINACAN_BASE_URL, 'admin_url' => admin_url(), 'site_url' => site_url(), - 'theme_items_list_url' => esc_url_raw( get_site_url() ) . '/' . \Tainacan\Theme_Helper::get_instance()->get_items_list_slug() + 'theme_items_list_url' => esc_url_raw( get_site_url() ) . '/' . \Tainacan\Theme_Helper::get_instance()->get_items_list_slug(), + 'collections_post_types' => $cpts ]; return $settings; @@ -271,3 +279,22 @@ function tainacan_blocks_get_category_icon_script() { $TAINACAN_VERSION ); } + +/** + * Registers the script that inserts the Query Loop Block variations + */ +function tainacan_blocks_get_variations_script() { + global $TAINACAN_BASE_URL; + global $TAINACAN_VERSION; + + wp_enqueue_script( + 'tainacan-blocks-query-variations', + $TAINACAN_BASE_URL . '/assets/js/tainacan_blocks_query_variations.js', + array('wp-blocks', 'wp-components'), + $TAINACAN_VERSION + ); + + $block_settings = tainacan_blocks_get_plugin_js_settings(); + wp_localize_script( 'tainacan-blocks-query-variations', 'tainacan_blocks', $block_settings ); +} + diff --git a/src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js b/src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js new file mode 100644 index 000000000..306231fb7 --- /dev/null +++ b/src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js @@ -0,0 +1,48 @@ +const { registerBlockVariation } = wp.blocks; +const { __ } = wp.i18n; +import icon from '../blocks/items-list/icon'; + +const POST_TYPES = tainacan_blocks.collections_post_types; + +Object.keys(POST_TYPES).forEach((postType) => { + const postName = POST_TYPES[postType]; + const VARIATION_NAME = 'tainacan-items-' + postType; + + registerBlockVariation( 'core/query', { + name: VARIATION_NAME, + title: postName, + icon: icon, + description: __('Displays a list of Tainacan itens', 'tainacan'), + isActive: ( { namespace, query } ) => { + return ( + namespace === VARIATION_NAME + && query.postType === postType + ); + }, + attributes: { + category: 'tainacan-blocks', + namespace: VARIATION_NAME, + query: { + postType: postType, + perPage: 12, + offset: 0 + }, + align: 'wide', + displayLayout: { + type: 'flex', + columns: 4 + } + }, + allowedControls: [ 'inherit', 'order', 'taxQuery', 'search' ], + innerBlocks: [ + [ + 'core/post-template', + {}, + [ + [ 'core/post-featured-image' ], + [ 'core/post-title' ] + ], + ] + ] + } ); +}); diff --git a/webpack.common.js b/webpack.common.js index f8b008f6b..0a93f73eb 100644 --- a/webpack.common.js +++ b/webpack.common.js @@ -9,6 +9,7 @@ module.exports = { tainacan_pages_common_scripts: './src/views/tainacan-pages-common-scripts.js', tainacan_blocks_common_scripts: './src/views/gutenberg-blocks/tainacan-blocks-common-scripts.js', tainacan_blocks_category_icon: './src/views/gutenberg-blocks/js/tainacan-blocks-category-icon.js', + tainacan_blocks_query_variations: './src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js', block_terms_list: './src/views/gutenberg-blocks/blocks/terms-list/index.js', block_items_list: './src/views/gutenberg-blocks/blocks/items-list/index.js', From e623900c2a41e56f9388a463a8066287ed457bb1 Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Mon, 16 Jan 2023 10:41:26 -0300 Subject: [PATCH 2/3] Registers a separate category for query loop variations. #764. --- .../gutenberg-blocks/class-tainacan-gutenberg-block.php | 8 +++++++- .../js/tainacan-blocks-query-variations.js | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php b/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php index 1c6a84d2f..2caab30f5 100644 --- a/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php +++ b/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php @@ -67,7 +67,13 @@ function tainacan_blocks_register_categories($categories, $editor_context) { array( array( 'slug' => 'tainacan-blocks', - 'title' => __( 'Tainacan', 'tainacan' ), + 'title' => __( 'Tainacan Blocks', 'tainacan' ), + ), + ), + array( + array( + 'slug' => 'tainacan-blocks-variations', + 'title' => __( 'Tainacan Query Loop Variations', 'tainacan' ), ), ) ); diff --git a/src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js b/src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js index 306231fb7..2a1cca89f 100644 --- a/src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js +++ b/src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js @@ -20,7 +20,7 @@ Object.keys(POST_TYPES).forEach((postType) => { ); }, attributes: { - category: 'tainacan-blocks', + category: 'tainacan-blocks-variations', namespace: VARIATION_NAME, query: { postType: postType, From 93ea87cc7fdf7cbf507c9e9958c7b1dd9f7145fb Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Fri, 3 Feb 2023 10:22:57 -0300 Subject: [PATCH 3/3] Fixes new Blocks Category reference. #764. --- src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php | 2 -- .../gutenberg-blocks/js/tainacan-blocks-category-icon.js | 4 +++- .../gutenberg-blocks/js/tainacan-blocks-query-variations.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php b/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php index 2caab30f5..7bb040966 100644 --- a/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php +++ b/src/views/gutenberg-blocks/class-tainacan-gutenberg-block.php @@ -69,8 +69,6 @@ function tainacan_blocks_register_categories($categories, $editor_context) { 'slug' => 'tainacan-blocks', 'title' => __( 'Tainacan Blocks', 'tainacan' ), ), - ), - array( array( 'slug' => 'tainacan-blocks-variations', 'title' => __( 'Tainacan Query Loop Variations', 'tainacan' ), diff --git a/src/views/gutenberg-blocks/js/tainacan-blocks-category-icon.js b/src/views/gutenberg-blocks/js/tainacan-blocks-category-icon.js index 4fcd49f4e..6ae98f543 100644 --- a/src/views/gutenberg-blocks/js/tainacan-blocks-category-icon.js +++ b/src/views/gutenberg-blocks/js/tainacan-blocks-category-icon.js @@ -49,6 +49,8 @@ var g2 = el('g', { transform: 'translate(-85.490815,-209.14412)' }); var svgIcon = el( 'svg', { width: 20, height: 20, viewBox: '0 0 20 20'}, path4, path6, path8, path10, path12, path14, path16, path18, path20, g1, g2); - if (wp.blocks.updateCategory) + if (wp.blocks.updateCategory) { wp.blocks.updateCategory( 'tainacan-blocks', { icon: svgIcon } ); + wp.blocks.updateCategory( 'tainacan-blocks-variations', { icon: svgIcon } ); + } } )(); \ No newline at end of file diff --git a/src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js b/src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js index 2a1cca89f..35b1c49db 100644 --- a/src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js +++ b/src/views/gutenberg-blocks/js/tainacan-blocks-query-variations.js @@ -12,6 +12,7 @@ Object.keys(POST_TYPES).forEach((postType) => { name: VARIATION_NAME, title: postName, icon: icon, + category: 'tainacan-blocks-variations', description: __('Displays a list of Tainacan itens', 'tainacan'), isActive: ( { namespace, query } ) => { return ( @@ -20,7 +21,6 @@ Object.keys(POST_TYPES).forEach((postType) => { ); }, attributes: { - category: 'tainacan-blocks-variations', namespace: VARIATION_NAME, query: { postType: postType,