diff --git a/plugins/custer/admin_menu_toggle.php b/plugins/custer/admin_menu_toggle.php index bbf5e07..89eeff2 100644 --- a/plugins/custer/admin_menu_toggle.php +++ b/plugins/custer/admin_menu_toggle.php @@ -13,7 +13,7 @@ if (!defined('ABSPATH')) { 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'])) { 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) { $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']); @@ -63,7 +63,7 @@ add_filter('plugin_action_links_custer/custer.php', __NAMESPACE__.'\add_link_to_ */ function toggle_custer_plugin_menu() { $slug_toggle = Custer::SLUG_TOOGLE_ADMIN_MENU; - $toggle_menu = Custer::TOGGLE_ADMIN_MENU; + $toggle_menu = Custer::OPTION_TOGGLE_MENU; global $wp; $current_slug = $wp->request; diff --git a/plugins/custer/change_id.php b/plugins/custer/change_id.php index ee920b6..14295e5 100644 --- a/plugins/custer/change_id.php +++ b/plugins/custer/change_id.php @@ -17,40 +17,127 @@ if (!defined('ABSPATH')) { * the only option is to force the author * -> there is no sens in forcing the logged_in, because it's already the default * -* [custer_change_id] -> default (author) -* [custer_change_id author] -> author -* [custer_change_id off] -> reset to logged_in +* [custer_author_id] -> give current user the id of author +* [custer_author_id set_anchor='anchor_name'] -> create anchor for author user +* [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 - * currently it can only be author + * set option value : + * 'off', 'author', 'set_anchor', or 'anchor' * */ - $option = 'author'; - if (is_array($options)) { - $option = reset($options); + if (empty($options)) { + $option = 'author'; + } + 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; } + /* + * 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') { $id = \CUSTER\get_author_id(); 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') { $id = Custer::reset_current_user_backup(); - wp_set_current_user($id); } - else - return; + return $id; } -add_shortcode('custer_change_id', __NAMESPACE__.'\shortcode_change_id'); diff --git a/plugins/custer/custer.php b/plugins/custer/custer.php index 0e51081..7d40b3e 100644 --- a/plugins/custer/custer.php +++ b/plugins/custer/custer.php @@ -4,7 +4,7 @@ Plugin Name: custer_plugin Plugin URI: Description: customize user : output infos on page, on email, and change current user id momentarly Author: hugogogo -Version: 0.1.1 +Version: 0.1.3 Author URI: */ @@ -18,7 +18,7 @@ if (!defined('ABSPATH')) { 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__) . '/format_user_infos.php'); include_once(plugin_dir_path(__FILE__) . '/user_infos.php'); diff --git a/plugins/custer/custer_class.php b/plugins/custer/custer_class.php index 8323e31..3cf33ab 100644 --- a/plugins/custer/custer_class.php +++ b/plugins/custer/custer_class.php @@ -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 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; @@ -38,6 +39,9 @@ class Custer { } public static function reset_current_user_backup() { $backup_user = self::$_backup_current_user; + if ($backup_user === null) { + $backup_user = get_current_user_id(); + } self::$_backup_current_user = null; return $backup_user; } diff --git a/plugins/custer/filter_mail.php b/plugins/custer/filter_mail.php index 378b779..db90223 100644 --- a/plugins/custer/filter_mail.php +++ b/plugins/custer/filter_mail.php @@ -57,22 +57,22 @@ function replace_words($matches, $user_id = null) { * 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 */ +//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 = '/\$\$(.*?)\$\$/'; +error_log("inside wp_mail hook, in filter_email_wp"); + $old_body = $args['message']; $new_body = preg_replace_callback($pattern, __NAMESPACE__.'\replace_words', $old_body); $args['message'] = $new_body; +error_log("args['message']"); +error_log(json_encode($args['message'])); + return $args; } add_filter('wp_mail', __NAMESPACE__.'\filter_email_wp', 10, 1); diff --git a/plugins/custer/author_id.php b/plugins/custer/get_user_id.php similarity index 54% rename from plugins/custer/author_id.php rename to plugins/custer/get_user_id.php index 4e3de3e..81d8b56 100644 --- a/plugins/custer/author_id.php +++ b/plugins/custer/get_user_id.php @@ -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; +} + + + + + ?>