migrate settings to WP settings API
This commit is contained in:
@ -16,17 +16,15 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
|
||||
/**
|
||||
* Statify Filter admin configuration.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
|
||||
/**
|
||||
* Initialize admin-only components of the plugin.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public static function init() {
|
||||
// Add actions.
|
||||
@ -46,91 +44,12 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
2
|
||||
);
|
||||
} else {
|
||||
add_action( 'admin_init', array( 'StatifyBlacklist_Settings', 'register_settings' ) );
|
||||
add_action( 'admin_menu', array( 'StatifyBlacklist_Admin', 'add_menu_page' ) );
|
||||
add_filter( 'plugin_action_links', array( 'StatifyBlacklist_Admin', 'plugin_actions_links' ), 10, 2 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update options.
|
||||
*
|
||||
* @since 1.1.1
|
||||
*
|
||||
* @param array $options Optional. New options to save.
|
||||
*
|
||||
* @return array|bool array of sanitized array on errors, FALSE if there were none.
|
||||
*/
|
||||
public static function update_options( $options = null ) {
|
||||
if ( isset( $options ) && current_user_can( 'manage_options' ) ) {
|
||||
|
||||
// Sanitize referer list.
|
||||
$given_referer = $options['referer']['blacklist'];
|
||||
$invalid_referer = array();
|
||||
if ( self::MODE_NORMAL === $options['referer']['regexp'] ) {
|
||||
// Sanitize URLs and remove empty inputs.
|
||||
$sanitized_referer = self::sanitize_urls( $given_referer );
|
||||
} elseif ( self::MODE_REGEX === $options['referer']['regexp'] || self::MODE_REGEX_CI === $options['referer']['regexp'] ) {
|
||||
$sanitized_referer = $given_referer;
|
||||
// Check regular expressions.
|
||||
$invalid_referer = self::sanitize_regex( $given_referer );
|
||||
} else {
|
||||
$sanitized_referer = $given_referer;
|
||||
}
|
||||
|
||||
// Sanitize target list.
|
||||
$given_target = $options['target']['blacklist'];
|
||||
$invalid_target = array();
|
||||
if ( self::MODE_REGEX === $options['target']['regexp'] || self::MODE_REGEX_CI === $options['target']['regexp'] ) {
|
||||
$sanitized_target = $given_target;
|
||||
// Check regular expressions.
|
||||
$invalid_target = self::sanitize_regex( $given_target );
|
||||
} else {
|
||||
$sanitized_target = $given_target;
|
||||
}
|
||||
|
||||
// Sanitize IPs and subnets and remove empty inputs.
|
||||
$given_ip = $options['ip']['blacklist'];
|
||||
$sanitized_ip = self::sanitize_ips( $given_ip );
|
||||
|
||||
// Abort on errors.
|
||||
$errors = array(
|
||||
'referer' => array(
|
||||
'sanitized' => $sanitized_referer,
|
||||
'diff' => array_diff( $given_referer, $sanitized_referer ),
|
||||
'invalid' => $invalid_referer,
|
||||
),
|
||||
'target' => array(
|
||||
'sanitized' => $sanitized_target,
|
||||
'diff' => array_diff( $given_target, $sanitized_target ),
|
||||
'invalid' => $invalid_target,
|
||||
),
|
||||
'ip' => array(
|
||||
'sanitized' => $sanitized_ip,
|
||||
'diff' => array_diff( $given_ip, $sanitized_ip ),
|
||||
),
|
||||
);
|
||||
if ( ! empty( $errors['referer']['diff'] )
|
||||
|| ! empty( $errors['referer']['invalid'] )
|
||||
|| ! empty( $errors['target']['diff'] )
|
||||
|| ! empty( $errors['target']['invalid'] )
|
||||
|| ! empty( $errors['ip']['diff'] ) ) {
|
||||
return $errors;
|
||||
}
|
||||
|
||||
// Update database on success.
|
||||
if ( self::$multisite ) {
|
||||
update_site_option( 'statify-blacklist', $options );
|
||||
} else {
|
||||
update_option( 'statify-blacklist', $options );
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh options.
|
||||
parent::update_options( $options );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add configuration page to admin menu.
|
||||
*
|
||||
@ -139,50 +58,33 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
public static function add_menu_page() {
|
||||
$title = __( 'Statify Filter', 'statify-blacklist' );
|
||||
if ( self::$multisite ) {
|
||||
add_submenu_page(
|
||||
'settings.php',
|
||||
add_options_page(
|
||||
$title,
|
||||
$title,
|
||||
'manage_network_plugins',
|
||||
'statify-blacklist-settings',
|
||||
array(
|
||||
'StatifyBlacklist_Admin',
|
||||
'settings_page',
|
||||
)
|
||||
'statify-blacklist',
|
||||
array( 'StatifyBlacklist_Settings', 'create_settings_page' )
|
||||
);
|
||||
} else {
|
||||
add_submenu_page(
|
||||
'options-general.php',
|
||||
add_options_page(
|
||||
$title,
|
||||
$title,
|
||||
'manage_options',
|
||||
'statify-blacklist',
|
||||
array(
|
||||
'StatifyBlacklist_Admin',
|
||||
'settings_page',
|
||||
)
|
||||
array( 'StatifyBlacklist_Settings', 'create_settings_page' )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Include the Statify-Blacklist settings page.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function settings_page() {
|
||||
include STATIFYBLACKLIST_DIR . '/views/settings-page.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add plugin meta links
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $links Registered links.
|
||||
* @param string $file The filename.
|
||||
*
|
||||
* @return array Merged links.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function plugin_meta_link( $links, $file ) {
|
||||
if ( STATIFYBLACKLIST_BASE === $file ) {
|
||||
@ -195,12 +97,12 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
/**
|
||||
* Add plugin action links.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $links Registered links.
|
||||
* @param string $file The filename.
|
||||
*
|
||||
* @return array Merged links.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function plugin_actions_links( $links, $file ) {
|
||||
$base = self::$multisite ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' );
|
||||
@ -298,11 +200,11 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
/**
|
||||
* Sanitize URLs and remove empty results.
|
||||
*
|
||||
* @since 1.1.1
|
||||
*
|
||||
* @param array $urls given array of URLs.
|
||||
*
|
||||
* @return array sanitized array.
|
||||
*
|
||||
* @since 1.1.1
|
||||
*/
|
||||
private static function sanitize_urls( $urls ) {
|
||||
return array_flip(
|
||||
@ -316,56 +218,4 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize IP addresses with optional CIDR notation and remove empty results.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @param array $ips given array of URLs.
|
||||
*
|
||||
* @return array sanitized array.
|
||||
*/
|
||||
private static function sanitize_ips( $ips ) {
|
||||
return array_filter(
|
||||
array_map( 'strtolower', $ips ),
|
||||
function ( $ip ) {
|
||||
return preg_match(
|
||||
'/^((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])(\/([0-9]|[1-2][0-9]|3[0-2]))?$/',
|
||||
$ip
|
||||
) ||
|
||||
preg_match(
|
||||
'/^(([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|([0-9a-f]{1,4}:){1,7}:|([0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}' .
|
||||
'|([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}|([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}' .
|
||||
'|([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}|([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}' .
|
||||
'|[0-9a-f]{1,4}:((:[0-9a-f]{1,4}){1,6})|:((:[0-9a-f]{1,4}){1,7}|:)' .
|
||||
'|fe80:(:[0-9a-f]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]' .
|
||||
'|1?[0-9])?[0-9])|([0-9a-f]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))' .
|
||||
'(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/i',
|
||||
$ip
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate regular expressions, i.e. remove duplicates and empty values and validate others.
|
||||
*
|
||||
* @since 1.5.0 #13
|
||||
*
|
||||
* @param array $expressions Given pre-sanitized array of regular expressions.
|
||||
*
|
||||
* @return array Array of invalid expressions.
|
||||
*/
|
||||
private static function sanitize_regex( $expressions ) {
|
||||
return array_filter(
|
||||
array_flip( $expressions ),
|
||||
function ( $re ) {
|
||||
// Check of preg_match() fails (warnings suppressed).
|
||||
|
||||
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
|
||||
return false === @preg_match( StatifyBlacklist::regex( $re, false ), null );
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user