Make WordPress Core

Ticket #44042: 44042.2.diff

File 44042.2.diff, 1.9 KB (added by donmhico, 6 years ago)

Changed the new filter name to pre_delete_option and added unit tests.

  • src/wp-includes/option.php

    diff --git src/wp-includes/option.php src/wp-includes/option.php
    index dcd17956b9..c03232584d 100644
    function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) 
    549549function delete_option( $option ) {
    550550        global $wpdb;
    551551
    552         $option = trim( $option );
     552        /**
     553         * Filters the option name before the option gets deleted.
     554         *
     555         * Returning an empty string short circuits the function.
     556         *
     557         * @since 5.3.0
     558         *
     559         * @param string $option Option name.
     560         */
     561        $option = trim( apply_filters( 'pre_delete_option', $option ) );
     562
    553563        if ( empty( $option ) ) {
    554564                return false;
    555565        }
  • tests/phpunit/tests/option/option.php

    diff --git tests/phpunit/tests/option/option.php tests/phpunit/tests/option/option.php
    index f61f59b88b..65f5d3b6f3 100644
    class Tests_Option_Option extends WP_UnitTestCase { 
    140140                $actual = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", $name ) );
    141141                $this->assertEquals( $expected, $actual->autoload );
    142142        }
     143
     144        /**
     145         * @ticket 44042
     146         */
     147        function test_pre_delete_option_filter() {
     148                add_filter( 'pre_delete_option', function( $option ) {
     149                        if ( 'test_option_test' === $option ) {
     150                                $option = 'test_option';
     151                        }
     152                       
     153                        return $option;
     154                } );
     155
     156                add_option( 'test_option', '1' );
     157                delete_option( 'test_option_test' );
     158
     159                $this->assertEquals( false, get_option( 'test_option' ) );
     160
     161        }
     162
     163        /**
     164         * Returning an empty string in `pre_delete_option` short circuits `delete_option()`.
     165         *
     166         * @ticket 44042
     167         */
     168        function test_pre_delete_option_short_circuit() {
     169                add_filter( 'pre_delete_option', function( $option ) {
     170                        if ( 'test_option' === $option ) {
     171                                return '';
     172                        }
     173                } );
     174
     175                add_option( 'test_option', '5' );
     176                delete_option( 'test_option' );
     177
     178                $this->assertEquals( '5', get_option( 'test_option' ) );
     179        }
    143180}