id, array( 'dashboard', 'plugins' ), true ) || Settings::set_up() || ! current_user_can( 'manage_options' ) ) { return; } printf( '

%s %s

', esc_html__( 'Runlight is not counting yet. Choose where it runs.', 'runlight' ), esc_url( admin_url( 'options-general.php?page=' . self::PAGE ) ), esc_html__( 'Set up Runlight', 'runlight' ) ); } public static function menu(): void { add_options_page( __( 'Runlight', 'runlight' ), __( 'Runlight', 'runlight' ), 'manage_options', self::PAGE, array( self::class, 'page' ) ); // A top-level item for the dashboard, where the numbers are, or the steps to see them there. // No position, so it sits below the core items. add_menu_page( __( 'Runlight', 'runlight' ), __( 'Runlight', 'runlight' ), 'manage_options', Dashboard::PAGE, array( Dashboard::class, 'page' ), self::icon() ); } /** The R mark, as wp-admin's menu wants it: a data URI it can tint. */ private static function icon(): string { $svg = ''; return 'data:image/svg+xml;base64,' . base64_encode( $svg ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode } /** * Settings and Dashboard links on the Plugins screen. * * @param array $links The links WordPress shows. * @return array */ public static function action_links( array $links ): array { array_unshift( $links, '' . esc_html__( 'Settings', 'runlight' ) . '' ); return $links; } public static function register(): void { register_setting( 'runlight', Settings::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( Settings::class, 'sanitize' ), ) ); } /** Whether the address answers as a Runlight, and what to say about it. */ private static function check( string $address ): array { $response = wp_remote_get( $address . '/api', array( 'timeout' => 5 ) ); if ( is_wp_error( $response ) ) { return array( false, sprintf( /* translators: %s: the error */ __( 'Could not reach it: %s', 'runlight' ), $response->get_error_message() ) ); } $body = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( 200 !== wp_remote_retrieve_response_code( $response ) || ! is_array( $body ) || 'runlight' !== ( $body['name'] ?? '' ) ) { return array( false, __( 'Something answered, but not Runlight. Check that this is the address Runlight answers at, such as https://example.com/runlight for an app with Runlight mounted, or https://stats.example.com for the standalone server.', 'runlight' ) ); } return array( true, sprintf( /* translators: %s: the version */ __( 'Connected to Runlight %s.', 'runlight' ), (string) ( $body['version'] ?? '' ) ) ); } /** * The check, asked for by the settings page once it has loaded, so a slow * or missing Runlight never holds the page up. Only the saved address is * checked, never one the request names. */ public static function ajax_check(): void { check_ajax_referer( 'runlight_check' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( null, 403 ); } $address = Settings::get()['address']; if ( '' === $address ) { wp_send_json_success( null ); } list( $ok, $message ) = self::check( $address ); wp_send_json_success( array( 'ok' => $ok, 'message' => $message, ) ); } /** The settings page's script, which asks for the check and shows its answer. */ public static function scripts( string $hook ): void { if ( 'settings_page_' . self::PAGE !== $hook ) { return; } $config = array( 'url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'runlight_check' ), 'dashboard' => Settings::get()['address'] . '/', 'open' => __( 'Open the dashboard', 'runlight' ), 'failed' => __( 'Could not check your Runlight from here.', 'runlight' ), ); wp_enqueue_script( 'runlight-settings', plugins_url( 'assets/settings.js', RUNLIGHT_PLUGIN_FILE ), array(), RUNLIGHT_PLUGIN_VERSION, true ); wp_localize_script( 'runlight-settings', 'runlightCheck', $config ); } /** * The first screen, before anything is counted: run Runlight in this * WordPress with one press, or connect to one run elsewhere. * * @param string $missing What stops Runlight running here, or an empty string. */ private static function setup( string $missing ): void { $notice = Connect::notice(); // The counting choices keep their defaults until they can be changed on the settings page. $defaults = static function (): void { foreach ( array( 'skip_admins', 'outbound', 'downloads' ) as $field ) { echo ''; } }; ?>

' . esc_html( Local::address() . '/' ) . '' ); ?>

>

' . esc_html__( 'Read how each one works.', 'runlight' ) . '' ); ?>

' . esc_html( Local::address() . '/' ) . '', '' . esc_html__( 'Settings, Permalinks', 'runlight' ) . '' ); ?>

' . esc_html__( 'Read the Runlight docs.', 'runlight' ) . '' ); ?>