From d93845c7779e39ab79aaec59e3b5c4c01d2cff15 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Fri, 20 Mar 2026 17:09:49 +0100 Subject: [PATCH] REFACTOR Moving component from dynamiques-blocks to carhop-blocks --- .../carhop-blocks/src/explore-tags/block.json | 20 ++++ .../carhop-blocks/src/explore-tags/edit.js | 16 +++ .../src/explore-tags/editor.scss | 9 ++ .../carhop-blocks/src/explore-tags/index.js | 22 +++++ .../carhop-blocks/src/explore-tags/render.php | 99 +++++++++++++++++++ .../carhop-blocks/src/explore-tags/save.js | 24 +++++ .../carhop-blocks/src/explore-tags/style.scss | 12 +++ .../carhop-blocks/src/explore-tags/view.js | 0 8 files changed, 202 insertions(+) create mode 100644 plugins/carhop-blocks/src/explore-tags/block.json create mode 100644 plugins/carhop-blocks/src/explore-tags/edit.js create mode 100644 plugins/carhop-blocks/src/explore-tags/editor.scss create mode 100644 plugins/carhop-blocks/src/explore-tags/index.js create mode 100644 plugins/carhop-blocks/src/explore-tags/render.php create mode 100644 plugins/carhop-blocks/src/explore-tags/save.js create mode 100644 plugins/carhop-blocks/src/explore-tags/style.scss create mode 100644 plugins/carhop-blocks/src/explore-tags/view.js diff --git a/plugins/carhop-blocks/src/explore-tags/block.json b/plugins/carhop-blocks/src/explore-tags/block.json new file mode 100644 index 0000000..5c73e19 --- /dev/null +++ b/plugins/carhop-blocks/src/explore-tags/block.json @@ -0,0 +1,20 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "carhop-blocks/explore-tags", + "version": "0.1.0", + "title": "Explore Tags", + "category": "carhop-blocks", + "icon": "smiley", + "description": "Example block scaffolded with Create Block tool.", + "example": {}, + "supports": { + "html": false + }, + "textdomain": "carhop-blocks", + "editorScript": "file:./index.js", + "editorStyle": "file:./index.css", + "style": "file:./style-index.css", + "viewScript": "file:./view.js", + "render": "file:./render.php" +} \ No newline at end of file diff --git a/plugins/carhop-blocks/src/explore-tags/edit.js b/plugins/carhop-blocks/src/explore-tags/edit.js new file mode 100644 index 0000000..7447284 --- /dev/null +++ b/plugins/carhop-blocks/src/explore-tags/edit.js @@ -0,0 +1,16 @@ +import { __ } from "@wordpress/i18n"; +import { useBlockProps } from "@wordpress/block-editor"; +import ServerSideRender from "@wordpress/server-side-render"; + +import "./editor.scss"; + +export default function Edit(props) { + return ( +
+ +
+ ); +} diff --git a/plugins/carhop-blocks/src/explore-tags/editor.scss b/plugins/carhop-blocks/src/explore-tags/editor.scss new file mode 100644 index 0000000..9535bd5 --- /dev/null +++ b/plugins/carhop-blocks/src/explore-tags/editor.scss @@ -0,0 +1,9 @@ +/** + * The following styles get applied inside the editor only. + * + * Replace them with your own styles or remove the file completely. + */ + +.wp-block-create-block-explore-tags { + border: 1px dotted #f00; +} diff --git a/plugins/carhop-blocks/src/explore-tags/index.js b/plugins/carhop-blocks/src/explore-tags/index.js new file mode 100644 index 0000000..2a7fe7d --- /dev/null +++ b/plugins/carhop-blocks/src/explore-tags/index.js @@ -0,0 +1,22 @@ +import { registerBlockType } from "@wordpress/blocks"; +import "./style.scss"; + +import Edit from "./edit"; +import save from "./save"; +import metadata from "./block.json"; + +registerBlockType(metadata.name, { + icon: { + src: ( + + + + + + + ), + foreground: "#136f63", + }, + edit: Edit, + save, +}); diff --git a/plugins/carhop-blocks/src/explore-tags/render.php b/plugins/carhop-blocks/src/explore-tags/render.php new file mode 100644 index 0000000..9c30e39 --- /dev/null +++ b/plugins/carhop-blocks/src/explore-tags/render.php @@ -0,0 +1,99 @@ +object_type ?? []; + } + + // Single ou archive d'un post type + if (is_singular()) { + return [get_post_type()]; + } + if (is_post_type_archive()) { + $post_type = get_query_var('post_type'); + return is_array($post_type) ? $post_type : [$post_type]; + } + + return []; + } +} + +if (!function_exists('explore_tags_get_post_ids')) { + function explore_tags_get_post_ids($post_types, $limit = 5000) + { + if (empty($post_types)) { + return []; + } + + return get_posts([ + 'post_type' => $post_types, + 'post_status' => 'publish', + 'fields' => 'ids', + 'posts_per_page' => $limit, + 'no_found_rows' => true, + ]); + } +} + +if (!function_exists('explore_tags_get_tags_url')) { + function explore_tags_get_tags_url($tag) + { + $current_blog_id = get_current_blog_id(); + if ($current_blog_id === 2) { + return add_query_arg('etiquette', $tag->slug, get_post_type_archive_link('articles')); + } + if (is_page()) { + $link = get_term_link($tag); + return !is_wp_error($link) ? $link : '#'; + } + + return add_query_arg('etiquette', $tag->slug, get_post_type_archive_link(get_post_type())); + } +} + +$post_types = explore_tags_get_post_types(); +$args = [ + 'taxonomy' => 'etiquettes', + 'hide_empty' => false, +]; + +if (!empty($post_types)) { + $post_ids = explore_tags_get_post_ids($post_types); + if (!empty($post_ids)) { + $args['object_ids'] = $post_ids; + } +} + +$tags = get_terms($args); + +?> + +
+
+
+

Tags

+

Explorer
par Tags

+
+ +
+
\ No newline at end of file diff --git a/plugins/carhop-blocks/src/explore-tags/save.js b/plugins/carhop-blocks/src/explore-tags/save.js new file mode 100644 index 0000000..d7cdd20 --- /dev/null +++ b/plugins/carhop-blocks/src/explore-tags/save.js @@ -0,0 +1,24 @@ +/** + * React hook that is used to mark the block wrapper element. + * It provides all the necessary props like the class name. + * + * @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops + */ +import { useBlockProps } from '@wordpress/block-editor'; + +/** + * The save function defines the way in which the different attributes should + * be combined into the final markup, which is then serialized by the block + * editor into `post_content`. + * + * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#save + * + * @return {Element} Element to render. + */ +export default function save() { + return ( +

+ { 'Explore Tags – hello from the saved content!' } +

+ ); +} diff --git a/plugins/carhop-blocks/src/explore-tags/style.scss b/plugins/carhop-blocks/src/explore-tags/style.scss new file mode 100644 index 0000000..b54d53e --- /dev/null +++ b/plugins/carhop-blocks/src/explore-tags/style.scss @@ -0,0 +1,12 @@ +/** + * The following styles get applied both on the front of your site + * and in the editor. + * + * Replace them with your own styles or remove the file completely. + */ + +.wp-block-create-block-explore-tags { + background-color: #21759b; + color: #fff; + padding: 2px; +} diff --git a/plugins/carhop-blocks/src/explore-tags/view.js b/plugins/carhop-blocks/src/explore-tags/view.js new file mode 100644 index 0000000..e69de29