From 38798472a177254d6c4f1346b52d949f60917f57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Colombaro?= Date: Sun, 16 Dec 2018 20:36:53 +0100 Subject: [PATCH] Fix admin option parsing --- src/class-wp-auto-links-helper.php | 10 +++ templates/admin.php | 135 ++++++++++++++--------------- 2 files changed, 77 insertions(+), 68 deletions(-) diff --git a/src/class-wp-auto-links-helper.php b/src/class-wp-auto-links-helper.php index 7319e31..ae721dc 100644 --- a/src/class-wp-auto-links-helper.php +++ b/src/class-wp-auto-links-helper.php @@ -256,6 +256,16 @@ public function show_options() include dirname(__DIR__).'/templates/admin.php'; } + /** + * @param mixed $value + * @param int $null + * @return int + */ + public static function option_integer($value, int $null = 0): int + { + return (is_numeric($value) && $value > 0) ? (int) $value : $null; + } + /** * Instantiate the filter. * diff --git a/templates/admin.php b/templates/admin.php index 221bac5..7119458 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -4,8 +4,8 @@ * Admin page */ -$instance = WP_Auto_Links_Helper::get_instance(); -$options = $instance->get_options(); +$helper = WP_Auto_Links_Helper::get_instance(); +$options = $helper->get_options(); $boolean_options = [ // Content handle @@ -42,9 +42,13 @@ ]; if (isset($_POST['submitted'])) { - check_admin_referer($instance::DOMAIN); + check_admin_referer($helper::DOMAIN); - foreach ($integer_options + $boolean_options as $option_name) { + foreach ($boolean_options as $option_name) { + $options[$option_name] = (isset($_POST[$option_name]) && !empty($_POST[$option_name])) ? (bool) $_POST[$option_name] : false; + } + + foreach ($integer_options as $option_name) { $val = (isset($_POST[$option_name]) && !empty($_POST[$option_name])) ? $_POST[$option_name] : 0; $options[$option_name] = is_numeric($val) ? (int) $val : 1; } @@ -54,9 +58,9 @@ $options['post_ignore'] = array_filter(array_map('trim', explode(',', sanitize_text_field($_POST['post_ignore']))), 'is_numeric'); $options['term_ignore'] = array_filter(array_map('trim', explode(',', sanitize_text_field($_POST['term_ignore']))), 'is_numeric'); - $instance->set_options($options); + $helper->set_options($options); - echo '

' . __('Plugin settings saved.', $instance::DOMAIN) . '

'; + echo '

' . __('Plugin settings saved.', $helper::DOMAIN) . '

'; } foreach ($boolean_options as $option_name) { @@ -71,15 +75,10 @@ $options['keywords'] = stripslashes($options['keywords']); -function not_null($value, int $null = 0): bool -{ - return (is_numeric($value) && $value > 0) ? (int) $value : $null; -} - -$options['max_links'] = not_null($options['max_links']); -$options['max_single_keyword'] = not_null($options['max_single_keyword'], -1); -$options['max_single_url'] = not_null($options['max_single_url']); -$options['min_term_usage'] = not_null($options['min_term_usage'], 1); +$options['max_links'] = $helper::option_integer($options['max_links']); +$options['max_single_keyword'] = $helper::option_integer($options['max_single_keyword'], -1); +$options['max_single_url'] = $helper::option_integer($options['max_single_url']); +$options['min_term_usage'] = $helper::option_integer($options['min_term_usage'], 1); ?> @@ -89,11 +88,11 @@ function not_null($value, int $null = 0): bool
- +

-

+

@@ -105,26 +104,26 @@ function not_null($value, int $null = 0): bool - +

- +

example,auto links,https://example.com
wiki,wikipedia,https://en.wikipedia.org/wiki/Main_Page
-

-

+

+

@@ -137,7 +136,7 @@ function not_null($value, int $null = 0): bool
@@ -151,7 +150,7 @@ function not_null($value, int $null = 0): bool
@@ -162,15 +161,15 @@ function not_null($value, int $null = 0): bool /> - may_slow_down(); ?> + may_slow_down(); ?> - + - + - +

@@ -178,29 +177,29 @@ function not_null($value, int $null = 0): bool

- may_slow_down(); ?> + may_slow_down(); ?>
-

-

+

+

@@ -208,9 +207,9 @@ function not_null($value, int $null = 0): bool @@ -218,9 +217,9 @@ function not_null($value, int $null = 0): bool @@ -228,9 +227,9 @@ function not_null($value, int $null = 0): bool @@ -238,9 +237,9 @@ function not_null($value, int $null = 0): bool @@ -248,57 +247,57 @@ function not_null($value, int $null = 0): bool
- may_slow_down(); ?> + may_slow_down(); ?>
- may_slow_down(); ?> + may_slow_down(); ?>
- may_slow_down(); ?> + may_slow_down(); ?>
- may_slow_down(); ?> + may_slow_down(); ?>
- may_slow_down(); ?> + may_slow_down(); ?>

- +

-

+

- + - + - + @@ -306,72 +305,72 @@ function not_null($value, int $null = 0): bool
+ value="" placeholder=""/>

- +

- +

+ value="" placeholder=""/>

- +

-

+

- + - + - +

- +

- +

- +