-
-
Save costdev/fdb4f8669fe204cce6ce9ffc28b4fd0b to your computer and use it in GitHub Desktop.
Test #21989
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Test #21989 | |
* Description: Tests <a href="https://core.trac.w.org/ticket/21989">#21989</a>. | |
* Author: WordPress Core Contributors | |
* Author URI: https://make.wordpress.org/core | |
* License: GPLv2 or later | |
* Version: 1.0.0 | |
*/ | |
class Test_21989 { | |
const OPTION = 'test_21989'; | |
const BASE_VALUE = 'a_string'; | |
const APPEND = '_1'; | |
private $expected = null; | |
private $notice = ''; | |
public function __construct() { | |
$this->expected = self::BASE_VALUE . self::APPEND; | |
// Comment this line to test `update_option()` only (option must exist). | |
delete_option( self::OPTION ); | |
add_action( 'admin_init', array( $this, 'init' ) ); | |
} | |
public function init() { | |
add_filter( 'sanitize_option_' . self::OPTION, array( $this, 'sanitize_option' ) ); | |
add_filter( 'pre_update_option_' . self::OPTION, array( $this, 'pre_update_option_option' ) ); | |
add_filter( 'pre_update_option', array( $this, 'pre_update_option' ), 10, 2 ); | |
add_action( 'add_option_' . self::OPTION, array( $this, 'add_option' ), 999, 2 ); | |
add_action( 'update_option', array( $this, 'update_option' ), 999, 3 ); | |
update_option( self::OPTION, 'a_string' ); | |
add_action( 'admin_notices', array( $this, 'admin_notices' ) ); | |
} | |
public function sanitize_option( $value ) { | |
$new_value = $value . self::APPEND; | |
$this->notice .= '<p>Before <code>sanitize_option</code>: ' . $value . '</p>'; | |
$this->notice .= '<p>After <code>sanitize_option</code>: ' . $new_value . '</p>'; | |
return $new_value; | |
} | |
public function pre_update_option_option( $value ) { | |
$is_expected = $this->expected === $value ? 'Yes' : 'No'; | |
$this->notice .= '<p><code>pre_update_option_test_{$option}</code> Still "' . $this->expected . '"? ' . $is_expected . '</p>'; | |
return $value; | |
} | |
public function pre_update_option( $value, $option ) { | |
if ( self::OPTION !== $option ) { | |
return $value; | |
} | |
$is_expected = $this->expected === $value ? 'Yes' : 'No'; | |
$this->notice .= '<p><code>pre_update_option</code> Still "' . $this->expected . '"? ' . $is_expected . '</p>'; | |
return $value; | |
} | |
public function add_option( $option, $value ) { | |
if ( self::OPTION !== $option ) { | |
return; | |
} | |
$this->notice .= '<p><code>add_option()</code>: ' . $value . '</p>'; | |
} | |
public function update_option( $option, $old_value, $value ) { | |
if ( self::OPTION !== $option ) { | |
return; | |
} | |
$this->notice .= '<p><code>update_option()</code>: ' . $value . '</p>'; | |
} | |
public function admin_notices() { | |
echo '<div class="notice notice-info"><p>' . $this->notice . '</p></div>'; | |
} | |
} | |
new Test_21989(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment