From 78168c6436cdccb310f2f24feedb7a6194c8cc77 Mon Sep 17 00:00:00 2001 From: Ajay DSouza Date: Mon, 2 May 2022 21:50:02 +0100 Subject: [PATCH] Block changes * Block shows a placeholder when used in non-Edit screens or when no content is generated * Fixed: Block settings were not saved --- includes/blocks/register-blocks.php | 15 ++++++- .../related-posts/build/index.asset.php | 2 +- includes/blocks/related-posts/build/index.js | 2 +- includes/blocks/related-posts/src/edit.js | 40 ++++++++++++------- includes/class-crp-query.php | 2 +- includes/main-query.php | 4 ++ readme.txt | 4 ++ 7 files changed, 51 insertions(+), 18 deletions(-) diff --git a/includes/blocks/register-blocks.php b/includes/blocks/register-blocks.php index 2803511..7dbcb48 100644 --- a/includes/blocks/register-blocks.php +++ b/includes/blocks/register-blocks.php @@ -56,7 +56,20 @@ function render_crp_block( $attributes ) { */ $arguments = apply_filters( 'crp_block_options', $arguments, $attributes ); - return get_crp( $arguments ); + $output = get_crp( $arguments ); + $is_empty_output = empty( wp_strip_all_tags( $output ) ); + $is_backend = defined( 'REST_REQUEST' ) && true === REST_REQUEST && 'edit' === filter_input( INPUT_GET, 'context', FILTER_UNSAFE_RAW ); + + if ( $is_backend && $is_empty_output ) { + $output = '

'; + $output .= __( 'Contextual Related Posts Block', 'contextual-related-posts' ); + $output .= '

'; + $output .= '

'; + $output .= __( 'No related posts were found or the block rendered empty. Please modify your block settings.', 'contextual-related-posts' ); + $output .= '

'; + } + + return $output; } /** diff --git a/includes/blocks/related-posts/build/index.asset.php b/includes/blocks/related-posts/build/index.asset.php index ce56412..e99ab32 100644 --- a/includes/blocks/related-posts/build/index.asset.php +++ b/includes/blocks/related-posts/build/index.asset.php @@ -1 +1 @@ - array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-server-side-render'), 'version' => '79313b027ff695c9e9ad15ab1d965fb2'); \ No newline at end of file + array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-server-side-render'), 'version' => '644b7ff81961cc116c1996f06630189b'); \ No newline at end of file diff --git a/includes/blocks/related-posts/build/index.js b/includes/blocks/related-posts/build/index.js index cbbe556..0d89b14 100644 --- a/includes/blocks/related-posts/build/index.js +++ b/includes/blocks/related-posts/build/index.js @@ -1 +1 @@ -!function(){"use strict";var e={n:function(t){var l=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(l,{a:l}),l},d:function(t,l){for(var o in l)e.o(l,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:l[o]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)}},t=window.wp.blocks,l=window.wp.element,o=window.wp.i18n,a=window.wp.serverSideRender,n=e.n(a),r=window.wp.blockEditor,s=window.wp.components;(0,t.registerBlockType)("contextual-related-posts/related-posts",{edit:function(e){let{attributes:t,setAttributes:a}=e;function d(e){return void 0===e||0===e||""===e||isNaN(e)?"":parseInt(e)}const{heading:u,limit:c,offset:p,show_excerpt:i,show_author:_,show_date:h,random_order:m,ordering:x,post_thumb_op:b,other_attributes:f}=t,w=(0,r.useBlockProps)();return(0,l.createElement)(l.Fragment,null,(0,l.createElement)(r.InspectorControls,null,(0,l.createElement)(s.PanelBody,{title:(0,o.__)("Related Posts Settings","contextual-related-posts"),initialOpen:!0},(0,l.createElement)(s.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(s.ToggleControl,{label:(0,o.__)("Show heading","contextual-related-posts"),help:u?(0,o.__)("Heading displayed","contextual-related-posts"):(0,o.__)("No Heading displayed","contextual-related-posts"),checked:u,onChange:()=>{a({heading:!u})}}))),(0,l.createElement)(s.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(s.TextControl,{label:(0,o.__)("Number of posts","contextual-related-posts"),value:c,onChange:e=>{a({limit:d(e)})},help:(0,o.__)("Maximum number of posts to display","contextual-related-posts")}))),(0,l.createElement)(s.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(s.TextControl,{label:(0,o.__)("Offset","contextual-related-posts"),value:p,onChange:e=>{a({offset:d(e)})},help:(0,o.__)("Number of posts to skip from the top","contextual-related-posts")}))),(0,l.createElement)(s.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(s.ToggleControl,{label:(0,o.__)("Show excerpt","contextual-related-posts"),help:i?(0,o.__)("Excerpt displayed","contextual-related-posts"):(0,o.__)("No excerpt","contextual-related-posts"),checked:i,onChange:()=>{a({show_excerpt:!i})}}))),(0,l.createElement)(s.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(s.ToggleControl,{label:(0,o.__)("Show author","contextual-related-posts"),help:_?(0,o.__)('"by Author Name" displayed',"contextual-related-posts"):(0,o.__)("No author displayed","contextual-related-posts"),checked:_,onChange:()=>{a({show_author:!_})}}))),(0,l.createElement)(s.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(s.ToggleControl,{label:(0,o.__)("Show date","contextual-related-posts"),help:h?(0,o.__)("Date of post displayed","contextual-related-posts"):(0,o.__)("Date of post not displayed","contextual-related-posts"),checked:h,onChange:()=>{a({show_date:!h})}}))),(0,l.createElement)(s.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(s.SelectControl,{label:(0,o.__)("Thumbnail option","contextual-related-posts"),value:b,onChange:e=>{a({post_thumb_op:e})},help:(0,o.__)("Location of the post thumbnail","contextual-related-posts"),options:[{value:"inline",label:(0,o.__)("Before title","contextual-related-posts")},{value:"after",label:(0,o.__)("After title","contextual-related-posts")},{value:"thumbs_only",label:(0,o.__)("Only thumbnail","contextual-related-posts")},{value:"text_only",label:(0,o.__)("Only text","contextual-related-posts")}]}))),(0,l.createElement)(s.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(s.RadioControl,{label:(0,o.__)("Order posts","contextual-related-posts"),selected:x,onChange:e=>{a({ordering:e})},help:(0,o.__)("This option directly edits the query","contextual-related-posts"),options:[{value:"relevance",label:(0,o.__)("By relevance","contextual-related-posts")},{value:"random",label:(0,o.__)("Randomly","contextual-related-posts")},{value:"date",label:(0,o.__)("By date","contextual-related-posts")}]}))),(0,l.createElement)(s.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(s.ToggleControl,{label:(0,o.__)("Randomize posts","contextual-related-posts"),help:m?(0,o.__)("Posts are shuffled on each load","contextual-related-posts"):(0,o.__)("Posts displayed based on above setting","contextual-related-posts"),checked:m,onChange:()=>{a({random_order:!m})}}))),(0,l.createElement)(s.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(s.TextareaControl,{label:(0,o.__)("Other attributes","contextual-related-posts"),value:f,onChange:e=>{a({other_attributes:void 0===e?"":e})},help:(0,o.__)("Enter other attributes in a URL-style string-query. e.g. post_types=post,page&link_nofollow=1&exclude_post_ids=5,6","contextual-related-posts")}))))),(0,l.createElement)("div",w,(0,l.createElement)(n(),{block:"contextual-related-posts/related-posts",attributes:t})))},save:()=>null})}(); \ No newline at end of file +!function(){"use strict";var e={n:function(t){var l=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(l,{a:l}),l},d:function(t,l){for(var o in l)e.o(l,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:l[o]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)}},t=window.wp.blocks,l=window.wp.element,o=window.wp.i18n,a=window.wp.serverSideRender,n=e.n(a),s=window.wp.blockEditor,r=window.wp.components;(0,t.registerBlockType)("contextual-related-posts/related-posts",{edit:function(e){let{attributes:t,setAttributes:a}=e;const d=null===wp.data.select("core/editor")?0:wp.data.select("core/editor").getCurrentPostId(),{heading:c,limit:u,offset:p,show_excerpt:i,show_author:_,show_date:h,random_order:m,ordering:x,post_thumb_op:b,other_attributes:w}=t,f=(0,s.useBlockProps)();return(0,l.createElement)(l.Fragment,null,(0,l.createElement)(s.InspectorControls,null,(0,l.createElement)(r.PanelBody,{title:(0,o.__)("Related Posts Settings","contextual-related-posts"),initialOpen:!0},(0,l.createElement)(r.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(r.ToggleControl,{label:(0,o.__)("Show heading","contextual-related-posts"),help:c?(0,o.__)("Heading displayed","contextual-related-posts"):(0,o.__)("No Heading displayed","contextual-related-posts"),checked:c,onChange:()=>{a({heading:!c})}}))),(0,l.createElement)(r.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(r.TextControl,{label:(0,o.__)("Number of posts","contextual-related-posts"),value:u,onChange:e=>{a({limit:void 0===e?"":e})},help:(0,o.__)("Maximum number of posts to display","contextual-related-posts")}))),(0,l.createElement)(r.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(r.TextControl,{label:(0,o.__)("Offset","contextual-related-posts"),value:p,onChange:e=>{a({offset:void 0===e?"":e})},help:(0,o.__)("Number of posts to skip from the top","contextual-related-posts")}))),(0,l.createElement)(r.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(r.ToggleControl,{label:(0,o.__)("Show excerpt","contextual-related-posts"),help:i?(0,o.__)("Excerpt displayed","contextual-related-posts"):(0,o.__)("No excerpt","contextual-related-posts"),checked:i,onChange:()=>{a({show_excerpt:!i})}}))),(0,l.createElement)(r.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(r.ToggleControl,{label:(0,o.__)("Show author","contextual-related-posts"),help:_?(0,o.__)('"by Author Name" displayed',"contextual-related-posts"):(0,o.__)("No author displayed","contextual-related-posts"),checked:_,onChange:()=>{a({show_author:!_})}}))),(0,l.createElement)(r.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(r.ToggleControl,{label:(0,o.__)("Show date","contextual-related-posts"),help:h?(0,o.__)("Date of post displayed","contextual-related-posts"):(0,o.__)("Date of post not displayed","contextual-related-posts"),checked:h,onChange:()=>{a({show_date:!h})}}))),(0,l.createElement)(r.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(r.SelectControl,{label:(0,o.__)("Thumbnail option","contextual-related-posts"),value:b,onChange:e=>{a({post_thumb_op:e})},help:(0,o.__)("Location of the post thumbnail","contextual-related-posts"),options:[{value:"inline",label:(0,o.__)("Before title","contextual-related-posts")},{value:"after",label:(0,o.__)("After title","contextual-related-posts")},{value:"thumbs_only",label:(0,o.__)("Only thumbnail","contextual-related-posts")},{value:"text_only",label:(0,o.__)("Only text","contextual-related-posts")}]}))),(0,l.createElement)(r.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(r.RadioControl,{label:(0,o.__)("Order posts","contextual-related-posts"),selected:x,onChange:e=>{a({ordering:e})},help:(0,o.__)("This option directly edits the query","contextual-related-posts"),options:[{value:"relevance",label:(0,o.__)("By relevance","contextual-related-posts")},{value:"random",label:(0,o.__)("Randomly","contextual-related-posts")},{value:"date",label:(0,o.__)("By date","contextual-related-posts")}]}))),(0,l.createElement)(r.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(r.ToggleControl,{label:(0,o.__)("Randomize posts","contextual-related-posts"),help:m?(0,o.__)("Posts are shuffled on each load","contextual-related-posts"):(0,o.__)("Posts displayed based on above setting","contextual-related-posts"),checked:m,onChange:()=>{a({random_order:!m})}}))),(0,l.createElement)(r.PanelRow,null,(0,l.createElement)("fieldset",null,(0,l.createElement)(r.TextareaControl,{label:(0,o.__)("Other attributes","contextual-related-posts"),value:w,onChange:e=>{a({other_attributes:void 0===e?"":e})},help:(0,o.__)("Enter other attributes in a URL-style string-query. e.g. post_types=post,page&link_nofollow=1&exclude_post_ids=5,6","contextual-related-posts")}))))),(0,l.createElement)("div",f,d?(0,l.createElement)(n(),{block:"contextual-related-posts/related-posts",attributes:t}):(0,l.createElement)(r.Placeholder,{icon:"list-view",label:(0,o.__)("Contextual Related Posts","contextual-related-posts"),instructions:(0,o.__)("This is a placeholder for the related posts block. Visit the front end of your site to see the related posts.","contextual-related-posts")})))},save:()=>null})}(); \ No newline at end of file diff --git a/includes/blocks/related-posts/src/edit.js b/includes/blocks/related-posts/src/edit.js index 586709c..802f62c 100644 --- a/includes/blocks/related-posts/src/edit.js +++ b/includes/blocks/related-posts/src/edit.js @@ -23,6 +23,7 @@ import { PanelRow, SelectControl, RadioControl, + Placeholder, } from '@wordpress/components'; /** @@ -42,14 +43,10 @@ import { * @return {WPElement} Element to render. */ export default function Edit({ attributes, setAttributes }) { - function processNumber(input) { - const output = - undefined === input || 0 === input || '' === input || isNaN(input) - ? '' - : parseInt(input); - return output; - } - + const postId = + null === wp.data.select('core/editor') + ? 0 + : wp.data.select('core/editor').getCurrentPostId(); const { heading, limit, @@ -68,10 +65,14 @@ export default function Edit({ attributes, setAttributes }) { setAttributes({ heading: !heading }); }; const onChangeLimit = (newLimit) => { - setAttributes({ limit: processNumber(newLimit) }); + setAttributes({ + limit: undefined === newLimit ? '' : newLimit, + }); }; const onChangeOffset = (newOffset) => { - setAttributes({ offset: processNumber(newOffset) }); + setAttributes({ + offset: undefined === newOffset ? '' : newOffset, + }); }; const toggleShowExcerpt = () => { setAttributes({ show_excerpt: !show_excerpt }); @@ -288,10 +289,21 @@ export default function Edit({ attributes, setAttributes }) {
- + {!postId ? ( + + ) : ( + + )}
); diff --git a/includes/class-crp-query.php b/includes/class-crp-query.php index f285434..48354d1 100644 --- a/includes/class-crp-query.php +++ b/includes/class-crp-query.php @@ -181,7 +181,7 @@ public function prepare_query_args( $args = array() ) { // Set the number of posts to be retrieved. Use posts_per_page if set else use limit. if ( empty( $args['posts_per_page'] ) ) { - $args['posts_per_page'] = ( $args['strict_limit'] ) ? $args['limit'] : ( $args['limit'] * 3 ); + $args['posts_per_page'] = ( $args['strict_limit'] ) ? absint( $args['limit'] ) : ( absint( $args['limit'] ) * 3 ); } if ( empty( $args['post_type'] ) ) { diff --git a/includes/main-query.php b/includes/main-query.php index 9b1f475..cf524a8 100644 --- a/includes/main-query.php +++ b/includes/main-query.php @@ -21,6 +21,10 @@ function get_crp( $args = array() ) { global $post, $crp_settings; + if ( ! $post ) { + return ''; + } + $crp_settings = crp_get_settings(); $defaults = array( diff --git a/readme.txt b/readme.txt index b3cffe3..b28b122 100644 --- a/readme.txt +++ b/readme.txt @@ -167,8 +167,12 @@ You can insert the related posts anywhere in your post using the `[crp]` shortco Release post: [https://webberzone.com/blog/contextual-related-posts-v3-2-0/](https://webberzone.com/blog/contextual-related-posts-v3-2-0/) +* Enhancements: + * Block shows a placeholder when used in non-Edit screens or when no content is generated + * Bug fix: * Missing text-only.min.css file + * Block settings were not saved = 3.2.2 =