Completed
Push — add/blacklist-and-whitelist-se... ( 25e567 )
by
unknown
101:29 queued 91:01
created

Jetpack_Sync_Settings::reset_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
	);
19
20
	static $array_settings = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $array_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...
21
		'post_types_blacklist',
22
	);
23
24
	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...
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
		$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting );
43
44
		if ( false === $value ) {
45
			$default_name = "default_$setting"; // e.g. default_dequeue_max_bytes
46
			$value        = Jetpack_Sync_Defaults::$$default_name;
47
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
48
		}
49
50
		if ( is_numeric( $value ) ) {
51
			return intval( $value );
52
		}
53
54
		// specifically for the post_types blacklist, we want to include the hardcoded settings
55
		if ( $setting === 'post_types_blacklist' ) {
56
			$value = 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...
57
		}
58
59
		return $value;
60
	}
61
62
	static function update_settings( $new_settings ) {
63
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
64
		foreach ( $validated_settings as $setting => $value ) {
65
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
66
		}
67
	}
68
69
	// returns escapted SQL that can be injected into a WHERE clause
70
	static function get_blacklisted_post_types_sql() {
71
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
72
	}
73
74
	static function reset_data() {
75
		$valid_settings  = self::$valid_settings;
76
		$settings_prefix = self::SETTINGS_OPTION_PREFIX;
77
		foreach ( $valid_settings as $option => $value ) {
78
			delete_option( $settings_prefix . $option );
79
		}
80
		self::set_importing( null );
81
	}
82
83
	static function set_importing( $is_importing ) {
84
		// set to NULL to revert to WP_IMPORTING, the standard behaviour
85
		self::$is_importing = $is_importing;
86
	}
87
88
	static function is_importing() {
89
		if ( ! is_null( self::$is_importing ) ) {
90
			return self::$is_importing;
91
		}
92
93
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
94
	}
95
}
96