From ecc072e25b31e0ec2ff42fb3c09580e9e1d7b208 Mon Sep 17 00:00:00 2001 From: asus Date: Thu, 14 Mar 2024 10:29:02 +0100 Subject: [PATCH] added redirection without need of a page, use it for paypal success or failure message already --- 2 | 108 ------------------ plugins/cipf_plugin/php/admin_modif_prof.php | 18 ++- .../cipf_plugin/php/author_restriction.php | 2 + plugins/cipf_plugin/php/paypal/routes.php | 2 +- plugins/cipf_plugin/php/prof_check_page.php | 2 + plugins/cipf_plugin/php/redirections.php | 90 ++++++++++++++- plugins/cipf_plugin/php/renew_card.php | 8 +- plugins/cipf_plugin/utils/plgntls_class.php | 11 +- plugins/cipf_plugin/utils/plgntls_fetch.js | 14 ++- 9 files changed, 124 insertions(+), 131 deletions(-) delete mode 100644 2 diff --git a/2 b/2 deleted file mode 100644 index b33c33b..0000000 --- a/2 +++ /dev/null @@ -1,108 +0,0 @@ - diff --git a/plugins/cipf_plugin/php/admin_modif_prof.php b/plugins/cipf_plugin/php/admin_modif_prof.php index 19c3620..8a72447 100644 --- a/plugins/cipf_plugin/php/admin_modif_prof.php +++ b/plugins/cipf_plugin/php/admin_modif_prof.php @@ -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'); +*/ diff --git a/plugins/cipf_plugin/php/author_restriction.php b/plugins/cipf_plugin/php/author_restriction.php index 096cb31..73b87a3 100644 --- a/plugins/cipf_plugin/php/author_restriction.php +++ b/plugins/cipf_plugin/php/author_restriction.php @@ -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; } diff --git a/plugins/cipf_plugin/php/paypal/routes.php b/plugins/cipf_plugin/php/paypal/routes.php index 7540c34..5c523b9 100644 --- a/plugins/cipf_plugin/php/paypal/routes.php +++ b/plugins/cipf_plugin/php/paypal/routes.php @@ -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', diff --git a/plugins/cipf_plugin/php/prof_check_page.php b/plugins/cipf_plugin/php/prof_check_page.php index d0d03ff..86c1b14 100644 --- a/plugins/cipf_plugin/php/prof_check_page.php +++ b/plugins/cipf_plugin/php/prof_check_page.php @@ -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; } diff --git a/plugins/cipf_plugin/php/redirections.php b/plugins/cipf_plugin/php/redirections.php index aa53499..cfa79a0 100644 --- a/plugins/cipf_plugin/php/redirections.php +++ b/plugins/cipf_plugin/php/redirections.php @@ -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'); + + + + + ?> diff --git a/plugins/cipf_plugin/php/renew_card.php b/plugins/cipf_plugin/php/renew_card.php index 96dbb15..b592ed0 100644 --- a/plugins/cipf_plugin/php/renew_card.php +++ b/plugins/cipf_plugin/php/renew_card.php @@ -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; diff --git a/plugins/cipf_plugin/utils/plgntls_class.php b/plugins/cipf_plugin/utils/plgntls_class.php index fc386ca..66833d2 100644 --- a/plugins/cipf_plugin/utils/plgntls_class.php +++ b/plugins/cipf_plugin/utils/plgntls_class.php @@ -76,12 +76,16 @@ class PLGNTLS_class const META_PAYEMENT_STATUS = 'cipf_payement_status'; const META_ORDER_ID = 'cipf_order_id'; - // SLUG + // SLUG & URL & QUERY const SLUG_PROF_INACTIV = 'validation-en-cours'; const SLUG_RENEW_CARD = 'commande'; const SLUG_PAGE_REDIRECTION = 'redirection_cipf'; - const SLUG_PAYPAL_REDIRECTION_SUCCESS = self::SLUG_PAGE_REDIRECTION; - const SLUG_PAYPAL_REDIRECTION_FAILURE = 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_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 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 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 BASE_REST_ROUTE = 'cipf_plugin/api/v1'; // for routes, in php/paypal/routes.php && php/admin_modif_prof.php diff --git a/plugins/cipf_plugin/utils/plgntls_fetch.js b/plugins/cipf_plugin/utils/plgntls_fetch.js index 323a743..2190cdf 100644 --- a/plugins/cipf_plugin/utils/plgntls_fetch.js +++ b/plugins/cipf_plugin/utils/plgntls_fetch.js @@ -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 = {}) { console.log("inside PLGNTLS_fetch"); url = PLGNTLS_data.fetch_url + url; @@ -13,9 +14,10 @@ export function PLGNTLS_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') { window.PLGNTLS_fetch = PLGNTLS_fetch; }