Skip to content

Commit

Permalink
Block changes
Browse files Browse the repository at this point in the history
    * Block shows a placeholder when used in non-Edit screens or when no content is generated
    * Fixed: Block settings were not saved
  • Loading branch information
ajaydsouza committed May 2, 2022
1 parent 5bb78d3 commit 78168c6
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 18 deletions.
15 changes: 14 additions & 1 deletion includes/blocks/register-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<h3>';
$output .= __( 'Contextual Related Posts Block', 'contextual-related-posts' );
$output .= '</h3>';
$output .= '<p>';
$output .= __( 'No related posts were found or the block rendered empty. Please modify your block settings.', 'contextual-related-posts' );
$output .= '</p>';
}

return $output;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/blocks/related-posts/build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-server-side-render'), 'version' => '79313b027ff695c9e9ad15ab1d965fb2');
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-server-side-render'), 'version' => '644b7ff81961cc116c1996f06630189b');
2 changes: 1 addition & 1 deletion includes/blocks/related-posts/build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 26 additions & 14 deletions includes/blocks/related-posts/src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
PanelRow,
SelectControl,
RadioControl,
Placeholder,
} from '@wordpress/components';

/**
Expand All @@ -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,
Expand All @@ -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 });
Expand Down Expand Up @@ -288,10 +289,21 @@ export default function Edit({ attributes, setAttributes }) {
</InspectorControls>

<div {...blockProps}>
<ServerSideRender
block="contextual-related-posts/related-posts"
attributes={attributes}
/>
{!postId ? (
<Placeholder
icon={'list-view'}
label={__('Contextual Related Posts', 'contextual-related-posts')}
instructions={__(
'This is a placeholder for the related posts block. Visit the front end of your site to see the related posts.',
'contextual-related-posts'
)}
></Placeholder>
) : (
<ServerSideRender
block="contextual-related-posts/related-posts"
attributes={attributes}
/>
)}
</div>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion includes/class-crp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand Down
4 changes: 4 additions & 0 deletions includes/main-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
function get_crp( $args = array() ) {
global $post, $crp_settings;

if ( ! $post ) {
return '';
}

$crp_settings = crp_get_settings();

$defaults = array(
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 =

Expand Down

0 comments on commit 78168c6

Please sign in to comment.