leepeterson
9/5/2017 - 2:15 AM

Discover where is array global being modified, because WordPress.

Discover where is array global being modified, because WordPress.

<?php
namespace Rarst;

/**
 * Discover where is array global being modified, because WordPress.
 */
class Global_Sniffer implements \ArrayAccess {

	protected $name;

	protected $data;

	/**
	 * @param string $name
	 * @param array  $data
	 */
	public function __construct( $name, array $data = [ ] ) {
		$this->name = $name;

		if ( isset( $GLOBALS[ $name ] ) && is_array( $GLOBALS[ $name ] ) ) {
			$data = $GLOBALS[ $name ];
		}

		$this->data       = $data;
		$GLOBALS[ $name ] = $this;
	}

	/**
	 * @inheritdoc
	 */
	public function offsetExists( $offset ) {
		return isset( $this->data[ $offset ] );
	}

	/**
	 * @inheritdoc
	 */
	public function offsetGet( $offset ) {
		return $this->data[ $offset ];
	}

	/**
	 * @inheritdoc
	 */
	public function offsetSet( $offset, $value ) {
		var_dump( $this->name, $offset, $value, wp_debug_backtrace_summary( null, 1, false ) );
		$this->data[ $offset ] = $value;
	}

	/**
	 * @inheritdoc
	 */
	public function offsetUnset( $offset ) {
		unset( $this->data[ $offset ] );
	}
}

// Example:
// new Global_Sniffer( '_wp_admin_css_colors' );