From 58d02708b9eec8ce7afe37dd2bae8ec1f83fa10c Mon Sep 17 00:00:00 2001 From: asus Date: Tue, 19 Mar 2024 20:15:26 +0100 Subject: [PATCH] updated acf find non init field value with new technique to init with temp value first --- plugins/cipf_plugin/cipf_plugin.php | 2 +- ...{profs_display_css.php => display_css.php} | 38 ++++---- .../cipf_plugin/php/paypal/payment_page.php | 94 +++++++++++++++++++ plugins/cipf_plugin/php/paypal/paypal.php | 1 + plugins/cipf_plugin/php/profs_form.php | 12 +-- plugins/cipf_plugin/php/profs_profil.php | 4 +- plugins/cipf_plugin/php/profs_states.php | 38 +++++++- plugins/cipf_plugin/utils/plgntls_class.php | 1 + 8 files changed, 154 insertions(+), 36 deletions(-) rename plugins/cipf_plugin/php/{profs_display_css.php => display_css.php} (66%) create mode 100644 plugins/cipf_plugin/php/paypal/payment_page.php diff --git a/plugins/cipf_plugin/cipf_plugin.php b/plugins/cipf_plugin/cipf_plugin.php index cdd745c..784289e 100644 --- a/plugins/cipf_plugin/cipf_plugin.php +++ b/plugins/cipf_plugin/cipf_plugin.php @@ -45,9 +45,9 @@ include_once(PLGNTLS_class::root_path() . 'php/profs_profil.php'); include_once(PLGNTLS_class::root_path() . 'php/profs_form.php'); include_once(PLGNTLS_class::root_path() . 'php/profs_dates.php'); include_once(PLGNTLS_class::root_path() . 'php/profs_states.php'); -include_once(PLGNTLS_class::root_path() . 'php/profs_display_css.php'); include_once(PLGNTLS_class::root_path() . 'php/partners_register.php'); include_once(PLGNTLS_class::root_path() . 'php/partners_page.php'); +include_once(PLGNTLS_class::root_path() . 'php/display_css.php'); // form builder patch : diff --git a/plugins/cipf_plugin/php/profs_display_css.php b/plugins/cipf_plugin/php/display_css.php similarity index 66% rename from plugins/cipf_plugin/php/profs_display_css.php rename to plugins/cipf_plugin/php/display_css.php index 7eb45cb..b687e82 100644 --- a/plugins/cipf_plugin/php/profs_display_css.php +++ b/plugins/cipf_plugin/php/display_css.php @@ -11,7 +11,7 @@ if (!defined('ABSPATH')) { -function prof_display_css_CIPF() { +function display_page_css_CIPF($user_id = null) { $css_for_states = array(); /* @@ -21,18 +21,18 @@ function prof_display_css_CIPF() { * - 'echec' * - 'aucun' */ - if (is_payment_success_CIPF()) { + if (is_payment_success_CIPF($user_id)) { $css_for_states[] = 'css/display_states/paiement_reussi.css'; - set_payment_nothing_CIPF(); + set_payment_nothing_CIPF($user_id); } - else if (is_payment_failure_CIPF()) { + else if (is_payment_failure_CIPF($user_id)) { $css_for_states[] = 'css/display_states/paiement_echec.css'; - set_payment_nothing_CIPF(); + set_payment_nothing_CIPF($user_id); } - else if (is_payment_started_CIPF()) { + else if (is_payment_started_CIPF($user_id)) { $css_for_states[] = 'css/display_states/paiement_en_cours.css'; } - else if (is_payment_nothing_CIPF()) { + else if (is_payment_nothing_CIPF($user_id)) { $css_for_states[] = 'css/display_states/paiement_aucun.css'; } @@ -45,22 +45,22 @@ function prof_display_css_CIPF() { * 5. waiting_valid . 'en attente valide' * 6. expired . 'carte expiree' */ - if (is_account_new_CIPF()) { + if (is_account_new_CIPF($user_id)) { $css_for_states[] = 'css/display_states/compte_nouveau_prof.css'; } - else if (is_account_to_pay_CIPF()) { + else if (is_account_to_pay_CIPF($user_id)) { $css_for_states[] = 'css/display_states/compte_doit_payer.css'; } - else if (is_account_valid_CIPF()) { + else if (is_account_valid_CIPF($user_id)) { $css_for_states[] = 'css/display_states/compte_carte_valide.css'; } - else if (is_account_waiting_invalid_CIPF()) { + else if (is_account_waiting_invalid_CIPF($user_id)) { $css_for_states[] = 'css/display_states/compte_en_attente_invalide.css'; } - else if (is_account_waiting_valid_CIPF()) { + else if (is_account_waiting_valid_CIPF($user_id)) { $css_for_states[] = 'css/display_states/compte_en_attente_valide.css'; } - else if (is_account_expired_CIPF()) { + else if (is_account_expired_CIPF($user_id)) { $css_for_states[] = 'css/display_states/compte_carte_expiree.css'; } @@ -70,10 +70,10 @@ function prof_display_css_CIPF() { * - 'Renouvellement' * */ - if (is_card_new_CIPF()) { + if (is_card_new_CIPF($user_id)) { $css_for_states[] = 'css/display_states/carte_commande.css'; } - else if (is_card_renew_CIPF()) { + else if (is_card_renew_CIPF($user_id)) { $css_for_states[] = 'css/display_states/carte_renouvellement.css'; } @@ -83,15 +83,15 @@ function prof_display_css_CIPF() { * - 'Virement' * */ - if (is_payment_method_paypal_CIPF()) { + if (is_payment_method_paypal_CIPF($user_id)) { $css_for_states[] = 'css/display_states/type_paypal.css'; } - else if (is_payment_method_transfert_CIPF()) { + else if (is_payment_method_transfert_CIPF($user_id)) { $css_for_states[] = 'css/display_states/type_virement.css'; } - - return $css_for_states; + $cipf_states = new PLGNTLS_class(); + $cipf_states->add_to_front($css_for_states); } diff --git a/plugins/cipf_plugin/php/paypal/payment_page.php b/plugins/cipf_plugin/php/paypal/payment_page.php new file mode 100644 index 0000000..96ab4d1 --- /dev/null +++ b/plugins/cipf_plugin/php/paypal/payment_page.php @@ -0,0 +1,94 @@ +request; + if ($current_slug !== $slug_paypal_page) { + return; + } + + // get the user id + $user_id = get_current_user_id(); + + // do checks here +} +add_action('wp', 'payment_page_checks_CIPF'); + + + + +/* +* if profil needs redirection, it happens here +* +*/ +function payment_page_redirects_CIPF() { + PLGNTLS_class::debug_infos(); + $slug_paypal_page = PLGNTLS_class::SLUG_PAYPAL_PAGE; + + // check the slug + global $wp; + $current_slug = $wp->request; + if ($current_slug !== $slug_paypal_page) { + return; + } + + // get the user id + $user_id = get_current_user_id(); + + // do redirections here + +} +add_action('template_redirect', 'payment_page_redirects_CIPF'); + + + + +/* +* time to upload some scripts and styles on prof profil page +* +*/ +function payment_page_scripts_CIPF() { + PLGNTLS_class::debug_infos(); + $slug_paypal_page = PLGNTLS_class::SLUG_PAYPAL_PAGE; + + // check the slug + global $wp; + $current_slug = $wp->request; + if ($current_slug !== $slug_paypal_page) { + return; + } + + // get the user id + $user_id = get_current_user_id(); + + // enqueue files here + display_page_css_CIPF($user_id); +} +add_action('wp_enqueue_scripts', 'payment_page_scripts_CIPF'); + + + + +?> + diff --git a/plugins/cipf_plugin/php/paypal/paypal.php b/plugins/cipf_plugin/php/paypal/paypal.php index 2ce3cb4..0f0de44 100644 --- a/plugins/cipf_plugin/php/paypal/paypal.php +++ b/plugins/cipf_plugin/php/paypal/paypal.php @@ -17,6 +17,7 @@ include_once(PLGNTLS_class::root_path() . '/php/paypal/shortcode.php'); include_once(PLGNTLS_class::root_path() . '/php/paypal/route_api_orders.php'); include_once(PLGNTLS_class::root_path() . '/php/paypal/route_api_orders_capture.php'); include_once(PLGNTLS_class::root_path() . '/php/paypal/routes.php'); +include_once(PLGNTLS_class::root_path() . '/php/paypal/payment_page.php'); diff --git a/plugins/cipf_plugin/php/profs_form.php b/plugins/cipf_plugin/php/profs_form.php index fde91f1..ed5d97d 100644 --- a/plugins/cipf_plugin/php/profs_form.php +++ b/plugins/cipf_plugin/php/profs_form.php @@ -158,18 +158,8 @@ function renew_page_filter_message_CIPF(){ return; $user_id = get_current_user_id(); - $acf_id = 'user_'.$user_id; - $cipf_renew = new PLGNTLS_class(); - - $card_state = get_field($acf_card_state['_name'], $acf_id); - - if ($card_state === 'Renouvellement') { - $cipf_renew->add_to_front(array( - array( 'css' => 'div#cipf_prof_carte_renouvellement {display: block;}' ), - array( 'css' => 'div#cipf_prof_carte_commande {display: none;}' ), - )); - } + display_page_css_CIPF($user_id); } add_action('wp_enqueue_scripts', 'renew_page_filter_message_CIPF'); diff --git a/plugins/cipf_plugin/php/profs_profil.php b/plugins/cipf_plugin/php/profs_profil.php index 7889b7d..e12227a 100644 --- a/plugins/cipf_plugin/php/profs_profil.php +++ b/plugins/cipf_plugin/php/profs_profil.php @@ -175,10 +175,8 @@ function prof_profil_scripts_CIPF() { // the way to find the id of the author of an author_page $author_id = get_queried_object_id(); - $cipf_prof = new PLGNTLS_class(); - $css_front_states = prof_display_css_CIPF(); - $cipf_prof->add_to_front($css_front_states); + display_page_css_CIPF($author_id); } add_action('wp_enqueue_scripts', 'prof_profil_scripts_CIPF', 11); diff --git a/plugins/cipf_plugin/php/profs_states.php b/plugins/cipf_plugin/php/profs_states.php index 8a3a44b..5c67afb 100644 --- a/plugins/cipf_plugin/php/profs_states.php +++ b/plugins/cipf_plugin/php/profs_states.php @@ -42,6 +42,32 @@ if (!defined('ABSPATH')) { +function get_field_init_CIPF($acf_field, $acf_id) { + $acf_state = get_field($acf_field['_name'], $acf_id); + if ($acf_state !== null) { + return $acf_state; + } + + /* + * if get_field returns null, it means it is not initialized + * - initialize it with 'temp' value + * - then find it's default value, and update with it + * - if no default value, update with first value + * + */ + update_field($acf_field['_name'], 'temps', $acf_id); + $acf_object = get_field_object($acf_field['_name'], $acf_id); + $default = $acf_object['default_value']; + if (empty($default)) { + $choices = $acf_object['choices']; + $default = reset($choices); + } + update_field($acf_field['_name'], $default, $acf_id); + $acf_state = get_field($acf_field['_name'], $acf_id); + + return $acf_state; +} + /* @@ -58,10 +84,18 @@ function is_acf_state_CIPF($user_id = null, $acf_field, $state_name) { /* * when acf fields have not been initated a first time, you can't find them by name - * so use key instead + * - one solution is to use key instead + * but it means knowing the key, in my case it prevents fabien to create a field himself + * - another solution would be to : + * try if is null, + * if yes it means it was not initalize, + * then initalize with any value, + * find the default value, + * and assign it * - */ $acf_state = get_field($acf_field['_key'], $acf_id); + */ + $acf_state = get_field_init_CIPF($acf_field, $acf_id);; if ($acf_state === $acf_field[$state_name]) { return true; diff --git a/plugins/cipf_plugin/utils/plgntls_class.php b/plugins/cipf_plugin/utils/plgntls_class.php index 7f61e9c..47fecbe 100644 --- a/plugins/cipf_plugin/utils/plgntls_class.php +++ b/plugins/cipf_plugin/utils/plgntls_class.php @@ -80,6 +80,7 @@ class PLGNTLS_class { const SLUG_PROF_INACTIV = 'validation-en-cours'; const SLUG_RENEW_CARD = 'commande'; const SLUG_PAGE_REDIRECTION = 'redirection_cipf'; + const SLUG_PAYPAL_PAGE = 'paiement'; const SLUG_PAYPAL_REDIRECTION_SUCCESS = self::SLUG_PAGE_REDIRECTION; const SLUG_PAYPAL_REDIRECTION_FAILURE = self::SLUG_PAGE_REDIRECTION; const SLUG_ADMIN_VALIDATE_PROF = 'admin_activate_prof_cipf'; // for admin_modif_prof.php