- removed custom user function and put it in custer

- changed main file for cipf instead of fipfcard
- reset some fields after prof form validation
This commit is contained in:
asus
2024-03-17 15:28:08 +01:00
parent eaa45c4ed1
commit a245bd4350
11 changed files with 82 additions and 788 deletions

View File

@@ -1,342 +0,0 @@
<?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!');
}
/*
* SECOND TRY : MODIFY THE ID OF THE FIELDS
*/
/*
* version 3 :
*
* trying with a filter on wp_get_current_user()
* used by form builder to retrieve the user before using get_metadat()
* -> the filter is not called in most cases, if user is instance of WP_USER
*/
//function filter_user_id_CIPF($id) {
// PLGNTLS_class::debug_infos();
// error_log("-in filter_user_id_CIPF, id : " . $id); // this is not output
// return 117;
//}
//function add_filter_for_user_id_CIPF($wp) {
// PLGNTLS_class::debug_infos();
// error_log("-in add_filter_for_user_id_CIPF"); // this is output
// add_filter('determine_current_user', 'filter_user_id_CIPF', 10, 1);
//}
//add_shortcode('cipf_change_meta_id', 'add_filter_for_user_id_CIPF');
////add_action('wp', 'add_filter_for_user_id_CIPF');
/*
* version 2 :
*
* adding a filter on the page to alter the id used by the get_metadata function
* because it seems it is the function used by formBuilder to retrieve the fields
* -> in divi-for-builder/includes/modules/FormField/FormField.php line 5319 : $user_metadata = get_user_meta( $current_user->ID, '', true );
* -> in wp-includes/meta.php line 635
*
* somehow it makes the function get_user_metadata returns nothing
*
*/
//function filter_user_id_CIPF($null, $object_id, $meta_key, $single, $meta_type ) {
// PLGNTLS_class::debug_infos();
// error_log("---");
// error_log("-object_id");
// error_log(json_encode($object_id));
// error_log("-meta_key");
// error_log(json_encode($meta_key));
// error_log("-single");
// error_log(json_encode($single));
// error_log("-meta_type");
// error_log(json_encode($meta_type));
//
// $object_id = 117;
//
// /*
// * I just copied the code of wordpress :
// * i'm not sure there is a better way
// *
// */
// $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
//
// if ( ! $meta_cache ) {
// $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
// if ( isset( $meta_cache[ $object_id ] ) ) {
// $meta_cache = $meta_cache[ $object_id ];
// } else {
// $meta_cache = null;
// }
// }
//
// if ( ! $meta_key ) {
// return $meta_cache;
// }
//
// if ( isset( $meta_cache[ $meta_key ] ) ) {
// if ( $single ) {
// return maybe_unserialize( $meta_cache[ $meta_key ][0] );
// } else {
// return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] );
// }
// }
// /*
// * FIN copy
// */
//}
//function add_filter_for_user_id_CIPF($wp) {
// PLGNTLS_class::debug_infos();
// add_filter('get_user_metadata', 'filter_user_id_CIPF', 10, 5);
//}
//add_action('wp', 'add_filter_for_user_id_CIPF');
/*
* version 1:
*
* adding a shortcode to change the behavior of acf retrieval mechanism
* by changing the filter acf/pre_load_post_id that find the id acf will use
* for all the functions like get_field(), get_field_object(), etc
* it works, but formBuilder don't use acf functions :p
*
* [cipf_choose_acf_id] -> nothing happens
* [cipf_choose_acf_id logged_in] -> the next acf ids will be of the logged-in user
* [cipf_choose_acf_id post_creator] -> the next acf ids will be of the creator of the post
* (for the moment, only for author page)
* [cipf_choose_acf_id off] -> stop the use of specials ids
*
*/
//function test_acf_CIPF($acf_id, $id) {
// PLGNTLS_class::debug_infos();
// error_log("acf_id: " . $acf_id . ", id : " . $id);
// if ($id === null) {
// return null;
// }
// return 'user_'.$id;
//}
//
//function choose_acf_id_CIPF($options) {
// PLGNTLS_class::debug_infos();
//
// $option = reset($options);
// if (!is_string($option)) {
// return;
// }
//
// if ($option === 'logged_in') {
// $id = get_current_user_id();
// }
// else if ($option === 'post_creator') {
// $id = get_queried_object_id();
// }
// else if ($option === 'off') {
// $id = null;
// }
// else
// return;
//
// /*
// * this filter is called by acf to find the id to use
// * with functions like get_field()
// *
// */
// add_filter('acf/pre_load_post_id', function($null, $acf_id) use ($id) {
// return test_acf_CIPF($acf_id, $id);
// }, 10, 2);
//}
//add_shortcode('cipf_choose_acf_id', 'choose_acf_id_CIPF');
/*
* ABANDONNED
*
* FIRST TRY : HANDLE THE FORM SUBMIT
*
* not always simple to get the id of the author
* anyway it does not help to show the acf values on form
*
*/
/*
* custom action in php on custom form submission
* custom forms are not processed by divi form builder
* so they don't change between hooks before and after process
*
*/
//function admin_validate_prof_CIPF($data) {
// PLGNTLS_class::debug_infos();
// $acf_prof_is_activ = PLGNTLS_class::ACF_PROF_IS_ACTIV;
//
// if (empty($data))
// redirection_profil_CIPF();
//
// error_log("---");
// error_log("data");
// error_log(json_encode($data));
//
// if (!isset($data[$acf_prof_is_activ['_name']]))
// redirection_profil_CIPF();
//
// $is_activ = $data[$acf_prof_is_activ['_name']];
// error_log("is_activ");
// error_log($is_activ);
//}
/*
* version 4 :
* calling the action to validate from the url path and/or query
*
*/
//function url_custom_form_admin_validate_prof_CIPF() {
// PLGNTLS_class::debug_infos();
// $slug_admin_validate_prof = PLGNTLS_class::SLUG_ADMIN_VALIDATE_PROF;
//
// if (!isset($_GET['q']))
// return;
//
// /*
// * check the request /path/
// * for url : https://url.com/path/to/file/?query=value
// * -> $_GET: {"q":"/path/to/file/","query":"value"}
// *
// */
// $get_path = trim($_GET['q'], '/');
// if ($get_path !== $slug_admin_validate_prof)
// return;
//
// admin_validate_prof_CIPF($_POST);
//}
//add_action('init', 'url_custom_form_admin_validate_prof_CIPF');
/*
* version 3 :
* calling the action to validate from the FormBuilder hook
*
*/
//function custom_form_admin_validate_prof_CIPF($form_id, $post_array, $form_type) {
// PLGNTLS_class::debug_infos();
// $admin_validate_prof_field = PLGNTLS_class::ADMIN_VALIDATE_PROF_FIELD;
//
// if ($form_type !== 'custom')
// return;
//
// $field_id = $post_array['field_id'];
// $contains_id = in_array($admin_validate_prof_field, $field_id);
// if ($contains_id === false)
// return;
//
// admin_validate_prof_CIPF($post_array);
//}
//add_action('df_before_process', 'custom_form_admin_validate_prof_CIPF', 10, 3);
/*
* version 2 : ABANDONNED
* tried to receive the form in ajax from front with wp REST API
* 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");
// error_log(json_encode($request));
// error_log("_POST");
// 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":""}
//
// // 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::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');
/*
* version 1 : ABANDONNED
* enqueue script that should prevent the form to submit, but does not work
*
*/
//function admin_validate_prof_prevent_CIPF() {
// PLGNTLS_class::debug_infos();
// $handle = 'admin_prevent_submit';
// $url = PLGNTLS_class::root_url() . 'js/admin_prevent_submit.js';
// $dependencies = array();
// $version = null;
// $defer = true;
// wp_enqueue_script($handle, $url, $dependencies, $version, $defer);
//}
//add_action('wp_enqueue_scripts', 'admin_validate_prof_prevent_CIPF');
?>

View File

@@ -1,140 +0,0 @@
<?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!');
}
include_once(PLGNTLS_class::root_path() . 'php/format_user_infos.php');
/*
* callback to provide the user info corresponding to the $$key_word$$
*
*/
function replace_words_CIPF($matches, $user_id = null) {
PLGNTLS_class::debug_infos();
if ($user_id !== null) {
$current_user = get_user_by('id', $user_id);
}
else if (is_user_logged_in()) {
$current_user = wp_get_current_user();
$user_id = get_current_user_id();
}
else {
return "";
}
if ($current_user === false)
return "";
$query = $matches[1];
$result = format_user_info_CIPF($query, $current_user, $user_id);
/*
* if result is array, take the first element (not ideal)
*
*/
if (is_array($result))
$result = reset($result);
/*
* if is special query __author_page__
* return author page url
*
*/
if ($query === '__author_page__') {
$current_user_id = get_current_user_id();
$result = get_author_posts_url($current_user_id);
}
/*
* if no match, return $$<query>$$
*
*/
if (empty($result))
return $matches[0];
return $result;
}
/*
* filter emails in the form-builder hook, before the wp_mail hook
* it receives the id of the user, no need to have the user still logged-in
*
*/
function filter_email_fb_CIPF($reply_body, $post_array) {
PLGNTLS_class::debug_infos();
$id = $post_array['ID'];
// pattern : anything surrounded by '$$', ex : $$value$$
$pattern = '/\$\$(.*?)\$\$/';
// inline callback, with 'use' to get the id
$new_body = preg_replace_callback($pattern, function($matches) use ($id) {
return replace_words_CIPF($matches, $id);
}, $reply_body);
return $new_body;
}
add_filter('df_confirmation_body', 'filter_email_fb_CIPF', 10, 2); // the receive an email
add_filter('df_notification_body', 'filter_email_fb_CIPF', 10, 2); // the admin receive a notification
/*
* filter emails at the final point : the wp_mail hook
* it uses a callback that rely on the logged-in user
* it will works well most of the time, but it's possible
* that a user logged out before the email is sent
* 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 = '/\$\$(.*?)\$\$/';
$old_body = $args['message'];
$new_body = preg_replace_callback($pattern, 'replace_words_CIPF', $old_body);
$args['message'] = $new_body;
return $args;
}
add_filter('wp_mail', 'filter_email_wp_CIPF', 10, 1);
/*
all filters in form_builder :
1 $body = apply_filters( 'df_notification_body', $body, $post_array );
2 $email = apply_filters( 'df_notifcation_recipient', $email, $form_id, $post_array );
3 $title = apply_filters( 'wpml_translate_single_string', $title_get, 'divi-form-builder', 'Edit Post Button Title Text' );
4 $body = apply_filters( 'df_contact_body', $body, $post_array );
5 $body = apply_filters( 'df_contact_body', $body, $post_array );
6 $email = apply_filters( 'df_contact_recipient', $email, $form_id, $post_array );
7 $email = apply_filters( 'df_contact_recipient', $email, $form_id, $processed_post_array );
8 $reply_body = apply_filters( 'df_confirmation_body', $reply_body, $post_array );
9 $reply_body = apply_filters( 'df_confirmation_body', $reply_body, $post_array );
10 $reply_body = apply_filters( 'df_confirmation_body', $reply_body, $post_array );
11 $reply_body = apply_filters( 'df_confirmation_body', $reply_body, $post_array );
12 $message_content = apply_filters( 'the_content', get_post_field( 'post_content', $message_array['layout'] ) );
13 $content = apply_filters( 'the_content', get_post_field( 'post_content', $message_array['layout'] ) );
14 $content = apply_filters( 'the_content', get_post_field( 'post_content', $html_content_divi_layout) );
*/
?>

View File

@@ -1,96 +0,0 @@
<?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!');
}
function format_user_info_CIPF($query, &$current_user, $user_id) {
PLGNTLS_class::debug_infos();
$output_date_format = PLGNTLS_class::USER_INFO_DATE_FORMAT;
$is_acf = false;
/*
* check if it's an acf field
* first method : check if _field exist
*
$_acf_field = '_'.$query;
$acf_field = $current_user->$_acf_field;
if (!empty($acf_field))
$is_acf = true;
*/
/*
* check if it's an acf field, and a date
* second method : check what get_field_object() returns
*
*/
$acf_id = 'user_'.$user_id;
$acf_object = get_field_object($query, $acf_id);
if ($acf_object !== false)
$is_acf = true;
/*
* if is acf, use the acf return format
* otherwise, use the default wordpress value
*
*/
if ($is_acf)
$output = get_field($query, $acf_id);
else
$output = $current_user->$query;
/*
* try to extract a string
*
*/
while (is_array($output) && count($output) === 1)
$output = reset($output);
/*
* if is not acf
* check if is a date
* and format it
* to create a DateTime from a time stamp : https://stackoverflow.com/a/12039058/9497573
*
*/
if (!$is_acf) {
$timestamp = false;
if (is_string($output)) {
$timestamp = strtotime($output);
}
if ($timestamp !== false) {
$date = new DateTime('@' . $timestamp);
$output = $date->format($output_date_format);
}
}
/*
* return the result
*
*/
if (is_array($output) && count($output) === 0)
$output = '';
if (!is_string($output))
$output = json_encode($output, JSON_UNESCAPED_SLASHES);
return esc_html($output);
}
?>

View File

@@ -11,6 +11,63 @@ if (!defined('ABSPATH')) {
/*
* when form is validated, reset some fields
*
*/
function prof_form_reset_fields_CIPF($form_id, $post_array, $form_type) {
PLGNTLS_class::debug_infos();
$acf_cgv = PLGNTLS_class::ACF_PROF_CGV;
$user_id = get_current_user_id();
update_field($acf_cgv['_name'], array(""), 'user_'.$user_id);
}
//add_action('df_before_process', 'prof_form_reset_fields_CIPF', 10, 3);
add_action('df_after_process', 'prof_form_reset_fields_CIPF', 10, 3);
/*
*
* NOT USEFUL ANYMORE :
* it was to fix pbms in formbuilder with calculation field
* but I made 2 better fixes (css and js)
*
* reset some fields for the form to buy the card
* - cgv
* - paiement
* - livraison
* - tarif
* this action is called after redirection hook
*
*/
//function reset_some_fields_CIPF() {
// PLGNTLS_class::debug_infos();
// $slug_renew_card = PLGNTLS_class::SLUG_RENEW_CARD;
// $acf_cgv = PLGNTLS_class::ACF_PROF_CGV;
// $acf_payement = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD;
// $acf_delivery = PLGNTLS_class::ACF_CARD_PRICE_DELIVERY;
// $acf_price = PLGNTLS_class::ACF_CARD_PRICE_CHOICE;
//
// if (!is_page($slug_renew_card))
// return;
//
// $user_id = get_current_user_id();
// update_field($acf_cgv['_name'] , array(""), 'user_'.$user_id);
// update_field($acf_payement['_name'], array(""), 'user_'.$user_id);
// update_field($acf_delivery['_name'], array(""), 'user_'.$user_id);
// update_field($acf_price['_name'] , array(""), 'user_'.$user_id);
//}
//add_action('wp', 'reset_some_fields_CIPF');
/*
* on renew page :
* - check restrictions

View File

@@ -11,38 +11,6 @@ if (!defined('ABSPATH')) {
/*
*
* NOT USEFUL ANYMORE :
* it was to fix pbms in formbuilder with calculation field
* but I made 2 better fixes (css and js)
*
* reset some fields for the form to buy the card
* - cgv
* - paiement
* - livraison
* - tarif
* this action is called after redirection hook
*
*/
function reset_some_fields_CIPF() {
PLGNTLS_class::debug_infos();
$slug_renew_card = PLGNTLS_class::SLUG_RENEW_CARD;
$acf_cgv = PLGNTLS_class::ACF_PROF_CGV;
$acf_payement = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD;
$acf_delivery = PLGNTLS_class::ACF_CARD_PRICE_DELIVERY;
$acf_price = PLGNTLS_class::ACF_CARD_PRICE_CHOICE;
if (!is_page($slug_renew_card))
return;
$user_id = get_current_user_id();
update_field($acf_cgv['_name'] , array(""), 'user_'.$user_id);
update_field($acf_payement['_name'], array(""), 'user_'.$user_id);
update_field($acf_delivery['_name'], array(""), 'user_'.$user_id);
update_field($acf_price['_name'] , array(""), 'user_'.$user_id);
}
add_action('wp', 'reset_some_fields_CIPF');

View File

@@ -1,170 +0,0 @@
<?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!');
}
include_once(PLGNTLS_class::root_path() . 'php/format_user_infos.php');
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);
// if key was not in first array, add the new element to it
if (!isset($new_array[$key2])) {
$new_array[$key2] = $value2;
continue;
}
// if key was in first array, add both in an array
$value1 = extract_if_array_size_one_CIPF($new_array[$key2]);
if (empty($value1))
$new_array[$key2] = $value2;
else if (empty($value2))
$new_array[$key2] = $value1;
else {
$new_value = array($value1, $value2);
$new_array[$key] = $new_value;
}
}
return $new_array;
}
function output_list_front_CIPF($array, $current_user, $user_id) {
PLGNTLS_class::debug_infos();
$output = '<ul>';
foreach ($array as $key => $value) {
if (str_starts_with($key, '_'))
continue ;
$value = format_user_info_CIPF($key, $current_user, $user_id);
$output .= '<li>';
$output .= '<span>';
$output .= $key;
$output .= ' : ';
$output .= $value;
$output .= '</span>';
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
/*
* shortcode to write user info of post author
* 0 or 1 argument, usage :
* - [cipf_user_info] -> list of all availables infos
* - [cipf_user_info user_email] -> display the email
* - [cipf_user_info user_email user_login] -> display the email
* - [cipf_user_info user_email author='logged_in'] -> display the email of the connected user
* - [cipf_user_info user_email author='post_creator'] -> display the email of the creator of the page/post
*
*/
function current_user_infos_CIPF($atts) {
PLGNTLS_class::debug_infos();
if (!is_user_logged_in())
return ;
/*
* choose the default id target : logged in user, or post creator ?
*
$author_is = 'logged_in'; // logged in user
*/
$author_is = 'post_creator'; // creator of post (also for author pages)
/*
* has parameter 'author' ?
* if yes, removes it from $atts
*
*/
if (is_array($atts)) {
if (isset($atts['author'])) {
$author_is = $atts['author'];
unset($atts['author']);
}
}
/*
* should output all or a specific parameter ?
*
*/
$output_all = false;
if (empty($atts))
$output_all = true;
else if (count($atts) === 0)
$output_all = true;
/*
* get author id outside loop and outside singular page : https://wordpress.stackexchange.com/q/65548
*
*/
if ($author_is === 'logged_in') {
$current_user = wp_get_current_user();
$user_id = get_current_user_id();
}
else {
if (is_author()) {
$user_id = get_queried_object_id();
}
else if (in_the_loop()) {
$user_id = get_the_author_meta('ID');
}
else if (is_singular()) {
$user_id = get_queried_object()->post_author;
}
else {
global $wp_query;
if (!empty($wp_query->posts))
$user_id = $wp_query->posts[0]->post_author;
}
//$current_user = new WP_User($user_id);
$current_user = get_user_by('id', $user_id);
}
/*
* output all the available parameters (for help)
*
*/
if ($output_all) {
$user_properties = (array) get_userdata($user_id)->data;
$user_metas = get_user_meta($user_id);
$user_infos = merge_two_arrays_CIPF($user_metas, $user_properties);
return output_list_front_CIPF($user_infos, $current_user, $user_id);
}
/*
* real purpose of this shortcode :
* only return the first argument (that is not 'author')
*
*/
if (is_array($atts))
$query = $atts[0];
else if (is_string($atts))
$query = $atts;
else
return '';
// $output = $current_user->$query;
return format_user_info_CIPF($query, $current_user, $user_id);
}
add_shortcode('cipf_user_info', 'current_user_infos_CIPF');
?>