Skip to:
Content

bbPress.org

Changeset 6869


Ignore:
Timestamp:
10/12/2018 05:07:51 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Edit Locking Improvements:

  • Refactor to avoid doing unnecessary computations
  • Invert default return value from false to true, requiring time to pass validation as opposed to assuming
  • Improve obviousness of math computations for easier debuggability
  • Update variables passed into the end return filter
  • Add 6 unit tests for before/on/after, plus support for "0" as infinite
  • Fix bug causing "0" values to return the opposite value
  • Ensure only gmt/utc values are compared
  • Add optional flag to use WordPress time instead
  • Improve inline and function documentation

Fixes #3222. Props wpdennis.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/settings.php

    r6834 r6869  
    661661        </label>
    662662        <label for="_bbp_edit_lock">
    663                 <input name="_bbp_edit_lock" id="_bbp_edit_lock" type="number" min="0" step="1" value="<?php bbp_form_option( '_bbp_edit_lock', '0' ); ?>" class="small-text"<?php bbp_maybe_admin_setting_disabled( '_bbp_edit_lock' ); ?> />
     663                <input name="_bbp_edit_lock" id="_bbp_edit_lock" type="number" min="0" step="1" value="<?php bbp_form_option( '_bbp_edit_lock', '5' ); ?>" class="small-text"<?php bbp_maybe_admin_setting_disabled( '_bbp_edit_lock' ); ?> />
    664664
    665665        <?php $select = ob_get_clean(); ?>
     
    19451945         * @param string $option
    19461946         * @param string $default
    1947          * @param bool $slug
     1947         * @param bool   $is_slug
     1948         *
     1949         * @return mixed
    19481950         */
    1949         function bbp_get_form_option( $option, $default = '', $slug = false ) {
     1951        function bbp_get_form_option( $option, $default = '', $is_slug = false ) {
    19501952
    19511953                // Get the option and sanitize it
     
    19531955
    19541956                // Slug?
    1955                 if ( true === $slug ) {
     1957                if ( true === $is_slug ) {
    19561958                        $value = esc_attr( apply_filters( 'editable_slug', $value ) );
    19571959
     
    19671969
    19681970                // Filter & return
    1969                 return apply_filters( 'bbp_get_form_option', $value, $option );
     1971                return apply_filters( 'bbp_get_form_option', $value, $option, $default, $is_slug );
    19701972        }
    19711973
  • trunk/src/includes/common/functions.php

    r6864 r6869  
    191191 * Check a date against the length of time something can be edited.
    192192 *
     193 * It is recommended to leave $utc set to true and to work with UTC/GMT dates.
     194 * Turning this off will use the WordPress offset which is likely undesirable.
     195 *
    193196 * @since 2.0.0 bbPress (r3133)
    194  *
    195  * @param string $post_date_gmt
    196  *
    197  * @return bool True if date is past, False if not
    198  */
    199 function bbp_past_edit_lock( $post_date_gmt = '' ) {
     197 * @since 2.6.0 bbPress (r6868) Inverted some logic and added unit tests
     198 *
     199 * @param string  $datetime Gets run through strtotime()
     200 * @param boolean $utc      Default true. Is the timestamp in UTC?
     201 *
     202 * @return bool True by default, if date is past, or editing is disabled.
     203 */
     204function bbp_past_edit_lock( $datetime = '', $utc = true ) {
    200205
    201206        // Default value
    202         $retval = false;
    203 
    204         // Get number of minutes to allow editing for
    205         $minutes = (int) get_option( '_bbp_edit_lock', 5 );
    206 
    207         // Now
    208         $cur_time = current_time( 'timestamp', true );
    209 
    210         // Period of time
    211         $lockable  = "+{$minutes} minutes";
    212 
    213         // Add lockable time to post time
    214         $lock_time = strtotime( $lockable, strtotime( $post_date_gmt ) );
     207        $retval = true;
    215208
    216209        // Check if date and editing is allowed
    217         if ( ! empty( $post_date_gmt ) && bbp_allow_content_edit() ) {
    218 
    219                 // "0" minutes set, so allow forever
     210        if ( bbp_allow_content_edit() ) {
     211
     212                // Get number of minutes to allow editing for
     213                $minutes = bbp_get_edit_lock();
     214
     215                // 0 minutes means forever, so can never be past edit-lock time
    220216                if ( 0 === $minutes ) {
    221                         $retval = true;
    222 
    223                 // Not "0" so compare
    224                 } elseif ( $cur_time >= $lock_time ) {
    225                         $retval = true;
     217                        $retval = false;
     218
     219                // Checking against a specific datetime
     220                } elseif ( ! empty( $datetime ) ) {
     221
     222                        // Period of time
     223                        $lockable = "+{$minutes} minutes";
     224                        if ( true === $utc ) {
     225                                $lockable .= " UTC";
     226                        }
     227
     228                        // Now
     229                        $cur_time  = current_time( 'timestamp', $utc );
     230
     231                        // Get the duration in seconds
     232                        $duration  = strtotime( $lockable ) - $cur_time;
     233
     234                        // Diff the times down to seconds
     235                        $lock_time = strtotime( $lockable, $cur_time );
     236                        $past_time = strtotime( $datetime, $cur_time );
     237                        $diff_time = ( $lock_time - $past_time ) - $duration;
     238
     239                        // 0 minutes set, so allow editing forever
     240                        if ( 0 === $minutes ) {
     241                                $retval = false;
     242
     243                        // Check if less than lock time
     244                        } elseif ( $diff_time < $duration ) {
     245                                $retval = false;
     246                        }
    226247                }
    227248        }
    228249
    229250        // Filter & return
    230         return (bool) apply_filters( 'bbp_past_edit_lock', $retval, $cur_time, $lock_time, $post_date_gmt );
     251        return (bool) apply_filters( 'bbp_past_edit_lock', $retval, $datetime, $utc );
    231252}
    232253
  • trunk/src/includes/core/options.php

    r6823 r6869  
    602602
    603603/**
     604 * Output the number of minutes a topic or reply can be edited after it's
     605 * published. Used by `bbp_past_edit_lock()`.
     606 *
     607 * @since 2.6.0 bbPress (r3246)
     608 *
     609 * @param bool $default Optional. Default value 5
     610 */
     611function bbp_edit_lock( $default = 5 ) {
     612        echo bbp_get_edit_lock( $default );
     613}
     614        /**
     615         * Return the maximum length of a title
     616         *
     617         * @since 2.0.0 bbPress (r3246)
     618         *
     619         * @param bool $default Optional. Default value 5
     620         * @return int Is anonymous posting allowed?
     621         */
     622        function bbp_get_edit_lock( $default = 5 ) {
     623
     624                // Filter & return
     625                return (int) apply_filters( 'bbp_get_edit_lock', (int) get_option( '_bbp_edit_lock', $default ) );
     626        }
     627
     628/**
    604629 * Output the group forums root parent forum id
    605630 *
  • trunk/tests/phpunit/testcases/common/functions.php

    r6859 r6869  
    634634
    635635        /**
     636         * @group  locking
    636637         * @covers ::bbp_past_edit_lock
    637          * @todo   Implement test_bbp_past_edit_lock().
    638          */
    639         public function test_bbp_past_edit_lock() {
    640                 // Remove the following lines when you implement this test.
    641                 $this->markTestIncomplete(
    642                         'This test has not been implemented yet.'
    643                 );
     638         */
     639        public function test_bbp_past_edit_lock_before_5_minutes() {
     640                update_option( '_bbp_edit_lock', 5 );
     641                update_option( '_bbp_allow_content_edit', true );
     642
     643                // Before
     644                $result = bbp_past_edit_lock( '4 minutes 59 seconds ago UTC' );
     645                $this->assertFalse( $result );
     646        }
     647
     648        /**
     649         * @group  locking
     650         * @covers ::bbp_past_edit_lock
     651         */
     652        public function test_bbp_past_edit_lock_on_5_minutes() {
     653                update_option( '_bbp_edit_lock', 5 );
     654                update_option( '_bbp_allow_content_edit', true );
     655
     656                // On
     657                $result = bbp_past_edit_lock( '5 minutes ago UTC' );
     658                $this->assertTrue( $result );
     659        }
     660
     661        /**
     662         * @group  locking
     663         * @covers ::bbp_past_edit_lock
     664         */
     665        public function test_bbp_past_edit_lock_after_5_minutes() {
     666                update_option( '_bbp_edit_lock', 5 );
     667                update_option( '_bbp_allow_content_edit', true );
     668
     669                // After
     670                $result = bbp_past_edit_lock( '5 minutes 1 second ago UTC' );
     671                $this->assertTrue( $result );
     672        }
     673
     674        /**
     675         * @group  locking
     676         * @covers ::bbp_past_edit_lock
     677         */
     678        public function test_bbp_past_edit_lock_before_0_minutes() {
     679                update_option( '_bbp_edit_lock', 0 );
     680                update_option( '_bbp_allow_content_edit', true );
     681
     682                // Before
     683                $result = bbp_past_edit_lock( '4 minutes 59 seconds ago UTC' );
     684                $this->assertFalse( $result );
     685        }
     686
     687        /**
     688         * @group  locking
     689         * @covers ::bbp_past_edit_lock
     690         */
     691        public function test_bbp_past_edit_lock_on_0_minutes() {
     692                update_option( '_bbp_edit_lock', 0 );
     693                update_option( '_bbp_allow_content_edit', true );
     694
     695                // On
     696                $result = bbp_past_edit_lock( '5 minutes ago UTC' );
     697                $this->assertFalse( $result );
     698        }
     699
     700        /**
     701         * @group  locking
     702         * @covers ::bbp_past_edit_lock
     703         */
     704        public function test_bbp_past_edit_lock_after_0_minutes() {
     705                update_option( '_bbp_edit_lock', 0 );
     706                update_option( '_bbp_allow_content_edit', true );
     707
     708                // After
     709                $result = bbp_past_edit_lock( '5 minutes 1 second ago UTC' );
     710                $this->assertFalse( $result );
    644711        }
    645712
Note: See TracChangeset for help on using the changeset viewer.