updated acf find non init field value with new technique to init with temp value first

This commit is contained in:
asus
2024-03-19 20:15:26 +01:00
parent cbfbfabd18
commit 58d02708b9
8 changed files with 154 additions and 36 deletions

View File

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

View File

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

View File

@@ -0,0 +1,94 @@
<?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!');
}
/*
* early checks on payment page
*
*/
function payment_page_checks_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 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');
?>

View File

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

View File

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

View File

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

View File

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

View File

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