Files
2024_WORDPRESS_PLUGIN_custer/change_id.php
2024-07-17 15:18:42 +02:00

148 lines
2.9 KiB
PHP

<?php
namespace CUSTER;
/*
* it means someone outside wp is accessing the file, in this case kill it.
*/
if (!defined('ABSPATH')) {
die('You can not access this file!');
}
/*
* change the current user to the author id of the current object
* the only option is to force the author
* -> there is no sens in forcing the logged_in, because it's already the default
*
* [custer_author_id] -> give current user the id of author
* [custer_author_id off] -> reset to logged_in
*
* ! anchors not workink for the moment
* //[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
*
*/
function shortcode_author_id($options) {
$anchor_name = '';
$option = '';
$is_set_anchor = false;
/*
* set option value :
* 'off', 'author', 'set_anchor', or 'anchor'
*
*/
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 (is_array($options) && in_array('off', $options)) {
$option = 'off';
}
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();
}
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();
}
return $id;
}
?>