Completed
Push — fix/aag-section-header-mobile ( 4c98bd )
by
unknown
732:29 queued 720:04
created

Jetpack_Sync_Settings::is_doing_cron()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 7
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
		'disable'              => true,
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
	static $is_doing_cron;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_doing_cron.

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 $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...
26
27
	static function get_settings() {
28
		$settings = array();
29
		foreach ( array_keys( self::$valid_settings ) as $setting ) {
30
			$settings[ $setting ] = self::get_setting( $setting );
31
		}
32
33
		return $settings;
34
	}
35
36
	// Fetches the setting. It saves it if the setting doesn't exist, so that it gets
37
	// autoloaded on page load rather than re-queried every time.
38
	static function get_setting( $setting ) {
39
		if ( ! isset( self::$valid_settings[ $setting ] ) ) {
40
			return false;
41
		}
42
43
		if ( isset( self::$settings_cache[ $setting ] ) ) {
44
			return self::$settings_cache[ $setting ];
45
		}
46
47
		$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting );
48
49
		if ( false === $value ) {
50
			$default_name = "default_$setting"; // e.g. default_dequeue_max_bytes
51
			$value        = Jetpack_Sync_Defaults::$$default_name;
52
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
53
		}
54
55
		if ( is_numeric( $value ) ) {
56
			$value = intval( $value );
57
		}
58
59
		// specifically for the post_types blacklist, we want to include the hardcoded settings
60
		if ( $setting === 'post_types_blacklist' ) {
61
			$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...
62
		}
63
64
		// ditto for meta blacklist
65
		if ( $setting === 'meta_blacklist' ) {
66
			$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...
67
		}
68
69
		self::$settings_cache[ $setting ] = $value;
70
71
		return $value;
72
	}
73
74
	static function update_settings( $new_settings ) {
75
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
76
		foreach ( $validated_settings as $setting => $value ) {
77
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
78
			unset( self::$settings_cache[ $setting ] );
79
80
			// if we set the disabled option to true, clear the queues
81
			if ( 'disable' === $setting && !! $value ) {
82
				$listener = Jetpack_Sync_Listener::get_instance();
83
				$listener->get_sync_queue()->reset();
84
				$listener->get_full_sync_queue()->reset();
85
			}
86
		}
87
	}
88
89
	// returns escapted SQL that can be injected into a WHERE clause
90
	static function get_blacklisted_post_types_sql() {
91
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
92
	}
93
94
	static function reset_data() {
95
		$valid_settings       = self::$valid_settings;
96
		self::$settings_cache = array();
97
		foreach ( $valid_settings as $option => $value ) {
98
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
99
		}
100
		self::set_importing( null );
101
		self::set_doing_cron( null );
102
	}
103
104
	static function set_importing( $is_importing ) {
105
		// set to NULL to revert to WP_IMPORTING, the standard behaviour
106
		self::$is_importing = $is_importing;
107
	}
108
109
	static function is_importing() {
110
		if ( ! is_null( self::$is_importing ) ) {
111
			return self::$is_importing;
112
		}
113
114
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
115
	}
116
117
	static function set_doing_cron( $is_doing_cron ) {
118
		// set to NULL to revert to WP_IMPORTING, the standard behaviour
119
		self::$is_doing_cron = $is_doing_cron;
120
	}
121
122
	static function is_doing_cron() {
123
		if ( ! is_null( self::$is_doing_cron ) ) {
124
			return self::$is_doing_cron;
125
		}
126
127
		return defined( 'DOING_CRON' ) && DOING_CRON;
128
	}
129
}
130