← All changes
|
jetpack_vendor/automattic/jetpack-blaze/src/class-dashboard-config-data.php
+88
-18
12.3.2
→
16.3
View file →
| @@ -6,9 +6,12 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Blaze; |
| 9 | 9 | |
| 10 | +use Automattic\Jetpack\Connection\Manager; | |
| 11 | +use Automattic\Jetpack\Constants; | |
| 10 | 12 | use Automattic\Jetpack\Current_Plan; |
| 13 | +use Automattic\Jetpack\Status\Host; | |
| 11 | 14 | use Jetpack_Options; |
| 12 | 15 | |
| 13 | 16 | /** |
| 14 | 17 | * Class Dashboard_Config_Data |
| @@ -13,9 +16,35 @@ | ||
| 13 | 16 | /** |
| 14 | 17 | * Class Dashboard_Config_Data |
| 15 | 18 | */ |
| 16 | 19 | class Dashboard_Config_Data { |
| 20 | + | |
| 17 | 21 | /** |
| 22 | + * Blaze dashboard admin page. Default is admin.php. | |
| 23 | + * | |
| 24 | + * @var string | |
| 25 | + */ | |
| 26 | + private $admin_page; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Blaze dashboard menu slug. Default is 'advertising'. | |
| 30 | + * | |
| 31 | + * @var string | |
| 32 | + */ | |
| 33 | + private $menu_slug; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Dashboard config constructor. | |
| 37 | + * | |
| 38 | + * @param string $admin_page Dashboard admin page. Default is admin.php. | |
| 39 | + * @param string $menu_slug Dashboard menu slug. Default is 'advertising'. | |
| 40 | + */ | |
| 41 | + public function __construct( $admin_page = 'admin.php', $menu_slug = 'advertising' ) { | |
| 42 | + $this->admin_page = $admin_page; | |
| 43 | + $this->menu_slug = $menu_slug; | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 18 | 47 | * Set configData to window.configData. |
| 19 | 48 | * |
| 20 | 49 | * @param array $config_data The config data. |
| 21 | 50 | */ |
| @@ -20,9 +49,10 @@ | ||
| 20 | 49 | * @param array $config_data The config data. |
| 21 | 50 | */ |
| 22 | 51 | public function get_js_config_data( $config_data = null ) { |
| 23 | 52 | return 'window.configData = ' . wp_json_encode( |
| 24 | - $config_data === null ? $this->get_data() : $config_data | |
| 53 | + $config_data === null ? $this->get_data() : $config_data, | |
| 54 | + JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP | |
| 25 | 55 | ) . ';'; |
| 26 | 56 | } |
| 27 | 57 | |
| 28 | 58 | /** |
| @@ -29,10 +59,14 @@ | ||
| 29 | 59 | * Return the config for the app. |
| 30 | 60 | */ |
| 31 | 61 | public function get_data() { |
| 32 | 62 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 63 | + $host = new Host(); | |
| 33 | 64 | $empty_object = json_decode( '{}' ); |
| 34 | - return array( | |
| 65 | + | |
| 66 | + $user = $this->get_connected_user_identity(); | |
| 67 | + | |
| 68 | + $data = array( | |
| 35 | 69 | 'admin_page_base' => $this->get_admin_path(), |
| 36 | 70 | 'api_root' => esc_url_raw( rest_url() ), |
| 37 | 71 | 'blog_id' => $blog_id, |
| 38 | 72 | 'enable_all_sections' => false, |
| @@ -45,17 +79,15 @@ | ||
| 45 | 79 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 46 | 80 | 'site_name' => \get_bloginfo( 'name' ), |
| 47 | 81 | 'sections' => array(), |
| 48 | 82 | // Features are inlined in Calypso Blaze app (wp-calypso/apps/blaze-dashboard) |
| 49 | - 'features' => array(), | |
| 50 | - 'intial_state' => array( | |
| 83 | + 'features' => array( | |
| 84 | + 'is_running_in_jetpack_site' => ! $host->is_wpcom_simple(), | |
| 85 | + ), | |
| 86 | + 'initial_state' => array( | |
| 51 | 87 | 'currentUser' => array( |
| 52 | - 'id' => 1000, | |
| 53 | - 'user' => array( | |
| 54 | - 'ID' => 1000, | |
| 55 | - 'username' => 'no-user', | |
| 56 | - 'localeSlug' => $this->get_site_locale(), | |
| 57 | - ), | |
| 88 | + 'id' => $user['ID'], | |
| 89 | + 'user' => $user, | |
| 58 | 90 | 'capabilities' => array( |
| 59 | 91 | "$blog_id" => $this->get_current_user_capabilities(), |
| 60 | 92 | ), |
| 61 | 93 | ), |
| @@ -63,16 +95,19 @@ | ||
| 63 | 95 | 'items' => array( |
| 64 | 96 | "$blog_id" => array( |
| 65 | 97 | 'ID' => $blog_id, |
| 66 | 98 | 'URL' => site_url(), |
| 67 | - 'jetpack' => true, | |
| 99 | + 'jetpack' => ! $host->is_wpcom_simple(), | |
| 68 | 100 | 'visible' => true, |
| 69 | 101 | 'capabilities' => $empty_object, |
| 70 | 102 | 'products' => array(), |
| 71 | 103 | 'plan' => $empty_object, // we need this empty object, otherwise the front end would crash on insight page. |
| 72 | 104 | 'options' => array( |
| 73 | - 'admin_url' => admin_url(), | |
| 74 | - 'gmt_offset' => $this->get_gmt_offset(), | |
| 105 | + 'admin_url' => admin_url(), | |
| 106 | + 'gmt_offset' => $this->get_gmt_offset(), | |
| 107 | + 'is_wpcom_atomic' => $host->is_woa_site(), | |
| 108 | + 'is_wpcom_simple' => $host->is_wpcom_simple(), | |
| 109 | + 'jetpack_version' => Constants::get_constant( 'JETPACK__VERSION' ), | |
| 75 | 110 | ), |
| 76 | 111 | ), |
| 77 | 112 | ), |
| 78 | 113 | 'features' => array( "$blog_id" => array( 'data' => $this->get_plan_features() ) ), |
| @@ -78,11 +113,45 @@ | ||
| 78 | 113 | 'features' => array( "$blog_id" => array( 'data' => $this->get_plan_features() ) ), |
| 79 | 114 | ), |
| 80 | 115 | ), |
| 81 | 116 | ); |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Filter to allow modification of the Blaze dashboard config data. | |
| 120 | + * | |
| 121 | + * @param bool $data Blaze dashboard config data. | |
| 122 | + * | |
| 123 | + * @since 0.21.0 | |
| 124 | + */ | |
| 125 | + return apply_filters( 'jetpack_blaze_dashboard_config_data', $data ); | |
| 82 | 126 | } |
| 83 | 127 | |
| 84 | 128 | /** |
| 129 | + * Gets the WordPress.com user's identity, if connected. | |
| 130 | + * | |
| 131 | + * @return array|bool | |
| 132 | + */ | |
| 133 | + protected function get_connected_user_identity() { | |
| 134 | + $user_data = ( new Manager() )->get_connected_user_data(); | |
| 135 | + if ( ! $user_data ) { | |
| 136 | + return array( | |
| 137 | + 'ID' => 1000, | |
| 138 | + 'username' => 'no-user', | |
| 139 | + 'localeSlug' => $this->get_locale(), | |
| 140 | + 'site_count' => 1, | |
| 141 | + ); | |
| 142 | + } | |
| 143 | + | |
| 144 | + return array( | |
| 145 | + 'ID' => $user_data['ID'], | |
| 146 | + 'username' => $user_data['login'], | |
| 147 | + 'email' => $user_data['email'], | |
| 148 | + 'localeSlug' => $this->get_locale(), | |
| 149 | + 'site_count' => 1, | |
| 150 | + ); | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 85 | 154 | * Get the current site GMT Offset. |
| 86 | 155 | * |
| 87 | 156 | * @return float The current site GMT Offset by hours. |
| 88 | 157 | */ |
| @@ -93,11 +162,12 @@ | ||
| 93 | 162 | /** |
| 94 | 163 | * Page base for the Calypso admin page. |
| 95 | 164 | */ |
| 96 | 165 | protected function get_admin_path() { |
| 97 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 98 | 166 | if ( ! isset( $_SERVER['PHP_SELF'] ) || ! isset( $_SERVER['QUERY_STRING'] ) ) { |
| 99 | - $parsed = wp_parse_url( admin_url( 'tools.php?page=advertising' ) ); | |
| 167 | + $admin_path = $this->admin_page . '?page=' . $this->menu_slug; | |
| 168 | + $parsed = wp_parse_url( admin_url( $admin_path ) ); | |
| 169 | + | |
| 100 | 170 | return $parsed['path'] . '?' . $parsed['query']; |
| 101 | 171 | } |
| 102 | 172 | // We do this because page.js requires the exactly page base to be set otherwise it will not work properly. |
| 103 | 173 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| @@ -104,17 +174,17 @@ | ||
| 104 | 174 | return wp_unslash( $_SERVER['PHP_SELF'] ) . '?' . wp_unslash( $_SERVER['QUERY_STRING'] ); |
| 105 | 175 | } |
| 106 | 176 | |
| 107 | 177 | /** |
| 108 | - * Get locale acceptable by Calypso. | |
| 178 | + * Get the user's locale acceptable by Calypso. | |
| 109 | 179 | */ |
| 110 | - protected function get_site_locale() { | |
| 180 | + protected function get_locale() { | |
| 111 | 181 | /** |
| 112 | 182 | * In WP, locales are formatted as LANGUAGE_REGION, for example `en`, `en_US`, `es_AR`, |
| 113 | 183 | * but Calypso expects language-region, e.g. `en-us`, `en`, `es-ar`. So we need to convert |
| 114 | 184 | * them to lower case and replace the underscore with a dash. |
| 115 | 185 | */ |
| 116 | - $locale = strtolower( get_locale() ); | |
| 186 | + $locale = strtolower( get_user_locale() ); | |
| 117 | 187 | $locale = str_replace( '_', '-', $locale ); |
| 118 | 188 | |
| 119 | 189 | return $locale; |
| 120 | 190 | } |