Completed
Push — add/sync-action ( 9c2804...6a3641 )
by
unknown
192:45 queued 183:25
created

class.jetpack-admin.php (5 issues)

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
4
// Build the Jetpack admin menu as a whole
5
class Jetpack_Admin {
6
7
	/**
8
	 * @var Jetpack_Admin
9
	 **/
10
	private static $instance = null;
11
12
	/**
13
	 * @var Jetpack
14
	 **/
15
	private $jetpack;
16
17
	static function init() {
18
		if ( is_null( self::$instance ) ) {
19
			self::$instance = new Jetpack_Admin;
20
		}
21
		return self::$instance;
22
	}
23
24
	private function __construct() {
25
		$this->jetpack = Jetpack::init();
26
27
		jetpack_require_lib( 'admin-pages/class.jetpack-landing-page' );
28
		$this->landing_page = new Jetpack_Landing_Page;
0 ignored issues
show
The property landing_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...
29
30
		jetpack_require_lib( 'admin-pages/class.jetpack-settings-page' );
31
		$this->settings_page = new Jetpack_Settings_Page;
0 ignored issues
show
The property settings_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...
32
33
		jetpack_require_lib( 'admin-pages/class.jetpack-my-jetpack-page' );
34
		$this->my_jetpack_page = new Jetpack_My_Jetpack_Page;
0 ignored issues
show
The property my_jetpack_page does not seem to exist. Did you mean jetpack?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
35
36
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-dashboard.php';
37
		$this->sync_dashboard = new Jetpack_Sync_Dashboard;
38
		$this->sync_dashboard->init();
39
		
40
		if ( isset( $_POST['jetpack-set-master-user'] ) ) {
41
			add_action( 'init', array( $this->my_jetpack_page, 'jetpack_my_jetpack_change_user' ) );
0 ignored issues
show
The property my_jetpack_page does not seem to exist. Did you mean jetpack?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
42
		}
43
		
44
		// Add hooks for admin menus
45
		add_action( 'admin_menu',                    array( $this->landing_page, 'add_actions' ), 998 );
46
		add_action( 'jetpack_admin_menu',            array( $this, 'admin_menu_debugger' ) );
47
		add_action( 'jetpack_admin_menu',            array( $this->settings_page, 'add_actions' ) );
48
		add_action( 'jetpack_admin_menu',            array( $this->my_jetpack_page, 'add_actions' ) );
0 ignored issues
show
The property my_jetpack_page does not seem to exist. Did you mean jetpack?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
49
		add_action( 'jetpack_admin_menu',            array( $this->sync_dashboard, 'add_actions' ) );
50
51
		// Add redirect to current page for activation/deactivation of modules
52
		add_action( 'jetpack_pre_activate_module',   array( $this, 'fix_redirect' ), 10, 2 );
53
		add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ) );
54
55
		// Add module bulk actions handler
56
		add_action( 'jetpack_unrecognized_action',   array( $this, 'handle_unrecognized_action' ) );
57
	}
58
59 View Code Duplication
	static function sort_requires_connection_last( $module1, $module2 ) {
60
		if ( $module1['requires_connection'] == $module2['requires_connection'] ) {
61
			return 0;
62
		} elseif ( $module1['requires_connection'] ) {
63
			return 1;
64
		} elseif ( $module2['requires_connection'] ) {
65
			return -1;
66
		}
67
68
		return 0;
69
	}
70
71
	// Produce JS understandable objects of modules containing information for
72
	// presentation like description, name, configuration url, etc.
73
	function get_modules() {
74
		include_once( JETPACK__PLUGIN_DIR . 'modules/module-info.php' );
75
		$available_modules = $this->jetpack->get_available_modules();
76
		$active_modules    = $this->jetpack->get_active_modules();
77
		$modules           = array();
78
		$jetpack_active = Jetpack::is_active() || Jetpack::is_development_mode();
79
		foreach ( $available_modules as $module ) {
80
			if ( $module_array = $this->jetpack->get_module( $module ) ) {
81
				/**
82
				 * Filters each module's short description.
83
				 *
84
				 * @since 3.0.0
85
				 *
86
				 * @param string $module_array['description'] Module description.
87
				 * @param string $module Module slug.
88
				 */
89
				$short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module );
90
				// Fix: correct multibyte strings truncate with checking for mbstring extension
91
				$short_desc_trunc = ( function_exists( 'mb_strlen' ) )
92
							? ( ( mb_strlen( $short_desc ) > 143 )
93
								? mb_substr( $short_desc, 0, 140 ) . '...'
94
								: $short_desc )
95
							: ( ( strlen( $short_desc ) > 143 )
96
								? substr( $short_desc, 0, 140 ) . '...'
97
								: $short_desc );
98
99
				$module_array['module']            = $module;
100
				$module_array['activated']         = ( $jetpack_active ? in_array( $module, $active_modules ) : false );
101
				$module_array['deactivate_nonce']  = wp_create_nonce( 'jetpack_deactivate-' . $module );
102
				$module_array['activate_nonce']    = wp_create_nonce( 'jetpack_activate-' . $module );
103
				$module_array['available']         = self::is_module_available( $module_array );
104
				$module_array['short_description'] = $short_desc_trunc;
105
				$module_array['configure_url']     = Jetpack::module_configuration_url( $module );
106
107
				ob_start();
108
				/**
109
				 * Allow the display of a "Learn More" button.
110
				 * The dynamic part of the action, $module, is the module slug.
111
				 *
112
				 * @since 3.0.0
113
				 */
114
				do_action( 'jetpack_learn_more_button_' . $module );
115
				$module_array['learn_more_button'] = ob_get_clean();
116
117
				ob_start();
118
				/**
119
				 * Allow the display of information text when Jetpack is connected to WordPress.com.
120
				 * The dynamic part of the action, $module, is the module slug.
121
				 *
122
				 * @since 3.0.0
123
				 */
124
				do_action( 'jetpack_module_more_info_' . $module );
125
126
				/**
127
				* Filter the long description of a module.
128
	 			*
129
	 			* @since 3.5.0
130
	 			*
131
	 			* @param string ob_get_clean() The module long description.
132
				* @param string $module The module name.
133
	 			*/
134
				$module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module );
135
136
				ob_start();
137
				/**
138
				 * Filter the search terms for a module
139
				 *
140
				 * Search terms are typically added to the module headers, under "Additional Search Queries".
141
				 *
142
				 * Use syntax:
143
				 * function jetpack_$module_search_terms( $terms ) {
144
				 *  $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' );
145
				 *  return $terms;
146
				 * }
147
				 * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' );
148
				 *
149
				 * @since 3.5.0
150
				 *
151
				 * @param string The search terms (comma separated).
152
				 */
153
				echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] );
154
				$module_array['search_terms'] = ob_get_clean();
155
156
				$module_array['configurable'] = false;
157
				if (
158
					current_user_can( 'manage_options' ) &&
159
					/**
160
					 * Allow the display of a configuration link in the Jetpack Settings screen.
161
					 *
162
					 * @since 3.0.0
163
					 *
164
					 * @param string $module Module name.
165
					 * @param bool false Should the Configure module link be displayed? Default to false.
166
					 */
167
					apply_filters( 'jetpack_module_configurable_' . $module, false )
168
				) {
169
					$module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( Jetpack::module_configuration_url( $module ) ), __( 'Configure', 'jetpack' ) );
170
				}
171
172
				$modules[ $module ] = $module_array;
173
			}
174
		}
175
176
		uasort( $modules, array( $this->jetpack, 'sort_modules' ) );
177
178
		if ( ! Jetpack::is_active() ) {
179
			uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) );
180
		}
181
182
		return $modules;
183
	}
184
185
	static function is_module_available( $module ) {
186
		if ( ! is_array( $module ) || empty( $module ) )
187
			return false;
188
189
		/**
190
		 * We never want to show VaultPress as activatable through Jetpack.
191
		 */
192
		if ( 'vaultpress' === $module['module'] ) {
193
			return false;
194
		}
195
196
		if ( Jetpack::is_development_mode() ) {
197
			return ! ( $module['requires_connection'] );
198
		} else {
199
			return Jetpack::is_active();
200
		}
201
	}
202
203
	function handle_unrecognized_action( $action ) {
204
		switch( $action ) {
205
			case 'bulk-activate' :
206
				if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
207
					break;
208
				}
209
210
				$modules = (array) $_GET['modules'];
211
				$modules = array_map( 'sanitize_key', $modules );
212
				check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
213
				foreach( $modules as $module ) {
214
					Jetpack::log( 'activate', $module );
215
					Jetpack::activate_module( $module, false );
216
				}
217
				// The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
218
				wp_safe_redirect( wp_get_referer() );
219
				exit;
220 View Code Duplication
			case 'bulk-deactivate' :
221
				if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
222
					break;
223
				}
224
225
				$modules = (array) $_GET['modules'];
226
				$modules = array_map( 'sanitize_key', $modules );
227
				check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
228
				foreach ( $modules as $module ) {
229
					Jetpack::log( 'deactivate', $module );
230
					Jetpack::deactivate_module( $module );
231
					Jetpack::state( 'message', 'module_deactivated' );
232
				}
233
				Jetpack::state( 'module', $modules );
234
				wp_safe_redirect( wp_get_referer() );
235
				exit;
236
			default:
237
				return;
238
		}
239
	}
240
241
	function fix_redirect( $module, $redirect = true ) {
242
		if ( ! $redirect ) {
243
			return;
244
		}
245
		if ( wp_get_referer() ) {
246
			add_filter( 'wp_redirect', 'wp_get_referer' );
247
		}
248
	}
249
250
	function admin_menu_debugger() {
251
		$debugger_hook = add_submenu_page( null, __( 'Jetpack Debugging Center', 'jetpack' ), '', 'manage_options', 'jetpack-debugger', array( $this, 'debugger_page' ) );
252
		add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
253
	}
254
255
	function debugger_page() {
256
		nocache_headers();
257
		if ( ! current_user_can( 'manage_options' ) ) {
258
			die( '-1' );
259
		}
260
		Jetpack_Debugger::jetpack_debug_display_handler();
261
	}
262
}
263
Jetpack_Admin::init();
264