| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | require_once __DIR__ . '/permalinks/upsell-permalinks.php'; |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | function wpcomsh_map_feature_cap( $caps, $cap ) { |
| 19 | |
| 20 | switch ( $cap ) { |
| 21 | case 'update_core': |
| 22 | case 'update_languages': |
| 23 | |
| 24 | if ( ! wpcom_site_has_feature( WPCOM_Features::MANAGE_PLUGINS ) ) { |
| 25 | $caps[] = 'do_not_allow'; |
| 26 | } |
| 27 | break; |
| 28 | |
| 29 | case 'update_themes': |
| 30 | case 'delete_themes': |
| 31 | if ( ! wpcom_site_has_feature( WPCOM_Features::INSTALL_THEMES ) ) { |
| 32 | $caps[] = 'do_not_allow'; |
| 33 | } |
| 34 | break; |
| 35 | |
| 36 | case 'install_themes': |
| 37 | |
| 38 | if ( wpcomsh_is_theme_install_request() ) { |
| 39 | break; |
| 40 | } |
| 41 | |
| 42 | if ( ! wpcom_site_has_feature( WPCOM_Features::INSTALL_THEMES ) ) { |
| 43 | $caps[] = 'do_not_allow'; |
| 44 | } |
| 45 | break; |
| 46 | |
| 47 | case 'edit_themes': |
| 48 | if ( ! wpcom_site_has_feature( WPCOM_Features::EDIT_THEMES ) ) { |
| 49 | $caps[] = 'do_not_allow'; |
| 50 | } |
| 51 | break; |
| 52 | |
| 53 | case 'upload_themes': |
| 54 | if ( ! wpcom_site_has_feature( WPCOM_Features::UPLOAD_THEMES ) ) { |
| 55 | $caps[] = 'do_not_allow'; |
| 56 | } |
| 57 | break; |
| 58 | |
| 59 | case 'activate_plugins': |
| 60 | case 'install_plugins': |
| 61 | case 'update_plugins': |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | if ( wpcomsh_is_plugin_list_request() && wpcom_site_has_feature( WPCOM_Features::LIST_INSTALLED_PLUGINS ) ) { |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | if ( |
| 74 | wpcom_site_has_feature( WPCOM_Features::INSTALL_WOO_ONBOARDING_PLUGINS ) |
| 75 | && ( |
| 76 | wpcomsh_is_woocommerce_onboarding_plugin_request() |
| 77 | || wpcomsh_is_woocommerce_connect_request() |
| 78 | ) |
| 79 | ) { |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | if ( ! wpcom_site_has_feature( WPCOM_Features::INSTALL_PLUGINS ) ) { |
| 84 | $caps[] = 'do_not_allow'; |
| 85 | } |
| 86 | break; |
| 87 | |
| 88 | case 'upload_plugins': |
| 89 | if ( ! wpcom_site_has_feature( WPCOM_Features::UPLOAD_PLUGINS ) ) { |
| 90 | $caps[] = 'do_not_allow'; |
| 91 | } |
| 92 | break; |
| 93 | |
| 94 | case 'edit_plugins': |
| 95 | if ( ! wpcom_site_has_feature( WPCOM_Features::EDIT_PLUGINS ) ) { |
| 96 | $caps[] = 'do_not_allow'; |
| 97 | } |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | return $caps; |
| 102 | } |
| 103 | add_filter( 'map_meta_cap', 'wpcomsh_map_feature_cap', 10, 2 ); |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | function wpcomsh_is_plugin_list_request() { |
| 111 | return wpcomsh_is_xmlrpc_request_matching( '@^/sites/([^/]+)/plugins$@' ); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | function wpcomsh_is_theme_install_request() { |
| 120 | return wpcomsh_is_xmlrpc_request_matching( '@/sites/(.+)/themes/(.+)/install@' ); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | function wpcomsh_is_woocommerce_onboarding_plugin_request() { |
| 130 | $wp_json_prefix = preg_quote( rest_get_url_prefix(), '@' ); |
| 131 | |
| 132 | |
| 133 | if ( wpcomsh_is_wp_rest_request_matching( '@^/' . $wp_json_prefix . '/wc-admin/payment-gateway-suggestions@' ) ) { |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | $editable_methods = wpcomsh_get_rest_methods_as_array( \WP_REST_Server::EDITABLE ); |
| 138 | |
| 139 | if ( ! wpcomsh_is_wp_rest_request_matching( '@^/' . $wp_json_prefix . '/wc-admin/plugins/install@', $editable_methods ) && ! wpcomsh_is_wp_rest_request_matching( '@^/' . $wp_json_prefix . '/wc-admin/plugins/activate@', $editable_methods ) ) { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | $wp_referer = wp_get_referer(); |
| 144 | |
| 145 | if ( empty( $wp_referer ) ) { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | if ( str_starts_with( $wp_referer, admin_url( 'admin.php?page=wc-admin&task=payments&id=' ) ) ) { |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | $permitted_admin_paths = array( |
| 157 | |
| 158 | 'admin.php?page=wc-admin&task=payments', |
| 159 | |
| 160 | 'admin.php?page=wc-admin&task=woocommerce-payments', |
| 161 | |
| 162 | 'admin.php?page=wc-admin&task=tax', |
| 163 | |
| 164 | 'admin.php?page=wc-settings&tab=checkout', |
| 165 | ); |
| 166 | |
| 167 | foreach ( $permitted_admin_paths as $permitted_admin_path ) { |
| 168 | if ( $wp_referer === admin_url( $permitted_admin_path ) ) { |
| 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | function wpcomsh_is_woocommerce_connect_request() { |
| 183 | $wp_json_prefix = preg_quote( rest_get_url_prefix(), '@' ); |
| 184 | |
| 185 | $editable_methods = wpcomsh_get_rest_methods_as_array( \WP_REST_Server::EDITABLE ); |
| 186 | |
| 187 | $permitted_connect_api_paths = array( |
| 188 | '/wc-admin/plugins/connect-jetpack' => \WP_REST_Server::READABLE, |
| 189 | '/wc-admin/plugins/connect-square' => $editable_methods, |
| 190 | '/wc-admin/plugins/connect-wcpay' => $editable_methods, |
| 191 | ); |
| 192 | |
| 193 | foreach ( $permitted_connect_api_paths as $permitted_connect_api_path => $supported_methods ) { |
| 194 | if ( wpcomsh_is_wp_rest_request_matching( '@^/' . $wp_json_prefix . $permitted_connect_api_path . '@', $supported_methods ) ) { |
| 195 | return true; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | function wpcomsh_get_rest_methods_as_array( $method ) { |
| 209 | return array_map( |
| 210 | 'trim', |
| 211 | explode( ',', $method ) |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | function wpcomsh_maybe_remove_permalinks_menu_item() { |
| 219 | if ( wpcom_site_has_feature( WPCOM_Features::OPTIONS_PERMALINK ) ) { |
| 220 | return; |
| 221 | } |
| 222 | remove_submenu_page( 'options-general.php', 'options-permalink.php' ); |
| 223 | |
| 224 | if ( function_exists( 'wpcomsh_permalinks_upsell_page_on_atomic_sites' ) ) { |
| 225 | wpcomsh_permalinks_upsell_page_on_atomic_sites(); |
| 226 | } |
| 227 | } |
| 228 | add_action( 'admin_menu', 'wpcomsh_maybe_remove_permalinks_menu_item' ); |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | function wpcomsh_maybe_disable_permalink_page() { |
| 235 | if ( wpcom_site_has_feature( WPCOM_Features::OPTIONS_PERMALINK ) ) { |
| 236 | return; |
| 237 | } |
| 238 | if ( ! ( defined( 'AT_PROXIED_REQUEST' ) && AT_PROXIED_REQUEST ) ) { |
| 239 | wp_die( |
| 240 | esc_html__( 'You do not have permission to access this page.', 'wpcomsh' ), |
| 241 | '', |
| 242 | array( |
| 243 | 'back_link' => true, |
| 244 | 'response' => 403, |
| 245 | ) |
| 246 | ); |
| 247 | } else { |
| 248 | add_action( |
| 249 | 'admin_notices', |
| 250 | function () { |
| 251 | echo '<div class="notice notice-warning"><p>' . esc_html__( 'Proxied only: You can see this because you are proxied. Do not use this if you don\'t know why you are here.', 'wpcomsh' ) . '</p></div>'; |
| 252 | } |
| 253 | ); |
| 254 | } |
| 255 | } |
| 256 | add_action( 'load-options-permalink.php', 'wpcomsh_maybe_disable_permalink_page' ); |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | function wpcomsh_plupload_file_restrictions( $options ) { |
| 266 | $mimes_map = get_allowed_mime_types(); |
| 267 | |
| 268 | $allowed_extensions = array(); |
| 269 | $allowed_mime_types = array(); |
| 270 | |
| 271 | foreach ( $mimes_map as $ext_pattern => $mime ) { |
| 272 | |
| 273 | |
| 274 | $extensions = explode( '|', $ext_pattern ); |
| 275 | foreach ( $extensions as $ext ) { |
| 276 | |
| 277 | if ( ! str_starts_with( $ext, 'x-' ) ) { |
| 278 | $allowed_extensions[] = $ext; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | $allowed_mime_types[] = $mime; |
| 283 | } |
| 284 | |
| 285 | if ( empty( $allowed_extensions ) ) { |
| 286 | return $options; |
| 287 | } |
| 288 | |
| 289 | if ( ! isset( $options['filters'] ) || ! is_array( $options['filters'] ) ) { |
| 290 | $options['filters'] = array(); |
| 291 | } |
| 292 | $options['filters']['mime_types'] = array( |
| 293 | array( 'extensions' => implode( ',', $allowed_extensions ) ), |
| 294 | ); |
| 295 | |
| 296 | |
| 297 | $options['allowed_mime_types'] = $allowed_mime_types; |
| 298 | |
| 299 | return $options; |
| 300 | } |
| 301 | |
| 302 | add_action( 'plupload_init', 'wpcomsh_plupload_file_restrictions' ); |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | function wpcomsh_maybe_restrict_mimetypes( $mimes ) { |
| 311 | $disallowed_mimes = array(); |
| 312 | if ( ! wpcom_site_has_feature( WPCOM_Features::UPGRADED_UPLOAD_FILETYPES ) ) { |
| 313 | |
| 314 | $upgraded_upload_filetypes = 'mp3 m4a wav ogg zip txt tiff bmp'; |
| 315 | $disallowed_mimes = array_merge( $disallowed_mimes, explode( ' ', $upgraded_upload_filetypes ) ); |
| 316 | } |
| 317 | |
| 318 | |
| 319 | |
| 320 | if ( ! wpcom_site_can_upload_videos() ) { |
| 321 | |
| 322 | $video_upload_filetypes = 'ogv mp4 m4v mov wmv avi mpg 3gp 3g2'; |
| 323 | $disallowed_mimes = array_merge( $disallowed_mimes, explode( ' ', $video_upload_filetypes ) ); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | if ( ! wpcom_site_has_feature( WPCOM_Features::VIDEOPRESS ) ) { |
| 328 | $disallowed_mimes[] = 'ttml'; |
| 329 | } |
| 330 | |
| 331 | foreach ( $disallowed_mimes as $disallowed_mime ) { |
| 332 | foreach ( $mimes as $ext_pattern => $mime ) { |
| 333 | if ( strpos( $ext_pattern, $disallowed_mime ) !== false ) { |
| 334 | unset( $mimes[ $ext_pattern ] ); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | return $mimes; |
| 340 | } |
| 341 | add_filter( 'upload_mimes', 'wpcomsh_maybe_restrict_mimetypes', PHP_INT_MAX ); |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | function wpcomsh_maybe_redirect_to_calypso_plugin_pages() { |
| 348 | $request_uri = wp_unslash( $_SERVER['REQUEST_URI'] ); |
| 349 | |
| 350 | if ( false === strpos( $request_uri, '/wp-admin/plugin' ) ) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | if ( wpcom_site_has_feature( WPCOM_Features::MANAGE_PLUGINS ) ) { |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | if ( ! class_exists( 'Automattic\Jetpack\Status' ) ) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | $site = ( new Automattic\Jetpack\Status() )->get_site_suffix(); |
| 363 | |
| 364 | |
| 365 | if ( 0 === strpos( $request_uri, '/wp-admin/plugin-install.php' ) ) { |
| 366 | wp_safe_redirect( 'https://wordpress.com/plugins/' . $site ); |
| 367 | exit( 0 ); |
| 368 | } |
| 369 | } |
| 370 | add_action( 'plugins_loaded', 'wpcomsh_maybe_redirect_to_calypso_plugin_pages' ); |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | function wpcomsh_gate_footer_credit_feature() { |
| 380 | return wpcom_site_has_feature( WPCOM_Features::NO_WPCOM_BRANDING ); |
| 381 | } |
| 382 | add_filter( 'wpcom_better_footer_credit_can_customize', 'wpcomsh_gate_footer_credit_feature' ); |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | function wpcomsh_gate_jetpack_forms_integrations() { |
| 390 | return wpcom_site_has_feature( WPCOM_Features::FORM_INTEGRATIONS ); |
| 391 | } |
| 392 | add_filter( 'jetpack_forms_is_integrations_enabled', 'wpcomsh_gate_jetpack_forms_integrations' ); |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | function wpcomsh_maybe_remove_jetpack_dashboard_menu_item() { |
| 398 | if ( wpcom_site_has_feature( WPCOM_Features::JETPACK_DASHBOARD ) ) { |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | remove_submenu_page( 'jetpack', 'jetpack#/dashboard' ); |
| 403 | } |
| 404 | add_action( 'admin_menu', 'wpcomsh_maybe_remove_jetpack_dashboard_menu_item', 1000 ); |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | function wpcomsh_remove_jetpack_manage_menu_item() { |
| 411 | if ( ! class_exists( 'Jetpack' ) || ! class_exists( 'Jetpack_Options' ) || get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) { |
| 412 | return; |
| 413 | } |
| 414 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 415 | remove_submenu_page( 'jetpack', 'https://jetpack.com/redirect/?source=cloud-manage-dashboard-wp-menu&site=' . $blog_id ); |
| 416 | } |
| 417 | |
| 418 | add_action( 'admin_menu', 'wpcomsh_remove_jetpack_manage_menu_item', 1001 ); |