PluginProbe
Extendify / 0.9.3
Extendify v0.9.3
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / app / User.php

User.php in Extendify 0.9.3, at app/User.php

163 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helper class for interacting with the user
4 */
5
6 namespace Extendify;
7
8 use Extendify\Config;
9
10 /**
11 * Helper class for interacting with the user
12 */
13 class User
14 {
15
16 /**
17 * User unique, anonymous identifier
18 *
19 * @var string
20 */
21 public $uuid = '';
22
23 /**
24 * A WP user
25 *
26 * @var \WP_User
27 */
28 protected $user = null;
29
30 /**
31 * The DB key for scoping. For historical reasons do not change
32 *
33 * @var string
34 */
35 protected $key = 'extendifysdk_';
36
37 /**
38 * The class instance.
39 *
40 * @var $instance
41 */
42 protected static $instance = null;
43
44 /**
45 * Set up the user
46 *
47 * @param WP_User $user - A WP User object.
48 * @return void
49 */
50 public function __construct($user)
51 {
52 $this->user = $user;
53 }
54
55 /**
56 * Return the user ID
57 *
58 * @return void
59 */
60 private function setupUuid()
61 {
62 $uuid = \get_user_meta($this->user->ID, $this->key . 'uuid', true);
63 if (!$uuid) {
64 $id = \wp_hash(\wp_json_encode($this->user));
65 \update_user_meta($this->user->ID, $this->key . 'uuid', $id);
66 }
67
68 $this->uuid = $uuid;
69 }
70
71 /**
72 * Returns data about the user
73 * Use it like User::data('ID') to get the user id
74 *
75 * @param string $arguments - Right now a string of arguments, like ID.
76 * @return mixed - Data about the user.
77 */
78 private function dataHandler($arguments)
79 {
80 // Right now assume a single argument, but could expand to multiple.
81 if (isset($this->user->$arguments)) {
82 return $this->user->$arguments;
83 }
84
85 return \get_user_meta($this->user->ID, $this->key . $arguments, true);
86 }
87
88 /**
89 * Returns application state for the current user
90 * Use it like User::data('ID') to get the user id
91 *
92 * @return string - JSON representation of the current state
93 */
94 private function stateHandler()
95 {
96 $state = \get_user_meta($this->user->ID, $this->key . 'user_data');
97
98 // Add some state boilerplate code for the first load.
99 if (!isset($state[0])) {
100 $state[0] = '{}';
101 }
102
103 $userData = json_decode($state[0], true);
104 if (!isset($userData['version'])) {
105 $userData['version'] = 0;
106 }
107
108 // This will reset the allowed max imports to 0 once a week which will force the library to re-check.
109 if (!get_transient('extendify_import_max_check_' . $this->user->ID)) {
110 set_transient('extendify_import_max_check_' . $this->user->ID, time(), strtotime('1 week', 0));
111 $userData['state']['allowedImports'] = 0;
112 }
113
114 // Similar to above, this will give the user free imports once a month just for logging in.
115 if (!get_transient('extendify_free_extra_imports_check_' . $this->user->ID)) {
116 set_transient('extendify_free_extra_imports_check_' . $this->user->ID, time(), strtotime('first day of next month', 0));
117 $userData['state']['runningImports'] = 0;
118 }
119
120 if (!isset($userData['state']['sdkPartner']) || !$userData['state']['sdkPartner']) {
121 $userData['state']['sdkPartner'] = Config::$sdkPartner;
122 }
123
124 $userData['state']['uuid'] = self::data('uuid');
125 $userData['state']['canInstallPlugins'] = \current_user_can('install_plugins');
126 $userData['state']['canActivatePlugins'] = \current_user_can('activate_plugins');
127 $userData['state']['isAdmin'] = \current_user_can('create_users');
128
129 // If the license key is set on the server, force use it.
130 if (defined('EXTENDIFY_SITE_LICENSE')) {
131 $userData['state']['apiKey'] = constant('EXTENDIFY_SITE_LICENSE');
132 }
133
134 // This probably shouldn't have been wrapped in wp_json_encode,
135 // but needs to remain until we can safely log pro users out,
136 // as changing this now would erase all user data.
137 return \wp_json_encode($userData);
138 }
139
140 /**
141 * Allows to dynamically setup the user with uuid
142 * Use it like User::data('ID') to get the user id
143 *
144 * @param string $name - The name of the method to call.
145 * @param array $arguments - The arguments to pass in.
146 *
147 * @return mixed
148 */
149 public static function __callStatic($name, array $arguments)
150 {
151 $name = "{$name}Handler";
152 if (is_null(self::$instance)) {
153 require_once ABSPATH . 'wp-includes/pluggable.php';
154 self::$instance = new static(\wp_get_current_user());
155 $r = self::$instance;
156 $r->setupUuid();
157 }
158
159 $r = self::$instance;
160 return $r->$name(...$arguments);
161 }
162 }
163