From faefa98114310d9b5d0d46490eca953d6a203ff2 Mon Sep 17 00:00:00 2001 From: asus Date: Sun, 10 Mar 2024 11:13:44 +0100 Subject: [PATCH] - added debug function - fixed issues in acf fields use --- .../cipf_plugin/php/author_restriction.php | 1 + plugins/cipf_plugin/php/filter_mail.php | 3 + plugins/cipf_plugin/php/hide_admin.php | 1 + plugins/cipf_plugin/php/menus.php | 1 + .../php/paypal/route_api_orders.php | 5 +- .../php/paypal/route_api_orders_capture.php | 2 + .../php/paypal/route_api_utils.php | 6 +- plugins/cipf_plugin/php/paypal/routes.php | 4 +- plugins/cipf_plugin/php/paypal/shortcode.php | 5 +- .../php/paypal/update_user_payment.php | 66 ++++++++-- .../cipf_plugin/php/paypal/user_can_pay.php | 14 ++- plugins/cipf_plugin/php/prof_check_page.php | 3 + plugins/cipf_plugin/php/redirections.php | 1 + .../cipf_plugin/php/register_partenaires.php | 1 + plugins/cipf_plugin/php/renew_card.php | 7 +- plugins/cipf_plugin/php/user_infos.php | 5 + plugins/cipf_plugin/utils/plgntls_class.php | 116 +++++------------- 17 files changed, 135 insertions(+), 106 deletions(-) diff --git a/plugins/cipf_plugin/php/author_restriction.php b/plugins/cipf_plugin/php/author_restriction.php index 3dea627..5713607 100644 --- a/plugins/cipf_plugin/php/author_restriction.php +++ b/plugins/cipf_plugin/php/author_restriction.php @@ -11,6 +11,7 @@ if (!defined('ABSPATH')) { function restrict_author_page_CIPF() { + PLGNTLS_class::debug_infos(); if (!is_author()) return; diff --git a/plugins/cipf_plugin/php/filter_mail.php b/plugins/cipf_plugin/php/filter_mail.php index aa7ce7a..105f877 100644 --- a/plugins/cipf_plugin/php/filter_mail.php +++ b/plugins/cipf_plugin/php/filter_mail.php @@ -16,6 +16,7 @@ if (!defined('ABSPATH')) { * */ function replace_words_CIPF($matches, $user_id = null) { + PLGNTLS_class::debug_infos(); if ($user_id !== null) { $current_user = get_user_by('id', $user_id); } @@ -66,6 +67,7 @@ function replace_words_CIPF($matches, $user_id = null) { * */ function filter_email_fb_CIPF($reply_body, $post_array) { + PLGNTLS_class::debug_infos(); $id = $post_array['ID']; // pattern : anything surrounded by '$$', ex : $$value$$ $pattern = '/\$\$(.*?)\$\$/'; @@ -92,6 +94,7 @@ add_filter('df_notification_body', 'filter_email_fb_CIPF', 10, 2); // the admini * or event that a different user has already logged in */ function filter_email_wp_CIPF($args) { + PLGNTLS_class::debug_infos(); // pattern : anything surrounded by '$$', ex : $$value$$ $pattern = '/\$\$(.*?)\$\$/'; diff --git a/plugins/cipf_plugin/php/hide_admin.php b/plugins/cipf_plugin/php/hide_admin.php index b7349c2..526aae9 100644 --- a/plugins/cipf_plugin/php/hide_admin.php +++ b/plugins/cipf_plugin/php/hide_admin.php @@ -15,6 +15,7 @@ if (!defined('ABSPATH')) { * hide admin bar if access a front page and is not an admin */ function hide_admin_bar_CIPF() { + PLGNTLS_class::debug_infos(); if (!current_user_can('administrator') && !is_admin()) { show_admin_bar(false); } diff --git a/plugins/cipf_plugin/php/menus.php b/plugins/cipf_plugin/php/menus.php index 45827be..fc594d7 100644 --- a/plugins/cipf_plugin/php/menus.php +++ b/plugins/cipf_plugin/php/menus.php @@ -23,6 +23,7 @@ if (!defined('ABSPATH')) { * - $menu_redirect = 'www.un_autre_site.net/contact' -> https://www.un_autre_site.net/contact */ function change_menu_logout($items){ + PLGNTLS_class::debug_infos(); $menu_title = 'special logout'; // quelques urls utiles : diff --git a/plugins/cipf_plugin/php/paypal/route_api_orders.php b/plugins/cipf_plugin/php/paypal/route_api_orders.php index 7c0e7a9..b38b8c0 100644 --- a/plugins/cipf_plugin/php/paypal/route_api_orders.php +++ b/plugins/cipf_plugin/php/paypal/route_api_orders.php @@ -20,6 +20,7 @@ if (!defined('ABSPATH')) { * */ function handle_orders_request_CIPF($request_data) { + PLGNTLS_class::debug_infos(); // declaring outside the try..catch scope $order_response = array(); @@ -96,8 +97,8 @@ function handle_orders_request_CIPF($request_data) { * @see https://developer.paypal.com/docs/api/orders/v2/#orders_create * */ -function create_order_CIPF() -{ +function create_order_CIPF() { + PLGNTLS_class::debug_infos(); $paypal_api_base_url = PLGNTLS_class::PAYPAL_API_BASE_URL; $access_token = generate_access_token_CIPF(); diff --git a/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php b/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php index 6590147..87ccf6d 100644 --- a/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php +++ b/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php @@ -11,6 +11,7 @@ if (!defined('ABSPATH')) { function handle_orders_capture_request_CIPF($request) { + PLGNTLS_class::debug_infos(); $order_id = $request['orderID']; // declaring outside the try..catch scope $response_json = array(); @@ -74,6 +75,7 @@ function handle_orders_capture_request_CIPF($request) { * @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture */ function capture_order_CIPF($order_id) { + PLGNTLS_class::debug_infos(); $paypal_api_base_url = PLGNTLS_class::PAYPAL_API_BASE_URL; $access_token = generate_access_token_CIPF(); $url = $paypal_api_base_url . '/v2/checkout/orders/' . $order_id . '/capture'; diff --git a/plugins/cipf_plugin/php/paypal/route_api_utils.php b/plugins/cipf_plugin/php/paypal/route_api_utils.php index bce2e77..19cff63 100644 --- a/plugins/cipf_plugin/php/paypal/route_api_utils.php +++ b/plugins/cipf_plugin/php/paypal/route_api_utils.php @@ -14,6 +14,7 @@ if (!defined('ABSPATH')) { * */ function handle_json_response_CIPF($response, $ch) { + PLGNTLS_class::debug_infos(); if ($response === false) throw new HttpException('cURL error: ' . curl_error($ch), 502); @@ -32,6 +33,7 @@ function handle_json_response_CIPF($response, $ch) { ); } function handle_response_CIPF($response) { + PLGNTLS_class::debug_infos(); try { // Decode JSON response @@ -57,8 +59,8 @@ function handle_response_CIPF($response) { * @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend * @see https://developer.paypal.com/api/rest/authentication/ */ -function generate_access_token_CIPF() -{ +function generate_access_token_CIPF() { + PLGNTLS_class::debug_infos(); $paypal_client_id = PLGNTLS_class::PAYPAL_CLIENT_ID; $paypal_client_secret = PLGNTLS_class::PAYPAL_CLIENT_SECRET; $paypal_api_base_url = PLGNTLS_class::PAYPAL_API_BASE_URL; diff --git a/plugins/cipf_plugin/php/paypal/routes.php b/plugins/cipf_plugin/php/paypal/routes.php index 072cfe9..48a9e0d 100644 --- a/plugins/cipf_plugin/php/paypal/routes.php +++ b/plugins/cipf_plugin/php/paypal/routes.php @@ -11,8 +11,8 @@ if (!defined('ABSPATH')) { // handling routes and endpoints // diff routes and endpoints : https://stackoverflow.com/q/56075017/9497573 -function routes_endpoints_CIPF() -{ +function routes_endpoints_CIPF() { + PLGNTLS_class::debug_infos(); $base_rest_route = "cipf_plugin/api/v1"; register_rest_route($base_rest_route, '/orders', array( 'methods' => 'POST', diff --git a/plugins/cipf_plugin/php/paypal/shortcode.php b/plugins/cipf_plugin/php/paypal/shortcode.php index cbb8838..79afca9 100644 --- a/plugins/cipf_plugin/php/paypal/shortcode.php +++ b/plugins/cipf_plugin/php/paypal/shortcode.php @@ -15,8 +15,8 @@ if (!defined('ABSPATH')) { /** * call to paypal_shortcode_content() */ -function paypal_shortcode_content_CIPF() -{ +function paypal_shortcode_content_CIPF() { + PLGNTLS_class::debug_infos(); $slug_paypal_redirection_success = PLGNTLS_class::SLUG_PAYPAL_REDIRECTION_SUCCESS; $slug_paypal_redirection_failure = PLGNTLS_class::SLUG_PAYPAL_REDIRECTION_FAILURE; $paypal_client_id = PLGNTLS_class::PAYPAL_CLIENT_ID; @@ -55,6 +55,7 @@ add_shortcode('cipf_paypal_shortcode', 'paypal_shortcode_content_CIPF'); function no_payment_CIPF() { + PLGNTLS_class::debug_infos(); return; } diff --git a/plugins/cipf_plugin/php/paypal/update_user_payment.php b/plugins/cipf_plugin/php/paypal/update_user_payment.php index 6588d2f..4aa7539 100644 --- a/plugins/cipf_plugin/php/paypal/update_user_payment.php +++ b/plugins/cipf_plugin/php/paypal/update_user_payment.php @@ -35,7 +35,9 @@ if (!defined('ABSPATH')) { * */ function update_user_pre_order_CIPF($message) { + PLGNTLS_class::debug_infos(); $meta_payement_status = PLGNTLS_class::META_PAYEMENT_STATUS; + $meta_order_id = PLGNTLS_class::META_ORDER_ID; $order_id = $message->id; $user_id = get_current_user_id(); @@ -46,8 +48,8 @@ function update_user_pre_order_CIPF($message) { * - addind order_id to cipf_order_id meta field * */ - delete_user_meta($user_id, 'cipf_order_id'); - add_user_meta($user_id, 'cipf_order_id', $order_id); + delete_user_meta($user_id, $meta_order_id); + add_user_meta($user_id, $meta_order_id, $order_id); /* * create a meta field to check states of payements on prof author page : @@ -69,6 +71,7 @@ function update_user_pre_order_CIPF($message) { * */ function update_user_post_capture_CIPF($message) { + PLGNTLS_class::debug_infos(); $meta_payement_status = PLGNTLS_class::META_PAYEMENT_STATUS; $order_id = $message->id; @@ -94,7 +97,10 @@ function update_user_post_capture_CIPF($message) { * */ // find the user containing the order_id and delete this order_id + find_user_with_order_id_CIPF($user_id, $order_id); $user_id_to_update = delete_order_id_on_success_CIPF($user_id, $order_id); + if ($user_id_to_update === false) + return; if ($status === 'COMPLETED') { // proceed to validate payment for user validate_payment_for_user_CIPF($user_id_to_update, $order_id); @@ -111,6 +117,7 @@ function update_user_post_capture_CIPF($message) { * */ function failure_payment_for_user_CIPF($user_id, $order_id) { + PLGNTLS_class::debug_infos(); } @@ -119,6 +126,7 @@ function failure_payment_for_user_CIPF($user_id, $order_id) { * */ function validate_payment_for_user_CIPF($user_id, $order_id) { + PLGNTLS_class::debug_infos(); $acf_card_state = PLGNTLS_class::ACF_CARD_STATE; $acf_card_expiration = PLGNTLS_class::ACF_CARD_EXPIRATION; $acf_prof_can_renew = PLGNTLS_class::ACF_PROF_CAN_RENEW; @@ -234,19 +242,61 @@ add_action('orderid_deletion_event_CIPF', 'delete_order_id_later_CIPF', 5, 2); * bool false - if no match found * */ -function delete_order_id_on_success_CIPF($current_user_id, $order_id) -{ - $del_ret = delete_user_meta($current_user_id, 'cipf_order_id', $order_id); +function find_user_with_order_id_CIPF($current_user_id, $order_id) { + PLGNTLS_class::debug_infos(); + $meta_order_id = PLGNTLS_class::META_ORDER_ID; + $role_prof = PLGNTLS_class::ROLE_PROF; + + $user_metas = get_user_meta($current_user_id, $meta_order_id, false); + if (in_array($order_id, $user_metas)) { + return $current_user_id; + } + + // it means the current user didn't have this order_id + // so we look for another user +// $users = get_users(); +// foreach ($users as $user) { +// $user_id = $user->ID; +// $del_ret = delete_user_meta($user_id, $meta_order_id, $order_id); +// if ($del_ret === true) +// return $user_id; +// } + + // https://developer.wordpress.org/reference/classes/WP_User_Query/prepare_query/ + $users = get_users(array( + 'role' => $role_prof, + 'meta_key' => $meta_order_id, + 'meta_value' => $order_id, + 'fields' => 'ID', + )); + if (count($users) > 1) + throw new HttpException('multiple users with same order_id', 502); + else if (count($users) === 1) + return reset($users); + + + return false; +} + +/* +* @return mixed num - user_id +* bool false - if no match found +* +*/ +function delete_order_id_on_success_CIPF($current_user_id, $order_id) { + PLGNTLS_class::debug_infos(); + $meta_order_id = PLGNTLS_class::META_ORDER_ID; + + $del_ret = delete_user_meta($current_user_id, $meta_order_id, $order_id); if ($del_ret === true) return $current_user_id; // it means the current user didn't have this order_id // so we look for another user $users = get_users(); - foreach ($users as $user) - { + foreach ($users as $user) { $user_id = $user->ID; - $del_ret = delete_user_meta($user_id, 'cipf_order_id', $order_id); + $del_ret = delete_user_meta($user_id, $meta_order_id, $order_id); if ($del_ret === true) return $user_id; } diff --git a/plugins/cipf_plugin/php/paypal/user_can_pay.php b/plugins/cipf_plugin/php/paypal/user_can_pay.php index 4e1554f..8838202 100644 --- a/plugins/cipf_plugin/php/paypal/user_can_pay.php +++ b/plugins/cipf_plugin/php/paypal/user_can_pay.php @@ -1,5 +1,13 @@ post page || home page */ function redirection_page_CIPF(){ + PLGNTLS_class::debug_infos(); $slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION; if (!is_page($slug_page_redirection)) diff --git a/plugins/cipf_plugin/php/register_partenaires.php b/plugins/cipf_plugin/php/register_partenaires.php index 44703f5..443179f 100644 --- a/plugins/cipf_plugin/php/register_partenaires.php +++ b/plugins/cipf_plugin/php/register_partenaires.php @@ -14,6 +14,7 @@ if (!defined('ABSPATH')) { * */ function add_partenaires_PLGNTLS($customer_data){ + PLGNTLS_class::debug_infos(); $current_url = $_SERVER['HTTP_REFERER']; // not reliable to use referer, TODO: find another solution $path_brut = parse_url($current_url, PHP_URL_PATH); $path = trim($path_brut, '/'); diff --git a/plugins/cipf_plugin/php/renew_card.php b/plugins/cipf_plugin/php/renew_card.php index 86c6165..c965094 100644 --- a/plugins/cipf_plugin/php/renew_card.php +++ b/plugins/cipf_plugin/php/renew_card.php @@ -20,6 +20,7 @@ if (!defined('ABSPATH')) { * */ function reset_some_fields_CIPF() { + PLGNTLS_class::debug_infos(); $acf_cgv = PLGNTLS_class::ACF_PROF_CGV; $acf_payement = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD; $acf_delivery = PLGNTLS_class::ACF_CARD_PRICE_DELIVERY; @@ -48,6 +49,7 @@ function reset_some_fields_CIPF() { * */ function renew_page_restrictions_CIPF(){ + PLGNTLS_class::debug_infos(); $slug_renew_card = PLGNTLS_class::SLUG_RENEW_CARD; $slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION; $acf_prof_can_renew = PLGNTLS_class::ACF_PROF_CAN_RENEW; @@ -79,7 +81,7 @@ function renew_page_restrictions_CIPF(){ * */ if (current_user_can('professeur__professeure')) { - $can_renew = get_field($acf_prof_can_renew, $acf_id); + $can_renew = get_field($acf_prof_can_renew['_name'], $acf_id); if ($can_renew === false) { $redirect_url = home_url() . '/' . $slug_page_redirection; wp_redirect($redirect_url, 301); @@ -110,6 +112,7 @@ add_action('template_redirect', 'renew_page_restrictions_CIPF'); * */ function renew_page_filter_message_CIPF(){ + PLGNTLS_class::debug_infos(); $slug_renew_card = PLGNTLS_class::SLUG_RENEW_CARD; $acf_card_state = PLGNTLS_class::ACF_CARD_STATE; @@ -122,8 +125,6 @@ function renew_page_filter_message_CIPF(){ $cipf_renew = new PLGNTLS_class(); $card_state = get_field($acf_card_state['_name'], $acf_id); - error_log("card_state"); - error_log($card_state); if ($card_state === 'Renouvellement') { $cipf_renew->add_to_front(array( diff --git a/plugins/cipf_plugin/php/user_infos.php b/plugins/cipf_plugin/php/user_infos.php index ff5c54a..454e880 100644 --- a/plugins/cipf_plugin/php/user_infos.php +++ b/plugins/cipf_plugin/php/user_infos.php @@ -8,12 +8,14 @@ if (!defined('ABSPATH')) { } function extract_if_array_size_one_CIPF($value) { + PLGNTLS_class::debug_infos(); if (is_array($value) && count($value) === 1) return reset($value); return $value; } function merge_two_arrays_CIPF($array1, $array2) { + PLGNTLS_class::debug_infos(); $new_array = $array1; foreach ($array2 as $key2 => $value2) { $value = extract_if_array_size_one_CIPF($value2); @@ -38,6 +40,7 @@ function merge_two_arrays_CIPF($array1, $array2) { function output_list_front_CIPF($array) { + PLGNTLS_class::debug_infos(); $output = '