- added debug function

- fixed issues in acf fields use
This commit is contained in:
asus
2024-03-10 11:13:44 +01:00
parent f3a9be5a89
commit faefa98114
17 changed files with 135 additions and 106 deletions

View File

@@ -11,6 +11,7 @@ if (!defined('ABSPATH')) {
function restrict_author_page_CIPF() {
PLGNTLS_class::debug_infos();
if (!is_author())
return;

View File

@@ -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 = '/\$\$(.*?)\$\$/';

View File

@@ -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);
}

View File

@@ -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 :

View File

@@ -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();

View File

@@ -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';

View File

@@ -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;

View File

@@ -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',

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -1,5 +1,13 @@
<?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!');
}
@@ -9,6 +17,7 @@
*
*/
function check_can_pay_CIPF() {
PLGNTLS_class::debug_infos();
$acf_card_payment_method = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD;
$acf_card_price_choice = PLGNTLS_class::ACF_CARD_PRICE_CHOICE;
$acf_card_price_delivery = PLGNTLS_class::ACF_CARD_PRICE_DELIVERY;
@@ -38,13 +47,13 @@ function check_can_pay_CIPF() {
$tarif = (int)get_field($acf_card_price_choice['_name'], $acf_id);
$livraison = (int)get_field($acf_card_price_delivery['_name'], $acf_id);
$price = $tarif + $livraison;
update_field($acf_card_price_total, $price, $acf_id);
update_field($acf_card_price_total['_name'], $price, $acf_id);
/*
* price is not empty or 0
*
*/
$price = get_field($acf_card_price_total, $acf_id);
$price = get_field($acf_card_price_total['_name'], $acf_id);
if (empty($price)) {
return WP_Error('cannot_purchase', "no price selected");
}
@@ -52,6 +61,7 @@ function check_can_pay_CIPF() {
return WP_Error('cannot_purchase', "price is 0, nothing to purchase");
}
/*
* date validity is empty
* or is paste

View File

@@ -12,6 +12,7 @@ if (!defined('ABSPATH')) {
function handle_prof_is_activ_CIPF($author_id) {
PLGNTLS_class::debug_infos();
$acf_prof_is_activ = PLGNTLS_class::ACF_PROF_IS_ACTIV;
$slug_wait_activation = PLGNTLS_class::SLUG_PROF_INACTIV;
@@ -68,6 +69,7 @@ function handle_prof_is_activ_CIPF($author_id) {
*
*/
function show_prof_paiement_messages_CIPF($user_id) {
PLGNTLS_class::debug_infos();
$acf_prof_is_activ = PLGNTLS_class::ACF_PROF_IS_ACTIV;
$meta_payement_status = PLGNTLS_class::META_PAYEMENT_STATUS;
@@ -105,6 +107,7 @@ function show_prof_paiement_messages_CIPF($user_id) {
*
*/
function check_prof_page_CIPF() {
PLGNTLS_class::debug_infos();
// is an author page
if (!is_author())
return;

View File

@@ -15,6 +15,7 @@ if (!defined('ABSPATH')) {
* if partenaire -> 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))

View File

@@ -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, '/');

View File

@@ -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(

View File

@@ -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 = '<ul>';
foreach ($array as $key => $value) {
if (str_starts_with($key, '_'))
@@ -59,6 +62,7 @@ function output_list_front_CIPF($array) {
function format_user_info_CIPF($output, $query, &$current_user, $user_id) {
PLGNTLS_class::debug_infos();
$output_date_format = PLGNTLS_class::USER_INFO_DATE_FORMAT;
$is_acf = false;
@@ -127,6 +131,7 @@ function format_user_info_CIPF($output, $query, &$current_user, $user_id) {
*
*/
function current_user_infos_CIPF($atts) {
PLGNTLS_class::debug_infos();
if (!is_user_logged_in())
return ;

View File

@@ -61,93 +61,20 @@ class PLGNTLS_class
*
*/
// ACF
const ACF_CARD_STATE = ['_name'=>'etat_carte', 'new'=>'Commande', 'renew'=>'Renouvellement'];
const ACF_CARD_PAYMENT_METHOD = ['_name'=>'paiement', 'paypal'=>'Paypal', 'transfert'=>'Virement'];
const ACF_CARD_PRICE_CHOICE = ['_name'=>'tarif', 'low'=>'10', 'high'=>'15'];
const ACF_CARD_PRICE_DELIVERY = ['_name'=>'livraison', 'pdf'=>'PDF', 'post'=>'Fabrication'];
const ACF_PROF_IS_ACTIV = ['_name'=>'compte-actif', 'activ'=>'Actif', 'inactiv'=>'Inactif'];
const ACF_PROF_CGV = ['_name'=>'cgv', 'cgv'=>'cgv'];
const ACF_PROF_CAN_RENEW = ['_name'=>'renouvellement_possible', 'can'=>true, 'cannot'=>false];
const ACF_CARD_PRICE_TOTAL = ['_name'=>'somme_a_regler']; // input number
const ACF_CARD_EXPIRATION = ['_name'=>'fin_de_validite']; // input date
/*
const ACF_CARD_STATE = [
'_name' => 'etat_carte',
'new' => 'Commande',
'renew' => 'Renouvellement',
];
const ACF_CARD_PAYMENT_METHOD = [
'_name' => 'paiement',
'paypal' => 'Paypal',
'transfert' => 'Virement',
];
const ACF_CARD_PRICE_CHOICE = [
'_name' => 'tarif',
'low' => '10',
'high' => '15',
];
const ACF_CARD_PRICE_DELIVERY = [
'_name' => 'livraison',
'pdf' => 'PDF',
'post' => 'Fabrication',
];
const ACF_PROF_IS_ACTIV = [
'_name' => 'compte-actif',
'activ' => 'Actif',
'inactiv' => 'Inactif',
];
const ACF_PROF_CGV = [
'_name' => 'cgv',
'cgv' => 'cgv',
];
const ACF_PROF_CAN_RENEW = [
'_name' => 'renouvellement_possible',
'can' => true,
'cannot' => false,
];
const ACF_CARD_PRICE_TOTAL = ['_name' => 'somme_a_regler']; // input number
const ACF_CARD_EXPIRATION = ['_name' => 'fin_de_validite']; // input date
*/
/*
const ACF_CARD_STATE = 'etat_carte';
const ACF_CARD_STATE__FIRST = 'Commande';
const ACF_CARD_STATE__RENEW = 'Renouvellement';
const ACF_CARD_PAYMENT_METHOD = 'paiement';
const ACF_CARD_PAYMENT_METHOD__PAYPAL = 'Paypal';
const ACF_CARD_PAYMENT_METHOD__TRANSFERT = 'Virement';
const ACF_CARD_PRICE_CHOICE = 'tarif';
const ACF_CARD_PRICE_CHOICE__LOW = '10';
const ACF_CARD_PRICE_CHOICE__HIGH = '15';
const ACF_CARD_PRICE_DELIVERY = 'livraison';
const ACF_CARD_PRICE_DELIVERY__PDF = 'PDF';
const ACF_CARD_PRICE_DELIVERY__POST = 'Fabrication';
const ACF_PROF_IS_ACTIV = 'compte-actif';
const ACF_PROF_IS_ACTIV__ACTIV = 'Actif';
const ACF_PROF_IS_ACTIV__INACTIV = 'Inactif';
const ACF_PROF_CGV = 'cgv';
const ACF_PROF_CGV__CGV = 'cgv';
const ACF_CARD_PRICE_TOTAL = 'somme_a_regler'; // input number
const ACF_CARD_EXPIRATION = 'fin_de_validite'; // input date
const ACF_PROF_CAN_RENEW = 'renouvellement_possible'; // input true/false
*/
const ACF_CARD_STATE = ['_name'=>'etat_carte', 'new'=>'Commande', 'renew'=>'Renouvellement']; // radio button
const ACF_CARD_PAYMENT_METHOD = ['_name'=>'paiement', 'paypal'=>'Paypal', 'transfert'=>'Virement']; // radio button
const ACF_CARD_PRICE_CHOICE = ['_name'=>'tarif', 'low'=>'10', 'high'=>'15']; // radio button
const ACF_CARD_PRICE_DELIVERY = ['_name'=>'livraison', 'pdf'=>'PDF', 'post'=>'Fabrication']; // radio button
const ACF_PROF_IS_ACTIV = ['_name'=>'compte-actif', 'activ'=>'Actif', 'inactiv'=>'Inactif']; // radio button
const ACF_PROF_CGV = ['_name'=>'cgv', 'cgv'=>'cgv']; // checkbox
const ACF_PROF_CAN_RENEW = ['_name'=>'renouvellement_possible', 'can'=>true, 'cannot'=>false]; // true/false
const ACF_CARD_PRICE_TOTAL = ['_name'=>'somme_a_regler']; // number
const ACF_CARD_CARD_NUMBER = ['_name'=>'numero_de_la_carte']; // number
const ACF_CARD_EXPIRATION = ['_name'=>'fin_de_validite']; // date picker
// META
const META_PAYEMENT_STATUS = 'cipf_payement_status';
const META_ORDER_ID = 'cipf_order_id';
// SLUG
const SLUG_PROF_INACTIV = 'validation-en-cours';
@@ -163,6 +90,12 @@ class PLGNTLS_class
const PAYPAL_MESSAGE_SUCCESS = '`paiement reussi`';
const PAYPAL_MESSAGE_FAILURE = '`paiement raté`';
// ROLES
const ROLE_PROF = 'professeur__professeure';
const ROLE_PARTNER = 'partenaire';
const ROLE_FIPF = 'fipf';
const ROLE_ADMIN = 'administrator';
// OTHER
const CARD_RENEW_PERIOD = 31; // int : number of days before expiration when renew card start to be possible
const CARD_VALIDITY_TIME = '1 year'; // string : time of validity of the card (ex: '1 month' or '1 year' or '60 days')
@@ -170,6 +103,7 @@ class PLGNTLS_class
private static $_DEBUG_INFOS = false;
private static $_root_path;
private static $_root_url;
@@ -210,6 +144,19 @@ class PLGNTLS_class
return(self::$_root_url);
}
/*
* for debug purposes
*/
public static function debug_infos() {
if (self::$_DEBUG_INFOS === false)
return;
$trace = debug_backtrace();
$function = $trace[1]['function'];
$file = $trace[0]['file'];
$line = $trace[0]['line'];
error_log("-debug: function '".$function."' (in ".$file.", line ".$line);
}
public function add_to_front($srcs_arr = null, $vars = null) {
if (!is_array($vars))
@@ -412,7 +359,6 @@ class PLGNTLS_class
wp_add_inline_script($handle, $src->src, 'before');
}
private function add_inline_style($src) {
error_log("inside add_inline_style");
$handle = $src->depends;
if ($handle === null || empty($handle))
$handle = $this->_first_style;