added redirection without need of a page, use it for paypal success or failure message already

This commit is contained in:
asus
2024-03-14 10:29:02 +01:00
parent 2f4a5bb9ef
commit ecc072e25b
9 changed files with 124 additions and 131 deletions

View File

@@ -113,11 +113,13 @@ CIPF_admin_activation_prof
/*
* ABORTED
* tried to receive the form in ajax from front with wp REST API
* but then I'm not sure how to handle the front
* but I cannot verify if the user is logged in
* so it does not work
*
*/
function admin_validate_prof_form_CIPF($request) {
PLGNTLS_class::debug_infos();
$slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION;
error_log("-----");
error_log("inside admin_validate_prof_form_CIPF");
error_log("request");
@@ -126,19 +128,23 @@ function admin_validate_prof_form_CIPF($request) {
error_log(json_encode($_POST));
// _POST : {"field_title":["\u00c9tat du compte"],"field_name":["compte-actif"],"field_id":["cipf_admin_activate_prof"],"form_key":"40781-1","unique_id":"91eed9aa-2b92-4da7-a0b4-e94f24515223","form_type":"custom","divi-form-submit":"yes","form_id":"","form_type_confirm":""}
// maybe do a redirection here ?
// with wp_safe_redirect(home_url('/slug')); ?
// return new WP_REST_Response( array( 'message' => 'Form submitted successfully' ), 200 );
// Set up nocache headers before redirecting : https://developer.wordpress.org/reference/functions/wp_safe_redirect/#user-contributed-notes
nocache_headers();
$redirect_url = home_url() . '/' . $slug_page_redirection;
wp_redirect($redirect_url, 301);
exit;
}
function endpoint_form_admin_activate_prof_CIPF() {
PLGNTLS_class::debug_infos();
$base_rest_route = PLGNTLS_class::BASE_REST_ROUTE;
$base_rest_route = PLGNTLS_class::URL_BASE_REST_ROUTE;
register_rest_route($base_rest_route, '/admin_prof_activate_form', array(
'methods' => 'POST',
'callback' => 'admin_validate_prof_form_CIPF',
));
};
add_action('rest_api_init', 'endpoint_form_admin_activate_prof_CIPF');
*/

View File

@@ -41,6 +41,8 @@ function restrict_author_page_CIPF() {
$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;
}

View File

@@ -13,7 +13,7 @@ if (!defined('ABSPATH')) {
// diff routes and endpoints : https://stackoverflow.com/q/56075017/9497573
function routes_endpoints_CIPF() {
PLGNTLS_class::debug_infos();
$base_rest_route = PLGNTLS_class::BASE_REST_ROUTE;
$base_rest_route = PLGNTLS_class::URL_BASE_REST_ROUTE;
register_rest_route($base_rest_route, '/orders', array(
'methods' => 'POST',
'callback' => 'handle_orders_request_CIPF',

View File

@@ -46,6 +46,8 @@ function handle_prof_is_activ_CIPF($author_id) {
*/
$redirection_prof_inactiv = home_url() . '/' . $slug_wait_activation;
// Set up nocache headers before redirecting : https://developer.wordpress.org/reference/functions/wp_safe_redirect/#user-contributed-notes
nocache_headers();
wp_redirect($redirection_prof_inactiv, 301);
exit;
}

View File

@@ -14,25 +14,35 @@ if (!defined('ABSPATH')) {
* if prof -> author page
* if partenaire -> post page || home page
*/
function redirection_page_CIPF(){
function redirection_profil_page_CIPF(){
PLGNTLS_class::debug_infos();
$slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION;
$role_prof = PLGNTLS_class::ROLE_PROF;
$role_partner = PLGNTLS_class::ROLE_PARTNER;
if (!is_page($slug_page_redirection))
return;
$base_url = home_url();
$current_user_id = get_current_user_id();
// Set up nocache headers before redirecting : https://developer.wordpress.org/reference/functions/wp_safe_redirect/#user-contributed-notes
nocache_headers();
error_log("--- inside redirection profil page");
if (!is_user_logged_in()) {
foreach ($_COOKIE as $name => $value) {
error_log(json_encode("$name: $value"));
}
error_log("is not logged in");
wp_redirect($base_url, 301);
exit;
}
if (current_user_can($role_prof)) {
error_log("is prof");
$user_page = get_author_posts_url($current_user_id);
wp_redirect($user_page, 301);
exit;
}
else if (current_user_can($role_partner)) {
error_log("is partenaire");
$args = array(
'post_type' => 'post',
'author' => $current_user_id,
@@ -47,11 +57,81 @@ function redirection_page_CIPF(){
$redirect_url = get_permalink($query->ID);
}
wp_redirect($redirect_url, 301);
exit;
}
else {
error_log("is else");
wp_redirect($base_url, 301);
exit;
}
exit;
}
/*
* redirects when someone reaches the page with slug defined in SLUG_PAGE_REDIRECTION
*
*/
function redirection_page_CIPF(){
PLGNTLS_class::debug_infos();
$slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION;
$role_prof = PLGNTLS_class::ROLE_PROF;
$role_partner = PLGNTLS_class::ROLE_PARTNER;
if (!is_page($slug_page_redirection))
return;
redirection_profil_page_CIPF();
}
add_action('template_redirect', 'redirection_page_CIPF');
/*
* DOES NOT WORK so far
* because to use is_user_logged_in() in redirection_profil_page_CIPF
* the request should include the X-WP-Nonce header, but it does not
* redirects when someone enter the url or redirection
* /wp-json/cipf_plugin/api/v1/redirection_cipf
*
function endpoint_for_redirection_CIPF() {
PLGNTLS_class::debug_infos();
$slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION;
$base_rest_route = PLGNTLS_class::URL_BASE_REST_ROUTE;
register_rest_route($base_rest_route, '/' . $slug_page_redirection, array(
'methods' => 'GET',
'callback' => 'redirection_profil_page_CIPF',
));
};
add_action('rest_api_init', 'endpoint_for_redirection_CIPF');
*/
/*
*
*
*/
function custom_link_intercept() {
PLGNTLS_class::debug_infos();
$query_page_redirection = PLGNTLS_class::QUERY_REDIRECTION_PROFIL;
// Check if the request contains ?QUERY_REDIRECTION_PROFIL
if(isset($_GET[$query_page_redirection])) {
redirection_profil_page_CIPF();
}
}
add_action('init', 'custom_link_intercept');
?>

View File

@@ -30,14 +30,18 @@ function renew_page_restrictions_CIPF(){
$role_fipf = PLGNTLS_class::ROLE_FIPF;
$role_admin = PLGNTLS_class::ROLE_ADMIN;
$base_url = home_url();
if (!is_page($slug_renew_card))
return;
if (!is_user_logged_in()) {
// Set up nocache headers before redirecting : https://developer.wordpress.org/reference/functions/wp_safe_redirect/#user-contributed-notes
nocache_headers();
wp_redirect($base_url, 301);
exit;
}
$base_url = home_url();
$current_user_id = get_current_user_id();
$current_user = wp_get_current_user();
$acf_id = 'user_'.$current_user_id;
@@ -59,6 +63,8 @@ function renew_page_restrictions_CIPF(){
if (current_user_can($role_prof)) {
$can_renew = get_field($acf_prof_can_renew['_name'], $acf_id);
if ($can_renew === false) {
// Set up nocache headers before redirecting : https://developer.wordpress.org/reference/functions/wp_safe_redirect/#user-contributed-notes
nocache_headers();
$redirect_url = home_url() . '/' . $slug_page_redirection;
wp_redirect($redirect_url, 301);
exit;