Completed
Push — use-asset-tools-package-everyw... ( c90c8b...56f281 )
by
unknown
25:14 queued 17:28
created

class.jetpack-admin.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Automattic\Jetpack\Assets;
4
5
// Build the Jetpack admin menu as a whole
6
class Jetpack_Admin {
7
8
	/**
9
	 * @var Jetpack_Admin
10
	 **/
11
	private static $instance = null;
12
13
	/**
14
	 * @var Jetpack
15
	 **/
16
	private $jetpack;
17
18
	static function init() {
19
		if( isset( $_GET['page'] ) && $_GET['page'] === 'jetpack' ) {
20
			add_filter( 'nocache_headers', array( 'Jetpack_Admin', 'add_no_store_header' ), 100 );
21
		}
22
23
		if ( is_null( self::$instance ) ) {
24
			self::$instance = new Jetpack_Admin;
25
		}
26
		return self::$instance;
27
	}
28
29
	static function add_no_store_header( $headers ) {
30
		$headers['Cache-Control'] .= ', no-store';
31
		return $headers;
32
	}
33
34
	private function __construct() {
35
		$this->jetpack = Jetpack::init();
36
37
		jetpack_require_lib( 'admin-pages/class.jetpack-react-page' );
38
		$this->jetpack_react = new Jetpack_React_Page;
39
40
		jetpack_require_lib( 'admin-pages/class.jetpack-settings-page' );
41
		$this->fallback_page = new Jetpack_Settings_Page( Assets::get_instance() );
0 ignored issues
show
The property fallback_page 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...
42
43
		jetpack_require_lib( 'admin-pages/class-jetpack-about-page' );
44
		$this->jetpack_about = new Jetpack_About_Page;
45
46
		add_action( 'admin_menu',                    array( $this->jetpack_react, 'add_actions' ), 998 );
47
		add_action( 'jetpack_admin_menu',            array( $this->jetpack_react, 'jetpack_add_dashboard_sub_nav_item' ) );
48
		add_action( 'jetpack_admin_menu',            array( $this->jetpack_react, 'jetpack_add_settings_sub_nav_item' ) );
49
		add_action( 'jetpack_admin_menu',            array( $this, 'admin_menu_debugger' ) );
50
		add_action( 'jetpack_admin_menu',            array( $this->fallback_page, 'add_actions' ) );
51
		add_action( 'jetpack_admin_menu',            array( $this->jetpack_about, 'add_actions' ) );
52
53
		// Add redirect to current page for activation/deactivation of modules
54
		add_action( 'jetpack_pre_activate_module',   array( $this, 'fix_redirect' ), 10, 2 );
55
		add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ) );
56
57
		// Add module bulk actions handler
58
		add_action( 'jetpack_unrecognized_action',   array( $this, 'handle_unrecognized_action' ) );
59
	}
60
61 View Code Duplication
	static function sort_requires_connection_last( $module1, $module2 ) {
62
		if ( $module1['requires_connection'] == $module2['requires_connection'] ) {
63
			return 0;
64
		} elseif ( $module1['requires_connection'] ) {
65
			return 1;
66
		} elseif ( $module2['requires_connection'] ) {
67
			return -1;
68
		}
69
70
		return 0;
71
	}
72
73
	// Produce JS understandable objects of modules containing information for
74
	// presentation like description, name, configuration url, etc.
75
	function get_modules() {
76
		include_once( JETPACK__PLUGIN_DIR . 'modules/module-info.php' );
77
		$available_modules = Jetpack::get_available_modules();
78
		$active_modules    = Jetpack::get_active_modules();
79
		$modules           = array();
80
		$jetpack_active = Jetpack::is_active() || Jetpack::is_development_mode();
81
		$overrides = Jetpack_Modules_Overrides::instance();
82
		foreach ( $available_modules as $module ) {
83
			if ( $module_array = Jetpack::get_module( $module ) ) {
84
				/**
85
				 * Filters each module's short description.
86
				 *
87
				 * @since 3.0.0
88
				 *
89
				 * @param string $module_array['description'] Module description.
90
				 * @param string $module Module slug.
91
				 */
92
				$short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module );
93
				// Fix: correct multibyte strings truncate with checking for mbstring extension
94
				$short_desc_trunc = ( function_exists( 'mb_strlen' ) )
95
							? ( ( mb_strlen( $short_desc ) > 143 )
96
								? mb_substr( $short_desc, 0, 140 ) . '...'
97
								: $short_desc )
98
							: ( ( strlen( $short_desc ) > 143 )
99
								? substr( $short_desc, 0, 140 ) . '...'
100
								: $short_desc );
101
102
				$module_array['module']            = $module;
103
				$module_array['activated']         = ( $jetpack_active ? in_array( $module, $active_modules ) : false );
104
				$module_array['deactivate_nonce']  = wp_create_nonce( 'jetpack_deactivate-' . $module );
105
				$module_array['activate_nonce']    = wp_create_nonce( 'jetpack_activate-' . $module );
106
				$module_array['available']         = self::is_module_available( $module_array );
107
				$module_array['short_description'] = $short_desc_trunc;
108
				$module_array['configure_url']     = Jetpack::module_configuration_url( $module );
109
				$module_array['override']          = $overrides->get_module_override( $module );
110
111
				ob_start();
112
				/**
113
				 * Allow the display of a "Learn More" button.
114
				 * The dynamic part of the action, $module, is the module slug.
115
				 *
116
				 * @since 3.0.0
117
				 */
118
				do_action( 'jetpack_learn_more_button_' . $module );
119
				$module_array['learn_more_button'] = ob_get_clean();
120
121
				ob_start();
122
				/**
123
				 * Allow the display of information text when Jetpack is connected to WordPress.com.
124
				 * The dynamic part of the action, $module, is the module slug.
125
				 *
126
				 * @since 3.0.0
127
				 */
128
				do_action( 'jetpack_module_more_info_' . $module );
129
130
				/**
131
				* Filter the long description of a module.
132
	 			*
133
	 			* @since 3.5.0
134
	 			*
135
	 			* @param string ob_get_clean() The module long description.
136
				* @param string $module The module name.
137
	 			*/
138
				$module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module );
139
140
				ob_start();
141
				/**
142
				 * Filter the search terms for a module
143
				 *
144
				 * Search terms are typically added to the module headers, under "Additional Search Queries".
145
				 *
146
				 * Use syntax:
147
				 * function jetpack_$module_search_terms( $terms ) {
148
				 *  $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' );
149
				 *  return $terms;
150
				 * }
151
				 * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' );
152
				 *
153
				 * @since 3.5.0
154
				 *
155
				 * @param string The search terms (comma separated).
156
				 */
157
				echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] );
158
				$module_array['search_terms'] = ob_get_clean();
159
160
				$module_array['configurable'] = false;
161
				if (
162
					current_user_can( 'manage_options' ) &&
163
					/**
164
					 * Allow the display of a configuration link in the Jetpack Settings screen.
165
					 *
166
					 * @since 3.0.0
167
					 *
168
					 * @param string $module Module name.
169
					 * @param bool false Should the Configure module link be displayed? Default to false.
170
					 */
171
					apply_filters( 'jetpack_module_configurable_' . $module, false )
172
				) {
173
					$module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $module_array['configure_url'] ), __( 'Configure', 'jetpack' ) );
174
				}
175
176
				$modules[ $module ] = $module_array;
177
			}
178
		}
179
180
		uasort( $modules, array( $this->jetpack, 'sort_modules' ) );
181
182
		if ( ! Jetpack::is_active() ) {
183
			uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) );
184
		}
185
186
		return $modules;
187
	}
188
189
	static function is_module_available( $module ) {
190
		if ( ! is_array( $module ) || empty( $module ) )
191
			return false;
192
193
		/**
194
		 * We never want to show VaultPress as activatable through Jetpack.
195
		 */
196
		if ( 'vaultpress' === $module['module'] ) {
197
			return false;
198
		}
199
200
		if ( Jetpack::is_development_mode() ) {
201
			return ! ( $module['requires_connection'] );
202
		} else {
203
			if ( ! Jetpack::is_active() ) {
204
				return false;
205
			}
206
207
			return Jetpack_Plan::supports( $module['module'] );
208
		}
209
	}
210
211
	function handle_unrecognized_action( $action ) {
212
		switch( $action ) {
213
			case 'bulk-activate' :
214
				if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
215
					break;
216
				}
217
218
				$modules = (array) $_GET['modules'];
219
				$modules = array_map( 'sanitize_key', $modules );
220
				check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
221
				foreach( $modules as $module ) {
222
					Jetpack::log( 'activate', $module );
223
					Jetpack::activate_module( $module, false );
224
				}
225
				// The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
226
				wp_safe_redirect( wp_get_referer() );
227
				exit;
228 View Code Duplication
			case 'bulk-deactivate' :
229
				if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
230
					break;
231
				}
232
233
				$modules = (array) $_GET['modules'];
234
				$modules = array_map( 'sanitize_key', $modules );
235
				check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
236
				foreach ( $modules as $module ) {
237
					Jetpack::log( 'deactivate', $module );
238
					Jetpack::deactivate_module( $module );
239
					Jetpack::state( 'message', 'module_deactivated' );
240
				}
241
				Jetpack::state( 'module', $modules );
242
				wp_safe_redirect( wp_get_referer() );
243
				exit;
244
			default:
245
				return;
246
		}
247
	}
248
249
	function fix_redirect( $module, $redirect = true ) {
250
		if ( ! $redirect ) {
251
			return;
252
		}
253
		if ( wp_get_referer() ) {
254
			add_filter( 'wp_redirect', 'wp_get_referer' );
255
		}
256
	}
257
258
	function admin_menu_debugger() {
259
		jetpack_require_lib( 'debugger' );
260
		Jetpack_Debugger::disconnect_and_redirect();
261
		$debugger_hook = add_submenu_page(
262
			null,
263
			__( 'Debugging Center', 'jetpack' ),
264
			'',
265
			'manage_options',
266
			'jetpack-debugger',
267
			array( $this, 'wrap_debugger_page' )
268
		);
269
		add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
270
	}
271
272
	function wrap_debugger_page( ) {
273
		nocache_headers();
274
		if ( ! current_user_can( 'manage_options' ) ) {
275
			die( '-1' );
276
		}
277
		Jetpack_Admin_Page::wrap_ui( array( $this, 'debugger_page' ) );
278
	}
279
280
	function debugger_page() {
281
		jetpack_require_lib( 'debugger' );
282
		Jetpack_Debugger::jetpack_debug_display_handler();
283
	}
284
}
285
Jetpack_Admin::init();
286