| 1 |
<?php |
| 2 |
|
| 3 |
namespace Arraytics\PluginNotice; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class PluginNotice |
| 7 |
* |
| 8 |
* Handles displaying stories, banners, and rating notices in a WordPress plugin. |
| 9 |
* |
| 10 |
* This class is intended to be used as part of a Composer package and configured dynamically |
| 11 |
* from the consuming project. |
| 12 |
* |
| 13 |
* @package Arraytics\PluginNotice |
| 14 |
*/ |
| 15 |
class PluginNotice |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Plugin slug (used as identifier). |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $plugin_slug; |
| 23 |
|
| 24 |
/** |
| 25 |
* Human-readable plugin name. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $plugin_name; |
| 30 |
|
| 31 |
/** |
| 32 |
* Plugin website URL. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected $plugin_url; |
| 37 |
|
| 38 |
/** |
| 39 |
* Support page URL for the plugin. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected $support_url; |
| 44 |
|
| 45 |
/** |
| 46 |
* Review URL (typically on WordPress.org). |
| 47 |
* |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
protected $review_url; |
| 51 |
|
| 52 |
/** |
| 53 |
* Allowed admin screen(s) where the banner/notice can be shown. |
| 54 |
* |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
protected $allowed_screens; |
| 58 |
|
| 59 |
/** |
| 60 |
* API endpoint for fetching stories. |
| 61 |
* |
| 62 |
* @var string |
| 63 |
*/ |
| 64 |
protected $stories_api_url; |
| 65 |
|
| 66 |
/** |
| 67 |
* API endpoint for fetching banners. |
| 68 |
* |
| 69 |
* @var string |
| 70 |
*/ |
| 71 |
protected $banners_api_url; |
| 72 |
|
| 73 |
/** |
| 74 |
* Array of plugin slugs used to build the active plugins filter string. |
| 75 |
* |
| 76 |
* @var array |
| 77 |
*/ |
| 78 |
protected $base_plugins; |
| 79 |
|
| 80 |
/** |
| 81 |
* Slug of the pro version plugin, if available. |
| 82 |
* |
| 83 |
* @var string|null |
| 84 |
*/ |
| 85 |
protected $pro_plugin_slug = null; |
| 86 |
|
| 87 |
/** |
| 88 |
* Class name to check if the pro plugin is active. |
| 89 |
* |
| 90 |
* @var string|null |
| 91 |
*/ |
| 92 |
protected $pro_class = null; |
| 93 |
|
| 94 |
/** |
| 95 |
* Final computed filter string (comma-separated plugin slugs). |
| 96 |
* |
| 97 |
* @var string |
| 98 |
*/ |
| 99 |
protected $filter_string; |
| 100 |
|
| 101 |
/** |
| 102 |
* Store api url |
| 103 |
* |
| 104 |
* @var string |
| 105 |
*/ |
| 106 |
protected $api_url; |
| 107 |
|
| 108 |
/** |
| 109 |
* PluginNotice constructor. |
| 110 |
* |
| 111 |
* @param array $config { |
| 112 |
* Configuration options. |
| 113 |
* |
| 114 |
* @type string $plugin_slug |
| 115 |
* @type string $plugin_name |
| 116 |
* @type string $plugin_url |
| 117 |
* @type string $support_url |
| 118 |
* @type string $review_url |
| 119 |
* @type string $allowed_screens |
| 120 |
* @type string $stories_api_url |
| 121 |
* @type string $banners_api_url |
| 122 |
* @type array $base_plugins Optional. Default is [$plugin_slug]. |
| 123 |
* @type string|null $pro_plugin_slug Optional. Slug of the pro plugin. |
| 124 |
* @type string|null $pro_class Optional. Class name to check if pro is active. |
| 125 |
* @type string|null $filter_string Optional. Overrides auto-generated filter string. |
| 126 |
* } |
| 127 |
*/ |
| 128 |
public function __construct(array $config) |
| 129 |
{ |
| 130 |
$this->plugin_slug = $config['plugin_slug']; |
| 131 |
$this->plugin_name = $config['plugin_name']; |
| 132 |
$this->plugin_url = $config['plugin_url']; |
| 133 |
$this->support_url = $config['support_url']; |
| 134 |
$this->review_url = $config['review_url']; |
| 135 |
$this->allowed_screens = $config['allowed_screens']; |
| 136 |
$this->stories_api_url = $config['stories_api_url']; |
| 137 |
$this->banners_api_url = $config['banners_api_url']; |
| 138 |
|
| 139 |
$this->base_plugins = isset($config['base_plugins']) ? $config['base_plugins'] : [$this->plugin_slug]; |
| 140 |
$this->pro_plugin_slug = isset($config['pro_plugin_slug']) ? $config['pro_plugin_slug'] : null; |
| 141 |
$this->pro_class = isset($config['pro_class']) ? $config['pro_class'] : null; |
| 142 |
|
| 143 |
$this->filter_string = isset($config['filter_string']) ? $config['filter_string'] : $this->generate_filter_string(); |
| 144 |
$this->api_url = $config['api_url']; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Boot and render all available components (Notice, Stories, Banner, Rating). |
| 149 |
* |
| 150 |
* Should be called during the plugin's initialization phase. |
| 151 |
* |
| 152 |
* @return void |
| 153 |
*/ |
| 154 |
public function boot() |
| 155 |
{ |
| 156 |
// Initialize core notices |
| 157 |
Notice::init(); |
| 158 |
|
| 159 |
// Show stories from remote API |
| 160 |
Stories::instance($this->plugin_slug) |
| 161 |
->set_filter($this->filter_string) |
| 162 |
->set_plugin($this->plugin_name, $this->plugin_url) |
| 163 |
->set_api_url($this->stories_api_url) |
| 164 |
->call(); |
| 165 |
|
| 166 |
// Show banners from remote API |
| 167 |
Banner::instance($this->plugin_slug) |
| 168 |
->set_filter(ltrim($this->filter_string, ',')) |
| 169 |
->set_api_url($this->banners_api_url) |
| 170 |
->set_plugin_screens($this->allowed_screens) |
| 171 |
->call(); |
| 172 |
|
| 173 |
// Show rating prompt |
| 174 |
Rating::instance($this->plugin_slug) |
| 175 |
->set_plugin($this->plugin_name, $this->review_url) |
| 176 |
->set_allowed_screens($this->allowed_screens) |
| 177 |
->set_priority(30) |
| 178 |
->set_first_appear_day(7) |
| 179 |
->set_condition(true) |
| 180 |
->set_api_url($this->api_url) |
| 181 |
->set_support_url($this->support_url) |
| 182 |
->call(); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Generate a comma-separated plugin filter string. |
| 187 |
* |
| 188 |
* Adds the pro plugin slug if the corresponding class exists, |
| 189 |
* and removes `-free-only` suffix if pro is active. |
| 190 |
* |
| 191 |
* @return string |
| 192 |
*/ |
| 193 |
protected function generate_filter_string() |
| 194 |
{ |
| 195 |
$plugins = $this->base_plugins; |
| 196 |
|
| 197 |
if ( |
| 198 |
$this->pro_class && |
| 199 |
(class_exists($this->pro_class) || function_exists($this->pro_class)) && |
| 200 |
$this->pro_plugin_slug |
| 201 |
) { |
| 202 |
$plugins[] = $this->pro_plugin_slug; |
| 203 |
|
| 204 |
// Remove potential 'free-only' entry if pro is active |
| 205 |
$plugins = array_filter($plugins, function ($plugin) { |
| 206 |
return $plugin !== $this->plugin_slug . '-free-only'; |
| 207 |
}); |
| 208 |
} |
| 209 |
|
| 210 |
return implode(',', $plugins); |
| 211 |
} |
| 212 |
} |
| 213 |
|