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

PrivateContent Plugin

API Documentation

Static Helpers Class

Introduction

#

Defined in classes/pc_static.php

pc_static is the class collecting PrivateContent helper methods. Here are the ones useful when extending the plugin: call them statically.

$cats = pc_static::user_cats();

user_cats()

method#

Gets user categories (already translated through WPML/Polylang). Cached for the current request.

Usage

pc_static::user_cats($escape_no_reg = false)

Parameters

ParameterDescription
(bool) $escape_no_regwhether to exclude categories not allowed on registration

Return values

TypeDescription
(array)associative array (category ID => name)

restr_opts_arr()

method#

Gets restriction options, grouped by subject: PrivateContent categories plus the ones added by add-ons through pc_user_cat_dd_opts. The onlyvals_restr_opts_arr() method, having the same parameters, returns a flat array (value => label).

Usage

pc_static::restr_opts_arr($bulk_opts = true, $apply_filter = true, $remove = array())

Parameters

ParameterDescription
(bool) $bulk_optswhether to include "any logged user" and "unlogged users" options
(bool) $apply_filterwhether to include options added by add-ons
(array) $removeoption values to discard

Return values

TypeDescription
(array)array('pc_cats' => array('name' => .., 'opts' => array(value => label)), ..)

affected_pt()

method#

Gets post types affected by the restrictions engine. affected_tax() does the same with taxonomies. They can be extended through pvtcont_affected_pt and pvtcont_affected_tax.

Usage

pc_static::affected_pt()

Parameters

No parameters

Return values

TypeDescription
(array)post type slugs

is_same_origin_request()

method#

CSRF protection: checks whether the request comes from the website itself, through the browser Origin header. Requests without that header are accepted, to not break old browsers. Allowed hosts can be extended through pc_same_origin_allowed_hosts.

Usage

pc_static::is_same_origin_request()

Parameters

No parameters

Return values

TypeDescription
(bool)true if the request comes from an allowed host

Example

add_action('wp_ajax_nopriv_my_action', function() {
	if(!pc_static::is_same_origin_request() || !pc_user_logged(false)) {
		wp_send_json_error();
	}

	$user_id = (int)pc_user_logged('id');
	// ...
});

verify_nonce()

method#

Shortcut for wp_verify_nonce(), sanitizing the passed value.

Usage

pc_static::verify_nonce($str, $key)

Parameters

ParameterDescription
(string) $strnonce value (eg. $_POST['nonce'])
(string) $keynonce action

Return values

TypeDescription
(bool)true if the nonce is valid

sanitize_val()

method#

Recursively sanitizes submitted data: plain texts pass through sanitize_text_field() logic (keeping line breaks), while values containing HTML pass through an extended wp_kses_post().

Usage

pc_static::sanitize_val($data)

Parameters

ParameterDescription
(mixed) $datavalue or array to sanitize

Return values

TypeDescription
(mixed)sanitized data

login_cookie_duration()

method#

Gets the login cookie expiration timestamp, considering "remember me" (2 weeks), short-lived logins (10 minutes) or standard ones (1 hour). Customizable through pc_login_cookie_duration.

Usage

pc_static::login_cookie_duration($remember_me = false)

Parameters

ParameterDescription
(bool) $remember_mewhether the "remember me" option is used

Return values

TypeDescription
(int)expiration UNIX timestamp

curr_url()

method#

Gets the current page URL.

Usage

pc_static::curr_url()

Parameters

No parameters

Return values

TypeDescription
(string)current URL

man_url_attr()

method#

Adds, edits or removes URL query parameters.

Usage

pc_static::man_url_attr($action, $param_name, $val = false, $url = false)

Parameters

ParameterDescription
(string) $actionadd, edit or remove
(string|array) $param_nameone or more parameter names
(mixed) $valnew value (values array matching parameters array). Ignored removing
(string|false) $urlURL to manage. False to use the current URL

Return values

TypeDescription
(string)resulting URL

encrypt_number()

method#

Obfuscates a number, to not show it directly on frontend (eg. registration form IDs). decrypt_number($string) reverts it. This is not an encryption: never use it to protect sensitive data.

Usage

pc_static::encrypt_number($number)

Parameters

ParameterDescription
(int) $numbernumber to obfuscate

Return values

TypeDescription
(string)obfuscated string

safe_inline_script()

method#

Properly enqueues inline javascript on frontend. In AJAX contexts returns the code to be printed.

Usage

pc_static::safe_inline_script($unique_id, $code, $priority = 99999)

Parameters

ParameterDescription
(string) $unique_idunique script ID (the same code is printed only once)
(string) $codejavascript code, without script tags
(int) $priorityfooter printing priority

Return values

TypeDescription
(string)HTML code to print (empty if the script has been enqueued)

Let's get in touch!

Features list