class-fs-permission-manager.php
24.4 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
<?php
/**
* @package Freemius
* @copyright Copyright (c) 2022, Freemius, Inc.
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
* @since 2.5.1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* This class is responsible for managing the user permissions.
*
* @author Vova Feldman (@svovaf)
* @since 2.5.1
*/
class FS_Permission_Manager {
/**
* @var Freemius
*/
private $_fs;
/**
* @var FS_Storage
*/
private $_storage;
/**
* @var array<number,self>
*/
private static $_instances = array();
const PERMISSION_USER = 'user';
const PERMISSION_SITE = 'site';
const PERMISSION_EVENTS = 'events';
const PERMISSION_ESSENTIALS = 'essentials';
const PERMISSION_DIAGNOSTIC = 'diagnostic';
const PERMISSION_EXTENSIONS = 'extensions';
const PERMISSION_NEWSLETTER = 'newsletter';
/**
* @param Freemius $fs
*
* @return self
*/
static function instance( Freemius $fs ) {
$id = $fs->get_id();
if ( ! isset( self::$_instances[ $id ] ) ) {
self::$_instances[ $id ] = new self( $fs );
}
return self::$_instances[ $id ];
}
/**
* @param Freemius $fs
*/
protected function __construct( Freemius $fs ) {
$this->_fs = $fs;
$this->_storage = FS_Storage::instance( $fs->get_module_type(), $fs->get_slug() );
}
/**
* @return string[]
*/
static function get_all_permission_ids() {
return array(
self::PERMISSION_USER,
self::PERMISSION_SITE,
self::PERMISSION_EVENTS,
self::PERMISSION_ESSENTIALS,
self::PERMISSION_DIAGNOSTIC,
self::PERMISSION_EXTENSIONS,
self::PERMISSION_NEWSLETTER,
);
}
/**
* @return string[]
*/
static function get_api_managed_permission_ids() {
return array(
self::PERMISSION_USER,
self::PERMISSION_SITE,
self::PERMISSION_EXTENSIONS,
);
}
/**
* @param string $permission
*
* @return bool
*/
static function is_supported_permission( $permission ) {
return in_array( $permission, self::get_all_permission_ids() );
}
/**
* @since 2.5.3
*
* @return bool
*/
function is_premium_context() {
return ( $this->_fs->is_premium() || $this->_fs->can_use_premium_code() );
}
/**
* @param bool $is_license_activation
* @param array[] $extra_permissions
*
* @return array[]
*/
function get_permissions( $is_license_activation, array $extra_permissions = array() ) {
return $is_license_activation ?
$this->get_license_activation_permissions( $extra_permissions ) :
$this->get_opt_in_permissions( $extra_permissions );
}
#--------------------------------------------------------------------------------
#region Opt-In Permissions
#--------------------------------------------------------------------------------
/**
* @param array[] $extra_permissions
*
* @return array[]
*/
function get_opt_in_permissions(
array $extra_permissions = array(),
$load_default_from_storage = false,
$is_optional = false
) {
$permissions = array_merge(
$this->get_opt_in_required_permissions( $load_default_from_storage ),
$this->get_opt_in_optional_permissions( $load_default_from_storage, $is_optional ),
$extra_permissions
);
return $this->get_sorted_permissions_by_priority( $permissions );
}
/**
* @param bool $load_default_from_storage
*
* @return array[]
*/
function get_opt_in_required_permissions( $load_default_from_storage = false ) {
return array( $this->get_user_permission( $load_default_from_storage ) );
}
/**
* @param bool $load_default_from_storage
* @param bool $is_optional
*
* @return array[]
*/
function get_opt_in_optional_permissions(
$load_default_from_storage = false,
$is_optional = false
) {
return array_merge(
$this->get_opt_in_diagnostic_permissions( $load_default_from_storage, $is_optional ),
array( $this->get_extensions_permission(
false,
false,
$load_default_from_storage
) )
);
}
/**
* @param bool $load_default_from_storage
* @param bool $is_optional
*
* @return array[]
*/
function get_opt_in_diagnostic_permissions(
$load_default_from_storage = false,
$is_optional = false
) {
// Alias.
$fs = $this->_fs;
$permissions = array();
$permissions[] = $this->get_permission(
self::PERMISSION_SITE,
'admin-links',
$fs->get_text_inline( 'View Basic Website Info', 'permissions-site' ),
$fs->get_text_inline( 'Homepage URL & title, WP & PHP versions, and site language', 'permissions-site_desc' ),
sprintf(
/* translators: %s: 'Plugin' or 'Theme' */
$fs->get_text_inline( 'To provide additional functionality that\'s relevant to your website, avoid WordPress or PHP version incompatibilities that can break your website, and recognize which languages & regions the %s should be translated and tailored to.', 'permissions-site_tooltip' ),
$fs->get_module_label( true )
),
10,
$is_optional,
true,
$load_default_from_storage
);
$permissions[] = $this->get_permission(
self::PERMISSION_EVENTS,
'admin-' . ( $fs->is_plugin() ? 'plugins' : 'appearance' ),
sprintf( $fs->get_text_inline( 'View Basic %s Info', 'permissions-events' ), $fs->get_module_label() ),
sprintf(
/* translators: %s: 'Plugin' or 'Theme' */
$fs->get_text_inline( 'Current %s & SDK versions, and if active or uninstalled', 'permissions-events_desc' ),
$fs->get_module_label( true )
),
'',
20,
$is_optional,
true,
$load_default_from_storage
);
return $permissions;
}
#endregion
#--------------------------------------------------------------------------------
#region License Activation Permissions
#--------------------------------------------------------------------------------
/**
* @param array[] $extra_permissions
*
* @return array[]
*/
function get_license_activation_permissions(
array $extra_permissions = array(),
$include_optional_label = true
) {
$permissions = array_merge(
$this->get_license_required_permissions(),
$this->get_license_optional_permissions( $include_optional_label ),
$extra_permissions
);
return $this->get_sorted_permissions_by_priority( $permissions );
}
/**
* @param bool $load_default_from_storage
*
* @return array[]
*/
function get_license_required_permissions( $load_default_from_storage = false ) {
// Alias.
$fs = $this->_fs;
$permissions = array();
$permissions[] = $this->get_permission(
self::PERMISSION_ESSENTIALS,
'admin-links',
$fs->get_text_inline( 'View License Essentials', 'permissions-essentials' ),
$fs->get_text_inline(
sprintf(
/* translators: %s: 'Plugin' or 'Theme' */
'Homepage URL, %s version, SDK version',
$fs->get_module_label()
),
'permissions-essentials_desc'
),
sprintf(
/* translators: %s: 'Plugin' or 'Theme' */
$fs->get_text_inline( 'To let you manage & control where the license is activated and ensure %s security & feature updates are only delivered to websites you authorize.', 'permissions-essentials_tooltip' ),
$fs->get_module_label( true )
),
10,
false,
true,
$load_default_from_storage
);
$permissions[] = $this->get_permission(
self::PERMISSION_EVENTS,
'admin-' . ( $fs->is_plugin() ? 'plugins' : 'appearance' ),
sprintf( $fs->get_text_inline( 'View %s State', 'permissions-events' ), $fs->get_module_label() ),
sprintf(
/* translators: %s: 'Plugin' or 'Theme' */
$fs->get_text_inline( 'Is active, deactivated, or uninstalled', 'permissions-events_desc-paid' ),
$fs->get_module_label( true )
),
sprintf( $fs->get_text_inline( 'So you can reuse the license when the %s is no longer active.', 'permissions-events_tooltip' ), $fs->get_module_label( true ) ),
20,
false,
true,
$load_default_from_storage
);
return $permissions;
}
/**
* @return array[]
*/
function get_license_optional_permissions(
$include_optional_label = false,
$load_default_from_storage = false
) {
return array(
$this->get_diagnostic_permission( $include_optional_label, $load_default_from_storage ),
$this->get_extensions_permission( true, $include_optional_label, $load_default_from_storage ),
);
}
/**
* @param bool $include_optional_label
* @param bool $load_default_from_storage
*
* @return array
*/
function get_diagnostic_permission(
$include_optional_label = false,
$load_default_from_storage = false
) {
return $this->get_permission(
self::PERMISSION_DIAGNOSTIC,
'wordpress-alt',
$this->_fs->get_text_inline( 'View Diagnostic Info', 'permissions-diagnostic' ) . ( $include_optional_label ? ' (' . $this->_fs->get_text_inline( 'optional' ) . ')' : '' ),
$this->_fs->get_text_inline( 'WordPress & PHP versions, site language & title', 'permissions-diagnostic_desc' ),
sprintf(
/* translators: %s: 'Plugin' or 'Theme' */
$this->_fs->get_text_inline( 'To avoid breaking your website due to WordPress or PHP version incompatibilities, and recognize which languages & regions the %s should be translated and tailored to.', 'permissions-diagnostic_tooltip' ),
$this->_fs->get_module_label( true )
),
25,
true,
true,
$load_default_from_storage
);
}
#endregion
#--------------------------------------------------------------------------------
#region Common Permissions
#--------------------------------------------------------------------------------
/**
* @param bool $is_license_activation
* @param bool $include_optional_label
* @param bool $load_default_from_storage
*
* @return array
*/
function get_extensions_permission(
$is_license_activation,
$include_optional_label = false,
$load_default_from_storage = false
) {
$is_on_by_default = ! $is_license_activation;
return $this->get_permission(
self::PERMISSION_EXTENSIONS,
'block-default',
$this->_fs->get_text_inline( 'View Plugins & Themes List', 'permissions-extensions' ) . ( $is_license_activation ? ( $include_optional_label ? ' (' . $this->_fs->get_text_inline( 'optional' ) . ')' : '' ) : '' ),
$this->_fs->get_text_inline( 'Names, slugs, versions, and if active or not', 'permissions-extensions_desc' ),
$this->_fs->get_text_inline( 'To ensure compatibility and avoid conflicts with your installed plugins and themes.', 'permissions-events_tooltip' ),
25,
true,
$is_on_by_default,
$load_default_from_storage
);
}
/**
* @param bool $load_default_from_storage
*
* @return array
*/
function get_user_permission( $load_default_from_storage = false ) {
return $this->get_permission(
self::PERMISSION_USER,
'admin-users',
$this->_fs->get_text_inline( 'View Basic Profile Info', 'permissions-profile' ),
$this->_fs->get_text_inline( 'Your WordPress user\'s: first & last name, and email address', 'permissions-profile_desc' ),
$this->_fs->get_text_inline( 'Never miss important updates, get security warnings before they become public knowledge, and receive notifications about special offers and awesome new features.', 'permissions-profile_tooltip' ),
5,
false,
true,
$load_default_from_storage
);
}
#endregion
#--------------------------------------------------------------------------------
#region Optional Permissions
#--------------------------------------------------------------------------------
/**
* @return array[]
*/
function get_newsletter_permission() {
return $this->get_permission(
self::PERMISSION_NEWSLETTER,
'email-alt',
$this->_fs->get_text_inline( 'Newsletter', 'permissions-newsletter' ),
$this->_fs->get_text_inline( 'Updates, announcements, marketing, no spam', 'permissions-newsletter_desc' ),
'',
15
);
}
#endregion
#--------------------------------------------------------------------------------
#region Permissions Storage
#--------------------------------------------------------------------------------
/**
* @param int|null $blog_id
*
* @return bool
*/
function is_extensions_tracking_allowed( $blog_id = null ) {
return $this->is_permission_allowed( self::PERMISSION_EXTENSIONS, ! $this->_fs->is_premium(), $blog_id );
}
/**
* @param int|null $blog_id
*
* @return bool
*/
function is_essentials_tracking_allowed( $blog_id = null ) {
return $this->is_permission_allowed( self::PERMISSION_ESSENTIALS, true, $blog_id );
}
/**
* @param bool $default
*
* @return bool
*/
function is_diagnostic_tracking_allowed( $default = true ) {
return $this->is_premium_context() ?
$this->is_permission_allowed( self::PERMISSION_DIAGNOSTIC, $default ) :
$this->is_permission_allowed( self::PERMISSION_SITE, $default );
}
/**
* @param int|null $blog_id
*
* @return bool
*/
function is_homepage_url_tracking_allowed( $blog_id = null ) {
return $this->is_permission_allowed( $this->get_site_permission_name(), true, $blog_id );
}
/**
* @param int|null $blog_id
*
* @return bool
*/
function update_site_tracking( $is_enabled, $blog_id = null, $only_if_not_set = false ) {
$permissions = $this->get_site_tracking_permission_names();
$result = true;
foreach ( $permissions as $permission ) {
if ( ! $only_if_not_set || ! $this->is_permission_set( $permission, $blog_id ) ) {
$result = ( $result && $this->update_permission_tracking_flag( $permission, $is_enabled, $blog_id ) );
}
}
return $result;
}
/**
* @param string $permission
* @param bool $default
* @param int|null $blog_id
*
* @return bool
*/
function is_permission_allowed( $permission, $default = false, $blog_id = null ) {
if ( ! self::is_supported_permission( $permission ) ) {
return $default;
}
return $this->is_permission( $permission, true, $blog_id );
}
/**
* @param string $permission
* @param bool $is_allowed
* @param int|null $blog_id
*
* @return bool
*/
function is_permission( $permission, $is_allowed, $blog_id = null ) {
if ( ! self::is_supported_permission( $permission ) ) {
return false;
}
$tag = "is_{$permission}_tracking_allowed";
return ( $is_allowed === $this->_fs->apply_filters(
$tag,
$this->_storage->get(
$tag,
$this->get_permission_default( $permission ),
$blog_id,
FS_Storage::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED
)
) );
}
/**
* @param string $permission
* @param int|null $blog_id
*
* @return bool
*/
function is_permission_set( $permission, $blog_id = null ) {
$tag = "is_{$permission}_tracking_allowed";
$permission = $this->_storage->get(
$tag,
null,
$blog_id,
FS_Storage::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED
);
return is_bool( $permission );
}
/**
* @param string[] $permissions
* @param bool $is_allowed
*
* @return bool `true` if all given permissions are in sync with `$is_allowed`.
*/
function are_permissions( $permissions, $is_allowed, $blog_id = null ) {
foreach ( $permissions as $permission ) {
if ( ! $this->is_permission( $permission, $is_allowed, $blog_id ) ) {
return false;
}
}
return true;
}
/**
* @param string $permission
* @param bool $is_enabled
* @param int|null $blog_id
*
* @return bool `false` if permission not supported or `$is_enabled` is not a boolean.
*/
function update_permission_tracking_flag( $permission, $is_enabled, $blog_id = null ) {
if ( is_bool( $is_enabled ) && self::is_supported_permission( $permission ) ) {
$this->_storage->store(
"is_{$permission}_tracking_allowed",
$is_enabled,
$blog_id,
FS_Storage::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED
);
return true;
}
return false;
}
/**
* @param array<string,bool> $permissions
*/
function update_permissions_tracking_flag( $permissions ) {
foreach ( $permissions as $permission => $is_enabled ) {
$this->update_permission_tracking_flag( $permission, $is_enabled );
}
}
#endregion
/**
* @param string $permission
*
* @return bool
*/
function get_permission_default( $permission ) {
if (
$this->_fs->is_premium() &&
self::PERMISSION_EXTENSIONS === $permission
) {
return false;
}
// All permissions except for the extensions in paid version are on by default when the user opts in to usage tracking.
return true;
}
/**
* @return string
*/
function get_site_permission_name() {
return $this->is_premium_context() ?
self::PERMISSION_ESSENTIALS :
self::PERMISSION_SITE;
}
/**
* @return string[]
*/
function get_site_tracking_permission_names() {
return $this->is_premium_context() ?
array(
FS_Permission_Manager::PERMISSION_ESSENTIALS,
FS_Permission_Manager::PERMISSION_EVENTS,
) :
array( FS_Permission_Manager::PERMISSION_SITE );
}
#--------------------------------------------------------------------------------
#region Rendering
#--------------------------------------------------------------------------------
/**
* @param array $permission
*/
function render_permission( array $permission ) {
fs_require_template( 'connect/permission.php', $permission );
}
/**
* @param array $permissions_group
*/
function render_permissions_group( array $permissions_group ) {
$permissions_group[ 'fs' ] = $this->_fs;
fs_require_template( 'connect/permissions-group.php', $permissions_group );
}
function require_permissions_js() {
fs_require_once_template( 'js/permissions.php', $params );
}
#endregion
#--------------------------------------------------------------------------------
#region Helper Methods
#--------------------------------------------------------------------------------
/**
* @param string $id
* @param string $dashicon
* @param string $label
* @param string $desc
* @param string $tooltip
* @param int $priority
* @param bool $is_optional
* @param bool $is_on_by_default
* @param bool $load_from_storage
*
* @return array
*/
private function get_permission(
$id,
$dashicon,
$label,
$desc,
$tooltip = '',
$priority = 10,
$is_optional = false,
$is_on_by_default = true,
$load_from_storage = false
) {
$is_on = $load_from_storage ?
$this->is_permission_allowed( $id, $is_on_by_default ) :
$is_on_by_default;
return array(
'id' => $id,
'icon-class' => $this->_fs->apply_filters( "permission_{$id}_icon", "dashicons dashicons-{$dashicon}" ),
'label' => $this->_fs->apply_filters( "permission_{$id}_label", $label ),
'tooltip' => $this->_fs->apply_filters( "permission_{$id}_tooltip", $tooltip ),
'desc' => $this->_fs->apply_filters( "permission_{$id}_desc", $desc ),
'priority' => $this->_fs->apply_filters( "permission_{$id}_priority", $priority ),
'optional' => $is_optional,
'default' => $this->_fs->apply_filters( "permission_{$id}_default", $is_on ),
);
}
/**
* @param array $permissions
*
* @return array[]
*/
private function get_sorted_permissions_by_priority( array $permissions ) {
// Allow filtering of the permissions list.
$permissions = $this->_fs->apply_filters( 'permission_list', $permissions );
// Sort by priority.
uasort( $permissions, 'fs_sort_by_priority' );
return $permissions;
}
#endregion
}