Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: posts block improvements #2319

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion inc/render/class-posts-grid-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public function render_post_title( $tag, $post_url, $post_title ) {
$tag = sanitize_key( $tag );

if ( ! in_array( $tag, array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ), true ) ) {
$tag = 'h5';
$tag = 'h4';
}

return sprintf(
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/blocks/posts/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"postsToShow": {
"type": "number",
"default": 5
"default": 6
},
"order": {
"type": "string",
Expand Down Expand Up @@ -72,7 +72,7 @@
},
"titleTag": {
"type": "string",
"default": "h5"
"default": "h4"
},
"displayMeta": {
"type": "boolean",
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/blocks/posts/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const PostsTitle = ({ attributes, element, post }) => {
return '';
}

const Tag = ! [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ].includes( attributes.titleTag ) ? 'h5' : attributes.titleTag;
const Tag = ! [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ].includes( attributes.titleTag ) ? 'h4' : attributes.titleTag;

return (
<Tag key={ element } className="o-posts-grid-post-title">
Expand Down
27 changes: 27 additions & 0 deletions src/blocks/blocks/posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
_align,
_px,
boxValues,
changeActiveStyle,
getCustomPostTypeSlugs,
hex2rgba
} from '../../helpers/helper-functions.js';
Expand All @@ -59,6 +60,18 @@ import '../../components/store/index.js';
import FeaturedPost from './components/layout/featured.js';
import { domReady } from '../../helpers/frontend-helper-functions';

const styles = [
{
label: __( 'Default', 'otter-blocks' ),
value: 'default',
isDefault: true
},
{
label: __( 'Boxed', 'otter-blocks' ),
value: 'boxed'
}
];

const { attributes: defaultAttributes } = metadata;

/**
Expand Down Expand Up @@ -212,8 +225,22 @@ const Edit = ({
const { responsiveGetAttributes } = useResponsiveAttributes();

useEffect( () => {
let isNew = false;
if ( undefined === attributes.id ) {
isNew = true;
}

const fetch = async() => {
setSlugs( await getCustomPostTypeSlugs() );

if ( isNew ) {

// We want to set the default style to boxed for new blocks.
// We keep it inside async function as keeping it outside was not updating the block style.
// Ref: https://github.com/Codeinwp/otter-internals/issues/220
const classes = changeActiveStyle( attributes?.className, styles, 'boxed' );
setAttributes({ className: classes });
}
};
fetch();
}, []);
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/blocks/posts/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ const Inspector = ({
>
<SelectControl
label={ __( 'Title Tag', 'otter-blocks' ) }
value={ attributes.titleTag || 'h5' }
value={ attributes.titleTag || 'h4' }
options={ [
{ label: __( 'H1', 'otter-blocks' ), value: 'h1' },
{ label: __( 'H2', 'otter-blocks' ), value: 'h2' },
Expand Down
3 changes: 2 additions & 1 deletion src/blocks/blocks/posts/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@
}

&.is-style-boxed {
--border-width: 3px;
--border-width: 1px;

.o-posts-grid-post-body {
padding: var( --content-padding, 20px );
background: rgb( 240, 245, 250);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test-post-grid-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function test_render_sanitization() {
$this->assertEquals( $expected, $output );

// We expect the titleTag to be sanitized.
$expected = '<h5 class="o-posts-grid-post-title"><a href="http://www.example.com">Title</a></h5>';
$expected = '<h4 class="o-posts-grid-post-title"><a href="http://www.example.com">Title</a></h4>';
$output = $this->post_grid_block->render_post_title( $malformed_attributes['titleTag'], 'www.example.com', 'Title' );

$this->assertEquals( $expected, $output );
Expand Down
Loading