updated user info shortcode to output author page and format date

This commit is contained in:
asus
2024-03-07 14:51:10 +01:00
parent b8fbd84d53
commit 60f46265c2
6 changed files with 189 additions and 23 deletions

View File

@@ -45,6 +45,7 @@ include_once(PLGNTLS_class::get_path() . 'php/redirections.php');
include_once(PLGNTLS_class::get_path() . 'php/author_restriction.php');
include_once(PLGNTLS_class::get_path() . 'php/reset_acf_fields.php');
include_once(PLGNTLS_class::get_path() . 'php/filter_mail.php');
include_once(PLGNTLS_class::get_path() . 'php/prof_check_page.php');
// form builder patch :
//include_once(PLGNTLS_class::get_path() . 'php/form_builder_patch/url_validation.php');

View File

@@ -3,18 +3,20 @@
function restrict_author_page_CIPF() {
$can_access = false;
if (is_author())
$can_access = true;
else if (current_user_can('administrator'))
$can_access = true;
else if (current_user_can('editor'))
$can_access = true;
if ($can_access === false)
if (!is_author())
return;
$can_access = false;
if (current_user_can('administrator')) {
$can_access = true;
}
else if (current_user_can('editor')) {
$can_access = true;
}
if ($can_access === true)
return;
global $post;
$author_id = get_the_author_meta( 'ID' );
$current_user_id = get_current_user_id();

View File

@@ -0,0 +1,34 @@
<?php
function check_prof_page_CIPF() {
//add_action('template_redirect', 'is_prof_activ_CIPF');
}
add_action('init', 'check_prof_page_CIPF');
function is_prof_activ_CIPF() {
$acf_prof_is_activ = PLGNTLS_class::ACF_PROF_IS_ACTIV;
$slug_wait_activation = PLGNTLS_class::SLUG_PROF_INACTIV;
//$current_user = wp_get_current_user();
$user_id = get_current_user_id();
$acf_id = 'user_' . $user_id;
$is_activ = get_field($acf_prof_is_activ, $acf_id);
if (!empty($is_activ))
return;
$redirection_prof_inactiv = home_url() . '/' . $slug_wait_activation;
error_log("redirection_prof_inactiv");
error_log($redirection_prof_inactiv);
wp_redirect($redirection_prof_inactiv);
exit;
}
?>

View File

@@ -58,33 +58,155 @@ function output_list_front_CIPF($array) {
}
function format_user_info_CIPF($output, $query, &$current_user, $user_id) {
$output_date_format = PLGNTLS_class::USER_INFO_DATE_FORMAT;
$is_acf = false;
$is_date = false;
/*
* check if it's an acf field
* first method : check if _field exist
*
$_acf_field = '_'.$query;
$acf_field = $current_user->$_acf_field;
if (!empty($acf_field))
$is_acf = true;
*/
/*
* 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;
/*
* check if is date
*
*/
if ($is_acf) {
$acf_type = $acf_object['type'];
if ($acf_type && $acf_type === "date_picker")
$is_date = true;
}
/*
* if is date, transform format
*
*/
if ($is_date) {
$acf_date_string = $acf_object['value'];
$acf_date_format = $acf_object['return_format'];
$date = date_create_from_format($acf_date_format, $acf_date_string);
$output = $date->format($output_date_format);
}
/*
* return the result
*
*/
if (is_string($output))
return $output;
else
return json_encode($output);
}
/*
* shortcode to write user info of the logged in user
* 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 author] -> display the email of the author of the page/post instead of the connected user
*
*/
function current_user_infos_CIPF($atts) {
if (!is_user_logged_in())
return ;
$current_user = wp_get_current_user();
$current_user_infos = $current_user->data;
error_log("--atts");
error_log(json_encode($atts));
if (empty($atts)) {
$user_properties = (array) get_userdata($current_user->ID)->data;
$user_metas = get_user_meta($current_user->ID);
$current_user = wp_get_current_user();
$user_id = get_current_user_id();
/*
* has parameter 'author' ?
* if yes, removes it from $atts
*
*/
$has_author = false;
if (is_array($atts)) {
$needle_key = array_search('author', $atts);
if ($needle_key !== false) {
unset($atts[$needle_key]);
$has_author = true;
}
}
else {
if ($atts === 'author') {
$atts = '';
$has_author = true;
}
}
error_log(json_encode($atts));
/*
* 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 author id outside loop and outside singular page : https://wordpress.stackexchange.com/q/65548
*
*/
if ($has_author) {
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 {
global $wp_query;
if (!empty($wp_query->posts))
$user_id = $wp_query->posts[0]->post_author;
}
$current_user = new WP_User($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_CIPF($user_metas, $user_properties);
return output_list_front_CIPF($user_infos);
}
// only return the first argument
/*
* real purpose of this shortcode :
* only return the first argument (that is not 'author')
*
*/
$query = $atts[0];
$output = $current_user->$query;
if (is_string($output))
return $output;
else
return json_encode($output);
return format_user_info_CIPF($output, $query, $current_user, $user_id);
}
add_shortcode('cipf_user_info', 'current_user_infos_CIPF');

View File

@@ -62,8 +62,15 @@ class PLGNTLS_class
const ACF_CARD_PRICE_DELIVERY = 'livraison';
const ACF_CARD_PRICE_TOTAL = 'somme_a_regler';
const ACF_CARD_EXPIRATION = 'fin_de_validite';
const CARD_RENEW_PERIOD = 31; // int : number of days before expiration when renew card start to be possible
const CARD_VALIDITY_TIME = '1 year'; // string : time of validity of the card (ex: '1 month' or '1 year' or '60 days')
CONST ACF_PROF_IS_ACTIV = 'compte-actif';
CONST ACF_PROF_CAN_RENEW = 'renouvellement_possible';
const CARD_RENEW_PERIOD = 31; // int : number of days before expiration when renew card start to be possible
const CARD_VALIDITY_TIME = '1 year'; // string : time of validity of the card (ex: '1 month' or '1 year' or '60 days')
CONST SLUG_PROF_INACTIV = 'validation-en-cours';
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 $_root_path;
private static $_root_url;

Submodule private updated: e56631d8c9...da67b9e9c0