1 | <?php namespace EmailLog\Addon\License; |
||
17 | final class Licenser implements Loadie { |
||
18 | |||
19 | /** |
||
20 | * Bundle License object. |
||
21 | * |
||
22 | * @var \EmailLog\Addon\License\BundleLicense |
||
23 | */ |
||
24 | private $bundle_license; |
||
25 | |||
26 | /** |
||
27 | * List of Add-on updaters. |
||
28 | * |
||
29 | * @var \EmailLog\Addon\API\EDDUpdater[] |
||
30 | */ |
||
31 | private $updaters = array(); |
||
32 | |||
33 | /** |
||
34 | * List of add-ons. |
||
35 | * |
||
36 | * @var \EmailLog\Addon\AddonList |
||
37 | */ |
||
38 | private $addon_list; |
||
39 | |||
40 | /** |
||
41 | * Licenser constructor. |
||
42 | * If the bundle_license object is not passed a new object is created. |
||
43 | * If the addon_list object is not passed a new object is created. |
||
44 | * |
||
45 | * @param \EmailLog\Addon\License\BundleLicense|null $bundle_license Optional. Bundle License. |
||
46 | * @param \EmailLog\Addon\AddonList|null $addon_list Optional. Add-on List. |
||
47 | */ |
||
48 | public function __construct( $bundle_license = null, $addon_list = null ) { |
||
60 | |||
61 | /** |
||
62 | * Load all Licenser related hooks. |
||
63 | * |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | public function load() { |
||
67 | $this->bundle_license->load(); |
||
68 | |||
69 | add_action( 'el_before_addon_list', array( $this, 'render_bundle_license_form' ) ); |
||
70 | add_action( 'el_before_logs_list_table', array( $this, 'render_more_fields_addon_upsell_message' ) ); |
||
71 | |||
72 | add_action( 'el_bundle_license_activate', array( $this, 'activate_bundle_license' ) ); |
||
73 | add_action( 'el_bundle_license_deactivate', array( $this, 'deactivate_bundle_license' ) ); |
||
74 | |||
75 | add_action( 'el_license_activate', array( $this, 'activate_addon_license' ) ); |
||
76 | add_action( 'el_license_deactivate', array( $this, 'deactivate_addon_license' ) ); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Add an Add-on Updater. |
||
81 | * |
||
82 | * @param \EmailLog\Addon\API\EDDUpdater $updater Add-on Updater. |
||
83 | */ |
||
84 | public function add_updater( $updater ) { |
||
85 | if ( $updater instanceof EDDUpdater ) { |
||
86 | $this->updaters[ $updater->get_slug() ] = $updater; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get list of add-ons. |
||
92 | * |
||
93 | * @return \EmailLog\Addon\AddonList Add-on List. |
||
94 | */ |
||
95 | public function get_addon_list() { |
||
96 | return $this->addon_list; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Render the Bundle License Form. |
||
101 | */ |
||
102 | public function render_bundle_license_form() { |
||
103 | $action = 'el_bundle_license_activate'; |
||
104 | $action_text = __( 'Activate', 'email-log' ); |
||
105 | $button_class = 'button-primary'; |
||
106 | $expires = ''; |
||
107 | |||
108 | if ( $this->is_bundle_license_valid() ) { |
||
109 | $action = 'el_bundle_license_deactivate'; |
||
110 | $action_text = __( 'Deactivate', 'email-log' ); |
||
111 | $button_class = ''; |
||
112 | $expiry_date = date( 'F d, Y', strtotime( $this->get_bundle_license_expiry_date() ) ); |
||
113 | $expires = sprintf( __( 'Your license expires on %s', 'email-log' ), $expiry_date ); |
||
114 | } |
||
115 | ?> |
||
116 | |||
117 | <div class="bundle-license"> |
||
118 | <?php if ( ! $this->is_bundle_license_valid() ) : ?> |
||
119 | <p class="notice notice-warning"> |
||
120 | <?php |
||
121 | printf( |
||
122 | __( "Enter your license key to activate add-ons. If you don't have a license, then you can <a href='%s' target='_blank'>buy it</a>", 'email-log' ), |
||
123 | 'https://wpemaillog.com/store/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=notice&utm_content=buy-it' |
||
124 | ); |
||
125 | ?> |
||
126 | </p> |
||
127 | <?php endif; ?> |
||
128 | |||
129 | <form method="post"> |
||
130 | <input type="text" name="el-license" class="el-license" size="40" |
||
131 | title="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>" |
||
132 | placeholder="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>" |
||
133 | value="<?php echo esc_attr( $this->bundle_license->get_license_key() ); ?>"> |
||
134 | |||
135 | <input type="submit" class="button button-large <?php echo sanitize_html_class( $button_class ); ?>" |
||
136 | value="<?php echo esc_attr( $action_text ); ?>"> |
||
137 | |||
138 | <p class="expires"><?php echo esc_html( $expires ); ?></p> |
||
139 | |||
140 | <input type="hidden" name="el-action" value="<?php echo esc_attr( $action ); ?>"> |
||
141 | |||
142 | <?php wp_nonce_field( $action, $action . '_nonce' ); ?> |
||
143 | </form> |
||
144 | </div> |
||
145 | <?php |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Renders Upsell message for More Fields add-on. |
||
150 | * |
||
151 | * @since 2.2.5 |
||
152 | */ |
||
153 | public function render_more_fields_addon_upsell_message() { |
||
154 | echo '<span id = "el-pro-msg">'; |
||
155 | _e( 'Additional fields are available through More Fields add-on. ', 'email-log' ); |
||
156 | |||
157 | if ( $this->is_bundle_license_valid() ) { |
||
158 | echo '<a href="admin.php?page=email-log-addons">'; |
||
159 | _e( 'Install it', 'email-log' ); |
||
160 | echo '</a>'; |
||
161 | } else { |
||
162 | echo '<a href="https://wpemaillog.com/addons/more-fields/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=inline&utm_content=mf" style="color:red">'; |
||
163 | _e( 'Buy Now', 'email-log' ); |
||
164 | echo '</a>'; |
||
165 | } |
||
166 | |||
167 | echo '</span>'; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Activate Bundle License. |
||
172 | * |
||
173 | * @param array $request Request Object. |
||
174 | */ |
||
175 | public function activate_bundle_license( $request ) { |
||
176 | $license_key = sanitize_text_field( $request['el-license'] ); |
||
177 | |||
178 | $this->bundle_license->set_license_key( $license_key ); |
||
179 | |||
180 | try { |
||
181 | $this->bundle_license->activate(); |
||
182 | $message = __( 'Your license has been activated. You can now install add-ons, will receive automatic updates and access to email support.', 'email-log' ); |
||
183 | $type = 'updated'; |
||
184 | } catch ( \Exception $e ) { |
||
185 | $message = $e->getMessage(); |
||
186 | $type = 'error'; |
||
187 | } |
||
188 | |||
189 | add_settings_error( 'bundle-license', 'bundle-license', $message, $type ); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Deactivate Bundle License. |
||
194 | */ |
||
195 | public function deactivate_bundle_license() { |
||
196 | try { |
||
197 | $this->bundle_license->deactivate(); |
||
198 | $message = __( 'Your license has been deactivated. You will not receive automatic updates.', 'email-log' ); |
||
199 | $type = 'updated'; |
||
200 | } catch ( \Exception $e ) { |
||
201 | $message = $e->getMessage(); |
||
202 | $type = 'error'; |
||
203 | } |
||
204 | |||
205 | add_settings_error( 'bundle-license', 'bundle-license', $message, $type ); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Is the bundle license valid? |
||
210 | * |
||
211 | * @return bool True, if Bundle License is active, False otherwise. |
||
212 | */ |
||
213 | public function is_bundle_license_valid() { |
||
214 | return $this->bundle_license->is_valid(); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Get the expiry date of the Bundle License. |
||
219 | * |
||
220 | * @return false|string Expiry date, False if license is not valid. |
||
221 | */ |
||
222 | protected function get_bundle_license_expiry_date() { |
||
225 | |||
226 | /** |
||
227 | * Activate individual add-on License. |
||
228 | * |
||
229 | * @param array $request Request Array. |
||
230 | */ |
||
231 | public function activate_addon_license( $request ) { |
||
232 | $license_key = sanitize_text_field( $request['el-license'] ); |
||
233 | $addon_name = sanitize_text_field( $request['el-addon'] ); |
||
252 | |||
253 | /** |
||
254 | * Deactivate individual add-on License. |
||
255 | * |
||
256 | * @param array $request Request Array. |
||
257 | */ |
||
258 | public function deactivate_addon_license( $request ) { |
||
279 | |||
280 | /** |
||
281 | * Get the license key of an add-on. |
||
282 | * |
||
283 | * @param string $addon_name Addon. |
||
284 | * |
||
285 | * @return bool|string License key if found, False otherwise. |
||
286 | */ |
||
287 | public function get_addon_license_key( $addon_name ) { |
||
300 | |||
301 | /** |
||
302 | * Get the Download URL of an add-on. |
||
303 | * |
||
304 | * @param string $addon_slug Add-on slug. |
||
305 | * |
||
306 | * @return string Download URL. |
||
307 | */ |
||
308 | public function get_addon_download_url( $addon_slug ) { |
||
315 | } |
||
316 |