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

108
2
View File

@@ -1,108 +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!');
}
/*
* custom action in php on custom form submission
*
*/
function admin_validate_prof_CIPF($form_id, $post_array, $form_type) {
PLGNTLS_class::debug_infos();
$admin_validate_prof_field = PLGNTLS_class::ADMIN_VALIDATE_PROF_FIELD;
if ($form_type !== 'custom')
return;
error_log("---");
// error_log("in test_form_CIPF");
// error_log("form_id");
// error_log(json_encode($form_id));
// error_log("post_array");
// error_log(json_encode($post_array));
// error_log("form_type");
// error_log(json_encode($form_type));
$field_id = $post_array['field_id'];
$contains_id = in_array($admin_validate_prof_field, $field_id);
if ($contains_id === false)
return;
/* custom action */
}
add_action('df_before_process', 'admin_validate_prof_CIPF', 10, 3);
/*
before :
in test_form_CIPF
form_id
""
post_array
{"field_title":["\u00c9tat du compte"],"field_name":["de_fb_compte-actif"],"field_id":["de_fb_field_1"],"form_type_confirm":""}
form_type
"custom"
---
after :
in test_form_CIPF
form_id
""
post_array
{"field_title":["\u00c9tat du compte"],"field_name":["de_fb_compte-actif"],"field_id":["de_fb_field_1"],"form_type_confirm":""}
form_type
"custom"
---
exploded view :
{
"field_title":["\u00c9tat du compte"],
"field_name":["de_fb_compte-actif"],
"field_id":["de_fb_field_1"],
"form_type_confirm":""
}
{
"field_title":["\u00c9tat du compte"],
"field_name":["de_fb_compte-actif"],
"field_id":["de_fb_field_1"],
"compte-actif":"Actif",
"form_type_confirm":""
}
CIPF_admin_activation_prof
{
"field_title":["\u00c9tat du compte"],
"field_name":["compte-actif"],
"field_id":["cipf_admin_activation_prof"],
"compte-actif":"Inactif",
"form_type_confirm":""
}
--- with field test :
{
"field_title":["\u00c9tat du compte","test"],
"field_name":["compte-actif","de_fb_field_3"],
"field_id":["cipf_admin_activation_prof","de_fb_field_3"],
"field_3":"",
"form_type_confirm":""
}
*/
?>

View File

@@ -113,11 +113,13 @@ CIPF_admin_activation_prof
/* /*
* ABORTED * ABORTED
* tried to receive the form in ajax from front with wp REST API * 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) { function admin_validate_prof_form_CIPF($request) {
PLGNTLS_class::debug_infos(); PLGNTLS_class::debug_infos();
$slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION;
error_log("-----"); error_log("-----");
error_log("inside admin_validate_prof_form_CIPF"); error_log("inside admin_validate_prof_form_CIPF");
error_log("request"); error_log("request");
@@ -126,19 +128,23 @@ function admin_validate_prof_form_CIPF($request) {
error_log(json_encode($_POST)); 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":""} // _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 ? // Set up nocache headers before redirecting : https://developer.wordpress.org/reference/functions/wp_safe_redirect/#user-contributed-notes
// with wp_safe_redirect(home_url('/slug')); ? nocache_headers();
// return new WP_REST_Response( array( 'message' => 'Form submitted successfully' ), 200 ); $redirect_url = home_url() . '/' . $slug_page_redirection;
wp_redirect($redirect_url, 301);
exit;
} }
function endpoint_form_admin_activate_prof_CIPF() { function endpoint_form_admin_activate_prof_CIPF() {
PLGNTLS_class::debug_infos(); 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( register_rest_route($base_rest_route, '/admin_prof_activate_form', array(
'methods' => 'POST', 'methods' => 'POST',
'callback' => 'admin_validate_prof_form_CIPF', 'callback' => 'admin_validate_prof_form_CIPF',
)); ));
}; };
add_action('rest_api_init', 'endpoint_form_admin_activate_prof_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(); $current_user_id = get_current_user_id();
if ($current_user_id != $author_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); wp_redirect(home_url(), 301);
exit; exit;
} }

View File

@@ -13,7 +13,7 @@ if (!defined('ABSPATH')) {
// diff routes and endpoints : https://stackoverflow.com/q/56075017/9497573 // diff routes and endpoints : https://stackoverflow.com/q/56075017/9497573
function routes_endpoints_CIPF() { function routes_endpoints_CIPF() {
PLGNTLS_class::debug_infos(); 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( register_rest_route($base_rest_route, '/orders', array(
'methods' => 'POST', 'methods' => 'POST',
'callback' => 'handle_orders_request_CIPF', '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; $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); wp_redirect($redirection_prof_inactiv, 301);
exit; exit;
} }

View File

@@ -14,25 +14,35 @@ if (!defined('ABSPATH')) {
* if prof -> author page * if prof -> author page
* if partenaire -> post page || home page * if partenaire -> post page || home page
*/ */
function redirection_page_CIPF(){ function redirection_profil_page_CIPF(){
PLGNTLS_class::debug_infos(); PLGNTLS_class::debug_infos();
$slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION;
$role_prof = PLGNTLS_class::ROLE_PROF; $role_prof = PLGNTLS_class::ROLE_PROF;
$role_partner = PLGNTLS_class::ROLE_PARTNER; $role_partner = PLGNTLS_class::ROLE_PARTNER;
if (!is_page($slug_page_redirection))
return;
$base_url = home_url(); $base_url = home_url();
$current_user_id = get_current_user_id(); $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()) { 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); wp_redirect($base_url, 301);
exit;
} }
if (current_user_can($role_prof)) { if (current_user_can($role_prof)) {
error_log("is prof");
$user_page = get_author_posts_url($current_user_id); $user_page = get_author_posts_url($current_user_id);
wp_redirect($user_page, 301); wp_redirect($user_page, 301);
exit;
} }
else if (current_user_can($role_partner)) { else if (current_user_can($role_partner)) {
error_log("is partenaire");
$args = array( $args = array(
'post_type' => 'post', 'post_type' => 'post',
'author' => $current_user_id, 'author' => $current_user_id,
@@ -47,11 +57,81 @@ function redirection_page_CIPF(){
$redirect_url = get_permalink($query->ID); $redirect_url = get_permalink($query->ID);
} }
wp_redirect($redirect_url, 301); wp_redirect($redirect_url, 301);
exit;
}
else {
error_log("is else");
wp_redirect($base_url, 301);
exit;
} }
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'); 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_fipf = PLGNTLS_class::ROLE_FIPF;
$role_admin = PLGNTLS_class::ROLE_ADMIN; $role_admin = PLGNTLS_class::ROLE_ADMIN;
$base_url = home_url();
if (!is_page($slug_renew_card)) if (!is_page($slug_renew_card))
return; return;
if (!is_user_logged_in()) { 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); wp_redirect($base_url, 301);
exit;
} }
$base_url = home_url();
$current_user_id = get_current_user_id(); $current_user_id = get_current_user_id();
$current_user = wp_get_current_user(); $current_user = wp_get_current_user();
$acf_id = 'user_'.$current_user_id; $acf_id = 'user_'.$current_user_id;
@@ -59,6 +63,8 @@ function renew_page_restrictions_CIPF(){
if (current_user_can($role_prof)) { if (current_user_can($role_prof)) {
$can_renew = get_field($acf_prof_can_renew['_name'], $acf_id); $can_renew = get_field($acf_prof_can_renew['_name'], $acf_id);
if ($can_renew === false) { 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; $redirect_url = home_url() . '/' . $slug_page_redirection;
wp_redirect($redirect_url, 301); wp_redirect($redirect_url, 301);
exit; exit;

View File

@@ -76,12 +76,16 @@ class PLGNTLS_class
const META_PAYEMENT_STATUS = 'cipf_payement_status'; const META_PAYEMENT_STATUS = 'cipf_payement_status';
const META_ORDER_ID = 'cipf_order_id'; const META_ORDER_ID = 'cipf_order_id';
// SLUG // SLUG & URL & QUERY
const SLUG_PROF_INACTIV = 'validation-en-cours'; const SLUG_PROF_INACTIV = 'validation-en-cours';
const SLUG_RENEW_CARD = 'commande'; const SLUG_RENEW_CARD = 'commande';
const SLUG_PAGE_REDIRECTION = 'redirection_cipf'; const SLUG_PAGE_REDIRECTION = 'redirection_cipf';
const SLUG_PAYPAL_REDIRECTION_SUCCESS = self::SLUG_PAGE_REDIRECTION; // const SLUG_PAYPAL_REDIRECTION_SUCCESS = self::SLUG_PAGE_REDIRECTION;
const SLUG_PAYPAL_REDIRECTION_FAILURE = self::SLUG_PAGE_REDIRECTION; // const SLUG_PAYPAL_REDIRECTION_FAILURE = self::SLUG_PAGE_REDIRECTION;
const SLUG_PAYPAL_REDIRECTION_SUCCESS = '?'.self::QUERY_REDIRECTION_PROFIL;
const SLUG_PAYPAL_REDIRECTION_FAILURE = '?'.self::QUERY_REDIRECTION_PROFIL;
const URL_BASE_REST_ROUTE = 'cipf_plugin/api/v1'; // for routes, in php/paypal/routes.php && php/admin_modif_prof.php
const QUERY_REDIRECTION_PROFIL = 'redirection_cipf';
// PAYPAL // PAYPAL
const PAYPAL_CLIENT_ID = "AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl"; const PAYPAL_CLIENT_ID = "AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl";
@@ -101,7 +105,6 @@ class PLGNTLS_class
const CARD_VALIDITY_TIME = '1 year'; // string : time of validity of the card (ex: '1 month' or '1 year' or '60 days') const CARD_VALIDITY_TIME = '1 year'; // string : time of validity of the card (ex: '1 month' or '1 year' or '60 days')
const USER_INFO_DATE_FORMAT = 'd/m/Y'; // for user_infos.php (date format : https://www.php.net/manual/fr/datetime.format.php) const USER_INFO_DATE_FORMAT = 'd/m/Y'; // for user_infos.php (date format : https://www.php.net/manual/fr/datetime.format.php)
const ADMIN_VALIDATE_PROF_FIELD = 'cipf_admin_activate_prof'; // for admin_modif_prof.php const ADMIN_VALIDATE_PROF_FIELD = 'cipf_admin_activate_prof'; // for admin_modif_prof.php
const BASE_REST_ROUTE = 'cipf_plugin/api/v1'; // for routes, in php/paypal/routes.php && php/admin_modif_prof.php

View File

@@ -1,7 +1,8 @@
/** /*
* this file is there for scripts that uses modules import * this file is there for scripts that uses modules import
*/ *
*/
export function PLGNTLS_fetch(url, options = {}) { export function PLGNTLS_fetch(url, options = {}) {
console.log("inside PLGNTLS_fetch"); console.log("inside PLGNTLS_fetch");
url = PLGNTLS_data.fetch_url + url; url = PLGNTLS_data.fetch_url + url;
@@ -13,9 +14,10 @@ export function PLGNTLS_fetch(url, options = {}) {
return fetch(url, options); return fetch(url, options);
} }
/** /*
* For non-module scripts, attach to the global scope * For non-module scripts, attach to the global scope
*/ *
*/
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
window.PLGNTLS_fetch = PLGNTLS_fetch; window.PLGNTLS_fetch = PLGNTLS_fetch;
} }