diff --git a/plugins/cipf_plugin/cipf_plugin.php b/plugins/cipf_plugin/cipf_plugin.php index 2b48db8..749bd23 100644 --- a/plugins/cipf_plugin/cipf_plugin.php +++ b/plugins/cipf_plugin/cipf_plugin.php @@ -28,12 +28,6 @@ include_once( plugin_dir_path(__FILE__) . '/plgntls_class.php'); include_once(Plgntls::root_path() . 'php/utils/globals.php'); include_once(Plgntls::root_path() . 'php/utils/console_log.php'); -include_once(Plgntls::root_path() . 'php/admin_menu/admin_menu.php'); -include_once(Plgntls::root_path() . 'php/admin_menu/admin_menu_toggle.php'); -include_once(Plgntls::root_path() . 'php/admin_menu/menu_content.php'); -include_once(Plgntls::root_path() . 'php/admin_menu/post_paypal.php'); -include_once(Plgntls::root_path() . 'php/admin_menu/post_payment_messages.php'); - // paypal include_once(Plgntls::root_path() . 'php/paypal/paypal.php'); // profs diff --git a/plugins/custer/custer.php b/plugins/custer/custer.php index cbb4f81..e51bf8f 100644 --- a/plugins/custer/custer.php +++ b/plugins/custer/custer.php @@ -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'); + ?> diff --git a/plugins/custer/custer_class.php b/plugins/custer/custer_class.php index 3cf33ab..dd24bd8 100644 --- a/plugins/custer/custer_class.php +++ b/plugins/custer/custer_class.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; diff --git a/plugins/custer/filter_mail.php b/plugins/custer/filter_mail.php index 61472ae..c8eb726 100644 --- a/plugins/custer/filter_mail.php +++ b/plugins/custer/filter_mail.php @@ -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) diff --git a/plugins/custer/format_user_infos.php b/plugins/custer/format_user_infos.php index 6f5fad2..dd825fe 100644 --- a/plugins/custer/format_user_infos.php +++ b/plugins/custer/format_user_infos.php @@ -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; -} - - ?> diff --git a/plugins/custer/queries.php b/plugins/custer/queries.php new file mode 100644 index 0000000..c9b5465 --- /dev/null +++ b/plugins/custer/queries.php @@ -0,0 +1,72 @@ + '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) { + return ''; +} + + + + + + +?> diff --git a/plugins/custer/user_infos.php b/plugins/custer/user_infos.php index 909ad02..eb2f9aa 100644 --- a/plugins/custer/user_infos.php +++ b/plugins/custer/user_infos.php @@ -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 = '