Introduction
PHP class used to manage users: search, insertion, editing, deletion.
Is instantiated in website and reachable through php global pc_users.
Located in classes folder.
PHP class used to manage users: search, insertion, editing, deletion.
Is instantiated in website and reachable through php global pc_users.
Located in classes folder.
(array) $fixed_fields | array of database columns. Useful to detect extra fields and metas |
---|---|
(string) $validation_errors | static resource for methods using validator - contains HTML code with errors |
(string) $wp_sync_error | static resource for WP sync errors printing - contains a string |
(int) [protected] $user_id | static resource storing currently managed user ID for sequential operations |
(bool) $wp_user_sync; | flag to understand if wp-user-sync is enabled |
Method used to perform searches in users database to retrieve basic data and/or meta-data or simply to count results
get_users($args = array())
Only one parameter containing an associative array of data.
When nothing is specified, first 100 users - id sorted - are searched. Search retrieves basic data + all metas.
Is a quite big query to perform depending on your database. Try to be as specific as possible.
$args associative array keys => values:
key | value | default |
---|---|---|
(int/array) user_id | specific user ID or IDs array to fetch (by default queries every user) | false |
(int) limit | query limit (related to users) - default 100 (use -1 to fetch any) | 100 |
(int) offset | query offset (related to users) | 0 |
(int/bool) status | user status (1 = active / 2 = disabled / 3 = pending) - by default uses false to fetch any | false |
(int/array) categories | PrivateContent user category IDs | false |
(array) to_get | users data to fetch. Could be a fixed field or a meta key - by default is everything | array() |
(string) orderby | how to sort query results - Could be a fixed field or a meta key | id |
(string) order | sorting method (ASC / DESC) | ASC |
(array) search | multidimesional array of associative array, inspired by WP_query tax_query format. - Each search block is joint with AND condition - supported operators (=, !=, >, <, >=, <=, IN, NOT IN, LIKE, NOT LIKE) Structure example: array( array( 'relation' => 'AND', // AND or OR array('key'=>$field_key, 'operator'=>'=', 'val'=>$value), array('key'=>$field_key, 'operator'=>'=', 'val'=>$value) ) ) | array |
(string) custom_search | custom WHERE parameters to customize the search. Is added to the dynamically created code | false |
(bool) count | if true returns only query rows count | false |
(array) | array of associative arrays (key => val) containing matched users and queried data (id is always included) |
---|---|
(int) | query row count - only if count parameter is set to true |
<?php // search first 4 active users with an e-mail and username containing 'andrew' - getting only few data - and print them global $pc_users; $args = array( 'limit' => 4, 'status' => 1, 'to_get' => array('name', 'username', 'email'), 'search' => array( array( 'relation' => 'AND', array('key' => 'email', 'operator' => '!=', 'val' => ''), array('key' => 'username', 'operator' => 'LIKE', 'val' => '%andrew%'), ) ) ); $users = $pc_users->get_users($args); if(empty($users)) { // no users found } else { foreach($users as $u) { echo ''. $u['id'] .'
'; echo ''. $u['name'] .'
'; echo ''. $u['username'] .'
'; echo ''. $u['email'] .'
'; } } ?>
Method used to create query part to search for user categories
categories_query($cats)
(array) $cats | user categories ID array |
---|
(string) | WHERE condition part related to these categories |
---|
Method used to get a specific user's data
get_user($user_id, $args = array())
(int) $user_id | user ID to match |
---|---|
(array) $args | get_users query args (except user_id index) |
(bool/array) | false if user is not found otherwise associative data array for the user |
---|
<?php // get only few data for user = 5 and print them global $pc_users; $args = array( 'to_get' => array('name', 'username', 'email'), ); $user = $pc_users->get_user(5, $args); echo ''. $user['name'] .'
'; echo ''. $user['username'] .'
'; echo ''. $user['email'] .'
'; ?>
Method used to get a specific user's field
get_user_field($user_id, $field)
(int) $user_id | user ID to match |
---|---|
(string) $field | field name to retreve - could be basic data or meta data |
(bool/mixed) | false if user is not found otherwise the field value |
---|
<?php // get username for user ID = 5 global $pc_users; $username = $pc_users->get_user_field(5, 'username'); ?>
Method used to print fetched data. Get user category names, implodes arrays, renders dates and single-option checks.
data_to_human($index, $data, $ignore_dates = false)
(string) $index | index relative to the value stored in database (could be a fixed field or a meta key) |
---|---|
(mixed) $data | fetched data related to the index |
(bool) $ignore_dates | whether to ignore insert and registration dates |
(string) | value string ready to be printed |
---|
<?php // print user categories global $pc_users; $cats = $pc_users->get_user_field(5, 'categories'); echo 'Categories: '. $pc_users->data_to_human('categories', $cats); ?>
Method used to insert a user into PrivateContent database.
Performs also data validation on passed values as specified in registered PrivateContent fields.
Eventually performs also meta-data insertion.
insert_user($data, $status = 1, $allow_wp_sync_fail = false)
(array) $data | registration data - see table below to know more |
---|---|
(int) $status | new user's status (1=active, 2=disabled, 3=pending) |
(bool) $allow_wp_sync_fail | whether to allow registration also if WP user sync fails |
$data is an associative array, containing user's fixed data and meta data.
The function already performs validation. Here are specifications (pay attention to mandatory ones).
key | notes |
---|---|
(string) name | user's name - max length 150 characters |
(string) surname | user's surname - max length 150 characters |
(string) (mandatory) username | user's username - max length 150 characters |
(string) tel | user's telephone - max length 20 characters |
(string) email | user's email. Must be a valid e-mail with max length of 255 characters. If WP-sync enabled is mandatory |
(string) (mandatory) psw | user's password |
(int) disable_pvt_page | 0 to enable user's private page, 1 to disable |
(array) (mandatory) categories | user categories. Use IDs |
(int) | newly inserted user ID |
---|---|
(bool) | false if problems are found (and you should check $validation_errors and $wp_sync_error properties) |
<?php // insert a new user checking the answer global $pc_users; $data = array( 'name' => 'test', 'username' => 'lcweb', 'email' => 'test@lcweb.it', 'categories'=> array(2,4) ); $result = $pc_users->insert_user($data, 3, true); if(!$result) { // an error occurred - check related properties var_dump($pc_users->validation_errors, $pc_users->wp_sync_error); } else { // user successfully created and placed in pending status } ?>
Method used to update a user into PrivateContent database.
Performs also data validation on passed values as specified in registered PrivateContent fields.
Eventually performs also meta data update.
update_user($user_id, $data)
(int) $user_id | user ID to update |
---|---|
(array) $data | User data. Associative array containing fixed fields and meta data. Check insert_user to know more. Plus, you can use status key |
(bool) | true is successfully updated otherwise false |
---|
<?php // update user 5 - setting new email and surname global $pc_users; $data = array( 'email' => 'test2@lcweb.it', 'surname' => 'testing' ); $result = $pc_users->update_user(5, $data); if(!$result) { // an error occurred - check related properties } else { // user successfully updated } ?>
Method used to update status for oe or more users
change_status($users_id, $new_status)
(int/array) $user_id | user ID to update or array of user IDs |
---|---|
(int) $new_status/th> | new status (1 = active / 2 = disabled / 3 = pending) |
(int) | number of users with changed status (zero could mean user already had that status) |
---|
<?php // set two users in disabled status global $pc_users; $pc_users->change_status(array(5, 6), 2); ?>
Method used to delete a user.
Totally remove user and its meta data + private page content + eventual WP user sync
delete_user($user_id)
(int) $user_id | user ID to remove |
---|
(bool) | true if user has been deleted otherwise false |
---|
<?php // delete user #5 global $pc_users; if($pc_users->delete_user(5)) { // user successfully deleted } ?>
Method used to check if an e-mail is already in database. Useful to avoid doubled values.
user_mail_exists($email, $user_id = false)
(string) $email | email to check |
---|---|
(int) $user_id | User ID to exclude from query (useful to update user's email) |
(int) | user ID having same e-mail |
---|---|
(bool) | true is unique |
Documentation in progress ..
Method used to get user ID from username.
username_to_id($username)
(int) $username | the user username |
---|
(int) | matched user ID |
---|---|
(bool) | false if user not found |
Method used to get user username from ID.
id_to_username($user_id)
(int) $user_id | the user ID |
---|
(string) | matched user username |
---|---|
(bool) | false if user not found |
Method used to check a user ID ans be sure it exists in database.
This is a protected method. Use only in class extensions.
check_user_id($subj)
(int/string) $subj | variable used to target anuser via id or username |
---|
(int) | the user id or zero |
---|