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

PrivateContent Plugin

API Documentation

Getting Started

Overview

#

This documentation is addressed to web developers wanting to tweak or extend PrivateContent.

To be as compatible as possible with the core plugin and avoid troubles during updates, it is strongly suggested to use the classes and functions documented here. Especially to manage users and their data: they perform validations, trigger the right hooks and keep add-ons in sync. Direct queries on PrivateContent tables bypass all of this.

If you think any useful function or method is missing, feel free to get in touch at support [at] lcweb.it and we can talk about its implementation!

Let's briefly understand how PrivateContent works under the hood.

When PrivateContent is ready

#

PrivateContent elements are not all available at the same moment. Here is the loading sequence:

  1. plugin files inclusion: global objects $pc_users, $pc_meta and $pc_wp_user are created
  2. plugins_loaded (priority 1): PC_USERS_TABLE and PC_META_TABLE constants are defined
  3. plugins_loaded (priority 100): the $pc_restr_wizard global object is created
  4. wp_loaded (priority 1): the pvtcont_init action is fired. At its priority 1 the logged user is identified and $GLOBALS['pc_user_id'] is set
  5. right after the user check, the pc_user_session_checked action is fired

Then, the right place for code involving PrivateContent users is pvtcont_init (using a priority higher than 1) instead of WordPress init or wp_loaded.

// code involving PrivateContent users
add_action('pvtcont_init', function() {
	if(pc_user_logged(false)) {
		// a PrivateContent user is logged
	}
}, 10);

Custom fields registered through pc_form_fields_filter are cached on the first form framework initialization. Add the filter as soon as possible (eg. in your plugin's main file or on plugins_loaded), never inside a later hook.

Restriction side

#

The essential resource is the pc_user_logged() API function: use it to know whether a user is logged and to get its data.

Normally restrictions are based on user categories, then you should use the pc_user_check() API function. It returns 1 (allowed), 2 (wrong permissions) or false (not logged): always compare its result strictly, since 2 is a truthy value.

if(pc_user_check(array(2, 5)) === 1) {
	// user belongs to category 2 or 5
}

To know whether the current user can see a specific post or term, considering restrictions inherited from parents and categories, use the Restrictions class.

The fastest way to get the logged user ID, without queries, is the global variable. However it is surely safer to use pc_user_logged('id'), checking the database in real time (eg. a user disabled in the meantime).

if(isset($GLOBALS['pc_user_id'])) {
	$user_id = (int)$GLOBALS['pc_user_id'];
}
else {
	// no user logged
}

Users management side

#

As you probably know, PrivateContent users are apart from WordPress ones and stored in dedicated database tables. This unless you use the WP User Sync system, creating WordPress "mirrors" and eventually emulating their roles.

Using WP User Sync, users are also able to use WordPress-based login and logout systems. In any case, PrivateContent users won't be able to reach wp-admin.

To insert and manage users, use the Users Management class methods. To extend stored data, in most cases the User Meta table will be more than enough and it is already integrated in the APIs.

In case of new fields, register them in the form framework through the pc_form_fields_filter filter. It is essential to trigger automatic data validation and to use them in forms, exports and imports.

Users private page is a custom post type named pg_user_page. The container page contents are replaced for each user. To extend the users reserved area, target the container page (to manage global layout or sidebar) or use the pc_pvt_page_contents filter to inject contents.

Security notes

#

Membership data is sensitive: keep these points in mind when extending PrivateContent.

  • Never trust user IDs coming from requests. To know who is acting, use pc_user_logged('id'), never a value passed through $_POST or $_GET
  • PrivateContent users are not WordPress users: in your wp_ajax_nopriv_ handlers identify them through pc_user_logged() and protect state-changing requests with a nonce or with pc_static::is_same_origin_request()
  • the custom_search parameter of get_users() is raw SQL: escape any dynamic value with $wpdb->prepare() or esc_sql(). Also whitelist values passed to the order and orderby parameters
  • always escape users data when printing it (esc_html(), esc_attr(), esc_url()): it has been typed by your visitors
  • the pc_registered_user action receives form data, password included in plain text: never store or log it
  • never unserialize() data coming from requests. For stored values use maybe_unserialize() or JSON

Let's get in touch!

Features list