PluginProbe
Boxzilla – WordPress Popup Builder / 3.1
Boxzilla – WordPress Popup Builder v3.1
3.4.11 3.4.10 3.4.9 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 trunk 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 All 72 releases
boxzilla / src / licensing / class-license-manager.php

class-license-manager.php in Boxzilla – WordPress Popup Builder 3.1, at src/licensing/class-license-manager.php

166 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Boxzilla\Licensing;
4
5 use Boxzilla\Admin\Notices;
6 use Boxzilla\Collection;
7 use Exception;
8
9 class LicenseManager {
10
11 /**
12 * @var array
13 */
14 protected $extensions = array();
15
16 /**
17 * @var License
18 */
19 protected $license;
20
21 /**
22 * @var API
23 */
24 protected $api;
25
26 /**
27 * @var Notices
28 */
29 protected $notices;
30
31 /**
32 * @param Collection $extensions
33 * @param API $api
34 * @param License $license
35 * @param Notices $notices
36 */
37 public function __construct( Collection $extensions, API $api, License $license, Notices $notices ) {
38 $this->extensions = $extensions;
39 $this->license = $license;
40 $this->api = $api;
41 $this->notices = $notices;
42 }
43
44 /**
45 * @param mixed $object
46 * @param string $property
47 * @param string $default
48 *
49 * @return string
50 */
51 protected function get_object_property( $object, $property, $default = '' ) {
52 return isset( $object->$property ) ? $object->$property : $default;
53 }
54
55 /**
56 * @return bool
57 */
58 public function hook() {
59
60 // do nothing if no extensions are registered at this point
61 if( count( $this->extensions ) === 0 ) {
62 return;
63 }
64
65 // hooks
66 add_action( 'boxzilla_after_settings', array( $this, 'show_license_form' ) );
67 add_action( 'admin_notices', array( $this, 'show_notice' ), 1 );
68
69 // listen for activation / deactivation requests
70 $this->listen();
71 }
72
73 /**
74 * Maybe show notice to activate license.
75 */
76 public function show_notice() {
77 global $current_screen;
78
79 if( $this->license->activated ) {
80 return;
81 }
82
83 if( $this->get_object_property( $current_screen, 'post_type' ) !== 'boxzilla-box' ) {
84 return;
85 }
86
87 $plugin = $this->extensions->random();
88 $this->notices->add( sprintf( 'Please <a href="%s">activate your Boxzilla license</a> to use %s.', admin_url( 'edit.php?post_type=boxzilla-box&page=boxzilla-settings' ), '<strong>' . $plugin->name() . '</strong>' ), 'warning' );
89 }
90
91 /**
92 * @return void
93 */
94 protected function listen() {
95
96 // do nothing if not authenticated
97 if( ! current_user_can( 'manage_options' ) ) {
98 return;
99 }
100
101 // nothing to do
102 if( ! isset( $_POST['boxzilla_license_form'] ) ) {
103 return;
104 }
105
106 $action = isset( $_POST['action'] ) ? $_POST['action'] : 'activate';
107 $key_changed = false;
108
109 // did key change or was "activate" button pressed?
110 $new_license_key = sanitize_text_field( $_POST['boxzilla_license_key'] );
111 if( $new_license_key !== $this->license->key ) {
112 $this->license->key = $new_license_key;
113 $key_changed = true;
114 }
115
116 // run actions
117 if( $action === 'deactivate' ) {
118 $this->deactivate_license();
119 } elseif( $action === 'activate' || $key_changed ) {
120 $this->activate_license();
121 }
122 }
123
124 /**
125 * Deactivate the license
126 */
127 protected function deactivate_license() {
128 try {
129 $activation = $this->api->deactivate_license();
130 $this->notices->add( $activation->message, 'info' );
131 } catch( API_Exception $e ) {
132 $this->notices->add( $e->getMessage(), 'warning' );
133 }
134
135 $this->license->activated = false;
136 $this->license->activation_key = '';
137 $this->license->save();
138 }
139
140 /**
141 * Activate the license
142 */
143 protected function activate_license() {
144 try {
145 $activation = $this->api->activate_license();
146 } catch( API_Exception $e ) {
147 $this->notices->add( $e->getMessage(), 'warning' );
148 return;
149 }
150
151 $this->license->activation_key = $activation->key;
152 $this->license->activated = true;
153 $this->license->save();
154
155 $this->notices->add( $activation->message, 'info' );
156 }
157
158 /**
159 * Shows the license form
160 */
161 public function show_license_form() {
162 $license = $this->license;
163 require __DIR__ . '/views/license-form.php';
164 }
165
166 }