$v) { if (empty($v)) $amr_nicenames[$i] = ''; else { if ($s = esc_attr($v)) $amr_nicenames[$i] = $s; else { echo '

Error in string:'.$s.'

'; return(false); } } } } else { echo '

Array of names not passed

'; return(false); } } ausers_update_option ('amr-users-nicenames', $amr_nicenames); $excluded = array(); if ((isset($_POST['nex'])) and (is_array($_POST['nex']))) { foreach ($_POST['nex'] as $i => $v) { if ($v) $excluded[$i] = true; } } ausers_update_option('amr-users-nicenames-excluded', $excluded); $showinwplist = array(); if ((isset($_POST['wp'])) and (is_array($_POST['wp']))) { foreach ($_POST['wp'] as $i => $v) { if ($v) $showinwplist[$i] = true; } } ausers_update_option('amr-users-show-in-wplist', $showinwplist); $ftypes = array(); if ((isset($_POST['ftype'])) and (is_array($_POST['ftype']))) { foreach ($_POST['ftype'] as $i => $v) { if ($v) $ftypes[$i] = esc_attr($v); } } ausers_update_option('amr-users-field-types', $ftypes); echo amr_users_message(__('Options Updated', 'amr-users')); return (true); } function ameta_listnicefield ($nnid, $nnval, $v, $v2=NULL) { echo "\n\t".'
  • ' .'
  • '; } function alist_rebuild_names () { return ('
    '); } function alist_rebuild_names_update () { return ('
                   
    '); } function ameta_list_nicenames_for_input($nicenames) { $fields_types_fixed = amr_fixed_fieldstypes(); //20170912 stop people messing up because they think its a format option /* get the standard names and then the meta names */ if (!($excluded = ausers_get_option('amr-users-nicenames-excluded'))) $excluded = array(); if (!($showinwplist = ausers_get_option('amr-users-show-in-wplist'))) $showinwplist = array(); if (!($fields_types = ausers_get_option('amr-users-field-types'))) $fields_types = $fields_types_defaults; $fieldtypes = amr_fieldtypes(); //the generic list //we need to allow manual exclusion of metakeys becuase of s2members strange time keys on access_cap_limits and who knows there might be others. if (!($excluded_meta_keys = ausers_get_option('amr-users-excluded-meta-keys'))) $excluded_meta_keys = amr_default_excluded_metakeys(); foreach ($excluded_meta_keys as $k=> $on) { // just get the excluded ones if (!$on) unset ($excluded_meta_keys[$k]); } $orig_mk = ausers_get_option('amr-users-original-keys') ; $wpfields = amr_get_usermasterfields(); foreach ($nicenames as $i=> $k) { // just in case, check exclude if (isset ($excluded_meta_keys[$i])) { if (!empty ($excluded_meta_keys[$i] )) { unset ($nicenames[$i]); } } } ksort($nicenames); //force excluded fields to the bottom foreach ($nicenames as $n => $nn) { if (empty($excluded[$n])) $sorttotop[$n] = true; } $nicenames = auser_sortbyother($nicenames, $sorttotop); $total = count($nicenames); $totalex = count($excluded_meta_keys); echo PHP_EOL.'
    '.PHP_EOL; echo '
    '; echo '

    '.__('Fields & Nice Names', 'amr-users').' ('.$total.') ' .'

    ' .'

    ' .sprintf(__('Note: %s meta keys excluded.','amr-users'),$totalex) .' ' .__('Some pseudo fields added','amr-users').'.' .' ' //.'

    '.__('Nicer names for list headings','amr-users').'

    ' //.'' ; echo alist_rebuild_names_update(); echo ''; echo '' .'' .'' .'' .''; foreach ($nicenames as $i => $v ) { if (!empty($excluded[$i])) $class = ' class="alternate striped" '; else $class = ''; echo "\n\t".'' .''; echo ''; echo ''; //20170112 $ftype=''; $disabled = ''; if (!empty($fields_types_fixed [$i])) { $ftype = $fields_types_fixed [$i]; $disabled = ' disabled="disabled" '; } elseif (!empty($fields_types[$i]) ) { //20170912 default fieldtypes must not be changed, can only add new $ftype = $fields_types[$i]; } echo ''; // end 20170112 echo ''; } echo "\n\t".'
    ' .__('Technical Name','amr-users') .'' .__('Nice Name','amr-users') .'' .'' .__('Show in wp user list?','amr-users') .'' .'
    '.__('wp fields only','amr-users').'' .'
    ' .__('Exclude from all reports?','amr-users') .'' .__('Field type','amr-users').'
    '.__('(Data type for format options)','amr-users') .'
    '; echo ''; if ((empty($orig_mk[$i]) or (!($orig_mk[$i] == $i))) and (!in_array($i,$wpfields))){ echo ' ! ';} echo ''; if ($i==='ID') echo ' ' ; else { echo ''; } echo '
    ' .PHP_EOL. '
    '.PHP_EOL; return; } function amrmeta_check_find_fields() { global $amr_nicenames; $amr_nicenames = ausers_get_option ('amr-users-nicenames'); // refetch so have all includidng excluded if (is_wp_error($amr_nicenames) or (empty ($amr_nicenames))) { /* *** Check if we have nicenames already built */ echo '

    '.__('List of possible fields not yet built.', 'amr-users').'

    '; track_progress('Before counting users'); $result = count_users(); track_progress('After counting users'); $total_users = $result['total_users']; if ($total_users > 1000) { amr_users_message( __('You have many users. Please be patient when you rebuild.', 'amr-users')); echo '

    '; foreach ($result['avail_roles'] as $i => $t) { echo '
    '.__($i).' '.$t; } echo '

    '; echo alist_rebuild_names(); return; } else { echo '

    '.__('Automatically rebuilding list of possible fields now.', 'amr-users').'

    '; track_progress('Before rebuilding names'); $amr_nicenames = ameta_rebuildnicenames(); track_progress('After rebuilding names'); echo '

    '.__('List Rebuilt', 'amr-users').'

    '; } ausers_update_option ('amr-users-nicenames', $amr_nicenames); } } function amrmeta_nicenameshelp() { // style="background-image: url(images/screen-options-right-up.gif);" $html = '
      ' .'
    1. '.__('Please exclude any meta keys you do not need. ', 'amr-users').__('Some plugins do weird things in their meta value content. ', 'amr-users') .'
    2. ' .'
    3. '.__('If you are not seeing all the fields you expect to see, then rebuild the list. ', 'amr-users') .'
    4. ' .'
    5. ' .__('Please note that what you see is dependant on the data in your system. If there is no meta data for a field you are expecting to see, it is impossible for that field to appear.', 'amr-users').'
    6. ' .'
    7. '.__('If you add another user related plugin that adds meta data, first add some data to at least one user. Then you may need to rebuild the list of fields below and/or reconfigure your reports if you want to see the new data.', 'amr-users').'
    8. ' .'
    '; return( $html); } function amr_get_alluserkeys( ) { global $wpdb,$amr_nicenames; /* get all user data and attempt to extract out any object values into arrays for listing */ $keys = array( 'avatar'=>'avatar'); if (!is_network_admin()) { $keys['comment_count'] ='comment_count'; $keys['post_count'] ='post_count'; $post_types=get_post_types(); foreach ($post_types as $posttype) $keys[$posttype] = $posttype.'_count'; } $all = amr_get_usermasterfields(); //echo '

    '.sprintf(__('You have %s main user table fields', 'amr-users'),count($all)).'

    '; foreach ($all as $i2 => $v2){ if (!amr_excluded_userkey($v2) ) { $keys[$v2] = $v2; if (isset($keys[$v2])) { //echo ' ✓'.$v2; } //else echo '
    '.__('Added to report DB:', 'amr-users').' '.$v2; } else { if (isset($keys[$v2])) unset($keys[$v2]); //echo '
    '.__('Excluded:', 'amr-users').' "'.$v2.'"'; } } /* Do the meta first */ if (!($excluded_meta_keys = ausers_get_option('amr-users-excluded-meta-keys'))) $excluded_meta_keys = amr_default_excluded_metakeys(); $text = array(); //20170812 foreach ($excluded_meta_keys as $key=>$on) { // work out which not to bother with if ($on) $text[] = $key; } $string = "'".implode($text,"','")."'"; $w = " WHERE meta_key NOT IN (".$string.")"; $q = "SELECT DISTINCTROW meta_key, meta_value FROM $wpdb->usermeta".$w; // need the meta value for those like ym where there is one key but there may be complex stuff in the meta. if ($mkeys = amr_get_next_level_keys( $q)) { //if (WP_DEBUG) {echo '
    For Debug: next level keys'; var_dump($mkeys);} if (is_array($mkeys)) { $keys = array_merge ($keys, $mkeys); echo '

    '.count($mkeys).' distinct "fields" dug out from the meta key/value combination records.

    '; } //if (WP_DEBUG) {echo '
    For Debug: Merged keys'; var_dump($keys);} } unset($mkeys); echo '

    '.__('Check for fields from non wp tables.', 'amr-users').__('..or extracted fields.', 'amr-users').'

    '; $keys2 = apply_filters('amr_get_fields', $keys); //eg: 'avatar'=>'avatar', foreach ($keys2 as $k => $v) { if (!isset($keys[$k]) and !isset($amr_nicenames[$v])) echo '"'.$v.'" added.
    '; } return($keys2); } function amr_get_next_level_keys( $q) { /* get all user data and attempt to extract out any object values into arrays for listing */ global $wpdb, $orig_mk; if (!$orig_mk = ausers_get_option('amr-users-original-keys')) // the record of where the fields came from $orig_mk = array(); $all = $wpdb->get_results($q, ARRAY_A); // print_r ($all); if (is_wp_error($all)) {amr_flag_error ($all); return;} if (!is_array ($all)) return; sort($all); // arrays of meta_key, meta_value echo '

    '.sprintf(__('You have %u distinct meta key / meta value records. ','amr-users'),count($all)).'

    '; _e('Permanently exclude some: ', 'amr-users'); foreach ($all as $i2 => $v2) { if (amr_permanently_excluded_metakeys($v2['meta_key']) ){ unset ($all[$i2]); echo '.'; } } echo '
    '; _e('...Deserialising and rationalising...looking for new fields.', 'amr-users'); foreach ($all as $i2 => $v2) { /* array of meta key, meta value*/ /* Exclude non useful stuff */ // print_r ($v2); $mk = $v2['meta_key']; $mv = $v2['meta_value']; if (!amr_excluded_userkey($mk) ) { if (!empty($mv)) { $temp = maybe_unserialize($mv); //$temp = maybe_unserialize($temp); // have to double unserialise for gravity forms - why? //if (is_array($temp)) {if (WP_DEBUG) {echo '
    Got an array'; var_dump($temp);}} //if (WP_DEBUG) echo '
    Did it unserialise: '; var_dump($temp); $temp = objectToArray ($temp); /* *must do all so can cope with incomplete objects */ $key = str_replace(' ','_', $mk); /* html does not like spaces in the names*/ if ((is_array($temp)) and (!amr_is_assoc($temp) ) ){ // its a numeric array - for now just take the first value $temp = array_pop($temp); } if ((is_array($temp)) and (amr_is_assoc($temp) ) ){ foreach ($temp as $i3 => $v3) { if (is_array($v3) and function_exists('amr_dig_deeper')) { // *** needs work still //if (WP_DEBUG) echo'
    ** go down a level for '.$i3; $key2 = $key.'_'.str_replace(' ','_', $mk); /* html does not like spaces in the names*/ $subkeys = amr_get_next_level_down($mk, $key2, $v3); //if (WP_DEBUG) echo '
    **** got back '.$subkeys; $keys = array_merge($keys,$subkeys); } else { $mkey = $key.'-'.str_replace(' ','_', $i3); /* html does not like spaces in the names*/ $keys[$mkey] = $mkey; if (!isset($orig_mk[$mkey])) { $orig_mk[$mkey] = $mk; echo '
    '.__('Added complex meta to report DB: ','amr-users').$mkey.'
    '; } else { //echo ' ✓'.$mkey; } } } } else { // not an array $keys[$key] = $key; if (!isset($orig_mk[$key])) { // was !empty before $orig_mk[$key] = $mk; //echo '
    '.__('Added meta to report DB: ','amr-users').$key.'
    '; } else { //echo ' .'; // ✓'.$key.' '; // already had this, just indicate volume perhaps } } } else { // the value is empty so we cannot dig down if (!isset ($keys[$mk])) { //if (!isset ($orig_mk[$key])) { $keys[$mk] = $mk; if (!isset($orig_mk[$mk])) { $orig_mk[$mk] = $mk; // same same //echo '
    '.__('Added to report DB: ','amr-users').$mk.'
    '; } //else echo ' ✓'; } } } } unset($all); //if (WP_DEBUG) {echo '
    In Debug Only: Original keys mapping: '; var_dump($orig_mk);} ausers_update_option('amr-users-original-keys', $orig_mk); echo '
    '; //if (WP_DEBUG) {echo '
    For Debug: Merged keys'; var_dump($keys);} return ($keys); } function ameta_rebuildnicenames (){ global $wpdb,$amr_nicenames; /* */ // amr_users_message (__('Rebuilding List of possible fields. This could take a while - I have to query evey meta record, of which there can be multiple for each main record. Please be patient...', 'amr-users')); /* check if we have some options already in Database. - use their names, if not, use default, else overwrite .*/ flush(); /* try does not always work */ $oldnn = ausers_get_option('amr-users-nicenames'); $nn = ameta_defaultnicenames(); /* get the default list names required */ /* Add any new fields in */ unset($list); $list = amr_get_alluserkeys(); /* maybe only do this if a refresh is required ? No only happens on admin anyway ? */ //echo '

    '.__('Try to make some nicer names.', 'amr-users').'

    '; /**** wp has changed - need to alllow for prefix now on fields. Actually due to wpmu - keep the prefix, let the user remove it! */ foreach ($list as $i => $v) { if (empty( $nn[$v])) { /* set a reasonable default nice name */ if (!empty($oldnn[$v])) { $nn[$v] = $oldnn[$v]; //echo '.'; //echo '
    '. sprintf(__('Use existing name %s for %s', 'amr-users'),$nn[$v],$v); } else { // take the last part of the field only - no not nice too unpredictable //$lastdash = strripos($v,'-'); //$nn[$v] = substr($v, $lastdash); $nn[$v] = $v; $nn[$v] = apply_filters('amr-users-nicenames-first',$nn[$v]); $nn[$v] = str_replace('s2member_custom_fields','s2m',$nn[$v]); // if it is a s2member field - reduce length of name $nn[$v] = (str_replace('s2member', 's2m',$nn[$v])); $nn[$v] = (str_replace('capabilities', 'Cap',$nn[$v])); $nn[$v] = (str_replace('-', ' ',$nn[$v])); // if (isset ($wpdb->prefix)) {$nn[$v] = str_replace ($wpdb->prefix, '', $nn[$v]);} /* Note prefix has underscore*/ $nn[$v] = trim($nn[$v],'_'); $nn[$v] = (str_replace('_', ' ',$nn[$v])); $nn[$v] = ucwords ($nn[$v]); //echo '
    '. sprintf(__('Created name %s for %s', 'amr-users'),$nn[$v],$v); $nn[$v] = apply_filters('amr-users-nicenames-last',$nn[$v]); // allow so can reset nice names now and then from addon } } } unset($list); amr_check_for_table_prefixes($nn) ; ausers_update_option('amr-users-nicenames',$nn); $amr_nicenames = $nn; return($nn); } function amr_check_for_table_prefixes ($nn) { // use a field that is always there and has the table prefixes $prefixes_in_use = array(); //$checkfield = 'user-settings-time'; $checkfield = 'capabilities'; foreach ($nn as $i=> $n) { if (stristr($i, $checkfield)) { $prefixes_in_use[] = substr($i, 0, strpos($i,$checkfield)); //str_replace($checkfield, '', $i); } } ausers_update_option('amr-users-prefixes-in-use', $prefixes_in_use); } function amr_meta_nice_names_page() { /* may be able to work generically */ global $amr_nicenames; global $ausersadminurl; //amr_meta_main_admin_header('Find fields, make nice names' ); amr_meta_admin_headings ($plugin_page=''); // does the nonce check etc if (isset($_POST['action']) and !($_POST['action'] === "save")) return; echo PHP_EOL.'
     
    '.PHP_EOL; // NB do not check the values of submit fields as they may be translated. if (isset($_POST['update']) ) { // and ($_POST['update'] === "Update")) {/* Validate the input and save */ if (amrmeta_validate_nicenames()) { // updates inside the function now } else echo '

    '.__('Validation failed', 'amr-users').'

    '; } if (isset($_POST['resetnice'])) { if (ausers_delete_option ('amr-users-nicenames')) _e('Deleting all nice name settings in database','amr-users'); echo ' '; if (ausers_delete_option ('amr-users-nicenames-excluded')) _e('Deleting all nice name exclusion settings in database','amr-users'); echo ' '; if (ausers_delete_option ('amr-users-original-keys')) _e('Deleting original keys mapping in database','amr-users'); echo ' '; if (ausers_delete_option ('amr-users-field-types')) _e('Deleting field type specifications in database','amr-users'); echo '
    '; } if (isset($_POST['rebuild']) or isset($_POST['resetnice'])) { $amr_nicenames = ameta_rebuildnicenames (); _e('Rebuild Complete.', 'amr-users'); return; } else { amrmeta_check_find_fields(); } $amr_nicenames = ausers_get_option('amr-users-nicenames'); ameta_list_nicenames_for_input($amr_nicenames); echo ausers_form_end(); } //end amrmeta nice names option_page