Merge branch 'master' into emails
This commit is contained in:
@@ -4,7 +4,7 @@ Plugin Name: hggg_custer
|
||||
Plugin URI:
|
||||
Description: customize user : output infos on page, on email, and change current user id momentarly
|
||||
Author: hugogogo
|
||||
Version: 0.2.1
|
||||
Version: 0.2.2
|
||||
Author URI:
|
||||
*/
|
||||
|
||||
@@ -23,6 +23,7 @@ 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');
|
||||
include_once(plugin_dir_path(__FILE__) . '/queries.php');
|
||||
|
||||
include_once(plugin_dir_path(__FILE__) . '/admin_menu.php');
|
||||
include_once(plugin_dir_path(__FILE__) . '/admin_menu_toggle.php');
|
||||
@@ -30,4 +31,5 @@ include_once(plugin_dir_path(__FILE__) . '/admin_menu_toggle.php');
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
|
||||
@@ -21,6 +21,11 @@ class Custer {
|
||||
const SLUG_TOOGLE_ADMIN_MENU = ['_name'=>'toogle_admin_menu_url_custer', 'toggle'=>'toggle', 'show'=>'show', 'hide'=>'hide'];
|
||||
const OPTION_TOGGLE_MENU = ['_name'=>'toggle_admin_menu_option_custer', 'show'=>'show', 'hide'=>'hide'];
|
||||
const OPTION_ANCHOR = 'custer_anchor_ids';
|
||||
const QUERIES = [
|
||||
'__author_page_url__',
|
||||
'__user_post_url__',
|
||||
'__admin_email__',
|
||||
];
|
||||
|
||||
private static $_backup_current_user = null;
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ function replace_words($matches, $user_id = null) {
|
||||
}
|
||||
|
||||
$query = $matches[1];
|
||||
$result = \CUSTER\format_user_info($query, $current_user, $user_id);
|
||||
$result = \CUSTER\format_user_info($query, $user_id);
|
||||
|
||||
/*
|
||||
* if result is array, take the first element (not ideal)
|
||||
|
||||
@@ -48,33 +48,26 @@ function return_result($output, $if_empty = '') {
|
||||
* format the output
|
||||
* if is acf, use acf default format
|
||||
*/
|
||||
function format_user_info($query, $current_user, $user_id, $if_empty = '') {
|
||||
function format_user_info($query, $user_id, $if_empty = '') {
|
||||
$output_date_format = Custer::USER_INFO_DATE_FORMAT;
|
||||
$special_queries = Custer::QUERIES;
|
||||
|
||||
$current_user = get_user_by('id', $user_id);
|
||||
$is_acf = false;
|
||||
|
||||
|
||||
/*
|
||||
* if is special query __author_page__
|
||||
* return author page url
|
||||
* if is special query
|
||||
*
|
||||
*/
|
||||
if ($query === '__author_page_url__') {
|
||||
$output = get_author_posts_url($user_id);
|
||||
return \CUSTER\return_result($output);
|
||||
if (in_array($query, $special_queries, true)) {
|
||||
$trimmed_query = trim($query, '_');
|
||||
$function_name = __NAMESPACE__.'\find_'.$trimmed_query;
|
||||
$output = $function_name($user_id);
|
||||
return \CUSTER\return_result($output, $if_empty);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* if is special query __user_post_url__
|
||||
* return author page url
|
||||
*
|
||||
*/
|
||||
if ($query === '__user_post_url__') {
|
||||
$output = \CUSTER\find_user_post_url($user_id);
|
||||
return \CUSTER\return_result($output);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* check if it's an acf field
|
||||
@@ -101,12 +94,12 @@ function format_user_info($query, $current_user, $user_id, $if_empty = '') {
|
||||
|
||||
|
||||
/*
|
||||
* try to extract a string
|
||||
* try to extract a single entity
|
||||
* for example : ["string"] -> "string"
|
||||
* or : ["", "string"] -> "string"
|
||||
*
|
||||
*/
|
||||
while (is_array($output) && count($output) === 1) {
|
||||
$output = reset($output);
|
||||
}
|
||||
$output = \CUSTER\extract_smallest_entity($output);
|
||||
|
||||
|
||||
|
||||
@@ -139,31 +132,5 @@ function format_user_info($query, $current_user, $user_id, $if_empty = '') {
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* find the posts of the user and return the first one
|
||||
*
|
||||
*/
|
||||
function find_user_post_url($user_id) {
|
||||
$user_post_url = '';
|
||||
|
||||
$args = array(
|
||||
'post_type' => 'post',
|
||||
'author' => $user_id,
|
||||
'posts_per_page' => 1,
|
||||
);
|
||||
$posts = get_posts($args);
|
||||
|
||||
if (empty($posts)) {
|
||||
$user_post_url = '';
|
||||
}
|
||||
else {
|
||||
$query = reset($posts);
|
||||
$user_post_url = get_permalink($query->ID);
|
||||
}
|
||||
|
||||
return $user_post_url;
|
||||
}
|
||||
|
||||
|
||||
|
||||
?>
|
||||
|
||||
73
plugins/custer/queries.php
Normal file
73
plugins/custer/queries.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?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 get_queries($user_id, $if_empty) {
|
||||
$special_queries = Custer::QUERIES;
|
||||
|
||||
$queries = array();
|
||||
foreach($special_queries as $query) {
|
||||
$queries[$query] = \CUSTER\format_user_info($query, $user_id, $if_empty);
|
||||
}
|
||||
return $queries;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function find_author_page_url($user_id) {
|
||||
return get_author_posts_url($user_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* find the posts of the user and return the first one
|
||||
*
|
||||
*/
|
||||
function find_user_post_url($user_id) {
|
||||
$user_post_url = '';
|
||||
|
||||
$args = array(
|
||||
'post_type' => 'post',
|
||||
'author' => $user_id,
|
||||
'posts_per_page' => 1,
|
||||
);
|
||||
$posts = get_posts($args);
|
||||
|
||||
if (empty($posts)) {
|
||||
$user_post_url = '';
|
||||
}
|
||||
else {
|
||||
$query = reset($posts);
|
||||
$user_post_url = get_permalink($query->ID);
|
||||
}
|
||||
|
||||
return $user_post_url;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*
|
||||
*/
|
||||
function find_admin_email($user_id) {
|
||||
$admin_email = get_option( 'admin_email' );
|
||||
return $admin_email;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
@@ -12,42 +12,53 @@ if (!defined('ABSPATH')) {
|
||||
|
||||
|
||||
|
||||
function extract_if_array_size_one($value) {
|
||||
if (is_array($value) && count($value) === 1)
|
||||
return reset($value);
|
||||
function extract_smallest_entity($value) {
|
||||
$size = 1;
|
||||
while (is_array($value) && $size === 1) {
|
||||
// https://stackoverflow.com/questions/2216052/how-to-check-whether-an-array-is-empty-using-php#answer-20177855
|
||||
$tmp_array = array_filter($value);
|
||||
$size = count($tmp_array);
|
||||
if ($size === 1) {
|
||||
$value = reset($tmp_array);
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
function merge_two_arrays($array1, $array2) {
|
||||
$new_array = $array1;
|
||||
foreach ($array2 as $key2 => $value2) {
|
||||
$value = \CUSTER\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 = \CUSTER\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;
|
||||
function merge_arrays(...$arrays) {
|
||||
// extract first element
|
||||
$new_array = array_shift($arrays);
|
||||
// then loop through the next arrays
|
||||
foreach ($arrays as $array) {
|
||||
foreach ($array as $key => $value) {
|
||||
$value = \CUSTER\extract_smallest_entity($value);
|
||||
// if key does not already exist, simply add it and the value to new array
|
||||
if (!isset($new_array[$key])) {
|
||||
$new_array[$key] = $value;
|
||||
continue;
|
||||
}
|
||||
// if key already exist, add a new key with a (number) suffix
|
||||
$i = 1;
|
||||
while (isset($new_array[$key.'('.$i.')'])) {
|
||||
++$i;
|
||||
}
|
||||
$new_array[$key.'('.$i.')'] = $value;
|
||||
}
|
||||
}
|
||||
ksort($new_array, 'strnatcasecmp');
|
||||
return $new_array;
|
||||
}
|
||||
|
||||
|
||||
function output_list_front($array, $current_user, $user_id) {
|
||||
function output_list_front($array, $user_id, $if_empty) {
|
||||
$output = '<ul>';
|
||||
foreach ($array as $key => $value) {
|
||||
if (str_starts_with($key, '_'))
|
||||
continue ;
|
||||
$value = \CUSTER\format_user_info($key, $current_user, $user_id);
|
||||
if (str_starts_with($key, '_')) {
|
||||
if (!str_starts_with($key, '__')) {
|
||||
continue ;
|
||||
}
|
||||
}
|
||||
$value = \CUSTER\format_user_info($key, $user_id, $if_empty);
|
||||
$output .= '<li>';
|
||||
$output .= '<span>';
|
||||
$output .= $key;
|
||||
@@ -150,13 +161,6 @@ function current_user_infos($atts) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* if id is for 'author' && shortcode 'custer_change_id' is set
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
$current_user = get_user_by('id', $user_id);
|
||||
|
||||
|
||||
/*
|
||||
@@ -166,8 +170,9 @@ function current_user_infos($atts) {
|
||||
if ($output_all) {
|
||||
$user_properties = (array) get_userdata($user_id)->data;
|
||||
$user_metas = get_user_meta($user_id);
|
||||
$user_infos = \CUSTER\merge_two_arrays($user_metas, $user_properties);
|
||||
return \CUSTER\output_list_front($user_infos, $current_user, $user_id);
|
||||
$queries = \CUSTER\get_queries($user_id, $if_empty);
|
||||
$user_infos = \CUSTER\merge_arrays($user_metas, $user_properties, $queries);
|
||||
return \CUSTER\output_list_front($user_infos, $user_id, $if_empty);
|
||||
}
|
||||
|
||||
|
||||
@@ -182,7 +187,7 @@ function current_user_infos($atts) {
|
||||
$query = $atts;
|
||||
else
|
||||
return '';
|
||||
return \CUSTER\format_user_info($query, $current_user, $user_id, $if_empty);
|
||||
return \CUSTER\format_user_info($query, $user_id, $if_empty);
|
||||
}
|
||||
add_shortcode('custer_user_info', __NAMESPACE__.'\current_user_infos');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user