forked from dkoryto/License-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-product.php
More file actions
320 lines (271 loc) · 7.12 KB
/
class-product.php
File metadata and controls
320 lines (271 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
if ( ! class_exists( 'Yoast_Product_v2', false ) ) {
/**
* Class Yoast_Product
*/
class Yoast_Product_v2 {
/**
* @var string The URL of the shop running the EDD API.
*/
protected $api_url;
/**
* @var string The item name in the EDD shop.
*/
protected $item_name;
/**
* @var string The theme slug or plugin file
*/
protected $slug;
/**
* @var string The version number of the item
*/
protected $version;
/**
* @var string The absolute url on which users can purchase a license
*/
protected $item_url;
/**
* @var string Absolute admin URL on which users can enter their license key.
*/
protected $license_page_url;
/**
* @var string The text domain used for translating strings
*/
protected $text_domain;
/**
* @var string The item author
*/
protected $author;
/**
* @var string Relative file path to the plugin.
*/
protected $file;
/** @var int Product ID in backend system for quick lookup */
protected $product_id;
/** @var string URL referring to the extension page */
protected $extension_url;
/**
* Yoast_Product constructor.
*
* @param string $api_url The URL of the shop running the EDD API.
* @param string $item_name The item name in the EDD shop.
* @param string $slug The slug of the plugin, for shiny updates this needs to be a valid HTML id.
* @param string $version The version number of the item.
* @param string $item_url The absolute url on which users can purchase a license.
* @param string $license_page_url Absolute admin URL on which users can enter their license key.
* @param string $text_domain The text domain used for translating strings.
* @param string $author The item author.
* @param string $file The relative file path to the plugin.
* @param int $product_id The ID of the product in the backend system.
*/
public function __construct( $api_url, $item_name, $slug, $version, $item_url = '', $license_page_url = '#', $text_domain = 'yoast', $author = 'Yoast', $file = '', $product_id = 0 ) {
$this->set_api_url( $api_url );
$this->set_item_name( $item_name );
$this->set_slug( $slug );
$this->set_version( $version );
$this->set_item_url( $item_url );
$this->set_text_domain( $text_domain );
$this->set_author( $author );
$this->set_file( $file );
$this->set_product_id( $product_id );
$this->set_license_page_url( $license_page_url );
}
/**
* @param string $api_url
*/
public function set_api_url( $api_url ) {
$this->api_url = $api_url;
}
/**
* @return string
*/
public function get_api_url() {
return $this->api_url;
}
/**
* @param string $author
*/
public function set_author( $author ) {
$this->author = $author;
}
/**
* @return string
*/
public function get_author() {
return $this->author;
}
/**
* @param string $item_name
*/
public function set_item_name( $item_name ) {
$this->item_name = $item_name;
}
/**
* @return string
*/
public function get_item_name() {
return $this->item_name;
}
/**
* @param string $item_url
*/
public function set_item_url( $item_url ) {
if ( empty( $item_url ) ) {
$item_url = $this->api_url;
}
$this->item_url = $item_url;
}
/**
* @return string
*/
public function get_item_url() {
return $this->item_url;
}
/**
* @param string $license_page_url
*/
public function set_license_page_url( $license_page_url ) {
if ( is_admin() && is_multisite() ) {
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( is_plugin_active_for_network( $this->get_file() ) ) {
$this->license_page_url = network_admin_url( $license_page_url );
return;
}
}
$this->license_page_url = admin_url( $license_page_url );
}
/**
* @return string
*/
public function get_license_page_url() {
return $this->license_page_url;
}
/**
* @param string $slug
*/
public function set_slug( $slug ) {
$this->slug = $slug;
}
/**
* @return string
*/
public function get_slug() {
return $this->slug;
}
/**
* Returns the dirname of the slug and limits it to 15 chars
*
* @return string
*/
public function get_transient_prefix() {
return substr( md5( $this->file ), 0, 15 );
}
/**
* @param string $text_domain
*/
public function set_text_domain( $text_domain ) {
$this->text_domain = $text_domain;
}
/**
* @return string
*/
public function get_text_domain() {
return $this->text_domain;
}
/**
* @param string $version
*/
public function set_version( $version ) {
$this->version = $version;
}
/**
* @return string
*/
public function get_version() {
return $this->version;
}
/**
* Returns the file path relative to the plugins folder
*
* @return string
*/
public function get_file() {
/*
* Fall back to the slug for BC reasons.
*
* We used to pass the file to the slug field, but this isn't supported with the shiny updates in WordPress.
* WordPress uses the slug in the HTML as an ID and a slash isn't a valid
*/
return empty( $this->file ) ? $this->slug : $this->file;
}
/**
* Sets the file path relative to the plugins folder
*
* @param string $file Relative file path to the plugin.
*/
public function set_file( $file ) {
$this->file = $file;
}
/**
* Return the Product ID
*
* @return int
*/
public function get_product_id() {
return $this->product_id;
}
/**
* Set the product ID
*
* @param int $product_id Product ID to set.
*/
public function set_product_id( $product_id ) {
$this->product_id = (int) $product_id;
}
/**
* Gets a Google Analytics Campaign url for this product
*
* @param string $link_identifier
*
* @return string The full URL
*/
public function get_tracking_url( $link_identifier = '' ) {
return $this->add_campaign_attributes( $this->get_item_url(), $link_identifier );
}
/**
* Returns the extension url if set, otherwise it will be the tracking url.
*
* @param string $link_identifier
*
* @return string
*/
public function get_extension_url( $link_identifier = '' ) {
if ( $this->extension_url ) {
return $this->add_campaign_attributes( $this->extension_url, $link_identifier );
}
return $this->get_tracking_url( $link_identifier );
}
/**
* Sets the extension url.
*
* @param string $extension_url
*/
public function set_extension_url( $extension_url ) {
$this->extension_url = $extension_url;
}
private function add_campaign_attributes( $url, $link_identifier ) {
$tracking_vars = array(
'utm_campaign' => $this->get_item_name() . ' licensing',
'utm_medium' => 'link',
'utm_source' => $this->get_item_name(),
'utm_content' => $link_identifier,
);
// URL encode tracking vars.
$tracking_vars = urlencode_deep( $tracking_vars );
$query_string = build_query( $tracking_vars );
return $url . '#' . $query_string;
}
}
}