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

@@ -2,9 +2,9 @@
/*
Plugin Name: cipf_plugin
Plugin URI:
Description: test avec page de redirection
Description: version avec vraie page redirection && patch formbuilder calculation
Author: hugogogo
Version: 0.2.8
Version: 0.2.8.2
Author URI:
*/
@@ -55,8 +55,8 @@ include_once(PLGNTLS_class::root_path() . 'php/admin_modif_prof.php');
/*
include_once(PLGNTLS_class::root_path() . 'php/form_builder_patch/url_validation.php');
include_once(PLGNTLS_class::root_path() . 'php/form_builder_patch/multiple_modals.php');
include_once(PLGNTLS_class::root_path() . 'php/form_builder_patch/form_calculation.php');
*/
include_once(PLGNTLS_class::root_path() . 'php/form_builder_patch/form_calculation.php');

View File

@@ -18,60 +18,12 @@ if (!defined('ABSPATH')) {
/*
* version 4 :
* modifying directly the current user : very hardcore, and need to store it
*/
function shortcode_choose_meta_id_CIPF ($options) {
PLGNTLS_class::debug_infos();
// $role_fipf = PLGNTLS_class::ROLE_FIPF;
// $role_admin = PLGNTLS_class::ROLE_ADMIN;
//
// $current_user = wp_get_current_user();
//
// $allowed_roles = array($role_admin, $role_fipf);
// if (!array_intersect($allowed_roles, $current_user->roles))
// return;
$option = reset($options);
if (!is_string($option)) {
return;
}
if ($option === 'logged_in') {
$id = get_current_user_id();
error_log("logged_in id : " . $id);
PLGNTLS_class::set_current_user_backup();
wp_set_current_user($id);
}
else if ($option === 'post_creator') {
$id = get_queried_object_id();
error_log("post_creator id : " . $id);
PLGNTLS_class::set_current_user_backup();
wp_set_current_user($id);
}
else if ($option === 'off') {
$id = PLGNTLS_class::reset_current_user_backup();
error_log("reset id : " . $id);
wp_set_current_user($id);
}
else
return;
//add_action('set_current_user', 'force_set_user_CIPF');
}
add_shortcode('cipf_change_meta_id', 'shortcode_choose_meta_id_CIPF');
/*
* version 3 :
*
* trying with a filter on wp_get_current_user()
* used by form builder to retrieve the user before using get_metadat()
* -> somehow the filter is never called
* -> the filter is not called in most cases, if user is instance of WP_USER
*/
//function filter_user_id_CIPF($id) {
// PLGNTLS_class::debug_infos();
@@ -83,7 +35,8 @@ add_shortcode('cipf_change_meta_id', 'shortcode_choose_meta_id_CIPF');
// error_log("-in add_filter_for_user_id_CIPF"); // this is output
// add_filter('determine_current_user', 'filter_user_id_CIPF', 10, 1);
//}
//add_action('wp', 'add_filter_for_user_id_CIPF');
//add_shortcode('cipf_change_meta_id', 'add_filter_for_user_id_CIPF');
////add_action('wp', 'add_filter_for_user_id_CIPF');
@@ -114,11 +67,11 @@ add_shortcode('cipf_change_meta_id', 'shortcode_choose_meta_id_CIPF');
//
// $object_id = 117;
//
///*
//* I just copied the code of wordpress :
//* i'm not sure there is a better way
//*
//*/
// /*
// * I just copied the code of wordpress :
// * i'm not sure there is a better way
// *
// */
// $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
//
// if ( ! $meta_cache ) {
@@ -141,9 +94,9 @@ add_shortcode('cipf_change_meta_id', 'shortcode_choose_meta_id_CIPF');
// return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] );
// }
// }
///*
//* FIN copy
//*/
// /*
// * FIN copy
// */
//}
//function add_filter_for_user_id_CIPF($wp) {
// PLGNTLS_class::debug_infos();

View File

@@ -44,9 +44,81 @@ function check_can_pay_CIPF() {
* in case it was changed from admin pannel
*
*/
$tarif = (int)get_field($acf_card_price_choice['_name'], $acf_id);
$livraison = (int)get_field($acf_card_price_delivery['_name'], $acf_id);
$price = $tarif + $livraison;
$tarif_object = get_field_object($acf_card_price_choice['_name'], $acf_id);
error_log("tarif_object");
error_log(json_encode($tarif_object));
/*
{
"ID":34541,
"key":"field_65d24447cc89f",
"label":"Tarif",
"name":"tarif",
"aria-label":"",
"prefix":"acf",
"type":"radio",
"value":"Je suis hors r\u00e9seau FIPF et je souhaite commander la carte \u00e0 15 \u20ac",
"menu_order":3,
"instructions":"",
"required":0,
"id":"",
"class":"",
"conditional_logic":0,
"parent":34538,
"wrapper":{
"width":"",
"class":"",
"id":""
},
"choices":{
"10":"Mon association adh\u00e8re \u00e0 la FIPF, je peux commander la carte \u00e0 10 \u20ac",
"15":"Je suis hors r\u00e9seau FIPF et je souhaite commander la carte \u00e0 15 \u20ac"
},
"default_value":"",
"return_format":"label",
"allow_null":0,
"other_choice":0,
"layout":"vertical",
"save_other_choice":0,
"_name":"tarif",
"_valid":1
}
{
"ID":34541,
"key":"field_65d24447cc89f",
"label":"Tarif",
"name":"tarif",
"aria-label":"",
"prefix":"acf",
"type":"radio",
"value":"15",
"menu_order":3,
"instructions":"",
"required":0,"id":"",
"class":"",
"conditional_logic":0,
"parent":34538,
"wrapper":{"width":"","class":"","id":""},
"choices":{
"10":"Mon association adh\u00e8re \u00e0 la FIPF, je peux commander la carte \u00e0 10 \u20ac",
"15":"Je suis hors r\u00e9seau FIPF et je souhaite commander la carte \u00e0 15 \u20ac"
},
"default_value":"",
"return_format":"value",
"allow_null":0,
"other_choice":0,
"layout":"vertical",
"save_other_choice":0,
"_name":"tarif",
"_valid":1
}
*/
// $tarif_value = ;
// $tarif = (int)$tarif_value;
$livraison_object = get_field_object($acf_card_price_delivery['_name'], $acf_id);
// $livraison_value = ;
// $livraison = (int)$livraison_value;
// $price = $tarif + $livraison;
$price = 3;
update_field($acf_card_price_total['_name'], $price, $acf_id);
/*

View File

@@ -69,16 +69,16 @@ function redirection_profil_CIPF(){
* redirects when on the page with slug defined in SLUG_PAGE_REDIRECTION
*
*/
//function redirection_page_CIPF(){
// PLGNTLS_class::debug_infos();
// $slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION;
//
// if (!is_page($slug_page_redirection))
// return;
//
// redirection_profil_CIPF();
//}
//add_action('template_redirect', 'redirection_page_CIPF');
function redirection_page_CIPF(){
PLGNTLS_class::debug_infos();
$slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION;
if (!is_page($slug_page_redirection))
return;
redirection_profil_CIPF();
}
add_action('template_redirect', 'redirection_page_CIPF');
@@ -90,35 +90,35 @@ function redirection_profil_CIPF(){
* works even if the page actually does not exists
*
*/
function redirection_query_CIPF() {
PLGNTLS_class::debug_infos();
$slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION;
if (!isset($_GET['q']))
return;
/*
* check the request /path/
* for url : https://url.com/path/to/file/?query=value
* -> $_GET: {"q":"/path/to/file/","query":"value"}
*
*/
$get_path = trim($_GET['q'], '/');
if ($get_path !== $slug_page_redirection)
return;
/*
* check the request ?query
*
if(!isset($_GET['query']))
return;
*/
redirection_profil_CIPF();
}
add_action('template_redirect', 'redirection_query_CIPF');
//add_action('init', 'redirection_query_CIPF');
//function redirection_query_CIPF() {
// PLGNTLS_class::debug_infos();
// $slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION;
//
//
// if (!isset($_GET['q']))
// return;
//
// /*
// * check the request /path/
// * for url : https://url.com/path/to/file/?query=value
// * -> $_GET: {"q":"/path/to/file/","query":"value"}
// *
// */
// $get_path = trim($_GET['q'], '/');
// if ($get_path !== $slug_page_redirection)
// return;
//
// /*
// * check the request ?query
// *
// if(!isset($_GET['query']))
// return;
// */
//
// redirection_profil_CIPF();
//}
//add_action('template_redirect', 'redirection_query_CIPF');
////add_action('init', 'redirection_query_CIPF');

View File

@@ -54,8 +54,7 @@ if (!defined('ABSPATH')) {
*
*/
class PLGNTLS_class
{
class PLGNTLS_class {
/*
* const declarations
*
@@ -117,7 +116,6 @@ class PLGNTLS_class
private static $_file_name;
private static $_root_path;
private static $_root_url;
private static $_backup_current_user = null;
private $_first_script;
private $_first_style;
@@ -192,26 +190,6 @@ class PLGNTLS_class
/*
* 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;
}
/*
* for debug purposes

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');
?>

Submodule private updated: 1a7692205d...33f4d57f8d