This commit is contained in:
2026-04-26 23:47:49 +02:00
parent 1b95f03d1e
commit b073e009d8
5288 changed files with 1112699 additions and 55536 deletions

View File

@@ -0,0 +1,384 @@
<?php
/**
* Created by PhpStorm.
* User: gaupoit
* Date: 7/24/19
* Time: 15:00
*/
if ( ! class_exists( 'PPW_Asset_Services' ) ) {
class PPW_Asset_Services {
/**
* Current screen
*
* @var
*/
private $screen;
/**
* Page name of current screen
* @var string
*/
private $page;
/**
* Tab name of current screen
* @var
*/
private $tab;
public function __construct( $screen, $get_params ) {
$this->screen = $screen;
if ( isset( $get_params['page'] ) ) {
$this->page = $get_params['page'];
}
if ( isset( $get_params['tab'] ) ) {
$this->tab = $get_params['tab'];
}
}
/**
* Render css and js for entire site tab
*/
public function load_assets_for_entire_site_tab() {
$module = PPW_Constants::ENTIRE_SITE_MODULE;
if ( PPW_Constants::MENU_NAME === $this->page && 'entire_site' === $this->tab ) {
$this->load_bundle_css( PPW_VERSION );
$this->load_js( $module, PPW_VERSION );
wp_localize_script(
"ppw-$module-js",
'ppw_entire_site_data',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
)
);
$this->load_select2_lib();
$this->load_toastr_lib();
}
}
/**
* Render css & js for sitewide submenu
*/
public function load_assets_for_entire_site_page() {
$module = PPW_Constants::ENTIRE_SITE_MODULE;
if ( PPW_Constants::SITEWIDE_PAGE_PREFIX === $this->page && ( 'general' === $this->tab || null === $this->tab ) ) {
$this->load_bundle_css( PPW_VERSION );
$this->load_js( $module, PPW_VERSION );
wp_localize_script(
"ppw-$module-js",
'ppw_entire_site_data',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
)
);
$this->load_select2_lib();
$this->load_toastr_lib();
}
}
public function load_assets_for_shortcode_page() {
if ( PPW_Constants::PCP_PAGE_PREFIX === $this->page && ( 'general' === $this->tab || null === $this->tab ) ) {
$this->load_bundle_css( PPW_VERSION );
$this->load_toastr_lib();
$this->load_shared_lib();
}
}
public function load_assets_for_external_page() {
if ( PPW_Constants::EXTERNAL_SERVICES_PREFIX === $this->page && ( 'recaptcha' === $this->tab || null === $this->tab ) ) {
$this->load_bundle_css( PPW_VERSION );
$this->load_toastr_lib();
$this->load_shared_lib();
$module = PPW_Constants::EXTERNAL_SETTINGS_MODULE;
$this->load_select2_lib();
$this->load_js( $module, PPW_VERSION );
wp_localize_script(
"ppw-$module-js",
'ppw_external_data',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'home_url' => ppw_core_get_home_url_with_ssl(),
)
);
}
}
public function load_assets_for_external_configuration() {
if ( PPW_Constants::EXTERNAL_SERVICES_PREFIX === $this->page && 'configuration' === $this->tab ) {
$this->load_bundle_css( PPW_VERSION );
$this->load_toastr_lib();
$this->load_shared_lib();
$module = PPW_Constants::EXTERNAL_CONFIGURATION_MODULE;
$this->load_select2_lib();
$this->load_js( $module, PPW_VERSION );
wp_localize_script(
"ppw-$module-js",
'ppw_external_data',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'home_url' => ppw_core_get_home_url_with_ssl(),
)
);
}
}
/**
* Load assets for shortcode setting.
*/
public function load_assets_for_shortcode_setting() {
$module = PPW_Constants::SHORTCODES_SETTINGS_MODULE;
$is_shortcode_tab = PPW_Constants::MENU_NAME === $this->page
&& 'shortcodes' === $this->tab;
$is_pcp_submenu = PPW_Constants::PCP_PAGE_PREFIX === $this->page
&& ( 'general' === $this->tab || null === $this->tab );
if ( $is_shortcode_tab || $is_pcp_submenu ) {
$this->load_select2_lib();
$this->load_js( $module, PPW_VERSION );
wp_localize_script(
"ppw-$module-js",
'ppw_shortcode_data',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'home_url' => ppw_core_get_home_url_with_ssl(),
'nonce' => wp_create_nonce( PPW_Constants::GENERAL_FORM_NONCE ),
)
);
}
}
/**
* Is Partial Protection submenu.
*
* @param string $page Page name.
* @param string $tab Tab name.
*
* @return bool
*/
public static function is_partial_protection_submenu( $page, $tab ) {
return PPW_Constants::PCP_PAGE_PREFIX === $page
&& ( 'general' === $tab || null === $tab );
}
/**
* Render css and js for general tab
*/
public function load_assets_for_general_tab() {
$module = PPW_Constants::GENERAL_SETTINGS_MODULE;
if ( PPW_Constants::MENU_NAME === $this->page && ( $module === $this->tab || null === $this->tab ) ) {
$this->load_bundle_css( PPW_VERSION );
$this->load_js( $module, PPW_VERSION );
wp_localize_script(
"ppw-$module-js",
'ppw_general_data',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'home_url' => ppw_core_get_home_url_with_ssl(),
'nonce' => wp_create_nonce( 'wp_rest' ),
)
);
$this->load_select2_lib();
$this->load_toastr_lib();
}
}
/**
* Render css and js for general tab
*/
public function load_assets_for_misc_tab() {
$module = PPW_Constants::MISC_SETTINGS_MODULE;
if ( PPW_Constants::MENU_NAME === $this->page && ( $module === $this->tab || null === $this->tab ) ) {
$this->load_bundle_css( PPW_VERSION );
$this->load_js( $module, PPW_VERSION );
wp_localize_script(
"ppw-$module-js",
'ppw_misc_data',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'home_url' => ppw_core_get_home_url_with_ssl(),
'nonce' => wp_create_nonce( 'wp_rest' ),
)
);
$this->load_select2_lib();
$this->load_toastr_lib();
do_action( PPW_Constants::HOOK_ADVANCED_TAB_LOAD_ASSETS );
}
}
/**
* Render css and js for general tab
*/
public function load_assets_for_category_page() {
global $pagenow;
$module = 'category';
$is_show = 'edit-tags.php' === $pagenow && isset( $_GET['taxonomy'] ) && 'category' === $_GET['taxonomy']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- We not hanlde nonce for load assets.
$is_show = apply_filters( 'ppwp_is_load_assets_for_category_page', $is_show );
if ( $is_show ) {
$this->load_bundle_css( PPW_VERSION );
$this->load_css( $module, PPW_VERSION );
$this->load_js( $module, PPW_VERSION );
wp_localize_script(
"ppw-$module-js",
'ppw_category_data',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'home_url' => ppw_core_get_home_url_with_ssl(),
'nonce' => wp_create_nonce( 'wp_rest' ),
)
);
$this->load_select2_lib();
$this->load_toastr_lib();
do_action( 'ppw_category_page_load_assets' );
}
}
/**
* Render css and js for troubleshoot tab
*/
public function load_assets_for_troubleshoot_tab() {
$module = PPW_Constants::TROUBLESHOOT_SETTINGS_MODULE;
if ( PPW_Constants::MENU_NAME === $this->page && ( $module === $this->tab || null === $this->tab ) ) {
$this->load_bundle_css( PPW_VERSION );
$this->load_js( $module, PPW_VERSION );
}
}
/**
* Load asserts for meta-box.
*/
public function load_assets_for_meta_box() {
$module = PPW_Constants::META_BOX_MODULE;
$this->load_css( $module, PPW_VERSION );
$this->load_js( $module, PPW_VERSION );
wp_localize_script(
"ppw-$module-js",
'save_password_data',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'error_message' => array(
'duplicate_password' => PPW_Constants::DUPLICATE_PASSWORD,
'empty_password' => PPW_Constants::EMPTY_PASSWORD,
'space_password' => PPW_Constants::SPACE_PASSWORD,
),
)
);
$this->load_toastr_lib();
}
/**
* Load asserts for shortcodes setting tab.
*/
public function load_assets_for_shortcodes() {
$module = PPW_Constants::SHORTCODES_SETTINGS_MODULE;
if ( PPW_Constants::MENU_NAME === $this->page && $module === $this->tab ) {
$this->load_bundle_css( PPW_VERSION );
$this->load_toastr_lib();
$this->load_shared_lib();
}
}
/**
* Render Select2 library
*/
public function load_select2_lib() {
wp_enqueue_script( 'ppw-select2-js', PPW_DIR_URL . 'admin/js/lib/select2.min.js', array( 'jquery' ), '4.0.3', true );
wp_enqueue_style( 'ppw-select2-css', PPW_DIR_URL . 'admin/css/lib/select2.min.css', array(), '4.0.3', 'all' );
}
/**
* Load js file to show notice when deactivating the plugin.
*/
public function load_js_show_notice_deactivate_plugin() {
if ( 'plugins' === $this->screen ) {
wp_enqueue_script( 'ppw-notice-deactivate-js', PPW_DIR_URL . 'admin/js/class-ppw-notice-deactivate.js', array( 'jquery' ), PPW_VERSION, true );
wp_localize_script(
'ppw-notice-deactivate-js',
'ppw_deactivate_data',
array(
'is_active_pro' => is_plugin_active( PPW_Constants::PRO_DIRECTORY ) || is_plugin_active( PPW_Constants::DEV_PRO_DIRECTORY ),
)
);
}
}
/**
* Load the shared lib.
*/
public function load_shared_lib() {
wp_enqueue_script( 'ppw-shared-js', PPW_DIR_URL . 'includes/views/shared/assets/dist/ppwUtils.bundle.js', array( 'jquery' ), PPW_VERSION, true );
wp_localize_script(
'ppw-shared-js',
'ppw_general_data',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
)
);
}
/**
* Render Toastr library
*/
public function load_toastr_lib() {
wp_enqueue_script( 'ppw-toastr-js', PPW_DIR_URL . 'admin/js/lib/toastr.min.js', array( 'jquery' ), '2.1.3', true );
wp_enqueue_style( 'ppw-toastr-css', PPW_DIR_URL . 'admin/css/lib/toastr.min.css', array(), '2.1.3', 'all' );
}
/**
* Render CSS to hide feature set password of WP
*/
public function load_css_hide_feature_set_password_wp() {
$page_post_screens = apply_filters(
PPW_Constants::HOOK_HIDE_DEFAULT_PW_WP_POSITION,
array(
'edit-post',
'edit-page',
'page',
'post',
)
);
if ( in_array( $this->screen, $page_post_screens, true ) ) {
wp_enqueue_style( 'ppw-hide-default-css', PPW_DIR_URL . 'admin/css/ppw-hide-default.css', array(), PPW_VERSION, 'all' );
}
}
/**
* Helper function to load css of module
*
* @param string $module Module for loading.
* @param string $version Version to load.
* @param array $dependencies Dependencies.
*/
public function load_css( $module, $version, $dependencies = array() ) {
wp_enqueue_style( "ppw-$module-css", PPW_VIEW_URL . "dist/ppw-$module.css", $dependencies, $version, 'all' );
}
/**
* Load bundle css file
*
* @param string $version CSS version.
*/
public function load_bundle_css( $version ) {
wp_enqueue_style( 'ppw-bundle-css', PPW_DIR_URL . 'admin/css/dist/ppw-setting.css', array(), $version, 'all' );
if ( ppw_is_wp_version_compatible( '5.3' ) ) {
wp_enqueue_style( 'ppw-bundle-css-wp-5-3', PPW_DIR_URL . 'includes/views/dist/ppw-general-wp-5-3.css', array(), $version, 'all' );
}
}
/**
* Helper function to load js of module
*
* @param string $module Module for loading.
* @param string $version Version to load.
* @param array $dependencies Dependencies.
*/
public function load_js( $module, $version, $dependencies = array() ) {
wp_enqueue_script( "ppw-$module-js", PPW_VIEW_URL . "dist/ppw-$module.js", $dependencies, $version, 'all' );
}
}
}

View File

@@ -0,0 +1,241 @@
<?php
use W3TC\Dispatcher;
if ( ! class_exists( 'PPW_Cache_Services' ) ) {
/**
* Class PPW_Free_Handle_Cache
*/
class PPW_Cache_Services {
public function __construct() {
if ( ! function_exists( 'is_plugin_active' ) ) {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
}
/**
* Handle cache for page/post have password type is role with Super Cache plugin
*
* @param $new_role_password
* @param $id
* @param $current_roles_password
* @param $current_global_passwords
* @deprecated
*/
function handle_cache_for_password_type_role_with_super_cache( $new_role_password, $id, $current_roles_password, $current_global_passwords ) {
if ( ! is_plugin_active( 'wp-super-cache/wp-cache.php' ) ) {
return;
}
if ( ! empty( $new_role_password ) ) {
$uri = $this->get_uri_by_post_id( $id );
global $cache_rejected_uri;
if ( ! in_array( '/' . $uri, $cache_rejected_uri ) ) {
$this->add_page_post_to_list_not_cache_for_super_cache( $id, $uri, $cache_rejected_uri );
}
} else {
if ( ! empty( $current_global_passwords ) ) {
return;
}
$result = array_values( $current_roles_password );
$result = array_filter( $result, 'strlen' );
if ( empty( $result ) ) {
$this->remove_page_post_to_list_not_cache_for_super_cache( $id );
}
}
}
/**
* Handle cache for page/post have password type is global with Super Cache plugin
*
* @param $new_global_passwords
* @param $id
* @param $current_roles_password
* @deprecated
*/
function handle_cache_for_password_type_global_with_super_cache( $new_global_passwords, $id, $current_roles_password ) {
if ( ! is_plugin_active( 'wp-super-cache/wp-cache.php' ) ) {
return;
}
if ( ! empty( $new_global_passwords ) ) {
$uri = $this->get_uri_by_post_id( $id );
global $cache_rejected_uri;
if ( ! in_array( '/' . $uri, $cache_rejected_uri ) ) {
$this->add_page_post_to_list_not_cache_for_super_cache( $id, $uri, $cache_rejected_uri );
}
} else {
$result = array_values( $current_roles_password );
$result = array_filter( $result, 'strlen' );
if ( empty( $result ) ) {
$this->remove_page_post_to_list_not_cache_for_super_cache( $id );
}
}
}
/**
* Add page or post to list not cache of WP Super Cache plugin
*
* @param $post_id
* @param $uri
* @param $cache_rejected_uri
* @deprecated
*/
function add_page_post_to_list_not_cache_for_super_cache( $post_id, $uri, $cache_rejected_uri ) {
// 1. Add URI to list not cache
global $wp_cache_config_file;
array_push( $cache_rejected_uri, '/' . $uri );
$all_uri = array_map( function ( $key, $cache ) {
return " $key => '$cache'";
}, array_keys( $cache_rejected_uri ), $cache_rejected_uri );
$all_uri = implode( ',', $all_uri );
$text = "array($all_uri )";
wp_cache_replace_line( '^ *\$cache_rejected_uri', "\$cache_rejected_uri = $text;", $wp_cache_config_file );
// 3. Clear cache by post_id
wp_cache_post_edit( $post_id );
}
/**
* Remove page or post in list not cache of WP Super Cache plugin
*
* @param $post_id
* @deprecated
*/
function remove_page_post_to_list_not_cache_for_super_cache( $post_id ) {
// 1. Get URI in permalink by post_id
$uri = $this->get_uri_by_post_id( $post_id );
$uri = '/' . $uri;
// 2. Remove URI in list not cache
global $cache_rejected_uri, $wp_cache_config_file;
$cache_rejected_uri = array_unique( $cache_rejected_uri );
if ( ( $key = array_search( $uri, $cache_rejected_uri ) ) !== false ) {
unset( $cache_rejected_uri[ $key ] );
$all_uri = array_map( function ( $key, $cache ) {
return " $key => '$cache'";
}, array_keys( $cache_rejected_uri ), $cache_rejected_uri );
$all_uri = implode( ',', $all_uri );
$text = "array($all_uri )";
wp_cache_replace_line( '^ *\$cache_rejected_uri', "\$cache_rejected_uri = $text;", $wp_cache_config_file );
}
}
/**
* get URI by post_id
*
* @param $post_id
*
* @return string
* @deprecated
*/
function get_uri_by_post_id( $post_id ) {
$permalink = get_permalink( $post_id );
$parse_url = parse_url( $permalink );
$uri = $parse_url['path'];
$uri = explode( '/', $uri );
$uri = ! empty( $uri[ count( $uri ) - 1 ] ) ? $uri[ count( $uri ) - 1 ] : $uri[ count( $uri ) - 2 ];
return $uri;
}
/**
* Clear cache all page
* @deprecated
*/
function clear_cache_super_cache() {
if ( ! is_plugin_active( 'wp-super-cache/wp-cache.php' ) ) {
return;
}
if ( is_multisite() ) {
wp_cache_clear_cache( get_current_blog_id() );
} else {
wp_cache_clear_cache();
}
}
/**
* Check W3 Total Cache has config cookie group
*
* @return bool
*/
function check_has_config_w3_total_cache() {
$config = Dispatcher::config();
$groups = $config->get_array( 'pgcache.cookiegroups.groups' );
if ( ! array_key_exists( 'password_protect_wordpress', $groups ) ) {
return false;
}
if ( ! $groups['password_protect_wordpress']['enabled'] || $groups['password_protect_wordpress']['cache'] ) {
return false;
}
$cookies = $groups['password_protect_wordpress']['cookies'];
return in_array( 'pda_protect_password', $cookies ) && in_array( 'wp-postpass-role_*', $cookies );
}
/**
* Update cookie to option and modify htaccess file for WP Fastest Cache plugin
* @deprecated
*/
function custom_option_exclude_page_and_cookie_fastest_cache() {
if ( ! is_plugin_active( 'wp-fastest-cache/wpFastestCache.php' ) ) {
return;
}
$old_fastest = get_option( "WpFastestCacheExclude" );
$fastest_cache = new WpFastestCache();
if ( $old_fastest ) {
$old_fastest = json_decode( $old_fastest );
$tmp1 = false;
$tmp2_free = false;
foreach ( $old_fastest as $fast ) {
if ( 'cookie' === $fast->type && 'contain' === $fast->prefix && 'pda_protect_password' === $fast->content ) {
$tmp1 = true;
}
if ( 'cookie' === $fast->type && 'contain' === $fast->prefix && 'wp-postpass-role_' === $fast->content ) {
$tmp2_free = true;
}
}
if ( ! $tmp1 ) {
array_push( $old_fastest, (object) array(
'prefix' => 'contain',
'content' => 'pda_protect_password',
'type' => 'cookie'
) );
}
if ( ! $tmp2_free ) {
array_push( $old_fastest, (object) array(
'prefix' => 'contain',
'content' => 'wp-goldpass-pagepost_',
'type' => 'cookie'
) );
}
update_option( "WpFastestCacheExclude", json_encode( $old_fastest ) );
$fastest_cache->modify_htaccess_for_exclude();
} else {
$ppwp_cookie = array(
(object) array(
'prefix' => 'contain',
'content' => 'pda_protect_password',
'type' => 'cookie'
)
);
array_push( $ppwp_cookie, (object) array(
'prefix' => 'contain',
'content' => 'wp-postpass-role_',
'type' => 'cookie'
) );
add_option( "WpFastestCacheExclude", json_encode( $ppwp_cookie ), null, "yes" );
$fastest_cache->modify_htaccess_for_exclude();
}
}
}
}

View File

@@ -0,0 +1,355 @@
<?php
/**
*
* Class PPW_Category
*/
class PPW_Category_Service {
const COOKIE_NAME = 'ppw_cat-';
const OPTION_NAME = 'ppwp_category_options';
const SHARED_CATEGORY_TYPE = 'shared_category';
/**
* @var null|integer Category ID.
*/
private $category_id = null;
/**
* Is using post password required
* @var bool
*/
private $unlocked = false;
/**
* @var null|PPW_Category_Service Instance.
*/
protected static $instance = null;
private $is_pro_activated;
/**
* Get instance.
*
* @return PPW_Category_Service
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Register hooks.
*
* @param bool $is_pro_activated Is pro activated
*/
public function register( $is_pro_activated = false ) {
$this->is_pro_activated = $is_pro_activated;
$this->unlocked = $this->is_pro_activated;
add_filter( 'post_password_required', array( $this, 'category_password_required' ), 15, 2 );
add_filter( 'ppw_is_valid_password', array( $this, 'check_valid_password' ), 15, 3 );
add_action( 'category_pre_add_form', array( $this, 'display_category_ui' ) );
// Check post is protected before.
add_filter( 'ppw_is_valid_cookie', array( $this, 'ppw_is_valid_cookie' ) );
add_filter( 'ppwp_post_password_required', array( $this, 'ppwp_post_password_required' ), 100 );
}
/**
* Check if content is protected and unlocked password form.
*
* @param boolean $is_valid
*
* @return bool True is valid cookie.
*/
public function ppw_is_valid_cookie( $is_valid ) {
$this->unlocked = $is_valid;
return $is_valid;
}
/**
* If post is protected and
*
* @param array $data Unlock data from PPWP Fro.
*
* @return mixed
*/
public function ppwp_post_password_required( $data ) {
if ( ! isset( $data['is_content_unlocked'] ) ) {
return $data;
}
$this->unlocked = $data['is_post_protected'] && $data['is_content_unlocked'];
return $data;
}
/**
* Display category UI.
*/
public function display_category_ui() {
$categories = ppw_get_terms_with_all_lang(
array(
'taxonomy' => 'category',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
)
);
$is_protect = ppw_core_get_setting_type_bool_by_option_name( 'ppwp_is_protect_category', self::OPTION_NAME );
$protected_categories = ppw_core_get_setting_type_array_by_option_name( 'ppwp_protected_categories_selected', self::OPTION_NAME );
// Get first password to display to user.
$passwords = PPW_Repository_Passwords::get_instance()->get_all_shared_categories_password();
if ( count( $passwords ) > 0 ) {
$password = $passwords[0]->password;
} else {
$password = '';
}
ob_start();
include PPW_DIR_PATH . 'includes/views/category/view-option.php';
echo ob_get_clean(); // phpcs:ignore -- we already escape on the view-option.php
}
/**
* Filters whether a category requires the user to supply a password.
*
* @param bool Whether the user needs to supply a password.
* True if password has not been provided or is incorrect,
* false if password has been supplied or is not required.
*
* @return bool false if a password is not required or the correct password cookie is present, true otherwise.
*/
public function category_password_required( $required, $post ) {
$post_id = ! empty( $post ) ? $post->ID : false;
if ( ! $post_id ) {
return $required;
}
// Get all protected categories of a Post.
$protected_categories = $this->get_protected_categories( $post_id );
if ( empty( $protected_categories ) ) {
return $required;
}
$validated_cookie = apply_filters( 'ppw_category_is_valid_cookie', $this->is_valid_cookie(), $post_id, $post, $required );
if ( $validated_cookie ) {
return false;
}
add_filter( 'ppwp_ppf_action_url', array( $this, 'get_action_url' ), 9999 );
// Unlocked by PPWP Free and PPWP Pro
// Include Single, AL, Group.
if ( apply_filters( 'ppw_category_unlocked', $this->unlocked, $post_id, $protected_categories ) ) {
$this->unlocked = $this->is_pro_activated;
return $required;
}
return true;
}
/**
* Get protected categories if user turn on Option.
*
* @param integer $post_id Post ID.
*
* @return array Empty if user turn off option or post ID not include protected categories.
*/
public function get_protected_categories( $post_id ) {
// Check user has turn on option.
$enabled = ppw_core_get_setting_type_bool_by_option_name( 'ppwp_is_protect_category', self::OPTION_NAME );
$enabled = apply_filters( 'ppw_category_is_enabled', $enabled, $post_id );
if ( ! $enabled ) {
return array();
}
$categories_id = $this->get_categories_by_post_id( $post_id );
// Not handle with categories of post are empty.
if ( empty( $categories_id ) ) {
return array();
}
// Get protected categories of current post.
$protected_categories = ppw_core_get_setting_type_array_by_option_name( 'ppwp_protected_categories_selected', self::OPTION_NAME );;
$protected_categories = array_intersect( $categories_id, $protected_categories );
// Reorder index of array.
return array_values( $protected_categories );
}
/**
* Replace current action by action URL of category to check password.
*/
public function get_action_url( $url ) {
if ( is_category() ) {
$callback_url = get_category_link( get_queried_object_id() );
} elseif ( is_singular() ) {
$callback_url = get_the_permalink();
} else {
global $wp;
$callback_url = home_url( $wp->request );
}
$callback_url = rawurlencode( $callback_url );
return add_query_arg(
array(
PPW_Constants::CALL_BACK_URL_PARAM => $callback_url,
),
$url
);
}
/**
* Check password is valid after show message if it is wrong password.
*
* @param bool $is_valid Is valid password.
* @param int $post_id Post ID.
* @param string $password Password.
*
* @return bool True if password is valid.
*/
public function check_valid_password( $is_valid, $post_id, $password ) {
$protected_categories = $this->get_protected_categories( $post_id );
if ( empty( $protected_categories ) ) {
return $is_valid;
}
return apply_filters( 'ppw_category_is_valid_password', $this->is_valid_password( $protected_categories, $password, $post_id ), $is_valid, $protected_categories, $post_id, $password ) || $is_valid;
}
/**
* Set password to cookie
*
* @param string $password Password string.
* @param string $cookie_name Cookie name.
* @param false|int $password_id Password ID.
*
* @return bool
*/
public function set_password_to_cookie( $password, $cookie_name, $password_id = false ) {
$password_hashed = wp_hash_password( $password );
$expire = apply_filters( PPW_Constants::HOOK_COOKIE_EXPIRED, time() + 7 * DAY_IN_SECONDS );
$password_cookie_expired = ppw_core_get_setting_type_string( PPW_Constants::COOKIE_EXPIRED );
if ( ! empty( $password_cookie_expired ) ) {
$time = explode( ' ', $password_cookie_expired )[0];
$unit = ppw_core_get_unit_time( $password_cookie_expired );
if ( 0 !== $unit ) {
$expire = apply_filters( PPW_Constants::HOOK_COOKIE_EXPIRED, time() + (int) $time * $unit );
}
}
$referer = wp_get_referer();
if ( $referer ) {
$secure = ( 'https' === parse_url( $referer, PHP_URL_SCHEME ) );
} else {
$secure = false;
}
if ( $password_id ) {
$password_hashed = $password_id . '|' . $password_hashed;
}
$expire = apply_filters( 'ppw_cookie_expire', $expire );
$expire = apply_filters( 'ppwp_cookie_expiry', $expire );
return setcookie( $cookie_name . COOKIEHASH, $password_hashed, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure );
}
/**
* Handle password with data sent by user.
*
* @param array $categories_id Categories ID.
* @param string $password Password.
* @param integer $post_id Post ID.
*
* @return bool
*/
public function is_valid_password( $categories_id, $password, $post_id ) {
// Get current roles will empty if user using subdomain because path of cookie.
$password_info = PPW_Repository_Passwords::get_instance()->find_by_shared_category_password( $password );
if ( ! $password_info ) {
return false;
}
$this->set_password_to_cookie( $password, self::COOKIE_NAME, $password_info->id );
$this->set_password_to_cookie( $password, PPW_Constants::WP_POST_PASS );
do_action( 'ppw_category_after_set_password_to_cookie', $categories_id, $password_info, $post_id );
return true;
}
/**
* Is valid cookie.
*
* @return bool
*/
public function is_valid_cookie() {
$_cookie = wp_unslash( $_COOKIE );
if ( ! isset( $_cookie[ self::COOKIE_NAME . COOKIEHASH ] ) ) {
return false;
}
$cookie_value = $_cookie[ self::COOKIE_NAME . COOKIEHASH ];
$cookie_value = explode( '|', $cookie_value );
if ( count( $cookie_value ) < 2 ) {
return false;
}
$password_id = (int) $cookie_value[0];
$password_hashed = $cookie_value[1];
$password_info = PPW_Repository_Passwords::get_instance()->get_shared_category_password( $password_id );
if ( ! $password_info ) {
return false;
}
if ( ppw_free_check_password( $password_info->password, $password_hashed ) ) {
return true;
}
return false;
}
/**
* Get categories by post ID.
*
* @param int $post_id Post ID.
*
* @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if any of the
* taxonomies don't exist.
* @link https://developer.wordpress.org/reference/functions/wp_get_post_categories/
*/
public function get_categories_by_post_id( $post_id ) {
$terms = get_the_terms( $post_id, $this->get_current_taxonomy( $post_id ) );
if ( empty( $terms ) ) {
return array();
}
return array_map(
function ( $term ) {
return $term->term_id;
},
$terms
);
}
/**
* Get current taxonomy from Post ID.
*
* @param int $post_id Post ID.
*
* @return string Current taxonomy.
*/
public function get_current_taxonomy( $post_id ) {
return apply_filters( 'ppw_category_get_taxonomy', 'category', $post_id );
}
}

View File

@@ -0,0 +1,510 @@
<?php
class PPW_Content_Protection {
/**
* @var PPW_Content_Protection
*/
protected static $instance;
const POST_TYPE = 'ppwp-section';
const SETTING_META_KEY = 'ppw_pcp_setting';
const PASSWORD_GLOBAL_TYPE = 'PCPSection';
const PASSWORD_ROLE_TYPE = 'PCPSectionRole_';
const COOKIE_NAME = 'ppw_section-';
const ADMIN_PATH = 'edit.php?post_type=ppwp-section';
const SHORTCODE_COLUMN = 'ppw_pcp_shortcode';
/**
* @var PPW_Password_Services
*/
private $password_service;
/**
* @var PPW_Repository_Passwords
*/
private $password_repo;
/**
* @return PPW_Content_Protection
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct() {
$this->password_service = new PPW_Password_Services();
$this->password_repo = new PPW_Repository_Passwords();
}
public function register() {
add_action( 'init', array( $this, 'register_post_type' ) );
add_action( 'wp_ajax_ppw_pcp_validate_password', array( $this, 'validate_password' ) );
add_action( 'wp_ajax_nopriv_ppw_pcp_validate_password', array( $this, 'validate_password' ) );
add_filter( 'ppw_shortcode_render_content', array( $this, 'handle_shortcode_with_area' ), 10, 2 );
add_filter( 'et_builder_load_actions', array( $this, 'add_action_to_divi' ) );
add_filter( 'ppw_pcp_valid_shortcode', array( $this, 'valid_shortcode' ), 10, 2 );
add_action( 'admin_init', array( $this, 'maybe_add_metabox' ) );
add_filter( 'ppw_shortcode_unlock_content', array( $this, 'maybe_unlock_content_by_cookie' ), 15, 2 );
add_filter( 'ppw_pcp_submenu_add_new_tab', array( $this, 'add_pcp_tab' ), 1000 );
add_filter( 'manage_' . self::POST_TYPE . '_posts_columns', array( $this, 'add_shortcode_column' ) );
add_action( 'manage_' . self::POST_TYPE . '_posts_custom_column', array(
$this,
'render_content_custom_column',
), 10, 2 );
add_action( 'admin_notices', array( $this, 'handle_admin_notices' ) );
}
public function add_shortcode_column( $columns ) {
$inserted = array(
self::SHORTCODE_COLUMN => 'Shortcode'
);
return array_slice( $columns, 0, 2, true )
+ $inserted
+ array_slice( $columns, 2, count( $columns ) - 1, true );
}
public function render_content_custom_column( $column, $post_id ) {
if ( $column === self::SHORTCODE_COLUMN ) {
echo '[ppwp section="' . absint( $post_id ) . '" /]';
}
}
public function add_pcp_tab( $tabs ) {
$tabs[] = array(
'tab' => 'section',
'tab_name' => 'Section Protection',
'link' => self::ADMIN_PATH,
);
return $tabs;
}
public function valid_shortcode( $content, $attrs ) {
if ( ! isset( $attrs['section'] ) || ! is_numeric( $attrs['section'] ) ) {
return $content;
}
if ( absint( $attrs['section'] ) > 0 ) {
return true;
}
return $content;
}
public function register_post_type() {
$args = array(
'labels' => array(
'name' => __( 'Section Protection', 'password-protect-page' ),
'singular_name' => __( 'Section Protection', 'password-protect-page' ),
'add_new' => __( 'Add New', 'password-protect-page' ),
'add_new_item' => __( 'Add New', 'password-protect-page' ),
'edit_item' => __( 'Edit', 'password-protect-page' ),
'new_item' => __( 'New', 'password-protect-page' ),
'view_item' => __( 'View', 'password-protect-page' ),
'search_items' => __( 'Search', 'password-protect-page' ),
'not_found' => __( 'No found', 'password-protect-page' ),
'not_found_in_trash' => __( 'No found in Trash', 'password-protect-page' ),
),
'public' => false,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => self::ADMIN_PATH,
'_builtin' => false,
'capability_type' => 'post',
'supports' => array( 'title', 'editor' ),
'rewrite' => false,
'query_var' => false,
'show_in_rest' => true,
);
if ( current_user_can( 'administrator' ) ) {
$args['public'] = true;
$args['publicly_queryable'] = true;
}
// if ( is_admin() ) {
// global $pagenow, $typenow, $post;
// if ( 'edit.php' === $pagenow || self::POST_TYPE === $typenow ) {
// $args['show_in_menu'] = PPW_Constants::MENU_NAME;
// } elseif ( isset( $_GET['post_type'] ) && self::POST_TYPE === $_GET['post_type'] ) {
// $args['show_in_menu'] = PPW_Constants::MENU_NAME;
// } elseif ( isset( $_GET['post'] ) && isset( $_GET['action'] ) ) {
// $post_type = get_post_type( $_GET['post'] );
//
// if ( $post_type === self::POST_TYPE ) {
// $args['show_in_menu'] = PPW_Constants::MENU_NAME;
// }
// }
// }
register_post_type( self::POST_TYPE, $args );
}
/**
* Add ajax support for Divi builder.
*
* @param array $actions array of allowed actions.
*
* @return array
*/
public function add_action_to_divi( $actions ) {
$actions[] = 'ppw_pcp_validate_password';
return $actions;
}
public function handle_shortcode_with_area( $content, $attrs ) {
if ( ! isset( $attrs['section'] ) ) {
return $content;
}
$post_id = absint( $attrs['section'] );
if ( ! $post_id ) {
return $content;
}
$post = get_post( $post_id );
if ( empty( $post ) || $post->post_type !== self::POST_TYPE ) {
return $content;
}
$post_content = $this->massage_post_content( $post_id, $post->post_content );
if ( is_null( $post_content ) ) {
return $content;
}
return $post_content;
}
public function massage_post_content( $post_id, $post_content ) {
$post_content = ppw_support_third_party_content_plugin( $post_id, $post_content );
return apply_filters( 'the_content', $post_content );
}
public function maybe_add_metabox() {
$attributes = apply_filters(
'ppw_pcp_metabox_attributes',
array(
'title' => __( 'Password Protect WordPress', PPW_Constants::DOMAIN ),
'callback' => array( $this, 'display_metabox' ),
'screen' => array( self::POST_TYPE ),
'context' => 'side',
'priority' => 'high',
)
);
add_meta_box(
'ppw_pcp_meta_box',
$attributes['title'],
$attributes['callback'],
$attributes['screen'],
$attributes['context'],
$attributes['priority']
);
}
public function display_metabox() {
ob_start();
?>
<div id="ppw-pcp-metabox"></div>
<?php
wp_enqueue_script( 'ppw-pcp-metabox', PPW_DIR_URL . 'admin/js/dist/pcp-metabox.js', array(), PPW_VERSION, true );
wp_localize_script(
'ppw-pcp-metabox',
'pcpMetabox',
array(
'rest_url' => get_rest_url( null, '/wppp/v1/pcp' ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'post_id' => get_the_ID(),
'customize_url' => admin_url( 'customize.php' ),
'extra_html' => apply_filters( 'ppw_pcp_metabox_extra_html', '')
)
);
echo ob_get_clean(); // phpcs:ignore -- we cannot escape ob_start ob_get_clean(), there are no variable to escape in statement above
}
public function validate_single_password( $params ) {
$password = $params['password'];
$area = $params['area'];
$roles = ppw_core_get_current_role();
$args = array(
'post_id' => $area,
'roles' => $roles,
'global_type' => self::PASSWORD_GLOBAL_TYPE,
'role_type' => self::PASSWORD_ROLE_TYPE,
);
$password_info = $this->password_repo->find_activated_password( $password, $args );
if ( empty( $password_info ) ) {
return false;
}
$this->password_service->set_cookie_bypass_cache( $password, self::COOKIE_NAME . $area . 'p' . $password_info->id );
do_action( 'ppw_after_save_section_cookie', $password_info );
return true;
}
public function validate_password() {
if ( ! isset( $_POST['pss'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Processing form data without nonce verification. - Not verify nonce for password validate.
wp_send_json(
array(
'success' => false,
'message' => 'Please enter the correct password!',
),
403
);
exit();
}
if ( ! isset( $_POST['area'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Processing form data without nonce verification. - Not verify nonce for password validate.
wp_send_json(
array(
'success' => false,
'message' => 'Area value is required!',
),
403
);
exit();
}
if ( ! isset( $_POST['post_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Processing form data without nonce verification. - Not verify nonce for password validate.
wp_send_json(
array(
'success' => false,
'message' => 'Post ID is required!',
),
403
);
exit();
}
$password = wp_unslash( $_POST['pss'] ); // phpcs:ignore -- not sanitize password because we allow all character.
$area = absint( $_POST['area'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Processing form data without nonce verification. - Not verify nonce for password validate.
$post_id = absint( $_POST['post_id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Processing form data without nonce verification. - Not verify nonce for password validate.
$post = get_post( $area );
if ( empty( $post ) ) {
wp_send_json(
array(
'success' => false,
'message' => 'Post does not exist',
),
403
);
exit();
}
if ( $post->post_type !== self::POST_TYPE ) {
wp_send_json(
array(
'success' => false,
'message' => 'Post type is not valid',
),
403
);
exit();
}
$setting = get_post_meta( $area, self::SETTING_META_KEY, true );
$params = array(
'password' => $password,
'area' => $area,
'post_id' => $post_id,
'setting' => $setting,
);
$is_valid_by_single = $this->validate_single_password( $params );
$is_valid_password = apply_filters( 'ppw_pcp_area_is_valid_password', $is_valid_by_single, $params );
if ( $is_valid_password ) {
wp_send_json(
array(
'reload' => ! ppw_core_get_setting_type_bool_by_option_name( PPW_Constants::NO_RELOAD_PAGE, PPW_Constants::MISC_OPTIONS ),
'success' => true,
'message' => '',
'content' => $this->massage_post_content( $area, $post->post_content ),
),
200
);
exit();
}
wp_send_json(
array(
'success' => false,
'message' => get_theme_mod( 'ppwp_pcp_err_msg_text', PPW_Constants::DEFAULT_SHORTCODE_ERROR_MSG ),
),
400
);
}
public function check_area_exist( $post ) {
if ( empty( $post ) ) {
return false;
}
if ( $post->post_type !== self::POST_TYPE ) {
return false;
}
return true;
}
public function get_info_from_area_cookie( $area ) {
$_cookie = wp_unslash( $_COOKIE );
if ( empty( $_cookie ) ) {
return array();
}
$cookie_keys = array_filter(
array_keys( $_cookie ),
function ( $key ) {
return false !== strpos( $key, self::COOKIE_NAME );
}
);
if ( 0 === count( $cookie_keys ) ) {
return array();
}
$results = array();
foreach ( $cookie_keys as $cookie_key ) {
$info = str_replace( self::COOKIE_NAME, '', $cookie_key );
$info = str_replace( COOKIEHASH, '', $info );
$info = explode( 'p', $info );
$password_id = absint( ppw_get_value( $info, 1, 0 ) );
$cookie_area = absint( ppw_get_value( $info, 0, 0 ) );
if ( $area !== $cookie_area ) {
continue;
}
$results[ $password_id ] = array(
'cookie_name' => $cookie_key,
'cookie_value' => $_cookie[ $cookie_key ],
);
}
return $results;
}
public function validate_cookie( $area ) {
$cookie_passwords = $this->get_info_from_area_cookie( $area );
if ( empty( $cookie_passwords ) ) {
return false;
}
$password_ids = array_keys( $cookie_passwords );
$roles = ppw_core_get_current_role();
$params = array(
'post_id' => $area,
'roles' => $roles,
'global_type' => self::PASSWORD_GLOBAL_TYPE,
'role_type' => self::PASSWORD_ROLE_TYPE,
'allow_to_check_expired' => false,
);
$password_infos = $this->password_repo->find_activated_passwords_by_ids(
$password_ids,
$params
);
if ( empty( $password_infos ) ) {
return false;
}
foreach ( $password_infos as $password_info ) {
if ( ! isset( $cookie_passwords[ $password_info->id ] ) ) {
continue;
}
$cookie = $cookie_passwords[ $password_info->id ];
$hashed_password = $cookie['cookie_value'];
if ( ppw_free_check_password( $password_info->password, $hashed_password ) ) {
return true;
}
}
return false;
}
public function maybe_unlock_content_by_cookie( $is_valid, $attrs ) {
if ( $is_valid || ! isset( $attrs['section'] ) ) {
return $is_valid;
}
$area = $attrs['section'];
$setting = get_post_meta( $area, self::SETTING_META_KEY, true );
$post = get_post( $area );
if ( ! $this->check_area_exist( $post ) ) {
return $is_valid;
}
$params = array(
'area' => absint( $area ),
'setting' => $setting,
);
$is_valid_cookie = $this->validate_cookie( $params['area'] );
return apply_filters( 'ppw_pcp_valid_area_password', $is_valid_cookie, $params );
}
public function handle_admin_notices() {
if ( ! function_exists( 'get_current_screen' ) ) {
return;
}
$current_screen = get_current_screen();
if ( ! isset( $current_screen->post_type ) ) {
return;
}
if ( $current_screen->post_type !== self::POST_TYPE ) {
return;
}
$button_contexts = array( 'ppwp-section' );
if ( in_array( $current_screen->id, $button_contexts ) ) {
$edit_url = admin_url( self::ADMIN_PATH );
$button = '<span style="float: right;">
<a href="' . $edit_url . '">
<button class="button button-primary">
Go back to all sections
</button>
</a>
</span>';
} else {
$button = '';
}
?>
<div class="notice">
<p>
<div>
<b>Password Protect WordPress: Section Protection</b>
<?php echo $button;?>
</div>
<a target="_blank" rel="noopener noreferrer"
href="https://passwordprotectwp.com/docs/section-protection/?utm_source=user-website&utm_medium=section-protection-list&utm_campaign=ppwp-free">Protect section of your
content</a> by creating section templates below. Add these sections to your content using
auto-generated section shortcodes.
</p>
</div>
<?php
}
}

View File

@@ -0,0 +1,816 @@
<?php
if ( ! class_exists( 'PPW_Customizer_PCP' ) ) {
class PPW_Customizer_PCP {
/**
* Instance of PPW_Pro_Shortcode class.
*
* @var PPW_Customizer_PCP
*/
protected static $instance = null;
const PANEL = 'ppwp_pcp';
const GENERAL_SECTION = 'ppwp_pcp_form';
const FORM_SECTION = 'ppwp_pcp_form';
const ERR_MSG_SECTION = 'ppwp_pcp_err_msg';
const BUTTON_SECTION = 'ppwp_pcp_button';
/**
* Get instance of PPW_Customizer
*
* @return PPW_Customizer_PCP
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
// Use static instead of self due to the inheritance later.
// For example: ChildSC extends this class, when we call get_instance
// it will return the object of child class. On the other hand, self function
// will return the object of base class.
self::$instance = new static();
}
return self::$instance;
}
public function register() {
add_action( 'customize_register', array( $this, 'customize_register' ), 15 );
add_action( 'wp_head', array( $this, 'dynamic_styles' ) );
add_filter( 'ppw_pcp_attributes', array( $this, 'load_customizer_attributes' ) );
add_filter( 'ppw_validated_pcp_password', array( $this, 'load_customizer_err_msg' ), 10, 3 );
}
/**
* Load customizer attributes.
*
* @param array $attrs Attributes
*
* @return array
*/
public function load_customizer_attributes( $attrs ) {
$attrs['headline'] = get_theme_mod( 'ppwp_pcp_form_headline', PPW_Constants::DEFAULT_SHORTCODE_HEADLINE );
$attrs['description'] = get_theme_mod( 'ppwp_pcp_form_description', PPW_Constants::DEFAULT_SHORTCODE_DESCRIPTION );
$attrs['label'] = get_theme_mod( 'ppwp_pcp_form_label', PPW_Constants::DEFAULT_SHORTCODE_LABEL );
$attrs['placeholder'] = get_theme_mod( 'ppwp_pcp_form_placeholder', '' );
$attrs['error_msg'] = get_theme_mod( 'ppwp_pcp_err_msg_text', PPW_Constants::DEFAULT_SHORTCODE_ERROR_MSG );
$attrs['button'] = get_theme_mod( 'ppwp_pcp_button_text', PPW_Constants::DEFAULT_SHORTCODE_BUTTON );
$attrs['loading'] = get_theme_mod( 'ppwp_pcp_button_loading_text', PPW_Constants::DEFAULT_SHORTCODE_LOADING );
$attrs['show_password'] = get_theme_mod( 'ppwp_pcp_form_is_show_password', PPW_Constants::DEFAULT_SHORTCODE_SHOW_PASSWORD );
$attrs['show_password_text'] = get_theme_mod( 'ppwp_pcp_form_show_password_text', PPW_Constants::DEFAULT_SHORTCODE_SHOW_PASSWORD_TEXT );
// $attrs['desc_above_btn'] = get_theme_mod( 'ppwp_pcp_description_above_btn', PPW_Constants::DEFAULT_SHORTCODE_DESC_ABOVE_PWD_BTN );
$attrs['desc_below_form'] = get_theme_mod( 'ppwp_pcp_description_below_form', PPW_Constants::DEFAULT_SHORTCODE_DESC_BELOW_PWD_FORM );
return $attrs;
}
/**
* Load customizer error message.
*
* @param array $attrs Attributes.
* @param string $password Password.
* @param array $parsed_attrs Parsed attributes.
*
* @return array
*/
public function load_customizer_err_msg( $attrs, $password, $parsed_attrs ) {
if ( isset( $parsed_attrs['error_msg'] ) ) {
return $attrs;
}
if ( isset( $attrs['is_valid_password'] ) && ! $attrs['is_valid_password'] ) {
$attrs['message'] = get_theme_mod( 'ppwp_pcp_err_msg_text', PPW_Constants::DEFAULT_SHORTCODE_ERROR_MSG );
}
return $attrs;
}
/**
* Register customizer.
*
* @param WP_Customize $wp_customize WP Customize class.
*/
public function customize_register( $wp_customize ) {
if ( ! class_exists( 'PPW_Title_Group_Control' ) ) {
include PPW_DIR_PATH . 'includes/customizers/class-ppw-title-group-control.php';
}
if ( ! class_exists( 'PPW_Toggle_Control' ) ) {
include PPW_DIR_PATH . 'includes/customizers/class-ppw-toggle-control.php';
}
if ( ! class_exists( 'PPW_Text_Editor_Custom_Control' ) ) {
include PPW_DIR_PATH . 'includes/customizers/class-ppw-text-editor-control.php';
}
$wp_customize->add_panel(
self::PANEL,
array(
'priority' => 9990,
'capability' => 'edit_theme_options',
'theme_supports' => '',
'title' => __( 'PPWP Partial Protection Form', 'password-protect-page' ),
)
);
$this->append_form_section( $wp_customize );
$this->append_err_msg_section( $wp_customize );
$this->append_button_section( $wp_customize );
}
/**
* Append form section.
*
* @param WP_Customize $wp_customize WP Customize class.
*/
public function append_form_section( $wp_customize ) {
$wp_customize->add_section(
self::FORM_SECTION,
array(
'title' => __( 'Password Form', 'password-protect-page' ),
'panel' => self::PANEL,
'priority' => 10,
)
);
$wp_customize->add_setting( 'ppwp_pcp_form_background_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_pcp_form_background_title', array(
'label' => __( 'Background', 'password-protect-page' ),
'section' => self::FORM_SECTION,
'settings' => 'ppwp_pcp_form_background_title',
'type' => 'control_title',
'priority' => 0,
) )
);
$wp_customize->add_setting( 'ppwp_pcp_form_background_color' );
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize,
'ppwp_pcp_form_background_color_control',
array(
'label' => __( 'Form Background Color', 'password-protect-page' ),
'section' => self::FORM_SECTION,
'settings' => 'ppwp_pcp_form_background_color',
'priority' => 10,
)
)
);
$wp_customize->add_setting( 'ppwp_pcp_form_padding' );
$wp_customize->add_control(
'ppwp_pcp_form_padding_control',
array(
'label' => __( 'Padding', 'password-protect-page' ),
'description' => __( 'Padding in px', 'password-protect-page' ),
'section' => self::FORM_SECTION,
'settings' => 'ppwp_pcp_form_padding',
'type' => 'number',
'priority' => 20,
)
);
/* form background border radius */
$wp_customize->add_setting( 'ppwp_pcp_form_border_radius', array(
'default' => PPW_Constants::DEFAULT_FORM_BORDER_RADIUS,
) );
$wp_customize->add_control( 'ppwp_pcp_form_border_radius_control', array(
'label' => __( 'Border Radius', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'description' => 'Border Radius in px',
'settings' => 'ppwp_pcp_form_border_radius',
'type' => 'number',
'priority' => 40,
) );
$wp_customize->add_setting( 'ppwp_pcp_form_headline_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_pcp_form_headline_title', array(
'label' => __( 'Headline', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_headline_title',
'type' => 'control_title',
'priority' => 50,
) )
);
$wp_customize->add_setting( 'ppwp_pcp_form_headline', array(
'default' => PPW_Constants::DEFAULT_SHORTCODE_HEADLINE,
) );
$wp_customize->add_control(
new PPW_Text_Editor_Custom_Control(
$wp_customize,
'ppwp_pcp_form_headline',
array(
'label' => __( 'Headline', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_headline',
'type' => 'textarea',
'priority' => 60,
)
)
);
$wp_customize->add_setting( 'ppwp_pcp_form_headline_font_size', array(
'default' => PPW_Constants::DEFAULT_HEADLINE_FONT_SIZE,
) );
$wp_customize->add_control( 'ppwp_pcp_form_headline_font_size_control', array(
'label' => __( 'Font Size', 'password-protect-page' ),
'description' => __( 'Font size in px', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_headline_font_size',
'type' => 'number',
'priority' => 70,
) );
$wp_customize->add_setting( 'ppwp_pcp_form_headline_font_weight', array(
'default' => PPW_Constants::DEFAULT_HEADLINE_FONT_WEIGHT,
) );
$wp_customize->add_control( 'ppwp_pcp_form_headline_font_weight_control', array(
'label' => __( 'Font Weight', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_headline_font_weight',
'type' => 'number',
'priority' => 80,
) );
$wp_customize->add_setting( 'ppwp_pcp_form_headline_color', array(
'default' => PPW_Constants::DEFAULT_HEADLINE_FONT_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pcp_form_headline_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_headline_color',
'priority' => 90,
) )
);
$wp_customize->add_setting( 'ppwp_pcp_form_description_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_pcp_form_description_title', array(
'label' => __( 'Description Above Form', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_description_title',
'type' => 'control_title',
'priority' => 100,
) )
);
$wp_customize->add_setting( 'ppwp_pcp_form_description', array(
'default' => PPW_Constants::DEFAULT_SHORTCODE_DESCRIPTION,
) );
$wp_customize->add_control(
new PPW_Text_Editor_Custom_Control(
$wp_customize,
'ppwp_pcp_form_description',
array(
'label' => __( 'Description', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_description',
'type' => 'textarea',
'priority' => 110,
)
)
);
$wp_customize->add_setting( 'ppwp_pcp_form_description_font_size', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_SIZE,
) );
$wp_customize->add_control( 'ppwp_pcp_form_description_font_size_control', array(
'label' => __( 'Font Size', 'password-protect-page' ),
'description' => __( 'Font size in px', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_description_font_size',
'type' => 'number',
'priority' => 120,
) );
$wp_customize->add_setting( 'ppwp_pcp_form_description_font_weight', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_WEIGHT,
) );
$wp_customize->add_control( 'ppwp_pcp_form_description_font_weight_control', array(
'label' => __( 'Font Weight', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_description_font_weight',
'type' => 'number',
'priority' => 130,
) );
$wp_customize->add_setting( 'ppwp_pcp_form_description_color', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pcp_form_description_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_description_color',
'priority' => 140,
) )
);
$wp_customize->add_setting( 'ppwp_pcp_form_desc_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_pcp_form_desc_title',
array(
'label' => __( 'Description Below Form', 'password-protect-page' ),
'section' => 'ppwp_pcp_form',
'settings' => 'ppwp_pcp_form_desc_title',
'type' => 'control_title',
'priority' => 145,
)
)
);
$wp_customize->add_setting( 'ppwp_pcp_description_below_form', array(
'default' => PPW_Constants::DEFAULT_SHORTCODE_DESC_BELOW_PWD_FORM,
) );
$wp_customize->add_control(
new PPW_Text_Editor_Custom_Control(
$wp_customize,
'ppwp_pcp_description_below_form', array(
'label' => __( 'Description', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_description_below_form',
'type' => 'textarea',
'priority' => 146,
)
) );
$wp_customize->add_setting( 'ppwp_pcp_form_description_below_form_font_size', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_SIZE,
) );
$wp_customize->add_control( 'ppwp_pcp_form_description_below_form_font_size_control', array(
'label' => __( 'Font Size', 'password-protect-page' ),
'description' => __( 'Font size in px', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_description_below_form_font_size',
'type' => 'number',
'priority' => 147,
) );
$wp_customize->add_setting( 'ppwp_pcp_form_description_below_form_font_weight', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_WEIGHT,
) );
$wp_customize->add_control( 'ppwp_pcp_form_description_below_form_font_weight_control', array(
'label' => __( 'Font Weight', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_description_below_form_font_weight',
'type' => 'number',
'priority' => 148,
) );
$wp_customize->add_setting( 'ppwp_pcp_form_description_below_form_color', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pcp_form_description_below_form_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => self::GENERAL_SECTION,
'settings' => 'ppwp_pcp_form_description_below_form_color',
'priority' => 149,
) )
);
// $wp_customize->add_setting( 'ppwp_pcp_desc_above_btn' );
// $wp_customize->add_control(
// new PPW_Title_Group_Control(
// $wp_customize,
// 'ppwp_pcp_desc_above_btn',
// array(
// 'label' => __( 'Description Above Button', 'password-protect-page' ),
// 'section' => 'ppwp_pcp_form',
// 'settings' => 'ppwp_pcp_desc_above_btn',
// 'type' => 'control_title',
// 'priority' => 150,
// )
// )
// );
// $wp_customize->add_setting( 'ppwp_pcp_description_above_btn', array(
// 'default' => PPW_Constants::DEFAULT_SHORTCODE_DESC_ABOVE_PWD_BTN,
// ) );
// $wp_customize->add_control(
// new PPW_Text_Editor_Custom_Control(
// $wp_customize,
// 'ppwp_pcp_description_above_btn',
// array(
// 'label' => __( 'Description', 'password-protect-page' ),
// 'section' => self::GENERAL_SECTION,
// 'settings' => 'ppwp_pcp_description_above_btn',
// 'type' => 'textarea',
// 'priority' => 152,
// )
// )
// );
// $wp_customize->add_setting( 'ppwp_pcp_form_description_above_btn_font_size', array(
// 'default' => PPW_Constants::DEFAULT_TEXT_FONT_SIZE,
// ) );
// $wp_customize->add_control( 'ppwp_pcp_form_description_above_btn_font_size_control', array(
// 'label' => __( 'Font Size', 'password-protect-page' ),
// 'description' => __( 'Font size in px', 'password-protect-page' ),
// 'section' => self::GENERAL_SECTION,
// 'settings' => 'ppwp_pcp_form_description_above_btn_font_size',
// 'type' => 'number',
// 'priority' => 153,
// ) );
// $wp_customize->add_setting( 'ppwp_pcp_form_description_above_btn_font_weight', array(
// 'default' => PPW_Constants::DEFAULT_TEXT_FONT_WEIGHT,
// ) );
// $wp_customize->add_control( 'ppwp_pcp_form_description_above_btn_font_weight_control', array(
// 'label' => __( 'Font Weight', 'password-protect-page' ),
// 'section' => self::GENERAL_SECTION,
// 'settings' => 'ppwp_pcp_form_description_above_btn_font_weight',
// 'type' => 'number',
// 'priority' => 154,
// ) );
// $wp_customize->add_setting( 'ppwp_pcp_form_description_above_btn_color', array(
// 'default' => PPW_Constants::DEFAULT_TEXT_FONT_COLOR,
// ) );
// $wp_customize->add_control(
// new \WP_Customize_Color_Control(
// $wp_customize,
// 'ppwp_pcp_form_description_above_btn_color_control', array(
// 'label' => __( 'Text Color', 'password-protect-page' ),
// 'section' => self::GENERAL_SECTION,
// 'settings' => 'ppwp_pcp_form_description_above_btn_color',
// 'priority' => 155,
// ) )
// );
$wp_customize->add_setting( 'ppwp_pcp_form_label_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_pcp_form_label_title',
array(
'label' => __( 'Password Field', 'password-protect-page' ),
'section' => 'ppwp_pcp_form',
'settings' => 'ppwp_pcp_form_label_title',
'type' => 'control_title',
'priority' => 159,
)
)
);
$wp_customize->add_setting( 'ppwp_pcp_form_label', array(
'default' => PPW_Constants::DEFAULT_SHORTCODE_LABEL,
) );
$wp_customize->add_control( 'ppwp_pcp_form_label_control', array(
'label' => __( 'Password Label', 'password-protect-page' ),
'section' => 'ppwp_pcp_form',
'settings' => 'ppwp_pcp_form_label',
'type' => 'text',
'priority' => 160,
) );
$wp_customize->add_setting( 'ppwp_pcp_form_label_font_size', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_SIZE,
) );
$wp_customize->add_control( 'ppwp_pcp_form_label_font_size_control', array(
'label' => __( 'Font Size', 'password-protect-page' ),
'description' => __( 'Font size in px', 'password-protect-page' ),
'section' => 'ppwp_pcp_form',
'settings' => 'ppwp_pcp_form_label_font_size',
'type' => 'number',
'priority' => 170,
) );
$wp_customize->add_setting( 'ppwp_pcp_form_label_font_weight', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_WEIGHT,
) );
$wp_customize->add_control( 'ppwp_pcp_form_label_font_weight_control', array(
'label' => __( 'Font Weight', 'password-protect-page' ),
'section' => 'ppwp_pcp_form',
'settings' => 'ppwp_pcp_form_label_font_weight',
'type' => 'number',
'priority' => 180,
) );
$wp_customize->add_setting( 'ppwp_pcp_form_label_color', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pcp_form_label_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => 'ppwp_pcp_form',
'settings' => 'ppwp_pcp_form_label_color',
'priority' => 190,
) )
);
$wp_customize->add_setting( 'ppwp_pcp_form_placeholder', array(
'default' => PPW_Constants::DEFAULT_PLACEHOLDER,
) );
$wp_customize->add_control( 'ppwp_pcp_form_placeholder_control', array(
'label' => __( 'Placeholder', 'password-protect-page' ),
'section' => 'ppwp_pcp_form',
'settings' => 'ppwp_pcp_form_placeholder',
'type' => 'text',
'priority' => 200,
) );
$wp_customize->add_setting( 'ppwp_pcp_form_show_password_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_pcp_form_show_password_title', array(
'label' => __( 'Show Password', 'password-protect-page' ),
'section' => 'ppwp_pcp_form',
'settings' => 'ppwp_pcp_form_show_password_title',
'type' => 'control_title',
'priority' => 210,
) )
);
$wp_customize->add_setting( 'ppwp_pcp_form_is_show_password', array(
'default' => PPW_Constants::DEFAULT_SHORTCODE_SHOW_PASSWORD,
) );
$wp_customize->add_control(
new PPW_Toggle_Control(
$wp_customize,
'ppwp_pcp_form_is_show_password_control', array(
'label' => __( 'Show Password Reveal Button', 'password-protect-page' ),
'section' => 'ppwp_pcp_form',
'type' => 'toggle',
'settings' => 'ppwp_pcp_form_is_show_password',
'priority' => 220,
) )
);
$wp_customize->add_setting( 'ppwp_pcp_form_show_password_text', array(
'default' => PPW_Constants::DEFAULT_SHORTCODE_SHOW_PASSWORD_TEXT,
) );
$wp_customize->add_control( 'ppwp_pcp_form_show_password_text_control', array(
'label' => __( 'Button Text', 'password-protect-page' ),
'section' => 'ppwp_pcp_form',
'settings' => 'ppwp_pcp_form_show_password_text',
'type' => 'text',
'priority' => 230,
) );
}
/**
* Append error message section.
*
* @param WP_Customize $wp_customize WP Customize class.
*/
public function append_err_msg_section( $wp_customize ) {
/* form error message section */
$wp_customize->add_section( self::ERR_MSG_SECTION, array(
'title' => __( 'Error Message', 'password-protect-page' ),
'panel' => self::PANEL,
'priority' => 20,
) );
$wp_customize->add_setting( 'ppwp_pcp_err_msg_text', array(
'default' => PPW_Constants::DEFAULT_SHORTCODE_ERROR_MSG,
) );
$wp_customize->add_control(
new PPW_Text_Editor_Custom_Control(
$wp_customize,
'ppwp_pcp_err_msg_text',
array(
'label' => __( 'Wrong Password Message', 'password-protect-page' ),
'section' => self::ERR_MSG_SECTION,
'settings' => 'ppwp_pcp_err_msg_text',
'type' => 'textarea',
'priority' => 10,
)
)
);
$wp_customize->add_setting( 'ppwp_pcp_err_msg_text_font_size', array(
'default' => PPW_Constants::DEFAULT_ERROR_TEXT_FONT_SIZE,
) );
$wp_customize->add_control( 'ppwp_pcp_err_msg_text_font_size_control', array(
'label' => __( 'Font Size', 'password-protect-page' ),
'description' => __( 'Font size in px', 'password-protect-page' ),
'section' => self::ERR_MSG_SECTION,
'settings' => 'ppwp_pcp_err_msg_text_font_size',
'type' => 'number',
'priority' => 20,
) );
$wp_customize->add_setting( 'ppwp_pcp_err_msg_text_font_weight', array(
'default' => PPW_Constants::DEFAULT_ERROR_TEXT_FONT_WEIGHT,
) );
$wp_customize->add_control( 'ppwp_pcp_err_msg_text_font_weight_control', array(
'label' => __( 'Font Weight', 'password-protect-page' ),
'section' => self::ERR_MSG_SECTION,
'settings' => 'ppwp_pcp_err_msg_text_font_weight',
'type' => 'number',
'priority' => 25,
) );
$wp_customize->add_setting( 'ppwp_pcp_err_msg_text_color', array(
'default' => PPW_Constants::DEFAULT_ERROR_TEXT_FONT_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pcp_err_msg_text_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => self::ERR_MSG_SECTION,
'settings' => 'ppwp_pcp_err_msg_text_color',
'priority' => 30,
) )
);
$wp_customize->add_setting( 'ppwp_pcp_err_msg_background_color', array(
'default' => '#ffffff',
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pcp_err_msg_background_color_control', array(
'label' => __( 'Background Color', 'password-protect-page' ),
'section' => self::ERR_MSG_SECTION,
'settings' => 'ppwp_pcp_err_msg_background_color',
'priority' => 35,
) )
);
}
/**
* Append button section.
*
* @param WP_Customize $wp_customize WP Customize class.
*/
public function append_button_section( $wp_customize ) {
$wp_customize->add_section( self::BUTTON_SECTION, array(
'title' => __( 'Button', 'password-protect-page' ),
'panel' => self::PANEL,
'priority' => 300,
) );
$wp_customize->add_setting( 'ppwp_pcp_button_text', array(
'default' => PPW_Constants::DEFAULT_SHORTCODE_BUTTON,
) );
$wp_customize->add_control( 'ppwp_pcp_button_text_control', array(
'label' => __( 'Button Label', 'password-protect-page' ),
'section' => 'ppwp_pcp_button',
'settings' => 'ppwp_pcp_button_text',
'type' => 'text',
) );
$wp_customize->add_setting( 'ppwp_pcp_button_loading_text', array(
'default' => __( PPW_Constants::DEFAULT_SHORTCODE_LOADING, 'password-protect-page' ),
) );
$wp_customize->add_control( 'ppwp_pcp_button_loading_text_control', array(
'label' => __( 'Button Loading Label', 'password-protect-page' ),
'section' => 'ppwp_pcp_button',
'settings' => 'ppwp_pcp_button_loading_text',
'type' => 'text',
) );
$wp_customize->add_setting( 'ppwp_pcp_button_text_color', array(
'default' => PPW_Constants::DEFAULT_BUTTON_TEXT_FONT_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pcp_button_text_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => 'ppwp_pcp_button',
'settings' => 'ppwp_pcp_button_text_color',
) )
);
$wp_customize->add_setting( 'ppwp_pcp_button_text_hover_color', array(
'default' => PPW_Constants::DEFAULT_BUTTON_TEXT_HOVER_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pcp_button_text_hover_color_control', array(
'label' => __( 'Text Color (Hover)', 'password-protect-page' ),
'section' => 'ppwp_pcp_button',
'settings' => 'ppwp_pcp_button_text_hover_color',
) )
);
$wp_customize->add_setting( 'ppwp_pcp_button_background_color', array(
'default' => PPW_Constants::DEFAULT_BUTTON_BACKGROUND_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pcp_button_background_color_control', array(
'label' => __( 'Background Color', 'password-protect-page' ),
'section' => 'ppwp_pcp_button',
'settings' => 'ppwp_pcp_button_background_color',
) )
);
$wp_customize->add_setting( 'ppwp_pcp_button_background_hover_color', array(
'default' => PPW_Constants::DEFAULT_BUTTON_BACKGROUND_HOVER_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pcp_button_background_hover_color_control', array(
'label' => __( 'Background Color (Hover)', 'password-protect-page' ),
'section' => 'ppwp_pcp_button',
'settings' => 'ppwp_pcp_button_background_hover_color',
) )
);
}
/**
* Add dynamic styles
*
* @return void
*/
public function dynamic_styles() {
$ppw_custom_css = "
<style>
.ppw-form {
background-color: " . get_theme_mod( 'ppwp_pcp_form_background_color', PPW_Constants::DEFAULT_FORM_BACKGROUND_COLOR ) . "!important;
padding: " . get_theme_mod( 'ppwp_pcp_form_padding', PPW_Constants::DEFAULT_FORM_PADDING ) . "px!important;
border-radius: " . get_theme_mod( 'ppwp_pcp_form_border_radius', PPW_Constants::DEFAULT_FORM_BORDER_RADIUS ) . "px!important;
}
.ppw-headline.ppw-pcp-pf-headline {
font-size: " . get_theme_mod( 'ppwp_pcp_form_headline_font_size', PPW_Constants::DEFAULT_HEADLINE_FONT_SIZE ) . "px!important;
font-weight: " . get_theme_mod( 'ppwp_pcp_form_headline_font_weight', PPW_Constants::DEFAULT_HEADLINE_FONT_WEIGHT ) . "!important;
color: " . get_theme_mod( 'ppwp_pcp_form_headline_color', PPW_Constants::DEFAULT_HEADLINE_FONT_COLOR ) . "!important;
}
.ppw-description.ppw-pcp-pf-desc {
font-size: " . get_theme_mod( 'ppwp_pcp_form_description_font_size', PPW_Constants::DEFAULT_TEXT_FONT_SIZE ) . "px!important;
font-weight: " . get_theme_mod( 'ppwp_pcp_form_description_font_weight', PPW_Constants::DEFAULT_TEXT_FONT_WEIGHT ) . "!important;
color: " . get_theme_mod( 'ppwp_pcp_form_description_color', PPW_Constants::DEFAULT_TEXT_FONT_COLOR ) . "!important;
}
.ppw-pcp-pf-desc-above-btn {
display: block;
}
.ppw-pcp-pf-desc-below-form {
font-size: " . get_theme_mod( 'ppwp_pcp_form_description_below_form_font_size', PPW_Constants::DEFAULT_TEXT_FONT_SIZE ) . "px!important;
font-weight: " . get_theme_mod( 'ppwp_pcp_form_description_below_form_font_weight', PPW_Constants::DEFAULT_TEXT_FONT_WEIGHT ) . "!important;
color: " . get_theme_mod( 'ppwp_pcp_form_description_below_form_color', PPW_Constants::DEFAULT_TEXT_FONT_COLOR ) . "!important;
}
.ppw-input label.ppw-pcp-password-label {
font-size: " . get_theme_mod( 'ppwp_pcp_form_label_font_size', PPW_Constants::DEFAULT_TEXT_FONT_SIZE ) . "px!important;
font-weight: " . get_theme_mod( 'ppwp_pcp_form_label_font_weight', PPW_Constants::DEFAULT_TEXT_FONT_WEIGHT ) . "!important;
color: " . get_theme_mod( 'ppwp_pcp_form_label_color', PPW_Constants::DEFAULT_TEXT_FONT_COLOR ) . "!important;
}
.ppw-form input[type='submit'] {
color: " . get_theme_mod( 'ppwp_pcp_button_text_color', PPW_Constants::DEFAULT_BUTTON_TEXT_FONT_COLOR ) . "!important;
background: " . get_theme_mod( 'ppwp_pcp_button_background_color', PPW_Constants::DEFAULT_BUTTON_BACKGROUND_COLOR ) . "!important;
}
.ppw-form input[type='submit']:hover {
color: " . get_theme_mod( 'ppwp_pcp_button_text_hover_color', PPW_Constants::DEFAULT_BUTTON_TEXT_HOVER_COLOR ) . "!important;
background: " . get_theme_mod( 'ppwp_pcp_button_background_hover_color', PPW_Constants::DEFAULT_BUTTON_BACKGROUND_HOVER_COLOR ) . "!important;
}
div.ppw-error.ppw-pcp-pf-error-msg {
font-size: " . get_theme_mod( 'ppwp_pcp_err_msg_text_font_size', PPW_Constants::DEFAULT_ERROR_TEXT_FONT_SIZE ) . "px!important;
font-weight: " . get_theme_mod( 'ppwp_pcp_err_msg_text_font_weight', PPW_Constants::DEFAULT_ERROR_TEXT_FONT_WEIGHT ) . "!important;
color: " . get_theme_mod( 'ppwp_pcp_err_msg_text_color', PPW_Constants::DEFAULT_ERROR_TEXT_FONT_COLOR ) . "!important;
background: " . get_theme_mod( 'ppwp_pcp_err_msg_background_color', PPW_Constants::DEFAULT_ERROR_TEXT_BACKGROUND_COLOR ) . "!important;
}
</style>
";
// compress $ppw_custom_css.
$ppw_custom_css = preg_replace( "/\s{2,}/", " ", str_replace( "\n", "", str_replace( ', ', ",", $ppw_custom_css ) ) );
echo $ppw_custom_css;
}
/*
* Optimize css.
*/
public function optimize_css( $sw_custom_css ) {
return preg_replace( "/\s{2,}/", " ", str_replace( "\n", "", str_replace( ', ', ",", $sw_custom_css ) ) );
}
}
}

View File

@@ -0,0 +1,440 @@
<?php
if ( ! class_exists( 'PPW_Customizer_Sitewide' ) ) {
// TODO: need to force PPW_Pro_Customizer_Service extend this class to remove the code duplication.
class PPW_Customizer_Sitewide {
/**
* Instance of PPW_Pro_Shortcode class.
*
* @var PPW_Customizer_Sitewide
*/
protected static $instance = null;
public function register_sitewide_form() {
add_action( 'customize_register', array( $this, 'customize_register' ), 15 );
}
public static function register_themes() {
$self = new self();
add_action( 'customize_register', array( $self, 'customize_register_themes' ), 25 );
add_action( 'ppw_custom_style_form_entire_site', array( $self, 'load_css_to_pro' ), 15 );
}
public function register_sitewide_style() {
add_action( PPW_Constants::HOOK_CUSTOM_STYLE_FORM_ENTIRE_SITE, array( $this, 'dynamic_styles' ) );
}
/**
* @param $wp_customize
*/
public function customize_register_themes( $wp_customize ) {
if ( ! class_exists( 'PPW_Presets_Control' ) ) {
include PPW_DIR_PATH . 'includes/customizers/class-ppw-presets.php';
}
$wp_customize->add_section( 'ppw_customize_presets', array(
'title' => __( 'Themes', 'password-protect-page' ),
'panel' => 'ppwp_sitewide',
'priority' => 50,
) );
$wp_customize->add_setting( 'ppw_customize_presets_settings', array(
'default' => 'default0',
'capability' => 'edit_theme_options',
) );
$wp_customize->add_control(
new PPW_Presets_Control( $wp_customize, 'ppw_customize_presets_settings',
array(
'section' => 'ppw_customize_presets',
// 'label' => __( 'Themes', 'loginpress' ),
'choices' => $this->get_templates(),
)
)
);
}
/**
* Get instance of PPW_Customizer
*
* @return PPW_Customizer_Sitewide
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
// Use static instead of self due to the inheritance later.
// For example: ChildSC extends this class, when we call get_instance
// it will return the object of child class. On the other hand, self function
// will return the object of base class.
self::$instance = new static();
}
return self::$instance;
}
/**
* Register customizer fields
*
* @param object $wp_customize customizer object.
*
* @return void
*/
public function customize_register( $wp_customize ) {
if ( ! class_exists( 'PPW_Toggle_Control' ) ) {
include PPW_DIR_PATH . 'includes/customizers/class-ppw-title-group-control.php';
}
if ( ! class_exists( 'PPW_Title_Group_Control' ) ) {
include PPW_DIR_PATH . 'includes/customizers/class-ppw-toggle-control.php';
}
/* register toggle control */
$wp_customize->register_control_type( 'PPW_Toggle_Control' );
$wp_customize->register_control_type( 'PPW_Title_Group_Control' );
$wp_customize->add_panel( 'ppwp_sitewide',
array(
'priority' => 9980,
'capability' => 'edit_theme_options',
'theme_supports' => '',
'title' => __( 'PPWP Sitewide Login Form', 'password-protect-page' ),
)
);
/* form logo section */
$wp_customize->add_section( 'ppwp_pro_form_logo', array(
'title' => __( 'Logo', 'password-protect-page' ),
'panel' => 'ppwp_sitewide',
'priority' => 100,
) );
// Add an option to disable the logo.
$wp_customize->add_setting( 'ppwp_pro_logo_disable' );
$wp_customize->add_control(
new PPW_Toggle_Control(
$wp_customize,
'ppwp_pro_logo_disable_control', array(
'label' => __( 'Disable Logo', 'password-protect-page' ),
'section' => 'ppwp_pro_form_logo',
'type' => 'toggle',
'settings' => 'ppwp_pro_logo_disable',
) )
);
/* logo customize */
$wp_customize->add_setting( 'ppwp_pro_logo_customize', array(
'default' => __( PPW_DIR_URL . 'includes/views/entire-site/assets/ppwp-logo.png', 'password-protect-page' ),
) );
$wp_customize->add_control(
new \WP_Customize_Image_Control(
$wp_customize,
'ppwp_pro_logo_customize_control', array(
'label' => __( 'Logo Image', 'password-protect-page' ),
'section' => 'ppwp_pro_form_logo',
'settings' => 'ppwp_pro_logo_customize',
) )
);
/* logo width */
$wp_customize->add_setting( 'ppwp_pro_logo_customize_width' );
$wp_customize->add_control( 'ppwp_pro_logo_customize_width_control', array(
'label' => __( 'Logo Width', 'password-protect-page' ),
'description' => __( 'Width in px', 'password-protect-page' ),
'section' => 'ppwp_pro_form_logo',
'settings' => 'ppwp_pro_logo_customize_width',
'type' => 'number',
) );
/* logo height */
$wp_customize->add_setting( 'ppwp_pro_logo_customize_height' );
$wp_customize->add_control( 'ppwp_pro_logo_customize_height_control', array(
'label' => __( 'Logo Height', 'password-protect-page' ),
'description' => __( 'Height in px', 'password-protect-page' ),
'section' => 'ppwp_pro_form_logo',
'settings' => 'ppwp_pro_logo_customize_height',
'type' => 'number',
) );
/* logo border-radius */
$wp_customize->add_setting( 'ppwp_pro_logo_customize_border_radius' );
$wp_customize->add_control( 'ppwp_pro_logo_customize_border_radius_control', array(
'label' => __( 'Logo Radius', 'password-protect-page' ),
'description' => __( 'Border Radius in %', 'password-protect-page' ),
'section' => 'ppwp_pro_form_logo',
'settings' => 'ppwp_pro_logo_customize_border_radius',
'type' => 'number',
) );
/* password form section */
$wp_customize->add_section( 'ppwp_pro_form_instructions', array(
'title' => __( 'Password Form', 'password-protect-page' ),
'panel' => 'ppwp_sitewide',
'priority' => 300,
) );
/* form section group */
$wp_customize->add_setting( 'ppwp_pro_form_section_group' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_pro_form_section_group', array(
'label' => __( 'Password Form', 'password-protect-page' ),
'section' => 'ppwp_pro_form_instructions',
'settings' => 'ppwp_pro_form_section_group',
'type' => 'control_title',
) )
);
/* form countdown section */
apply_filters('ppwp_customizer_custom_fields', $wp_customize, $wp_customize);
/* enable form transparency */
$wp_customize->add_setting( 'ppwp_pro_form_enable_transparency' );
$wp_customize->add_control(
new PPW_Toggle_Control(
$wp_customize,
'ppwp_pro_form_enable_transparency_control', array(
'label' => __( 'Enable Form Transparency', 'password-protect-page' ),
'section' => 'ppwp_pro_form_instructions',
'type' => 'toggle',
'settings' => 'ppwp_pro_form_enable_transparency',
) )
);
/* password form background color */
$wp_customize->add_setting( 'ppwp_pro_form_instructions_background_color', array(
'default' => '',
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pro_form_instructions_background_color_control', array(
'label' => __( 'Form Background Color', 'password-protect-page' ),
'section' => 'ppwp_pro_form_instructions',
'settings' => 'ppwp_pro_form_instructions_background_color',
) )
);
/* password form width */
$wp_customize->add_setting( 'ppwp_pro_form_instructions_width' );
$wp_customize->add_control( 'ppwp_pro_form_instructions_width_control', array(
'label' => __( 'Form Width', 'password-protect-page' ),
'section' => 'ppwp_pro_form_instructions',
'settings' => 'ppwp_pro_form_instructions_width',
'description' => 'Width in px',
'type' => 'number',
) );
/* password form border radius */
$wp_customize->add_setting( 'ppwp_pro_form_instructions_border_radius' );
$wp_customize->add_control( 'ppwp_pro_form_instructions_border_radius_control', array(
'label' => __( 'Form Border Radius', 'password-protect-page' ),
'section' => 'ppwp_pro_form_instructions',
'settings' => 'ppwp_pro_form_instructions_border_radius',
'description' => 'Border Radius in px',
'type' => 'number',
) );
/* password label group */
$wp_customize->add_setting( 'ppwp_pro_password_label_group' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_pro_password_label_group', array(
'label' => __( 'Password Field', 'password-protect-page' ),
'section' => 'ppwp_pro_form_instructions',
'settings' => 'ppwp_pro_password_label_group',
'type' => 'control_title',
) )
);
/* password label font size */
$wp_customize->add_setting( 'ppwp_pro_form_instructions_password_label_font_size' );
$wp_customize->add_control( 'ppwp_pro_form_instructions_password_label_font_size_control', array(
'label' => __( 'Font Size', 'password-protect-page' ),
'section' => 'ppwp_pro_form_instructions',
'settings' => 'ppwp_pro_form_instructions_password_label_font_size',
'description' => __( 'Font size in px', 'password-protect-page' ),
'type' => 'number',
) );
/* password label color */
$wp_customize->add_setting( 'ppwp_pro_form_instructions_password_label_color' );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pro_form_instructions_password_label_color_control', array(
'label' => __( 'Label Color', 'password-protect-page' ),
'section' => 'ppwp_pro_form_instructions',
'settings' => 'ppwp_pro_form_instructions_password_label_color',
) )
);
/* placeholder text */
$wp_customize->add_setting( 'ppwp_pro_form_instructions_placeholder' );
$wp_customize->add_control( 'ppwp_pro_form_instructions_placeholder_control', array(
'label' => __( 'Placeholder', 'password-protect-page' ),
'section' => 'ppwp_pro_form_instructions',
'settings' => 'ppwp_pro_form_instructions_placeholder',
'type' => 'text',
) );
/* form button section */
$wp_customize->add_section( 'ppwp_pro_form_button', array(
'title' => __( 'Button', 'password-protect-page' ),
'panel' => 'ppwp_sitewide',
'priority' => 400,
) );
/* button label */
$wp_customize->add_setting( 'ppwp_pro_form_button_label', array(
'default' => __( 'Enter', 'password-protect-page' ),
) );
$wp_customize->add_control( 'ppwp_pro_form_button_label_control', array(
'label' => __( 'Button Label', 'password-protect-page' ),
'section' => 'ppwp_pro_form_button',
'settings' => 'ppwp_pro_form_button_label',
'type' => 'text',
) );
/* button text color */
$wp_customize->add_setting( 'ppwp_pro_form_button_text_color' );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pro_form_button_text_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => 'ppwp_pro_form_button',
'settings' => 'ppwp_pro_form_button_text_color',
) )
);
/* button background color */
$wp_customize->add_setting( 'ppwp_pro_form_button_background_color' );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_pro_form_button_background_color_control', array(
'label' => __( 'Background Color', 'password-protect-page' ),
'section' => 'ppwp_pro_form_button',
'settings' => 'ppwp_pro_form_button_background_color',
) )
);
}
/**
* Add dynamic styles
*
* TODO: move this styles into css file.
* @return void
*/
public function dynamic_styles() {
$sw_custom_css = $this->get_preset_css();
$sw_custom_css = $sw_custom_css . '
.pda-form-login {
width: ' . esc_attr( get_theme_mod( 'ppwp_pro_form_instructions_width' ) ) . 'px!important;
}
.pda-form-login label {
font-size: ' . esc_attr( get_theme_mod( 'ppwp_pro_form_instructions_password_label_font_size' ) ) . 'px!important;
color: ' . esc_attr( get_theme_mod( 'ppwp_pro_form_instructions_password_label_color' ) ) . '!important;
}
.pda-form-login form {
background-color: ' . esc_attr( get_theme_mod( 'ppwp_pro_form_instructions_background_color' ) ) . '!important;
border-radius: ' . esc_attr( get_theme_mod( 'ppwp_pro_form_instructions_border_radius' ) ) . 'px!important;
}
.pda-form-login a.ppw-swp-logo {
background-image: none, url(' . esc_url( get_theme_mod( 'ppwp_pro_logo_customize', PPW_DIR_URL . 'includes/views/entire-site/assets/ppwp-logo.png' ) ) . ')!important;
background-size: cover;
width: ' . esc_attr( get_theme_mod( 'ppwp_pro_logo_customize_width', '' ) ) . 'px!important;
height: ' . esc_attr( get_theme_mod( 'ppwp_pro_logo_customize_height', '' ) ) . 'px!important;
border-radius: ' . esc_attr( get_theme_mod( 'ppwp_pro_logo_customize_border_radius', '' ) ) . '%!important;
}
.pda-form-login .button-login {
color: ' . esc_attr( get_theme_mod( 'ppwp_pro_form_button_text_color' ) ) . '!important;
background-color: ' . esc_attr( get_theme_mod( 'ppwp_pro_form_button_background_color' ) ) . '!important;
border-color: ' . esc_attr( get_theme_mod( 'ppwp_pro_form_button_background_color' ) ) . '!important;
}
';
// remove space in $sw_custom_css.
$sw_custom_css = $this->optimize_css( $sw_custom_css );
echo $sw_custom_css; // phpcs:ignore -- we already escase inside the css
}
/*
* Optimize css.
*/
public function optimize_css( $sw_custom_css ) {
return preg_replace( "/\s{2,}/", " ", str_replace( "\n", "", str_replace( ', ', ",", $sw_custom_css ) ) );
}
/**
* Get templates.
*
* @return array
*/
public function get_templates() {
$free_templates = array();
$themes_name = array(
__( 'Default', 'password-protect-page' ),
__( 'Event', 'password-protect-page' ),
__( 'Business', 'password-protect-page' ),
__( 'Wedding', 'password-protect-page' ),
);
foreach ( $themes_name as $index => $theme_name ) {
$free_templates["default{$index}"] = array(
'thumbnail' => PPW_DIR_URL . 'includes/customizers/assets/images/thumbnail/sw-default' . $index . '.png',
'id' => "default{$index}",
'name' => $theme_name,
'css_file' => PPW_DIR_PATH . 'includes/customizers/assets/css/sw-default' . $index . '.php',
);
}
return apply_filters( 'ppw_customizer_sitewide_templates', $free_templates );
}
/**
* Get css of a theme.
*
* @return string
*/
public function get_preset_css() {
$default_preset = 'default0';
$preset = esc_attr( get_theme_mod( 'ppw_customize_presets_settings', $default_preset ) );
$sw_custom_css = '';
if ( $preset !== $default_preset ) {
$templates = $this->get_templates();
if ( isset( $templates[ $preset ], $templates[ $preset ]['css_file'] ) ) {
ob_start();
include_once( $templates[ $preset ]['css_file'] );
?>
@media screen and (max-width: 768px) {
.pda-form-login {
width: 100%;
}
.pda-form-login form {
width: 90%;
}
}
<?php
$sw_custom_css = ob_get_clean();
}
}
return $sw_custom_css;
}
public function load_css_to_pro() {
echo $this->optimize_css( $this->get_preset_css() ); // phpcs:ignores -- we don't need to escape css, already escape $preset above
}
}
}

View File

@@ -0,0 +1,63 @@
<?php
if ( ! class_exists( 'PPW_Customizer_Upsell' ) ) {
/**
* Register PPW_Customizer_Upsell Configurations.
*/
class PPW_Customizer_Upsell {
/**
* Register upsell section for customize
*
* @var PPW_Customizer_Upsell
*/
protected static $instance = null;
/**
* Constructor for PPW_Customizer_Upsell
*/
public function __construct() {
add_action( 'customize_register', array( $this, 'customize_register' ) );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue' ) );
}
/**
* Get instance of PPW_Customizer_Upsell
*
* @return PPW_Customizer_Upsell
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new static();
}
return self::$instance;
}
public function customize_register( $wp_customize ) {
include PPW_DIR_PATH . 'includes/customizers/class-ppw-customize-link-section.php';
$wp_customize->register_section_type( 'PPW_Customize_Link_Section' );
$wp_customize->add_section(
new PPW_Customize_Link_Section( $wp_customize, 'ppwp_upsell',
array(
'ppwp_text' => __( 'More options available in PPWP Pro', 'password-protect-page' ),
'ppwp_url' => esc_url( 'https://passwordprotectwp.com/features/lite-vs-pro-version/?utm_source=user-website&utm_medium=wp-customizer&utm_campaign=ppwp-free#sitewide-customizer' ),
'capability' => 'edit_theme_options',
'priority' => 9999,
'type' => 'ppwp-upsell-section',
)
)
);
}
public function enqueue() {
wp_enqueue_script( 'ppw-upsell-section-scripts', PPW_DIR_URL . 'includes/customizers/assets/ppw-upsell-section.js', array( 'jquery' ), PPW_VERSION, true );
}
}
}

View File

@@ -0,0 +1,749 @@
<?php
if ( ! class_exists( 'PPW_Customizer_Service' ) ) {
class PPW_Customizer_Service {
/**
* Instance of PPW_Pro_Shortcode class.
*
* @var PPW_Customizer_Service
*/
protected static $instance = null;
/**
* Constructor for PPW_Customizer
*/
public function __construct() {
add_action( 'customize_register', array( $this, 'customize_register' ) );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue' ) );
add_action( 'wp_head', array( $this, 'dynamic_styles' ) );
}
/**
* Get instance of PPW_Customizer
*
* @return PPW_Customizer_Service
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
// Use static instead of self due to the inheritance later.
// For example: ChildSC extends this class, when we call get_instance
// it will return the object of child class. On the other hand, self function
// will return the object of base class.
self::$instance = new static();
}
return self::$instance;
}
/**
* Get below text style.
*
* @return string
*/
public function get_below_text_style() {
if ( defined( 'PPW_PRO_VERSION' ) ) {
return '';
}
$desc_font_size = get_theme_mod( 'ppwp_form_instructions_below_text_font_size' );
$desc_font_weight = get_theme_mod( 'ppwp_form_instructions_below_text_font_weight' );
$desc_color = get_theme_mod( 'ppwp_form_instructions_below_text_color' );
$customizer_style = "
.ppw-ppf-desc-below {
font-size: " . $desc_font_size . "px!important;
font-weight: " . $desc_font_weight . "!important;
color: " . $desc_color . "!important;
}
";
return $customizer_style;
}
/**
* Add below description customize.
*
* @param $wp_customize
*/
public function add_below_desc( $wp_customize ) {
if ( defined( 'PPW_PRO_VERSION' ) ) {
return;
}
$wp_customize->add_setting( 'ppwp_form_instructions_description_below_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_form_instructions_description_below_title',
array(
'label' => __( 'Description Below Form', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_description_below_title',
'type' => 'control_title',
)
)
);
/* instructions text */
$wp_customize->add_setting( 'ppwp_form_instructions_below_text' );
$wp_customize->add_control(
new PPW_Text_Editor_Custom_Control(
$wp_customize,
'ppwp_form_instructions_below_text',
array(
'label' => __( 'Description', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_below_text',
'type' => 'textarea',
)
)
);
/* instructions font size */
$wp_customize->add_setting( 'ppwp_form_instructions_below_text_font_size' );
$wp_customize->add_control(
'ppwp_form_instructions_below_text_font_size_control',
array(
'label' => __( 'Font Size', 'password-protect-page' ),
'description' => __( 'Font size in px', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_below_text_font_size',
'type' => 'number',
) );
/* instructions font weight */
$wp_customize->add_setting( 'ppwp_form_instructions_below_text_font_weight' );
$wp_customize->add_control(
'ppwp_form_instructions_below_text_font_weight_control',
array(
'label' => __( 'Font Weight', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_below_text_font_weight',
'type' => 'number',
) );
/* text color - form instructions */
$wp_customize->add_setting( 'ppwp_form_instructions_below_text_color' );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_form_instructions_below_text_color_control',
array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_below_text_color',
)
)
);
}
/**
* Register customizer fields
*
* @param object $wp_customize customizer object.
*
* @return void
*/
public function customize_register( $wp_customize ) {
if ( ! class_exists( 'PPW_Title_Group_Control' ) ) {
include PPW_DIR_PATH . 'includes/customizers/class-ppw-title-group-control.php';
}
if ( ! class_exists( 'PPW_Toggle_Control' ) ) {
include PPW_DIR_PATH . 'includes/customizers/class-ppw-toggle-control.php';
}
if ( ! class_exists( 'PPW_Text_Editor_Custom_Control' ) ) {
include PPW_DIR_PATH . 'includes/customizers/class-ppw-text-editor-control.php';
}
/* register toggle control */
$wp_customize->register_control_type( 'PPW_Toggle_Control' );
$wp_customize->register_control_type( 'PPW_Title_Group_Control' );
$wp_customize->register_control_type( 'PPW_Text_Editor_Custom_Control' );
$wp_customize->add_panel( 'ppwp',
array(
'priority' => 9970,
'capability' => 'edit_theme_options',
'theme_supports' => '',
'title' => __( 'PPWP Single Password Form', 'password-protect-page' ),
)
);
/* form instructions section */
$wp_customize->add_section( 'ppwp_form_instructions', array(
'title' => __( 'Password Form', 'password-protect-page' ),
'panel' => 'ppwp',
'priority' => 100,
) );
$wp_customize->add_setting( 'ppwp_form_instructions_background_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_form_instructions_background_title', array(
'label' => __( 'Background', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_background_title',
'type' => 'control_title',
) )
);
/* form background color */
$wp_customize->add_setting( 'ppwp_form_instructions_background_color', array(
'default' => PPW_Constants::DEFAULT_FORM_BACKGROUND_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_form_instructions_background_color_control', array(
'label' => __( 'Background Color', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_background_color',
) )
);
/* form background padding */
$wp_customize->add_setting( 'ppwp_form_instructions_padding', array(
'default' => PPW_Constants::DEFAULT_FORM_PADDING,
) );
$wp_customize->add_control( 'ppwp_form_instructions_padding_control', array(
'label' => __( 'Padding', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'description' => 'Padding in px',
'settings' => 'ppwp_form_instructions_padding',
'type' => 'number',
) );
/* form background border radius */
$wp_customize->add_setting( 'ppwp_form_instructions_border_radius', array(
'default' => PPW_Constants::DEFAULT_FORM_BORDER_RADIUS,
) );
$wp_customize->add_control( 'ppwp_form_instructions_border_radius_control', array(
'label' => __( 'Border Radius', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'description' => 'Border Radius in px',
'settings' => 'ppwp_form_instructions_border_radius',
'type' => 'number',
) );
$wp_customize->add_setting( 'ppwp_form_instructions_headline_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_form_instructions_headline_title', array(
'label' => __( 'Headline', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_headline_title',
'type' => 'control_title',
) )
);
/* instructions headline */
$wp_customize->add_setting( 'ppwp_form_instructions_headline', array(
'default' => __( PPW_Constants::DEFAULT_HEADLINE_TEXT, 'password-protect-page' ),
) );
$wp_customize->add_control(
new PPW_Text_Editor_Custom_Control(
$wp_customize,
'ppwp_form_instructions_headline',
array(
'label' => __( 'Headline', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_headline',
'type' => 'textarea',
)
)
);
/* headline font size */
$wp_customize->add_setting( 'ppwp_form_instructions_headline_font_size', array(
'default' => PPW_Constants::DEFAULT_HEADLINE_FONT_SIZE,
) );
$wp_customize->add_control( 'ppwp_form_instructions_headline_font_size_control', array(
'label' => __( 'Font Size', 'password-protect-page' ),
'description' => __( 'Font size in px', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_headline_font_size',
'type' => 'number',
) );
/* headline font weight */
$wp_customize->add_setting( 'ppwp_form_instructions_headline_font_weight', array(
'default' => PPW_Constants::DEFAULT_HEADLINE_FONT_WEIGHT,
) );
$wp_customize->add_control( 'ppwp_form_instructions_headline_font_weight_control', array(
'label' => __( 'Font Weight', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_headline_font_weight',
'type' => 'number',
) );
/* headline color */
$wp_customize->add_setting( 'ppwp_form_instructions_headline_color', array(
'default' => PPW_Constants::DEFAULT_HEADLINE_FONT_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_form_instructions_headline_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_headline_color',
) )
);
$wp_customize->add_setting( 'ppwp_form_instructions_description_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_form_instructions_description_title', array(
'label' => __( 'Description Above Form', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_description_title',
'type' => 'control_title',
) )
);
/* instructions text */
$wp_customize->add_setting( 'ppwp_form_instructions_text', array(
'default' => __( apply_filters( PPW_Constants::HOOK_MESSAGE_PASSWORD_FORM, PPW_Constants::DEFAULT_FORM_MESSAGE ), 'password-protect-page' ),
) );
$wp_customize->add_control(
new PPW_Text_Editor_Custom_Control(
$wp_customize,
'ppwp_form_instructions_text',
array(
'label' => __( 'Description', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_text',
'type' => 'textarea',
)
)
);
/* instructions font size */
$wp_customize->add_setting( 'ppwp_form_instructions_text_font_size', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_SIZE,
) );
$wp_customize->add_control( 'ppwp_form_instructions_text_font_size_control', array(
'label' => __( 'Font Size', 'password-protect-page' ),
'description' => __( 'Font size in px', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_text_font_size',
'type' => 'number',
) );
/* instructions font weight */
$wp_customize->add_setting( 'ppwp_form_instructions_text_font_weight', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_WEIGHT,
) );
$wp_customize->add_control( 'ppwp_form_instructions_text_font_weight_control', array(
'label' => __( 'Font Weight', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_text_font_weight',
'type' => 'number',
) );
/* text color - form instructions */
$wp_customize->add_setting( 'ppwp_form_instructions_text_color', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_form_instructions_text_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_text_color',
) )
);
$this->add_below_desc( $wp_customize );
// Add one more tab below "Description Text Color" control.
do_action( 'ppw_customize_after_text_color', $wp_customize );
$wp_customize->add_setting( 'ppwp_form_instructions_label_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_form_instructions_label_title', array(
'label' => __( 'Password Field', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_label_title',
'type' => 'control_title',
) )
);
$wp_customize->add_setting( 'ppwp_form_instructions_password_label', array(
'default' => PPW_Constants::DEFAULT_PASSWORD_LABEL,
) );
$wp_customize->add_control( 'ppwp_form_instructions_password_label_control', array(
'label' => __( 'Password Label', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_password_label',
'type' => 'text',
) );
/* instructions font size */
$wp_customize->add_setting( 'ppwp_form_instructions_password_label_font_size', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_SIZE,
) );
$wp_customize->add_control( 'ppwp_form_instructions_password_label_font_size_control', array(
'label' => __( 'Font Size', 'password-protect-page' ),
'description' => __( 'Font size in px', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_password_label_font_size',
'type' => 'number',
) );
/* instructions font weight */
$wp_customize->add_setting( 'ppwp_form_instructions_password_label_font_weight', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_WEIGHT,
) );
$wp_customize->add_control( 'ppwp_form_instructions_password_label_font_weight_control', array(
'label' => __( 'Font Weight', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_password_label_font_weight',
'type' => 'number',
) );
/* text color - form instructions */
$wp_customize->add_setting( 'ppwp_form_instructions_password_label_color', array(
'default' => PPW_Constants::DEFAULT_TEXT_FONT_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_form_instructions_password_label_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_password_label_color',
) )
);
/* placeholder text */
$wp_customize->add_setting( 'ppwp_form_instructions_placeholder', array(
'default' => __( PPW_Constants::DEFAULT_PLACEHOLDER, 'password-protect-page' ),
) );
$wp_customize->add_control( 'ppwp_form_instructions_placeholder_control', array(
'label' => __( 'Placeholder', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_placeholder',
'type' => 'text',
) );
$wp_customize->add_setting( 'ppwp_form_instructions_show_password_title' );
$wp_customize->add_control(
new PPW_Title_Group_Control(
$wp_customize,
'ppwp_form_instructions_show_password_title', array(
'label' => __( 'Show Password', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_show_password_title',
'type' => 'control_title',
) )
);
/* password typing - form instructions */
$wp_customize->add_setting( 'ppwp_form_instructions_is_show_password', array(
'default' => PPW_Constants::DEFAULT_IS_SHOW_PASSWORD,
) );
$wp_customize->add_control(
new PPW_Toggle_Control(
$wp_customize,
'ppwp_form_instructions_is_show_password_control', array(
'label' => __( 'Show Password Reveal Button', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'type' => 'toggle',
'settings' => 'ppwp_form_instructions_is_show_password',
) )
);
/* show password text */
$wp_customize->add_setting( 'ppwp_form_instructions_show_password_text', array(
'default' => PPW_Constants::DEFAULT_SHOW_PASSWORD_TEXT,
) );
$wp_customize->add_control( 'ppwp_form_instructions_show_password_text_control', array(
'label' => __( 'Button Text', 'password-protect-page' ),
'section' => 'ppwp_form_instructions',
'settings' => 'ppwp_form_instructions_show_password_text',
'type' => 'text',
) );
do_action('ppw_customize_after_form_instructions', $wp_customize);
/* form error message section */
$wp_customize->add_section( 'ppwp_form_error_message', array(
'title' => __( 'Error Messages', 'password-protect-page' ),
'panel' => 'ppwp',
'priority' => 200,
) );
/* error message text */
$wp_customize->add_setting( 'ppwp_form_error_message_text', array(
'default' => __( apply_filters( PPW_Constants::HOOK_MESSAGE_ENTERING_WRONG_PASSWORD, PPW_Constants::DEFAULT_WRONG_PASSWORD_MESSAGE ), 'password-protect-page' ),
) );
$wp_customize->add_control(
new PPW_Text_Editor_Custom_Control(
$wp_customize,
'ppwp_form_error_message_text',
array(
'label' => __( 'Wrong Password Message', 'password-protect-page' ),
'section' => 'ppwp_form_error_message',
'settings' => 'ppwp_form_error_message_text',
'type' => 'textarea',
'priority' => 10,
)
)
);
/* error message text */
$wp_customize->add_setting( 'ppwp_form_error_recaptcha_message_text', array(
'default' => __( PPW_Constants::DEFAULT_ERROR_RECAPTCHA_MESSAGE, 'password-protect-page' ),
) );
$wp_customize->add_control(
new PPW_Text_Editor_Custom_Control(
$wp_customize,
'ppwp_form_error_recaptcha_message_text',
array(
'label' => __( 'Failed reCAPTCHA Message', 'password-protect-page' ),
'section' => 'ppwp_form_error_message',
'settings' => 'ppwp_form_error_recaptcha_message_text',
'type' => 'textarea',
'priority' => 15,
)
)
);
/* error message font size */
$wp_customize->add_setting( 'ppwp_form_error_message_text_font_size', array(
'default' => PPW_Constants::DEFAULT_ERROR_TEXT_FONT_SIZE,
) );
$wp_customize->add_control( 'ppwp_form_error_message_text_font_size_control', array(
'label' => __( 'Font Size', 'password-protect-page' ),
'description' => __( 'Font size in px', 'password-protect-page' ),
'section' => 'ppwp_form_error_message',
'settings' => 'ppwp_form_error_message_text_font_size',
'type' => 'number',
'priority' => 20,
) );
/* error message font weight */
$wp_customize->add_setting( 'ppwp_form_error_message_text_font_weight', array(
'default' => PPW_Constants::DEFAULT_ERROR_TEXT_FONT_WEIGHT,
) );
$wp_customize->add_control( 'ppwp_form_error_message_text_font_weight_control', array(
'label' => __( 'Font Weight', 'password-protect-page' ),
'section' => 'ppwp_form_error_message',
'settings' => 'ppwp_form_error_message_text_font_weight',
'type' => 'number',
'priority' => 25,
) );
/* error message text color */
$wp_customize->add_setting( 'ppwp_form_error_message_text_color', array(
'default' => PPW_Constants::DEFAULT_ERROR_TEXT_FONT_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_form_error_message_text_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => 'ppwp_form_error_message',
'settings' => 'ppwp_form_error_message_text_color',
'priority' => 30,
) )
);
/* error message background color */
$wp_customize->add_setting( 'ppwp_form_error_message_background_color', array(
'default' => '#ffffff',
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_form_error_message_background_color_control', array(
'label' => __( 'Background Color', 'password-protect-page' ),
'section' => 'ppwp_form_error_message',
'settings' => 'ppwp_form_error_message_background_color',
'priority' => 35,
) )
);
/* form button */
$wp_customize->add_section( 'ppwp_form_button', array(
'title' => __( 'Button', 'password-protect-page' ),
'panel' => 'ppwp',
'priority' => 300,
) );
/* button label */
$wp_customize->add_setting( 'ppwp_form_button_label', array(
'default' => __( PPW_Constants::DEFAULT_SUBMIT_LABEL, 'password-protect-page' ),
) );
$wp_customize->add_control( 'ppwp_form_button_label_control', array(
'label' => __( 'Button Label', 'password-protect-page' ),
'section' => 'ppwp_form_button',
'settings' => 'ppwp_form_button_label',
'type' => 'text',
) );
/* button text color */
$wp_customize->add_setting( 'ppwp_form_button_text_color', array(
'default' => PPW_Constants::DEFAULT_BUTTON_TEXT_FONT_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_form_button_text_color_control', array(
'label' => __( 'Text Color', 'password-protect-page' ),
'section' => 'ppwp_form_button',
'settings' => 'ppwp_form_button_text_color',
) )
);
/* button text hover color */
$wp_customize->add_setting( 'ppwp_form_button_text_hover_color', array(
'default' => PPW_Constants::DEFAULT_BUTTON_TEXT_HOVER_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_form_button_text_hover_color_control', array(
'label' => __( 'Text Color (Hover)', 'password-protect-page' ),
'section' => 'ppwp_form_button',
'settings' => 'ppwp_form_button_text_hover_color',
) )
);
/* button background color */
$wp_customize->add_setting( 'ppwp_form_button_background_color', array(
'default' => PPW_Constants::DEFAULT_BUTTON_BACKGROUND_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_form_button_background_color_control', array(
'label' => __( 'Background Color', 'password-protect-page' ),
'section' => 'ppwp_form_button',
'settings' => 'ppwp_form_button_background_color',
) )
);
/* button background hover color */
$wp_customize->add_setting( 'ppwp_form_button_background_hover_color', array(
'default' => PPW_Constants::DEFAULT_BUTTON_BACKGROUND_HOVER_COLOR,
) );
$wp_customize->add_control(
new \WP_Customize_Color_Control(
$wp_customize,
'ppwp_form_button_background_hover_color_control', array(
'label' => __( 'Background Color (Hover)', 'password-protect-page' ),
'section' => 'ppwp_form_button',
'settings' => 'ppwp_form_button_background_hover_color',
) )
);
}
/**
* Add dynamic styles
*
* @return void
*/
public function dynamic_styles() {
$below_text_styles = $this->get_below_text_style();
$ppw_custom_css = "
<style>
.ppw-ppf-input-container {
background-color: " . get_theme_mod( 'ppwp_form_instructions_background_color', PPW_Constants::DEFAULT_FORM_BACKGROUND_COLOR ) . "!important;
padding: " . get_theme_mod( 'ppwp_form_instructions_padding', PPW_Constants::DEFAULT_FORM_PADDING ) . "px!important;
border-radius: " . get_theme_mod( 'ppwp_form_instructions_border_radius', PPW_Constants::DEFAULT_FORM_BORDER_RADIUS ) . "px!important;
}
.ppw-ppf-input-container div.ppw-ppf-headline {
font-size: " . get_theme_mod( 'ppwp_form_instructions_headline_font_size', PPW_Constants::DEFAULT_HEADLINE_FONT_SIZE ) . "px!important;
font-weight: " . get_theme_mod( 'ppwp_form_instructions_headline_font_weight', PPW_Constants::DEFAULT_HEADLINE_FONT_WEIGHT ) . "!important;
color: " . get_theme_mod( 'ppwp_form_instructions_headline_color', PPW_Constants::DEFAULT_HEADLINE_FONT_COLOR ) . "!important;
}
.ppw-ppf-input-container div.ppw-ppf-desc {
font-size: " . get_theme_mod( 'ppwp_form_instructions_text_font_size', PPW_Constants::DEFAULT_TEXT_FONT_SIZE ) . "px!important;
font-weight: " . get_theme_mod( 'ppwp_form_instructions_text_font_weight', PPW_Constants::DEFAULT_TEXT_FONT_WEIGHT ) . "!important;
color: " . get_theme_mod( 'ppwp_form_instructions_text_color', PPW_Constants::DEFAULT_TEXT_FONT_COLOR ) . "!important;
}
.ppw-ppf-input-container label.ppw-pwd-label {
font-size: " . get_theme_mod( 'ppwp_form_instructions_password_label_font_size', PPW_Constants::DEFAULT_TEXT_FONT_SIZE ) . "px!important;
font-weight: " . get_theme_mod( 'ppwp_form_instructions_password_label_font_weight', PPW_Constants::DEFAULT_TEXT_FONT_WEIGHT ) . "!important;
color: " . get_theme_mod( 'ppwp_form_instructions_password_label_color', PPW_Constants::DEFAULT_TEXT_FONT_COLOR ) . "!important;
}
div.ppwp-wrong-pw-error {
font-size: " . get_theme_mod( 'ppwp_form_error_message_text_font_size', PPW_Constants::DEFAULT_ERROR_TEXT_FONT_SIZE ) . "px!important;
font-weight: " . get_theme_mod( 'ppwp_form_error_message_text_font_weight', PPW_Constants::DEFAULT_ERROR_TEXT_FONT_WEIGHT ) . "!important;
color: " . get_theme_mod( 'ppwp_form_error_message_text_color', PPW_Constants::DEFAULT_ERROR_TEXT_FONT_COLOR ) . "!important;
background: " . get_theme_mod( 'ppwp_form_error_message_background_color', PPW_Constants::DEFAULT_ERROR_TEXT_BACKGROUND_COLOR ) . "!important;
}
.ppw-ppf-input-container input[type='submit'] {
color: " . get_theme_mod( 'ppwp_form_button_text_color', PPW_Constants::DEFAULT_BUTTON_TEXT_FONT_COLOR ) . "!important;
background: " . get_theme_mod( 'ppwp_form_button_background_color', PPW_Constants::DEFAULT_BUTTON_BACKGROUND_COLOR ) . "!important;
}
.ppw-ppf-input-container input[type='submit']:hover {
color: " . get_theme_mod( 'ppwp_form_button_text_hover_color', PPW_Constants::DEFAULT_BUTTON_TEXT_HOVER_COLOR ) . "!important;
background: " . get_theme_mod( 'ppwp_form_button_background_hover_color', PPW_Constants::DEFAULT_BUTTON_BACKGROUND_HOVER_COLOR ) . "!important;
}
{$below_text_styles}
</style>
";
// compress $ppw_custom_css.
$ppw_custom_css = preg_replace( "/\s{2,}/", " ", str_replace( "\n", "", str_replace( ', ', ",", $ppw_custom_css ) ) );
echo $ppw_custom_css;
}
/**
* Enqueue script for customizer control
*/
public function enqueue() {
wp_enqueue_script( 'ppwp-customizer', PPW_DIR_URL . 'admin/js/customizer.js', array( 'jquery' ), PPW_VERSION, true );
wp_localize_script(
"ppwp-customizer",
'ppw_data',
array(
'backgroundDIR' => PPW_DIR_URL . 'includes/customizers/assets/images/backgrounds/',
)
);
}
}
}

View File

@@ -0,0 +1,168 @@
<?php
if ( ! class_exists( 'PPW_Entire_Site_Services' ) ) {
class PPW_Entire_Site_Services {
/**
* Auth cookie
*
* @return bool
*/
function validate_auth_cookie_entire_site() {
$cookie_elements = $this->parse_cookie_entire_site();
if ( false === $cookie_elements ) {
return false;
}
if ( (int) $cookie_elements[1] < time() ) {
return false;
}
$password = ppw_core_get_setting_entire_site_type_string( PPW_Constants::PASSWORD_ENTIRE_SITE );
$hash = hash_hmac( 'md5', PPW_Constants::ENTIRE_SITE_COOKIE_NAME, $password );
return $cookie_elements[0] === $hash;
}
/**
* Parse cookie
*
* @return array|bool
*/
function parse_cookie_entire_site() {
$_cookie = wp_unslash( $_COOKIE );
$cookie_name = PPW_Constants::ENTIRE_SITE_COOKIE_NAME;
if ( empty( $_cookie[ $cookie_name ] ) ) {
return false;
}
$cookie = $_cookie[ $cookie_name ];
$cookie_elements = explode( '|', $cookie );
if ( count( $cookie_elements ) !== 2 ) {
return false;
}
return $cookie_elements;
}
/**
* Check is valid password
*
* @param $password
*
* @return bool
*/
public function entire_site_is_valid_password( $password ) {
$_request = wp_unslash( $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Processing form data without nonce verification. - Not verify nonce for password validate.
if ( ! isset( $_request['input_wp_protect_password'] ) ) {
return false;
}
$password_input = $_request['input_wp_protect_password'];
$validated = md5( $password_input ) === $password;
return apply_filters( 'ppw_sitewide_valid_password', $validated );
}
/**
* Set password to cookie
*
* @param string $password Password.
*/
public function entire_site_set_password_to_cookie( $password ) {
$expiration = time() + 7 * DAY_IN_SECONDS;
$cookie_expired = ppw_core_get_setting_type_string( PPW_Constants::COOKIE_EXPIRED );
if ( ! empty( $cookie_expired ) ) {
$time = explode( ' ', $cookie_expired )[0];
$unit = ppw_core_get_unit_time( $cookie_expired );
if ( 0 !== $unit ) {
$expiration = time() + (int) $time * $unit;
}
}
$hash = hash_hmac( 'md5', PPW_Constants::ENTIRE_SITE_COOKIE_NAME, $password );
$cookie = $hash . '|' . $expiration;
$expiration = apply_filters( 'ppw_sitewide_cookie_expiration', $expiration, $password );
ppw_free_bypass_cache_with_cookie_for_pro_version( $cookie, $expiration );
setcookie( PPW_Constants::ENTIRE_SITE_COOKIE_NAME, $cookie, $expiration, COOKIEPATH, COOKIE_DOMAIN );
}
/**
* Redirect after enter password
*/
public function entire_site_redirect_after_enter_password() {
// Can get the HTTP_REFERER first as the redirect URL that:
// Fixes the private link redirection belonged to PPP Pro.
$_server = wp_unslash( $_SERVER );
if ( ! empty( $_server['HTTP_REFERER'] ) ) {
$current_url = $_server['HTTP_REFERER'];
} else {
global $wp;
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
}
// TODO: consider to user wp_safe_redirect.
wp_redirect( $current_url );
}
/**
* Handle before update settings for entire site
*
* @param $data_settings
*
* @return bool
*/
public function handle_before_update_settings( $data_settings ) {
// Clear cache Super Cache plugin
// $free_cache = new PPW_Cache_Services();
// $free_cache->clear_cache_super_cache();
if ( array_key_exists( PPW_Constants::IS_PROTECT_ENTIRE_SITE, $data_settings ) && $data_settings[ PPW_Constants::IS_PROTECT_ENTIRE_SITE ] === "true" ) {
// Create new password
if ( ! array_key_exists( PPW_Constants::SET_NEW_PASSWORD_ENTIRE_SITE, $data_settings ) ) {
return $this->create_new_password( $data_settings );
}
// Change password
if ( array_key_exists( PPW_Constants::SET_NEW_PASSWORD_ENTIRE_SITE, $data_settings ) && $data_settings[ PPW_Constants::SET_NEW_PASSWORD_ENTIRE_SITE ] === "true" ) {
return $this->change_password( $data_settings );
}
// Don't change password
return true;
}
// Unprotect entire site
return delete_option( PPW_Constants::ENTIRE_SITE_OPTIONS );
}
/**
* Create new password entire site
*
* @param $data_settings
*
* @return bool
*/
public function create_new_password( $data_settings ) {
$data_settings[ PPW_Constants::PASSWORD_ENTIRE_SITE ] = md5( $data_settings[ PPW_Constants::PASSWORD_ENTIRE_SITE ] );
update_option( PPW_Constants::ENTIRE_SITE_OPTIONS, $data_settings );
return true;
}
/**
* Change password entire site
*
* @param $data_settings
*
* @return bool
*/
public function change_password( $data_settings ) {
$data_settings[ PPW_Constants::PASSWORD_ENTIRE_SITE ] = md5( $data_settings[ PPW_Constants::PASSWORD_ENTIRE_SITE ] );
unset( $data_settings[ PPW_Constants::SET_NEW_PASSWORD_ENTIRE_SITE ] );
update_option( PPW_Constants::ENTIRE_SITE_OPTIONS, $data_settings );
return true;
}
}
}

View File

@@ -0,0 +1,62 @@
<?php
/**
* Created by PhpStorm.
* User: gaupoit
* Date: 7/31/19
* Time: 14:57
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'PPW_Default_PW_Manager_Services' ) ) {
class PPW_Default_PW_Manager_Services extends PPW_Migration_Manager {
/**
* Get module name.
*
* Retrieve the module name.
*
* @return string Module name.
* @since 1.7.0
* @access public
*
*/
public function get_name() {
return 'default-pwd-migration';
}
public function get_action() {
return 'ppw_default_pwd_migration';
}
public function get_plugin_name() {
return 'ppw';
}
public function get_plugin_label() {
return __( PPW_PLUGIN_NAME, 'password-protect-page' );
}
public function get_updater_label() {
return sprintf( '<strong>%s </strong> &#8211;', __( 'Password Protect WordPress', 'password-protect-page' ) );
}
public function get_query_limit() {
// TODO: Implement get_query_limit() method.
}
public function get_migrations_class() {
return 'PPW_Default_PW_Migrations';
}
public function get_migration_label() {
return sprintf( '<strong>%s </strong> &#8211;', __( 'PPWP Data Migration', 'password-protect-page' ) );
}
public function get_success_message() {
return '<p>' . sprintf( __( '%s The <a href="https://passwordprotectwp.com/password-migration/" target="_blank" rel="noopener noreferrer">password migration process</a> is now complete. Thank you for your patience!', 'password-protect-page' ), $this->get_updater_label() ) . '</p>';
}
}
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* Created by PhpStorm.
* User: gaupoit
* Date: 7/25/19
* Time: 14:48
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'PPW_Migration' ) ) {
class PPW_Migration extends PPW_Background_Task {
}
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* Created by PhpStorm.
* User: gaupoit
* Date: 7/31/19
* Time: 14:57
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'PPW_Default_PW_Migrations' ) ) {
class PPW_Default_PW_Migrations {
public static function migrate_v_2_6_0() {
error_log( 'Migrate Default Password' );
$free_service = new PPW_Password_Services();
$free_service->migrate_default_password();
PPW_Options_Services::get_instance()->add_flag( PPW_Constants::MIGRATED_DEFAULT_PW );
error_log( 'Migrated OK' );
}
}
}

View File

@@ -0,0 +1,48 @@
<?php
/**
* Created by PhpStorm.
* User: gaupoit
* Date: 7/30/19
* Time: 20:34
*/
if ( ! class_exists( 'PPW_Options_Services' ) ) {
class PPW_Options_Services {
protected static $instance;
private $prefix;
public function __construct() {
$this->prefix = 'ppw_pro';
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new PPW_Options_Services();
}
return self::$instance;
}
public function add_flag( $flag ) {
update_option( $this->prefix . '_' . $flag, 1 );
}
public function get_flag( $flag ) {
return get_option( $this->prefix . '_' . $flag );
}
public function delete_flag( $flag ) {
$option_name = $this->prefix . '_' . $flag;
if ( is_multisite() ) {
foreach ( get_sites() as $site ) {
delete_blog_option( $site->blog_id, $option_name );
}
} else {
delete_option( $option_name );
}
}
}
}

View File

@@ -0,0 +1,134 @@
<?php
class PPW_Password_Recovery_Manager extends PPW_Background_Task_Manager {
private $completed;
public function __construct() {
$this->handle_admin_notices();
parent::__construct();
}
protected function handle_admin_notices() {
if ( ! is_admin() ) {
return;
}
$action = 'admin_notices';
if ( $this->is_completed() ) {
add_action( $action, array( $this, 'admin_notice_upgrade_is_completed' ) );
}
if ( $this->is_running() ) {
add_action( $action, array( $this, 'admin_notice_upgrade_is_running' ) );
}
}
public function is_running() {
$task_runner = $this->get_task_runner();
return $task_runner->is_running();
}
public function is_completed() {
if ( isset( $this->completed ) ) {
return $this->completed;
}
$this->completed = $this->get_flag( 'completed' );
return $this->completed;
}
public function get_task_runner_class() {
return 'PPW_Password_Recovery';
}
public function get_query_limit() {
return 100;
}
public function on_runner_complete( $did_tasks = false ) {
// Implement log here
if ( $did_tasks ) {
$this->add_flag( 'completed' );
}
}
public function start_run() {
$updater = $this->get_task_runner();
if ( $updater->is_running() ) {
return;
}
$restore_password_callbacks = $this->get_restore_password_callbacks();
if ( empty( $restore_password_callbacks ) ) {
$this->on_runner_complete();
return;
}
foreach ( $restore_password_callbacks as $callback ) {
$updater->push_to_queue(
array(
'callback' => $callback,
)
);
}
$updater->save()->dispatch();
}
public function get_restore_password_callbacks() {
$callbacks[] = array( 'PPW_Password_Recovery_Manager', 'restore_passwords' );
return $callbacks;
}
public static function restore_passwords() {
return ( new PPW_Password_Services() )->restore_wp_post_password();
}
public function admin_notice_upgrade_is_running() {
$continue_action = $this->get_continue_action_url();
$message = '<p>'
. __( 'Password recovery process is running in the background.', 'password-protect-page' )
. '</p>'
. '<p>'
. sprintf( 'Taking a while? <a href="%s" class="button-primary">Click here to run it now</a>', $continue_action )
. '</p>';
echo '<div class="notice notice-warning">' . wp_kses_post( $message ) . '</div>';
}
public function admin_notice_upgrade_is_completed() {
$this->delete_flag( 'completed' );
$message = $this->get_success_message();
if ( ! empty( $message ) ) {
echo '<div class="notice notice-success">' . wp_kses_post( $message ) . '</div>';
}
}
public function get_action() {
return 'password_recovery_process';
}
public function get_plugin_name() {
return 'ppw';
}
public function get_plugin_label() {
return __( PPW_PLUGIN_NAME, 'password-protect-page' );
}
public function get_name() {
return 'ppw-password-recovery';
}
public function get_success_message() {
return '<p>' . sprintf( __( '%s <a href="https://passwordprotectwp.com/docs/password-migration/#backup" target="_blank" rel="noopener noreferrer">Password recovery process</a> is now complete.. Thank you for your patience!', 'password-protect-page' ), $this->get_updater_label() ) . '</p>';
}
public function get_updater_label() {
return sprintf( '<strong>%s </strong> &#8211;', __( 'Password Protect WordPress', 'password-protect-page' ) );
}
}

View File

@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: gaupoit
* Date: 7/25/19
* Time: 14:48
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class PPW_Password_Recovery extends PPW_Background_Task {
}

View File

@@ -0,0 +1,566 @@
<?php
/**
*
* Class PPW_Recaptcha
*/
class PPW_Recaptcha {
const TYPE_PARAM = 'ppwp_type';
const TYPE_VALUE = 'recaptcha';
const RECAPTCHA_V3_TYPE = 'recaptcha_v3';
const RECAPTCHA_V2_CHECKBOX_TYPE = 'recaptcha_v2_checkbox';
const RECAPTCHA_V2_INVISIBLE_TYPE = 'recaptcha_v2_invisible';
const SINGLE_PASSWORD = 'single';
const PCP_PASSWORD = 'pcp';
const SITEWIDE_PASSWORD = 'sitewide';
private $show_message = false;
/**
* @var PPW_Recaptcha
*/
protected static $instance;
/**
* @return PPW_Recaptcha
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Recaptcha error message.
*
* @return string
*/
public function get_error_message() {
$message = get_theme_mod( 'ppwp_form_error_recaptcha_message_text', PPW_Constants::DEFAULT_ERROR_RECAPTCHA_MESSAGE );
$message = wp_kses_post( $message );
return _x( $message, PPW_Constants::CONTEXT_PASSWORD_FORM, 'password-protect-page' );
}
/**
* Register hooks.
* @since 1.0.0
*/
public function register() {
add_filter( 'ppwp_customize_ppf', array( $this, 'maybe_customize_error_message' ), 25 );
add_filter( 'ppwp_ppf_redirect_url', array( $this, 'maybe_add_blocked_message' ), 20, 2 );
add_filter( 'ppwp_ppf_referrer_url', array( $this, 'maybe_remove_recaptcha_query' ), 10, 2 );
add_filter( 'ppwpea_recaptcha_v2_site_key', array( $this, 'get_ppwpea_recaptcha_v2_api_key' ), 10 );
add_filter( 'ppwpea_recaptcha_v2_secret', array( $this, 'get_ppwpea_recaptcha_v2_api_secret' ), 10 );
add_action( 'wp_footer', array( $this, 'load_js_in_footer' ), 10 );
add_action( 'ppw_custom_footer_form_entire_site', array( $this, 'maybe_load_sitewide_recaptcha_js' ), 10 );
add_action( 'ppw_sitewide_above_submit_button', array( $this, 'maybe_add_recaptcha_input_below_sitewide_form' ), 10 );
add_action( 'ppw_sitewide_custom_internal_css', array( $this, 'customize_sitewide_css' ), 10 );
add_filter( 'ppw_sitewide_valid_password', array( $this, 'validate_sitewide_password' ), 10 );
}
/**
* Remove blocked query if user enter right password.
*
* @param string $referrer_url Referrer URL.
*
* @return string
*/
public function maybe_remove_recaptcha_query( $referrer_url ) {
if ( ! $this->using_single_recaptcha() ) {
return $referrer_url;
}
if ( $this->has_recaptcha_parameter( $referrer_url ) ) {
$referrer_url = add_query_arg( self::TYPE_PARAM, false, $referrer_url );
}
return $referrer_url;
}
public function using_recaptcha() {
return ppw_core_get_setting_type_bool_by_option_name( PPW_Constants::USING_RECAPTCHA, PPW_Constants::EXTERNAL_OPTIONS );
}
/**
* Add blocked message if user turn on option.
*
* @param string $redirect_url Redirect URL.
* @param array $params Parameters.
*
* @return string
*/
public function maybe_add_blocked_message( $redirect_url, $params ) {
if ( ! $this->using_single_recaptcha() ) {
return $redirect_url;
}
if ( $params['is_valid'] ) {
return $redirect_url;
}
if ( ! $this->show_message ) {
// Remove blocked parameter if URL has it.
if ( $this->has_recaptcha_parameter( $redirect_url ) ) {
$redirect_url = add_query_arg( self::TYPE_PARAM, false, $redirect_url );
}
return $redirect_url;
}
$redirect_url = add_query_arg( self::TYPE_PARAM, self::TYPE_VALUE, $redirect_url );
return $redirect_url;
}
/**
* Has recaptcha parameter on URL.
*
* @param string $url $url URL.
* @param string $query_value Query value.
*
* @return bool
*/
private function has_recaptcha_parameter( $url = '', $query_value = self::TYPE_VALUE ) {
if ( empty( $url ) ) {
$query_params = ppw_core_get_query_param();
} else {
$query_params = ppw_core_get_param_in_url( $url );
}
if ( ! isset( $query_params[ self::TYPE_PARAM ] ) ) {
return false;
}
return $query_value === $query_params[ self::TYPE_PARAM ];
}
/**
* Customize error message.
*
* @param array $params Parameters.
*
* @return array
*/
public function maybe_customize_error_message( $params ) {
if ( ! $this->using_single_recaptcha() ) {
return $params;
}
if ( $this->has_recaptcha_parameter() ) {
$message = $this->get_error_message();
$params['error_msg'] = apply_filters( 'ppw_recaptcha_error_message', $message, $params );
}
return $params;
}
/**
* Validate recaptcha.
*
* @return bool
*/
public function is_valid_recaptcha() {
$_post = wp_unslash( $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- We no need to handle nonce verification here because already handle on parent function.
if ( ! isset( $_post['g-recaptcha-response'] ) ) {
$this->show_message = true;
return false;
}
$result = $this->verify_recaptcha( $_post['g-recaptcha-response'] );
if ( ! $result['success'] ) {
$this->show_message = true;
return false;
}
return true;
}
/**
* Get limit score.
*
* @return double
*/
public function get_limit_score() {
$score = ppw_core_get_settings_by_option_name( PPW_Constants::RECAPTCHA_SCORE, PPW_Constants::EXTERNAL_OPTIONS );
if ( is_null( $score ) ) {
return (double) 0.5;
}
return (double) $score;
}
/**
* Get setting api key of recaptcha with current type.
*
* @param string $type Recaptcha type.
* @param string $default Default value.
*
* @return string
*/
public function get_setting_api_key( $type = '', $default = '' ) {
if ( empty( $type ) ) {
$type = $this->get_recaptcha_type();
}
switch ( $type ) {
case self::RECAPTCHA_V3_TYPE:
return $this->get_recaptcha_v3_api_key();
case self::RECAPTCHA_V2_CHECKBOX_TYPE:
return $this->get_recaptcha_v2_api_key();
default:
return $default;
}
}
/**
* Get setting api secret of recaptcha with current type.
*
* @param string $type Recaptcha type.
* @param string $default Default value.
*
* @return string
*/
public function get_setting_api_secret( $type = '', $default = '' ) {
if ( empty( $type ) ) {
$type = $this->get_recaptcha_type();
}
switch ( $type ) {
case self::RECAPTCHA_V3_TYPE:
return $this->get_recaptcha_v3_api_secret();
case self::RECAPTCHA_V2_CHECKBOX_TYPE:
return $this->get_recaptcha_v2_api_secret();
default:
return $default;
}
}
/**
* Get recaptcha v3 API key.
*
* @return string
*/
public function get_recaptcha_v3_api_key() {
return ppw_core_get_setting_type_string_by_option_name( PPW_Constants::RECAPTCHA_API_KEY, PPW_Constants::EXTERNAL_OPTIONS );
}
/**
* Get recaptcha v3 API secret.
*
* @return string
*/
public function get_recaptcha_v3_api_secret() {
return ppw_core_get_setting_type_string_by_option_name( PPW_Constants::RECAPTCHA_API_SECRET, PPW_Constants::EXTERNAL_OPTIONS );
}
/**
* Get recaptcha v2 API key.
*
* @return string
*/
public function get_recaptcha_v2_api_key() {
return ppw_core_get_setting_type_string_by_option_name( PPW_Constants::RECAPTCHA_V2_CHECKBOX_API_KEY, PPW_Constants::EXTERNAL_OPTIONS );
}
/**
* Get recaptcha v2 API secret.
*
* @return string
*/
public function get_recaptcha_v2_api_secret() {
return ppw_core_get_setting_type_string_by_option_name( PPW_Constants::RECAPTCHA_V2_CHECKBOX_API_SECRET, PPW_Constants::EXTERNAL_OPTIONS );
}
/**
* Get recaptcha type.
*
* @return string
*/
public function get_recaptcha_type() {
$recaptcha_type = ppw_core_get_setting_type_string_by_option_name( PPW_Constants::RECAPTCHA_TYPE, PPW_Constants::EXTERNAL_OPTIONS );
return $recaptcha_type ? $recaptcha_type : self::RECAPTCHA_V3_TYPE;
}
/**
* Get password types selected.
*
* @return string[]
*/
public function get_password_types() {
$password_types = ppw_core_get_setting_type_array_by_option_name( PPW_Constants::RECAPTCHA_PASSWORD_TYPES, PPW_Constants::EXTERNAL_OPTIONS );
return $password_types ? $password_types : array( 'single' );
}
/**
* Using single recaptcha
*
* @return bool
*/
public function using_single_recaptcha() {
return $this->using_recaptcha() && in_array( self::SINGLE_PASSWORD, $this->get_password_types() );
}
/**
* Using sitewide recaptcha
*
* @return bool
*/
public function using_sitewide_recaptcha() {
return $this->using_recaptcha() && in_array( self::SITEWIDE_PASSWORD, $this->get_password_types() );
}
/**
* Using sitewide recaptcha
*
* @return bool
*/
public function using_pcp_recaptcha() {
return $this->using_recaptcha() && in_array( self::PCP_PASSWORD, $this->get_password_types() );
}
/**
* Load recaptcha v2 javascript.
*/
public function load_recaptcha_v2_js() {
ob_start();
?>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<?php
echo ob_get_clean(); // phpcs:ignore -- we cannot escape ob_start ob_get_clean(), there are no variable to escape in statement above
}
/**
* Load recaptcha v3 javascript.
*/
public function load_recaptcha_v3_js() {
$recaptcha_key = $this->get_recaptcha_v3_api_key();
ob_start();
?>
<script src="https://www.google.com/recaptcha/api.js?render=<?php echo esc_attr( $recaptcha_key ); ?>"></script>
<script>
grecaptcha.ready(function () {
grecaptcha.execute('<?php echo esc_attr( $recaptcha_key ); ?>', {action: 'enter_password'}).then(function (token) {
var recaptchaResponse = document.getElementById('ppwRecaptchaResponse');
recaptchaResponse.value = token;
});
});
</script>
<?php
echo ob_get_clean(); // phpcs:ignore -- we already escape the $recaptcha_key above
}
/**
* Verify google recaptcha V3.
*
* @param string $recaptcha_response Recaptcha response.
*
* @return array
*/
public function verify_recaptcha( $recaptcha_response ) {
$default = array(
'success' => false,
'message' => '',
);
if ( ! $recaptcha_response ) {
return $default;
}
$secret = $this->get_setting_api_secret();
if ( ! $secret ) {
return $default;
}
$response = wp_remote_post(
'https://www.google.com/recaptcha/api/siteverify',
array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => array(
'secret' => $secret,
'response' => $recaptcha_response,
),
'cookies' => array(),
)
);
if ( is_wp_error( $response ) ) {
return $default;
}
$body = wp_remote_retrieve_body( $response );
$body = json_decode( $body );
// Whether this request was a valid reCAPTCHA token for your site.
$success = isset( $body->success ) && $body->success;
$external = true;
if ( $this->get_recaptcha_type() === self::RECAPTCHA_V3_TYPE ) {
$limit_score = $this->get_limit_score();
// The score for this request (0.0 - 1.0) 1.0 is very likely a good interaction, 0.0 is very likely a bot.
$score = isset( $body->score ) ? (double) $body->score : 0;
$external = $score > $limit_score;
}
$default['success'] = $success && $external;
return $default;
}
/**
* Load PPWPEA api key.
*
* @param string $key Recaptcha API key.
*
* @return string
*/
public function get_ppwpea_recaptcha_v2_api_key( $key ) {
return $this->get_recaptcha_v2_api_key();
}
/**
* Load PPWPEA api secret.
*
* @param string $secret Recaptcha API secret.
*
* @return string
*/
public function get_ppwpea_recaptcha_v2_api_secret( $secret ) {
return $this->get_recaptcha_v2_api_secret();
}
public function maybe_load_sitewide_recaptcha_js() {
if ( ! $this->using_sitewide_recaptcha() ) {
return;
}
$this->add_recaptcha_to_head();
}
/**
* Add recaptcha to head.
*/
public function add_recaptcha_to_head() {
$recaptcha_type = $this->get_recaptcha_type();
switch ( $recaptcha_type ) {
case self::RECAPTCHA_V3_TYPE:
$this->load_recaptcha_v3_js();
break;
case self::RECAPTCHA_V2_CHECKBOX_TYPE:
case self::RECAPTCHA_V2_INVISIBLE_TYPE:
$this->load_recaptcha_v2_js();
break;
}
}
/**
* Add recaptcha input below sitewide form.
*/
public function maybe_add_recaptcha_input_below_sitewide_form() {
$recaptcha_input = $this->get_recaptcha_input();
if ( ! empty( $recaptcha_input ) ) {
echo $recaptcha_input;
}
}
/**
* Get recaptcha input.
*
* @return string
*/
public function get_recaptcha_input() {
switch ( $this->get_recaptcha_type() ) {
case PPW_Recaptcha::RECAPTCHA_V2_CHECKBOX_TYPE:
$site_key = $this->get_recaptcha_v2_api_key();
return '<div class="ppw-recaptcha g-recaptcha" data-sitekey="' . $site_key . '"></div>';
default:
return '<input type="hidden" name="g-recaptcha-response" id="ppwRecaptchaResponse" />';
}
}
/**
* Customize sitewide css.
*/
public function customize_sitewide_css() {
if ( ! $this->using_sitewide_recaptcha() ) {
return;
}
?>
.g-recaptcha {
transform:scale(0.9);
transform-origin:0 0;
}
<?php
}
/**
* Validate sitewide password form.
*
* @param $validated
*
* @return bool
*/
public function validate_sitewide_password( $validated ) {
if ( ! $validated ) {
return $validated;
}
if ( ! $this->using_sitewide_recaptcha() ) {
return $validated;
}
if ( ! $this->is_valid_recaptcha() ) {
add_filter( 'ppw_sitewide_error_message', array( $this, 'get_sitewide_error_message' ) );
return false;
}
return $validated;
}
/**
* Get sitewide error message.
*
* @return string
*/
public function get_sitewide_error_message() {
return __( 'Google reCAPTCHA verification failed, please try again later.', 'password-protect-page' );
}
/**
* Load JS in footer.
*/
public function load_js_in_footer() {
if ( ! $this->using_single_recaptcha() ) {
return;
}
$allowed = is_singular();
$allowed = apply_filters( 'ppw_recaptcha_allowed_to_load_script', $allowed );
if ( ! $allowed ) {
return;
}
$post_id = get_the_ID();
if ( ! $post_id || ! post_password_required( $post_id ) ) {
return;
}
$this->add_recaptcha_to_head();
}
}

View File

@@ -0,0 +1,803 @@
<?php
/**
* PPWP Shortcoe
*/
if ( ! class_exists( 'PPW_Shortcode' ) ) {
/**
*
* Class PPW_Shortcode
*/
class PPW_Shortcode {
/**
* Short code attributes.
*
* @var array
*/
private $attributes;
/**
* Supported roles.
*
* @var array
*/
private $supported_roles;
/**
* Supported post types.
*
* @var array
*/
private $supported_post_types;
/**
* The main class name which using to add the index.
*
* @var string
*/
private $main_class_name;
/**
* Register the short code ppwp_content_protector with WordPress
* and include the asserts for it.
*/
public function __construct() {
$this->attributes = apply_filters(
PPW_Constants::HOOK_SHORT_CODE_ATTRS,
array(
'passwords' => '',
'headline' => PPW_Constants::DEFAULT_SHORTCODE_HEADLINE,
'description' => PPW_Constants::DEFAULT_SHORTCODE_DESCRIPTION,
'id' => '',
'class' => '',
'placeholder' => '',
'button' => PPW_Constants::DEFAULT_SHORTCODE_BUTTON,
'whitelisted_roles' => '',
'group' => '',
'label' => PPW_Constants::DEFAULT_SHORTCODE_LABEL,
'error_msg' => PPW_Constants::DEFAULT_SHORTCODE_ERROR_MSG,
'loading' => PPW_Constants::DEFAULT_SHORTCODE_LOADING,
'on' => '',
'off' => '',
'acf_field' => '',
'show_password' => PPW_Constants::DEFAULT_SHORTCODE_SHOW_PASSWORD,
'show_password_text' => PPW_Constants::DEFAULT_SHORTCODE_SHOW_PASSWORD_TEXT,
'section' => 0,
'desc_above_btn' => PPW_Constants::DEFAULT_SHORTCODE_DESC_ABOVE_PWD_BTN,
'desc_below_form' => PPW_Constants::DEFAULT_SHORTCODE_DESC_BELOW_PWD_FORM,
)
);
// Defined by WordPress: https://wordpress.org/support/article/roles-and-capabilities/.
$this->supported_roles = apply_filters(
PPW_Constants::HOOK_SUPPORTED_WHITELIST_ROLES,
array(
'administrator',
'editor',
'author',
'contributor',
'subscriber',
)
);
$this->supported_post_types = apply_filters(
PPW_Constants::HOOK_SUPPORTED_POST_TYPES,
array(
'page',
'post',
)
);
add_shortcode( PPW_Constants::PPW_HOOK_SHORT_CODE_NAME, array( $this, 'render_shortcode' ) );
add_filter( 'ppw_content_shortcode_source', array( $this, 'render_block_content' ), 15 );
// Support page builder.
add_action( 'the_post', array( $this, 'maybe_remove_ppwp_shortcode' ), 10 );
add_action( 'the_post', array( $this, 'maybe_add_ppwp_shortcode' ), 99999 );
/**
* Need to keep the old Pro version work, because the sidewide shortcode is using global var ppwContentGlobal.
*/
if ( defined( 'PPW_PRO_VERSION' ) ) {
$pro_version = ppw_get_pro_version();
if ( version_compare( $pro_version, '1.2.2', '<' ) ) {
add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts' ) );
}
}
$this->main_class_name = PPW_Constants::DEFAULT_SHORTCODE_CLASS_NAME;
}
/**
* Maybe remove shortcode before WPBakery and WordPress do_shortcode in FrontEnd.
*/
public function maybe_remove_ppwp_shortcode() {
if ( ! ppw_free_has_support_shortcode_page_builder() ) {
return;
}
remove_shortcode( PPW_Constants::PPW_HOOK_SHORT_CODE_NAME );
}
/**
* Maybe add shortcode back.
*/
public function maybe_add_ppwp_shortcode() {
if ( ! ppw_free_has_support_shortcode_page_builder() ) {
return;
}
add_filter( 'the_content', function ( $content ) {
add_shortcode( PPW_Constants::PPW_HOOK_SHORT_CODE_NAME, array( $this, 'render_shortcode' ) );
/* translators: Opening curly double quote. */
$opening_quote = _x( '&#8220;', 'opening curly double quote' );
/* translators: Closing curly double quote. */
$closing_quote = _x( '&#8221;', 'closing curly double quote' );
/* translators: Apostrophe, for example in 'cause or can't. */
$apos = _x( '&#8217;', 'apostrophe' );
/* translators: Prime, for example in 9' (nine feet). */
$prime = _x( '&#8242;', 'prime' );
/* translators: Double prime, for example in 9" (nine inches). */
$double_prime = _x( '&#8243;', 'double prime' );
/* translators: Opening curly single quote. */
$opening_single_quote = _x( '&#8216;', 'opening curly single quote' );
/* translators: Closing curly single quote. */
$closing_single_quote = _x( '&#8217;', 'closing curly single quote' );
$matches = ppw_free_search_shortcode_content( $content );
if ( ! empty( $matches ) ) {
foreach ( $matches as $match ) {
// The shortcode argument list
$old_argument_shortcode = $match[3];
$argument_shortcode = $match[3];
$argument_shortcode = str_replace( $opening_quote, '"', $argument_shortcode );
$argument_shortcode = str_replace( $closing_quote, '"', $argument_shortcode );
$argument_shortcode = str_replace( $apos, '\'', $argument_shortcode );
$argument_shortcode = str_replace( $prime, '\'', $argument_shortcode );
$argument_shortcode = str_replace( $double_prime, '"', $argument_shortcode );
$argument_shortcode = str_replace( $opening_single_quote, '\'', $argument_shortcode );
$argument_shortcode = str_replace( $closing_single_quote, '\'', $argument_shortcode );
$content = str_replace( $old_argument_shortcode, $argument_shortcode, $content );
}
}
$content = do_shortcode( $content );
return $content;
}, 99999 );
}
/**
* Get short code instance
*
* @return PPW_Shortcode
*/
public static function get_instance() {
return new PPW_Shortcode();
}
/**
* Render password form or restricted content
* 0. Check current post type is in whitelist types
* 1. Check is valid attributes
* 2. Check whitelist roles
* 3. Check password is correct compare to Cookie
* 4. Show form
*
* @param array $attrs list of attributes including password.
* @param string $content the content inside short code.
*
* @return string
*/
public function render_shortcode( $attrs, $content = null ) {
global $page;
// In case the shortcode is outside in the loop, the page is 0.
$number = ! empty( $page ) ? $page : 1;
$this->attributes = apply_filters( 'ppw_pcp_attributes', $this->attributes, $number );
$attrs = shortcode_atts(
$this->attributes,
$attrs
);
$message = $this->is_valid_shortcode( $attrs, $content );
$message = apply_filters( 'ppw_pcp_valid_shortcode', $message, $attrs );
if ( true !== $message ) {
return $this->get_invalid_shortcode_message( $message, $attrs );
}
$content = sprintf(
'<div class="%s">%s</div>',
$this->get_main_class_name( $attrs ),
do_shortcode( $content )
);
$whitelisted_roles = apply_filters( PPW_Constants::HOOK_SHORT_CODE_WHITELISTED_ROLES, $attrs['whitelisted_roles'] );
if ( $this->is_whitelisted_role( $whitelisted_roles ) ) {
// Remember to wrap the content between the parent div. If you want to replace the shortcode content.
return apply_filters( PPW_Constants::HOOK_SHORTCODE_RENDER_CONTENT, $content, $attrs );
}
// Unlock content by datetime.
$unlocked = apply_filters( 'ppw_shortcode_unlock_content', $this->is_unlock_content_by_time( $attrs ), $attrs );
if ( $unlocked ) {
return apply_filters( PPW_Constants::HOOK_SHORTCODE_RENDER_CONTENT, $content, $attrs );
}
do_action( PPW_Constants::HOOK_SHORT_CODE_BEFORE_CHECK_PASSWORD, $content );
// Passwords attribute format: passwords="123 345 898942".
$passwords = apply_filters( PPW_Constants::HOOK_SHORTCODE_PASSWORDS, array_filter( explode( ' ', trim( $attrs['passwords'] ) ), 'strlen' ), $attrs );
foreach ( $passwords as $password ) {
// When passwords attribute having special characters eg: <script>alert('hello')</script>. WP will encode the HTML tag. Need to decode to compare the value in Cookie.
$hashed_password = wp_hash_password( wp_specialchars_decode( $password ) );
if ( $this->is_valid_password( $hashed_password ) ) {
// Remember to wrap the content between the parent div. If you want to replace the shortcode content.
return apply_filters( PPW_Constants::HOOK_SHORTCODE_RENDER_CONTENT, $content, $attrs );
}
}
do_action( PPW_Constants::HOOK_SHORT_CODE_AFTER_CHECK_PASSWORD, $content );
$this->add_scripts();
// Show custom text instead of password form.
$custom_form = apply_filters( PPW_Constants::HOOK_SHORTCODE_BEFORE_RENDER_PASSWORD_FORM, false, $attrs );
if ( false !== $custom_form ) {
return sprintf(
'<div class="%s">%s</div>',
$this->get_main_class_name( $attrs ),
$this->massage_attributes( $custom_form )
);
}
$password_form = $this->get_restricted_content_form( $attrs, $number );
return apply_filters( 'ppw_pcp_password_form', $password_form, $attrs );
}
/**
* Show content if user set on_date or off_date attribute.
* $on_date: Date to unlock content
* $off_date: Date to protect content.
*
* @param array $attrs Attributes.
*
* @return false True is unlock content else false.
*/
private function is_unlock_content_by_time( $attrs ) {
$on_date = false;
if ( '' !== $attrs['on'] ) {
$on_date = strtotime( $attrs['on'] );
}
$off_date = false;
if ( '' !== $attrs['off'] ) {
$off_date = strtotime( $attrs['off'] );
}
// Show password form if on_date and off_date are empty.
if ( ! $on_date && ! $off_date ) {
return false;
}
$now = current_time( 'timestamp' );
// Unlock content between on_date and off_date.
if ( $on_date && $off_date && $on_date <= $now && $off_date >= $now ) {
return apply_filters( 'ppw_shortcode_unlock_content_by_time', true, $attrs );
}
// Unlock content from on_date.
if ( $on_date && ! $off_date && $now >= $on_date ) {
return apply_filters( 'ppw_shortcode_unlock_content_by_time', true, $attrs );
}
return false;
}
/**
* Require javascript bundle file for shortcode.
*/
public function add_scripts() {
static $count_script = 0;
$count_script ++;
$assert_folder = '/public/js/dist';
$is_using_pcp_recaptcha = PPW_Recaptcha::get_instance()->using_pcp_recaptcha();
wp_enqueue_script(
'ppw-cookie',
PPW_DIR_URL . "$assert_folder/ppw-rc-form.bundle.js",
array( 'jquery' ),
PPW_VERSION,
false
);
wp_localize_script(
'ppw-cookie',
'ppwContentGlobal',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'restUrl' => get_rest_url(),
'ajax_nonce' => wp_create_nonce( 'ppw_pcp_nonce' ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'cookieExpiration' => $this->get_cookie_expiration(),
'supportedClassNames' => apply_filters(
'ppw_shortcode_supported_class_name',
array(
'defaultType' => PPW_Constants::DEFAULT_SHORTCODE_CLASS_NAME,
)
),
'label' => array(
'LOADING' => _x( 'Loading...', PPW_Constants::CONTEXT_PCP_PASSWORD_FORM, 'password-protect-page' ),
),
'isUsingPCPRecaptcha' => $is_using_pcp_recaptcha
)
);
// Avoid conflict with updating post on Gutenberg when updating post.
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
if ( $is_using_pcp_recaptcha && $count_script === 1 ) {
add_action( 'wp_footer', function () {
PPW_Recaptcha::get_instance()->add_recaptcha_to_head();
} );
}
}
}
/**
* Check whether short code is valid.
*
* @param array $attrs Shortcode attributes.
* @param string $content Short code content.
*
* @return string
*/
private function is_valid_shortcode( $attrs, $content ) {
if ( ! $this->is_supported_post_types( get_post_type() ) ) {
/* translators: %s: Short code name */
$message = sprintf( __( 'Our Free version [%s] shortcode doesn\'t support Custom Post Type', 'password-protect-page' ), PPW_Constants::PPW_HOOK_SHORT_CODE_NAME );
return apply_filters( PPW_Constants::HOOK_SHORTCODE_NOT_SUPPORT_TYPE_ERROR_MESSAGE, $message );
}
/* translators: %s: Short code name */
$message = sprintf( __( '[%s] Empty content, invalid attributes or values', 'password-protect-page' ), PPW_Constants::PPW_HOOK_SHORT_CODE_NAME );
$message = apply_filters( PPW_Constants::HOOK_SHORT_CODE_ERROR_MESSAGE, $message );
if ( $this->is_empty_content( $content, $attrs ) ) {
return $message;
}
if ( ! $this->is_valid_attributes( $attrs ) ) {
return $message;
}
return true;
}
/**
* @param $attrs
*/
private function is_valid_date( $attrs ) {
if ( '' !== $attrs['on'] && ! ppw_free_validate_date( $attrs['on'] ) ) {
return false;
}
if ( '' !== $attrs['off'] && ! ppw_free_validate_date( $attrs['off'] ) ) {
return false;
}
return true;
}
/**
* Check if the password is valid, comparing with cookie.
*
* @param string $password Password.
*
* @return bool
*/
private function is_valid_password( $password ) {
$is_valid = apply_filters( 'ppw_shortcode_is_valid_password_with_cookie', false, $password, $_COOKIE );
if ( $is_valid ) {
return apply_filters( 'ppw_shortcode_after_check_is_valid_password_with_cookie', $is_valid, $password, array() );
}
$cookie_name = 'ppw_rc-' . get_the_ID();
if ( ! isset( $_COOKIE[ $cookie_name ] ) ) {
return false;
}
global $wp_hasher;
$cookie_val = json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ) ); // phpcs:ignore -- Here do not need to sanitize $_COOKIE data, because we use it for comparision.
if ( ! is_array( $cookie_val ) ) {
return false;
}
foreach ( $cookie_val as $val ) {
if ( get_the_ID() !== (int) $val->post_id ) {
continue;
}
foreach ( $val->passwords as $cookie_pass ) {
if ( $wp_hasher->CheckPassword( $cookie_pass, $password ) ) {
return true;
}
}
}
return false;
}
/**
* Get restricted content form.
*
* @param array $attrs Short-code attributes.
* @param int $number Short-code attributes.
*
* @return array|mixed
*/
private function get_restricted_content_form( $attrs, $number ) {
$checkbox = '';
if ( wp_validate_boolean( $attrs['show_password'] ) ) {
$checkbox = '<label class="ppw-pcp-checkbox-label"><input class="ppw-pcp-checkbox" type="checkbox" /> ' . _x( $this->massage_attributes( $attrs['show_password_text'] ), PPW_Constants::CONTEXT_PCP_PASSWORD_FORM, 'password-protect-page' ) . '</label>';
}
$desc_above_btn = '';
if ( wp_validate_boolean( $attrs['desc_above_btn'] ) ) {
$desc_above_btn = '<span class="ppw-pcp-pf-desc-above-btn">'._x( $this->massage_attributes( $attrs['desc_above_btn'] ), PPW_Constants::CONTEXT_PCP_PASSWORD_FORM, 'password-protect-page' ).'</span>';
}
// Temp hide recaptcha on section protection.
if ( PPW_Recaptcha::get_instance()->using_pcp_recaptcha() && empty( $attrs['section'] ) ) {
$recaptcha_input = PPW_Recaptcha::get_instance()->get_recaptcha_input();
} else {
$recaptcha_input = '';
}
ob_start();
include apply_filters(
PPW_Constants::HOOK_SHORT_CODE_TEMPLATE,
PPW_DIR_PATH . 'includes/views/shortcode/view-ppw-restriced-content-form.php'
);
$form_template = ob_get_contents();
ob_end_clean();
$className = '' === $attrs['class'] ? $this->get_main_class_name( $attrs ) : $this->get_main_class_name( $attrs ) . ' ' . $attrs['class'];
// phpcs:disable
$form_params = array(
PPW_Constants::SHORT_CODE_FORM_HEADLINE => _x( $this->massage_attributes( $attrs['headline'] ), PPW_Constants::CONTEXT_PCP_PASSWORD_FORM, 'password-protect-page' ),
PPW_Constants::SHORT_CODE_FORM_INSTRUCT => _x( $this->massage_attributes( $attrs['description'] ), PPW_Constants::CONTEXT_PCP_PASSWORD_FORM, 'password-protect-page' ),
PPW_Constants::SHORT_CODE_FORM_PLACEHOLDER => _x( $this->massage_attributes( $attrs['placeholder'] ), PPW_Constants::CONTEXT_PCP_PASSWORD_FORM, 'password-protect-page' ),
PPW_Constants::SHORT_CODE_FORM_AUTH => get_the_ID(),
PPW_Constants::SHORT_CODE_BUTTON => _x( wp_kses_post( $attrs['button'] ), PPW_Constants::CONTEXT_PCP_PASSWORD_FORM, 'password-protect-page' ),
PPW_Constants::SHORT_CODE_FORM_CURRENT_URL => $this->get_the_permalink_without_cache( wp_rand( 0, 100 ) ),
PPW_Constants::SHORT_CODE_FORM_ID => esc_attr( '' === $attrs['id'] ? get_the_ID() . wp_rand( 0, 1000 ) : wp_kses_post( $attrs['id'] ) ),
PPW_Constants::SHORT_CODE_FORM_CLASS => esc_attr( $className ),
PPW_Constants::SHORT_CODE_PASSWORD_LABEL => _x( $this->massage_attributes( $attrs['label'] ), PPW_Constants::CONTEXT_PCP_PASSWORD_FORM, 'password-protect-page' ),
PPW_Constants::SHORTCODE_ABOVE_PASSWORD_INPUT => apply_filters( 'ppw_pcp_above_password_field', '', $attrs ),
PPW_Constants::SHORTCODE_BELOW_PASSWORD_INPUT => apply_filters( 'ppw_pcp_below_password_field', '', $attrs ),
PPW_Constants::SHORT_CODE_FORM_ERROR_MESSAGE => '',
PPW_Constants::SHORTCODE_DESC_ABOVE_BTN => $desc_above_btn,
PPW_Constants::SHORTCODE_DESC_BELOW_FORM => _x( $this->massage_attributes( $attrs['desc_below_form'] ), PPW_Constants::CONTEXT_PCP_PASSWORD_FORM, 'password-protect-page' ),
'[PPW_PAGE]' => $number,
'[PPW_CHECKBOX]' => $checkbox,
'[PPW_BUTTON_LOADING]' => esc_attr_x( $attrs['loading'], PPW_Constants::CONTEXT_PCP_PASSWORD_FORM, 'password-protect-page' ),
'[AREA]' => absint( $attrs['section'] ),
'[PPW_RECAPTCHA_INPUT]' => $this->massage_attributes( $recaptcha_input ),
);
// phpcs:enable
foreach ( $form_params as $key => $value ) {
$form_template = str_replace( $key, $value, $form_template );
}
return $form_template;
}
/**
* Massage attributes before showing the front end.
*
* @param string $val The value.
*
* @return mixed
*/
private function massage_attributes( $val ) {
return wp_kses_post( html_entity_decode( $val ) );
}
/**
* Get post permalink with random value
*
* @param int $rand_value Random value.
*
* @return string
*/
private function get_the_permalink_without_cache( $rand_value ) {
return get_the_permalink() . "?action=postpass&r=$rand_value";
}
/**
* Validate short_code attributes
*
* @param array $attrs Attributes.
*
* @return bool
*/
private function is_valid_attributes( $attrs ) {
$required_attrs = apply_filters(
PPW_Constants::HOOK_SHORTCODE_ATTRIBUTES_VALIDATION,
array(
array(
'key' => 'passwords',
'length' => 100,
'delimiter' => ' ',
),
),
$attrs
);
foreach ( $required_attrs as $attr ) {
$val = trim( $attrs[ $attr['key'] ] );
if ( '' === $val ) {
return false;
}
$items = explode( $attr['delimiter'], $val );
foreach ( $items as $item ) {
if ( $attr['length'] < strlen( $item ) ) {
return false;
}
}
}
if ( ! $this->is_valid_date( $attrs ) ) {
return false;
}
return true;
}
/**
* Invalid shortcode message.
*
* @param string $message Error message.
* @param array $attrs Attributes shortcode.
*
* @return string
*/
private function get_invalid_shortcode_message( $message, $attrs ) {
$color = esc_attr( PPW_Constants::PPW_ERROR_MESSAGE_COLOR );
return sprintf(
'<span class="%s" style="color:%s;display: block">%s</span>',
$this->get_main_class_name( $attrs ),
$color,
$message
);
}
/**
* Is whitelisted roles
*
* @param string $whitelisted_roles Attribute whitelist roles from shortcode.
*
* @return bool
*/
private function is_whitelisted_role( $whitelisted_roles ) {
$roles = explode( ',', trim( $whitelisted_roles ) );
foreach ( $roles as $role ) {
$role = trim( $role );
if ( in_array( $role, $this->supported_roles, true ) && current_user_can( $role ) ) {
return true;
}
}
return false;
}
/**
* Is current post type supported.
*
* @param string $type Current post type.
*
* @return bool
*/
private function is_supported_post_types( $type ) {
$is_bypass = apply_filters( PPW_Constants::HOOK_SHORTCODE_ALLOW_BYPASS_VALID_POST_TYPE, defined( 'PPW_PRO_VERSION' ) );
if ( $is_bypass ) {
return true;
}
return in_array( $type, $this->supported_post_types, true );
}
/**
* Get cookie expiration
*
* @return int
*/
public function get_cookie_expiration() {
$default = apply_filters( PPW_Constants::HOOK_COOKIE_EXPIRED, time() + 7 * DAY_IN_SECONDS );
$setting_expiration = ppw_core_get_setting_type_string( PPW_Constants::COOKIE_EXPIRED );
if ( empty( $setting_expiration ) ) {
return $default;
}
$tmp = explode( ' ', $setting_expiration );
if ( count( $tmp ) < 2 ) {
return $default;
}
$val = $tmp[0];
$unit = ppw_core_get_unit_time( $setting_expiration );
if ( 0 === $unit ) {
return $default;
}
return apply_filters( PPW_Constants::HOOK_COOKIE_EXPIRED, time() + (int) $val * $unit );
}
/**
* Check whether the content is empty.
*
* @param string $content The content.
* @param array $attrs The shortcode attributes.
*
* @return bool
*/
private function is_empty_content( $content, $attrs ) {
$is_empty = '' === $content;
return apply_filters( 'ppwp_shortcode_is_empty_content', $is_empty, $content, $attrs );
}
/**
* Get attributes from shortcode
*
* @param array $parsed_atts Shortcode attributes in array type.
*
* @return array
*/
public function get_attributes( $parsed_atts ) {
// Default values for attributes.
$default_values = array(
'passwords' => array(),
'cookie' => $this->get_cookie_expiration(),
);
// Shortcode_parse_atts return array or empty which we only use array.
if ( ! is_array( $parsed_atts ) ) {
return $default_values;
}
// Get passwords attribute.
if ( isset( $parsed_atts['passwords'] ) ) {
$default_values['passwords'] = $this->get_passwords_attribute( $parsed_atts );
}
// Get cookie attribute.
if ( isset( $parsed_atts['cookie'] ) && intval( $parsed_atts['cookie'] ) > PPW_Constants::MIN_COOKIE_EXPIRED ) {
$default_values['cookie'] = $this->get_expired_time_cookie_attribute( $parsed_atts );
}
return $default_values;
}
/**
* Convert string password to array
* Example:
* Input: 'a b c'
* Output: ['a','b','c']
*
* @param array $parsed_atts Attributes parsed.
*
* @return array
*/
private function get_passwords_attribute( $parsed_atts ) {
return array_map(
function ( $p ) {
return wp_specialchars_decode( $p );
},
explode( ' ', $parsed_atts['passwords'] )
);
}
/**
* Convert day to timestamp of cookie.
*
* @param array $parsed_atts Attributes parsed.
*
* @return int
*/
private function get_expired_time_cookie_attribute( $parsed_atts ) {
$hours = absint( $parsed_atts['cookie'] );
$hours = $hours > PPW_Constants::MAX_COOKIE_EXPIRED ? PPW_Constants::MAX_COOKIE_EXPIRED : $hours;
return time() + $hours * HOUR_IN_SECONDS;
}
/**
* Get main class name.
*
* @param array $attrs Attributes shortcode.
*
* @return string
*/
public function get_main_class_name( $attrs ) {
$post_fix = empty( $attrs['type'] )
? ''
: '-' . $attrs['type'];
return $this->main_class_name . $post_fix;
}
/**
* Get content for post by page number. Case use break page in content.
*
* @param object $post The post content.
* @param int $page The page number.
*
* @return bool|string
*/
public function get_content_by_page_number( $post, $page ) {
if ( function_exists( 'generate_postdata' ) ) {
$postdata = generate_postdata( $post );
$pages = $postdata['pages'];
} else {
$postdata = setup_postdata( $post );
global $pages;
}
if ( false === $postdata ) {
return false;
}
return $pages[ $page - 1 ];
}
/**
* Handle block content on Gutenberg
*
* @param string $content Post content.
*
* @return string Content after rendered.
*/
public function render_block_content( $content ) {
if ( ! function_exists( 'parse_blocks' ) ||
! function_exists( 'has_blocks' ) ||
! function_exists( 'render_block' )
) {
return $content;
}
if ( has_blocks( $content ) ) {
$content_markup = '';
$blocks = parse_blocks( $content );
foreach ( $blocks as $block ) {
$content_markup .= render_block( $block );
}
return $content_markup;
}
return $content;
}
}
}

View File

@@ -0,0 +1,49 @@
<?php
if ( ! class_exists( 'PPW_Password_Subscribe' ) ) {
class PPW_Password_Subscribe {
/**
* Handle subscriber request(Call api to save data for subscriber)
*
* @param string $email email user request.
*
* @return array
*/
public function handle_subscribe_request( $email ) {
$ppw_config = include PPW_DIR_PATH . 'config.php';
$data = array(
'email' => $email,
'plugin' => 'ppwp',
);
$args = array(
'body' => json_encode( $data ),
'timeout' => '100',
'redirection' => '5',
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(
'Content-Type' => 'application/json',
),
);
$response = wp_remote_post(
$ppw_config->subscribe_api,
$args
);
$status_code = absint( wp_remote_retrieve_response_code( $response ) );
if ( is_wp_error( $response ) ) {
return array(
'error_message' => $response->get_error_message(),
);
} else if ( $status_code >= 400 ) {
return array(
'error_message' => __('Invalid email address', 'password-protect-page'),
);
} else {
update_user_meta( get_current_user_id(), PPW_Constants::USER_SUBSCRIBE, true );
return array(
'data' => json_decode( wp_remote_retrieve_body( $response ) ),
);
}
}
}
}