diff --git a/README.md b/README.md index 55c97a5..27b2e74 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ 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.", @@ -64,6 +64,7 @@ Sections will require a `changelog` property with an html string of your changel } } ``` +> **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. @@ -120,6 +121,23 @@ 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 (optional) + +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( plugins_url( 'assets', __FILE__ ) ); +``` + +Expected file structure in your asset directory: +``` +assets/ +├── icon-128x128.png +├── icon-256x256.png +├── icon.svg +├── banner-772x250.png +└── banner-1544x500.png +``` ## Set textdomain diff --git a/src/Client.php b/src/Client.php index e2a7883..8f4c657 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 b12dc1e..6cb2a08 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -1,4 +1,5 @@ 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 ( empty( $release->banners ) ) { + $release->banners = $this->findImagesFromAssetPath( 'banner' ); + } + + if ( empty( $release->icons ) ) { + $release->icons = $this->findImagesFromAssetPath( 'icon' ); + } + } + if ( isset( $release->sections ) ) { $release->sections = (array) $release->sections; } @@ -253,4 +265,25 @@ private function get_version_info() { return $version_info; } + + /** + * 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 ) { + $images = array(); + + if ( 'icon' === $prefix ) { + $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 ) { + $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; + } }