Skip to content

Commit c6b9be8

Browse files
author
Dor Kohn
committed
fix: improve connection string error handling for common copy-paste mistakes
Author: Dor Kohn Context: Product Manager Home Assignment - Add normalize_cloudinary_url() method to handle CLOUDINARY_URL= prefix - Add angle bracket detection with helpful error message - Update regex to support cloud names with underscores/hyphens/numbers - Apply consistent normalization across wizard, settings, and upgrade flows
1 parent 56c8ef2 commit c6b9be8

1 file changed

Lines changed: 52 additions & 4 deletions

File tree

php/class-connect.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ class Connect extends Settings_Component implements Config, Setup, Notice {
9090

9191
/**
9292
* Regex to match Cloudinary environment variable.
93+
* Updated to support cloud names with underscores, hyphens, and numbers.
9394
*/
94-
const CLOUDINARY_VARIABLE_REGEX = '^(?:CLOUDINARY_URL=)?cloudinary://[0-9]+:[A-Za-z_\-0-9]+@[A-Za-z]+';
95+
const CLOUDINARY_VARIABLE_REGEX = '^(?:CLOUDINARY_URL=)?cloudinary://[0-9]+:[A-Za-z0-9_\-]+@[A-Za-z0-9_\-]+';
9596

9697
/**
9798
* Initiate the plugin resources.
@@ -272,14 +273,25 @@ public function verify_connection( $data ) {
272273
return $data;
273274
}
274275

275-
$data['cloudinary_url'] = str_replace( 'CLOUDINARY_URL=', '', $data['cloudinary_url'] );
276+
$data['cloudinary_url'] = $this->normalize_cloudinary_url( $data['cloudinary_url'] );
276277
$current = $this->plugin->settings->find_setting( 'connect' )->get_value();
277278

278279
// Same URL, return original data.
279280
if ( $current['cloudinary_url'] === $data['cloudinary_url'] ) {
280281
return $data;
281282
}
282283

284+
// Check for angle brackets early with helpful message.
285+
if ( preg_match( '/[<>]/', $data['cloudinary_url'] ) ) {
286+
$admin->add_admin_notice(
287+
'angle_brackets',
288+
__( 'Your connection string contains angle brackets (< or >). Replace the placeholder values like <your_api_key> with your actual credentials.', 'cloudinary' ),
289+
'error'
290+
);
291+
292+
return $current;
293+
}
294+
283295
// Pattern match to ensure validity of the provided url.
284296
if ( ! preg_match( '~' . self::CLOUDINARY_VARIABLE_REGEX . '~', $data['cloudinary_url'] ) ) {
285297
$admin->add_admin_notice(
@@ -376,6 +388,27 @@ public function is_connected() {
376388
return true;
377389
}
378390

391+
/**
392+
* Normalize a Cloudinary URL by trimming whitespace, quotes, and removing the CLOUDINARY_URL= prefix.
393+
*
394+
* @param string $url The URL to normalize.
395+
*
396+
* @return string The normalized URL.
397+
*/
398+
protected function normalize_cloudinary_url( $url ) {
399+
if ( ! is_string( $url ) ) {
400+
return $url;
401+
}
402+
// Trim whitespace.
403+
$url = trim( $url );
404+
// Trim surrounding quotes (single or double).
405+
$url = trim( $url, " \t\n\r\0\x0B\"'" );
406+
// Remove CLOUDINARY_URL= prefix if present.
407+
$url = preg_replace( '/^CLOUDINARY_URL=/', '', $url );
408+
409+
return $url;
410+
}
411+
379412
/**
380413
* Test the connection url.
381414
*
@@ -384,6 +417,21 @@ public function is_connected() {
384417
* @return array
385418
*/
386419
public function test_connection( $url ) {
420+
// Normalize the URL first.
421+
$url = $this->normalize_cloudinary_url( $url );
422+
423+
// Check for angle brackets - common copy-paste error from Cloudinary dashboard.
424+
if ( is_string( $url ) && preg_match( '/[<>]/', $url ) ) {
425+
return array(
426+
'type' => 'invalid_url',
427+
'message' => __(
428+
'Your connection string contains angle brackets (< or >). Replace the placeholder values like <your_api_key> with your actual credentials. The format should be: cloudinary://API_KEY:API_SECRET@CLOUD_NAME',
429+
'cloudinary'
430+
),
431+
'url' => $url,
432+
);
433+
}
434+
387435
$result = array(
388436
'type' => 'connection_success',
389437
'message' => null,
@@ -903,7 +951,7 @@ public function upgrade_connection( $old_version ) {
903951
}
904952

905953
// Test upgraded details.
906-
$data['cloudinary_url'] = str_replace( 'CLOUDINARY_URL=', '', $data['cloudinary_url'] );
954+
$data['cloudinary_url'] = $this->normalize_cloudinary_url( $data['cloudinary_url'] );
907955
$test = $this->test_connection( $data['cloudinary_url'] );
908956

909957
if ( 'connection_success' === $test['type'] ) {
@@ -942,7 +990,7 @@ public function maybe_connection_string_constant( $value, $setting ) {
942990
static $url = null;
943991

944992
if ( empty( $url ) ) {
945-
$url = str_replace( 'CLOUDINARY_URL=', '', CLOUDINARY_CONNECTION_STRING );
993+
$url = $this->normalize_cloudinary_url( CLOUDINARY_CONNECTION_STRING );
946994
}
947995

948996
if ( 'cloudinary_url' === $setting ) {

0 commit comments

Comments
 (0)