| 1 |
<?php |
| 2 |
|
| 3 |
namespace Allex\Module; |
| 4 |
|
| 5 |
use Allex\Container; |
| 6 |
use Twig_Environment; |
| 7 |
|
| 8 |
class Upgrade extends Abstract_Module |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var string |
| 12 |
*/ |
| 13 |
protected $url; |
| 14 |
|
| 15 |
/** |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
protected $plugin_basename; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $plugin_name; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $plugin_title; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var Twig_Environment |
| 32 |
*/ |
| 33 |
protected $twig; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var int |
| 37 |
*/ |
| 38 |
protected $subscription_discount = 20; |
| 39 |
|
| 40 |
/** |
| 41 |
* Upgrade constructor. |
| 42 |
* |
| 43 |
* @param Container $container |
| 44 |
*/ |
| 45 |
public function __construct(Container $container) |
| 46 |
{ |
| 47 |
parent::__construct($container); |
| 48 |
|
| 49 |
$this->plugin_basename = $this->container['PLUGIN_BASENAME']; |
| 50 |
$this->plugin_name = $this->container['PLUGIN_NAME']; |
| 51 |
$this->plugin_title = $this->container['PLUGIN_TITLE']; |
| 52 |
$this->twig = $this->container['twig']; |
| 53 |
$this->assets_base_url = $this->container['ASSETS_BASE_URL']; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Add an Upgrade link to the action links in the plugin list. |
| 58 |
* |
| 59 |
* @param string $addons_page_url |
| 60 |
*/ |
| 61 |
public function init($addons_page_url) |
| 62 |
{ |
| 63 |
$this->url = $addons_page_url; |
| 64 |
|
| 65 |
$this->add_hooks(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Add the hooks. |
| 70 |
*/ |
| 71 |
protected function add_hooks() |
| 72 |
{ |
| 73 |
add_action( |
| 74 |
'plugin_action_links_' . $this->plugin_basename, |
| 75 |
[$this, 'plugin_action_links'], |
| 76 |
999 |
| 77 |
); |
| 78 |
add_action('allex_upgrade_sidebar_ad', [$this, 'render_sidebar_ad']); |
| 79 |
add_filter('allex_upgrade_show_sidebar_ad', [$this, 'filter_allex_upgrade_show_sidebar_ad'], 10, 2); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param array $links |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function plugin_action_links($links) |
| 88 |
{ |
| 89 |
$link = '<a href="' . $this->url . '" target="_blank" class="allex-highlight allex-upgrade-link ' . $this->plugin_name . '">' |
| 90 |
. __('Upgrade', 'allex') . '</a>'; |
| 91 |
|
| 92 |
$links = array_merge($links, [$link]); |
| 93 |
|
| 94 |
return $links; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Echo the sidebar with a form to subscribe for 20% discount. |
| 99 |
* |
| 100 |
* @param string $plugin_name |
| 101 |
*/ |
| 102 |
public function render_sidebar_ad($plugin_name = null) |
| 103 |
{ |
| 104 |
/* |
| 105 |
* --------------------------------------------------------------------- |
| 106 |
* Backward compatibility for users using PublishPress and UpStream with |
| 107 |
* an older version of this library where the $plugin_name param is not |
| 108 |
* sent. We force its value, so the add-ons page keeps working until |
| 109 |
* they update the plugin. |
| 110 |
* |
| 111 |
* @todo: Remove this after a few releases |
| 112 |
* |
| 113 |
* @since 1.16.3 |
| 114 |
*/ |
| 115 |
if (is_null($plugin_name)) { |
| 116 |
$plugin_name = $this->plugin_name; |
| 117 |
} |
| 118 |
/* |
| 119 |
* --------------------------------------------------------------------- |
| 120 |
*/ |
| 121 |
|
| 122 |
// If not related to this plugin, we skip it. |
| 123 |
if ($plugin_name !== $this->plugin_name) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
// @todo: The path have to be relative to plugin, not to the file. Having multiple plugins using this, only the same image will be used. |
| 128 |
$img_url = $this->assets_base_url . '/img/gift-box.png?v=' . $this->container['VERSION']; |
| 129 |
|
| 130 |
/** |
| 131 |
* Get the link for the subscription page. |
| 132 |
* |
| 133 |
* @param array $ad_link |
| 134 |
* @param string $plugin_name |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
$ad_link = apply_filters('allex_upgrade_link', '', $this->plugin_name); |
| 139 |
|
| 140 |
echo $this->twig->render( |
| 141 |
'subscription_ad.twig', |
| 142 |
[ |
| 143 |
'image_src' => $img_url, |
| 144 |
'link' => $ad_link, |
| 145 |
'text' => [ |
| 146 |
'save' => __('Save', 'allex'), |
| 147 |
'discount' => $this->subscription_discount, |
| 148 |
'item' => sprintf(__('off the %s extensions', 'allex'), $this->plugin_title), |
| 149 |
'getit' => __('Get it', 'allex'), |
| 150 |
], |
| 151 |
] |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @param bool $show_sidebar |
| 157 |
* @param string $plugin_name |
| 158 |
* |
| 159 |
* @return bool |
| 160 |
*/ |
| 161 |
public function filter_allex_upgrade_show_sidebar_ad($show_sidebar, $plugin_name = null) |
| 162 |
{ |
| 163 |
if ((defined('DOING_AJAX') && DOING_AJAX) |
| 164 |
|| (defined('DOING_CRON') && DOING_CRON) |
| 165 |
|| !is_admin()) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
if ($plugin_name !== $this->plugin_name) { |
| 170 |
return $show_sidebar; |
| 171 |
} |
| 172 |
|
| 173 |
// Check if we have all add-ons installed. If so, we do not show the sidebar. |
| 174 |
$addons_installed = apply_filters('allex_installed_addons', [], $this->plugin_name); |
| 175 |
|
| 176 |
$show_sidebar = count($addons_installed) === 0; |
| 177 |
|
| 178 |
|
| 179 |
return $show_sidebar; |
| 180 |
} |
| 181 |
} |
| 182 |
|