diff --git a/classes/admin/class-slack-settings.php b/classes/admin/class-slack-settings.php index 125e0cdd4..98e55835f 100644 --- a/classes/admin/class-slack-settings.php +++ b/classes/admin/class-slack-settings.php @@ -13,35 +13,39 @@ class Slack_Settings { /** - * The Slack client ID. + * The Progress Planner API root URL. * * @var string */ - const CLIENT_ID = '3300596017925.8228567943589'; + const API_ROOT = 'https://prpl.fyi/api/v1'; /** - * The Slack client secret. + * The Progress Planner OAuth endpoint. * * @var string */ - const CLIENT_SECRET = '79dfe36cd83b4be906d1c8f5906923be'; + const OAUTH_ENDPOINT = '/slack/oauth'; /** - * The redirect URI for the Slack OAuth callback. + * Get the authorization URL for Slack OAuth. * - * @var string + * @return string */ - public $redirect_uri; + private function get_auth_url() { + $site_url = \admin_url( 'admin.php?page=progress-planner-slack' ); + $params = [ + 'site_url' => \rawurlencode( $site_url ), + 'site_name' => \rawurlencode( \get_bloginfo( 'name' ) ), + ]; + + return self::API_ROOT . self::OAUTH_ENDPOINT . '?' . \http_build_query( $params ); + } /** * Constructor. */ public function __construct() { - $this->redirect_uri = \defined( 'PROGRESS_PLANNER_SLACK_DEBUG' ) && \PROGRESS_PLANNER_SLACK_DEBUG - ? 'https://a549-146-212-36-44.ngrok-free.app/wp-admin/admin.php?page=progress-planner-slack&slack_oauth=1' - : admin_url( 'admin.php?page=progress-planner-slack&slack_oauth=1' ); - // Add the admin submenu page. \add_action( 'admin_menu', [ $this, 'add_admin_menu_page' ], 99 ); \add_action( 'admin_init', [ $this, 'handle_oauth_callback' ] ); @@ -71,28 +75,12 @@ public function add_admin_menu_page() { * Handle the OAuth callback from Slack. */ public function handle_oauth_callback() { - if ( ! isset( $_GET['slack_oauth'] ) || ! isset( $_GET['code'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended + if ( ! isset( $_GET['slack_token'] ) ) { return; } - $response = wp_remote_post( - 'https://slack.com/api/oauth.v2.access', - [ - 'body' => [ - 'client_id' => self::CLIENT_ID, - 'client_secret' => self::CLIENT_SECRET, - 'code' => sanitize_text_field( wp_unslash( $_GET['code'] ) ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended - 'redirect_uri' => $this->redirect_uri, - ], - ] - ); - - if ( ! is_wp_error( $response ) ) { - $body = json_decode( wp_remote_retrieve_body( $response ), true ); - if ( ! empty( $body['access_token'] ) ) { - update_option( 'slack_access_token', $body['access_token'] ); - } - } + $token = sanitize_text_field( wp_unslash( $_GET['slack_token'] ) ); + update_option( 'slack_access_token', $token ); wp_safe_redirect( admin_url( 'admin.php?page=progress-planner-slack' ) ); exit; @@ -136,7 +124,7 @@ public function render_page() {
redirect_uri ); + $auth_url = $this->get_auth_url(); ?>