PluginProbe
Extendify / 0.2.0
Extendify v0.2.0
3.2.1 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 All 127 releases
extendify / app / User.php

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

149 lines 3.8 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\Library;
7
8 use Extendify\Library\App;
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 the application state for he 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 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 if (!$userData['state']['sdkPartner']) {
115 $userData['state']['sdkPartner'] = App::$sdkPartner;
116 }
117
118 $userData['state']['uuid'] = self::data('uuid');
119 $userData['state']['canInstallPlugins'] = \current_user_can('install_plugins');
120 $userData['state']['canActivatePlugins'] = \current_user_can('activate_plugins');
121 $userData['state']['isAdmin'] = \current_user_can('create_users');
122
123 return \wp_json_encode($userData);
124 }
125
126 /**
127 * Allows to dynamically setup the user with uuid
128 * Use it like User::data('ID') to get the user id
129 *
130 * @param string $name - The name of the method to call.
131 * @param array $arguments - The arguments to pass in.
132 *
133 * @return mixed
134 */
135 public static function __callStatic($name, array $arguments)
136 {
137 $name = "{$name}Handler";
138 if (is_null(self::$instance)) {
139 require_once ABSPATH . 'wp-includes/pluggable.php';
140 self::$instance = new static(\wp_get_current_user());
141 $r = self::$instance;
142 $r->setupUuid();
143 }
144
145 $r = self::$instance;
146 return $r->$name(...$arguments);
147 }
148 }
149