66 lines
1.5 KiB
PHP
66 lines
1.5 KiB
PHP
<?php
|
|
|
|
/*
|
|
* it means someone outside wp is accessing the file, in this case kill it.
|
|
*/
|
|
if (!defined('ABSPATH')) {
|
|
die('You can not access this file!');
|
|
}
|
|
|
|
|
|
|
|
|
|
function restrict_author_page_CIPF() {
|
|
PLGNTLS_class::debug_infos();
|
|
$role_fipf = PLGNTLS_class::ROLE_FIPF;
|
|
$role_admin = PLGNTLS_class::ROLE_ADMIN;
|
|
|
|
$slug = get_post_field( 'post_name', get_post() );
|
|
error_log("slug");
|
|
error_log($slug);
|
|
// error_log("request");
|
|
// error_log(json_encode($_REQUEST));
|
|
// error_log("server");
|
|
// error_log(json_encode($_SERVER));
|
|
|
|
if (!is_author())
|
|
return;
|
|
PLGNTLS_class::debug_infos();
|
|
|
|
$current_user = wp_get_current_user();
|
|
|
|
/*
|
|
* check multiple user roles
|
|
* https://developer.wordpress.org/reference/functions/current_user_can/#div-comment-4083
|
|
* if user->role is found in array of allowed role, no redirection needed
|
|
*
|
|
*/
|
|
$allowed_roles = array($role_admin, $role_fipf);
|
|
if (array_intersect($allowed_roles, $current_user->roles))
|
|
return;
|
|
PLGNTLS_class::debug_infos();
|
|
|
|
/*
|
|
* get_queried_object_id() would work too
|
|
* here get_the_author_meta works and is more explicit
|
|
*
|
|
$author_id = get_queried_object_id();
|
|
*/
|
|
$author_id = get_the_author_meta( 'ID' );
|
|
|
|
$current_user_id = get_current_user_id();
|
|
|
|
if ($current_user_id != $author_id) {
|
|
// Set up nocache headers before redirecting : https://developer.wordpress.org/reference/functions/wp_safe_redirect/#user-contributed-notes
|
|
nocache_headers();
|
|
wp_redirect(home_url(), 301);
|
|
exit;
|
|
}
|
|
PLGNTLS_class::debug_infos();
|
|
}
|
|
add_action('template_redirect', 'restrict_author_page_CIPF', 10);
|
|
|
|
|
|
|
|
?>
|