Start Time Changes When Error Message is Returned

Home Forums Calendar Products Community Events Start Time Changes When Error Message is Returned

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1035830
    Sean
    Participant

    Hi,

    We are using some code you supplied in snippets to alter the default Start Time and End Time for events on the Community Events: Submit an Event form. It works fine, except that when an error message is returned (because the user left a required field blank, etc), the Start Time is changed to be the same as the End Time that was entered when the user first tried to submit the form.

    We’ve confirmed that the issue only occurs with this snippet activated. How can we modify this so that when error messages are returned on the Community Events: Submit an Event form, the Start Time that was originally entered is not altered?

    /**
     * Helps to change the default start/end times for new event.
     */
    class Change_Tribe_Default_Event_Times
    {
        const TWELVEHOUR     = 12;
        const TWENTYFOURHOUR = 24;
    
        protected $start    = 9;
        protected $end      = 11;
        protected $mode     = self::TWELVEHOUR;
        protected $mode_set = false;
    
        /**
         * Provide the desired default start and end hours in 24hr format (ie 15 = 3pm).
         *
         * @param $start_hour
         * @param $end_hour
         */
        public function __construct( $start_hour, $end_hour ) {
            $this->settings( $start_hour, $end_hour );
            $this->add_filters();
        }
    
        protected function settings( $start_hour, $end_hour ) {
            $this->start = $this->safe_hour( $start_hour );
            $this->end   = $this->safe_hour( $end_hour );
        }
    
        protected function add_filters() {
            add_filter( 'tribe_get_hour_options', array( $this, 'change_default_time' ), 10, 3 );
            add_filter( 'tribe_get_meridian_options', array( $this, 'change_default_meridian' ), 10, 3 );
        }
    
        protected function set_mode() {
            if ( $this->mode_set ) return;
    
            if ( strstr( get_option('time_format', $this->time_format() ), 'H' ) )
                $this->mode = self::TWENTYFOURHOUR;
    
            $this->mode_set = true;
        }
    
        protected function safe_hour( $hour ) {
            $hour = absint( $hour );
            if ( $hour < 0 )  $hour = 0;
            if ( $hour > 23 ) $hour = 23;
            return $hour;
        }
    
        public function change_default_time( $hour, $date, $isStart ) {
            $this->set_mode();
            if ( 'post-new.php' !== $GLOBALS['pagenow'] ) return $hour; // Only intervene if it's a new event
    
            if ( $isStart ) return $this->corrected_time( $this->start );
            else return $this->corrected_time( $this->end );
        }
    
        /**
         * If working in the 12hr clock, converts the hour appropriately.
         *
         * @param $hour
         * @return int
         */
        protected function corrected_time( $hour ) {
            $this->set_mode();
            if ( self::TWENTYFOURHOUR === $this->mode ) return $hour;
            if ( $hour > 12 ) return $hour - 12;
            return $hour;
        }
    
        public function change_default_meridian( $meridian, $date, $isStart ) {
            if ( 'post-new.php' !== $GLOBALS['pagenow'] ) return $meridian; // Only intervene if it's a new event
    
            $meridian = 'am';
            if ( $isStart && 12 <= $this->start ) $meridian = 'pm';
            if ( ! $isStart && 12 <= $this->end ) $meridian = 'pm';
    
            if ( strstr( get_option('time_format', $this->time_format() ), 'A' ) )
                $meridian = strtoupper( $meridian );
    
            return $meridian;
        }
    
        protected function time_format() {
            if ( class_exists( 'Tribe__Events__Date_Utils' ) ) return Tribe__Events__Date_Utils::TIMEFORMAT;
            if ( class_exists( 'TribeDateUtils') ) return TribeDateUtils::TIMEFORMAT;
            return 'g:i A';
        }
    }
    new Change_Tribe_Default_Event_Times(0, 0);
    
    // Change Start Time on Community Events Submit an Event form
    add_action( 'tribe_community_events_form_start_time_selector', 'comm_default_start_time', 10, 2 );
    function comm_default_start_time( $output, $event_id ) {
    
        if ( !$event_id ) {
    
            $new_time = strtotime( '12am' );
    
            $start_date = tribe_event_format_date( $new_time, true, Tribe__Events__Date_Utils::DBDATETIMEFORMAT );
    
            $start_minutes     = Tribe__Events__View_Helpers::getMinuteOptions( $start_date, true );
            $start_hours = Tribe__Events__View_Helpers::getHourOptions( $is_all_day == 'yes' ? null : $start_date, true );
            $start_meridian = Tribe__Events__View_Helpers::getMeridianOptions( $start_date, true );
    
            $output = '';
            $output .= sprintf( '<select name="EventStartHour">%s</select>', $start_hours );
            $output .= sprintf( '<select name="EventStartMinute">%s</select>', $start_minutes );
            if ( ! tribe_community_events_use_24hr_format() ) {
                $output .= sprintf( '<select name="EventStartMeridian">%s</select>', $start_meridian );
            }
        }
    
        return $output;
    }
    
    add_action( 'tribe_community_events_form_end_time_selector', 'comm_default_end_time', 10, 2 );
    function comm_default_end_time( $output, $event_id ) {
    
        if ( !$event_id ) {
    
            $new_time = strtotime( '12am' );
    
            $start_date = tribe_event_format_date( $new_time, true, Tribe__Events__Date_Utils::DBDATETIMEFORMAT );
    
            $start_minutes     = Tribe__Events__View_Helpers::getMinuteOptions( $start_date, true );
            $start_hours = Tribe__Events__View_Helpers::getHourOptions( $is_all_day == 'yes' ? null : $start_date, true );
            $start_meridian = Tribe__Events__View_Helpers::getMeridianOptions( $start_date, true );
    
            $output = '';
            $output .= sprintf( '<select name="EventStartHour">%s</select>', $start_hours );
            $output .= sprintf( '<select name="EventStartMinute">%s</select>', $start_minutes );
            if ( ! tribe_community_events_use_24hr_format() ) {
                $output .= sprintf( '<select name="EventStartMeridian">%s</select>', $start_meridian );
            }
        }
    
        return $output;
    }
    #1036938
    Brian
    Member

    Hi,

    The patch we are adding to fix it so all fields retain their value on a required field error might resolve this.

    I do not think changing the snippet itself can help with the issue.

    Did this work in 3.12?

    Thanks

    #1037039
    Sean
    Participant

    Okay that’s good to hear a fix is on the way. Is this fix (so all fields retain their value on a required field error) set to be released in the next maintenance release, 4.0.1?

    I’m not sure if it occurred in 3.12 as we didn’t start testing/QA until 4.0 beta was available.

    #1037187
    Brian
    Member

    Okay that’s good to hear a fix is on the way. Is this fix (so all fields retain their value on a required field error) set to be released in the next maintenance release, 4.0.1?

    It is being worked on with a hopefully release within days, but that could change if something comes up.

    #1038474
    Sean
    Participant

    Okay sounds good Brian. Thank you.

    #1038942
    Brian
    Member

    I am going to close this ticket to new replies, but the issue is still open and we will update this ticket once we release a fix.

    If you have any new questions or issues please create a new ticket and reference this one.

    Thanks

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Start Time Changes When Error Message is Returned’ is closed to new replies.