Making WordPress.org

Changeset 5850


Ignore:
Timestamp:
08/31/2017 12:34:12 PM (9 years ago)
Author:
SergeyBiryukov
Message:

Make/Meetings: Add "Occurrence in a month" option for recurrent meetings, allowing for a more flexible schedule like the first and third Thursday of every month.

The occurrence day is based on the day of week that the Start Date falls on.

Fixes #2218.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-meeting-posttype/wporg-meeting-posttype.php

    r5849 r5850  
    3030        }
    3131
    32         public function meeting_column_width() {
    33                 echo '<style type="text/css">.column-team { width:10em !important; overflow:hidden }</style>';
     32        public function meeting_column_width() { ?>
     33                <style type="text/css">
     34                        .column-team { width: 10em !important; overflow: hidden; }
     35                        #meeting-info .recurring label { padding-right: 10px; }
     36                </style>
     37                <?php
    3438        }
    3539
     
    6569                                        array(
    6670                                                'key'=>'recurring',
    67                                                 'value'=>array( 'weekly', 'biweekly', 'monthly', '1' ),
     71                                                'value'=>array( 'weekly', 'biweekly', 'occurrence', 'monthly', '1' ),
    6872                                                'compare'=>'NOT IN',
    6973                                        ),
     
    8084                                        array(
    8185                                                'key'=>'recurring',
    82                                                 'value'=>array( 'weekly', 'biweekly', 'monthly', '1' ),
     86                                                'value'=>array( 'weekly', 'biweekly', 'occurrence', 'monthly', '1' ),
    8387                                                'compare'=>'IN',
    8488                                        ),
     
    115119                // for each entry, set a fake meta value to show the next date for recurring meetings
    116120                array_walk( $posts, function ( &$post ) {
    117                         if ( $post->recurring == 'weekly' || $post->recurring === '1' ) {
     121                        if ( 'weekly' === $post->recurring || '1' === $post->recurring ) {
    118122                                try {
    119123                                        // from the start date, advance the week until it's past now
    120                                         $start = new DateTime( $post->start_date.' '.$post->time.' GMT' );
    121                                         $next = $start;
    122                                         $now = new DateTime();
    123                                         if ( $now > $next ) {
    124                                                 $interval = $start->diff($now);
     124                                        $start = new DateTime( $post->start_date . ' ' . $post->time . ' GMT' );
     125                                        $next  = $start;
     126                                        $now   = new DateTime();
     127
     128                                        if ( $next < $now ) {
     129                                                $interval = $start->diff( $now );
    125130                                                // add one to days to account for events that happened earlier today
    126                                                 $weekdiff = ceil( ($interval->days+1) / 7 );
    127                                                 $next->modify('+ '.$weekdiff.' weeks');
     131                                                $weekdiff = ceil( ( $interval->days + 1 ) / 7 );
     132                                                $next->modify( '+ ' . $weekdiff . ' weeks' );
    128133                                        }
     134
    129135                                        $post->next_date = $next->format( 'Y-m-d' );
    130                                 } catch (Exception $e) {
     136                                } catch ( Exception $e ) {
    131137                                        // if the datetime is invalid, then set the post->next_date to the start date instead
    132138                                        $post->next_date = $post->start_date;
    133139                                }
    134                         } else if ( $post->recurring == 'biweekly' ) {
     140                        } else if ( 'biweekly' === $post->recurring ) {
    135141                                try {
    136142                                        // advance the start date 2 weeks at a time until it's past now
    137                                         $start = new DateTime( $post->start_date.' '.$post->time.' GMT' );
    138                                         $next = $start;
    139                                         $now = new DateTime();
    140                                         while ( $now > $next ) {
    141                                                 $next->modify('+2 weeks');
     143                                        $start = new DateTime( $post->start_date . ' ' . $post->time . ' GMT' );
     144                                        $next  = $start;
     145                                        $now   = new DateTime();
     146
     147                                        while ( $next < $now ) {
     148                                                $next->modify( '+2 weeks' );
    142149                                        }
    143                                         $post->next_date = $next->format('Y-m-d');
    144                                 } catch (Exception $e) {
     150
     151                                        $post->next_date = $next->format( 'Y-m-d' );
     152                                } catch ( Exception $e ) {
    145153                                        // if the datetime is invalid, then set the post->next_date to the start date instead
    146154                                        $post->next_date = $post->start_date;
    147155                                }
    148                         } else if ( $post->recurring == 'monthly' ) {
     156                        } else if ( 'occurrence' === $post->recurring ) {
    149157                                try {
    150                                         // advance the start date 1 month at a time until it's past now
    151                                         $start = new DateTime( $post->start_date.' '.$post->time.' GMT' );
    152                                         $next = $start;
    153                                         $now = new DateTime();
    154                                         while ( $now > $next ) {
    155                                                 $next->modify('+1 month');
     158                                        // advance the start date in the current month until it's past now
     159                                        $start = new DateTime( $post->start_date . ' ' . $post->time . ' GMT' );
     160                                        $next  = $start;
     161                                        $now   = new DateTime();
     162
     163                                        $day_index = date( 'w', strtotime( $post->start_date . ' ' . $post->time . ' GMT' ) );
     164                                        $day_name  = $GLOBALS['wp_locale']->get_weekday( $day_index );
     165                                        $numerals  = array( 'first', 'second', 'third', 'fourth' );
     166                                        $months    = array( 'this month', 'next month' );
     167
     168                                        foreach ( $post->occurrence as $index ) {
     169                                                foreach ( $months as $month ) {
     170                                                        $next->modify( $numerals[ $index - 1 ] . ' ' . $day_name . ' of ' . $month );
     171                                                        if ( $next > $now ) {
     172                                                                break 2;
     173                                                        }
     174                                                }
    156175                                        }
    157                                         $post->next_date = $next->format('Y-m-d');
    158                                 } catch (Exception $e) {
     176
     177                                        $post->next_date = $next->format( 'Y-m-d' );
     178                                } catch ( Exception $e ) {
    159179                                        // if the datetime is invalid, then set the post->next_date to the start date instead
    160180                                        $post->next_date = $post->start_date;
    161181                                }
    162                         }
    163                         else {
     182                        } else if ( 'monthly' === $post->recurring ) {
     183                                try {
     184                                        // advance the start date 1 month at a time until it's past now
     185                                        $start = new DateTime( $post->start_date . ' ' . $post->time . ' GMT' );
     186                                        $next  = $start;
     187                                        $now   = new DateTime();
     188
     189                                        while ( $next < $now ) {
     190                                                $next->modify( '+1 month' );
     191                                        }
     192
     193                                        $post->next_date = $next->format( 'Y-m-d' );
     194                                } catch ( Exception $e ) {
     195                                        // if the datetime is invalid, then set the post->next_date to the start date instead
     196                                        $post->next_date = $post->start_date;
     197                                }
     198                        } else {
    164199                                $post->next_date = $post->start_date;
    165200                        }
     
    228263        public function add_meta_boxes() {
    229264                add_meta_box(
    230                         'meeting_info',
     265                        'meeting-info',
    231266                        'Meeting Info',
    232267                        array( $this, 'render_meta_boxes' ),
     
    241276                wp_enqueue_style( 'jquery-ui-style', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css', true);
    242277
    243                 $meta = get_post_custom( $post->ID );
    244                 $team = ! isset( $meta['team'][0] ) ? '' : $meta['team'][0];
    245                 $start = ! isset( $meta['start_date'][0] ) ? '' : $meta['start_date'][0];
    246                 $end = ! isset( $meta['end_date'][0] ) ? '' : $meta['end_date'][0];
    247                 $time = ! isset( $meta['time'][0] ) ? '' : $meta['time'][0];
    248                 $recurring = ! isset( $meta['recurring'][0] ) ? '' : $meta['recurring'][0];
    249                 if ( $recurring === '1' ) {
     278                $meta       = get_post_custom( $post->ID );
     279                $team       = isset( $meta['team'][0] ) ? $meta['team'][0] : '';
     280                $start      = isset( $meta['start_date'][0] ) ? $meta['start_date'][0] : '';
     281                $end        = isset( $meta['end_date'][0] ) ? $meta['end_date'][0] : '';
     282                $time       = isset( $meta['time'][0] ) ? $meta['time'][0] : '';
     283                $recurring  = isset( $meta['recurring'][0] ) ? $meta['recurring'][0] : '';
     284                if ( '1' === $recurring ) {
    250285                        $recurring = 'weekly';
    251286                }
    252                 $link = ! isset( $meta['link'][0] ) ? '' : $meta['link'][0];
    253                 $location = ! isset( $meta['location'][0] ) ? '' : $meta['location'][0];
     287                $occurrence = isset( $meta['occurrence'][0] ) ? unserialize( $meta['occurrence'][0] ) : array();
     288                $link       = isset( $meta['link'][0] ) ? $meta['link'][0] : '';
     289                $location   = isset( $meta['location'][0] ) ? $meta['location'][0] : '';
    254290                wp_nonce_field( 'save_meeting_meta_'.$post->ID , 'meeting_nonce' );
    255291                ?>
    256292
    257293                <p>
    258                 <label for="team"><?php _e( 'Team: ', 'wporg' ); ?>
    259                         <input type="text" id="team" name="team" class="regular-text wide" value="<?php echo esc_attr($team); ?>">
     294                <label for="team">
     295                        <?php _e( 'Team: ', 'wporg' ); ?>
     296                        <input type="text" id="team" name="team" class="regular-text wide" value="<?php echo esc_attr( $team ); ?>">
    260297                </label>
    261298                </p>
    262299                <p>
    263                 <label for="start_date"><?php _e( 'Start Date', 'wporg' ); ?>
    264                         <input type="text" name="start_date" class="date" value="<?php echo esc_attr($start); ?>">
    265                 </label>
    266                 <label for="end_date"><?php _e( 'End Date', 'wporg' ); ?>
    267                         <input type="text" name="end_date" class="date" value="<?php echo esc_attr($end); ?>">
     300                <label for="start_date">
     301                        <?php _e( 'Start Date', 'wporg' ); ?>
     302                        <input type="text" name="start_date" id="start_date" class="date" value="<?php echo esc_attr( $start ); ?>">
     303                </label>
     304                <label for="end_date">
     305                        <?php _e( 'End Date', 'wporg' ); ?>
     306                        <input type="text" name="end_date" id="end_date" class="date" value="<?php echo esc_attr( $end ); ?>">
    268307                </label>
    269308                </p>
    270309                <p>
    271                 <label for="time"><?php _e( 'Time (UTC)', 'wporg' ); ?>
    272                         <input type="text" name="time" class="time" value="<?php echo esc_attr($time); ?>">
    273                 </label>
    274                 <label for="recurring"><?php _e( 'Recurring: ', 'wporg' ); ?>
     310                <label for="time">
     311                        <?php _e( 'Time (UTC)', 'wporg' ); ?>
     312                        <input type="text" name="time" id="time" class="time" value="<?php echo esc_attr( $time ); ?>">
     313                </label>
     314                </p>
     315                <p class="recurring">
     316                <?php _e( 'Recurring: ', 'wporg' ); ?><br />
     317                <label for="weekly">
    275318                        <input type="radio" name="recurring" value="weekly" id="weekly" class="regular-radio" <?php checked( $recurring, 'weekly' ); ?>>
    276                         <label for="weekly"><?php _e( 'Weekly', 'wporg' ); ?></label>
     319                        <?php _e( 'Weekly', 'wporg' ); ?>
     320                </label><br />
     321
     322                <label for="biweekly">
    277323                        <input type="radio" name="recurring" value="biweekly" id="biweekly" class="regular-radio" <?php checked( $recurring, 'biweekly' ); ?>>
    278                         <label for="biweekly"><?php _e( 'Biweekly', 'wporg' ); ?></label>
     324                        <?php _e( 'Biweekly', 'wporg' ); ?>
     325                </label><br />
     326
     327                <label for="occurrence">
     328                        <input type="radio" name="recurring" value="occurrence" id="occurrence" class="regular-radio" <?php checked( $recurring, 'occurrence' ); ?>>
     329                        <?php _e( 'Occurrence in a month:', 'wporg' ); ?>
     330                </label>
     331                <label for="week-1">
     332                        <input type="checkbox" name="occurrence[]" value="1" id="week-1" <?php checked( in_array( 1, $occurrence ) ); ?>>
     333                        <?php _e( '1st', 'wporg' ); ?>
     334                </label>
     335                <label for="week-2">
     336                        <input type="checkbox" name="occurrence[]" value="2" id="week-2" <?php checked( in_array( 2, $occurrence ) ); ?>>
     337                        <?php _e( '2nd', 'wporg' ); ?>
     338                </label>
     339                <label for="week-3">
     340                        <input type="checkbox" name="occurrence[]" value="3" id="week-3" <?php checked( in_array( 3, $occurrence ) ); ?>>
     341                        <?php _e( '3rd', 'wporg' ); ?>
     342                </label>
     343                <label for="week-4">
     344                        <input type="checkbox" name="occurrence[]" value="4" id="week-4" <?php checked( in_array( 4, $occurrence ) ); ?>>
     345                        <?php _e( '4th', 'wporg' ); ?>
     346                </label><br />
     347
     348                <label for="monthly">
    279349                        <input type="radio" name="recurring" value="monthly" id="monthly" class="regular-radio" <?php checked( $recurring, 'monthly' ); ?>>
    280                         <label for="monthly"><?php _e( 'Monthly', 'wporg' ); ?></label>
     350                        <?php _e( 'Monthly', 'wporg' ); ?>
    281351                </label>
    282352                </p>
    283353                <p>
    284354                <label for="link"><?php _e( 'Link: ', 'wporg' ); ?>
    285                         <input type="text" name="link" class="regular-text wide" value="<?php echo esc_url($link); ?>">
     355                        <input type="text" name="link" id="link" class="regular-text wide" value="<?php echo esc_url( $link ); ?>">
    286356                </label>
    287357                </p>
    288358                <p>
    289359                <label for="location"><?php _e( 'Location: ', 'wporg' ); ?>
    290                         <input type="text" id="location" name="location" class="regular-text wide" value="<?php echo esc_attr($location); ?>">
     360                        <input type="text" name="location" id="location" class="regular-text wide" value="<?php echo esc_attr( $location ); ?>">
    291361                </label>
    292362                </p>
    293363                <script>
    294                 jQuery(document).ready(function($){
     364                jQuery(document).ready( function($) {
    295365                        $('.date').datepicker({
    296                                 dateFormat: "yy-mm-dd"
     366                                dateFormat: 'yy-mm-dd'
    297367                        });
    298                         $(document).on( 'keydown', '#title, #location', function( e ) {
    299                                 var keyCode = e.keyCode || e.which;
    300                                 if ( 9 == keyCode){
    301                                         e.preventDefault();
    302                                         var target = $(this).attr('id') == 'title' ? '#team' : 'textarea#content';
    303                                         if ( (target === '#team') || $('#wp-content-wrap').hasClass('html-active') ) {
    304                                                 $(target).focus();
    305                                         } else {
    306                                                 tinymce.execCommand('mceFocus',false,'content');
    307                                         }
    308                                 }
     368
     369                        $('input[name="recurring"]').change( function() {
     370                                var disabled = ( 'occurrence' !== $(this).val() );
     371                                $('#meeting-info').find('[name^="occurrence"]').prop('disabled', disabled);
    309372                        });
     373
     374                        if ( 'occurrence' !== $('input[name="recurring"]:checked').val() ) {
     375                                $('#meeting-info').find('[name^="occurrence"]').prop('disabled', true);
     376                        }
    310377                });
    311378                </script>
     
    328395
    329396                // Don't save for revisions
    330                 if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
     397                if ( isset( $post->post_type ) && 'revision' === $post->post_type ) {
    331398                        return $post_id;
    332399                }
     
    337404                }
    338405
    339                 $team = ! isset( $meta['team'][0] ) ? '' : $meta['team'][0];
    340                 $start = ! isset( $meta['start_date'][0] ) ? '' : $meta['start_date'][0];
    341                 $end = ! isset( $meta['end_date'][0] ) ? '' : $meta['end_date'][0];
    342                 $recurring = ! isset( $meta['recurring'][0] ) ? '' : $meta['recurring'][0];
    343                 $link = ! isset( $meta['link'][0] ) ? '' : $meta['link'][0];
    344                 $location = ! isset( $meta['location'][0] ) ? '' : $meta['location'][0];
    345 
    346                 $meta['team'] = ( isset( $_POST['team'] ) ? esc_textarea( $_POST['team'] ) : '' );
    347                 $meta['start_date'] = ( isset( $_POST['start_date'] ) ? esc_textarea( $_POST['start_date'] ) : '' );
    348                 $meta['end_date'] = ( isset( $_POST['end_date'] ) ? esc_textarea( $_POST['end_date'] ) : '' );
    349                 $meta['time'] = ( isset( $_POST['time'] ) ? esc_textarea( $_POST['time'] ) : '' );
    350                 $meta['recurring'] = ( isset ( $_POST['recurring'] ) && ( in_array( $_POST['recurring'], array( 'weekly', 'biweekly', 'monthly' ) ) ) ? ( $_POST['recurring'] ) : '' );
    351                 $meta['link'] = ( isset( $_POST['link'] ) ? esc_url( $_POST['link'] ) : '' );
    352                 $meta['location'] = ( isset( $_POST['location'] ) ? esc_textarea( $_POST['location'] ) : '' );
     406                $meta['team']        = ( isset( $_POST['team'] ) ? esc_textarea( $_POST['team'] ) : '' );
     407                $meta['start_date']  = ( isset( $_POST['start_date'] ) ? esc_textarea( $_POST['start_date'] ) : '' );
     408                $meta['end_date']    = ( isset( $_POST['end_date'] ) ? esc_textarea( $_POST['end_date'] ) : '' );
     409                $meta['time']        = ( isset( $_POST['time'] ) ? esc_textarea( $_POST['time'] ) : '' );
     410                $meta['recurring']   = ( isset( $_POST['recurring'] )
     411                                         && in_array( $_POST['recurring'], array( 'weekly', 'biweekly', 'occurrence', 'monthly' ) )
     412                                         ? ( $_POST['recurring'] ) : '' );
     413                $meta['occurrence']  = ( isset( $_POST['occurrence'] ) && 'occurrence' === $meta['recurring']
     414                                         && is_array( $_POST['occurrence'] )
     415                                         ? array_map( 'intval', $_POST['occurrence'] ) : array() );
     416                $meta['link']        = ( isset( $_POST['link'] ) ? esc_url( $_POST['link'] ) : '' );
     417                $meta['location']    = ( isset( $_POST['location'] ) ? esc_textarea( $_POST['location'] ) : '' );
    353418
    354419                foreach ( $meta as $key => $value ) {
Note: See TracChangeset for help on using the changeset viewer.