PluginProbe
User Access Manager / 2.1.3
User Access Manager v2.1.3
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Setup / Update / Update1.php

Update1.php in User Access Manager 2.1.3, at src/Setup/Update/Update1.php

187 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Update1.php
4 *
5 * The Update1 class file.
6 *
7 * PHP versions 5
8 *
9 * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 * @copyright 2008-2017 Alexander Schneider
11 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 * @version SVN: $id$
13 * @link http://wordpress.org/extend/plugins/user-access-manager/
14 */
15 namespace UserAccessManager\Setup\Update;
16
17 /**
18 * Class Update1
19 *
20 * @package UserAccessManager\Setup\Update
21 */
22 class Update1 extends Update implements UpdateInterface
23 {
24 /**
25 * Returns the version.
26 *
27 * @return string
28 */
29 public function getVersion()
30 {
31 return '1.0';
32 }
33
34 /**
35 * Updates the user group table to version 1.0.
36 *
37 * @param string $userGroupTable
38 *
39 * @return bool
40 */
41 private function updateToUserGroupTableUpdate($userGroupTable)
42 {
43 $alterQuery = "ALTER TABLE {$userGroupTable}
44 ADD read_access TINYTEXT NOT NULL DEFAULT '',
45 ADD write_access TINYTEXT NOT NULL DEFAULT '',
46 ADD ip_range MEDIUMTEXT NULL DEFAULT ''";
47
48 $this->database->query($alterQuery);
49
50 $updateQuery = "UPDATE {$userGroupTable} SET read_access = 'group', write_access = 'group'";
51 $success = $this->database->query($updateQuery) !== false;
52
53 $selectQuery = "SHOW columns FROM {$userGroupTable} LIKE 'ip_range'";
54 $dbIpRange = (string)$this->database->getVariable($selectQuery);
55
56 if ($dbIpRange !== 'ip_range') {
57 $alterQuery = "ALTER TABLE {$userGroupTable} ADD ip_range MEDIUMTEXT NULL DEFAULT ''";
58 $success = $this->database->query($alterQuery) !== false;
59 }
60
61 return $success;
62 }
63
64 /**
65 * Returns the select query for the object type.
66 *
67 * @param string $objectType
68 * @param string $userGroupToPost
69 * @param string $userGroupToCategory
70 * @param string $userGroupToUser
71 * @param string $userGroupToRole
72 *
73 * @return null|string
74 */
75 private function getObjectSelectQuery(
76 $objectType,
77 $userGroupToPost,
78 $userGroupToCategory,
79 $userGroupToUser,
80 $userGroupToRole
81 ) {
82 $addition = '';
83
84 if ($this->objectHandler->isPostType($objectType) === true) {
85 $dbIdName = 'post_id';
86 $database = $userGroupToPost.', '.$this->database->getPostsTable();
87 $addition = " WHERE post_id = ID AND post_type = '{$objectType}'";
88 } elseif ($objectType === 'category') {
89 $dbIdName = 'category_id';
90 $database = $userGroupToCategory;
91 } elseif ($objectType === 'user') {
92 $dbIdName = 'user_id';
93 $database = $userGroupToUser;
94 } elseif ($objectType === 'role') {
95 $dbIdName = 'role_name';
96 $database = $userGroupToRole;
97 } else {
98 return null;
99 }
100
101 return "SELECT {$dbIdName} AS id, group_id AS groupId FROM {$database} {$addition}";
102 }
103
104 /**
105 * Updates the user group to object table to version 1.0.
106 */
107 private function updateToUserGroupToObjectTableUpdate()
108 {
109 $prefix = $this->database->getPrefix();
110
111 $charsetCollate = $this->database->getCharset();
112 $userGroupToObject = $prefix.'uam_accessgroup_to_object';
113 $userGroupToPost = $prefix.'uam_accessgroup_to_post';
114 $userGroupToUser = $prefix.'uam_accessgroup_to_user';
115 $userGroupToCategory = $prefix.'uam_accessgroup_to_category';
116 $userGroupToRole = $prefix.'uam_accessgroup_to_role';
117
118 $alterQuery = "ALTER TABLE '{$userGroupToObject}'
119 CHANGE 'object_id' 'object_id' VARCHAR(64) {$charsetCollate}";
120 $success = $this->database->query($alterQuery) !== false;
121
122 if ($success === false) {
123 return false;
124 }
125
126 $objectTypes = $this->objectHandler->getObjectTypes();
127
128 foreach ($objectTypes as $objectType) {
129 $query = $this->getObjectSelectQuery(
130 $objectType,
131 $userGroupToPost,
132 $userGroupToCategory,
133 $userGroupToUser,
134 $userGroupToRole
135 );
136
137 if ($query === null) {
138 continue;
139 }
140
141 $dbObjects = (array)$this->database->getResults($query);
142
143 foreach ($dbObjects as $dbObject) {
144 $insert = $this->database->insert(
145 $userGroupToObject,
146 [
147 'group_id' => $dbObject->groupId,
148 'object_id' => $dbObject->id,
149 'object_type' => $objectType
150 ],
151 [
152 '%d',
153 '%d',
154 '%s'
155 ]
156 );
157 $success = $success && $insert !== false;
158 }
159 }
160
161 $dropQuery = "DROP TABLE {$userGroupToPost},
162 {$userGroupToUser},
163 {$userGroupToCategory},
164 {$userGroupToRole}";
165
166 return $success && $this->database->query($dropQuery) !== false;
167 }
168
169 /**
170 * Executes the update.
171 *
172 * @return bool
173 */
174 public function update()
175 {
176 $success = true;
177 $userGroupTable = $this->database->getUserGroupTable();
178 $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '{$userGroupTable}'");
179
180 if ($dbUserGroup === $userGroupTable) {
181 $success = $this->updateToUserGroupTableUpdate($userGroupTable);
182 }
183
184 return $success && $this->updateToUserGroupToObjectTableUpdate();
185 }
186 }
187