Completed
Push — add/vr-shortcode ( 271979...022ee9 )
by
unknown
26:28 queued 20:24
created

class.jetpack-admin.php (6 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
		// Add hooks for admin menus
34
		add_action( 'admin_menu',                    array( $this->landing_page, 'add_actions' ), 998 );
35
		add_action( 'jetpack_admin_menu',            array( $this, 'admin_menu_debugger' ) );
36
		add_action( 'jetpack_admin_menu',            array( $this->settings_page, 'add_actions' ) );
37
38
		// Add redirect to current page for activation/deactivation of modules
39
		add_action( 'jetpack_pre_activate_module',   array( $this, 'fix_redirect' ), 10, 2 );
40
		add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ) );
41
42
		// Add module bulk actions handler
43
		add_action( 'jetpack_unrecognized_action',   array( $this, 'handle_unrecognized_action' ) );
44
	}
45
46 View Code Duplication
	static function sort_requires_connection_last( $module1, $module2 ) {
47
		if ( $module1['requires_connection'] == $module2['requires_connection'] ) {
48
			return 0;
49
		} elseif ( $module1['requires_connection'] ) {
50
			return 1;
51
		} elseif ( $module2['requires_connection'] ) {
52
			return -1;
53
		}
54
55
		return 0;
56
	}
57
58
	// Produce JS understandable objects of modules containing information for
59
	// presentation like description, name, configuration url, etc.
60
	function get_modules() {
61
		include_once( JETPACK__PLUGIN_DIR . 'modules/module-info.php' );
62
		$available_modules = $this->jetpack->get_available_modules();
63
		$active_modules    = $this->jetpack->get_active_modules();
64
		$modules           = array();
65
		$jetpack_active = Jetpack::is_active() || Jetpack::is_development_mode();
66
		foreach ( $available_modules as $module ) {
67
			if ( $module_array = $this->jetpack->get_module( $module ) ) {
68
				$short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module );
69
				// Fix: correct multibyte strings truncate with checking for mbstring extension
70
				$short_desc_trunc = ( function_exists( 'mb_strlen' ) )
71
							? ( ( mb_strlen( $short_desc ) > 143 )
72
								? mb_substr( $short_desc, 0, 140 ) . '...'
73
								: $short_desc )
74
							: ( ( strlen( $short_desc ) > 143 )
75
								? substr( $short_desc, 0, 140 ) . '...'
76
								: $short_desc );
77
78
				$module_array['module']            = $module;
79
				$module_array['activated']         = ( $jetpack_active ? in_array( $module, $active_modules ) : false );
80
				$module_array['deactivate_nonce']  = wp_create_nonce( 'jetpack_deactivate-' . $module );
81
				$module_array['activate_nonce']    = wp_create_nonce( 'jetpack_activate-' . $module );
82
				$module_array['available']         = self::is_module_available( $module_array );
83
				$module_array['short_description'] = $short_desc_trunc;
84
				$module_array['configure_url']     = Jetpack::module_configuration_url( $module );
85
86
				ob_start();
87
				do_action( 'jetpack_learn_more_button_' . $module );
88
				$module_array['learn_more_button'] = ob_get_clean();
89
90
				ob_start();
91 View Code Duplication
				if ( Jetpack::is_active() && has_action( 'jetpack_module_more_info_connected_' . $module ) ) {
92
					do_action( 'jetpack_module_more_info_connected_' . $module );
93
				} else {
94
					do_action( 'jetpack_module_more_info_' . $module );
95
				}
96
				
97
				/**
98
				* Filter the long description of a module.
99
	 			*
100
	 			* @since 3.5.0
101
	 			*
102
	 			* @param string ob_get_clean() The module long description.
103
				* @param string $module The module name.
104
	 			*/
105
				$module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module );
106
107
				ob_start();
108
				/**
109
				 * Filter the search terms for a module
110
				 *
111
				 * Search terms are be typically added to a module in module-info.php.
112
				 *
113
				 * Use syntax:
114
				 * function jetpack_$module_search_terms( $terms ) {
115
				 *  $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' );
116
				 *  return $terms;
117
				 * }
118
				 * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' );
119
				 *
120
				 * @since 3.5.0
121
				 * @param string The search terms (comma separated)
122
				 */
123
				echo apply_filters( 'jetpack_search_terms_' . $module, '' );
124
				$module_array['search_terms'] = ob_get_clean();
125
126
				$module_array['configurable'] = false;
127
				if ( current_user_can( 'manage_options' ) && apply_filters( 'jetpack_module_configurable_' . $module, false ) ) {
128
					$module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( Jetpack::module_configuration_url( $module ) ), __( 'Configure', 'jetpack' ) );
129
				}
130
131
				$modules[ $module ] = $module_array;
132
			}
133
		}
134
135
		uasort( $modules, array( $this->jetpack, 'sort_modules' ) );
136
137
		if ( ! Jetpack::is_active() ) {
138
			uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) );
139
		}
140
141
		return $modules;
142
	}
143
144
	static function is_module_available( $module ) {
145
		if ( ! is_array( $module ) || empty( $module ) )
146
			return false;
147
148
		/**
149
		 * We never want to show VaultPress as activate-able through Jetpack.
150
		 */
151
		if ( 'vaultpress' === $module['module'] ) {
152
			return false;
153
		}
154
155
		if ( Jetpack::is_development_mode() ) {
156
			return ! ( $module['requires_connection'] && ! Jetpack::is_active() );
157
		} else {
158
			return Jetpack::is_active();
159
		}
160
	}
161
162
	function handle_unrecognized_action( $action ) {
163
		switch( $action ) {
164
			case 'bulk-activate' :
165
				if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
166
					break;
167
				}
168
169
				$modules = (array) $_GET['modules'];
170
				$modules = array_map( 'sanitize_key', $modules );
171
				check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
172
				foreach( $modules as $module ) {
173
					Jetpack::log( 'activate', $module );
174
					Jetpack::activate_module( $module, false );
175
				}
176
				// The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
177
				wp_safe_redirect( wp_get_referer() );
178
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle_unrecognized_action() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
179 View Code Duplication
			case 'bulk-deactivate' :
180
				if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
181
					break;
182
				}
183
184
				$modules = (array) $_GET['modules'];
185
				$modules = array_map( 'sanitize_key', $modules );
186
				check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
187
				foreach ( $modules as $module ) {
188
					Jetpack::log( 'deactivate', $module );
189
					Jetpack::deactivate_module( $module );
190
					Jetpack::state( 'message', 'module_deactivated' );
191
				}
192
				Jetpack::state( 'module', $modules );
0 ignored issues
show
$modules is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193
				wp_safe_redirect( wp_get_referer() );
194
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle_unrecognized_action() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
195
			default:
196
				return;
197
		}
198
	}
199
200
	function fix_redirect( $module, $redirect = true ) {
201
		if ( ! $redirect ) {
202
			return;
203
		}
204
		if ( wp_get_referer() ) {
205
			add_filter( 'wp_redirect', 'wp_get_referer' );
206
		}
207
	}
208
209
	function admin_menu_debugger() {
210
		$debugger_hook = add_submenu_page( null, __( 'Jetpack Debugging Center', 'jetpack' ), '', 'manage_options', 'jetpack-debugger', array( $this, 'debugger_page' ) );
211
		add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
212
	}
213
214
	function debugger_page() {
215
		nocache_headers();
216
		if ( ! current_user_can( 'manage_options' ) ) {
217
			die( '-1' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method debugger_page() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
218
		}
219
		Jetpack_Debugger::jetpack_debug_display_handler();
220
	}
221
}
222
Jetpack_Admin::init();
223