PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 3.1.5
Jetpack – WP Security, Backup, Speed, & Growth v3.1.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 in Jetpack – WP Security, Backup, Speed, & Growth 3.1.5, at class.jetpack-admin.php

178 lines 5.9 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 jetpack_require_lib( 'admin-pages/class.jetpack-settings-page' );
29
30 // Initialize objects building landing and settings pages
31 $this->landing_page = new Jetpack_Landing_Page;
32 $this->settings_page = new Jetpack_Settings_Page;
33
34 // Add hooks for admin menus
35 add_action( 'admin_menu', array( $this->landing_page, 'add_actions' ), 998 );
36 add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) );
37 add_action( 'jetpack_admin_menu', array( $this->settings_page, 'add_actions' ) );
38
39 // Add redirect to current page for activation/deactivation of modules
40 add_action( 'jetpack_pre_activate_module', array( $this, 'fix_redirect' ) );
41 add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ) );
42
43 // Add module bulk actions handler
44 add_action( 'jetpack_unrecognized_action', array( $this, 'handle_unrecognized_action' ) );
45 }
46
47 static function sort_requires_connection_last( $module1, $module2 ) {
48 if ( $module1['requires_connection'] == $module2['requires_connection'] )
49 return 0;
50 if ( $module1['requires_connection'] )
51 return 1;
52 if ( $module2['requires_connection'] )
53 return -1;
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
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 $short_desc_trunc = ( strlen( $short_desc ) > 143 ) ? substr( $short_desc, 0, 140 ) . '...' : $short_desc;
70
71 $module_array['module'] = $module;
72 $module_array['activated'] = in_array( $module, $active_modules );
73 $module_array['deactivate_nonce'] = wp_create_nonce( 'jetpack_deactivate-' . $module );
74 $module_array['activate_nonce'] = wp_create_nonce( 'jetpack_activate-' . $module );
75 $module_array['available'] = self::is_module_available( $module_array );
76 $module_array['short_description'] = $short_desc_trunc;
77 $module_array['configure_url'] = Jetpack::module_configuration_url( $module );
78
79 ob_start();
80 do_action( 'jetpack_learn_more_button_' . $module );
81 $module_array['learn_more_button'] = ob_get_clean();
82
83 ob_start();
84 if ( Jetpack::is_active() && has_action( 'jetpack_module_more_info_connected_' . $module ) ) {
85 do_action( 'jetpack_module_more_info_connected_' . $module );
86 } else {
87 do_action( 'jetpack_module_more_info_' . $module );
88 }
89 $module_array['long_description'] = ob_get_clean();
90
91 $module_array['configurable'] = false;
92 if ( current_user_can( 'manage_options' ) && apply_filters( 'jetpack_module_configurable_' . $module, false ) ) {
93 $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( Jetpack::module_configuration_url( $module ) ), __( 'Configure', 'jetpack' ) );
94 }
95
96 $modules[ $module ] = $module_array;
97 }
98 }
99
100 uasort( $modules, array( $this->jetpack, 'sort_modules' ) );
101
102 if ( ! Jetpack::is_active() ) {
103 uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) );
104 }
105
106 return $modules;
107 }
108
109 static function is_module_available( $module ) {
110 if ( ! is_array( $module ) || empty( $module ) )
111 return false;
112
113 if ( Jetpack::is_development_mode() ) {
114 return ! ( $module['requires_connection'] && ! Jetpack::is_active() );
115 } else {
116 return Jetpack::is_active();
117 }
118 }
119
120 function handle_unrecognized_action( $action ) {
121 switch( $action ) {
122 case 'bulk-activate' :
123 if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
124 break;
125 }
126
127 $modules = (array) $_GET['modules'];
128 $modules = array_map( 'sanitize_key', $modules );
129 check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
130 foreach( $modules as $module ) {
131 Jetpack::log( 'activate', $module );
132 Jetpack::activate_module( $module, false );
133 }
134 // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
135 wp_safe_redirect( wp_get_referer() );
136 exit;
137 case 'bulk-deactivate' :
138 if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
139 break;
140 }
141
142 $modules = (array) $_GET['modules'];
143 $modules = array_map( 'sanitize_key', $modules );
144 check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
145 foreach ( $modules as $module ) {
146 Jetpack::log( 'deactivate', $module );
147 Jetpack::deactivate_module( $module );
148 Jetpack::state( 'message', 'module_deactivated' );
149 }
150 Jetpack::state( 'module', $modules );
151 wp_safe_redirect( wp_get_referer() );
152 exit;
153 default:
154 return;
155 }
156 }
157
158 function fix_redirect() {
159 if ( wp_get_referer() ) {
160 add_filter( 'wp_redirect', 'wp_get_referer' );
161 }
162 }
163
164 function admin_menu_debugger() {
165 $debugger_hook = add_submenu_page( null, __( 'Jetpack Debugging Center', 'jetpack' ), '', 'manage_options', 'jetpack-debugger', array( $this, 'debugger_page' ) );
166 add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
167 }
168
169 function debugger_page() {
170 nocache_headers();
171 if ( ! current_user_can( 'manage_options' ) ) {
172 die( '-1' );
173 }
174 Jetpack_Debugger::jetpack_debug_display_handler();
175 }
176 }
177 Jetpack_Admin::init();
178