From 871fc924d96c610f650b4fe34128c134459f1c49 Mon Sep 17 00:00:00 2001 From: maniruzzamanak Date: Fri, 2 Feb 2024 14:36:44 +0600 Subject: [PATCH 1/7] Enhancement - Added Logo and banner url from asset path --- README.md | 24 +++++++++- src/Client.php | 20 +++++++++ src/Updater.php | 114 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 155 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 50f419d..9a52157 100644 --- a/README.md +++ b/README.md @@ -46,15 +46,28 @@ Sections will require a `changelog` property with an html string of your changel "author_profile": "https://surecart.com", "version": "0.9.0", "requires": "5.6", - "tested": "6.1.0", + "tested": "6.4.3", "requires_php": "5.3", "sections": { "description": "This is my plugin description.", "changelog": "

1.0 – July 20, 2022

", "frequently asked questions": "

Question

Answer

" - } + }, + "icons": [ + "https://example.com/assets/icon-128x128.png", + "https://example.com/assets/icon-256x256.png", + "https://example.com/assets/icon.svg", + ], + "banners": [ + "https://example.com/assets/banner-772x250.png", + "https://example.com/assets/banner-1544x500.png", + ] } ``` +!!! note Handling Release Images from urls or asset path + - **URL:** If you want to set the reease icons and banners from url, then fill the above URL's. And, those will get higher priority than asset path images. + + - **Asset path:** And if you want to get icons and banners from plugin asset directory, after instantiate the client, set asset path using `$client->set_asset_path( $path )`, And hence, no need to pass banners, icons from `release.json` file. ## Usage Example @@ -103,6 +116,13 @@ Make sure you call this function directly, never use any action hook to call thi ```php $client = new \SureCart\Licensing\Client( 'Twenty Twelve', 'pt_jzieNYQdE5LMAxksscgU6H4', __FILE__ ); ``` +## Set asset path. + +You should set your plugin files asset path for icons and banners. + +```php +$client->set_asset_path( 'your-project-asset-path' ) +``` ## Set textdomain diff --git a/src/Client.php b/src/Client.php index 05def08..463c5b0 100644 --- a/src/Client.php +++ b/src/Client.php @@ -78,6 +78,13 @@ class Client { */ public $textdomain; + /** + * Asset Path + * + * @var string + */ + public $asset_path; + /** * The Object of Updater Class * @@ -341,5 +348,18 @@ public function __( $text ) { */ public function set_textdomain( $textdomain ) { $this->textdomain = $textdomain; + + return $this; + } + + /** + * Set project asset path for images. + * + * @param string $asset_path The asset path for images. + */ + public function set_asset_path( $asset_path ) { + $this->asset_path = $asset_path; + + return $this; } } diff --git a/src/Updater.php b/src/Updater.php index 0ca94b5..1e7d0be 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -146,7 +146,22 @@ private function get_project_latest_version() { } if ( isset( $release->banners ) ) { - $release->banners = (array) $release->banners; + $release->banners = $this->getReleaseBanners( (array) $release->banners ); + } + + if ( isset( $release->icons ) ) { + $release->icons = $this->getReleaseIcons( (array) $release->icons ); + } + + // If there is asset path, then try to find the images from the asset path. + if ( ! empty ( $this->client->asset_path ) ) { + if ( ! isset( $release->banners ) || ( isset( $release->banners ) && ! count( $release->banners ) ) ) { + $release->banners = $this->findImagesFromAssetPath( 'banner' ); + } + + if ( ! isset( $release->icons ) || ( isset( $release->icons ) && ! count( $release->icons ) ) ) { + $release->icons = $this->findImagesFromAssetPath( 'icon' ); + } } if ( isset( $release->sections ) ) { @@ -232,4 +247,101 @@ private function get_version_info() { return $version_info; } + + /** + * Get processed banners. + * + * @param array $banners The banners. + * @return array The processed banners. + */ + public function getReleaseBanners( $banners ) { + $release_banners = []; + + foreach ( $banners as $banner ) { + if ( strpos( $banner, '772x250' ) !== false ) { + $release_banners['low'] = $banner; + } elseif ( strpos( $banner, '1544x500' ) !== false ) { + $release_banners['high'] = $banner; + } + } + + return $release_banners; + } + + /** + * Get processed icons. + * + * @param array $icons The icons. + * @return array The processed icons. + */ + public function getReleaseIcons( $icons ) { + $release_icons = []; + + foreach ( $icons as $icon ) { + if ( strpos( $icon, '.svg' ) !== false ) { + $release_icons['svg'] = $icon; + } elseif ( strpos( $icon, '128x128' ) !== false ) { + $release_icons['1x'] = $icon; + } elseif ( strpos( $icon, '256x256' ) !== false ) { + $release_icons['2x'] = $icon; + } + } + + return $release_icons; + } + + /** + * Find images from the asset path. + * + * @param string $prefix The prefix of the image, eg: banner or icon. + * @return array The images. + */ + public function findImagesFromAssetPath( $prefix ) { + $sizes = []; + $images = []; + $allowed_extensions = [ + 'jpg', + 'png', + ]; + + // Set the sizes and allowed extensions for the banner and icon. + if ( 'icon' === $prefix ) { + $sizes = [ + '1x' => '128x128', + '2x' => '256x256', + 'svg' => 'icon', + ]; + $allowed_extensions[] = 'svg'; + } elseif ( 'banner' === $prefix ) { + $sizes = [ + 'low' => '772x250', + 'high' => '1544x500', + ]; + } + + // Loop through the sizes and extensions to find the images. + foreach ( $sizes as $size => $dimension ) { + foreach ( $allowed_extensions as $extension ) { + $image_file_name = 'svg' === $extension ? "/$prefix.$extension" : "/$prefix-$dimension.$extension"; + $image_url = $this->client->asset_path . $image_file_name; + + if ( $this->urlExists( $image_url ) ) { + $images[ $size ] = $image_url; + } + } + } + + return $images; + } + + /** + * Check if the URL exists. + * + * @param string $url The URL to check. + * @return boolean True if the URL exists, false if not. + */ + private function urlExists( $url ) { + $headers = @get_headers( $url ); + return $headers && strpos( $headers[0], '200' ); + } } From 2e249cd2984a3ebadb94455d2c5523e848012719 Mon Sep 17 00:00:00 2001 From: maniruzzamanak Date: Fri, 2 Feb 2024 15:03:19 +0600 Subject: [PATCH 2/7] fixed coding standard --- src/Updater.php | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Updater.php b/src/Updater.php index 1e7d0be..c9c0569 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -1,6 +1,8 @@ client->asset_path ) ) { + if ( ! empty( $this->client->asset_path ) ) { if ( ! isset( $release->banners ) || ( isset( $release->banners ) && ! count( $release->banners ) ) ) { $release->banners = $this->findImagesFromAssetPath( 'banner' ); } @@ -255,7 +257,7 @@ private function get_version_info() { * @return array The processed banners. */ public function getReleaseBanners( $banners ) { - $release_banners = []; + $release_banners = array(); foreach ( $banners as $banner ) { if ( strpos( $banner, '772x250' ) !== false ) { @@ -297,26 +299,26 @@ public function getReleaseIcons( $icons ) { * @return array The images. */ public function findImagesFromAssetPath( $prefix ) { - $sizes = []; - $images = []; - $allowed_extensions = [ + $sizes = array(); + $images = array(); + $allowed_extensions = array( 'jpg', 'png', - ]; + ); // Set the sizes and allowed extensions for the banner and icon. if ( 'icon' === $prefix ) { - $sizes = [ + $sizes = array( '1x' => '128x128', '2x' => '256x256', 'svg' => 'icon', - ]; + ); $allowed_extensions[] = 'svg'; } elseif ( 'banner' === $prefix ) { - $sizes = [ + $sizes = array( 'low' => '772x250', 'high' => '1544x500', - ]; + ); } // Loop through the sizes and extensions to find the images. @@ -341,7 +343,11 @@ public function findImagesFromAssetPath( $prefix ) { * @return boolean True if the URL exists, false if not. */ private function urlExists( $url ) { - $headers = @get_headers( $url ); - return $headers && strpos( $headers[0], '200' ); + try { + $headers = @get_headers( $url ); + return $headers && strpos( $headers[0], '200' ); + } catch ( Exception $e ) { + return false; + } } } From b797083ab538c2b4768cebdb656a52b272b7b1f7 Mon Sep 17 00:00:00 2001 From: maniruzzamanak Date: Fri, 2 Feb 2024 15:06:42 +0600 Subject: [PATCH 3/7] Fixed more coding standards --- src/Updater.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Updater.php b/src/Updater.php index c9c0569..6b49dfc 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -277,7 +277,7 @@ public function getReleaseBanners( $banners ) { * @return array The processed icons. */ public function getReleaseIcons( $icons ) { - $release_icons = []; + $release_icons = array(); foreach ( $icons as $icon ) { if ( strpos( $icon, '.svg' ) !== false ) { @@ -308,7 +308,7 @@ public function findImagesFromAssetPath( $prefix ) { // Set the sizes and allowed extensions for the banner and icon. if ( 'icon' === $prefix ) { - $sizes = array( + $sizes = array( '1x' => '128x128', '2x' => '256x256', 'svg' => 'icon', @@ -344,7 +344,7 @@ public function findImagesFromAssetPath( $prefix ) { */ private function urlExists( $url ) { try { - $headers = @get_headers( $url ); + $headers = get_headers( $url ); return $headers && strpos( $headers[0], '200' ); } catch ( Exception $e ) { return false; From 027a148e6a7e52f9146259b0b1666f1b43adc506 Mon Sep 17 00:00:00 2001 From: ManiruzzamanAkash Date: Wed, 15 Apr 2026 21:08:27 +0600 Subject: [PATCH 4/7] remove urlExists check and added sanitization --- src/Updater.php | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/src/Updater.php b/src/Updater.php index 6b49dfc..f2c12a0 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -326,28 +326,11 @@ public function findImagesFromAssetPath( $prefix ) { foreach ( $allowed_extensions as $extension ) { $image_file_name = 'svg' === $extension ? "/$prefix.$extension" : "/$prefix-$dimension.$extension"; $image_url = $this->client->asset_path . $image_file_name; - - if ( $this->urlExists( $image_url ) ) { - $images[ $size ] = $image_url; - } + $images[ $size ] = esc_url_raw( $image_url ); } } return $images; } - /** - * Check if the URL exists. - * - * @param string $url The URL to check. - * @return boolean True if the URL exists, false if not. - */ - private function urlExists( $url ) { - try { - $headers = get_headers( $url ); - return $headers && strpos( $headers[0], '200' ); - } catch ( Exception $e ) { - return false; - } - } } From 26f4c1b6de4638251076214700f29b49fafdf189 Mon Sep 17 00:00:00 2001 From: ManiruzzamanAkash Date: Wed, 15 Apr 2026 23:41:07 +0600 Subject: [PATCH 5/7] simplify asset path logic --- README.md | 5 +-- src/Updater.php | 90 ++++++------------------------------------------- 2 files changed, 12 insertions(+), 83 deletions(-) diff --git a/README.md b/README.md index 5a2978d..b9a2111 100644 --- a/README.md +++ b/README.md @@ -64,10 +64,7 @@ Sections will require a `changelog` property with an html string of your changel } } ``` -!!! note Handling Release Images from urls or asset path - - **URL:** If you want to set the reease icons and banners from url, then fill the above URL's. And, those will get higher priority than asset path images. - - - **Asset path:** And if you want to get icons and banners from plugin asset directory, after instantiate the client, set asset path using `$client->set_asset_path( $path )`, And hence, no need to pass banners, icons from `release.json` file. +> **Note:** Icons and banners in `release.json` take priority over asset path images. If you prefer to serve them from your plugin's asset directory instead, omit the `icons` and `banners` keys from `release.json` and set the asset path using `$client->set_asset_path( $path )`. ### ⚠️ Important In order for updates to work, the `slug` in release.json must match the **folder name** of your plugin or theme. diff --git a/src/Updater.php b/src/Updater.php index dd8f9c6..59f7a3a 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -1,7 +1,6 @@ banners ) ) { - $release->banners = $this->getReleaseBanners( (array) $release->banners ); + $release->banners = (array) $release->banners; } if ( isset( $release->icons ) ) { - $release->icons = $this->getReleaseIcons( (array) $release->icons ); + $release->icons = (array) $release->icons; } // If there is asset path, then try to find the images from the asset path. if ( ! empty( $this->client->asset_path ) ) { - if ( ! isset( $release->banners ) || ( isset( $release->banners ) && ! count( $release->banners ) ) ) { + if ( empty( $release->banners ) ) { $release->banners = $this->findImagesFromAssetPath( 'banner' ); } - if ( ! isset( $release->icons ) || ( isset( $release->icons ) && ! count( $release->icons ) ) ) { + if ( empty( $release->icons ) ) { $release->icons = $this->findImagesFromAssetPath( 'icon' ); } } - if ( isset( $release->icons ) ) { - $release->icons = (array) $release->icons; - } - if ( isset( $release->sections ) ) { $release->sections = (array) $release->sections; } @@ -271,48 +266,6 @@ private function get_version_info() { return $version_info; } - /** - * Get processed banners. - * - * @param array $banners The banners. - * @return array The processed banners. - */ - public function getReleaseBanners( $banners ) { - $release_banners = array(); - - foreach ( $banners as $banner ) { - if ( strpos( $banner, '772x250' ) !== false ) { - $release_banners['low'] = $banner; - } elseif ( strpos( $banner, '1544x500' ) !== false ) { - $release_banners['high'] = $banner; - } - } - - return $release_banners; - } - - /** - * Get processed icons. - * - * @param array $icons The icons. - * @return array The processed icons. - */ - public function getReleaseIcons( $icons ) { - $release_icons = array(); - - foreach ( $icons as $icon ) { - if ( strpos( $icon, '.svg' ) !== false ) { - $release_icons['svg'] = $icon; - } elseif ( strpos( $icon, '128x128' ) !== false ) { - $release_icons['1x'] = $icon; - } elseif ( strpos( $icon, '256x256' ) !== false ) { - $release_icons['2x'] = $icon; - } - } - - return $release_icons; - } - /** * Find images from the asset path. * @@ -320,38 +273,17 @@ public function getReleaseIcons( $icons ) { * @return array The images. */ public function findImagesFromAssetPath( $prefix ) { - $sizes = array(); - $images = array(); - $allowed_extensions = array( - 'jpg', - 'png', - ); - - // Set the sizes and allowed extensions for the banner and icon. + $images = array(); + if ( 'icon' === $prefix ) { - $sizes = array( - '1x' => '128x128', - '2x' => '256x256', - 'svg' => 'icon', - ); - $allowed_extensions[] = 'svg'; + $images['1x'] = esc_url_raw( $this->client->asset_path . '/icon-128x128.png' ); + $images['2x'] = esc_url_raw( $this->client->asset_path . '/icon-256x256.png' ); + $images['svg'] = esc_url_raw( $this->client->asset_path . '/icon.svg' ); } elseif ( 'banner' === $prefix ) { - $sizes = array( - 'low' => '772x250', - 'high' => '1544x500', - ); - } - - // Loop through the sizes and extensions to find the images. - foreach ( $sizes as $size => $dimension ) { - foreach ( $allowed_extensions as $extension ) { - $image_file_name = 'svg' === $extension ? "/$prefix.$extension" : "/$prefix-$dimension.$extension"; - $image_url = $this->client->asset_path . $image_file_name; - $images[ $size ] = esc_url_raw( $image_url ); - } + $images['low'] = esc_url_raw( $this->client->asset_path . '/banner-772x250.png' ); + $images['high'] = esc_url_raw( $this->client->asset_path . '/banner-1544x500.png' ); } return $images; } - } From ba21cfc7473998c0d25289cf1eb29942d18a6d3c Mon Sep 17 00:00:00 2001 From: ManiruzzamanAkash Date: Wed, 15 Apr 2026 23:43:49 +0600 Subject: [PATCH 6/7] updated Set asset path (optional) instruction --- README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b9a2111..dae23f6 100644 --- a/README.md +++ b/README.md @@ -121,12 +121,22 @@ Make sure you call this function directly, never use any action hook to call thi ```php $client = new \SureCart\Licensing\Client( 'Twenty Twelve', 'pt_jzieNYQdE5LMAxksscgU6H4', __FILE__ ); ``` -## Set asset path. +## Set asset path (optional) -You should set your plugin files asset path for icons and banners. +If you want to serve plugin icons and banners from your asset directory instead of providing URLs in `release.json`, you can set the asset path. This is only used as a fallback when `icons` and `banners` are not present in `release.json`. ```php -$client->set_asset_path( 'your-project-asset-path' ) +$client->set_asset_path( 'https://example.com/wp-content/plugins/my-plugin/assets' ); +``` + +Expected file structure in your asset directory: +``` +assets/ +├── icon-128x128.png +├── icon-256x256.png +├── icon.svg +├── banner-772x250.png +└── banner-1544x500.png ``` ## Set textdomain From d105708ef79c797ba09000e2b53c8c36f0616e85 Mon Sep 17 00:00:00 2001 From: ManiruzzamanAkash Date: Wed, 15 Apr 2026 23:48:39 +0600 Subject: [PATCH 7/7] fixed phpcs and updated instructions for set asset path --- README.md | 2 +- src/Updater.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dae23f6..27b2e74 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ $client = new \SureCart\Licensing\Client( 'Twenty Twelve', 'pt_jzieNYQdE5LMAxkss If you want to serve plugin icons and banners from your asset directory instead of providing URLs in `release.json`, you can set the asset path. This is only used as a fallback when `icons` and `banners` are not present in `release.json`. ```php -$client->set_asset_path( 'https://example.com/wp-content/plugins/my-plugin/assets' ); +$client->set_asset_path( plugins_url( 'assets', __FILE__ ) ); ``` Expected file structure in your asset directory: diff --git a/src/Updater.php b/src/Updater.php index 59f7a3a..6cb2a08 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -1,6 +1,6 @@