Completed
Push — add/use-native-mysql-locks-whe... ( be4675...9e3d6a )
by
unknown
160:01 queued 150:59
created

Jetpack_Sync_Settings::get_settings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-defaults.php';
4
5
class Jetpack_Sync_Settings {
6
	const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_';
7
8
	static $valid_settings = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $valid_settings.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
9
		'dequeue_max_bytes'    => true,
10
		'upload_max_bytes'     => true,
11
		'upload_max_rows'      => true,
12
		'sync_wait_time'       => true,
13
		'sync_wait_threshold'  => true,
14
		'max_queue_size'       => true,
15
		'max_queue_lag'        => true,
16
		'queue_max_writes_sec' => true,
17
		'post_types_blacklist' => true,
18
		'meta_blacklist'       => true,
19
		'use_mysql_named_lock' => true, // 0 or 1
20
	);
21
22
	static $is_importing;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_importing.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
23
24
	static $settings_cache = array(); // some settings can be expensive to compute - let's cache them
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $settings_cache.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
25
26
	static function get_settings() {
27
		$settings = array();
28
		foreach ( array_keys( self::$valid_settings ) as $setting ) {
29
			$settings[ $setting ] = self::get_setting( $setting );
30
		}
31
32
		return $settings;
33
	}
34
35
	// Fetches the setting. It saves it if the setting doesn't exist, so that it gets
36
	// autoloaded on page load rather than re-queried every time.
37
	static function get_setting( $setting ) {
38
		if ( ! isset( self::$valid_settings[ $setting ] ) ) {
39
			return false;
40
		}
41
42
		if ( isset( self::$settings_cache[ $setting ] ) ) {
43
			return self::$settings_cache[ $setting ];
44
		}
45
46
		$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting );
47
48
		if ( false === $value ) {
49
			$default_name = "default_$setting"; // e.g. default_dequeue_max_bytes
50
			$value        = Jetpack_Sync_Defaults::$$default_name;
51
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
52
		}
53
54
		if ( is_numeric( $value ) ) {
55
			$value = intval( $value );
56
		}
57
58
		// specifically for the post_types blacklist, we want to include the hardcoded settings
59
		if ( $setting === 'post_types_blacklist' ) {
60
			$value = array_unique( array_merge( $value, Jetpack_Sync_Defaults::$blacklisted_post_types ) );
0 ignored issues
show
Bug introduced by
The property blacklisted_post_types cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
61
		}
62
63
		// ditto for meta blacklist
64
		if ( $setting === 'meta_blacklist' ) {
65
			$value = array_unique( array_merge( $value, Jetpack_Sync_Defaults::$default_blacklist_meta_keys ) );
0 ignored issues
show
Bug introduced by
The property default_blacklist_meta_keys cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
66
		}
67
68
		self::$settings_cache[ $setting ] = $value;
69
70
		return $value;
71
	}
72
73
	static function update_settings( $new_settings ) {
74
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
75
		foreach ( $validated_settings as $setting => $value ) {
76
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
77
			unset( self::$settings_cache[ $setting ] );
78
		}
79
	}
80
81
	// returns escapted SQL that can be injected into a WHERE clause
82
	static function get_blacklisted_post_types_sql() {
83
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
84
	}
85
86
	static function reset_data() {
87
		$valid_settings       = self::$valid_settings;
88
		self::$settings_cache = array();
89
		foreach ( $valid_settings as $option => $value ) {
90
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
91
		}
92
		self::set_importing( null );
93
	}
94
95
	static function set_importing( $is_importing ) {
96
		// set to NULL to revert to WP_IMPORTING, the standard behaviour
97
		self::$is_importing = $is_importing;
98
	}
99
100
	static function is_importing() {
101
		if ( ! is_null( self::$is_importing ) ) {
102
			return self::$is_importing;
103
		}
104
105
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
106
	}
107
}
108