change author id works for prof profil and prof form
This commit is contained in:
@@ -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');
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user