Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
CRAP | n/a |
0 / 0 |
|
| jetpack_site_switcher_enqueue_scripts | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Site Switcher for Command Palette |
| 4 | * Enqueues the site switcher UI and loads the compact sites list endpoint |
| 5 | * |
| 6 | * @package automattic/jetpack |
| 7 | */ |
| 8 | |
| 9 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit( 0 ); |
| 13 | } |
| 14 | |
| 15 | // Load the Jetpack endpoint (not needed on WordPress.com simple sites) |
| 16 | if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
| 17 | require_once __DIR__ . '/site-switcher-endpoint.php'; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Enqueue site switcher scripts in all wp-admin pages. |
| 22 | */ |
| 23 | function jetpack_site_switcher_enqueue_scripts() { |
| 24 | // Only enqueue for users connected to WordPress.com (unless on WPCOM where all users are connected) |
| 25 | if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
| 26 | $connection_manager = new Connection_Manager(); |
| 27 | if ( ! $connection_manager->is_user_connected() ) { |
| 28 | return; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Load asset file for dependencies and version |
| 33 | $asset_file = JETPACK__PLUGIN_DIR . '_inc/build/site-switcher.min.asset.php'; |
| 34 | $asset = file_exists( $asset_file ) ? require $asset_file : array( |
| 35 | 'dependencies' => array( 'wp-api-fetch', 'wp-commands', 'wp-element', 'wp-i18n', 'wp-icons' ), |
| 36 | 'version' => JETPACK__VERSION, |
| 37 | ); |
| 38 | |
| 39 | // Register and enqueue site switcher JavaScript |
| 40 | wp_enqueue_script( |
| 41 | 'jetpack-site-switcher', |
| 42 | plugins_url( '_inc/build/site-switcher.min.js', JETPACK__PLUGIN_FILE ), |
| 43 | $asset['dependencies'], |
| 44 | $asset['version'], |
| 45 | true |
| 46 | ); |
| 47 | |
| 48 | // Pass configuration to JavaScript |
| 49 | $api_path = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
| 50 | ? '/rest/v1.1/me/sites/compact' |
| 51 | : '/jetpack/v4/sites/compact'; |
| 52 | |
| 53 | wp_add_inline_script( |
| 54 | 'jetpack-site-switcher', |
| 55 | sprintf( |
| 56 | 'window.jetpackSiteSwitcherConfig = %s;', |
| 57 | wp_json_encode( |
| 58 | array( |
| 59 | 'userId' => get_current_user_id(), |
| 60 | 'apiPath' => $api_path, |
| 61 | ), |
| 62 | JSON_HEX_TAG | JSON_HEX_AMP | JSON_UNESCAPED_SLASHES |
| 63 | ) |
| 64 | ), |
| 65 | 'before' |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | add_action( 'admin_enqueue_scripts', 'jetpack_site_switcher_enqueue_scripts' ); |