Skip to content

Commit

Permalink
Add default option for automatic rules enabled for backwards compat
Browse files Browse the repository at this point in the history
  • Loading branch information
nateweller committed Dec 23, 2022
1 parent db9681e commit d9ae22e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
55 changes: 55 additions & 0 deletions projects/packages/waf/src/class-compatibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Class used to manage backwards-compatibility of the package
*
* @package automattic/jetpack-waf
*/

namespace Automattic\Jetpack\Waf;

/**
* Defines methods for ensuring backwards compatibility.
*/
class Waf_Compatibility {

/**
* Add compatibilty hooks
*
* @since 0.8.0
*
* @return void
*/
public static function add_compatibility_hooks() {
add_filter( 'default_option_' . Waf_Runner::AUTOMATIC_RULES_ENABLED_OPTION_NAME, __CLASS__ . '::default_option_waf_automatic_rules', 10, 3 );
}

/**
* Provides a default value for sites that installed the WAF
* before the automatic rules option was introduced.
*
* @since 0.8.0
*
* @param mixed $default The default value to return if the option does not exist in the database.
* @param string $option Option name.
* @param bool $passed_default Was get_option() passed a default value.
*
* @return mixed The default value to return if the option does not exist in the database.
*/
public static function default_option_waf_automatic_rules( $default, $option, $passed_default ) {
// Allow get_option() to override this default value
if ( $passed_default ) {
return $default;
}

return self::get_default_automatic_rules_option();
}

/**
* If the option is not available, use the WAF module status
* to determine whether or not to run automatic rules
*/
public static function get_default_automatic_rules_option() {
return Waf_Runner::is_enabled();
}

}
3 changes: 3 additions & 0 deletions projects/packages/waf/src/class-waf-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public static function init() {
add_action( 'jetpack_activate_module_waf', __CLASS__ . '::on_activation' );
add_action( 'jetpack_deactivate_module_waf', __CLASS__ . '::on_deactivation' );

// Ensure backwards compatibility
Waf_Compatibility::add_compatibility_hooks();

// Run the WAF
Waf_Runner::initialize();
}
Expand Down

0 comments on commit d9ae22e

Please sign in to comment.