Skip to content

Commit

Permalink
Save the event ID as block attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Copons committed Jan 23, 2020
1 parent 1691393 commit 25e0fb9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 26 deletions.
6 changes: 2 additions & 4 deletions extensions/blocks/eventbrite/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 6 additions & 16 deletions extensions/blocks/eventbrite/eventbrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 );

Expand All @@ -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 ) . "',
});"
);
Expand All @@ -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 ) . "',
});"
Expand All @@ -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,
)
);
}
Expand Down
3 changes: 3 additions & 0 deletions extensions/blocks/eventbrite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export const settings = {
url: {
type: 'string',
},
eventId: {
type: 'number',
},
useModal: {
type: 'boolean',
},
Expand Down
6 changes: 2 additions & 4 deletions extensions/blocks/eventbrite/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions extensions/blocks/eventbrite/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 25e0fb9

Please sign in to comment.