diff --git a/plugins/cipf_plugin/fipfcard_plugin.php b/plugins/cipf_plugin/fipfcard_plugin.php index 7b62402..49b8e52 100644 --- a/plugins/cipf_plugin/fipfcard_plugin.php +++ b/plugins/cipf_plugin/fipfcard_plugin.php @@ -4,7 +4,7 @@ Plugin Name: cipf_plugin Plugin URI: Description: test avec page de redirection Author: hugogogo -Version: 0.2.7.1 +Version: 0.2.8 Author URI: */ diff --git a/plugins/cipf_plugin/php/admin_modif_prof.php b/plugins/cipf_plugin/php/admin_modif_prof.php index d674de2..62f8748 100644 --- a/plugins/cipf_plugin/php/admin_modif_prof.php +++ b/plugins/cipf_plugin/php/admin_modif_prof.php @@ -11,103 +11,235 @@ if (!defined('ABSPATH')) { - -// Hook into the ACF field retrieval process /* -function test_acfs_CIPF($fields, $id) { - error_log("--- fields"); - - error_log("fields"); - error_log(json_encode($fields)); - error_log("id"); - error_log($id); - - return $fields; -} -function test_acf_CIPF($field, $id) { - error_log("--- " . $field['name']); - if ($field['name'] !== 'compte-actif') - return $field; - - error_log("acf hook"); - error_log("field"); - error_log(json_encode($field)); - error_log("_GET"); - error_log(json_encode($_GET)); - -// // Check if we are targeting a specific ACF field and user -// if ($field_key === 'your_field_key' && isset($_GET['user_id'])) { -// $user_id = $_GET['user_id']; // Get the user ID from the query parameters -// -// // Retrieve the ACF field object for the specified user -// $user_field = get_field_object($field_key, 'user_' . $user_id); -// -// if ($user_field) { -// return $user_field; // Return the ACF field object for the specified user -// } -// } -// -// // If not targeting a specific user, return the original field - return $field; -} -add_filter('acf/pre_render_field', 'test_acf_CIPF', 10, 2); -add_filter('acf/pre_render_fields', 'test_acfs_CIPF', 10, 2); -*/ -/* -add_filter('acf/load_field', 'test_acf_CIPF'); -*/ -/* -{"ID":40341,"key":"field_65e8acd5c2065","label":"Activation du compte","name":"compte-actif","aria-label":"","prefix":"acf","type":"radio","value":null,"menu_order":1,"instructions":"","required":0,"id":"","class":"","conditional_logic":0,"parent":34538,"wrapper":{"width":"","class":"","id":""},"choices":{"Actif":"Actif","Inactif":"Inactif"},"default_value":"","return_format":"value","allow_null":0,"other_choice":0,"layout":"vertical","save_other_choice":0,"_name":"compte-actif","_valid":1} +* SECOND TRY : MODIFY THE ID OF THE FIELDS */ - -function test_acf_CIPF($acf_id, $id) { +/* +* version 4 : +* modifying directly the current user : very hardcore, and need to store it +*/ +function shortcode_choose_meta_id_CIPF ($options) { PLGNTLS_class::debug_infos(); - error_log("acf_id: " . $acf_id . ", id : " . $id); - if ($id === null) { - return null; - } - return 'user_'.$id; -} +// $role_fipf = PLGNTLS_class::ROLE_FIPF; +// $role_admin = PLGNTLS_class::ROLE_ADMIN; +// +// $current_user = wp_get_current_user(); +// +// $allowed_roles = array($role_admin, $role_fipf); +// if (!array_intersect($allowed_roles, $current_user->roles)) +// return; -function choose_acf_id_CIPF($options) { - PLGNTLS_class::debug_infos(); - $shortcode_choose_acf_id = PLGNTLS_class::SHORTCODE_CHOOSE_ACF_ID; $option = reset($options); if (!is_string($option)) { return; } - if ($option === $shortcode_choose_acf_id['logged_in']) { + if ($option === 'logged_in') { $id = get_current_user_id(); + error_log("logged_in id : " . $id); + PLGNTLS_class::set_current_user_backup(); + wp_set_current_user($id); } - else if ($option === $shortcode_choose_acf_id['post_creator']) { + else if ($option === 'post_creator') { $id = get_queried_object_id(); + error_log("post_creator id : " . $id); + PLGNTLS_class::set_current_user_backup(); + wp_set_current_user($id); } - else if ($option === $shortcode_choose_acf_id['off']) { - $id = null; + else if ($option === 'off') { + $id = PLGNTLS_class::reset_current_user_backup(); + error_log("reset id : " . $id); + wp_set_current_user($id); } 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_action('set_current_user', 'force_set_user_CIPF'); } -add_shortcode('cipf_choose_acf_id', 'choose_acf_id_CIPF'); +add_shortcode('cipf_change_meta_id', 'shortcode_choose_meta_id_CIPF'); -// $new_body = preg_replace_callback($pattern, function($matches) use ($id) { -// return replace_words_CIPF($matches, $id); + +/* +* version 3 : +* +* trying with a filter on wp_get_current_user() +* used by form builder to retrieve the user before using get_metadat() +* -> somehow the filter is never called +*/ +//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_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 +* +*/ @@ -118,54 +250,32 @@ add_shortcode('cipf_choose_acf_id', 'choose_acf_id_CIPF'); * 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); -} +//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 3 : ABANDONNED -* 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 4 : -* calling the action to validate from the url query +* calling the action to validate from the url path and/or query * */ //function url_custom_form_admin_validate_prof_CIPF() { @@ -191,35 +301,28 @@ add_action('df_before_process', 'custom_form_admin_validate_prof_CIPF', 10, 3); + /* - -_POST : -{"field_title":["\u00c9tat du compte"],"field_name":["compte-actif"],"field_id":["cipf_admin_activate_prof"],"compte-actif":"Inactif","form_key":"40781-1","unique_id":"91eed9aa-2b92-4da7-a0b4-e94f24515223","form_type":"custom","divi-form-submit":"yes","form_id":"","form_type_confirm":""} -{ - "field_title":["\u00c9tat du compte"], - "field_name":["compte-actif"], - "field_id":["cipf_admin_activate_prof"], - "compte-actif":"Inactif", -- "form_key":"40781-1", -- "unique_id":"91eed9aa-2b92-4da7-a0b4-e94f24515223", -- "form_type":"custom", -- "divi-form-submit":"yes", -- "form_id":"", - "form_type_confirm":"" -} - ---- - -post_array : -{"field_title":["\u00c9tat du compte"],"field_name":["compte-actif"],"field_id":["cipf_admin_activate_prof"],"compte-actif":"Inactif","form_type_confirm":""} -{ - "field_title":["\u00c9tat du compte"], - "field_name":["compte-actif"], - "field_id":["cipf_admin_activate_prof"], - "compte-actif":"Inactif", - "form_type_confirm":"" -} +* 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); + diff --git a/plugins/cipf_plugin/utils/plgntls_class.php b/plugins/cipf_plugin/utils/plgntls_class.php index 08f05a3..e5b551d 100644 --- a/plugins/cipf_plugin/utils/plgntls_class.php +++ b/plugins/cipf_plugin/utils/plgntls_class.php @@ -101,7 +101,6 @@ class PLGNTLS_class const ROLE_ADMIN = 'administrator'; // SHORTCODES - const SHORTCODE_CHOOSE_ACF_ID = ['logged_in'=>'logged_in', 'post_creator'=>'post_creator', 'off'=>'off']; // OTHER const CARD_RENEW_PERIOD = 31; // int : number of days before expiration when renew card start to be possible @@ -118,6 +117,7 @@ class PLGNTLS_class private static $_file_name; private static $_root_path; private static $_root_url; + private static $_backup_current_user = null; private $_first_script; private $_first_style; @@ -189,8 +189,33 @@ class PLGNTLS_class return(self::$_root_url); } + + + + /* + * setter and getter for current user backup + * + */ + public static function set_current_user_backup() { + if (self::$_backup_current_user !== null) + return; + self::$_backup_current_user = get_current_user_id(); + } + public static function get_current_user_backup() { + return self::$_backup_current_user; + } + public static function reset_current_user_backup() { + $backup_user = self::$_backup_current_user; + self::$_backup_current_user = null; + return $backup_user; + } + + + + /* * for debug purposes + * */ public static function debug_infos() { if (self::$_DEBUG_INFOS === false)