Cloud computing platform
Revision | fa609a6ca801e365ce054d626f833c110fd83968 (tree) |
---|---|
Time | 2017-06-26 10:59:03 |
Author | naoki hirata <naoki@magi...> |
Commiter | naoki hirata |
WordPressテンプレート機能更新。
@@ -692,11 +692,13 @@ class BaseFrameContainer extends Core | ||
692 | 692 | require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/default-filters.php'); |
693 | 693 | require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/l10n.php'); |
694 | 694 | require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp.php'); |
695 | + require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-user.php'); | |
695 | 696 | require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-walker.php'); |
696 | 697 | require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-query.php'); // コンテンツデータ取得 |
697 | 698 | require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-comment-query.php'); |
698 | 699 | require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-walker-page.php'); |
699 | 700 | require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-theme.php'); |
701 | + require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-list-util.php'); | |
700 | 702 | // require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-walker-nav-menu.php'); |
701 | 703 | // require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-dependency.php'); |
702 | 704 | require_once($this->gEnv->getWordpressRootPath() . '/wp-includes/class-wp-post.php'); // コンテンツAPIマネージャーからWP_Post型でデータを取得 |
@@ -373,8 +373,8 @@ class WP_Comment_Query { | ||
373 | 373 | $this->parse_query(); |
374 | 374 | |
375 | 375 | // Parse meta query |
376 | - $this->meta_query = new WP_Meta_Query(); | |
377 | - $this->meta_query->parse_query_vars( $this->query_vars ); | |
376 | +// $this->meta_query = new WP_Meta_Query(); | |
377 | +// $this->meta_query->parse_query_vars( $this->query_vars ); | |
378 | 378 | |
379 | 379 | /** |
380 | 380 | * Fires before comments are retrieved. |
@@ -386,10 +386,10 @@ class WP_Comment_Query { | ||
386 | 386 | do_action_ref_array( 'pre_get_comments', array( &$this ) ); |
387 | 387 | |
388 | 388 | // Reparse query vars, in case they were modified in a 'pre_get_comments' callback. |
389 | - $this->meta_query->parse_query_vars( $this->query_vars ); | |
389 | +/* $this->meta_query->parse_query_vars( $this->query_vars ); | |
390 | 390 | if ( ! empty( $this->meta_query->queries ) ) { |
391 | 391 | $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); |
392 | - } | |
392 | + }*/ | |
393 | 393 | |
394 | 394 | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
395 | 395 | $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); |
@@ -830,17 +830,17 @@ class WP_Comment_Query { | ||
830 | 830 | $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; |
831 | 831 | } |
832 | 832 | |
833 | - if ( ! empty( $this->meta_query_clauses ) ) { | |
833 | +/* if ( ! empty( $this->meta_query_clauses ) ) { | |
834 | 834 | $join .= $this->meta_query_clauses['join']; |
835 | - | |
835 | +*/ | |
836 | 836 | // Strip leading 'AND'. |
837 | - $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] ); | |
838 | - | |
837 | +// $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] ); | |
838 | +/* | |
839 | 839 | if ( ! $this->query_vars['count'] ) { |
840 | 840 | $groupby = "{$wpdb->comments}.comment_ID"; |
841 | 841 | } |
842 | 842 | } |
843 | - | |
843 | +*/ | |
844 | 844 | if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) { |
845 | 845 | $this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' ); |
846 | 846 | $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() ); |
@@ -1127,11 +1127,11 @@ class WP_Comment_Query { | ||
1127 | 1127 | $allowed_keys[] = 'meta_value_num'; |
1128 | 1128 | } |
1129 | 1129 | |
1130 | - $meta_query_clauses = $this->meta_query->get_clauses(); | |
1130 | +/* $meta_query_clauses = $this->meta_query->get_clauses(); | |
1131 | 1131 | if ( $meta_query_clauses ) { |
1132 | 1132 | $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) ); |
1133 | 1133 | } |
1134 | - | |
1134 | +*/ | |
1135 | 1135 | $parsed = false; |
1136 | 1136 | if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) { |
1137 | 1137 | $parsed = "$wpdb->commentmeta.meta_value"; |
@@ -0,0 +1,267 @@ | ||
1 | +<?php | |
2 | +/** | |
3 | + * WordPress List utility class | |
4 | + * | |
5 | + * @package WordPress | |
6 | + * @since 4.7.0 | |
7 | + */ | |
8 | + | |
9 | +/** | |
10 | + * List utility. | |
11 | + * | |
12 | + * Utility class to handle operations on an array of objects. | |
13 | + * | |
14 | + * @since 4.7.0 | |
15 | + */ | |
16 | +class WP_List_Util { | |
17 | + /** | |
18 | + * The input array. | |
19 | + * | |
20 | + * @since 4.7.0 | |
21 | + * @access private | |
22 | + * @var array | |
23 | + */ | |
24 | + private $input = array(); | |
25 | + | |
26 | + /** | |
27 | + * The output array. | |
28 | + * | |
29 | + * @since 4.7.0 | |
30 | + * @access private | |
31 | + * @var array | |
32 | + */ | |
33 | + private $output = array(); | |
34 | + | |
35 | + /** | |
36 | + * Temporary arguments for sorting. | |
37 | + * | |
38 | + * @since 4.7.0 | |
39 | + * @access private | |
40 | + * @var array | |
41 | + */ | |
42 | + private $orderby = array(); | |
43 | + | |
44 | + /** | |
45 | + * Constructor. | |
46 | + * | |
47 | + * Sets the input array. | |
48 | + * | |
49 | + * @since 4.7.0 | |
50 | + * | |
51 | + * @param array $input Array to perform operations on. | |
52 | + */ | |
53 | + public function __construct( $input ) { | |
54 | + $this->output = $this->input = $input; | |
55 | + } | |
56 | + | |
57 | + /** | |
58 | + * Returns the original input array. | |
59 | + * | |
60 | + * @since 4.7.0 | |
61 | + * @access public | |
62 | + * | |
63 | + * @return array The input array. | |
64 | + */ | |
65 | + public function get_input() { | |
66 | + return $this->input; | |
67 | + } | |
68 | + | |
69 | + /** | |
70 | + * Returns the output array. | |
71 | + * | |
72 | + * @since 4.7.0 | |
73 | + * @access public | |
74 | + * | |
75 | + * @return array The output array. | |
76 | + */ | |
77 | + public function get_output() { | |
78 | + return $this->output; | |
79 | + } | |
80 | + | |
81 | + /** | |
82 | + * Filters the list, based on a set of key => value arguments. | |
83 | + * | |
84 | + * @since 4.7.0 | |
85 | + * | |
86 | + * @param array $args Optional. An array of key => value arguments to match | |
87 | + * against each object. Default empty array. | |
88 | + * @param string $operator Optional. The logical operation to perform. 'AND' means | |
89 | + * all elements from the array must match. 'OR' means only | |
90 | + * one element needs to match. 'NOT' means no elements may | |
91 | + * match. Default 'AND'. | |
92 | + * @return array Array of found values. | |
93 | + */ | |
94 | + public function filter( $args = array(), $operator = 'AND' ) { | |
95 | + if ( empty( $args ) ) { | |
96 | + return $this->output; | |
97 | + } | |
98 | + | |
99 | + $operator = strtoupper( $operator ); | |
100 | + | |
101 | + if ( ! in_array( $operator, array( 'AND', 'OR', 'NOT' ), true ) ) { | |
102 | + return array(); | |
103 | + } | |
104 | + | |
105 | + $count = count( $args ); | |
106 | + $filtered = array(); | |
107 | + | |
108 | + foreach ( $this->output as $key => $obj ) { | |
109 | + $to_match = (array) $obj; | |
110 | + | |
111 | + $matched = 0; | |
112 | + foreach ( $args as $m_key => $m_value ) { | |
113 | + if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] ) { | |
114 | + $matched++; | |
115 | + } | |
116 | + } | |
117 | + | |
118 | + if ( | |
119 | + ( 'AND' == $operator && $matched == $count ) || | |
120 | + ( 'OR' == $operator && $matched > 0 ) || | |
121 | + ( 'NOT' == $operator && 0 == $matched ) | |
122 | + ) { | |
123 | + $filtered[$key] = $obj; | |
124 | + } | |
125 | + } | |
126 | + | |
127 | + $this->output = $filtered; | |
128 | + | |
129 | + return $this->output; | |
130 | + } | |
131 | + | |
132 | + /** | |
133 | + * Plucks a certain field out of each object in the list. | |
134 | + * | |
135 | + * This has the same functionality and prototype of | |
136 | + * array_column() (PHP 5.5) but also supports objects. | |
137 | + * | |
138 | + * @since 4.7.0 | |
139 | + * | |
140 | + * @param int|string $field Field from the object to place instead of the entire object | |
141 | + * @param int|string $index_key Optional. Field from the object to use as keys for the new array. | |
142 | + * Default null. | |
143 | + * @return array Array of found values. If `$index_key` is set, an array of found values with keys | |
144 | + * corresponding to `$index_key`. If `$index_key` is null, array keys from the original | |
145 | + * `$list` will be preserved in the results. | |
146 | + */ | |
147 | + public function pluck( $field, $index_key = null ) { | |
148 | + if ( ! $index_key ) { | |
149 | + /* | |
150 | + * This is simple. Could at some point wrap array_column() | |
151 | + * if we knew we had an array of arrays. | |
152 | + */ | |
153 | + foreach ( $this->output as $key => $value ) { | |
154 | + if ( is_object( $value ) ) { | |
155 | + $this->output[ $key ] = $value->$field; | |
156 | + } else { | |
157 | + $this->output[ $key ] = $value[ $field ]; | |
158 | + } | |
159 | + } | |
160 | + return $this->output; | |
161 | + } | |
162 | + | |
163 | + /* | |
164 | + * When index_key is not set for a particular item, push the value | |
165 | + * to the end of the stack. This is how array_column() behaves. | |
166 | + */ | |
167 | + $newlist = array(); | |
168 | + foreach ( $this->output as $value ) { | |
169 | + if ( is_object( $value ) ) { | |
170 | + if ( isset( $value->$index_key ) ) { | |
171 | + $newlist[ $value->$index_key ] = $value->$field; | |
172 | + } else { | |
173 | + $newlist[] = $value->$field; | |
174 | + } | |
175 | + } else { | |
176 | + if ( isset( $value[ $index_key ] ) ) { | |
177 | + $newlist[ $value[ $index_key ] ] = $value[ $field ]; | |
178 | + } else { | |
179 | + $newlist[] = $value[ $field ]; | |
180 | + } | |
181 | + } | |
182 | + } | |
183 | + | |
184 | + $this->output = $newlist; | |
185 | + | |
186 | + return $this->output; | |
187 | + } | |
188 | + | |
189 | + /** | |
190 | + * Sorts the list, based on one or more orderby arguments. | |
191 | + * | |
192 | + * @since 4.7.0 | |
193 | + * | |
194 | + * @param string|array $orderby Optional. Either the field name to order by or an array | |
195 | + * of multiple orderby fields as $orderby => $order. | |
196 | + * @param string $order Optional. Either 'ASC' or 'DESC'. Only used if $orderby | |
197 | + * is a string. | |
198 | + * @param bool $preserve_keys Optional. Whether to preserve keys. Default false. | |
199 | + * @return array The sorted array. | |
200 | + */ | |
201 | + public function sort( $orderby = array(), $order = 'ASC', $preserve_keys = false ) { | |
202 | + if ( empty( $orderby ) ) { | |
203 | + return $this->output; | |
204 | + } | |
205 | + | |
206 | + if ( is_string( $orderby ) ) { | |
207 | + $orderby = array( $orderby => $order ); | |
208 | + } | |
209 | + | |
210 | + foreach ( $orderby as $field => $direction ) { | |
211 | + $orderby[ $field ] = 'DESC' === strtoupper( $direction ) ? 'DESC' : 'ASC'; | |
212 | + } | |
213 | + | |
214 | + $this->orderby = $orderby; | |
215 | + | |
216 | + if ( $preserve_keys ) { | |
217 | + uasort( $this->output, array( $this, 'sort_callback' ) ); | |
218 | + } else { | |
219 | + usort( $this->output, array( $this, 'sort_callback' ) ); | |
220 | + } | |
221 | + | |
222 | + $this->orderby = array(); | |
223 | + | |
224 | + return $this->output; | |
225 | + } | |
226 | + | |
227 | + /** | |
228 | + * Callback to sort the list by specific fields. | |
229 | + * | |
230 | + * @since 4.7.0 | |
231 | + * @access private | |
232 | + * | |
233 | + * @see WP_List_Util::sort() | |
234 | + * | |
235 | + * @param object|array $a One object to compare. | |
236 | + * @param object|array $b The other object to compare. | |
237 | + * @return int 0 if both objects equal. -1 if second object should come first, 1 otherwise. | |
238 | + */ | |
239 | + private function sort_callback( $a, $b ) { | |
240 | + if ( empty( $this->orderby ) ) { | |
241 | + return 0; | |
242 | + } | |
243 | + | |
244 | + $a = (array) $a; | |
245 | + $b = (array) $b; | |
246 | + | |
247 | + foreach ( $this->orderby as $field => $direction ) { | |
248 | + if ( ! isset( $a[ $field ] ) || ! isset( $b[ $field ] ) ) { | |
249 | + continue; | |
250 | + } | |
251 | + | |
252 | + if ( $a[ $field ] == $b[ $field ] ) { | |
253 | + continue; | |
254 | + } | |
255 | + | |
256 | + $results = 'DESC' === $direction ? array( 1, -1 ) : array( -1, 1 ); | |
257 | + | |
258 | + if ( is_numeric( $a[ $field ] ) && is_numeric( $b[ $field ] ) ) { | |
259 | + return ( $a[ $field ] < $b[ $field ] ) ? $results[0] : $results[1]; | |
260 | + } | |
261 | + | |
262 | + return 0 > strcmp( $a[ $field ], $b[ $field ] ) ? $results[0] : $results[1]; | |
263 | + } | |
264 | + | |
265 | + return 0; | |
266 | + } | |
267 | +} |
@@ -0,0 +1,789 @@ | ||
1 | +<?php | |
2 | +/** | |
3 | + * User API: WP_User class | |
4 | + * | |
5 | + * @package WordPress | |
6 | + * @subpackage Users | |
7 | + * @since 4.4.0 | |
8 | + */ | |
9 | + | |
10 | +/** | |
11 | + * Core class used to implement the WP_User object. | |
12 | + * | |
13 | + * @since 2.0.0 | |
14 | + * | |
15 | + * @property string $nickname | |
16 | + * @property string $description | |
17 | + * @property string $user_description | |
18 | + * @property string $first_name | |
19 | + * @property string $user_firstname | |
20 | + * @property string $last_name | |
21 | + * @property string $user_lastname | |
22 | + * @property string $user_login | |
23 | + * @property string $user_pass | |
24 | + * @property string $user_nicename | |
25 | + * @property string $user_email | |
26 | + * @property string $user_url | |
27 | + * @property string $user_registered | |
28 | + * @property string $user_activation_key | |
29 | + * @property string $user_status | |
30 | + * @property int $user_level | |
31 | + * @property string $display_name | |
32 | + * @property string $spam | |
33 | + * @property string $deleted | |
34 | + * @property string $locale | |
35 | + */ | |
36 | +class WP_User { | |
37 | + /** | |
38 | + * User data container. | |
39 | + * | |
40 | + * @since 2.0.0 | |
41 | + * @var object | |
42 | + */ | |
43 | + public $data; | |
44 | + | |
45 | + /** | |
46 | + * The user's ID. | |
47 | + * | |
48 | + * @since 2.1.0 | |
49 | + * @access public | |
50 | + * @var int | |
51 | + */ | |
52 | + public $ID = 0; | |
53 | + | |
54 | + /** | |
55 | + * The individual capabilities the user has been given. | |
56 | + * | |
57 | + * @since 2.0.0 | |
58 | + * @access public | |
59 | + * @var array | |
60 | + */ | |
61 | + public $caps = array(); | |
62 | + | |
63 | + /** | |
64 | + * User metadata option name. | |
65 | + * | |
66 | + * @since 2.0.0 | |
67 | + * @access public | |
68 | + * @var string | |
69 | + */ | |
70 | + public $cap_key; | |
71 | + | |
72 | + /** | |
73 | + * The roles the user is part of. | |
74 | + * | |
75 | + * @since 2.0.0 | |
76 | + * @access public | |
77 | + * @var array | |
78 | + */ | |
79 | + public $roles = array(); | |
80 | + | |
81 | + /** | |
82 | + * All capabilities the user has, including individual and role based. | |
83 | + * | |
84 | + * @since 2.0.0 | |
85 | + * @access public | |
86 | + * @var array | |
87 | + */ | |
88 | + public $allcaps = array(); | |
89 | + | |
90 | + /** | |
91 | + * The filter context applied to user data fields. | |
92 | + * | |
93 | + * @since 2.9.0 | |
94 | + * @access private | |
95 | + * @var string | |
96 | + */ | |
97 | + var $filter = null; | |
98 | + | |
99 | + /** | |
100 | + * @static | |
101 | + * @access private | |
102 | + * @var array | |
103 | + */ | |
104 | + private static $back_compat_keys; | |
105 | + | |
106 | + /** | |
107 | + * Constructor. | |
108 | + * | |
109 | + * Retrieves the userdata and passes it to WP_User::init(). | |
110 | + * | |
111 | + * @since 2.0.0 | |
112 | + * @access public | |
113 | + * | |
114 | + * @global wpdb $wpdb WordPress database abstraction object. | |
115 | + * | |
116 | + * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB. | |
117 | + * @param string $name Optional. User's username | |
118 | + * @param int $blog_id Optional Site ID, defaults to current site. | |
119 | + */ | |
120 | + public function __construct( $id = 0, $name = '', $blog_id = '' ) { | |
121 | + if ( ! isset( self::$back_compat_keys ) ) { | |
122 | + $prefix = $GLOBALS['wpdb']->prefix; | |
123 | + self::$back_compat_keys = array( | |
124 | + 'user_firstname' => 'first_name', | |
125 | + 'user_lastname' => 'last_name', | |
126 | + 'user_description' => 'description', | |
127 | + 'user_level' => $prefix . 'user_level', | |
128 | + $prefix . 'usersettings' => $prefix . 'user-settings', | |
129 | + $prefix . 'usersettingstime' => $prefix . 'user-settings-time', | |
130 | + ); | |
131 | + } | |
132 | + | |
133 | + if ( $id instanceof WP_User ) { | |
134 | + $this->init( $id->data, $blog_id ); | |
135 | + return; | |
136 | + } elseif ( is_object( $id ) ) { | |
137 | + $this->init( $id, $blog_id ); | |
138 | + return; | |
139 | + } | |
140 | + | |
141 | + if ( ! empty( $id ) && ! is_numeric( $id ) ) { | |
142 | + $name = $id; | |
143 | + $id = 0; | |
144 | + } | |
145 | + | |
146 | + if ( $id ) { | |
147 | + $data = self::get_data_by( 'id', $id ); | |
148 | + } else { | |
149 | + $data = self::get_data_by( 'login', $name ); | |
150 | + } | |
151 | + | |
152 | + if ( $data ) { | |
153 | + $this->init( $data, $blog_id ); | |
154 | + } else { | |
155 | + $this->data = new stdClass; | |
156 | + } | |
157 | + } | |
158 | + | |
159 | + /** | |
160 | + * Sets up object properties, including capabilities. | |
161 | + * | |
162 | + * @param object $data User DB row object. | |
163 | + * @param int $blog_id Optional. The site ID to initialize for. | |
164 | + */ | |
165 | + public function init( $data, $blog_id = '' ) { | |
166 | + $this->data = $data; | |
167 | + $this->ID = (int) $data->ID; | |
168 | + | |
169 | + $this->for_blog( $blog_id ); | |
170 | + } | |
171 | + | |
172 | + /** | |
173 | + * Return only the main user fields | |
174 | + * | |
175 | + * @since 3.3.0 | |
176 | + * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. | |
177 | + * | |
178 | + * @static | |
179 | + * | |
180 | + * @global wpdb $wpdb WordPress database abstraction object. | |
181 | + * | |
182 | + * @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'. | |
183 | + * @param string|int $value The field value | |
184 | + * @return object|false Raw user object | |
185 | + */ | |
186 | + public static function get_data_by( $field, $value ) { | |
187 | + global $wpdb; | |
188 | + | |
189 | + // 'ID' is an alias of 'id'. | |
190 | + if ( 'ID' === $field ) { | |
191 | + $field = 'id'; | |
192 | + } | |
193 | + | |
194 | + if ( 'id' == $field ) { | |
195 | + // Make sure the value is numeric to avoid casting objects, for example, | |
196 | + // to int 1. | |
197 | + if ( ! is_numeric( $value ) ) | |
198 | + return false; | |
199 | + $value = intval( $value ); | |
200 | + if ( $value < 1 ) | |
201 | + return false; | |
202 | + } else { | |
203 | + $value = trim( $value ); | |
204 | + } | |
205 | + | |
206 | + if ( !$value ) | |
207 | + return false; | |
208 | + | |
209 | + switch ( $field ) { | |
210 | + case 'id': | |
211 | + $user_id = $value; | |
212 | + $db_field = 'ID'; | |
213 | + break; | |
214 | + case 'slug': | |
215 | + $user_id = wp_cache_get($value, 'userslugs'); | |
216 | + $db_field = 'user_nicename'; | |
217 | + break; | |
218 | + case 'email': | |
219 | + $user_id = wp_cache_get($value, 'useremail'); | |
220 | + $db_field = 'user_email'; | |
221 | + break; | |
222 | + case 'login': | |
223 | + $value = sanitize_user( $value ); | |
224 | + $user_id = wp_cache_get($value, 'userlogins'); | |
225 | + $db_field = 'user_login'; | |
226 | + break; | |
227 | + default: | |
228 | + return false; | |
229 | + } | |
230 | + | |
231 | + if ( false !== $user_id ) { | |
232 | + if ( $user = wp_cache_get( $user_id, 'users' ) ) | |
233 | + return $user; | |
234 | + } | |
235 | + | |
236 | + if ( !$user = $wpdb->get_row( $wpdb->prepare( | |
237 | + "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value | |
238 | + ) ) ) | |
239 | + return false; | |
240 | + | |
241 | + update_user_caches( $user ); | |
242 | + | |
243 | + return $user; | |
244 | + } | |
245 | + | |
246 | + /** | |
247 | + * Makes private/protected methods readable for backward compatibility. | |
248 | + * | |
249 | + * @since 4.3.0 | |
250 | + * @access public | |
251 | + * | |
252 | + * @param callable $name Method to call. | |
253 | + * @param array $arguments Arguments to pass when calling. | |
254 | + * @return mixed|false Return value of the callback, false otherwise. | |
255 | + */ | |
256 | + public function __call( $name, $arguments ) { | |
257 | + if ( '_init_caps' === $name ) { | |
258 | + return call_user_func_array( array( $this, $name ), $arguments ); | |
259 | + } | |
260 | + return false; | |
261 | + } | |
262 | + | |
263 | + /** | |
264 | + * Magic method for checking the existence of a certain custom field. | |
265 | + * | |
266 | + * @since 3.3.0 | |
267 | + * @access public | |
268 | + * | |
269 | + * @param string $key User meta key to check if set. | |
270 | + * @return bool Whether the given user meta key is set. | |
271 | + */ | |
272 | + public function __isset( $key ) { | |
273 | + if ( 'id' == $key ) { | |
274 | + _deprecated_argument( 'WP_User->id', '2.1.0', | |
275 | + sprintf( | |
276 | + /* translators: %s: WP_User->ID */ | |
277 | + __( 'Use %s instead.' ), | |
278 | + '<code>WP_User->ID</code>' | |
279 | + ) | |
280 | + ); | |
281 | + $key = 'ID'; | |
282 | + } | |
283 | + | |
284 | + if ( isset( $this->data->$key ) ) | |
285 | + return true; | |
286 | + | |
287 | + if ( isset( self::$back_compat_keys[ $key ] ) ) | |
288 | + $key = self::$back_compat_keys[ $key ]; | |
289 | + | |
290 | + return metadata_exists( 'user', $this->ID, $key ); | |
291 | + } | |
292 | + | |
293 | + /** | |
294 | + * Magic method for accessing custom fields. | |
295 | + * | |
296 | + * @since 3.3.0 | |
297 | + * @access public | |
298 | + * | |
299 | + * @param string $key User meta key to retrieve. | |
300 | + * @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID. | |
301 | + */ | |
302 | + public function __get( $key ) { | |
303 | + if ( 'id' == $key ) { | |
304 | + _deprecated_argument( 'WP_User->id', '2.1.0', | |
305 | + sprintf( | |
306 | + /* translators: %s: WP_User->ID */ | |
307 | + __( 'Use %s instead.' ), | |
308 | + '<code>WP_User->ID</code>' | |
309 | + ) | |
310 | + ); | |
311 | + return $this->ID; | |
312 | + } | |
313 | + | |
314 | + if ( isset( $this->data->$key ) ) { | |
315 | + $value = $this->data->$key; | |
316 | + } else { | |
317 | + if ( isset( self::$back_compat_keys[ $key ] ) ) | |
318 | + $key = self::$back_compat_keys[ $key ]; | |
319 | + $value = get_user_meta( $this->ID, $key, true ); | |
320 | + } | |
321 | + | |
322 | + if ( $this->filter ) { | |
323 | + $value = sanitize_user_field( $key, $value, $this->ID, $this->filter ); | |
324 | + } | |
325 | + | |
326 | + return $value; | |
327 | + } | |
328 | + | |
329 | + /** | |
330 | + * Magic method for setting custom user fields. | |
331 | + * | |
332 | + * This method does not update custom fields in the database. It only stores | |
333 | + * the value on the WP_User instance. | |
334 | + * | |
335 | + * @since 3.3.0 | |
336 | + * @access public | |
337 | + * | |
338 | + * @param string $key User meta key. | |
339 | + * @param mixed $value User meta value. | |
340 | + */ | |
341 | + public function __set( $key, $value ) { | |
342 | + if ( 'id' == $key ) { | |
343 | + _deprecated_argument( 'WP_User->id', '2.1.0', | |
344 | + sprintf( | |
345 | + /* translators: %s: WP_User->ID */ | |
346 | + __( 'Use %s instead.' ), | |
347 | + '<code>WP_User->ID</code>' | |
348 | + ) | |
349 | + ); | |
350 | + $this->ID = $value; | |
351 | + return; | |
352 | + } | |
353 | + | |
354 | + $this->data->$key = $value; | |
355 | + } | |
356 | + | |
357 | + /** | |
358 | + * Magic method for unsetting a certain custom field. | |
359 | + * | |
360 | + * @since 4.4.0 | |
361 | + * @access public | |
362 | + * | |
363 | + * @param string $key User meta key to unset. | |
364 | + */ | |
365 | + public function __unset( $key ) { | |
366 | + if ( 'id' == $key ) { | |
367 | + _deprecated_argument( 'WP_User->id', '2.1.0', | |
368 | + sprintf( | |
369 | + /* translators: %s: WP_User->ID */ | |
370 | + __( 'Use %s instead.' ), | |
371 | + '<code>WP_User->ID</code>' | |
372 | + ) | |
373 | + ); | |
374 | + } | |
375 | + | |
376 | + if ( isset( $this->data->$key ) ) { | |
377 | + unset( $this->data->$key ); | |
378 | + } | |
379 | + | |
380 | + if ( isset( self::$back_compat_keys[ $key ] ) ) { | |
381 | + unset( self::$back_compat_keys[ $key ] ); | |
382 | + } | |
383 | + } | |
384 | + | |
385 | + /** | |
386 | + * Determine whether the user exists in the database. | |
387 | + * | |
388 | + * @since 3.4.0 | |
389 | + * @access public | |
390 | + * | |
391 | + * @return bool True if user exists in the database, false if not. | |
392 | + */ | |
393 | + public function exists() { | |
394 | + return ! empty( $this->ID ); | |
395 | + } | |
396 | + | |
397 | + /** | |
398 | + * Retrieve the value of a property or meta key. | |
399 | + * | |
400 | + * Retrieves from the users and usermeta table. | |
401 | + * | |
402 | + * @since 3.3.0 | |
403 | + * | |
404 | + * @param string $key Property | |
405 | + * @return mixed | |
406 | + */ | |
407 | + public function get( $key ) { | |
408 | + return $this->__get( $key ); | |
409 | + } | |
410 | + | |
411 | + /** | |
412 | + * Determine whether a property or meta key is set | |
413 | + * | |
414 | + * Consults the users and usermeta tables. | |
415 | + * | |
416 | + * @since 3.3.0 | |
417 | + * | |
418 | + * @param string $key Property | |
419 | + * @return bool | |
420 | + */ | |
421 | + public function has_prop( $key ) { | |
422 | + return $this->__isset( $key ); | |
423 | + } | |
424 | + | |
425 | + /** | |
426 | + * Return an array representation. | |
427 | + * | |
428 | + * @since 3.5.0 | |
429 | + * | |
430 | + * @return array Array representation. | |
431 | + */ | |
432 | + public function to_array() { | |
433 | + return get_object_vars( $this->data ); | |
434 | + } | |
435 | + | |
436 | + /** | |
437 | + * Set up capability object properties. | |
438 | + * | |
439 | + * Will set the value for the 'cap_key' property to current database table | |
440 | + * prefix, followed by 'capabilities'. Will then check to see if the | |
441 | + * property matching the 'cap_key' exists and is an array. If so, it will be | |
442 | + * used. | |
443 | + * | |
444 | + * @access protected | |
445 | + * @since 2.1.0 | |
446 | + * | |
447 | + * @global wpdb $wpdb WordPress database abstraction object. | |
448 | + * | |
449 | + * @param string $cap_key Optional capability key | |
450 | + */ | |
451 | + protected function _init_caps( $cap_key = '' ) { | |
452 | + global $wpdb; | |
453 | + | |
454 | + if ( empty($cap_key) ) | |
455 | + $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities'; | |
456 | + else | |
457 | + $this->cap_key = $cap_key; | |
458 | + | |
459 | + $this->caps = get_user_meta( $this->ID, $this->cap_key, true ); | |
460 | + | |
461 | + if ( ! is_array( $this->caps ) ) | |
462 | + $this->caps = array(); | |
463 | + | |
464 | + $this->get_role_caps(); | |
465 | + } | |
466 | + | |
467 | + /** | |
468 | + * Retrieve all of the role capabilities and merge with individual capabilities. | |
469 | + * | |
470 | + * All of the capabilities of the roles the user belongs to are merged with | |
471 | + * the users individual roles. This also means that the user can be denied | |
472 | + * specific roles that their role might have, but the specific user isn't | |
473 | + * granted permission to. | |
474 | + * | |
475 | + * @since 2.0.0 | |
476 | + * @access public | |
477 | + * | |
478 | + * @return array List of all capabilities for the user. | |
479 | + */ | |
480 | + public function get_role_caps() { | |
481 | + $wp_roles = wp_roles(); | |
482 | + | |
483 | + //Filter out caps that are not role names and assign to $this->roles | |
484 | + if ( is_array( $this->caps ) ) | |
485 | + $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) ); | |
486 | + | |
487 | + //Build $allcaps from role caps, overlay user's $caps | |
488 | + $this->allcaps = array(); | |
489 | + foreach ( (array) $this->roles as $role ) { | |
490 | + $the_role = $wp_roles->get_role( $role ); | |
491 | + $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities ); | |
492 | + } | |
493 | + $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); | |
494 | + | |
495 | + return $this->allcaps; | |
496 | + } | |
497 | + | |
498 | + /** | |
499 | + * Add role to user. | |
500 | + * | |
501 | + * Updates the user's meta data option with capabilities and roles. | |
502 | + * | |
503 | + * @since 2.0.0 | |
504 | + * @access public | |
505 | + * | |
506 | + * @param string $role Role name. | |
507 | + */ | |
508 | + public function add_role( $role ) { | |
509 | + if ( empty( $role ) ) { | |
510 | + return; | |
511 | + } | |
512 | + | |
513 | + $this->caps[$role] = true; | |
514 | + update_user_meta( $this->ID, $this->cap_key, $this->caps ); | |
515 | + $this->get_role_caps(); | |
516 | + $this->update_user_level_from_caps(); | |
517 | + | |
518 | + /** | |
519 | + * Fires immediately after the user has been given a new role. | |
520 | + * | |
521 | + * @since 4.3.0 | |
522 | + * | |
523 | + * @param int $user_id The user ID. | |
524 | + * @param string $role The new role. | |
525 | + */ | |
526 | + do_action( 'add_user_role', $this->ID, $role ); | |
527 | + } | |
528 | + | |
529 | + /** | |
530 | + * Remove role from user. | |
531 | + * | |
532 | + * @since 2.0.0 | |
533 | + * @access public | |
534 | + * | |
535 | + * @param string $role Role name. | |
536 | + */ | |
537 | + public function remove_role( $role ) { | |
538 | + if ( !in_array($role, $this->roles) ) | |
539 | + return; | |
540 | + unset( $this->caps[$role] ); | |
541 | + update_user_meta( $this->ID, $this->cap_key, $this->caps ); | |
542 | + $this->get_role_caps(); | |
543 | + $this->update_user_level_from_caps(); | |
544 | + | |
545 | + /** | |
546 | + * Fires immediately after a role as been removed from a user. | |
547 | + * | |
548 | + * @since 4.3.0 | |
549 | + * | |
550 | + * @param int $user_id The user ID. | |
551 | + * @param string $role The removed role. | |
552 | + */ | |
553 | + do_action( 'remove_user_role', $this->ID, $role ); | |
554 | + } | |
555 | + | |
556 | + /** | |
557 | + * Set the role of the user. | |
558 | + * | |
559 | + * This will remove the previous roles of the user and assign the user the | |
560 | + * new one. You can set the role to an empty string and it will remove all | |
561 | + * of the roles from the user. | |
562 | + * | |
563 | + * @since 2.0.0 | |
564 | + * @access public | |
565 | + * | |
566 | + * @param string $role Role name. | |
567 | + */ | |
568 | + public function set_role( $role ) { | |
569 | + if ( 1 == count( $this->roles ) && $role == current( $this->roles ) ) | |
570 | + return; | |
571 | + | |
572 | + foreach ( (array) $this->roles as $oldrole ) | |
573 | + unset( $this->caps[$oldrole] ); | |
574 | + | |
575 | + $old_roles = $this->roles; | |
576 | + if ( !empty( $role ) ) { | |
577 | + $this->caps[$role] = true; | |
578 | + $this->roles = array( $role => true ); | |
579 | + } else { | |
580 | + $this->roles = false; | |
581 | + } | |
582 | + update_user_meta( $this->ID, $this->cap_key, $this->caps ); | |
583 | + $this->get_role_caps(); | |
584 | + $this->update_user_level_from_caps(); | |
585 | + | |
586 | + /** | |
587 | + * Fires after the user's role has changed. | |
588 | + * | |
589 | + * @since 2.9.0 | |
590 | + * @since 3.6.0 Added $old_roles to include an array of the user's previous roles. | |
591 | + * | |
592 | + * @param int $user_id The user ID. | |
593 | + * @param string $role The new role. | |
594 | + * @param array $old_roles An array of the user's previous roles. | |
595 | + */ | |
596 | + do_action( 'set_user_role', $this->ID, $role, $old_roles ); | |
597 | + } | |
598 | + | |
599 | + /** | |
600 | + * Choose the maximum level the user has. | |
601 | + * | |
602 | + * Will compare the level from the $item parameter against the $max | |
603 | + * parameter. If the item is incorrect, then just the $max parameter value | |
604 | + * will be returned. | |
605 | + * | |
606 | + * Used to get the max level based on the capabilities the user has. This | |
607 | + * is also based on roles, so if the user is assigned the Administrator role | |
608 | + * then the capability 'level_10' will exist and the user will get that | |
609 | + * value. | |
610 | + * | |
611 | + * @since 2.0.0 | |
612 | + * @access public | |
613 | + * | |
614 | + * @param int $max Max level of user. | |
615 | + * @param string $item Level capability name. | |
616 | + * @return int Max Level. | |
617 | + */ | |
618 | + public function level_reduction( $max, $item ) { | |
619 | + if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) { | |
620 | + $level = intval( $matches[1] ); | |
621 | + return max( $max, $level ); | |
622 | + } else { | |
623 | + return $max; | |
624 | + } | |
625 | + } | |
626 | + | |
627 | + /** | |
628 | + * Update the maximum user level for the user. | |
629 | + * | |
630 | + * Updates the 'user_level' user metadata (includes prefix that is the | |
631 | + * database table prefix) with the maximum user level. Gets the value from | |
632 | + * the all of the capabilities that the user has. | |
633 | + * | |
634 | + * @since 2.0.0 | |
635 | + * @access public | |
636 | + * | |
637 | + * @global wpdb $wpdb WordPress database abstraction object. | |
638 | + */ | |
639 | + public function update_user_level_from_caps() { | |
640 | + global $wpdb; | |
641 | + $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 ); | |
642 | + update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level ); | |
643 | + } | |
644 | + | |
645 | + /** | |
646 | + * Add capability and grant or deny access to capability. | |
647 | + * | |
648 | + * @since 2.0.0 | |
649 | + * @access public | |
650 | + * | |
651 | + * @param string $cap Capability name. | |
652 | + * @param bool $grant Whether to grant capability to user. | |
653 | + */ | |
654 | + public function add_cap( $cap, $grant = true ) { | |
655 | + $this->caps[$cap] = $grant; | |
656 | + update_user_meta( $this->ID, $this->cap_key, $this->caps ); | |
657 | + $this->get_role_caps(); | |
658 | + $this->update_user_level_from_caps(); | |
659 | + } | |
660 | + | |
661 | + /** | |
662 | + * Remove capability from user. | |
663 | + * | |
664 | + * @since 2.0.0 | |
665 | + * @access public | |
666 | + * | |
667 | + * @param string $cap Capability name. | |
668 | + */ | |
669 | + public function remove_cap( $cap ) { | |
670 | + if ( ! isset( $this->caps[ $cap ] ) ) { | |
671 | + return; | |
672 | + } | |
673 | + unset( $this->caps[ $cap ] ); | |
674 | + update_user_meta( $this->ID, $this->cap_key, $this->caps ); | |
675 | + $this->get_role_caps(); | |
676 | + $this->update_user_level_from_caps(); | |
677 | + } | |
678 | + | |
679 | + /** | |
680 | + * Remove all of the capabilities of the user. | |
681 | + * | |
682 | + * @since 2.1.0 | |
683 | + * @access public | |
684 | + * | |
685 | + * @global wpdb $wpdb WordPress database abstraction object. | |
686 | + */ | |
687 | + public function remove_all_caps() { | |
688 | + global $wpdb; | |
689 | + $this->caps = array(); | |
690 | + delete_user_meta( $this->ID, $this->cap_key ); | |
691 | + delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' ); | |
692 | + $this->get_role_caps(); | |
693 | + } | |
694 | + | |
695 | + /** | |
696 | + * Whether user has capability or role name. | |
697 | + * | |
698 | + * While checking against particular roles in place of a capability is supported | |
699 | + * in part, this practice is discouraged as it may produce unreliable results. | |
700 | + * | |
701 | + * @since 2.0.0 | |
702 | + * @access public | |
703 | + * | |
704 | + * @see map_meta_cap() | |
705 | + * | |
706 | + * @param string $cap Capability name. | |
707 | + * @param int $object_id,... Optional. ID of the specific object to check against if `$cap` is a "meta" cap. | |
708 | + * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used | |
709 | + * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts', | |
710 | + * 'edit_others_posts', etc. The parameter is accessed via func_get_args() and passed | |
711 | + * to map_meta_cap(). | |
712 | + * @return bool Whether the current user has the given capability. If `$cap` is a meta cap and `$object_id` is | |
713 | + * passed, whether the current user has the given meta capability for the given object. | |
714 | + */ | |
715 | + public function has_cap( $cap ) { | |
716 | + if ( is_numeric( $cap ) ) { | |
717 | + _deprecated_argument( __FUNCTION__, '2.0.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') ); | |
718 | + $cap = $this->translate_level_to_cap( $cap ); | |
719 | + } | |
720 | + | |
721 | + $args = array_slice( func_get_args(), 1 ); | |
722 | + $args = array_merge( array( $cap, $this->ID ), $args ); | |
723 | + $caps = call_user_func_array( 'map_meta_cap', $args ); | |
724 | + | |
725 | + // Multisite super admin has all caps by definition, Unless specifically denied. | |
726 | + if ( is_multisite() && is_super_admin( $this->ID ) ) { | |
727 | + if ( in_array('do_not_allow', $caps) ) | |
728 | + return false; | |
729 | + return true; | |
730 | + } | |
731 | + | |
732 | + /** | |
733 | + * Dynamically filter a user's capabilities. | |
734 | + * | |
735 | + * @since 2.0.0 | |
736 | + * @since 3.7.0 Added the user object. | |
737 | + * | |
738 | + * @param array $allcaps An array of all the user's capabilities. | |
739 | + * @param array $caps Actual capabilities for meta capability. | |
740 | + * @param array $args Optional parameters passed to has_cap(), typically object ID. | |
741 | + * @param WP_User $user The user object. | |
742 | + */ | |
743 | + $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this ); | |
744 | + | |
745 | + // Everyone is allowed to exist. | |
746 | + $capabilities['exist'] = true; | |
747 | + | |
748 | + // Must have ALL requested caps. | |
749 | + foreach ( (array) $caps as $cap ) { | |
750 | + if ( empty( $capabilities[ $cap ] ) ) | |
751 | + return false; | |
752 | + } | |
753 | + | |
754 | + return true; | |
755 | + } | |
756 | + | |
757 | + /** | |
758 | + * Convert numeric level to level capability name. | |
759 | + * | |
760 | + * Prepends 'level_' to level number. | |
761 | + * | |
762 | + * @since 2.0.0 | |
763 | + * @access public | |
764 | + * | |
765 | + * @param int $level Level number, 1 to 10. | |
766 | + * @return string | |
767 | + */ | |
768 | + public function translate_level_to_cap( $level ) { | |
769 | + return 'level_' . $level; | |
770 | + } | |
771 | + | |
772 | + /** | |
773 | + * Set the site to operate on. Defaults to the current site. | |
774 | + * | |
775 | + * @since 3.0.0 | |
776 | + * | |
777 | + * @global wpdb $wpdb WordPress database abstraction object. | |
778 | + * | |
779 | + * @param int $blog_id Optional. Site ID, defaults to current site. | |
780 | + */ | |
781 | + public function for_blog( $blog_id = '' ) { | |
782 | + global $wpdb; | |
783 | + if ( ! empty( $blog_id ) ) | |
784 | + $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; | |
785 | + else | |
786 | + $cap_key = ''; | |
787 | + $this->_init_caps( $cap_key ); | |
788 | + } | |
789 | +} |