- reorganized files
- fixed error in partner edti/creation page
This commit is contained in:
81
plugins/cipf_plugin/php/_utils_checks_roles.php
Normal file
81
plugins/cipf_plugin/php/_utils_checks_roles.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?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 has_partner_post() {
|
||||
Plgntls::debug_infos();
|
||||
$current_user_id = get_current_user_id();
|
||||
|
||||
$args = array(
|
||||
'post_type' => 'post',
|
||||
'author' => $current_user_id,
|
||||
'posts_per_page' => 1,
|
||||
'post_status' => array('publish', 'draft'),
|
||||
);
|
||||
$post = get_posts($args);
|
||||
if (empty($post)) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
return reset($post);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* to use is_user_logged_in at early init hook
|
||||
* 1162 : wordpress_docker/volumes/wp_volume/wp-includes/pluggable.php
|
||||
*
|
||||
*/
|
||||
function is_user_logged_in_CIPF() {
|
||||
Plgntls::debug_infos(2);
|
||||
if (function_exists('is_user_logged_in')) {
|
||||
return is_user_logged_in();
|
||||
}
|
||||
else {
|
||||
$user = wp_get_current_user();
|
||||
return $user->exists();
|
||||
}
|
||||
Plgntls::debug_infos();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* to use current_user_can at early init hook
|
||||
* 877 : wordpress_docker/volumes/wp_volume/wp-includes/capabilities.php
|
||||
*
|
||||
*/
|
||||
function current_user_can_CIPF($capability) {
|
||||
Plgntls::debug_infos(2);
|
||||
if (function_exists('current_user_can')) {
|
||||
return current_user_can($capability);
|
||||
}
|
||||
else {
|
||||
$user = wp_get_current_user();
|
||||
return user_can($user, $capability);
|
||||
}
|
||||
Plgntls::debug_infos();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
@@ -18,7 +18,8 @@ function redirect_home_CIPF(){
|
||||
Plgntls::debug_infos();
|
||||
|
||||
// Set up nocache headers before redirecting : https://developer.wordpress.org/reference/functions/wp_safe_redirect/#user-contributed-notes
|
||||
nocache_headers();
|
||||
// i dont really know what it does, but it creates a warning sometimes : 'PHP Warning: Cannot modify - header information headers already sent in /var/www/html/wp-includes/pluggable.php'
|
||||
//nocache_headers();
|
||||
|
||||
wp_redirect(home_url(), 301);
|
||||
exit;
|
||||
@@ -31,7 +32,7 @@ function redirect_home_CIPF(){
|
||||
* redirects new prof to commande
|
||||
*
|
||||
*/
|
||||
function redirect_command_CIPF(){
|
||||
function redirect_prof_command_CIPF(){
|
||||
Plgntls::debug_infos();
|
||||
$slug_command_card = Cipf::SLUG_COMMAND_CARD;
|
||||
|
||||
@@ -82,25 +83,12 @@ function redirection_profil_CIPF(){
|
||||
*
|
||||
*/
|
||||
if (current_user_can($role_partner)) {
|
||||
$args = array(
|
||||
'post_type' => 'post',
|
||||
'author' => $current_user_id,
|
||||
'posts_per_page' => 1,
|
||||
'post_status' => array('publish', 'draft'),
|
||||
);
|
||||
$posts = get_posts($args);
|
||||
|
||||
/*
|
||||
* if post exists, redirects to it
|
||||
* otherwise redirects to post creation
|
||||
*
|
||||
*/
|
||||
if (empty($posts)) {
|
||||
$partner_post = has_partner_post();
|
||||
if (false === $partner_post) {
|
||||
$redirect_url = $partner_page_creation;
|
||||
}
|
||||
else {
|
||||
$query = reset($posts);
|
||||
$redirect_url = get_permalink($query->ID);
|
||||
$redirect_url = get_permalink($partner_post->ID);
|
||||
}
|
||||
wp_redirect($redirect_url, 301);
|
||||
exit;
|
||||
@@ -150,6 +138,31 @@ add_action('template_redirect', 'redirection_page_CIPF');
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* checks if partner current user is partner
|
||||
* works as soon as 'init' hook
|
||||
*
|
||||
* first hook to use is...() is parse_query
|
||||
* -> https://developer.wordpress.org/apis/hooks/action-reference/
|
||||
* - after 'init', before 'wp'
|
||||
* but 'init' already has set user, so we can recreate the functions
|
||||
*
|
||||
*/
|
||||
function redirects_home_if_not_partner() {
|
||||
Plgntls::debug_infos(2);
|
||||
$role_partner = Cipf::ROLE_PARTNER;
|
||||
|
||||
if (!is_user_logged_in_CIPF()) {
|
||||
redirect_home_CIPF();
|
||||
}
|
||||
if (!current_user_can_CIPF($role_partner)) {
|
||||
redirect_home_CIPF();
|
||||
}
|
||||
Plgntls::debug_infos();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
@@ -1,44 +0,0 @@
|
||||
<?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!');
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* menu deconnexion
|
||||
* 1. il faut creer un menu personalisé dans Apparence > Menus
|
||||
* 2. dans le code ci-dessous, changer la valeur de $menu_title pour correspondre au titre du menu
|
||||
* 3. et si besoin changer la valeur de $menu_redirect pour choisir la page de redirection :
|
||||
* - si laissée vide, la redirection se fera sur la page de connexion de wordpress
|
||||
* - avec $current_url la redirection se fera sur la page actuelle
|
||||
* - avec $base_url on redirige vers la page d'accueil du site (l'url sans chemin supplementaire)
|
||||
* cette variable $base_url peut etre utilisee pour construire une autre url :
|
||||
* - $menu_redirect = $base_url -> https://le_site_actuel.com/
|
||||
* - $menu_redirect = $base_url . 'contact' -> https://le_site_actuel.com/contact
|
||||
* - $menu_redirect = $current_url -> https://le_site_actuel.com/la_meme_page
|
||||
* - $menu_redirect = 'www.un_autre_site.net/contact' -> https://www.un_autre_site.net/contact
|
||||
*/
|
||||
function change_menu_logout($items){
|
||||
Plgntls::debug_infos();
|
||||
$menu_title = 'special logout';
|
||||
|
||||
// quelques urls utiles :
|
||||
$base_url = home_url();
|
||||
$current_url = home_url( $_SERVER['REQUEST_URI'] );
|
||||
|
||||
$menu_redirect = '';
|
||||
foreach($items as $item){
|
||||
if( $item->title === $menu_title){
|
||||
$item->url = wp_nonce_url( wp_logout_url( $menu_redirect ), 'log-out' );
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
add_filter('wp_nav_menu_objects', 'change_menu_logout');
|
||||
|
||||
|
||||
?>
|
||||
@@ -9,6 +9,44 @@ if (!defined('ABSPATH')) {
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* the earliest hook to use is_..() is parse_query
|
||||
* -> https://developer.wordpress.org/apis/hooks/action-reference/
|
||||
*
|
||||
*/
|
||||
function is_partner_form_creation_page_CIPF() {
|
||||
Plgntls::debug_infos();
|
||||
$slug_partner_create_page = Cipf::SLUG_PARTNER_CREATE_PAGE;
|
||||
|
||||
/*
|
||||
* only for the partner form creation page
|
||||
*
|
||||
*/
|
||||
if (!is_page($slug_partner_create_page)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* action to be done at the init state of the page
|
||||
* cannot check if user is logged in or partner at this step
|
||||
*
|
||||
*/
|
||||
function partner_form_creation_page_init_CIPF() {
|
||||
Plgntls::debug_infos(2);
|
||||
|
||||
redirects_home_if_not_partner();
|
||||
|
||||
// https://developer.wordpress.org/reference/functions/get_query_var/#more-information
|
||||
global $wp;
|
||||
$wp->add_query_var('pid');
|
||||
}
|
||||
add_action('init','partner_form_creation_page_init_CIPF');
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -18,33 +56,37 @@ if (!defined('ABSPATH')) {
|
||||
*/
|
||||
function partner_form_creation_page_CIPF() {
|
||||
Plgntls::debug_infos(2);
|
||||
$role_partner = Cipf::ROLE_PARTNER;
|
||||
$slug_partner_create_page = Cipf::SLUG_PARTNER_CREATE_PAGE;
|
||||
|
||||
/*
|
||||
* only for the partner form creation page
|
||||
*
|
||||
*/
|
||||
if (!is_page($slug_partner_create_page)) {
|
||||
redirects_home_if_not_partner();
|
||||
if (!is_partner_form_creation_page_CIPF()) {
|
||||
return;
|
||||
}
|
||||
Plgntls::debug_infos();
|
||||
|
||||
/*
|
||||
* redirect anyone that is not a partner
|
||||
* -> if partner don't have page yet && this is not edit page -> let him access it
|
||||
* if partner dont' have page yet && this is edit page -> redirect him
|
||||
* if partner have page && this is not edit page -> redirect him
|
||||
* -> if partner have page && this is edit page && this edit its page -> let him access it
|
||||
* if partner have page && this is edit page && this dont edit its page -> redirect him
|
||||
*
|
||||
* to check for pid, add 'pid' to query vars in 'init' hook
|
||||
* -> https://developer.wordpress.org/reference/functions/get_query_var/#more-information
|
||||
*
|
||||
*/
|
||||
if (!is_user_logged_in()) {
|
||||
redirect_home_CIPF();
|
||||
$is_edit_id = get_query_var('pid', false);
|
||||
$partner_post = has_partner_post();
|
||||
if (false === $partner_post) {
|
||||
if (false === $is_edit_id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!current_user_can($role_partner)) {
|
||||
redirect_home_CIPF();
|
||||
else {
|
||||
$post_id = $partner_post->ID;
|
||||
if ($is_edit_id == $post_id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* if a partner already has a page, redirect it
|
||||
*
|
||||
*/
|
||||
redirection_profil_CIPF();
|
||||
}
|
||||
add_action('template_redirect', 'partner_form_creation_page_CIPF');
|
||||
@@ -62,5 +104,4 @@ add_action('template_redirect', 'partner_form_creation_page_CIPF');
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
|
||||
@@ -17,7 +17,6 @@ function is_own_partner() {
|
||||
Plgntls::debug_infos(2);
|
||||
$role_partner = Cipf::ROLE_PARTNER;
|
||||
|
||||
error_log("in toggle A");
|
||||
/*
|
||||
* is_single() will only return true for published post
|
||||
* so instead checks for both is_singular and post_type
|
||||
@@ -34,27 +33,22 @@ error_log("post type: " . get_post_type());
|
||||
}
|
||||
*/
|
||||
|
||||
error_log("in toggle B");
|
||||
if (!is_user_logged_in()) {
|
||||
return false;
|
||||
}
|
||||
error_log("in toggle C");
|
||||
if (!current_user_can($role_partner)) {
|
||||
return false;
|
||||
}
|
||||
error_log("in toggle D");
|
||||
global $post;
|
||||
if (is_null($post)) {
|
||||
return false;
|
||||
}
|
||||
error_log("in toggle E");
|
||||
Plgntls::debug_infos();
|
||||
$current_post_author = (int)($post->post_author);
|
||||
$current_user_id = (int)get_current_user_id();
|
||||
if ($current_user_id !== $current_post_author) {
|
||||
return false;
|
||||
}
|
||||
error_log("in toggle F");
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -115,20 +109,16 @@ function toggle_partner_page_CIPF() {
|
||||
* - has query action
|
||||
*
|
||||
*/
|
||||
error_log("in toggle 1");
|
||||
if (!is_own_partner()) {
|
||||
return;
|
||||
}
|
||||
error_log("in toggle 2");
|
||||
Plgntls::debug_infos();
|
||||
if (!isset($_GET['action'])) {
|
||||
return;
|
||||
}
|
||||
error_log("in toggle 3");
|
||||
if ($_GET['action'] !== $toggle_partner_page) {
|
||||
return;
|
||||
}
|
||||
error_log("in toggle 4");
|
||||
|
||||
/*
|
||||
* get the post id and object
|
||||
@@ -139,7 +129,6 @@ error_log("in toggle 4");
|
||||
if (is_null($current_post)) {
|
||||
return;
|
||||
}
|
||||
error_log("in toggle 5");
|
||||
|
||||
/*
|
||||
* toogle the status
|
||||
|
||||
@@ -121,7 +121,7 @@ function prof_profil_redirects_CIPF() {
|
||||
*
|
||||
*/
|
||||
if (is_account_new_CIPF()) {
|
||||
redirect_command_CIPF();
|
||||
redirect_prof_command_CIPF();
|
||||
}
|
||||
}
|
||||
add_action('template_redirect', 'prof_profil_redirects_CIPF', 11);
|
||||
|
||||
Reference in New Issue
Block a user