Completed
Pull Request — master (#254)
by
unknown
02:09
created

update_autoptimize_option_on_network()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 3
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Autoptimize options handler.
4
 */
5
6
if ( ! defined( 'ABSPATH' ) ) {
7
    exit;
8
}
9
10
/**
11
 * This class takes care of the set and get of option for standalone and multisite WordPress instances.
12
 */
13
class autoptimizeOption
14
{
15
	/**
16
	 * Constructor, add filter on saving options.
17
	 */
18
	public function __construct()
19
    {
20
		add_action('init', [$this, 'check_multisite_on_saving_options']);
21
	}
22
23
	/**
24
	 * Retrieves the option in standalone and multisite instances.
25
	 * 
26
	 * @param string $option  Name of option to retrieve. Expected to not be SQL-escaped.
27
	 * @param mixed  $default Optional. Default value to return if the option does not exist.
28
	 * @return mixed Value set for the option.
29
	 */
30
    public static function get_option( $option, $default = false )
31
    {
32
		if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
33
			return get_network_option( get_main_network_id(), $option );
34
		} else {
35
			return get_option( $option, $default );
36
		}
37
    }
38
39
	/**
40
	 * Saves the option in standalone and multisite instances.
41
	 * 
42
	 * @param string      $option   Option name. Expected to not be SQL-escaped.
43
	 * @param mixed       $value    Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
44
	 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
45
	 *                              `$autoload` can only be updated using `update_option()` if `$value` is also changed.
46
     *			                    Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
47
     *				                the default value is 'yes'. Default null.
48
     * @return bool False if value was not updated and true if value was updated.
49
	 */
50 View Code Duplication
    public static function update_option( $option, $value, $autoload = null )
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...
51
    {
52
		if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
53
			return update_network_option( get_main_network_id(), $option, $value );
54
		} else {
55
			return update_option( $option, $value, $autoload );
56
		}
57
    }
58
59
	/**
60
	 * Use the pre_update_option filter to check if the option to be saved if from autoptimize and
61
	 * in that case, take care of multisite case.
62
	 */
63
	public static function check_multisite_on_saving_options()
64
    {
65
		if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
66
			add_filter( 'pre_update_option', [$this, 'update_autoptimize_option_on_network'], 10, 3 );
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
67
		}
68
	}
69
70 View Code Duplication
	public static function update_autoptimize_option_on_network( $value, $option, $old_value ) {
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...
71
		if( strpos( $option, 'autoptimize_' ) === 0 ) {
72
			if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
73
				 update_network_option( get_main_network_id(), $option, $value );
74
				 // Return old value, to stop update_option logic.
75
				 return $old_value;
76
			}
77
		}
78
		return $value;
79
	}
80
}
81
new autoptimizeOption();
82