Completed
Push — fix/double-ww-modal ( 5791ec )
by
unknown
09:48
created

class.jetpack-admin-page.php ➔ add_no_store_header()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
function add_no_store_header( $headers ) {
4
	$headers[ 'Cache-Control' ] .= ', no-store';
5
	return 	$headers;
6
}
7
8
// Shared logic between Jetpack admin pages
9
abstract class Jetpack_Admin_Page {
10
	// Add page specific actions given the page hook
11
	abstract function add_page_actions( $hook );
12
13
	// Create a menu item for the page and returns the hook
14
	abstract function get_page_hook();
15
16
	// Enqueue and localize page specific scripts
17
	abstract function page_admin_scripts();
18
19
	// Render page specific HTML
20
	abstract function page_render();
21
22
	/**
23
	 * Should we block the page rendering because the site is in IDC?
24
	 * @var bool
25
	 */
26
	static $block_page_rendering_for_idc;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $block_page_rendering_for_idc.

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...
27
28
	/**
29
	 * Flag to know if we already checked the plan.
30
	 *
31
	 * @since 4.4.0
32
	 *
33
	 * @var bool
34
	 */
35
	static $plan_checked = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $plan_checked.

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...
36
37
	/**
38
	 * Function called after admin_styles to load any additional needed styles.
39
	 *
40
	 * @since 4.3.0
41
	 */
42
	function additional_styles() {}
43
44
	function __construct() {
45
		$this->jetpack = Jetpack::init();
0 ignored issues
show
Bug introduced by
The property jetpack does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
		self::$block_page_rendering_for_idc = (
47
			Jetpack::validate_sync_error_idc_option() && ! Jetpack_Options::get_option( 'safe_mode_confirmed' )
48
		);
49
		if( $_GET[ 'page' ] == 'jetpack' ) {
50
			add_filter( 'nocache_headers', 'add_no_store_header' );
51
			nocache_headers();
52
		}
53
	}
54
55
	function add_actions() {
56
57
		// If user is not an admin and site is in Dev Mode, don't do anything
58
		if ( ! current_user_can( 'manage_options' ) && Jetpack::is_development_mode() ) {
59
			return;
60
		}
61
62
		// Don't add in the modules page unless modules are available!
63
		if ( $this->dont_show_if_not_active && ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
0 ignored issues
show
Bug introduced by
The property dont_show_if_not_active does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
64
			return;
65
		}
66
67
		// Initialize menu item for the page in the admin
68
		$hook = $this->get_page_hook();
69
70
		// Attach hooks common to all Jetpack admin pages based on the created
71
		// hook
72
		add_action( "load-$hook",                array( $this, 'admin_help'      ) );
73
		add_action( "load-$hook",                array( $this, 'admin_page_load' ) );
74
		add_action( "admin_head-$hook",          array( $this, 'admin_head'      ) );
75
76
		add_action( "admin_print_styles-$hook",  array( $this, 'admin_styles'    ) );
77
		add_action( "admin_print_scripts-$hook", array( $this, 'admin_scripts'   ) );
78
79
		if ( ! self::$block_page_rendering_for_idc ) {
80
			add_action( "admin_print_styles-$hook", array( $this, 'additional_styles' ) );
81
		}
82
83
		// Check if the site plan changed and deactivate modules accordingly.
84
		add_action( 'current_screen', array( $this, 'check_plan_deactivate_modules' ) );
85
86
		// Attach page specific actions in addition to the above
87
		$this->add_page_actions( $hook );
88
	}
89
90
	function admin_head() {
91 View Code Duplication
		if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) {
92
			/**
93
			 * Fires in the <head> of a particular Jetpack configuation page.
94
			 *
95
			 * The dynamic portion of the hook name, `$_GET['configure']`,
96
			 * refers to the slug of module, such as 'stats', 'sso', etc.
97
			 * A complete hook for the latter would be
98
			 * 'jetpack_module_configuation_head_sso'.
99
			 *
100
			 * @since 3.0.0
101
			 */
102
			do_action( 'jetpack_module_configuration_head_' . $_GET['configure'] );
103
		}
104
	}
105
106
	// Render the page with a common top and bottom part, and page specific content
107
	function render() {
108
		// We're in an IDC: we need a decision made before we show the UI again.
109
		if ( self::$block_page_rendering_for_idc ) {
110
			return;
111
		}
112
113
		$this->page_render();
114
	}
115
116
	function admin_help() {
117
		$this->jetpack->admin_help();
118
	}
119
120
	function admin_page_load() {
121
		// This is big.  For the moment, just call the existing one.
122
		$this->jetpack->admin_page_load();
123
	}
124
125
	function admin_page_top() {
126
		include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' );
127
	}
128
129
	function admin_page_bottom() {
130
		include_once( JETPACK__PLUGIN_DIR . '_inc/footer.php' );
131
	}
132
133
	// Add page specific scripts and jetpack stats for all menu pages
134
	function admin_scripts() {
135
		$this->page_admin_scripts(); // Delegate to inheriting class
136
		add_action( 'admin_footer', array( $this->jetpack, 'do_stats' ) );
137
	}
138
139
	// Enqueue the Jetpack admin stylesheet
140
	function admin_styles() {
141
		$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
142
143
		wp_enqueue_style( 'jetpack-admin', plugins_url( "css/jetpack-admin{$min}.css", JETPACK__PLUGIN_FILE ), array( 'genericons' ), JETPACK__VERSION . '-20121016' );
144
		wp_style_add_data( 'jetpack-admin', 'rtl', 'replace' );
145
		wp_style_add_data( 'jetpack-admin', 'suffix', $min );
146
	}
147
148
	/**
149
	 * Checks if WordPress version is too old to have REST API.
150
	 *
151
	 * @since 4.3
152
	 *
153
	 * @return bool
154
	 */
155
	function is_wp_version_too_old() {
156
		global $wp_version;
157
		return ( ! function_exists( 'rest_api_init' ) || version_compare( $wp_version, '4.4-z', '<=' ) );
158
	}
159
160
	/**
161
	 * Checks if REST API is enabled.
162
	 *
163
	 * @since 4.4.2
164
	 *
165
	 * @return bool
166
	 */
167
	function is_rest_api_enabled() {
168
		return
169
			/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
170
			apply_filters( 'rest_enabled', true ) &&
171
			/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
172
			apply_filters( 'rest_jsonp_enabled', true ) &&
173
			/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
174
			apply_filters( 'rest_authentication_errors', true );
175
	}
176
177
	/**
178
	 * Checks the site plan and deactivates modules that were active but are no longer included in the plan.
179
	 *
180
	 * @since 4.4.0
181
	 *
182
	 * @param $page
183
	 *
184
	 * @return bool|array
185
	 */
186
	function check_plan_deactivate_modules( $page ) {
187
		if (
188
			Jetpack::is_development_mode()
189
			|| ! in_array(
190
				$page->base,
191
				array(
192
					'toplevel_page_jetpack',
193
					'admin_page_jetpack_modules',
194
					'jetpack_page_vaultpress',
195
					'jetpack_page_stats',
196
					'jetpack_page_akismet-key-config'
197
				)
198
			)
199
			|| true === self::$plan_checked
200
		) {
201
			return false;
202
		}
203
204
		self::$plan_checked = true;
205
		$previous = get_option( 'jetpack_active_plan', '' );
206
		$current = Jetpack_Core_Json_Api_Endpoints::site_data();
207
		if ( ! $current || is_wp_error( $current ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $current of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
208
			// If we can't get information about the current plan we don't do anything
209
			self::$plan_checked = true;
210
			return;
211
		}
212
213
		$to_deactivate = array();
214
		if ( isset( $current->plan->product_slug ) ) {
215
			if (
216
				empty( $previous )
217
				|| ! isset( $previous['product_slug'] )
218
				|| $previous['product_slug'] !== $current->plan->product_slug
219
			) {
220
				$active = Jetpack::get_active_modules();
221
				switch ( $current->plan->product_slug ) {
222
					case 'jetpack_free':
223
						$to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics', 'wordads' );
224
						break;
225
					case 'jetpack_personal':
226
					case 'jetpack_personal_monthly':
227
						$to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics', 'wordads' );
228
						break;
229
					case 'jetpack_premium':
230
					case 'jetpack_premium_monthly':
231
						$to_deactivate = array( 'seo-tools', 'google-analytics' );
232
						break;
233
				}
234
				$to_deactivate = array_intersect( $active, $to_deactivate );
235
				if ( ! empty( $to_deactivate ) ) {
236
					Jetpack::update_active_modules( array_filter( array_diff( $active, $to_deactivate ) ) );
237
				}
238
			}
239
		}
240
		return array(
241
			'previous'   => $previous,
242
			'current'    => $current,
243
			'deactivate' => $to_deactivate
244
		);
245
	}
246
}
247