created plugin custer, and removed shortcodes change_id and user_infos from cipf plugin

This commit is contained in:
asus
2024-03-17 02:27:28 +01:00
parent 494943e4f7
commit 6ad1fb5137
13 changed files with 668 additions and 127 deletions

View File

@@ -0,0 +1,42 @@
<?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!');
}
/*
* get the current author id
* return 0 if not found
* get id outside loop : https://wordpress.stackexchange.com/q/65548
*
*/
function get_author_id() {
if (is_author()) {
$user_id = get_queried_object_id();
}
else if (in_the_loop()) {
$user_id = get_the_author_meta('ID');
}
// else if (is_singular()) {
// $user_id = get_queried_object()->post_author;
// }
else {
$user_id = 0;
}
return $user_id;
}
?>

View File

@@ -0,0 +1,58 @@
<?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_change_id] -> default (author)
* [custer_change_id author] -> author
* [custer_change_id off] -> reset to logged_in
*
*/
function shortcode_change_id($options) {
/*
* set the default value for option
* currently it can only be author
*
*/
$option = 'author';
if (is_array($options)) {
$option = reset($options);
}
if (!is_string($option)) {
return;
}
if ($option === 'author') {
$id = get_author_id();
Custer::set_current_user_backup();
wp_set_current_user($id);
}
else if ($option === 'off') {
$id = Custer::reset_current_user_backup();
wp_set_current_user($id);
}
else
return;
}
add_shortcode('custer_change_id', __NAMESPACE__.'\shortcode_change_id');
?>

31
plugins/custer/custer.php Normal file
View File

@@ -0,0 +1,31 @@
<?php
/*
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.0
Author URI:
*/
/*
* it means someone outside wp is accessing the file, in this case kill it.
*/
if (!defined('ABSPATH')) {
die('You can not access this file!');
}
include_once(plugin_dir_path(__FILE__) . '/custer_class.php');
include_once(plugin_dir_path(__FILE__) . '/author_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');
include_once(plugin_dir_path(__FILE__) . '/filter_mail.php');
//add_shortcode('custer_change_id', 'CUSTER\shortcode_change_id');
?>

View File

@@ -0,0 +1,48 @@
<?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!');
}
/*
*
*/
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)
private static $_backup_current_user = null;
/*
* 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;
}
}
?>

View File

@@ -0,0 +1,90 @@
<?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!');
}
/*
* callback to provide the user info corresponding to the $$key_word$$
*
*/
function replace_words($matches, $user_id = null) {
if ($user_id !== null) {
$current_user = get_user_by('id', $user_id);
}
else if (is_user_logged_in()) {
$current_user = wp_get_current_user();
$user_id = get_current_user_id();
}
else {
return "";
}
if ($current_user === false)
return "";
$query = $matches[1];
$result = format_user_info($query, $current_user, $user_id);
/*
* if result is array, take the first element (not ideal)
*
*/
if (is_array($result))
$result = reset($result);
/*
* if is special query __author_page__
* return author page url
*
*/
if ($query === '__author_page__') {
$current_user_id = get_current_user_id();
$result = get_author_posts_url($current_user_id);
}
/*
* if no match, return $$<query>$$
*
*/
if (empty($result))
return $matches[0];
return $result;
}
/*
* filter emails at the final point : the wp_mail hook
* it uses a callback that rely on the logged-in user
* it will works well most of the time, but it's possible
* that a user logged out before the email is sent
* or event that a different user has already logged in
*/
function filter_email_wp($args) {
// pattern : anything surrounded by '$$', ex : $$value$$
$pattern = '/\$\$(.*?)\$\$/';
$old_body = $args['message'];
$new_body = preg_replace_callback($pattern, __NAMESPACE__.'\replace_words', $old_body);
$args['message'] = $new_body;
return $args;
}
add_filter('wp_mail', __NAMESPACE__.'\filter_email_wp', 10, 1);
?>

View File

@@ -0,0 +1,87 @@
<?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!');
}
function format_user_info($query, &$current_user, $user_id) {
$output_date_format = Custer::USER_INFO_DATE_FORMAT;
$is_acf = false;
/*
* check if it's an acf field, and a date
* second method : check what get_field_object() returns
*
*/
$acf_id = 'user_'.$user_id;
$acf_object = get_field_object($query, $acf_id);
if ($acf_object !== false)
$is_acf = true;
/*
* if is acf, use the acf return format
* otherwise, use the default wordpress value
*
*/
if ($is_acf)
$output = get_field($query, $acf_id);
else
$output = $current_user->$query;
/*
* try to extract a string
*
*/
while (is_array($output) && count($output) === 1)
$output = reset($output);
/*
* if is not acf
* check if is a date
* and format it
* to create a DateTime from a time stamp : https://stackoverflow.com/a/12039058/9497573
*
*/
if (!$is_acf) {
$timestamp = false;
if (is_string($output)) {
$timestamp = strtotime($output);
}
if ($timestamp !== false) {
$date = new DateTime('@' . $timestamp);
$output = $date->format($output_date_format);
}
}
/*
* return the result
*
*/
if (is_array($output) && count($output) === 0)
$output = '';
if (!is_string($output))
$output = json_encode($output, JSON_UNESCAPED_SLASHES);
return esc_html($output);
}
?>

View File

@@ -0,0 +1,182 @@
<?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!');
}
function extract_if_array_size_one($value) {
if (is_array($value) && count($value) === 1)
return reset($value);
return $value;
}
function merge_two_arrays($array1, $array2) {
$new_array = $array1;
foreach ($array2 as $key2 => $value2) {
$value = extract_if_array_size_one($value2);
// if key was not in first array, add the new element to it
if (!isset($new_array[$key2])) {
$new_array[$key2] = $value2;
continue;
}
// if key was in first array, add both in an array
$value1 = extract_if_array_size_one($new_array[$key2]);
if (empty($value1))
$new_array[$key2] = $value2;
else if (empty($value2))
$new_array[$key2] = $value1;
else {
$new_value = array($value1, $value2);
$new_array[$key] = $new_value;
}
}
return $new_array;
}
function output_list_front($array, $current_user, $user_id) {
$output = '<ul>';
foreach ($array as $key => $value) {
if (str_starts_with($key, '_'))
continue ;
$value = format_user_info($key, $current_user, $user_id);
$output .= '<li>';
$output .= '<span>';
$output .= $key;
$output .= ' : ';
$output .= $value;
$output .= '</span>';
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
/*
* shortcode to write user info of post author
* 0 or 1 argument, usage :
* - [cipf_user_info] -> list of all availables infos
* - [cipf_user_info user_email] -> display the email
* - [cipf_user_info user_email user_login] -> display the email
* - [cipf_user_info user_email id='logged_in'] -> display the email of the connected user
* - [cipf_user_info user_email id='author'] -> display the email of the creator of the page/post
* - [cipf_user_info user_email id='logged_in' important] -> display the email of the connected user, even if surrounded by shortcode 'custer_change_id'
* important keyword has no effect if id='author'
*
*/
function current_user_infos($atts) {
$is_important = false;
/*
* choose the default id target :
* - logged_in
* - author
*
$id_is = 'logged_in';
*/
$id_is = 'author';
/*
* has parameter 'id' ?
* has parameter important ?
* if yes, removes them from $atts
*
*/
if (is_array($atts)) {
if (isset($atts['id'])) {
$id_is = $atts['id'];
unset($atts['id']);
}
if ($id_is === 'logged_in') {
if (in_array('important', $atts)) {
$is_important = true;
$key = array_search('important', $atts);
unset($atts[$key]);
}
}
}
/*
* should output all or a specific parameter ?
*
*/
$output_all = false;
if (empty($atts))
$output_all = true;
else if (count($atts) === 0)
$output_all = true;
/*
* get id according to attributes,
* and the atts of the other shortcode 'custer_change_id' if in use
*
*/
if ($id_is === 'logged_in') {
if ($is_important) {
$user_id = Custer::get_current_user_backup();
if ($user_id === null)
$user_id = get_current_user_id();
}
else {
$user_id = get_current_user_id();
}
}
else if ($id_is === 'author') {
$user_id = get_author_id();
}
else {
return '';
}
/*
* if id is for 'author' && shortcode 'custer_change_id' is set
*
*/
$current_user = get_user_by('id', $user_id);
/*
* output all the available parameters (for help)
*
*/
if ($output_all) {
$user_properties = (array) get_userdata($user_id)->data;
$user_metas = get_user_meta($user_id);
$user_infos = merge_two_arrays($user_metas, $user_properties);
return output_list_front($user_infos, $current_user, $user_id);
}
/*
* real purpose of this shortcode :
* only return the first argument
*
*/
if (is_array($atts))
$query = $atts[0];
else if (is_string($atts))
$query = $atts;
else
return '';
return format_user_info($query, $current_user, $user_id);
}
add_shortcode('custer_user_info', __NAMESPACE__.'\current_user_infos');
?>