5, 'redirection' => 0, 'headers' => array( 'Authorization' => 'Bearer ' . $settings['dashboard_key'], 'Content-Type' => 'application/json', ), 'body' => (string) wp_json_encode( array( 'origin' => self::admin_origin() ) ), ) ); if ( is_wp_error( $response ) ) { /* translators: %s: the error */ return sprintf( __( 'Could not reach your Runlight: %s', 'runlight' ), $response->get_error_message() ); } $body = json_decode( (string) wp_remote_retrieve_body( $response ), true ); $code = (int) wp_remote_retrieve_response_code( $response ); if ( 201 !== $code || ! is_array( $body ) || ! is_string( $body['ticket'] ?? null ) ) { if ( 401 === $code ) { return __( 'Your Runlight does not know this dashboard key. Connect again in the settings, or make a new key in Runlight under Settings, Install.', 'runlight' ); } $reason = is_array( $body ) && is_string( $body['error'] ?? null ) ? $body['error'] : sprintf( /* translators: %d: the HTTP status */ __( 'It answered %d.', 'runlight' ), $code ); /* translators: %s: what the Runlight said */ return sprintf( __( 'Your Runlight would not open the dashboard here: %s', 'runlight' ), $reason ); } return array( 'url' => $settings['address'] . '/embed?ticket=' . rawurlencode( $body['ticket'] ) . '&theme=light&lang=' . rawurlencode( $lang ), 'site' => is_string( $body['site'] ?? null ) ? $body['site'] : '', ); } public static function page(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $settings = Settings::get(); $setup = admin_url( 'options-general.php?page=runlight' ); $local = 'local' === $settings['mode']; $address = Settings::address(); // The dashboard starts in the language this admin reads wp-admin in, not the browser's. $lang = get_user_locale(); // The dashboard lets only its own origin frame it, so wp-admin served from another one, such as www. or http, opens it in a tab. $elsewhere = $local && '' !== $address && self::origin_of( $address ) !== self::admin_origin(); if ( $local ) { // Runlight here is on this site, signed in with this login, so the whole dashboard shows, not a share view. $ticket = '' !== $address ? array( 'url' => $address . '/?lang=' . rawurlencode( $lang ) ) : Library::missing(); $full = '' !== $address ? $address . '/?lang=' . rawurlencode( $lang ) : ''; } else { $ticket = '' !== $address && '' !== $settings['dashboard_key'] ? self::ticket( $settings, $lang ) : null; $site = is_array( $ticket ) && '' !== $ticket['site'] ? $ticket['site'] : $settings['site']; $full = '' !== $address ? $address . '/?' . ( '' !== $site ? 'site=' . rawurlencode( $site ) . '&' : '' ) . 'lang=' . rawurlencode( $lang ) : ''; } ?>