From d580252caca223d2168f4f621efa2dcf4cde3b7e Mon Sep 17 00:00:00 2001 From: inwerpsel Date: Thu, 29 Oct 2020 14:08:53 +0100 Subject: [PATCH 1/2] PLANET-5503 Format date in backend, drop frontend lib --- assets/src/blocks/Articles/ArticlePreview.js | 12 +++++------- classes/blocks/class-articles.php | 1 + classes/class-loader.php | 2 -- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/assets/src/blocks/Articles/ArticlePreview.js b/assets/src/blocks/Articles/ArticlePreview.js index 7125ebb8b..bcf57584c 100644 --- a/assets/src/blocks/Articles/ArticlePreview.js +++ b/assets/src/blocks/Articles/ArticlePreview.js @@ -1,7 +1,5 @@ import { Component } from '@wordpress/element'; -import { dateI18n } from '@wordpress/date'; import { unescape } from '../../functions/unescape'; -const { __ } = wp.i18n; export class ArticlePreview extends Component { constructor(props) { @@ -100,7 +98,7 @@ export class ArticlePreview extends Component { page_type_link, post_title, post_excerpt, - post_date + date_formatted, } } = this.props; @@ -149,10 +147,10 @@ export class ArticlePreview extends Component { }

{this.getAuthorLink()} - {post_date && - + {date_formatted && + }

diff --git a/classes/blocks/class-articles.php b/classes/blocks/class-articles.php index 6ddba171b..a798b0c08 100644 --- a/classes/blocks/class-articles.php +++ b/classes/blocks/class-articles.php @@ -230,6 +230,7 @@ private static function populate_post_items( $posts ) { $recent['page_type'] = $page_type; $recent['page_type_link'] = get_term_link( $page_type_id ); $recent['link'] = get_permalink( $recent['ID'] ); + $recent['date_formatted'] = get_the_date( '', $recent['ID'] ); $recent_posts[] = $recent; } diff --git a/classes/class-loader.php b/classes/class-loader.php index a960eb68c..f4ffe64ad 100644 --- a/classes/class-loader.php +++ b/classes/class-loader.php @@ -399,8 +399,6 @@ public function enqueue_public_assets() { 'wp-i18n', // URL helpers (as addQueryArgs). 'wp-url', - // Use to translate date. - 'wp-date', 'main', ], true From 8a1f648d94c01cea03aae01ddd74a8d7b8b4cfd7 Mon Sep 17 00:00:00 2001 From: inwerpsel Date: Thu, 29 Oct 2020 15:03:50 +0100 Subject: [PATCH 2/2] PLANET-5503 Custom addQueryArgs to avoid wp-url on frontend * We only use this function from the package on the frontend, and it can easily be replaced with a native JS API. --- .../src/blocks/Articles/useArticlesFetch.js | 7 ++++--- assets/src/blocks/Gallery/useGalleryImages.js | 2 +- .../Happypoint/useHappypointImageData.js | 2 +- assets/src/blocks/Socialmedia/Socialmedia.js | 3 --- .../blocks/Spreadsheet/SpreadsheetFrontend.js | 2 +- .../Takeactionboxout/Takeactionboxout.js | 3 --- assets/src/functions/addQueryArgs.js | 21 +++++++++++++++++++ classes/class-loader.php | 2 -- 8 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 assets/src/functions/addQueryArgs.js diff --git a/assets/src/blocks/Articles/useArticlesFetch.js b/assets/src/blocks/Articles/useArticlesFetch.js index 552dc22e6..249272ff8 100644 --- a/assets/src/blocks/Articles/useArticlesFetch.js +++ b/assets/src/blocks/Articles/useArticlesFetch.js @@ -1,8 +1,8 @@ import { useState, useEffect } from '@wordpress/element'; import { fetchJson } from '../../functions/fetchJson'; +import { addQueryArgs } from '../../functions/addQueryArgs'; const { apiFetch } = wp; -const { addQueryArgs } = wp.url; export const useArticlesFetch = (attributes, postType, postId, baseUrl = null, postCategories = []) => { const { article_count, post_types, posts, tags, ignore_categories } = attributes; @@ -34,11 +34,12 @@ export const useArticlesFetch = (attributes, postType, postId, baseUrl = null, p args.categories = postCategories; } + const path = addQueryArgs('planet4/v1/get-posts', args); try { const response = baseUrl - ? await fetchJson(`${ baseUrl }/wp-json/${ addQueryArgs('planet4/v1/get-posts', args) }`) - : await apiFetch({ path: addQueryArgs('planet4/v1/get-posts', args) }); + ? await fetchJson(`${ baseUrl }/wp-json/${ path }`) + : await apiFetch({ path }); const newPosts = [...prevPosts, ...response.recent_posts]; diff --git a/assets/src/blocks/Gallery/useGalleryImages.js b/assets/src/blocks/Gallery/useGalleryImages.js index 4ab82c348..a8d170209 100644 --- a/assets/src/blocks/Gallery/useGalleryImages.js +++ b/assets/src/blocks/Gallery/useGalleryImages.js @@ -1,8 +1,8 @@ import { useState, useEffect } from '@wordpress/element'; import { fetchJson } from '../../functions/fetchJson'; +import { addQueryArgs } from '../../functions/addQueryArgs'; const { apiFetch } = wp; -const { addQueryArgs } = wp.url; const GALLERY_IMAGE_SIZES = { 'slider': 'retina-large', diff --git a/assets/src/blocks/Happypoint/useHappypointImageData.js b/assets/src/blocks/Happypoint/useHappypointImageData.js index f9ad42463..b6866ba04 100644 --- a/assets/src/blocks/Happypoint/useHappypointImageData.js +++ b/assets/src/blocks/Happypoint/useHappypointImageData.js @@ -1,8 +1,8 @@ import { useState, useEffect } from '@wordpress/element'; import { fetchJson } from '../../functions/fetchJson'; +import { addQueryArgs } from '../../functions/addQueryArgs'; const { apiFetch } = wp; -const { addQueryArgs } = wp.url; export const useHappypointImageData = imageId => { const [imageData, setImageData] = useState({}); diff --git a/assets/src/blocks/Socialmedia/Socialmedia.js b/assets/src/blocks/Socialmedia/Socialmedia.js index e02473256..f90fa1416 100644 --- a/assets/src/blocks/Socialmedia/Socialmedia.js +++ b/assets/src/blocks/Socialmedia/Socialmedia.js @@ -10,9 +10,6 @@ import { import withCharacterCounter from '../../components/withCharacterCounter/withCharacterCounter'; import {URLInput} from "../../components/URLInput/URLInput"; -const {apiFetch} = wp; -const {addQueryArgs} = wp.url; - const TextControl = withCharacterCounter( BaseTextControl ); const TextareaControl = withCharacterCounter( BaseTextareaControl ); diff --git a/assets/src/blocks/Spreadsheet/SpreadsheetFrontend.js b/assets/src/blocks/Spreadsheet/SpreadsheetFrontend.js index 21b37da3f..5fe235c3a 100644 --- a/assets/src/blocks/Spreadsheet/SpreadsheetFrontend.js +++ b/assets/src/blocks/Spreadsheet/SpreadsheetFrontend.js @@ -3,9 +3,9 @@ import { ArrowIcon } from './ArrowIcon'; import { toDeclarations } from '../toDeclarations'; import { HighlightMatches } from '../HighlightMatches'; import { fetchJson } from '../../functions/fetchJson'; +import { addQueryArgs } from '../../functions/addQueryArgs'; const { apiFetch } = wp; -const { addQueryArgs } = wp.url; const { __ } = wp.i18n; const placeholderData = { diff --git a/assets/src/blocks/Takeactionboxout/Takeactionboxout.js b/assets/src/blocks/Takeactionboxout/Takeactionboxout.js index c34e1fc1d..87278c007 100644 --- a/assets/src/blocks/Takeactionboxout/Takeactionboxout.js +++ b/assets/src/blocks/Takeactionboxout/Takeactionboxout.js @@ -14,9 +14,6 @@ import { } from '@wordpress/components'; import withCharacterCounter from '../../components/withCharacterCounter/withCharacterCounter'; -const {apiFetch} = wp; -const {addQueryArgs} = wp.url; - const TextControl = withCharacterCounter( BaseTextControl ); const TextareaControl = withCharacterCounter( BaseTextareaControl ); diff --git a/assets/src/functions/addQueryArgs.js b/assets/src/functions/addQueryArgs.js new file mode 100644 index 000000000..d1660d06e --- /dev/null +++ b/assets/src/functions/addQueryArgs.js @@ -0,0 +1,21 @@ +export const addQueryArgs = (path, args) => { + if (typeof path !== 'string' || typeof args !== 'object') { + return path; + } + + Object.keys(args).forEach(k => { + const value = args[k]; + if (typeof value === 'undefined' || value === '') { + delete args[k]; + return; + } + if (Array.isArray(value)) { + value.forEach((v, i) => { + args[`${ k }[${ i }]`] = v; + }); + delete args[k]; + } + }); + + return `${ path }?${ new URLSearchParams(args) }`; +}; diff --git a/classes/class-loader.php b/classes/class-loader.php index f4ffe64ad..43e103eb6 100644 --- a/classes/class-loader.php +++ b/classes/class-loader.php @@ -397,8 +397,6 @@ public function enqueue_public_assets() { 'wp-element', // Exports the __() function. 'wp-i18n', - // URL helpers (as addQueryArgs). - 'wp-url', 'main', ], true