Skip to content

Commit

Permalink
Options instance normalize.
Browse files Browse the repository at this point in the history
  • Loading branch information
sybrew committed Aug 20, 2016
1 parent 7a9ff5e commit 2c561ce
Show file tree
Hide file tree
Showing 7 changed files with 724 additions and 578 deletions.
1 change: 0 additions & 1 deletion inc/classes/accountactivation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ protected function do_free_activation() {
$success = $this->update_option_multi( $options );

if ( $success ) {
$this->set_options_instance( $options );
$this->set_error_notice( array( 601 => '' ) );
return true;
} else {
Expand Down
77 changes: 52 additions & 25 deletions inc/classes/core.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function handle_update_post() {
break;

case $this->request_name['enable-feed'] :
$success = $this->update_option( '_enable_feed', true );
$success = $this->update_option( '_enable_feed', true, 'regular', false );
$code = $success ? 702 : 703;
$this->set_error_notice( array( $code => '' ) );
break;
Expand Down Expand Up @@ -751,8 +751,14 @@ protected function update_option( $option, $value, $type = 'instance', $kill = t

$this->initialize_option_update_instance( $type );

if ( empty( $options['_instance'] ) && '_instance' !== $option )
wp_die( 'Supply instance key before updating other options.' );

$success = update_option( TSF_EXTENSION_MANAGER_SITE_OPTIONS, $options );

$key = '_instance' === $option ? $value : $options['_instance'];
$this->set_options_instance( $options, $key );

if ( false === $this->verify_option_update_instance( $kill ) )
return false;

Expand Down Expand Up @@ -799,6 +805,11 @@ protected function update_option_multi( array $options = array(), $type = 'insta

$this->initialize_option_update_instance( $type );

if ( empty( $options['_instance'] ) )
wp_die( 'Supply instance key before updating other options.' );

$this->set_options_instance( $options, $options['_instance'] );

$success = update_option( TSF_EXTENSION_MANAGER_SITE_OPTIONS, $options );

if ( false === $this->verify_option_update_instance( $kill ) )
Expand All @@ -815,28 +826,59 @@ protected function update_option_multi( array $options = array(), $type = 'insta
* @return string The hashed option.
*/
protected function get_options_instance() {
return $this->get_option( $this->get_option( '_instance' ) );
return get_option( 'tsfem_i_' . $this->get_option( '_instance' ) );
}

/**
* Updates verification instance option.
*
* @since 1.0.0
*
* @param string $value The option value.
* @param string $key Optional. The options key. Must be supplied when activating account.
* @return bool True on success, false on failure.
*/
protected function update_options_instance( $value, $key = '' ) {

$key = $key ? $key : $this->get_option( '_instance' );

return update_option( 'tsfem_i_' . $key, $value );
}

/**
* Deletes option instance on account deactivation.
*
* @since 1.0.0
*
* @return bool
*/
protected function delete_options_instance() {

delete_option( 'tsfem_i_' . $this->get_option( '_instance' ) );

return true;
}

/**
* Binds options to an unique hash and saves it in a comparison option.
* This prevents users from altering the options from outside this plugin.
*
* @since 1.0.0
* @param array $options The options to hash.
*
* @param array $options The options to hash.
* @param string $key The instance key, needs to be supplied on plugin activation.
* @return bool True on success, false on failure.
*/
protected function set_options_instance( $options ) {
protected function set_options_instance( $options, $key = '' ) {

if ( empty( $options['instance'] ) )
if ( empty( $options['_instance'] ) )
return false;

$_options = serialize( $options );
$hash = $this->make_hash( $_options, 'auth' );
$hash = $this->make_hash( $_options );

if ( $hash ) {
$update = update_option( $options['instance'], $hash );
$update = $this->update_options_instance( $hash, $key );

if ( false === $update ) {
$this->set_error_notice( array( 7001 => '' ) );
Expand All @@ -849,21 +891,6 @@ protected function set_options_instance( $options ) {
}
}

/**
* Deletes option instance on account deactivation.
*
* @since 1.0.0
*
* @return bool True on success, false on failure.
*/
protected function delete_options_instance() {

$instance = $this->get_option( '_instance' );
delete_option( $instance );

return true;
}

/**
* Returns hash key based on sha256 if available. Otherwise it will fall back
* to md5 (wp_hash()). Hash will alter every 24 hours.
Expand Down Expand Up @@ -894,7 +921,7 @@ protected function make_hash( $data ) {
* @param string $data The data to compare hash with.
* @return bool True when hash passes, false on failure.
*/
public function verify_options( $data ) {
public function verify_options_hash( $data ) {
return hash_equals( $this->make_hash( $data ), $this->get_options_instance() );
}

Expand All @@ -905,7 +932,7 @@ public function verify_options( $data ) {
*
* @param string $type What type of update this is, accepts 'instance' and 'regular'.
*/
protected function initialize_option_update_instance( $type = '' ) {
protected function initialize_option_update_instance( $type = 'regular' ) {

if ( 'instance' === $type ) {
$type = 'update_option_instance';
Expand Down Expand Up @@ -937,7 +964,7 @@ protected function verify_option_update_instance( $kill = true ) {

$verify = SecureOption::verified_option_update();

if ( false === $verify && $kill )
if ( $kill && false === $verify )
$this->kill_options();

SecureOption::reset();
Expand Down
4 changes: 1 addition & 3 deletions inc/classes/panes.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ protected function get_seo_trends_and_updates_overview() {
$output = '';

$feed_enabled = $this->get_option( '_enable_feed', false );
//$feed_enabled = false;
//$this->update_option( '_enable_feed', false );

if ( $feed_enabled ) {
$output = $this->get_trends_output();
Expand Down Expand Up @@ -206,7 +204,7 @@ public function wp_ajax_enable_feeds() {
'type' => 'unknown',
);
} else {
$type = $this->update_option( '_enable_feed', true ) ? 'success' : 'error';
$type = $this->update_option( '_enable_feed', true, 'regular', false ) ? 'success' : 'error';

if ( 'success' === $type ) {
$results = array(
Expand Down
8 changes: 4 additions & 4 deletions inc/classes/secureoption.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static function get( $type = '' ) {
public static function set_update_instance( $instance, $bits ) {

switch ( self::get_property( '_type' ) ) :
case 'update_options' :
case 'update_option' :
case 'update_option_instance' :
static::$_instance = array( $instance, $bits );
return true;
Expand Down Expand Up @@ -132,7 +132,7 @@ public static function verify_option_instance( $value, $old_value, $option ) {

$type = self::get_property( '_type' );

if ( empty( self::$_instance ) ) {
if ( empty( self::$_instance ) || empty( $type ) ) {
self::reset();
wp_die( '<code>' . esc_html( $option ) . '</code> is a protected option.' );
}
Expand All @@ -153,9 +153,9 @@ public static function verify_option_instance( $value, $old_value, $option ) {
//* Always update instance before updating options when deactivating.
if ( 'update_option_instance' === $type ) {
$verified = true;
} else {
} elseif ( 'update_option' === $type ) {
$options = get_option( TSF_EXTENSION_MANAGER_SITE_OPTIONS );
if ( tsf_extension_manager()->verify_options( serialize( $options ) ) ) {
if ( tsf_extension_manager()->verify_options_hash( serialize( $options ) ) ) {
$verified = true;
} else {
self::reset();
Expand Down
2 changes: 1 addition & 1 deletion inc/classes/trends.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static function prototype_trends() {
if ( ! function_exists( 'simplexml_load_string' ) )
return -1;

$transient_name = 'latest-seo-feed-transient';
$transient_name = 'tsfem_latest_seo_feed';
$output = get_transient( $transient_name );

if ( false === $output ) {
Expand Down
Loading

0 comments on commit 2c561ce

Please sign in to comment.