From 01deb1aa4586bac8bde6b5624e611b9b2d196fef Mon Sep 17 00:00:00 2001 From: Stefano Novelli Date: Mon, 16 Dec 2024 10:35:19 +0100 Subject: [PATCH 1/2] Refactor TwitterShortcode to improve error handling and readability. --- src/LaravelShortcodePlus.php | 4 +++- src/LaravelShortcodePlusServiceProvider.php | 4 +++- src/Shortcodes/TwitterShortcode.php | 15 ++++++++++----- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/LaravelShortcodePlus.php b/src/LaravelShortcodePlus.php index c8bbe7a..7b7f174 100755 --- a/src/LaravelShortcodePlus.php +++ b/src/LaravelShortcodePlus.php @@ -22,7 +22,9 @@ final class LaravelShortcodePlus { - public function __construct(protected string $content = '') {} + public function __construct(protected string $content = '') + { + } public static function source(string $source): LaravelShortcodePlus { diff --git a/src/LaravelShortcodePlusServiceProvider.php b/src/LaravelShortcodePlusServiceProvider.php index 6590ec0..0e8c8a4 100644 --- a/src/LaravelShortcodePlusServiceProvider.php +++ b/src/LaravelShortcodePlusServiceProvider.php @@ -42,7 +42,9 @@ public function configurePackage(Package $package): void ->hasMigration('create_laravel-shortcode-plus_table'); } - public function packageRegistered() {} + public function packageRegistered() + { + } public function packageBooted(): void { diff --git a/src/Shortcodes/TwitterShortcode.php b/src/Shortcodes/TwitterShortcode.php index 3eeeed2..6ef04a5 100644 --- a/src/Shortcodes/TwitterShortcode.php +++ b/src/Shortcodes/TwitterShortcode.php @@ -27,13 +27,18 @@ public function register($shortcode): string private static function getOembed(string $url): ?string { - curl_setopt_array($curl = curl_init(), [ - CURLOPT_URL => "https://publish.twitter.com/oembed?url=$url&omit_script=1", - CURLOPT_RETURNTRANSFER => true, - ]); + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, 'https://publish.twitter.com/oembed?url='.urlencode($url).'&omit_script=1'); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); curl_close($curl); - return json_decode($response)->html ?? null; + if ($response === false) { + return null; + } + + $data = json_decode($response, true); + + return $data['html'] ?? null; } } From a2c8bb50e2e3480a5a83ccf7ffadf091daf2cd3b Mon Sep 17 00:00:00 2001 From: murdercode Date: Mon, 16 Dec 2024 09:35:44 +0000 Subject: [PATCH 2/2] Fix styling --- src/LaravelShortcodePlus.php | 4 +--- src/LaravelShortcodePlusServiceProvider.php | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/LaravelShortcodePlus.php b/src/LaravelShortcodePlus.php index 7b7f174..c8bbe7a 100755 --- a/src/LaravelShortcodePlus.php +++ b/src/LaravelShortcodePlus.php @@ -22,9 +22,7 @@ final class LaravelShortcodePlus { - public function __construct(protected string $content = '') - { - } + public function __construct(protected string $content = '') {} public static function source(string $source): LaravelShortcodePlus { diff --git a/src/LaravelShortcodePlusServiceProvider.php b/src/LaravelShortcodePlusServiceProvider.php index 0e8c8a4..6590ec0 100644 --- a/src/LaravelShortcodePlusServiceProvider.php +++ b/src/LaravelShortcodePlusServiceProvider.php @@ -42,9 +42,7 @@ public function configurePackage(Package $package): void ->hasMigration('create_laravel-shortcode-plus_table'); } - public function packageRegistered() - { - } + public function packageRegistered() {} public function packageBooted(): void {