From 25e0fb9ea537008d550607d00b0964ee2b2c41df Mon Sep 17 00:00:00 2001 From: Copons Date: Thu, 23 Jan 2020 17:34:04 +0000 Subject: [PATCH] Save the event ID as block attribute --- extensions/blocks/eventbrite/edit.js | 6 ++---- extensions/blocks/eventbrite/eventbrite.php | 22 ++++++--------------- extensions/blocks/eventbrite/index.js | 3 +++ extensions/blocks/eventbrite/save.js | 6 ++---- extensions/blocks/eventbrite/utils.js | 3 +-- 5 files changed, 14 insertions(+), 26 deletions(-) diff --git a/extensions/blocks/eventbrite/edit.js b/extensions/blocks/eventbrite/edit.js index 75408ce52bfab..ee0480839bae5 100644 --- a/extensions/blocks/eventbrite/edit.js +++ b/extensions/blocks/eventbrite/edit.js @@ -110,7 +110,7 @@ class EventbriteEdit extends Component { return; } - this.props.setAttributes( { url } ); + this.props.setAttributes( { eventId: eventIdFromUrl( url ), url } ); // Setting the `resolvingUrl` state here, then waiting for `componentDidUpdate()` to // be called before actually resolving it ensures that the `editedUrl` state has also been @@ -290,9 +290,7 @@ class EventbriteEdit extends Component { renderInlinePreview() { const { className } = this.props; - const { url } = this.props.attributes; - - const eventId = url ? eventIdFromUrl( url ) : null; + const { eventId } = this.props.attributes; if ( ! eventId ) { return; diff --git a/extensions/blocks/eventbrite/eventbrite.php b/extensions/blocks/eventbrite/eventbrite.php index b0bb980607d29..6110351e782e7 100644 --- a/extensions/blocks/eventbrite/eventbrite.php +++ b/extensions/blocks/eventbrite/eventbrite.php @@ -14,8 +14,7 @@ ) ); -const JETPACK_EVENTBRITE_ID_FROM_URL_REGEX = '(\d+)\/?\s*$'; -const JETPACK_EVENTBRITE_WIDGET_SLUG = 'eventbrite-widget'; +const JETPACK_EVENTBRITE_WIDGET_SLUG = 'eventbrite-widget'; /** * Eventbrite block registration/dependency delclaration. @@ -26,19 +25,11 @@ * @return string */ function jetpack_render_eventbrite_block( $attr, $content ) { - if ( empty( $attr['url'] ) ) { + if ( empty( $attr['eventId'] ) || empty( $attr['url'] ) ) { return ''; } - $matches = array(); - preg_match( '/' . JETPACK_EVENTBRITE_ID_FROM_URL_REGEX . '/', $attr['url'], $matches ); - $event_id = isset( $matches[1] ) && $matches[1] ? $matches[1] : null; - - if ( ! $event_id ) { - return ''; - } - - $widget_id = JETPACK_EVENTBRITE_WIDGET_SLUG . '-' . $event_id; + $widget_id = JETPACK_EVENTBRITE_WIDGET_SLUG . '-' . $attr['eventId']; wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true ); @@ -48,7 +39,7 @@ function jetpack_render_eventbrite_block( $attr, $content ) { 'eventbrite-widget', "window.EBWidgets.createWidget({ widgetType: 'checkout', - eventId: " . absint( $event_id ) . ", + eventId: " . absint( $attr['eventId'] ) . ", iframeContainerId: '" . esc_js( $widget_id ) . "', });" ); @@ -66,7 +57,7 @@ function jetpack_render_eventbrite_block( $attr, $content ) { 'eventbrite-widget', "window.EBWidgets.createWidget({ widgetType: 'checkout', - eventId: " . absint( $event_id ) . ", + eventId: " . absint( $attr['eventId'] ) . ", modal: true, modalTriggerElementId: '" . esc_js( $widget_id ) . "', });" @@ -89,8 +80,7 @@ function jetpack_eventbrite_block_editor_assets() { 'jetpack-blocks-editor', 'Jetpack_Block_Eventbrite_Settings', array( - 'event_id_from_url_regex' => JETPACK_EVENTBRITE_ID_FROM_URL_REGEX, - 'widget_slug' => JETPACK_EVENTBRITE_WIDGET_SLUG, + 'widget_slug' => JETPACK_EVENTBRITE_WIDGET_SLUG, ) ); } diff --git a/extensions/blocks/eventbrite/index.js b/extensions/blocks/eventbrite/index.js index 3704fd3bfa29f..f38ee46a515c7 100644 --- a/extensions/blocks/eventbrite/index.js +++ b/extensions/blocks/eventbrite/index.js @@ -48,6 +48,9 @@ export const settings = { url: { type: 'string', }, + eventId: { + type: 'number', + }, useModal: { type: 'boolean', }, diff --git a/extensions/blocks/eventbrite/save.js b/extensions/blocks/eventbrite/save.js index 78b6df1b49099..1b1ee5e0f7b65 100644 --- a/extensions/blocks/eventbrite/save.js +++ b/extensions/blocks/eventbrite/save.js @@ -7,7 +7,7 @@ import { RichText, getColorClassName } from '@wordpress/block-editor'; /** * Internal dependencies */ -import { createWidgetId, eventIdFromUrl } from './utils'; +import { createWidgetId } from './utils'; /** * Adapted button save function from @wordpress/block-library @@ -72,9 +72,7 @@ function saveButton( eventId, attributes ) { } export default function save( { attributes } ) { - const { useModal, url } = attributes; - - const eventId = eventIdFromUrl( url ); + const { eventId, useModal } = attributes; if ( ! eventId ) { return; diff --git a/extensions/blocks/eventbrite/utils.js b/extensions/blocks/eventbrite/utils.js index 795b1d1af9f9c..d5f9284209a7b 100644 --- a/extensions/blocks/eventbrite/utils.js +++ b/extensions/blocks/eventbrite/utils.js @@ -27,8 +27,7 @@ export function eventIdFromUrl( url ) { return null; } - const regex = new RegExp( window.Jetpack_Block_Eventbrite_Settings.event_id_from_url_regex ); - const match = url.match( regex ); + const match = url.match( /(\d+)\/?\s*$/ ); return match && match[ 1 ] ? match[ 1 ] : null; }