Skip to content

Commit

Permalink
Add secret to block editor (view).
Browse files Browse the repository at this point in the history
  • Loading branch information
doekenorg committed Feb 8, 2024
1 parent e14d1d7 commit dfe5808
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions future/includes/class-gv-shortcode-gravityview.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public static function map_block_atts_to_shortcode_atts( $block_attributes = arr
$block_to_shortcode_attributes_map = array(
'viewId' => 'id',
'postId' => 'post_id',
'secret' => 'secret',
'pageSize' => 'page_size',
'sortField' => 'sort_field',
'sortDirection' => 'sort_direction',
Expand Down
4 changes: 4 additions & 0 deletions future/includes/gutenberg/blocks/view/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"default": 0,
"type": "number"
},
"secret": {
"default": null,
"type": "string"
},
"pageSize": {
"default": "",
"type": "string"
Expand Down
11 changes: 7 additions & 4 deletions future/includes/gutenberg/blocks/view/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ public function modify_block_meta( $block_meta ) {
* @return string $output
*/
static function render( $block_attributes = array() ) {
$shortcode_attributes = [];
$mapped_shortcode_attributes = \GV\Shortcodes\gravityview::map_block_atts_to_shortcode_atts( $block_attributes );

$shortcode_attributes = \GV\Shortcodes\gravityview::map_block_atts_to_shortcode_atts( $block_attributes );

foreach ( $shortcode_attributes as $attribute => $value ) {
foreach ( $mapped_shortcode_attributes as $attribute => $value ) {
$value = esc_attr( sanitize_text_field( $value ) );
if ( empty( $value ) ) {
continue;
}

$shortcode_attributes[] = sprintf(
'%s="%s"',
Expand All @@ -54,7 +57,7 @@ static function render( $block_attributes = array() ) {
$shortcode = sprintf( '[gravityview %s]', implode( ' ', $shortcode_attributes ) );

if ( Arr::get( $block_attributes, 'previewAsShortcode' ) ) {
return json_encode(
return wp_json_encode(
array(
'content' => $shortcode,
'script' => '',
Expand Down
12 changes: 10 additions & 2 deletions future/includes/gutenberg/blocks/view/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function Edit( { attributes, setAttributes, name: blockName } ) {
backLinkLabel,
previewBlock,
previewAsShortcode,
showPreviewImage
showPreviewImage,
} = attributes;

const previewImage = gkGravityViewBlocks[ blockName ]?.previewImage && <img className="preview-image" src={ gkGravityViewBlocks[ blockName ]?.previewImage } alt={ __( 'Block preview image.', 'gk-gravityview' ) } />;
Expand Down Expand Up @@ -98,7 +98,15 @@ export default function Edit( { attributes, setAttributes, name: blockName } ) {
<ViewSelector
viewId={ viewId }
isSidebar={ true }
onChange={ ( _viewId ) => setAttributes( { viewId: _viewId, previewBlock: previewBlock && !_viewId ? false : previewBlock } ) }
onChange={ ( _viewId ) => {
const selectedView = gkGravityViewBlocks.views.find( option => option.value === _viewId );

setAttributes( {
viewId: _viewId,
secret: selectedView?.secret,
previewBlock: previewBlock && !_viewId ? false : previewBlock,
} );
} }
/>

<PreviewControl
Expand Down
2 changes: 1 addition & 1 deletion future/includes/gutenberg/build/view.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-url'), 'version' => '1596599e826da2dc679a');
<?php return array('dependencies' => array('moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-url'), 'version' => '1255077c62b5acef6f65');
2 changes: 1 addition & 1 deletion future/includes/gutenberg/build/view.js

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions future/includes/gutenberg/class-gv-gutenberg-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GravityKit\GravityView\Gutenberg;

use GravityKit\GravityView\Foundation\Helpers\Arr;
use GV\View;
use GVCommon;

class Blocks {
Expand Down Expand Up @@ -224,15 +225,18 @@ public function get_views() {
);

$formatted_views = array_map(
function ( $view ) {
return array(
'value' => (string) $view->ID,
'label' => sprintf(
function ( $post ) {
$view = View::from_post( $post );

return [
'value' => (string) $view->ID,
'label' => sprintf(
'%s (#%d)',
$view->post_title ?: esc_html__( 'View', 'gk-gravityview' ),
$view->ID
),
);
'secret' => $view->get_validation_secret(),
];
},
$views
);
Expand Down

0 comments on commit dfe5808

Please sign in to comment.