Completed
Push — feature/assets-cdn ( 4f639f...7aef3f )
by George
07:52
created

Jetpack_Admin_Page::admin_page_top()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
// Shared logic between Jetpack admin pages
4
abstract class Jetpack_Admin_Page {
5
	// Add page specific actions given the page hook
6
	abstract function add_page_actions( $hook );
7
8
	// Create a menu item for the page and returns the hook
9
	abstract function get_page_hook();
10
11
	// Enqueue and localize page specific scripts
12
	abstract function page_admin_scripts();
13
14
	// Render page specific HTML
15
	abstract function page_render();
16
17
	/**
18
	 * Should we block the page rendering because the site is in IDC?
19
	 * @var bool
20
	 */
21
	static $block_page_rendering_for_idc;
22
23
	/**
24
	 * Function called after admin_styles to load any additional needed styles.
25
	 *
26
	 * @since 4.3.0
27
	 */
28
	function additional_styles() {}
29
30
	function __construct() {
31
		$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...
32
		self::$block_page_rendering_for_idc = (
33
			Jetpack::validate_sync_error_idc_option() && ! Jetpack_Options::get_option( 'safe_mode_confirmed' )
34
		);
35
	}
36
37
	function add_actions() {
38
39
		// If user is not an admin and site is in Dev Mode, don't do anything
40
		if ( ! current_user_can( 'manage_options' ) && Jetpack::is_development_mode() ) {
41
			return;
42
		}
43
44
		// Don't add in the modules page unless modules are available!
45
		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...
46
			return;
47
		}
48
49
		// Initialize menu item for the page in the admin
50
		$hook = $this->get_page_hook();
51
52
		// Attach hooks common to all Jetpack admin pages based on the created
53
		// hook
54
		add_action( "load-$hook",                array( $this, 'admin_help'      ) );
55
		add_action( "load-$hook",                array( $this, 'admin_page_load' ) );
56
		add_action( "admin_head-$hook",          array( $this, 'admin_head'      ) );
57
58
		add_action( "admin_print_styles-$hook",  array( $this, 'admin_styles'    ) );
59
		add_action( "admin_print_scripts-$hook", array( $this, 'admin_scripts'   ) );
60
61
		if ( ! self::$block_page_rendering_for_idc ) {
62
			add_action( "admin_print_styles-$hook", array( $this, 'additional_styles' ) );
63
		}
64
65
		// Check if the site plan changed and deactivate modules accordingly.
66
		add_action( 'current_screen', array( $this, 'check_plan_deactivate_modules' ) );
67
68
		// Attach page specific actions in addition to the above
69
		$this->add_page_actions( $hook );
70
	}
71
72
	function admin_head() {
73 View Code Duplication
		if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) {
74
			/**
75
			 * Fires in the <head> of a particular Jetpack configuation page.
76
			 *
77
			 * The dynamic portion of the hook name, `$_GET['configure']`,
78
			 * refers to the slug of module, such as 'stats', 'sso', etc.
79
			 * A complete hook for the latter would be
80
			 * 'jetpack_module_configuation_head_sso'.
81
			 *
82
			 * @since 3.0.0
83
			 */
84
			do_action( 'jetpack_module_configuration_head_' . $_GET['configure'] );
85
		}
86
	}
87
88
	// Render the page with a common top and bottom part, and page specific content
89
	function render() {
90
		// We're in an IDC: we need a decision made before we show the UI again.
91
		if ( self::$block_page_rendering_for_idc ) {
92
			return;
93
		}
94
95
		$this->page_render();
96
	}
97
98
	function admin_help() {
99
		$this->jetpack->admin_help();
100
	}
101
102
	function admin_page_load() {
103
		// This is big.  For the moment, just call the existing one.
104
		$this->jetpack->admin_page_load();
105
	}
106
107
	function admin_page_top() {
108
		include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' );
109
	}
110
111
	function admin_page_bottom() {
112
		include_once( JETPACK__PLUGIN_DIR . '_inc/footer.php' );
113
	}
114
115
	// Add page specific scripts and jetpack stats for all menu pages
116
	function admin_scripts() {
117
		$this->page_admin_scripts(); // Delegate to inheriting class
118
		add_action( 'admin_footer', array( $this->jetpack, 'do_stats' ) );
119
	}
120
121
	// Enqueue the Jetpack admin stylesheet
122
	function admin_styles() {
123
		$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
124
125
		wp_enqueue_style( 'jetpack-admin', plugins_url( "css/jetpack-admin{$min}.css", JETPACK__PLUGIN_FILE ), array( 'genericons' ), JETPACK__VERSION . '-20121016' );
126
		wp_style_add_data( 'jetpack-admin', 'rtl', 'replace' );
127
		wp_style_add_data( 'jetpack-admin', 'suffix', $min );
128
	}
129
130
	/**
131
	 * Checks if WordPress version is too old to have REST API.
132
	 *
133
	 * @since 4.3
134
	 *
135
	 * @return bool
136
	 */
137
	function is_wp_version_too_old() {
138
		global $wp_version;
139
		return ( ! function_exists( 'rest_api_init' ) || version_compare( $wp_version, '4.4-z', '<=' ) );
140
	}
141
142
	/**
143
	 * Checks if REST API is enabled.
144
	 *
145
	 * @since 4.4.2
146
	 *
147
	 * @return bool
148
	 */
149
	function is_rest_api_enabled() {
150
		return
151
			/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
152
			apply_filters( 'rest_enabled', true ) &&
153
			/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
154
			apply_filters( 'rest_jsonp_enabled', true ) &&
155
			/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
156
			apply_filters( 'rest_authentication_errors', true );
157
	}
158
159
	/**
160
	 * Checks the site plan and deactivates modules that were active but are no longer included in the plan.
161
	 *
162
	 * @since 4.4.0
163
	 *
164
	 * @param $page
165
	 *
166
	 * @return array
167
	 */
168
	function check_plan_deactivate_modules( $page ) {
169
		if (
170
			Jetpack::is_development_mode()
171
			|| ! in_array(
172
				$page->base,
173
				array(
174
					'toplevel_page_jetpack',
175
					'admin_page_jetpack_modules',
176
					'jetpack_page_vaultpress',
177
					'jetpack_page_stats',
178
					'jetpack_page_akismet-key-config'
179
				)
180
			)
181
		) {
182
			return false;
183
		}
184
185
		$current = Jetpack::get_active_plan();
186
187
		$to_deactivate = array();
188
		if ( isset( $current['product_slug'] ) ) {
189
			$active = Jetpack::get_active_modules();
190
			switch ( $current['product_slug'] ) {
191 View Code Duplication
				case 'jetpack_free':
192
					$to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics', 'wordads', 'search' );
193
					break;
194
				case 'jetpack_personal':
195 View Code Duplication
				case 'jetpack_personal_monthly':
196
					$to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics', 'wordads', 'search' );
197
					break;
198
				case 'jetpack_premium':
199
				case 'jetpack_premium_monthly':
200
					$to_deactivate = array( 'seo-tools', 'google-analytics', 'search' );
201
					break;
202
			}
203
			$to_deactivate = array_intersect( $active, $to_deactivate );
204
205
			$to_leave_enabled = array();
206
			foreach ( $to_deactivate as $feature ) {
207
				if ( Jetpack::active_plan_supports( $feature ) ) {
208
					$to_leave_enabled []= $feature;
209
				}
210
			}
211
			$to_deactivate = array_diff( $to_deactivate, $to_leave_enabled );
212
213
			if ( ! empty( $to_deactivate ) ) {
214
				Jetpack::update_active_modules( array_filter( array_diff( $active, $to_deactivate ) ) );
215
			}
216
		}
217
		return array(
218
			'current'    => $current,
219
			'deactivate' => $to_deactivate
220
		);
221
	}
222
}
223