Repository URL to install this package:
<?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() );
}
/**
* @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,
Loading ...