Custom Fields
Re-Envisioned
Support General Clearing timestamp when changing date field to empty value

  • Creator
    Topic
  • #17333
    Johannes Gross
    Participant

    Just noticed that the timestamp value remains saved when deleting the value of a datetime field (did not try it with a date field). However, the display string is removed from storage.
    As a result, if I try to filter the timestamp after removing the datetime value, it still applies the old timestamp value.
    Here is my code:

    add_filter( 'rwmb_meta_boxes', 'twt_register_meta_boxes' );
    function twt_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array(
            'id'         => 'specials-c',
            'title'      => 'Specials',
            'post_types' => 'page',
            'context'    => 'normal',
            'priority'   => 'high',
            'revision' => true,
            'include' => array(
                'ID' => array(14),
            ),
            'fields' => array(
                array(
                    'name'   => '',
                    'id'     => 'specials',
                    'type'   => 'group',
                    'clone'  => true,
                    'sort_clone' => true,
                    'collapsible' => true,
                    'default_state' => 'collapsed',
                    'group_title' => '{title} {date_display}',
                    'fields' => array(
                        array(
                            'name' => 'Featured Image',
                            'id'   => 'img',
                            'type' => 'single_image',
                            'image_size' => 'medium'
                        ),
                        array(
                            'name' => 'Make Headline Image Clickable',
                            'desc' => 'to open up larger',
                            'id'   => 'img_clickable',
                            'type' => 'checkbox',
                            'image_size' => 'medium'
                        ),
                        array(
                            'name' => 'Date Display',
                            'desc' => 'e.g. September 6-13, 2019',
                            'id'   => 'date_display',
                            'type' => 'text',
                        ),
                        array(
                            'name' => 'Time Display',
                            'desc' => 'e.g. Noon-5pm',
                            'id'   => 'time_display',
                            'type' => 'text',
                        ),
                        array(
                            'name' => 'Title',
                            'id'   => 'title',
                            'type' => 'textarea',
                            'rows' => 2,
                        ),
                        array(
                            'name' => 'Content',
                            'id'   => 'content',
                            'type' => 'wysiwyg',
                            'options' => array(
                                'textarea_rows' => 6,
                                'teeny'         => false,
                            ),
                        ),
                        array(
                            'name' => '(Optional) Start Display At',
                            'desc' => 'Keep empty to start displaying right away',
                            'id'   => 'display_start',
                            'type' => 'datetime',
                            'js_options' => array(
                                'stepMinute'      => 15,
                                'showTimepicker'  => true,
                                'controlType'     => 'select',
                                'showButtonPanel' => false,
                                'oneLine'         => true,
                            ),
                            'inline'     => true,
                            'timestamp'  => true,
                        ),
                        array(
                            'name' => '(Optional) End Display At',
                            'desc' => 'Keep empty to have the website not automatically hide it',
                            'id'   => 'display_end',
                            'type' => 'datetime',
                            'js_options' => array(
                                'stepMinute'      => 15,
                                'showTimepicker'  => true,
                                'controlType'     => 'select',
                                'showButtonPanel' => false,
                                'oneLine'         => true,
                            ),
                            'inline'     => true,
                            'timestamp'  => true,
                        ),
                        array(
                            'name' => 'Inactive',
                            'id'   => 'inactive',
                            'type' => 'checkbox',
                            'desc'  => 'to preview inactive specials add ?preview to the URL'
                        ),
                    ),
                ),
            )
        );
        return $meta_boxes;
    }
    add_shortcode('twt_specials', 'twt_specials_shortcode');
    function twt_specials_shortcode() {
        ob_start();
        $specials = get_post_meta(get_the_ID(), 'specials', true);
        ?>
        <div class="twt-specials-listings">
            <?php
            foreach($specials as $special) {
                if(!empty($special['inactive']) && !isset($_GET['preview'])) {
                    continue;
                }
            $local_timestamp = current_time( 'timestamp' );
            if ( ! empty( $special['display_start']['timestamp'] ) && $special['display_start']['timestamp'] > $local_timestamp ) {
                continue;
            }
            if ( ! empty( $special['display_end']['timestamp'] ) && $special['display_end']['timestamp'] < $local_timestamp ) {
                continue;
            }
                ?>
                <div class="twt-special-listing-c">
                <div class="twt-special-listing chalkboard-bg content has-black-background-color">
                    <?php if(!empty($special['img'])) {
                        if(!empty($special['img_clickable'])) {
                            $url = wp_get_attachment_url($special['img']);
                            echo '<a href="'.$url.'">';
                        }
                        echo wp_get_attachment_image($special['img'], 'medium_large', false, array('class' => 'special-img'));
                        if(!empty($special['img_clickable'])) {
                            echo '</a>';
                        }
                    }
                    if(!empty($special['date_display']) || !empty($special['time_display'])) {
                        ?>
                        <div class="special-meta row justify-content-center extra-padding">
                            <?php if(!empty($special['date_display'])) { ?>
                                <div class="col-auto"><i class="icon-cal" aria-hidden="true"></i><span class="screen-reader-text
    ">Date</span> <?= $special['date_display']; ?></div>
                            <?php } ?>
                            <?php if(!empty($special['date_display'])) { ?>
                                <div class="col-auto"><i class="icon-clock" aria-hidden="true"></i><span class="screen-reader-text
    ">Time</span> <?= $special['time_display']; ?></div>
                            <?php } ?>
                        </div>
                        <?php
                    }
                    if(!empty($special['title'])) { ?>
                        <h2><?= nl2br(apply_filters('the_title', $special['title'])); ?></h2>
                    <?php }
                    if(!empty($special['content'])) {
                    ?>
                    <div class="content">
                        <?= apply_filters('the_content', $special['content']); ?>
                    </div>
                    <?php } ?>
                </div>
                </div>
                <?php
            }
            ?>
        </div>
        <?php
        $specials_output = ob_get_clean();
        if(!empty($specials_output)) {
            return '<div class="specials-output">'.$specials_output.'</div>';
        } else {
            $twt_options = get_option('twt-global-content');
            return '<p style="text-align:center">Sorry, there are no specials posted right now. Please check back later or <a href="'.($twt_options['fb'] ?? '').'" target="_blank">follow us on Facebook</a>.</p>';
        }
    }

    Can you check into it and fix it if applicable? Thank you!

  • You must be logged in to reply to this topic.