Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement - Added Logo and banner url from asset path #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<h4>1.0 – July 20, 2022</h4><ul><li>Bug fixes.</li><li>Initital release.</li></ul>",
"frequently asked questions": "<h4>Question<h4><p>Answer</p>"
}
},
"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
Expand Down Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ class Client {
*/
public $textdomain;

/**
* Asset Path
*
* @var string
*/
public $asset_path;

/**
* The Object of Updater Class
*
Expand Down Expand Up @@ -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;
}
}
120 changes: 119 additions & 1 deletion src/Updater.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace SureCart\Licensing;

use Exception;

/**
* This class will handle the updates.
*/
Expand Down Expand Up @@ -146,7 +148,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 ) ) {
Expand Down Expand Up @@ -232,4 +249,105 @@ 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.
*
* @param string $prefix The prefix of the image, eg: banner or icon.
* @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.
if ( 'icon' === $prefix ) {
$sizes = array(
'1x' => '128x128',
'2x' => '256x256',
'svg' => 'icon',
);
$allowed_extensions[] = '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;

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 ) {
try {
$headers = get_headers( $url );
return $headers && strpos( $headers[0], '200' );
} catch ( Exception $e ) {
return false;
}
}
}
Loading