Skip to content

Commit

Permalink
changed block render wrapper attributes
Browse files Browse the repository at this point in the history
changed className supports to false
removed ghostkit attributes and classes, since it is added automatically by the Ghost Kit
added code to automatically replace old deprecated block 'nk/visual-portfolio' to new 'visual-portfolio/saved'
  • Loading branch information
nk-o committed Nov 25, 2023
1 parent 9e7fd65 commit 74dfa0d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 108 deletions.
2 changes: 1 addition & 1 deletion build/gutenberg/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('jquery', 'lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-date', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '73ed561ca29115334e44');
<?php return array('dependencies' => array('jquery', 'lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-date', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => 'c562e423610c6172e411');
2 changes: 1 addition & 1 deletion build/gutenberg/index.js

Large diffs are not rendered by default.

32 changes: 7 additions & 25 deletions classes/class-gutenberg-saved.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Gutenberg block.
*
* @package ghostkit
* @package visual-portfolio
*/

if ( ! defined( 'ABSPATH' ) ) {
Expand Down Expand Up @@ -87,8 +87,6 @@ public function register_block_layouts_editor() {
* @return string
*/
public function block_render( $attributes ) {
ob_start();

$attributes = array_merge(
array(
'id' => '',
Expand All @@ -108,29 +106,13 @@ public function block_render( $attributes ) {

$class_name = 'wp-block-visual-portfolio';

if ( $attributes['align'] ) {
$class_name .= ' align' . $attributes['align'];
}

if ( $attributes['className'] ) {
$class_name .= ' ' . $attributes['className'];
}
?>
<div
<?php
echo ' class="' . esc_attr( $class_name ) . '"';
echo isset( $attributes['ghostkitSR'] ) && $attributes['ghostkitSR'] ? ' data-ghostkit-sr="' . esc_attr( $attributes['ghostkitSR'] ) . '"' : '';
?>
>
<?php
// The function returns clean data because it includes templates that use escaping functions before output.
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo Visual_Portfolio_Get::get( array( 'id' => $attributes['id'] ) );
?>
</div>
<?php
$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => $class_name,
)
);

return ob_get_clean();
return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, Visual_Portfolio_Get::get( array( 'id' => $attributes['id'] ) ) );
}
}
new Visual_Portfolio_Gutenberg_Saved_Block();
22 changes: 7 additions & 15 deletions classes/class-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Gutenberg block.
*
* @package ghostkit
* @package visual-portfolio
*/

if ( ! defined( 'ABSPATH' ) ) {
Expand Down Expand Up @@ -186,21 +186,13 @@ public function block_render( $attributes ) {

$class_name = 'wp-block-visual-portfolio';

if ( $attributes['align'] ) {
$class_name .= ' align' . $attributes['align'];
}

if ( $attributes['className'] ) {
$class_name .= ' ' . $attributes['className'];
}

$result = '<div class="' . esc_attr( $class_name ) . '"' . ( isset( $attributes['ghostkitSR'] ) && $attributes['ghostkitSR'] ? ' data-ghostkit-sr="' . esc_attr( $attributes['ghostkitSR'] ) . '"' : '' ) . '>';

$result .= Visual_Portfolio_Get::get( $attributes );

$result .= '</div>';
$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => $class_name,
)
);

return $result;
return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, Visual_Portfolio_Get::get( $attributes ) );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion gutenberg/block-saved/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"supports": {
"ghostkit": true,
"anchor": true,
"className": true,
"className": false,
"html": false,
"align": ["wide", "full"]
}
Expand Down
35 changes: 0 additions & 35 deletions gutenberg/block-saved/deprecated.js

This file was deleted.

15 changes: 2 additions & 13 deletions gutenberg/block-saved/edit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import classnames from 'classnames/dedupe';

import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { Button, PanelBody, Placeholder, Spinner } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
Expand All @@ -19,7 +17,7 @@ const { plugin_name: pluginName } = window.VPGutenbergVariables;
export default function BlockEdit(props) {
const { clientId, setAttributes, attributes } = props;

const { id, ghostkitClassname } = attributes;
const { id } = attributes;

const { portfolioLayouts } = useSelect(
(select) => ({
Expand Down Expand Up @@ -111,16 +109,7 @@ export default function BlockEdit(props) {
);
}

let className = '';

// add custom classname.
if (ghostkitClassname) {
className = classnames(className, ghostkitClassname);
}

const blockProps = useBlockProps({
className,
});
const blockProps = useBlockProps();

return (
<div {...blockProps}>
Expand Down
29 changes: 26 additions & 3 deletions gutenberg/block-saved/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import './style.scss';

import { registerBlockType } from '@wordpress/blocks';
import { createBlock, registerBlockType } from '@wordpress/blocks';
import { useDispatch } from '@wordpress/data';

import metadata from './block.json';
import deprecated from './deprecated';
import edit from './edit';
import save from './save';
import transforms from './transforms';
Expand Down Expand Up @@ -82,9 +82,32 @@ registerBlockType('nk/visual-portfolio', {
...settings,
title,
name: 'nk/visual-portfolio',
attributes: {
id: {
type: 'string',
},
align: {
type: 'string',
},
className: {
type: 'string',
},
anchor: {
type: 'string',
},
},
edit: (props) => {
const { replaceBlocks } = useDispatch('core/block-editor');

replaceBlocks(
[props.clientId],
createBlock('visual-portfolio/saved', props.attributes || {})
);

return null;
},
supports: {
...metadata.supports,
inserter: false,
},
deprecated,
});
2 changes: 1 addition & 1 deletion gutenberg/block/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"supports": {
"ghostkit": true,
"anchor": true,
"className": true,
"className": false,
"html": false,
"align": ["wide", "full"]
}
Expand Down
14 changes: 1 addition & 13 deletions gutenberg/block/edit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import classnames from 'classnames/dedupe';

import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';

Expand Down Expand Up @@ -62,7 +60,6 @@ export default function BlockEdit(props) {
setup_wizard: setupWizard,
preview_image_example: previewExample,
layout,
ghostkitClassname,
} = attributes;

// Display setup wizard on mount.
Expand All @@ -75,13 +72,6 @@ export default function BlockEdit(props) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

let className = '';

// add custom classname.
if (ghostkitClassname) {
className = classnames(className, ghostkitClassname);
}

// Display block preview.
if (previewExample === 'true') {
return (
Expand All @@ -94,9 +84,7 @@ export default function BlockEdit(props) {
);
}

const blockProps = useBlockProps({
className,
});
const blockProps = useBlockProps();

return (
<div {...blockProps}>
Expand Down

0 comments on commit 74dfa0d

Please sign in to comment.