From 41e6e91ffe118293665cd3c50e31b95c0cffd1da Mon Sep 17 00:00:00 2001 From: Szymon Kasprzyk Date: Tue, 4 Feb 2025 09:35:33 +0100 Subject: [PATCH 01/13] updates news block --- .../plugins/vf-embl-news-block/acf.json | 267 ++++++++++++++++-- .../plugins/vf-embl-news-block/index.php | 1 + .../plugins/vf-embl-news-block/template.php | 146 ++++++++-- 3 files changed, 372 insertions(+), 42 deletions(-) diff --git a/wp-content/plugins/vf-embl-news-block/acf.json b/wp-content/plugins/vf-embl-news-block/acf.json index 11b53772d..c3ebe6dd6 100644 --- a/wp-content/plugins/vf-embl-news-block/acf.json +++ b/wp-content/plugins/vf-embl-news-block/acf.json @@ -3,11 +3,12 @@ "title": "VF EMBL News", "fields": [ { - "key": "field_60bf5b1d10f6d", - "label": "Display publications", - "name": "display_publication", + "key": "field_679e43a8ff272", + "label": "Data pull method", + "name": "news_fetch", + "aria-label": "", "type": "select", - "instructions": "", + "instructions": "For Admin only", "required": 0, "conditional_logic": 0, "wrapper": { @@ -16,25 +17,88 @@ "id": "" }, "choices": { - "embl": "EMBL News", - "embl-ebi": "EMBL-EBI News" + "default": "Default", + "custom": "Custom endpoints", + "contenthub": "contentHub HTML" }, - "default_value": "embl", - "allow_null": 0, + "default_value": "default", + "return_format": "value", "multiple": 0, + "allow_null": 0, + "allow_in_bindings": 1, "ui": 0, - "return_format": "value", "ajax": 0, "placeholder": "" }, + { + "key": "field_679e2ac32681b", + "label": "WP REST API 1", + "name": "wprest_api_1", + "aria-label": "", + "type": "url", + "instructions": "Example:\r\nhttps:\/\/www.embl.org\/news\/wp-json\/wp\/v2\/posts?embl_taxonomy=1514&per_page=4", + "required": 0, + "conditional_logic": [ + [ + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "custom" + } + ] + ], + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "default_value": "", + "allow_in_bindings": 0, + "placeholder": "" + }, + { + "key": "field_679e2b3b2681c", + "label": "WP REST API 2", + "name": "wprest_api_1", + "aria-label": "", + "type": "url", + "instructions": "Example:\r\nhttps:\/\/www.embl.org\/news\/wp-json\/custom\/v1\/awards?award_site=grenoble&award_unit=molecular-systems-biology", + "required": 0, + "conditional_logic": [ + [ + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "custom" + } + ] + ], + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "default_value": "", + "allow_in_bindings": 0, + "placeholder": "" + }, { "key": "field_5eb014ed283d9", "label": "Limit", "name": "limit", + "aria-label": "", "type": "range", "instructions": "Maximum number of articles to display.", "required": 0, - "conditional_logic": 0, + "conditional_logic": [ + [ + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "default" + } + ] + ], "wrapper": { "width": "", "class": "", @@ -43,18 +107,63 @@ "default_value": 3, "min": 1, "max": 20, + "allow_in_bindings": 1, "step": "", "prepend": "", "append": "" }, + { + "key": "field_60bf5b1d10f6d", + "label": "Display publications", + "name": "display_publication", + "aria-label": "", + "type": "select", + "instructions": "", + "required": 0, + "conditional_logic": [ + [ + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "default" + } + ] + ], + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "choices": { + "embl": "EMBL News", + "embl-ebi": "EMBL-EBI News" + }, + "default_value": "embl", + "return_format": "value", + "multiple": 0, + "allow_null": 0, + "allow_in_bindings": 1, + "ui": 0, + "ajax": 0, + "placeholder": "" + }, { "key": "field_61b88fe5f7a3f", "label": "Variant", "name": "variant", + "aria-label": "", "type": "radio", "instructions": "", "required": 0, - "conditional_logic": 0, + "conditional_logic": [ + [ + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "default" + } + ] + ], "wrapper": { "width": "", "class": "", @@ -64,21 +173,31 @@ "vf-news-item-default": "Default", "vf-news-container-featured": "Container" }, + "default_value": "vf-news-item-default", + "return_format": "value", "allow_null": 0, "other_choice": 0, - "default_value": "vf-news-item-default", + "allow_in_bindings": 1, "layout": "horizontal", - "return_format": "value", "save_other_choice": 0 }, { "key": "field_5eb01560283da", "label": "Search by", "name": "type", + "aria-label": "", "type": "select", "instructions": "", "required": 0, - "conditional_logic": 0, + "conditional_logic": [ + [ + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "default" + } + ] + ], "wrapper": { "width": "", "class": "", @@ -92,10 +211,11 @@ "tags": "Free Tag(s)" }, "default_value": "latest", - "allow_null": 0, + "return_format": "value", "multiple": 0, + "allow_null": 0, + "allow_in_bindings": 1, "ui": 0, - "return_format": "value", "ajax": 0, "placeholder": "" }, @@ -103,6 +223,7 @@ "key": "field_5eb029fdc00fb", "label": "Latest", "name": "", + "aria-label": "", "type": "message", "instructions": "", "required": 0, @@ -112,12 +233,22 @@ "field": "field_5eb01560283da", "operator": "==", "value": "latest" + }, + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "default" } ], [ { "field": "field_5eb01560283da", "operator": "==empty" + }, + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "default" } ] ], @@ -134,6 +265,7 @@ "key": "field_5eb01627283db", "label": "EMBL Taxonomy", "name": "embl_terms", + "aria-label": "", "type": "taxonomy", "instructions": "Filter news by EMBL taxonomy term.", "required": 0, @@ -143,6 +275,11 @@ "field": "field_5eb01560283da", "operator": "==", "value": "taxonomy" + }, + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "default" } ] ], @@ -152,18 +289,22 @@ "id": "" }, "taxonomy": "embl_taxonomy", - "field_type": "select", - "allow_null": 1, "add_term": 0, "save_terms": 0, "load_terms": 0, "return_format": "id", - "multiple": 0 + "field_type": "select", + "allow_null": 1, + "allow_in_bindings": 1, + "bidirectional": 0, + "multiple": 0, + "bidirectional_target": [] }, { "key": "field_5eb01691283dc", "label": "Keyword", "name": "keyword", + "aria-label": "", "type": "text", "instructions": "Filter news by a keyword search.", "required": 0, @@ -173,6 +314,11 @@ "field": "field_5eb01560283da", "operator": "==", "value": "keyword" + }, + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "default" } ] ], @@ -182,15 +328,17 @@ "id": "" }, "default_value": "", + "maxlength": "", + "allow_in_bindings": 1, "placeholder": "", "prepend": "", - "append": "", - "maxlength": "" + "append": "" }, { "key": "field_5eb016ca283dd", "label": "Article ID(s)", "name": "ids", + "aria-label": "", "type": "text", "instructions": "Filter news by article IDs (comma separated, e.g. \"9996, 12060\").", "required": 0, @@ -200,6 +348,11 @@ "field": "field_5eb01560283da", "operator": "==", "value": "ids" + }, + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "default" } ] ], @@ -209,15 +362,17 @@ "id": "" }, "default_value": "", + "maxlength": "", + "allow_in_bindings": 1, "placeholder": "", "prepend": "", - "append": "", - "maxlength": "" + "append": "" }, { "key": "field_5eb01805283df", "label": "Free Tag(s)", "name": "tags", + "aria-label": "", "type": "text", "instructions": "Filter news by tags (comma separated, e.g. \"bioinformatics, chemistry\").", "required": 0, @@ -227,6 +382,11 @@ "field": "field_5eb01560283da", "operator": "==", "value": "tags" + }, + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "default" } ] ], @@ -236,10 +396,65 @@ "id": "" }, "default_value": "", + "maxlength": "", + "allow_in_bindings": 1, "placeholder": "", "prepend": "", - "append": "", - "maxlength": "" + "append": "" + }, + { + "key": "field_67a08be960436", + "label": "contentHub data fetch HTML", + "name": "contenthub_data_fetch", + "aria-label": "", + "type": "textarea", + "instructions": "", + "required": 0, + "conditional_logic": [ + [ + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "contenthub" + } + ] + ], + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "default_value": "", + "maxlength": "", + "allow_in_bindings": 0, + "rows": "", + "placeholder": "", + "new_lines": "" + }, + { + "key": "field_67a09b7de6cb9", + "label": "Section header", + "name": "section_header", + "aria-label": "", + "type": "link", + "instructions": "", + "required": 0, + "conditional_logic": [ + [ + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "custom" + } + ] + ], + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "return_format": "array", + "allow_in_bindings": 0 } ], "location": [ @@ -259,5 +474,5 @@ "hide_on_screen": "", "active": true, "description": "VF EMBL News ACF Gutenberg block", - "modified": 1642671260 + "show_in_rest": 0 } \ No newline at end of file diff --git a/wp-content/plugins/vf-embl-news-block/index.php b/wp-content/plugins/vf-embl-news-block/index.php index 184385d10..0d8fd7cd0 100644 --- a/wp-content/plugins/vf-embl-news-block/index.php +++ b/wp-content/plugins/vf-embl-news-block/index.php @@ -32,6 +32,7 @@ public function __construct() { 'acf/settings/load_json', array($this, 'acf_settings_load_json') ); + } /** diff --git a/wp-content/plugins/vf-embl-news-block/template.php b/wp-content/plugins/vf-embl-news-block/template.php index 2599f9bac..ca96009b8 100644 --- a/wp-content/plugins/vf-embl-news-block/template.php +++ b/wp-content/plugins/vf-embl-news-block/template.php @@ -1,19 +1,4 @@ 20 ? 3 : $limit; @@ -45,6 +30,10 @@ $tags = explode(',', $tags ?? ''); $tags = array_map('trim', $tags); + + +if ($fetch == 'default' || empty($fetch)) { + if (is_int($embl_terms)) { $embl_terms = array($embl_terms); } @@ -139,5 +128,130 @@ ); echo $content; +} + +else if($fetch == 'custom') { +// Show default preview instruction +if (empty(get_field('wprest_api_1')) && empty(get_field('wprest_api_2'))) { + if ($is_preview) { ?> +
+
+

+ +

+
+
+ + +
+ +document.addEventListener("DOMContentLoaded", async function() { + async function fetchAndDisplayLatestProjects() { + const endpoint1 = "' . esc_js(get_field('wprest_api_1')) . '"; + const endpoint2 = "' . esc_js(get_field('wprest_api_2')) . '"; + + console.log("Fetching from:", endpoint1, endpoint2); // Debugging + + const endpoints = [endpoint1, endpoint2].filter(Boolean); // Filter out empty endpoints + let mergedPosts = []; + + for (const endpoint of endpoints) { + try { + const response = await fetch(endpoint); + if (!response.ok) throw new Error("Network response was not ok: " + response.statusText); + const data = await response.json(); + console.log("Data from", endpoint, data); // Debugging + + if (Array.isArray(data)) { + mergedPosts = mergedPosts.concat(data); + } else if (Array.isArray(data.posts)) { + mergedPosts = mergedPosts.concat(data.posts); + } + } catch (error) { + console.error("Error fetching data from", endpoint, error); + } + } + + if (mergedPosts.length === 0) { + console.warn("No posts found."); + return; + } + + const normalizedPosts = mergedPosts.map(post => ({ + id: post.id, + title: post.title?.rendered || post.title || "No title", + date: post.date || new Date().toISOString(), + excerpt: post.excerpt?.rendered ? post.excerpt.rendered.replace(/<[^>]+>/g, "") : (post.excerpt || ""), + image: post.featured_image_src, + url: post.link || post.url || "#" + })); + + normalizedPosts.sort((a, b) => new Date(b.date) - new Date(a.date)); + const latestPosts = normalizedPosts.slice(0, 4); + + const container = document.querySelector("#vf-news-container"); + if (!container) { + console.error("Container with ID #vf-news-container not found."); + return; + } + + container.innerHTML = ` + + `; + } + + fetchAndDisplayLatestProjects(); + }); + + '; + + echo $fetchPosts; + + } +else if($fetch == 'contenthub') { + if ($is_preview) { ?> +
+
+

+ +

+
+
+ + From c1ecee8c6416c3f6618e4137aa32b737bf174b4d Mon Sep 17 00:00:00 2001 From: Szymon Kasprzyk Date: Tue, 4 Feb 2025 10:02:40 +0100 Subject: [PATCH 02/13] adds section header --- .../plugins/vf-embl-news-block/acf.json | 44 ++++++++++++++++--- .../plugins/vf-embl-news-block/template.php | 28 ++++++------ 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/wp-content/plugins/vf-embl-news-block/acf.json b/wp-content/plugins/vf-embl-news-block/acf.json index c3ebe6dd6..8c0481e0d 100644 --- a/wp-content/plugins/vf-embl-news-block/acf.json +++ b/wp-content/plugins/vf-embl-news-block/acf.json @@ -36,7 +36,7 @@ "name": "wprest_api_1", "aria-label": "", "type": "url", - "instructions": "Example:\r\nhttps:\/\/www.embl.org\/news\/wp-json\/wp\/v2\/posts?embl_taxonomy=1514&per_page=4", + "instructions": "Example:\r\nhttps:\/\/www.embl.org\/news\/wp-json\/wp\/v2\/posts?embl_taxonomy=5154&per_page=4", "required": 0, "conditional_logic": [ [ @@ -59,10 +59,10 @@ { "key": "field_679e2b3b2681c", "label": "WP REST API 2", - "name": "wprest_api_1", + "name": "wprest_api_2", "aria-label": "", "type": "url", - "instructions": "Example:\r\nhttps:\/\/www.embl.org\/news\/wp-json\/custom\/v1\/awards?award_site=grenoble&award_unit=molecular-systems-biology", + "instructions": "Example:\r\nhttps:\/\/www.embl.org\/news\/wp-json\/custom\/v1\/awards?award_site=grenoble&award_unit=molecular%20systems%20biology", "required": 0, "conditional_logic": [ [ @@ -434,9 +434,9 @@ { "key": "field_67a09b7de6cb9", "label": "Section header", - "name": "section_header", + "name": "section_header_text", "aria-label": "", - "type": "link", + "type": "text", "instructions": "", "required": 0, "conditional_logic": [ @@ -453,8 +453,38 @@ "class": "", "id": "" }, - "return_format": "array", - "allow_in_bindings": 0 + "default_value": "Latest news", + "maxlength": "", + "allow_in_bindings": 1, + "placeholder": "", + "prepend": "", + "append": "" + }, + { + "key": "field_67a1d5d158acc", + "label": "Section header URL", + "name": "section_header_url", + "aria-label": "", + "type": "url", + "instructions": "", + "required": 0, + "conditional_logic": [ + [ + { + "field": "field_679e43a8ff272", + "operator": "==", + "value": "custom" + } + ] + ], + "wrapper": { + "width": "", + "class": "", + "id": "" + }, + "default_value": "https:\/\/www.embl.org\/news\/", + "allow_in_bindings": 0, + "placeholder": "" } ], "location": [ diff --git a/wp-content/plugins/vf-embl-news-block/template.php b/wp-content/plugins/vf-embl-news-block/template.php index ca96009b8..eb9eac37d 100644 --- a/wp-content/plugins/vf-embl-news-block/template.php +++ b/wp-content/plugins/vf-embl-news-block/template.php @@ -31,7 +31,6 @@ $tags = array_map('trim', $tags); - if ($fetch == 'default' || empty($fetch)) { if (is_int($embl_terms)) { @@ -147,18 +146,9 @@
@@ -166,6 +156,8 @@ async function fetchAndDisplayLatestProjects() { const endpoint1 = "' . esc_js(get_field('wprest_api_1')) . '"; const endpoint2 = "' . esc_js(get_field('wprest_api_2')) . '"; + const header = "' . esc_js(get_field('section_header_text')) . '"; + const headerURL = "' . esc_js(get_field('section_header_url')) . '"; console.log("Fetching from:", endpoint1, endpoint2); // Debugging @@ -214,9 +206,12 @@ container.innerHTML = `