PluginProbe
UpdraftCentral Dashboard / 0.8.0
UpdraftCentral Dashboard v0.8.0
0.8.33 0.7.2 0.7.3 0.7.4 0.8.0 0.8.1 0.8.10 0.8.11 0.8.12 0.8.13 0.8.14 0.8.15 0.8.16 0.8.17 0.8.18 0.8.19 0.8.2 0.8.20 0.8.21 0.8.22 0.8.23 0.8.24 0.8.25 0.8.26 0.8.27 All 51 releases
updraftcentral / classes / licence-manager.php

licence-manager.php in UpdraftCentral Dashboard 0.8.0, at classes/licence-manager.php

136 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('UD_CENTRAL_DIR')) die('Security check');
4
5 /*
6 Handles entitlements.
7
8 This default class handles entitlements in conjunction with the Simba Plugins Updates + Licensing Manager - https://wordpress.org/plugins/simba-plugin-updates-manager/ . Thus, you will need that plugin to be installed and active, if you want to handle licensing - otherwise, all usage is unlimited (for users who can get to the dashboard).
9
10 If it is installed, then admins get unlimited access, and everyone else requires valid licenses. You can modify this with the updraftcentral_licencemanager_nolicencesrequired filter to remove all licensing; or, various other filters to replace things piecemeal.
11
12 */
13
14 if (!class_exists('UpdraftCentral_Licence_Manager')) :
15
16 class UpdraftCentral_Licence_Manager {
17
18 private $user = null;
19
20 private $rc = null;
21
22 private $no_licences_required = false;
23
24 /**
25 * Pass in a UpdraftCentral_User
26 *
27 * @param string $user
28 * @param array $central_object
29 */
30 public function __construct($user, $central_object) {
31
32 /*
33 Filter: updraftcentral_licencemanager_nolicencesrequired
34 Use this filter to de-activate licensing for specified users - allow unlimited access to everyone. By default, we limit it to admins.
35 (You then also need to hook into various other filters in order to actually do something useful - the default values allow everyone unlimited licences)
36 e.g. to optionally integrate with the Simba updates/licensing manager
37 $simba_manager_installed = class_exists('Updraft_Manager_Premium');
38 $no_licences_required = ($no_licences_required || !$simba_manager_installed);
39 $require_spm_version = '1.5.15';
40 if (!$no_licences_required && (!$simba_manager_installed || !defined('UDMANAGER_VERSION') || !version_compare(UDMANAGER_VERSION, $require_spm_version, '>='))) {
41 throw new Exception(sprintf('The Simba updates + licensing manager (version %s or later) is required - please install and activate it.', $require_spm_version));
42 }
43 */
44
45 $this->no_licences_required = apply_filters('updraftcentral_licencemanager_nolicencesrequired', true, $user);
46 $this->user = $user;
47 $this->rc = $central_object;
48
49 add_shortcode('updraftcentral_licences_in_use', array($this, 'how_many_licences_in_use'));
50 add_shortcode('updraftcentral_licences_total', array($this, 'how_many_licences_available'));
51 }
52
53 public function is_slot_available() {
54 if ($this->no_licences_required) return true;
55 $licences_in_use = $this->how_many_licences_in_use();
56 $licences_available = $this->how_many_licences_available();
57
58 return $licences_available < 0 || $licences_in_use < $licences_available;
59 }
60
61 /**
62 * Used both internally, and as a shortcode
63 *
64 * @return int
65 */
66 public function how_many_licences_in_use() {
67 if (!is_array($this->user->sites)) return 0;
68
69 return count($this->user->sites);
70 }
71
72 public function get_entitlements_expiring_in($expire_window_begin, $expire_window_end) {
73
74 /*
75 Example if integrating with Simba Updates/Licensing manager:
76 return count(Updraft_Manager_Premium::db_get_all_entitlements($user->user_id, $expire_window_begin, $expire_window_end, array('updraft-central'), 'updraft-central'))
77 No format for the returned array is specified or needed. The only important thing is that there's 1 entry for each entitlement. You can just push a value of 'true' on for each licence if there's no info needed.
78 Returns -1 for 'unlimited'
79 */
80
81 return apply_filters('updraftcentral_licencemanager_get_entitlements_expiring_in', 0, $expire_window_begin, $expire_window_end, $this, $this->user);
82
83 }
84
85 /**
86 * How many licences recently expired
87 *
88 * @param integer $how_long_ago Default: 4 weeks
89 * @return array
90 */
91 public function how_many_licences_recently_expired($how_long_ago = 2419200) {
92 if ($this->no_licences_required) {
93 $how_many = 0;
94 } else {
95 $time_now = time();
96 $how_many = $this->get_entitlements_expiring_in($time_now, ($time_now - $how_long_ago));
97 }
98
99 return apply_filters('updraftcentral_how_many_licences_recently_expired', $how_many, $how_long_ago, $this, $this->user);
100 }
101
102 /**
103 * How many licences soon expiring
104 *
105 * @param integer $in_next Default: 4 weeks
106 * @return array
107 */
108 public function how_many_licences_soon_expiring($in_next = 2419200) {
109 if ($this->no_licences_required) {
110 $how_many = 0;
111 } else {
112 $time_now = time();
113 $how_many = $this->get_entitlements_expiring_in($time_now, ($time_now + $in_next));
114 }
115
116 return apply_filters('updraftcentral_how_many_licences_soon_expiring', $how_many, $in_next, $this, $this->user);
117 }
118
119 /**
120 * Used both internally, and as a shortcode
121 *
122 * @return array
123 */
124 public function how_many_licences_available() {
125 if ($this->no_licences_required) {
126 $entitlements_available = -1;
127 } else {
128 $entitlements_available = $this->get_entitlements_expiring_in(time(), PHP_INT_MAX);
129 }
130
131 return apply_filters('updraftcentral_how_many_licences_available', $entitlements_available, $this, $this->user, $this->no_licences_required);
132 }
133 }
134
135 endif;
136