delete-old-posts-programmatically
/
freemius
/
includes
/
managers
/
class-fs-permission-manager.php
class-fs-admin-menu-manager.php
1 year ago
class-fs-admin-notice-manager.php
1 year ago
class-fs-cache-manager.php
5 years ago
class-fs-checkout-manager.php
1 year ago
class-fs-clone-manager.php
11 months ago
class-fs-contact-form-manager.php
1 year ago
class-fs-debug-manager.php
1 year ago
class-fs-gdpr-manager.php
3 years ago
class-fs-key-value-storage.php
3 years ago
class-fs-license-manager.php
5 years ago
class-fs-option-manager.php
3 years ago
class-fs-permission-manager.php
3 years ago
class-fs-plan-manager.php
2 years ago
class-fs-plugin-manager.php
3 years ago
index.php
5 years ago
class-fs-permission-manager.php
707 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package Freemius |
| 4 | * @copyright Copyright (c) 2022, Freemius, Inc. |
| 5 | * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 |
| 6 | * @since 2.5.1 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * This class is responsible for managing the user permissions. |
| 15 | * |
| 16 | * @author Vova Feldman (@svovaf) |
| 17 | * @since 2.5.1 |
| 18 | */ |
| 19 | class FS_Permission_Manager { |
| 20 | /** |
| 21 | * @var Freemius |
| 22 | */ |
| 23 | private $_fs; |
| 24 | /** |
| 25 | * @var FS_Storage |
| 26 | */ |
| 27 | private $_storage; |
| 28 | |
| 29 | /** |
| 30 | * @var array<number,self> |
| 31 | */ |
| 32 | private static $_instances = array(); |
| 33 | |
| 34 | const PERMISSION_USER = 'user'; |
| 35 | const PERMISSION_SITE = 'site'; |
| 36 | const PERMISSION_EVENTS = 'events'; |
| 37 | const PERMISSION_ESSENTIALS = 'essentials'; |
| 38 | const PERMISSION_DIAGNOSTIC = 'diagnostic'; |
| 39 | const PERMISSION_EXTENSIONS = 'extensions'; |
| 40 | const PERMISSION_NEWSLETTER = 'newsletter'; |
| 41 | |
| 42 | /** |
| 43 | * @param Freemius $fs |
| 44 | * |
| 45 | * @return self |
| 46 | */ |
| 47 | static function instance( Freemius $fs ) { |
| 48 | $id = $fs->get_id(); |
| 49 | |
| 50 | if ( ! isset( self::$_instances[ $id ] ) ) { |
| 51 | self::$_instances[ $id ] = new self( $fs ); |
| 52 | } |
| 53 | |
| 54 | return self::$_instances[ $id ]; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param Freemius $fs |
| 59 | */ |
| 60 | protected function __construct( Freemius $fs ) { |
| 61 | $this->_fs = $fs; |
| 62 | $this->_storage = FS_Storage::instance( $fs->get_module_type(), $fs->get_slug() ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return string[] |
| 67 | */ |
| 68 | static function get_all_permission_ids() { |
| 69 | return array( |
| 70 | self::PERMISSION_USER, |
| 71 | self::PERMISSION_SITE, |
| 72 | self::PERMISSION_EVENTS, |
| 73 | self::PERMISSION_ESSENTIALS, |
| 74 | self::PERMISSION_DIAGNOSTIC, |
| 75 | self::PERMISSION_EXTENSIONS, |
| 76 | self::PERMISSION_NEWSLETTER, |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @return string[] |
| 82 | */ |
| 83 | static function get_api_managed_permission_ids() { |
| 84 | return array( |
| 85 | self::PERMISSION_USER, |
| 86 | self::PERMISSION_SITE, |
| 87 | self::PERMISSION_EXTENSIONS, |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param string $permission |
| 93 | * |
| 94 | * @return bool |
| 95 | */ |
| 96 | static function is_supported_permission( $permission ) { |
| 97 | return in_array( $permission, self::get_all_permission_ids() ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @since 2.5.3 |
| 102 | * |
| 103 | * @return bool |
| 104 | */ |
| 105 | function is_premium_context() { |
| 106 | return ( $this->_fs->is_premium() || $this->_fs->can_use_premium_code() ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param bool $is_license_activation |
| 111 | * @param array[] $extra_permissions |
| 112 | * |
| 113 | * @return array[] |
| 114 | */ |
| 115 | function get_permissions( $is_license_activation, array $extra_permissions = array() ) { |
| 116 | return $is_license_activation ? |
| 117 | $this->get_license_activation_permissions( $extra_permissions ) : |
| 118 | $this->get_opt_in_permissions( $extra_permissions ); |
| 119 | } |
| 120 | |
| 121 | #-------------------------------------------------------------------------------- |
| 122 | #region Opt-In Permissions |
| 123 | #-------------------------------------------------------------------------------- |
| 124 | |
| 125 | /** |
| 126 | * @param array[] $extra_permissions |
| 127 | * |
| 128 | * @return array[] |
| 129 | */ |
| 130 | function get_opt_in_permissions( |
| 131 | array $extra_permissions = array(), |
| 132 | $load_default_from_storage = false, |
| 133 | $is_optional = false |
| 134 | ) { |
| 135 | $permissions = array_merge( |
| 136 | $this->get_opt_in_required_permissions( $load_default_from_storage ), |
| 137 | $this->get_opt_in_optional_permissions( $load_default_from_storage, $is_optional ), |
| 138 | $extra_permissions |
| 139 | ); |
| 140 | |
| 141 | return $this->get_sorted_permissions_by_priority( $permissions ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @param bool $load_default_from_storage |
| 146 | * |
| 147 | * @return array[] |
| 148 | */ |
| 149 | function get_opt_in_required_permissions( $load_default_from_storage = false ) { |
| 150 | return array( $this->get_user_permission( $load_default_from_storage ) ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @param bool $load_default_from_storage |
| 155 | * @param bool $is_optional |
| 156 | * |
| 157 | * @return array[] |
| 158 | */ |
| 159 | function get_opt_in_optional_permissions( |
| 160 | $load_default_from_storage = false, |
| 161 | $is_optional = false |
| 162 | ) { |
| 163 | return array_merge( |
| 164 | $this->get_opt_in_diagnostic_permissions( $load_default_from_storage, $is_optional ), |
| 165 | array( $this->get_extensions_permission( |
| 166 | false, |
| 167 | false, |
| 168 | $load_default_from_storage |
| 169 | ) ) |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @param bool $load_default_from_storage |
| 175 | * @param bool $is_optional |
| 176 | * |
| 177 | * @return array[] |
| 178 | */ |
| 179 | function get_opt_in_diagnostic_permissions( |
| 180 | $load_default_from_storage = false, |
| 181 | $is_optional = false |
| 182 | ) { |
| 183 | // Alias. |
| 184 | $fs = $this->_fs; |
| 185 | |
| 186 | $permissions = array(); |
| 187 | |
| 188 | $permissions[] = $this->get_permission( |
| 189 | self::PERMISSION_SITE, |
| 190 | 'admin-links', |
| 191 | $fs->get_text_inline( 'View Basic Website Info', 'permissions-site' ), |
| 192 | $fs->get_text_inline( 'Homepage URL & title, WP & PHP versions, and site language', 'permissions-site_desc' ), |
| 193 | sprintf( |
| 194 | /* translators: %s: 'Plugin' or 'Theme' */ |
| 195 | $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' ), |
| 196 | $fs->get_module_label( true ) |
| 197 | ), |
| 198 | 10, |
| 199 | $is_optional, |
| 200 | true, |
| 201 | $load_default_from_storage |
| 202 | ); |
| 203 | |
| 204 | $permissions[] = $this->get_permission( |
| 205 | self::PERMISSION_EVENTS, |
| 206 | 'admin-' . ( $fs->is_plugin() ? 'plugins' : 'appearance' ), |
| 207 | sprintf( $fs->get_text_inline( 'View Basic %s Info', 'permissions-events' ), $fs->get_module_label() ), |
| 208 | sprintf( |
| 209 | /* translators: %s: 'Plugin' or 'Theme' */ |
| 210 | $fs->get_text_inline( 'Current %s & SDK versions, and if active or uninstalled', 'permissions-events_desc' ), |
| 211 | $fs->get_module_label( true ) |
| 212 | ), |
| 213 | '', |
| 214 | 20, |
| 215 | $is_optional, |
| 216 | true, |
| 217 | $load_default_from_storage |
| 218 | ); |
| 219 | |
| 220 | return $permissions; |
| 221 | } |
| 222 | |
| 223 | #endregion |
| 224 | |
| 225 | #-------------------------------------------------------------------------------- |
| 226 | #region License Activation Permissions |
| 227 | #-------------------------------------------------------------------------------- |
| 228 | |
| 229 | /** |
| 230 | * @param array[] $extra_permissions |
| 231 | * |
| 232 | * @return array[] |
| 233 | */ |
| 234 | function get_license_activation_permissions( |
| 235 | array $extra_permissions = array(), |
| 236 | $include_optional_label = true |
| 237 | ) { |
| 238 | $permissions = array_merge( |
| 239 | $this->get_license_required_permissions(), |
| 240 | $this->get_license_optional_permissions( $include_optional_label ), |
| 241 | $extra_permissions |
| 242 | ); |
| 243 | |
| 244 | return $this->get_sorted_permissions_by_priority( $permissions ); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * @param bool $load_default_from_storage |
| 249 | * |
| 250 | * @return array[] |
| 251 | */ |
| 252 | function get_license_required_permissions( $load_default_from_storage = false ) { |
| 253 | // Alias. |
| 254 | $fs = $this->_fs; |
| 255 | |
| 256 | $permissions = array(); |
| 257 | |
| 258 | $permissions[] = $this->get_permission( |
| 259 | self::PERMISSION_ESSENTIALS, |
| 260 | 'admin-links', |
| 261 | $fs->get_text_inline( 'View License Essentials', 'permissions-essentials' ), |
| 262 | $fs->get_text_inline( |
| 263 | sprintf( |
| 264 | /* translators: %s: 'Plugin' or 'Theme' */ |
| 265 | 'Homepage URL, %s version, SDK version', |
| 266 | $fs->get_module_label() |
| 267 | ), |
| 268 | 'permissions-essentials_desc' |
| 269 | ), |
| 270 | sprintf( |
| 271 | /* translators: %s: 'Plugin' or 'Theme' */ |
| 272 | $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' ), |
| 273 | $fs->get_module_label( true ) |
| 274 | ), |
| 275 | 10, |
| 276 | false, |
| 277 | true, |
| 278 | $load_default_from_storage |
| 279 | ); |
| 280 | |
| 281 | $permissions[] = $this->get_permission( |
| 282 | self::PERMISSION_EVENTS, |
| 283 | 'admin-' . ( $fs->is_plugin() ? 'plugins' : 'appearance' ), |
| 284 | sprintf( $fs->get_text_inline( 'View %s State', 'permissions-events' ), $fs->get_module_label() ), |
| 285 | sprintf( |
| 286 | /* translators: %s: 'Plugin' or 'Theme' */ |
| 287 | $fs->get_text_inline( 'Is active, deactivated, or uninstalled', 'permissions-events_desc-paid' ), |
| 288 | $fs->get_module_label( true ) |
| 289 | ), |
| 290 | 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 ) ), |
| 291 | 20, |
| 292 | false, |
| 293 | true, |
| 294 | $load_default_from_storage |
| 295 | ); |
| 296 | |
| 297 | return $permissions; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * @return array[] |
| 302 | */ |
| 303 | function get_license_optional_permissions( |
| 304 | $include_optional_label = false, |
| 305 | $load_default_from_storage = false |
| 306 | ) { |
| 307 | return array( |
| 308 | $this->get_diagnostic_permission( $include_optional_label, $load_default_from_storage ), |
| 309 | $this->get_extensions_permission( true, $include_optional_label, $load_default_from_storage ), |
| 310 | ); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * @param bool $include_optional_label |
| 315 | * @param bool $load_default_from_storage |
| 316 | * |
| 317 | * @return array |
| 318 | */ |
| 319 | function get_diagnostic_permission( |
| 320 | $include_optional_label = false, |
| 321 | $load_default_from_storage = false |
| 322 | ) { |
| 323 | return $this->get_permission( |
| 324 | self::PERMISSION_DIAGNOSTIC, |
| 325 | 'wordpress-alt', |
| 326 | $this->_fs->get_text_inline( 'View Diagnostic Info', 'permissions-diagnostic' ) . ( $include_optional_label ? ' (' . $this->_fs->get_text_inline( 'optional' ) . ')' : '' ), |
| 327 | $this->_fs->get_text_inline( 'WordPress & PHP versions, site language & title', 'permissions-diagnostic_desc' ), |
| 328 | sprintf( |
| 329 | /* translators: %s: 'Plugin' or 'Theme' */ |
| 330 | $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' ), |
| 331 | $this->_fs->get_module_label( true ) |
| 332 | ), |
| 333 | 25, |
| 334 | true, |
| 335 | true, |
| 336 | $load_default_from_storage |
| 337 | ); |
| 338 | } |
| 339 | |
| 340 | #endregion |
| 341 | |
| 342 | #-------------------------------------------------------------------------------- |
| 343 | #region Common Permissions |
| 344 | #-------------------------------------------------------------------------------- |
| 345 | |
| 346 | /** |
| 347 | * @param bool $is_license_activation |
| 348 | * @param bool $include_optional_label |
| 349 | * @param bool $load_default_from_storage |
| 350 | * |
| 351 | * @return array |
| 352 | */ |
| 353 | function get_extensions_permission( |
| 354 | $is_license_activation, |
| 355 | $include_optional_label = false, |
| 356 | $load_default_from_storage = false |
| 357 | ) { |
| 358 | $is_on_by_default = ! $is_license_activation; |
| 359 | |
| 360 | return $this->get_permission( |
| 361 | self::PERMISSION_EXTENSIONS, |
| 362 | 'block-default', |
| 363 | $this->_fs->get_text_inline( 'View Plugins & Themes List', 'permissions-extensions' ) . ( $is_license_activation ? ( $include_optional_label ? ' (' . $this->_fs->get_text_inline( 'optional' ) . ')' : '' ) : '' ), |
| 364 | $this->_fs->get_text_inline( 'Names, slugs, versions, and if active or not', 'permissions-extensions_desc' ), |
| 365 | $this->_fs->get_text_inline( 'To ensure compatibility and avoid conflicts with your installed plugins and themes.', 'permissions-events_tooltip' ), |
| 366 | 25, |
| 367 | true, |
| 368 | $is_on_by_default, |
| 369 | $load_default_from_storage |
| 370 | ); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * @param bool $load_default_from_storage |
| 375 | * |
| 376 | * @return array |
| 377 | */ |
| 378 | function get_user_permission( $load_default_from_storage = false ) { |
| 379 | return $this->get_permission( |
| 380 | self::PERMISSION_USER, |
| 381 | 'admin-users', |
| 382 | $this->_fs->get_text_inline( 'View Basic Profile Info', 'permissions-profile' ), |
| 383 | $this->_fs->get_text_inline( 'Your WordPress user\'s: first & last name, and email address', 'permissions-profile_desc' ), |
| 384 | $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' ), |
| 385 | 5, |
| 386 | false, |
| 387 | true, |
| 388 | $load_default_from_storage |
| 389 | ); |
| 390 | } |
| 391 | |
| 392 | #endregion |
| 393 | |
| 394 | #-------------------------------------------------------------------------------- |
| 395 | #region Optional Permissions |
| 396 | #-------------------------------------------------------------------------------- |
| 397 | |
| 398 | /** |
| 399 | * @return array[] |
| 400 | */ |
| 401 | function get_newsletter_permission() { |
| 402 | return $this->get_permission( |
| 403 | self::PERMISSION_NEWSLETTER, |
| 404 | 'email-alt', |
| 405 | $this->_fs->get_text_inline( 'Newsletter', 'permissions-newsletter' ), |
| 406 | $this->_fs->get_text_inline( 'Updates, announcements, marketing, no spam', 'permissions-newsletter_desc' ), |
| 407 | '', |
| 408 | 15 |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | #endregion |
| 413 | |
| 414 | #-------------------------------------------------------------------------------- |
| 415 | #region Permissions Storage |
| 416 | #-------------------------------------------------------------------------------- |
| 417 | |
| 418 | /** |
| 419 | * @param int|null $blog_id |
| 420 | * |
| 421 | * @return bool |
| 422 | */ |
| 423 | function is_extensions_tracking_allowed( $blog_id = null ) { |
| 424 | return $this->is_permission_allowed( self::PERMISSION_EXTENSIONS, ! $this->_fs->is_premium(), $blog_id ); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * @param int|null $blog_id |
| 429 | * |
| 430 | * @return bool |
| 431 | */ |
| 432 | function is_essentials_tracking_allowed( $blog_id = null ) { |
| 433 | return $this->is_permission_allowed( self::PERMISSION_ESSENTIALS, true, $blog_id ); |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * @param bool $default |
| 438 | * |
| 439 | * @return bool |
| 440 | */ |
| 441 | function is_diagnostic_tracking_allowed( $default = true ) { |
| 442 | return $this->is_premium_context() ? |
| 443 | $this->is_permission_allowed( self::PERMISSION_DIAGNOSTIC, $default ) : |
| 444 | $this->is_permission_allowed( self::PERMISSION_SITE, $default ); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * @param int|null $blog_id |
| 449 | * |
| 450 | * @return bool |
| 451 | */ |
| 452 | function is_homepage_url_tracking_allowed( $blog_id = null ) { |
| 453 | return $this->is_permission_allowed( $this->get_site_permission_name(), true, $blog_id ); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * @param int|null $blog_id |
| 458 | * |
| 459 | * @return bool |
| 460 | */ |
| 461 | function update_site_tracking( $is_enabled, $blog_id = null, $only_if_not_set = false ) { |
| 462 | $permissions = $this->get_site_tracking_permission_names(); |
| 463 | |
| 464 | $result = true; |
| 465 | foreach ( $permissions as $permission ) { |
| 466 | if ( ! $only_if_not_set || ! $this->is_permission_set( $permission, $blog_id ) ) { |
| 467 | $result = ( $result && $this->update_permission_tracking_flag( $permission, $is_enabled, $blog_id ) ); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return $result; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * @param string $permission |
| 476 | * @param bool $default |
| 477 | * @param int|null $blog_id |
| 478 | * |
| 479 | * @return bool |
| 480 | */ |
| 481 | function is_permission_allowed( $permission, $default = false, $blog_id = null ) { |
| 482 | if ( ! self::is_supported_permission( $permission ) ) { |
| 483 | return $default; |
| 484 | } |
| 485 | |
| 486 | return $this->is_permission( $permission, true, $blog_id ); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * @param string $permission |
| 491 | * @param bool $is_allowed |
| 492 | * @param int|null $blog_id |
| 493 | * |
| 494 | * @return bool |
| 495 | */ |
| 496 | function is_permission( $permission, $is_allowed, $blog_id = null ) { |
| 497 | if ( ! self::is_supported_permission( $permission ) ) { |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | $tag = "is_{$permission}_tracking_allowed"; |
| 502 | |
| 503 | return ( $is_allowed === $this->_fs->apply_filters( |
| 504 | $tag, |
| 505 | $this->_storage->get( |
| 506 | $tag, |
| 507 | $this->get_permission_default( $permission ), |
| 508 | $blog_id, |
| 509 | FS_Storage::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED |
| 510 | ) |
| 511 | ) ); |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * @param string $permission |
| 516 | * @param int|null $blog_id |
| 517 | * |
| 518 | * @return bool |
| 519 | */ |
| 520 | function is_permission_set( $permission, $blog_id = null ) { |
| 521 | $tag = "is_{$permission}_tracking_allowed"; |
| 522 | |
| 523 | $permission = $this->_storage->get( |
| 524 | $tag, |
| 525 | null, |
| 526 | $blog_id, |
| 527 | FS_Storage::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED |
| 528 | ); |
| 529 | |
| 530 | return is_bool( $permission ); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * @param string[] $permissions |
| 535 | * @param bool $is_allowed |
| 536 | * |
| 537 | * @return bool `true` if all given permissions are in sync with `$is_allowed`. |
| 538 | */ |
| 539 | function are_permissions( $permissions, $is_allowed, $blog_id = null ) { |
| 540 | foreach ( $permissions as $permission ) { |
| 541 | if ( ! $this->is_permission( $permission, $is_allowed, $blog_id ) ) { |
| 542 | return false; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * @param string $permission |
| 551 | * @param bool $is_enabled |
| 552 | * @param int|null $blog_id |
| 553 | * |
| 554 | * @return bool `false` if permission not supported or `$is_enabled` is not a boolean. |
| 555 | */ |
| 556 | function update_permission_tracking_flag( $permission, $is_enabled, $blog_id = null ) { |
| 557 | if ( is_bool( $is_enabled ) && self::is_supported_permission( $permission ) ) { |
| 558 | $this->_storage->store( |
| 559 | "is_{$permission}_tracking_allowed", |
| 560 | $is_enabled, |
| 561 | $blog_id, |
| 562 | FS_Storage::OPTION_LEVEL_NETWORK_ACTIVATED_NOT_DELEGATED |
| 563 | ); |
| 564 | |
| 565 | return true; |
| 566 | } |
| 567 | |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * @param array<string,bool> $permissions |
| 573 | */ |
| 574 | function update_permissions_tracking_flag( $permissions ) { |
| 575 | foreach ( $permissions as $permission => $is_enabled ) { |
| 576 | $this->update_permission_tracking_flag( $permission, $is_enabled ); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | #endregion |
| 581 | |
| 582 | |
| 583 | /** |
| 584 | * @param string $permission |
| 585 | * |
| 586 | * @return bool |
| 587 | */ |
| 588 | function get_permission_default( $permission ) { |
| 589 | if ( |
| 590 | $this->_fs->is_premium() && |
| 591 | self::PERMISSION_EXTENSIONS === $permission |
| 592 | ) { |
| 593 | return false; |
| 594 | } |
| 595 | |
| 596 | // All permissions except for the extensions in paid version are on by default when the user opts in to usage tracking. |
| 597 | return true; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * @return string |
| 602 | */ |
| 603 | function get_site_permission_name() { |
| 604 | return $this->is_premium_context() ? |
| 605 | self::PERMISSION_ESSENTIALS : |
| 606 | self::PERMISSION_SITE; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * @return string[] |
| 611 | */ |
| 612 | function get_site_tracking_permission_names() { |
| 613 | return $this->is_premium_context() ? |
| 614 | array( |
| 615 | FS_Permission_Manager::PERMISSION_ESSENTIALS, |
| 616 | FS_Permission_Manager::PERMISSION_EVENTS, |
| 617 | ) : |
| 618 | array( FS_Permission_Manager::PERMISSION_SITE ); |
| 619 | } |
| 620 | |
| 621 | #-------------------------------------------------------------------------------- |
| 622 | #region Rendering |
| 623 | #-------------------------------------------------------------------------------- |
| 624 | |
| 625 | /** |
| 626 | * @param array $permission |
| 627 | */ |
| 628 | function render_permission( array $permission ) { |
| 629 | fs_require_template( 'connect/permission.php', $permission ); |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * @param array $permissions_group |
| 634 | */ |
| 635 | function render_permissions_group( array $permissions_group ) { |
| 636 | $permissions_group[ 'fs' ] = $this->_fs; |
| 637 | |
| 638 | fs_require_template( 'connect/permissions-group.php', $permissions_group ); |
| 639 | } |
| 640 | |
| 641 | function require_permissions_js() { |
| 642 | fs_require_once_template( 'js/permissions.php', $params ); |
| 643 | } |
| 644 | |
| 645 | #endregion |
| 646 | |
| 647 | #-------------------------------------------------------------------------------- |
| 648 | #region Helper Methods |
| 649 | #-------------------------------------------------------------------------------- |
| 650 | |
| 651 | /** |
| 652 | * @param string $id |
| 653 | * @param string $dashicon |
| 654 | * @param string $label |
| 655 | * @param string $desc |
| 656 | * @param string $tooltip |
| 657 | * @param int $priority |
| 658 | * @param bool $is_optional |
| 659 | * @param bool $is_on_by_default |
| 660 | * @param bool $load_from_storage |
| 661 | * |
| 662 | * @return array |
| 663 | */ |
| 664 | private function get_permission( |
| 665 | $id, |
| 666 | $dashicon, |
| 667 | $label, |
| 668 | $desc, |
| 669 | $tooltip = '', |
| 670 | $priority = 10, |
| 671 | $is_optional = false, |
| 672 | $is_on_by_default = true, |
| 673 | $load_from_storage = false |
| 674 | ) { |
| 675 | $is_on = $load_from_storage ? |
| 676 | $this->is_permission_allowed( $id, $is_on_by_default ) : |
| 677 | $is_on_by_default; |
| 678 | |
| 679 | return array( |
| 680 | 'id' => $id, |
| 681 | 'icon-class' => $this->_fs->apply_filters( "permission_{$id}_icon", "dashicons dashicons-{$dashicon}" ), |
| 682 | 'label' => $this->_fs->apply_filters( "permission_{$id}_label", $label ), |
| 683 | 'tooltip' => $this->_fs->apply_filters( "permission_{$id}_tooltip", $tooltip ), |
| 684 | 'desc' => $this->_fs->apply_filters( "permission_{$id}_desc", $desc ), |
| 685 | 'priority' => $this->_fs->apply_filters( "permission_{$id}_priority", $priority ), |
| 686 | 'optional' => $is_optional, |
| 687 | 'default' => $this->_fs->apply_filters( "permission_{$id}_default", $is_on ), |
| 688 | ); |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * @param array $permissions |
| 693 | * |
| 694 | * @return array[] |
| 695 | */ |
| 696 | private function get_sorted_permissions_by_priority( array $permissions ) { |
| 697 | // Allow filtering of the permissions list. |
| 698 | $permissions = $this->_fs->apply_filters( 'permission_list', $permissions ); |
| 699 | |
| 700 | // Sort by priority. |
| 701 | uasort( $permissions, 'fs_sort_by_priority' ); |
| 702 | |
| 703 | return $permissions; |
| 704 | } |
| 705 | |
| 706 | #endregion |
| 707 | } |