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