change author id works for prof profil and prof form

This commit is contained in:
asus
2024-03-18 12:23:27 +01:00
parent 82b6593dd9
commit 98dfb17bf4
6 changed files with 165 additions and 36 deletions

View File

@@ -13,7 +13,7 @@ if (!defined('ABSPATH')) {
function toggle_menu($menu_page_title, $menu_title, $menu_capability, $menu_slug, $menu_callback) { function toggle_menu($menu_page_title, $menu_title, $menu_capability, $menu_slug, $menu_callback) {
$toggle_menu = Custer::TOGGLE_ADMIN_MENU; $toggle_menu = Custer::OPTION_TOGGLE_MENU;
if (false === get_option($toggle_menu['_name'])) { if (false === get_option($toggle_menu['_name'])) {
add_option($toggle_menu['_name'], $toggle_menu['hide']); add_option($toggle_menu['_name'], $toggle_menu['hide']);
@@ -40,7 +40,7 @@ function toggle_menu($menu_page_title, $menu_title, $menu_capability, $menu_slug
*/ */
function add_link_to_custer_plugin($links) { function add_link_to_custer_plugin($links) {
$slug_toggle = Custer::SLUG_TOOGLE_ADMIN_MENU; $slug_toggle = Custer::SLUG_TOOGLE_ADMIN_MENU;
$toggle_menu = Custer::TOGGLE_ADMIN_MENU; $toggle_menu = Custer::OPTION_TOGGLE_MENU;
$toggle = get_option($toggle_menu['_name']); $toggle = get_option($toggle_menu['_name']);
@@ -63,7 +63,7 @@ add_filter('plugin_action_links_custer/custer.php', __NAMESPACE__.'\add_link_to_
*/ */
function toggle_custer_plugin_menu() { function toggle_custer_plugin_menu() {
$slug_toggle = Custer::SLUG_TOOGLE_ADMIN_MENU; $slug_toggle = Custer::SLUG_TOOGLE_ADMIN_MENU;
$toggle_menu = Custer::TOGGLE_ADMIN_MENU; $toggle_menu = Custer::OPTION_TOGGLE_MENU;
global $wp; global $wp;
$current_slug = $wp->request; $current_slug = $wp->request;

View File

@@ -17,40 +17,127 @@ if (!defined('ABSPATH')) {
* the only option is to force the author * the only option is to force the author
* -> there is no sens in forcing the logged_in, because it's already the default * -> there is no sens in forcing the logged_in, because it's already the default
* *
* [custer_change_id] -> default (author) * [custer_author_id] -> give current user the id of author
* [custer_change_id author] -> author * [custer_author_id set_anchor='anchor_name'] -> create anchor for author user
* [custer_change_id off] -> reset to logged_in * [custer_author_id anchor='anchor_name'] -> give current user the id stored in the anchor_name
* [custer_author_id off] -> reset to logged_in
* *
*/ */
function shortcode_change_id($options) { function shortcode_author_id($options) {
$anchor_name = '';
$option = '';
$is_set_anchor = false;
/* /*
* set the default value for option * set option value :
* currently it can only be author * 'off', 'author', 'set_anchor', or 'anchor'
* *
*/ */
$option = 'author'; if (empty($options)) {
if (is_array($options)) { $option = 'author';
$option = reset($options); }
if (isset($options['set_anchor'])) {
$option = 'author';
$is_set_anchor = true;
$anchor_name = $options['set_anchor'];
if (empty($anchor_name)) {
return;
}
unset($options['set_anchor']);
}
if (isset($options['anchor'])) {
if ($is_set_anchor) {
return;
}
$option = 'anchor';
$anchor_name = $options['anchor'];
if (empty($anchor_name)) {
return;
}
unset($options['anchor']);
}
if (in_array('off', $options)) {
$option = 'off';
} }
if (!is_string($option)) { if (empty($option)) {
return; return;
} }
/*
* find id according to option
* if option is set_anchor, it sets it and return 0
*
*/
$id = \CUSTER\find_id_wih_option($option, $anchor_name);
if ($id === 0) {
return;
}
/*
* set current user
*
*/
if ($is_set_anchor) {
\CUSTER\create_anchor($anchor_name, $id);
}
else {
wp_set_current_user($id);
}
}
add_shortcode('custer_author_id', __NAMESPACE__.'\shortcode_author_id');
function create_anchor($anchor_name, $id) {
$option_anchor = Custer::OPTION_ANCHOR;
/*
* if needed, create the option
*
*/
if (false === get_option($option_anchor)) {
add_option($option_anchor, array());
}
/*
* add or update the id for the anchor name
*
*/
$anchors = get_option($option_anchor);
$anchors[$anchor_name] = $id;
update_option($option_anchor, $anchors);
}
/*
* find ids according to options :
* - author : change the current user to the author of the page or post
* - anchor : uses the id stored in options with anchor_name
* - off : reset current user to the id before the changes
*
*/
function find_id_wih_option($option, $anchor_name) {
if ($option === 'author') { if ($option === 'author') {
$id = \CUSTER\get_author_id(); $id = \CUSTER\get_author_id();
Custer::set_current_user_backup(); Custer::set_current_user_backup();
wp_set_current_user($id); }
else if ($option === 'anchor') {
$id = \CUSTER\get_anchor_id($anchor_name);
Custer::set_current_user_backup();
} }
else if ($option === 'off') { else if ($option === 'off') {
$id = Custer::reset_current_user_backup(); $id = Custer::reset_current_user_backup();
wp_set_current_user($id);
} }
else return $id;
return;
} }
add_shortcode('custer_change_id', __NAMESPACE__.'\shortcode_change_id');

View File

@@ -4,7 +4,7 @@ Plugin Name: custer_plugin
Plugin URI: Plugin URI:
Description: customize user : output infos on page, on email, and change current user id momentarly Description: customize user : output infos on page, on email, and change current user id momentarly
Author: hugogogo Author: hugogogo
Version: 0.1.1 Version: 0.1.3
Author URI: Author URI:
*/ */
@@ -18,7 +18,7 @@ if (!defined('ABSPATH')) {
include_once(plugin_dir_path(__FILE__) . '/custer_class.php'); include_once(plugin_dir_path(__FILE__) . '/custer_class.php');
include_once(plugin_dir_path(__FILE__) . '/author_id.php'); include_once(plugin_dir_path(__FILE__) . '/get_user_id.php');
include_once(plugin_dir_path(__FILE__) . '/change_id.php'); include_once(plugin_dir_path(__FILE__) . '/change_id.php');
include_once(plugin_dir_path(__FILE__) . '/format_user_infos.php'); include_once(plugin_dir_path(__FILE__) . '/format_user_infos.php');
include_once(plugin_dir_path(__FILE__) . '/user_infos.php'); include_once(plugin_dir_path(__FILE__) . '/user_infos.php');

View File

@@ -19,7 +19,8 @@ class Custer {
const USER_INFO_DATE_FORMAT = 'd/m/Y'; // for user_infos.php (date format : https://www.php.net/manual/fr/datetime.format.php) const USER_INFO_DATE_FORMAT = 'd/m/Y'; // for user_infos.php (date format : https://www.php.net/manual/fr/datetime.format.php)
const SLUG_TOOGLE_ADMIN_MENU = ['_name'=>'toogle_admin_menu_url_custer', 'toggle'=>'toggle', 'show'=>'show', 'hide'=>'hide']; const SLUG_TOOGLE_ADMIN_MENU = ['_name'=>'toogle_admin_menu_url_custer', 'toggle'=>'toggle', 'show'=>'show', 'hide'=>'hide'];
const TOGGLE_ADMIN_MENU = ['_name'=>'toggle_admin_menu_option_custer', 'show'=>'show', 'hide'=>'hide']; const OPTION_TOGGLE_MENU = ['_name'=>'toggle_admin_menu_option_custer', 'show'=>'show', 'hide'=>'hide'];
const OPTION_ANCHOR = 'custer_anchor_ids';
private static $_backup_current_user = null; private static $_backup_current_user = null;
@@ -38,6 +39,9 @@ class Custer {
} }
public static function reset_current_user_backup() { public static function reset_current_user_backup() {
$backup_user = self::$_backup_current_user; $backup_user = self::$_backup_current_user;
if ($backup_user === null) {
$backup_user = get_current_user_id();
}
self::$_backup_current_user = null; self::$_backup_current_user = null;
return $backup_user; return $backup_user;
} }

View File

@@ -57,22 +57,22 @@ function replace_words($matches, $user_id = null) {
* filter emails in the form-builder hook, before the wp_mail hook * 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 * 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
*/ */
//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', __NAMESPACE__.'\filter_email_fb_CIPF', 10, 2); // the receive an email
//add_filter('df_notification_body', __NAMESPACE__.'\filter_email_fb_CIPF', 10, 2); // the admin receive a notification
@@ -87,10 +87,15 @@ function filter_email_wp($args) {
// pattern : anything surrounded by '$$', ex : $$value$$ // pattern : anything surrounded by '$$', ex : $$value$$
$pattern = '/\$\$(.*?)\$\$/'; $pattern = '/\$\$(.*?)\$\$/';
error_log("inside wp_mail hook, in filter_email_wp");
$old_body = $args['message']; $old_body = $args['message'];
$new_body = preg_replace_callback($pattern, __NAMESPACE__.'\replace_words', $old_body); $new_body = preg_replace_callback($pattern, __NAMESPACE__.'\replace_words', $old_body);
$args['message'] = $new_body; $args['message'] = $new_body;
error_log("args['message']");
error_log(json_encode($args['message']));
return $args; return $args;
} }
add_filter('wp_mail', __NAMESPACE__.'\filter_email_wp', 10, 1); add_filter('wp_mail', __NAMESPACE__.'\filter_email_wp', 10, 1);

View File

@@ -39,4 +39,37 @@ function get_author_id() {
/*
* get the anker user id
* return 0 if not found
*
*/
function get_anchor_id($anchor_name) {
$option_anchor = Custer::OPTION_ANCHOR;
error_log("anchor_name:");
error_log($anchor_name);
$anchors = get_option($option_anchor);
error_log("anchors:");
error_log(json_encode($anchors));
if ($anchors === false) {
error_log("anchors === false");
return 0;
}
if (isset($anchors[$anchor_name])) {
$user_id = $anchors[$anchor_name];
}
if (empty($user_id)) {
return 0;
}
return $user_id;
}
?> ?>