Ends in
00Days
00Hours
00Mins
00Secs
Save up to 50%

PrivateContent Plugin

API Documentation

WP User Sync Class

Introduction

#

Defined in classes/wp_user_sync.php

PHP class used to manage relationships between PrivateContent users and their WordPress mirrors: sync, detach, checks.

It is instantiated on plugin's load and reachable through the $pc_wp_user PHP global. It extends the main pc_users class.

Synced WordPress users have the pvtcontent role (plus the custom roles set in settings) and share the same password hash of the PrivateContent user.

Properties

#
PropertyDescription
(array) $forbidden_rolesroles that can't be assigned to synced users (administrator, editor, author, pvtcontent_admin ..)
(array|false) $wps_rolescustom roles assigned to synced users. False until get_wps_custom_roles() or set_wps_custom_roles() are used
(array) $privileged_capscapabilities considered administrative (manage_options, edit_users, install_plugins ..)
(string|false) $pwu_sync_errorerror found by the last sync operation

global_sync()

method#

Syncs every PrivateContent user not synced yet. Users must have a unique e-mail and username to be synced.

Usage

global_sync()

Parameters

No parameters

Return values

TypeDescription
(string)message reporting how many users have been synced and how many failed

sync_wp_user()

method#

Creates the WordPress mirror of a PrivateContent user, or updates an existing one. On creation, the pc_user_synced_with_wp action is triggered.

Usage

sync_wp_user($user_data = array(), $existing_id = 0, $save_in_db = false)

Parameters

ParameterDescription
(array) $user_datadata to use for the WP user - check the table below
(int) $existing_idsynced WordPress user ID to update. Zero to create a new one
(bool) $save_in_dbwhether to save the created WordPress user ID in the PrivateContent user record

User data

KeyNotes
(string) usernamerequired creating a new user
(string) emailrequired creating a new user
(string) pswrequired creating a new user. It must be the hashed password stored in the PrivateContent database
(int) idPrivateContent user ID, stored in the pc_user_id WP user meta
(string) nameused as WP first name
(string) surnameused as WP last name

Return values

TypeDescription
(int)created/updated WordPress user ID
(false)if an error occurred (check the $pwu_sync_error property)

Example

global $pc_users, $pc_wp_user;

$data = $pc_users->get_user(5, array('to_get' => array('id', 'username', 'email', 'psw', 'name', 'surname')));
$wp_user_id = $pc_wp_user->sync_wp_user($data, 0, true);

if(!$wp_user_id) {
	$error = $pc_wp_user->pwu_sync_error;
}

sync_psw_to_wp()

method#

Copies a PrivateContent password hash into the synced WordPress user.

Usage

sync_psw_to_wp($psw, $wp_user_id)

Parameters

ParameterDescription
(string) $pswalready hashed password
(int) $wp_user_idWordPress user ID

Return values

TypeDescription
(int|false)$wpdb->update() response

sync_psw_to_pc()

method#

Copies a WordPress user password hash into the synced PrivateContent user. If that user is logged, its login cookie is updated.

Usage

sync_psw_to_pc($wp_user_id, $pc_user_id)

Parameters

ParameterDescription
(int) $wp_user_idWordPress user ID
(int) $pc_user_idPrivateContent user ID

Return values

TypeDescription
(bool)true if the password has been synced

search_and_sync_matches()

method#

Searches matches between unsynced PrivateContent users and WordPress users having the same username and e-mail, then syncs them. Be careful: matched WordPress users become PrivateContent mirrors (role and password are overwritten).

Usage

search_and_sync_matches()

Parameters

No parameters

Return values

TypeDescription
(string)message reporting how many users have been synced

pc_user_from_wp()

method#

Creates a PrivateContent user starting from an existing WordPress user, then syncs them (the WordPress password is kept). Administrators and PrivateContent admins can't be used. On success the pc_user_created_from_wp action is triggered.

Usage

pc_user_from_wp($wp_user_id, $pc_cats)

Parameters

ParameterDescription
(int) $wp_user_idWordPress user ID
(array) $pc_catsuser category IDs to assign

Return values

TypeDescription
(int)created PrivateContent user ID
(WP_Error)if the user can't be created

Example

global $pc_wp_user;

$result = $pc_wp_user->pc_user_from_wp(12, array(3));
if(is_wp_error($result)) {
	echo esc_html($result->get_error_message());
}

global_detach()

method#

Detaches every synced user. WordPress mirror users are deleted.

Usage

global_detach()

Parameters

No parameters

Return values

TypeDescription
(string)message confirming the operation

detach_wp_user()

method#

Detaches a PrivateContent user from its WordPress mirror, deleting the WordPress user. The pc_user_detached_from_wp action is triggered right before the deletion.

Usage

detach_wp_user($user_id, $save_in_db = true, $wp_user_id = false)

Parameters

ParameterDescription
(int) $user_idPrivateContent user ID
(bool) $save_in_dbwhether to update the PrivateContent user record
(int|false) $wp_user_idsynced WordPress user ID, if already known (saves a query)

Return values

TypeDescription
(bool)true, or false if WordPress user deletion functions are not available

Example

global $pc_wp_user;
$pc_wp_user->detach_wp_user(5);

wp_user_is_linked()

method#

Checks whether a WordPress user is synced with a PrivateContent one.

Usage

wp_user_is_linked($wp_user_id)

Parameters

ParameterDescription
(int) $wp_user_idWordPress user ID

Return values

TypeDescription
(object)PrivateContent user data object, containing id, username, categories (serialized), psw (hash) and status
(null|false)if the user is not synced

Example

global $pc_wp_user;

$pc_user = $pc_wp_user->wp_user_is_linked(get_current_user_id());
if($pc_user) {
	$pc_user_id = (int)$pc_user->id;
}

pvtc_is_synced()

method#

Checks whether a PrivateContent user is synced with a WordPress one.

Usage

pvtc_is_synced($user_id, $return_id = false)

Parameters

ParameterDescription
(int) $user_idPrivateContent user ID
(bool) $return_idwhether to directly return the WordPress user ID

Return values

TypeDescription
(WP_User|int)WordPress user object (got through get_userdata()) or its ID
(false)if user is not synced

manual_user_login()

method#

Logs in a synced WordPress user (used by PrivateContent login systems). Any other logged WordPress user is logged out. Triggers the WordPress wp_login action.

Usage

manual_user_login($wp_user_id, $username, $remember_me = false)

Parameters

ParameterDescription
(int) $wp_user_idWordPress user ID
(string) $usernameWordPress username
(bool) $remember_mewhether to use a long-lasting authentication cookie

Use pc_login() to log in users: it also manages the WordPress mirror login.

update_nicename()

method#

Updates a WordPress user nicename and display name, using its first and last name (or username).

Usage

update_nicename($wp_user_id)

Parameters

ParameterDescription
(int) $wp_user_idWordPress user ID

Return values

No value returned

new_mail_is_ok()

method#

Checks whether an e-mail can be assigned to a WordPress user (not used by other WordPress users).

Usage

new_mail_is_ok($wp_user_id, $email)

Parameters

ParameterDescription
(int) $wp_user_idWordPress user ID
(string) $emailnew e-mail

Return values

TypeDescription
(bool)true if the e-mail can be used

wp_user_exists()

method#

Checks whether a username or an e-mail are already used by a WordPress user.

Usage

wp_user_exists($username, $email)

Parameters

ParameterDescription
(string) $usernameusername to check
(string) $emaile-mail to check

Return values

TypeDescription
(bool)true if one of them is used, otherwise false

get_wps_custom_roles()

method#

Gets roles assigned to synced users: pvtcontent plus the custom ones set in settings.

Usage

get_wps_custom_roles()

Parameters

No parameters

Return values

TypeDescription
(array)role slugs

set_wps_custom_roles()

method#

Applies roles set in settings to synced WordPress users.

Usage

set_wps_custom_roles($wp_user_id = false, $is_new_user = false)

Parameters

ParameterDescription
(int|array|false) $wp_user_idWordPress user ID or IDs array. False to update every synced user
(bool) $is_new_userif true, skips the update when the only role to assign is pvtcontent

Return values

TypeDescription
(bool)true if the operation succeeded, otherwise false

role_is_privileged()

method#

Checks whether a WordPress role has administrative capabilities (listed in the $privileged_caps property).

Usage

role_is_privileged($role_id)

Parameters

ParameterDescription
(string) $role_idrole slug

Return values

TypeDescription
(bool)true if the role is privileged

wp_user_is_privileged()

method#

Checks whether a WordPress user has administrative capabilities (listed in the $privileged_caps property).

Usage

wp_user_is_privileged($wp_user_id)

Parameters

ParameterDescription
(int) $wp_user_idWordPress user ID

Return values

TypeDescription
(bool)true if the user is privileged

Let's get in touch!

Features list