key = $key; $this->webhook = $webhook; $this->authorization = $authorization; } /** * @param array{state: string, challenge: string, redirectUri: string} $request */ public static function authorizationUrl(array $request): string { return add_query_arg(array_map('rawurlencode', [ 'page' => self::PAGE, self::AUTHORIZE_ACTION => '1', 'response_type' => 'code', 'state' => $request['state'], 'code_challenge' => $request['challenge'], 'code_challenge_method' => 'S256', 'redirect_uri' => $request['redirectUri'], ]), admin_url('options-general.php')); } public function register(string $pluginFile): void { add_action('admin_menu', [$this, 'menu']); add_action('admin_post_' . self::AUTHORIZE_ACTION, [$this, 'decide']); add_filter('plugin_action_links_' . plugin_basename($pluginFile), [$this, 'actionLinks']); } public function menu(): void { add_options_page( __('Lingo.dev', 'lingo-dev'), __('Lingo.dev', 'lingo-dev'), 'manage_options', self::PAGE, [$this, 'render'] ); } public function actionLinks(array $links): array { array_unshift($links, sprintf( '%s', esc_url(admin_url('options-general.php?page=' . self::PAGE)), esc_html__('Settings', 'lingo-dev') )); return $links; } public function decide(): void { if (!current_user_can('manage_options')) { wp_die(esc_html__('You are not allowed to connect this site to Lingo.dev.', 'lingo-dev'), '', ['response' => 403]); } check_admin_referer(self::AUTHORIZE_ACTION); $posted = []; foreach (array_merge(self::PARAMS, ['decision']) as $name) { if (isset($_POST[$name]) && is_string($_POST[$name])) { $posted[$name] = sanitize_text_field(wp_unslash($_POST[$name])); } } $request = Authorization::request($posted, Authorization::redirectUris()); if ($request === null) { wp_die(esc_html($this->invalidRequest()), '', ['response' => 400]); } $answer = ($posted['decision'] ?? null) === 'allow' && Plugin::languages() !== null ? ['code' => $this->authorization->grant($request['challenge'], $request['redirectUri'], time())] : ['error' => 'access_denied']; $host = (string) wp_parse_url($request['redirectUri'], PHP_URL_HOST); add_filter('allowed_redirect_hosts', static function (array $hosts) use ($host): array { $hosts[] = $host; return $hosts; }); wp_safe_redirect(add_query_arg( array_map('rawurlencode', $answer + ['state' => $request['state']]), $request['redirectUri'] )); exit; } public function render(): void { if (!current_user_can('manage_options')) { return; } $languages = Plugin::languages(); // phpcs:disable WordPress.Security.NonceVerification.Recommended $authorizing = isset($_GET[self::AUTHORIZE_ACTION]); $query = []; foreach (self::PARAMS as $name) { if (isset($_GET[$name]) && is_string($_GET[$name])) { $query[$name] = sanitize_text_field(wp_unslash($_GET[$name])); } } // phpcs:enable WordPress.Security.NonceVerification.Recommended $request = $authorizing ? Authorization::request($query, Authorization::redirectUris()) : null; if ($request !== null) { $this->renderConsent($request, $languages !== null); return; } echo '
' . esc_html($this->invalidRequest()) . '
' . esc_html($this->missingLanguages()) . '
' . esc_html($this->missingLanguages()) . '
' . sprintf(
/* translators: %s: the site's address */
esc_html__('Lingo.dev asks to connect to %s.', 'lingo-dev'),
'' . esc_html(home_url()) . ''
) . '
' . esc_html__('Once connected, Lingo.dev reads the posts of the post types you choose to translate and writes their translations through this plugin.', 'lingo-dev') . '
'; if ($this->key->exists()) { echo '' . esc_html__('This site is already connected to Lingo.dev. Connecting again replaces that connection.', 'lingo-dev') . '
'; } echo '