Completed
Push — branch-4.2 ( dbc98d...c2cbfd )
by
unknown
203:41 queued 194:34
created

Jetpack_Sync_Settings   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 2 Features 1
Metric Value
c 5
b 2
f 1
dl 0
loc 102
rs 10
wmc 18
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_settings() 0 8 2
C get_setting() 0 35 7
A update_settings() 0 7 2
A get_blacklisted_post_types_sql() 0 3 1
A reset_data() 0 8 2
A set_importing() 0 4 1
A is_importing() 0 7 3
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
	);
20
21
	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...
22
23
	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...
24
25
	static function get_settings() {
26
		$settings = array();
27
		foreach ( array_keys( self::$valid_settings ) as $setting ) {
28
			$settings[ $setting ] = self::get_setting( $setting );
29
		}
30
31
		return $settings;
32
	}
33
34
	// Fetches the setting. It saves it if the setting doesn't exist, so that it gets
35
	// autoloaded on page load rather than re-queried every time.
36
	static function get_setting( $setting ) {
37
		if ( ! isset( self::$valid_settings[ $setting ] ) ) {
38
			return false;
39
		}
40
41
		if ( isset( self::$settings_cache[ $setting ] ) ) {
42
			return self::$settings_cache[ $setting ];
43
		}
44
45
		$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting );
46
47
		if ( false === $value ) {
48
			$default_name = "default_$setting"; // e.g. default_dequeue_max_bytes
49
			$value        = Jetpack_Sync_Defaults::$$default_name;
50
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
51
		}
52
53
		if ( is_numeric( $value ) ) {
54
			$value = intval( $value );
55
		}
56
57
		// specifically for the post_types blacklist, we want to include the hardcoded settings
58
		if ( $setting === 'post_types_blacklist' ) {
59
			$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...
60
		}
61
62
		// ditto for meta blacklist
63
		if ( $setting === 'meta_blacklist' ) {
64
			$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...
65
		}
66
67
		self::$settings_cache[ $setting ] = $value;
68
69
		return $value;
70
	}
71
72
	static function update_settings( $new_settings ) {
73
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
74
		foreach ( $validated_settings as $setting => $value ) {
75
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
76
			unset( self::$settings_cache[ $setting ] );
77
		}
78
	}
79
80
	// returns escapted SQL that can be injected into a WHERE clause
81
	static function get_blacklisted_post_types_sql() {
82
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
83
	}
84
85
	static function reset_data() {
86
		$valid_settings       = self::$valid_settings;
87
		self::$settings_cache = array();
88
		foreach ( $valid_settings as $option => $value ) {
89
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
90
		}
91
		self::set_importing( null );
92
	}
93
94
	static function set_importing( $is_importing ) {
95
		// set to NULL to revert to WP_IMPORTING, the standard behaviour
96
		self::$is_importing = $is_importing;
97
	}
98
99
	static function is_importing() {
100
		if ( ! is_null( self::$is_importing ) ) {
101
			return self::$is_importing;
102
		}
103
104
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
105
	}
106
}
107