PluginProbe
MyRewards / trunk
MyRewards vtrunk
5.7.8 5.7.7 5.7.6 trunk 1.2.2 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.1 2.2.0 2.3.0 2.4.0 2.4.1 2.5.0 2.6.4 2.6.6 3.0.0.0 3.1.0 3.1.2 3.1.2.1 3.10.4 3.10.9 All 165 releases
woorewards / include / options.php

options.php in MyRewards trunk, at include/options.php

123 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace LWS\WOOREWARDS;
3
4 // don't call the file directly
5 if( !defined( 'ABSPATH' ) ) exit();
6
7 /** Save Pool options from Admin screen.
8 * Use a faky hidden field named 'lws-wr-pool-option' as entry point
9 * and save relevant fields as pool post. */
10 class Options
11 {
12 private $optPool = 'lws-wr-pool-option';
13 private $poolOptionPrefix = 'lws-wr-pool-option-';
14 private $pagePool = LWS_WOOREWARDS_PAGE.'.loyalty';
15 private $whitelist = array();
16
17 function __construct()
18 {
19 \add_action('pre_update_option_'.$this->optPool, array($this, 'savePool'), 9, 3);
20
21 global $wp_version;
22 if( \version_compare(explode('-', $wp_version, 2)[0], '5.5', '>=') )
23 \add_filter('allowed_options', array($this, 'whitelistPoolOptions'), 99999);
24 else
25 \add_filter('whitelist_options', array($this, 'whitelistPoolOptions'), 99999);
26 }
27
28 /** @return bool */
29 function isOptionPage()
30 {
31 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- called inside WP settings API
32 if( !isset($_POST['tab']) || false === strpos(\sanitize_text_field(\wp_unslash($_POST['tab'])), 'wr_loyalty') )
33 return false;
34
35 $vars = array(
36 'action' => 'update',
37 'option_page' => LWS_WOOREWARDS_PAGE.'.loyalty'
38 );
39 foreach( $vars as $var => $value )
40 {
41 if( !isset($GLOBALS[$var]) || $GLOBALS[$var] != $value )
42 return false;
43 }
44
45 return true;
46 }
47
48 /** grab pool options */
49 function whitelistPoolOptions($whitelistOptions)
50 {
51 // we are from settings screen
52 if( $this->isOptionPage() && isset($whitelistOptions[$this->pagePool]) )
53 {
54 // take all page fields except pool id tweak
55 $lastOptions = array($this->optPool);
56 $this->whitelist = array_diff($whitelistOptions[$this->pagePool], $lastOptions);
57 // if include the pool id saving tweak field,
58 // means we are explicitely from one pool edition
59 // only this one will be saved, the rest is done by savePool()
60 if (count($whitelistOptions[$this->pagePool]) != count($this->whitelist)) {
61 $whitelistOptions[$this->pagePool] = $lastOptions;
62 }
63 }
64 return $whitelistOptions;
65 }
66
67 /** Save any relevant $_POST in pool wp_post.
68 * @return $oldValue cause WP dont go further with that option. */
69 function savePool($value, $oldValue, $option)
70 {
71 if( $this->whitelist && ($poolId = intval($value)) >= 0 )
72 {
73 $pool = \apply_filters('lws_wr_pool_admin_options_get_pool', null, $poolId, $this->whitelist, $this->poolOptionPrefix);
74 if( empty($pool) && $poolId > 0 )
75 $pool = \LWS\WOOREWARDS\Collections\Pools::instanciate()->load(array('p' => $poolId, 'deep'=>false))->last();
76
77 if( empty($pool) )
78 {
79 if (defined('WP_DEBUG') && WP_DEBUG) error_log("Requested pool cannot be loaded or created ($poolId)."); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
80 \lws_admin_add_notice_once('wr-pool-option-update-failure', __("Requested loyalty system cannot be loaded or created.", 'woorewards-lite'), array('level'=>'error'));
81 }
82 else
83 {
84 \do_action('lws_wr_pool_admin_options_before_update', $pool, $this->whitelist);
85
86 foreach( $this->whitelist as $option )
87 {
88 $option = trim($option);
89 $value = isset($_POST[$option]) ? \wp_unslash($_POST[$option]) : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- nonce verified by WP settings API, value sanitized by pool setOption
90
91 if( substr($option, 0, strlen($this->poolOptionPrefix)) == $this->poolOptionPrefix )
92 {
93 $key = substr($option, strlen($this->poolOptionPrefix));
94 $pool->setOption($key, $value);
95 // special case
96 if( $key == 'title' && $value && !($poolId && $pool->getName()) )
97 $pool->setName($value);
98 }
99 else if( $option != $this->optPool )
100 {
101 \update_option($option, $value);
102 }
103 }
104
105 if( $pool->getName() )
106 $pool->ensureNameUnicity();
107 else
108 {
109 \lws_admin_add_notice_once('wr-pool-no-name', __("Please, set a <b>Title</b> for this Points and Rewards System.", 'woorewards-lite'), array('level' => 'warning'));
110 if( !$pool->getOption('disabled') )
111 {
112 $pool->setOption('disabled', true);
113 \lws_admin_add_notice_once('wr-pool-no-name', __("A Points and Rewards System without a <b>Title</b> cannot be turned <b>On</b>.", 'woorewards-lite'), array('level' => 'warning'));
114 }
115 }
116 $pool->save(false, false);
117 \do_action('lws_wr_pool_admin_options_after_update', $pool, $this->whitelist);
118 }
119 }
120 return $oldValue;
121 }
122 }
123