@@ -248,13 +248,7 @@ private function generate_and_store_code_verifier() {
248248 $ code_verifier = random_bytes ( 64 );
249249
250250 // Encode to Base64 string.
251- $ code_verifier = base64_encode ( $ code_verifier ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions
252-
253- // Convert Base64 to Base64URL by replacing “+” with “-” and “/” with “_”.
254- $ code_verifier = strtr ( $ code_verifier , '+/ ' , '-_ ' );
255-
256- // Remove padding character from the end of line.
257- $ code_verifier = rtrim ( $ code_verifier , '= ' );
251+ $ code_verifier = $ this ->base64_urlencode ( $ code_verifier );
258252
259253 // Store in database for later use.
260254 update_option ( 'ck_code_verifier ' , $ code_verifier );
@@ -317,15 +311,38 @@ private function delete_code_verifier() {
317311
318312 }
319313
314+ /**
315+ * Base64URL encode the given string.
316+ *
317+ * @since 2.0.0
318+ *
319+ * @param string $str String to encode.
320+ * @return string Encoded string.
321+ */
322+ public function base64_urlencode ( $ str ) {
323+
324+ // Encode to Base64 string.
325+ $ str = base64_encode ( $ str ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions
326+
327+ // Convert Base64 to Base64URL by replacing “+” with “-” and “/” with “_”.
328+ $ str = strtr ( $ str , '+/ ' , '-_ ' );
329+
330+ // Remove padding character from the end of line.
331+ $ str = rtrim ( $ str , '= ' );
332+
333+ return $ str ;
334+
335+ }
336+
320337 /**
321338 * Returns the URL used to begin the OAuth process
322339 *
323340 * @since 2.0.0
324341 *
325- * @param bool|string $state Optional state parameter to include in OAuth request .
326- * @return string OAuth URL
342+ * @param bool|string $return_url Return URL .
343+ * @return string OAuth URL
327344 */
328- public function get_oauth_url ( $ state = false ) {
345+ public function get_oauth_url ( $ return_url = false ) {
329346
330347 // Generate and store code verifier and challenge.
331348 $ code_verifier = $ this ->generate_and_store_code_verifier ();
@@ -340,9 +357,15 @@ public function get_oauth_url( $state = false ) {
340357 'code_challenge_method ' => 'S256 ' ,
341358 );
342359
343- // If a state parameter needs to be included, add it now.
344- if ( $ state ) {
345- $ args ['state ' ] = rawurlencode ( $ state );
360+ if ( $ return_url ) {
361+ $ args ['state ' ] = $ this ->base64_urlencode (
362+ wp_json_encode (
363+ array (
364+ 'return_to ' => $ return_url ,
365+ 'client_id ' => $ this ->client_id ,
366+ )
367+ )
368+ );
346369 }
347370
348371 // Return OAuth URL.
0 commit comments