Completed
Pull Request — master (#254)
by
unknown
01:50
created

autoptimizeOption::update_option()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 3
dl 13
loc 13
rs 9.8333
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
	 * Ensure that is_plugin_active_for_network function is declared.
25
	 */
26
	public static function maybe_include_plugin_functions() {
27
		if( ! function_exists( 'is_plugin_active_for_network' ) ) {
28
			include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
29
		}
30
	}
31
32
	/**
33
	 * Retrieves the option in standalone and multisite instances.
34
	 * 
35
	 * @param string $option  Name of option to retrieve. Expected to not be SQL-escaped.
36
	 * @param mixed  $default Optional. Default value to return if the option does not exist.
37
	 * @return mixed Value set for the option.
38
	 */
39
    public static function get_option( $option, $default = false )
40
    {
41
		// Ensure that is_plugin_active_for_network function is declared.
42
		self::maybe_include_plugin_functions();
43
		
44
		// This is always a network setting.
45
		if( 'autoptimize_enable_site_config' === $option ) {
46
			return get_network_option( get_main_network_id(), $option );
47
		}
48
49
		// If the plugin is network activated and our per site setting is not on, use the network configuration.
50
		$configuration_per_site = get_network_option( get_main_network_id(), 'autoptimize_enable_site_config' );
51
		if ( is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) && 'on' !== $configuration_per_site ) {
52
			return get_network_option( get_main_network_id(), $option );
53
		}
54
			
55
		return get_option( $option, $default );
56
    }
57
58
	/**
59
	 * Saves the option in standalone and multisite instances.
60
	 * 
61
	 * @param string      $option   Option name. Expected to not be SQL-escaped.
62
	 * @param mixed       $value    Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
63
	 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
64
	 *                              `$autoload` can only be updated using `update_option()` if `$value` is also changed.
65
     *			                    Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
66
     *				                the default value is 'yes'. Default null.
67
     * @return bool False if value was not updated and true if value was updated.
68
	 */
69 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...
70
    {
71
		
72
		// Ensure that is_plugin_active_for_network function is declared.
73
		self::maybe_include_plugin_functions();
74
		$blog_id = get_current_blog_id();
75
		
76
		if ( is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) && 1 === $blog_id ) {
77
			return update_network_option( get_main_network_id(), $option, $value );
78
		} else {
79
			return update_option( $option, $value, $autoload );
80
		}
81
    }
82
83
	/**
84
	 * Use the pre_update_option filter to check if the option to be saved if from autoptimize and
85
	 * in that case, take care of multisite case.
86
	 */
87
	public static function check_multisite_on_saving_options()
88
    {
89
		// Ensure that is_plugin_active_for_network function is declared.
90
		self::maybe_include_plugin_functions();
91
		$blog_id = get_current_blog_id();
92
		
93
		if ( is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) && 1 === $blog_id ) {
94
			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...
95
		}
96
	}
97
98 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...
99
		if( strpos( $option, 'autoptimize_' ) === 0 ) {
100
			
101
			// Ensure that is_plugin_active_for_network function is declared.
102
			self::maybe_include_plugin_functions();
103
			$blog_id = get_current_blog_id();
104
		
105
			if ( is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) && 1 === $blog_id ) {
106
				 update_network_option( get_main_network_id(), $option, $value );
107
				 // Return old value, to stop update_option logic.
108
				 return $old_value;
109
			}
110
		}
111
		return $value;
112
	}
113
}
114
new autoptimizeOption();
115