Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compatible with the events calendar #1353

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Vue.component('field-option-data', {
show_value: false,
sync_value: true,
options: [],
selected: []
selected: [],
display: !this.editing_form_field.hide_option_data // hide this field for the events calendar
};
},

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="panel-field-opt panel-field-opt-text">
<div v-show="display" class="panel-field-opt panel-field-opt-text">
<div>
{{ option_field.title }} <help-text v-if="option_field.help_text" :text="option_field.help_text"></help-text>
<ul class="pull-right list-inline field-option-actions">
Expand Down Expand Up @@ -40,7 +40,6 @@
<div class="action-buttons">&nbsp;</div>
</li>
</ul>

<ul :class="['option-field-option-chooser margin-0', show_value ? 'show-value' : '']">
<li v-for="(option, index) in options" :key="option.id" :data-index="index" class="clearfix option-field-option">
<div class="selector">
Expand Down
3 changes: 1 addition & 2 deletions assets/js-templates/form-components.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
</script>

<script type="text/x-template" id="tmpl-wpuf-field-option-data">
<div class="panel-field-opt panel-field-opt-text">
<div v-show="display" class="panel-field-opt panel-field-opt-text">
<div>
{{ option_field.title }} <help-text v-if="option_field.help_text" :text="option_field.help_text"></help-text>
<ul class="pull-right list-inline field-option-actions">
Expand Down Expand Up @@ -160,7 +160,6 @@
<div class="action-buttons">&nbsp;</div>
</li>
</ul>

<ul :class="['option-field-option-chooser margin-0', show_value ? 'show-value' : '']">
<li v-for="(option, index) in options" :key="option.id" :data-index="index" class="clearfix option-field-option">
<div class="selector">
Expand Down
3 changes: 2 additions & 1 deletion assets/js/wpuf-form-builder-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ Vue.component('field-option-data', {
show_value: false,
sync_value: true,
options: [],
selected: []
selected: [],
display: !this.editing_form_field.hide_option_data // hide this field for the events calendar
};
},

Expand Down
85 changes: 85 additions & 0 deletions class/post-form-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ abstract class WPUF_Post_Form_Template {
*/
protected $form_notifications;

/**
* The datetime format for The Event Calender
*
* @since WPUF_SINCE
*
* @var string
*/
const TIB_DATETIME_FORMAT = 'Y-m-d H:i:s';

public function __construct() {
$this->conditionals = [
'condition_status' => 'no',
Expand Down Expand Up @@ -141,6 +150,82 @@ public function is_enabled() {
* @return void
*/
public function after_insert( $post_id, $form_id, $form_settings ) {
// we can return form here if it is not a 'The Event Calendar' event
if ( class_exists( 'Tribe__Events__Main' ) && version_compare( Tribe__Events__Main::VERSION, 6, '<' ) ) {
$timezone = get_option( 'timezone_string', 'UTC+0' );
$start_date = wpuf_current_datetime()->format( self::TIB_DATETIME_FORMAT );
$end_date = wpuf_current_datetime()->format( self::TIB_DATETIME_FORMAT );
$start_date_utc = wpuf_current_datetime()->setTimezone( $timezone )->format( self::TIB_DATETIME_FORMAT );
$end_date_utc = wpuf_current_datetime()->setTimezone( $timezone )->format( self::TIB_DATETIME_FORMAT );

$meta_to_update = [];
$meta_to_delete = [];

if ( 'yes' === $post_data['_EventAllDay'] ) {
$p1d = new DateInterval( 'PT23H59M59S' );
$new_start_date = $start_date;
$new_end_date = $end_date;

$meta_to_update['_EventAllDay'] = $post_data['_EventAllDay'];
$meta_to_update['_EventStartDate'] = $new_start_date;
$meta_to_update['_EventEndDate'] = $new_end_date;
$meta_to_update['_EventStartDateUTC'] = $start_date_utc;
$meta_to_update['_EventEndDateUTC'] = $end_date_utc;
} else {
$meta_to_delete[] = '_EventAllDay';
$meta_to_update['_EventStartDate'] = $start_date;
$meta_to_update['_EventEndDate'] = $end_date;
$meta_to_update['_EventStartDateUTC'] = $start_date_utc;
$meta_to_update['_EventEndDateUTC'] = $end_date_utc;
$meta_to_update['_EventDuration'] = 32400;
}

foreach ( $meta_to_update as $meta_key => $meta_value ) {
update_post_meta( $post_id, $meta_key, $meta_value );
}

foreach ( $meta_to_delete as $meta_key ) {
delete_post_meta( $post_id, $meta_key );
}
}

$event_data = [
'EventAllDay' => ! empty( $post_data['_EventAllDay'] ) ? $post_data['_EventAllDay'] : 'yes',
'EventStartDate' => ! empty( $post_data['_EventStartDate'] ) ? $post_data['_EventStartDate'] : wpuf_current_datetime()->format( self::TIB_DATETIME_FORMAT ),
'EventEndDate' => ! empty( $post_data['_EventEndDate'] ) ? $post_data['_EventEndDate'] : wpuf_current_datetime()->format( self::TIB_DATETIME_FORMAT ),
'EventTimezone' => ! empty( $post_data['_EventTimeZone'] ) ? $post_data['_EventTimeZone'] : get_option( 'timezone_string', 'UTC+0' ),
];

if ( 'no' === $event_data['EventAllDay'] ) {
$event_data = [
'EventStartTime' => wpuf_current_datetime()->modify( $event_data['EventStartDate'] )->format( 'h:ia' ),
'EventEndTime' => wpuf_current_datetime()->modify( $event_data['EventEndDate'] )->format( 'h:ia' ),
];
}

$tribe_api = WP_PLUGIN_DIR . '/the-events-calendar/src/Tribe/API.php';

require_once $tribe_api;

/**
* Opportunity to change 'The Event Calendar' metadata just before WPUF is saving it to DB
*
* @since WPUFPRO_SINCE
*
* @param array $event_data The event metadata
* @param int $post_id The post id, in other words, The Event
*/
$event_data = apply_filters( 'wpuf_tib_event_meta', $event_data, $post_id );
Tribe__Events__API::saveEventMeta( $post_id, $event_data );

/**
* Hook fired just after WPUF is saved 'The Event Calendar' metadata to the DB
*
* @since WPUFPRO_SINCE
*
* @param int $post_id The post_id, in other words, the event_id
*/
do_action( 'wpuf_tib_after_saving_event_meta', $post_id );
}

/**
Expand Down
22 changes: 22 additions & 0 deletions includes/class-frontend-form-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,28 @@ public function submit_post() {
$guest_verify = isset( $this->form_settings['guest_email_verify'] ) ? $this->form_settings['guest_email_verify'] : 'false';
$attachments_to_delete = isset( $_POST['delete_attachments'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['delete_attachments'] ) ) : [];


// validation for The Events Calendar
if ( 'tribe_events' === $this->form_settings['post_type'] ) {
$start_date = ! empty( $_POST['_EventStartDate'] ) ? sanitize_text_field( wp_unslash( $_POST['_EventStartDate'] ) ) : '';
$end_date = ! empty( $_POST['_EventEndDate'] ) ? sanitize_text_field( wp_unslash( $_POST['_EventEndDate'] ) ) : '';

if ( empty( $start_date ) || empty( $end_date ) ) {
return;
}
try {
$origin = new DateTimeImmutable( $start_date );
$target = new DateTimeImmutable( $end_date );
$interval = $origin->diff( $target );

if ( $interval->invert ) {
$this->send_error( __( 'Event end date should be after event start date', 'wp-user-frontend' ) );
}
} catch ( Exception $ex ) {
$this->send_error( __( 'Error processing event date', 'wp-user-frontend' ) );
}
}

// check each form field for content restriction
foreach ( $this->form_fields as $single_field ) {
if ( empty( $single_field['content_restriction'] ) ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/class-frontend-render-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ public static function prepare_meta_fields( $meta_vars ) {
if ( is_array( $value_name ) && ! empty( $value_name ) ) {
$meta_key_value[ $value['name'] ] = implode( self::$separator, $value_name );
} else {
$meta_key_value[ $value['name'] ] = $value_name[0];
$meta_key_value[ $value['name'] ] = ! empty( $value_name[0] ) ? $value_name[0] : '';
}
break;

Expand Down
4 changes: 3 additions & 1 deletion includes/fields/class-abstract-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ public static function get_default_text_option_settings( $content_restriction =
* @return array
*/
public function get_default_option_dropdown_settings( $is_multiple = false ) {
return [
$defaults = [
'name' => 'options',
'title' => __( 'Options', 'wp-user-frontend' ),
'type' => 'option-data',
Expand All @@ -628,6 +628,8 @@ public function get_default_option_dropdown_settings( $is_multiple = false ) {
'priority' => 12,
'help_text' => __( 'Add options for the form field', 'wp-user-frontend' ),
];

return apply_filters( 'get_default_option_dropdown_settings', $defaults );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions includes/fields/class-field-dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public function render( $field_settings, $form_id, $type = 'post', $post_id = nu
}

/* Workaround for Events calendar venue and organizer field render in wpuf custom fields metabox */
if ( 'tribe_events' == get_post_type( $post_id ) ) {
// if ( 'tribe_events' == get_post_type( $post_id ) ) {
if ( '_EventVenueID' == $field_settings['name'] ) {
$field_settings['options'] = $this->get_posts( 'tribe_venue' );
} else if ( '_EventOrganizerID' == $field_settings['name'] ) {
$field_settings['options'] = $this->get_posts( 'tribe_organizer' );
}
}
// }

$name = $field_settings['name'];

Expand Down
79 changes: 45 additions & 34 deletions includes/free/post-form-templates/the_events_calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,49 +74,60 @@ public function __construct() {
'wpuf_cond' => $this->conditionals,
],
[
'input_type' => 'radio',
'template' => 'radio_field',
'required' => 'no',
'label' => __( 'All Day Event', 'wp-user-frontend' ),
'name' => '_EventAllDay',
'is_meta' => 'yes',
'selected' => [],
'inline' => 'yes',
'options' => [
'input_type' => 'radio',
'template' => 'radio_field',
'required' => 'no',
'label' => __( 'All Day Event', 'wpuf-pro' ),
'name' => '_EventAllDay',
'is_meta' => 'yes',
'inline' => 'yes',
'options' => [
'yes' => 'Yes',
'no' => 'No',
],
'wpuf_cond' => $this->conditionals,
'wpuf_cond' => $this->conditionals,
'wpuf_visibility' => $this->get_default_visibility_prop(),
],
[
'input_type' => 'url',
'template' => 'website_url',
'required' => 'no',
'label' => __( 'Event Website', 'wp-user-frontend' ),
'name' => '_EventURL',
'is_meta' => 'yes',
'width' => 'large',
'size' => 40,
'wpuf_cond' => $this->conditionals,
'input_type' => 'url',
'template' => 'website_url',
'required' => 'no',
'label' => __( 'Event Website', 'wpuf-pro' ),
'name' => '_EventURL',
'is_meta' => 'yes',
'placeholder' => '',
'default' => '',
'width' => 'large',
'size' => 40,
'wpuf_cond' => $this->conditionals,
'wpuf_visibility' => $this->get_default_visibility_prop(),
],
[
'input_type' => 'text',
'template' => 'text_field',
'required' => 'no',
'label' => __( 'Currency Symbol', 'wp-user-frontend' ),
'name' => '_EventCurrencySymbol',
'is_meta' => 'yes',
'size' => 40,
'wpuf_cond' => $this->conditionals,
'input_type' => 'text',
'template' => 'text_field',
'required' => 'no',
'label' => __( 'Currency Symbol', 'wpuf-pro' ),
'name' => '_EventCurrencySymbol',
'is_meta' => 'yes',
'default' => '',
'placeholder' => '',
'size' => '',
'wpuf_cond' => $this->conditionals,
'wpuf_visibility' => $this->get_default_visibility_prop(),
],
[
'input_type' => 'text',
'template' => 'text_field',
'required' => 'no',
'label' => __( 'Cost', 'wp-user-frontend' ),
'name' => '_EventCost',
'is_meta' => 'yes',
'wpuf_cond' => $this->conditionals,
'input_type' => 'text',
'template' => 'text_field',
'required' => 'no',
'label' => __( 'Cost', 'wpuf-pro' ),
'name' => '_EventCost',
'is_meta' => 'yes',
'default' => '',
'placeholder' => '',
'size' => '',
'help' => 'Enter a 0 for events that are free or leave blank to hide the field.',
'wpuf_cond' => $this->conditionals,
'wpuf_visibility' => $this->get_default_visibility_prop(),
],
[
'input_type' => 'image_upload',
Expand Down
2 changes: 1 addition & 1 deletion wpuf-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,7 @@ function taxnomy_select( $terms, $attr ) {
/**
* Returns form setting value
*
* @param init $form_id
* @param int $form_id
* @param boolen $status
*
* @return array
Expand Down