Skip to content

Commit 92eabd8

Browse files
author
Chris Gårdenberg
committed
feat: New type of script, when you use the booking form in the API.
Allows you to add script when the booking form from the API is used, rather than the plugin. This will enable you to add scripts that handle the `postMessage` messages from the booking form, and you can track purchases in your website with Google Analytics or any other analytics tool, that uses JavaScript. fixes #412
1 parent 7750b1e commit 92eabd8

9 files changed

Lines changed: 88 additions & 10 deletions

File tree

PLUGIN-CHECKSUM

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fb23ff65682db89695b43cc14c087c38
1+
6e94e3062bad0f92f73cb3a6950f6e11

content/template/bookingTemplate/template_A.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
<iframe id="eduadmin-booking-frame" class="edu-bookingform-page-frame"
1616
src="<?php echo esc_attr( $event['BookingFormUrl'] ); ?>"></iframe>
1717
<?php
18+
if ( ! key_exists( 'eduadmin-booking-form-javascript-set', $GLOBALS ) && ! empty( trim( EDU()->get_option( 'eduadmin-booking-form-javascript', '' ) ) ) ) {
19+
?>
20+
<script type="text/javascript">
21+
<?php echo EDU()->get_option( 'eduadmin-booking-form-javascript', '' ); ?>
22+
</script>
23+
<?php
24+
$GLOBALS['eduadmin-booking-form-javascript-set'] = true;
25+
}
1826
} else {
1927
echo _x( 'The booking form needs configuration in EduAdmin before this works.', 'frontend', 'eduadmin-booking' );
2028
}
@@ -304,7 +312,7 @@ class="backLink"><?php echo esc_html_x( '« Go back', 'frontend', 'eduadmin-book
304312
eduBookingView.SingleParticipant = <?php echo esc_js( $single_person_booking ? "true" : "false" ); ?>;
305313
eduBookingView.ProgrammeBooking = false;
306314
eduBookingView.ForceContactCivicRegNo = <?php echo( $selected_course['RequireCivicRegistrationNumber'] ? 'true' : 'false' ); ?>;
307-
eduBookingView.MaxParticipants = <?php echo esc_js( $event['ParticipantNumberLeft'] != null ? intval( $event['ParticipantNumberLeft'] ) : -1 ); ?>;
315+
eduBookingView.MaxParticipants = <?php echo esc_js( $event['ParticipantNumberLeft'] != null ? intval( $event['ParticipantNumberLeft'] ) : - 1 ); ?>;
308316
eduBookingView.AddParticipant();
309317
eduBookingView.CheckPrice(false);
310318
})();

content/template/detailTemplate/blocks/event-list.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
$event_interest_page = EDU()->get_option( 'eduadmin-interestEventPage' );
1313
$allow_interest_reg_event = EDU()->is_checked( 'eduadmin-allowInterestRegEvent', false );
14-
$show_more = ! empty( $attributes['showmore'] ) ? $attributes['showmore'] : -1;
14+
$show_more = ! empty( $attributes['showmore'] ) ? $attributes['showmore'] : - 1;
1515

1616
$use_eduadmin_form = EDU()->is_checked( 'eduadmin-useBookingFormFromApi' );
1717

@@ -21,6 +21,17 @@
2121

2222
$is_ondemand = $selected_course['OnDemand'];
2323

24+
if ( $use_eduadmin_form ) {
25+
if ( ! key_exists( 'eduadmin-booking-form-javascript-set', $GLOBALS ) && ! empty( trim( EDU()->get_option( 'eduadmin-booking-form-javascript', '' ) ) ) ) {
26+
?>
27+
<script type="text/javascript">
28+
<?php echo EDU()->get_option( 'eduadmin-booking-form-javascript', '' ); ?>
29+
</script>
30+
<?php
31+
$GLOBALS['eduadmin-booking-form-javascript-set'] = true;
32+
}
33+
}
34+
2435
?>
2536
<?php if ( $allow_region_search && empty( $_GET['eid'] ) && ! $is_ondemand ) : ?>
2637
<h3><?php esc_html_e( 'Region', 'eduadmin-booking' ); ?></h3>
@@ -54,7 +65,7 @@
5465
}
5566
include 'event-item.php';
5667
$last_city = $ev['City'];
57-
$i++;
68+
$i ++;
5869
}
5970
}
6071
if ( empty( $prices ) || empty( $events ) ) {

content/template/listTemplate/list-events.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
$order = array();
5050
$custom_order = explode( ' ', $custom_order_by_order );
5151
foreach ( $custom_order as $coVal ) {
52-
! isset( $coVal ) || $coVal === "asc" ? array_push( $order, 1 ) : array_push( $order, -1 );
52+
! isset( $coVal ) || $coVal === "asc" ? array_push( $order, 1 ) : array_push( $order, - 1 );
5353
}
5454
}
5555
} else {
@@ -171,6 +171,17 @@
171171

172172
do_action( 'eduadmin-list-event-view', $events );
173173

174+
if ( $use_eduadmin_form ) {
175+
if ( ! key_exists( 'eduadmin-booking-form-javascript-set', $GLOBALS ) && ! empty( trim( EDU()->get_option( 'eduadmin-booking-form-javascript', '' ) ) ) ) {
176+
?>
177+
<script type="text/javascript">
178+
<?php echo EDU()->get_option( 'eduadmin-booking-form-javascript', '' ); ?>
179+
</script>
180+
<?php
181+
$GLOBALS['eduadmin-booking-form-javascript-set'] = true;
182+
}
183+
}
184+
174185
?>
175186
<div class="eventListTable" data-eduwidget="listview-eventlist"
176187
data-template="<?php echo esc_attr( str_replace( 'template_', '', $attributes['template'] ) ); ?>"

content/template/listTemplate/template_A_listEvents.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626

2727
include 'blocks/event-block-a.php';
28-
$current_events++;
28+
$current_events ++;
2929
}
3030
?>
3131
</div><!-- /eventlist -->

content/template/programme/book.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
<iframe id="eduadmin-booking-frame" class="edu-bookingform-page-frame"
2727
src="<?php echo esc_attr( $programme['BookingFormUrl'] ); ?>"></iframe>
2828
<?php
29+
if ( ! empty( trim( EDU()->get_option( 'eduadmin-booking-form-javascript', '' ) ) ) ) {
30+
?>
31+
<script type="text/javascript">
32+
<?php echo EDU()->get_option( 'eduadmin-booking-form-javascript', '' ); ?>
33+
</script>
34+
<?php
35+
$GLOBALS['eduadmin-booking-form-javascript-set'] = true;
36+
}
2937
} else {
3038
echo _x( 'The booking form needs configuration in EduAdmin before this works.', 'frontend', 'eduadmin-booking' );
3139
}

includes/booking-settings.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,37 @@ function edu_render_booking_settings_page() {
2424
<br />
2525
<em><?php echo esc_html_x( 'By enabling this option, all options below will be ineffective, as we use an external booking form from EduAdmin instead.', 'backend', 'eduadmin-booking' ); ?></em>
2626
<br />
27+
<h3><?php echo esc_html_x( 'Javascript to run the booking form is used', 'backend', 'eduadmin-booking' ); ?></h3>
28+
<em><?php echo esc_html_x( 'Do not include &lt;script&gt; here, it is handled automatically.', 'backend', 'eduadmin-booking' ); ?></em>
29+
<table>
30+
<tr>
31+
<td style="vertical-align: top;">
32+
<textarea class="form-control" rows="12" cols="60"
33+
name="eduadmin-booking-form-javascript"><?php echo EDU()->get_option( 'eduadmin-booking-form-javascript' ); ?></textarea>
34+
</td>
35+
<td style="vertical-align: top;">
36+
The booking form will use
37+
<code>window.parent.postMessage</code> to post data from the form.
38+
<br />
39+
The event you want to keep track of it <code>eduadmin-order</code>.
40+
<br />
41+
<br />
42+
Example code to listen to the <code>eduadmin-order</code> "event":
43+
<br />
44+
45+
<pre>
46+
window.addEventListener('message', function (event) {
47+
if (event.data.type == "eduadmin-order") {
48+
// Here's where you'll write your custom code,
49+
// to handle the event from the booking form.
50+
// We'll just output it to the console log for now.
51+
console.log(event.data);
52+
}
53+
}, false);</pre>
54+
55+
</td>
56+
</tr>
57+
</table>
2758
<div
2859
class="non-eduform-options"<?php echo( ! EDU()->is_checked( 'eduadmin-useBookingFormFromApi', false ) ? ' style="display: block;"' : ' style="display: none;"' ); ?>>
2960
<?php
@@ -56,11 +87,11 @@ function edu_write_options( $g, $array, $depth, $selected_option ) {
5687
esc_html( wp_strip_all_tags( $g['CustomerGroupName'] ) ) .
5788
"</option>\n";
5889
if ( array_key_exists( (string) $g['CustomerGroupId'], $array ) ) {
59-
$depth++;
90+
$depth ++;
6091
foreach ( $array[ (string) $g['CustomerGroupId'] ] as $ng ) {
6192
edu_write_options( $ng, $array, $depth, $selected_option );
6293
}
63-
$depth--;
94+
$depth --;
6495
}
6596
}
6697

includes/edu-options.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ function eduadmin_settings_init() {
204204
register_setting( 'eduadmin-booking', 'eduadmin-loginField' );
205205
register_setting( 'eduadmin-booking', 'eduadmin-allowCustomerUpdate' );
206206

207+
register_setting( 'eduadmin-booking', 'eduadmin-booking-form-javascript' );
208+
207209
register_setting( 'eduadmin-booking', 'eduadmin-dontSendConfirmation' );
208210

209211
register_setting( 'eduadmin-booking', 'eduadmin-singlePersonBooking' );

includes/edu-shortcodes.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ function eduadmin_get_detailinfo( $attributes ) {
525525

526526
$last_city = '';
527527

528-
$show_more = isset( $attributes['showmore'] ) && ! empty( $attributes['showmore'] ) ? $attributes['showmore'] : -1;
528+
$show_more = isset( $attributes['showmore'] ) && ! empty( $attributes['showmore'] ) ? $attributes['showmore'] : - 1;
529529
$spot_left_option = EDU()->get_option( 'eduadmin-spotsLeft', 'exactNumbers' );
530530
$always_few_spots = EDU()->get_option( 'eduadmin-alwaysFewSpots', '3' );
531531
$spot_settings = EDU()->get_option( 'eduadmin-spotsSettings', "1-5\n5-10\n10+" );
@@ -581,7 +581,7 @@ function eduadmin_get_detailinfo( $attributes ) {
581581
$ret_str .= ob_get_clean();
582582

583583
$last_city = $ev['City'];
584-
$i++;
584+
$i ++;
585585
}
586586
}
587587
if ( empty( $events ) ) {
@@ -591,6 +591,13 @@ function eduadmin_get_detailinfo( $attributes ) {
591591
$ret_str .= '<div class="eventShowMore"><a class="neutral-btn" href="javascript://" onclick="eduDetailView.ShowAllEvents(\'eduev' . esc_attr( ( $group_by_city ? '-' . $last_city : '' ) ) . '\', this);">' . esc_html_x( 'Show all events', 'frontend', 'eduadmin-booking' ) . '</a></div>';
592592
}
593593
$ret_str .= '</div></div>';
594+
595+
if ( $use_eduadmin_form ) {
596+
if ( ! key_exists( 'eduadmin-booking-form-javascript-set', $GLOBALS ) && ! empty( trim( EDU()->get_option( 'eduadmin-booking-form-javascript', '' ) ) ) ) {
597+
$ret_str .= '<script type="text/javascript">' . EDU()->get_option( 'eduadmin-booking-form-javascript', '' ) . '</script>';
598+
$GLOBALS['eduadmin-booking-form-javascript-set'] = true;
599+
}
600+
}
594601
}
595602
}
596603
}

0 commit comments

Comments
 (0)