Test Setup Failed
Push — issue/3871 ( 31b6b8 )
by Ravinder
07:30
created

Give_Cache_Setting   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 244
Duplicated Lines 3.69 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 9
loc 244
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get_instance() 9 9 2
A setup() 0 12 1
A load_plugin_settings() 0 28 4
A __reload_plugin_settings() 0 9 2
A __setup_currencies_list() 0 12 1
A __setup_gateways_list() 0 26 1
A get_option() 0 11 3
A get_settings() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Class for managing plugin setting cache
4
 * Note: only use for internal purpose.
5
 *
6
 * @package     Give
7
 * @subpackage  Classes/Give_Cache_Setting
8
 * @copyright   Copyright (c) 2018, GiveWP
9
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
10
 * @since       2.4.0
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
class Give_Cache_Setting {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
19
	/**
20
	 * Instance.
21
	 *
22
	 * @since  2.4.0
23
	 * @access private
24
	 * @var Give_Cache_Setting
25
	 */
26
	static private $instance;
27
28
	/**
29
	 * Instance.
30
	 *
31
	 * @since  2.4.0
32
	 * @access private
33
	 * @var string
34
	 */
35
	static private $cache_key = 'giveAllOptions';
36
37
	/**
38
	 * Array of cached settings
39
	 *
40
	 * @since  2.4.0
41
	 * @access private
42
	 * @var array
43
	 */
44
	static private $settings = array(
45
		'give_settings'           => array(),
46
		'give_version'            => '',
47
		'give_completed_upgrades' => array(),
48
		'currencies'              => array(),
49
		'gateways'                => array(),
50
	);
51
52
	/**
53
	 * Array of cached setting db option names
54
	 *
55
	 * @since  2.4.0
56
	 * @access private
57
	 * @var array
58
	 */
59
	static private $db_option_ids = array(
60
		'give_settings',
61
		'give_version',
62
		'give_completed_upgrades',
63
	);
64
65
	/**
66
	 * Array of cached setting option names
67
	 *
68
	 * @since  2.4.0
69
	 * @access private
70
	 * @var array
71
	 */
72
	static private $all_option_ids;
73
74
	/**
75
	 * Singleton pattern.
76
	 *
77
	 * @since  2.4.0
78
	 * @access private
79
	 */
80
	private function __construct() {
81
	}
82
83
84
	/**
85
	 * Get instance.
86
	 *
87
	 * @since  2.4.0
88
	 * @access public
89
	 * @return Give_Cache_Setting
90
	 */
91 View Code Duplication
	public static function get_instance() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
		if ( null === static::$instance ) {
93
			self::$instance = new static();
94
95
			self::$instance->setup();
96
		}
97
98
		return self::$instance;
99
	}
100
101
	/**
102
	 * Setup
103
	 *
104
	 * @since  2.4.0
105
	 * @access private
106
	 */
107
	private function setup() {
108
		self::$all_option_ids = array_keys( self::$settings );
109
110
		$this->load_plugin_settings();
111
112
		add_action( 'added_option', array( $this, '__reload_plugin_settings' ) );
113
		add_action( 'updated_option', array( $this, '__reload_plugin_settings' ) );
114
		add_action( 'deleted_option', array( $this, '__reload_plugin_settings' ) );
115
116
		add_action( 'give_init', array( $this, '__setup_currencies_list' ), 11 );
117
		add_action( 'give_init', array( $this, '__setup_gateways_list' ), 11 );
118
	}
119
120
	/**
121
	 * Load plugin settings
122
	 *
123
	 * @since  2.4.0
124
	 * @access private
125
	 */
126
	private function load_plugin_settings() {
127
		global $wpdb;
128
129
		$cache = wp_cache_get( self::$cache_key, 'options' );
130
131
		// Load options from cache.
132
		if ( false !== $cache ) {
133
			self::$settings = $cache;
134
135
			return;
136
		}
137
138
		$db_option_ids = '\'' . implode( '\',\'', self::$db_option_ids ) . '\'';
139
140
		$tmp     = array();
141
		$sql     = "SELECT option_name, option_value FROM $wpdb->options WHERE option_name IN ({$db_option_ids}) ";
142
		$results = $wpdb->get_results( $sql );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
143
144
		if ( ! empty( $results ) ) {
145
146
			/* @var  stdClass $result */
147
			foreach ( $results as $result ) {
148
				self::$settings[ $result->option_name ] = maybe_unserialize( $result->option_value );
149
			}
150
151
			wp_cache_set( self::$cache_key, $tmp, 'options' );
152
		}
153
	}
154
155
	/**
156
	 * Reload option when add, update or delete
157
	 * Note: only for internal logic
158
	 *
159
	 * @since 2.4.0
160
	 *
161
	 * @param $option_name
162
	 */
163
	public function __reload_plugin_settings( $option_name ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Cache_Setting::__reload_plugin_settings" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
164
		// Bailout.
165
		if ( ! in_array( $option_name, self::$db_option_ids ) ) {
166
			return;
167
		}
168
169
		wp_cache_delete( self::$cache_key, 'options' );
170
		$this->load_plugin_settings();
171
	}
172
173
	/**
174
	 * Setup currencies list
175
	 *
176
	 * @since 2.4.0
177
	 */
178
	public function __setup_currencies_list() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Cache_Setting::__setup_currencies_list" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
179
		$currencies = require_once GIVE_PLUGIN_DIR . 'includes/currency/currencies-list.php';
180
181
		/**
182
		 * Filter the supported currency list
183
		 *
184
		 * @since 2.4.0
185
		 */
186
		$currencies = apply_filters( 'give_register_currency', $currencies );
187
188
		self::$settings['currencies'] = $currencies;
189
	}
190
191
192
	/**
193
	 * Setup gateway list
194
	 *
195
	 * @since 2.4.0
196
	 */
197
	public function __setup_gateways_list() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Cache_Setting::__setup_gateways_list" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
198
		// Default, built-in gateways
199
		$gateways = array(
200
			'paypal'  => array(
201
				'admin_label'    => __( 'PayPal Standard', 'give' ),
202
				'checkout_label' => __( 'PayPal', 'give' ),
203
			),
204
			'manual'  => array(
205
				'admin_label'    => __( 'Test Donation', 'give' ),
206
				'checkout_label' => __( 'Test Donation', 'give' ),
207
			),
208
			'offline' => array(
209
				'admin_label'    => esc_attr__( 'Offline Donation', 'give' ),
210
				'checkout_label' => esc_attr__( 'Offline Donation', 'give' ),
211
			),
212
		);
213
214
		/**
215
		 * Filter the supported gateways list
216
		 *
217
		 * @since 2.4.0
218
		 */
219
		$gateways = apply_filters( 'give_register_gateway', $gateways );
220
221
		self::$settings['gateways'] = $gateways;
222
	}
223
224
225
	/**
226
	 * Get option
227
	 *
228
	 * @since  2.4.0
229
	 * @access public
230
	 *
231
	 * @param      $option_name
232
	 * @param bool $default
233
	 *
234
	 * @return mixed
235
	 */
236
	public static function get_option( $option_name, $default = false ) {
237
		$value = $default;
238
239
		if ( in_array( $option_name, self::$all_option_ids ) ) {
240
			$value = ! empty( self::$settings[ $option_name ] )
241
				? self::$settings[ $option_name ]
242
				: $default;
243
		}
244
245
		return $value;
246
	}
247
248
	/**
249
	 * Get plugin settings
250
	 *
251
	 * @since  2.4.0
252
	 * @access public
253
	 */
254
	public static function get_settings() {
255
256
		/**
257
		 * Filter the plugin setting
258
		 */
259
		return (array) apply_filters( 'give_get_settings', self::$settings['give_settings'] );
260
	}
261
}
262
263
Give_Cache_Setting::get_instance();
264