Skip to content

Commit

Permalink
Merge branch 'feature/taxonomies' into dev
Browse files Browse the repository at this point in the history
Conflicts:
	pmpro-register-helper.php
  • Loading branch information
ideadude committed Jul 22, 2022
2 parents 07629dd + 494e73a commit a6bf352
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 8 deletions.
101 changes: 93 additions & 8 deletions classes/class.field.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function defaults() {
$this->just_profile = false;
$this->class = null;
$this->sanitize = true;
$this->taxonomy = false;
}

/*
Expand Down Expand Up @@ -100,6 +101,31 @@ function set($name, $type, $attr = array())
$this->save_function = array( $this, 'saveUsersTable' );
}

// Save user taxonomy fields to wp_term_relationships, not usermeta.
if ( ! empty( $this->taxonomy ) ) {
// Use the save taxonomy function.
$this->save_function = array( $this, 'saveTermRelationshipsTable' );

// Populate terms from the taxonomy if options are empty.
if ( empty( $this->options ) ) {
$terms = get_terms( $this->taxonomy, array( 'hide_empty' => false ) );
if ( isset( $terms->errors ) ) {
$this->options = array();
} else {
// Note: wp_list_pluck was causing issues here.
$this->options = array();
foreach( $terms as $term ) {
$this->options[$term->term_id] = $term->name;
}
}
}

// If select, let's add an empty option to the top.
if ( $this->type == "select" ) {
$this->options = array('') + $this->options;
}
}

//default fields
if($this->type == "text")
{
Expand All @@ -118,8 +144,8 @@ function set($name, $type, $attr = array())
$this->options = array("", "- choose one -");

//is a non associative array is passed, set values to labels
$repair_non_associative_options = apply_filters("pmprorh_repair_non_associative_options", true);
if($repair_non_associative_options && !$this->is_assoc($this->options))
$repair_non_associative_options = apply_filters("pmprorh_repair_non_associative_options", true );
if($repair_non_associative_options && ! $this->is_assoc( $this->options ) )
{
$newoptions = array();
foreach($this->options as $option)
Expand Down Expand Up @@ -178,6 +204,37 @@ function saveUsersTable( $user_id, $name, $value ) {
wp_update_user( array( 'ID' => $user_id, $name => $value ) );
}

// Save function for user taxonomy field.
function saveTermRelationshipsTable( $user_id, $name, $value ) {
// We expect an array below.
if ( ! is_array( $value ) ) {
$value = array( $value );
}

// Get term ids if slugs were passed in.
$new_values = array();
foreach ( $value as $term_id ) {
if ( is_numeric( $term_id ) ) {
$new_values[] = intval( $term_id );
} else {
// Assume slug passed for some reason.
$term_object = get_term_by( 'slug', $term_id, $this->taxonomy );
$new_values[] = intval( $term->term_id );
}
}

// Make sure term ids are already terms for this taxonomy.
$valid_terms = get_terms( $this->taxonomy, array( 'hide_empty' => false ) );
$valid_term_ids = wp_list_pluck( $valid_terms, 'term_id' );
$new_values = array_intersect( $valid_term_ids, $new_values );

// Sets the terms for the user.
wp_set_object_terms( $user_id, $new_values, $this->taxonomy, false );

// Remove the user taxonomy relationship to terms from the cache.
clean_object_term_cache( $user_id, $this->taxonomy );
}

//save function for files
function saveFile($user_id, $name, $value)
{
Expand Down Expand Up @@ -852,7 +909,16 @@ function displayAtCheckout()
global $current_user;

//value passed yet?
if($this->type == "date") {
if ( ! empty( $this->taxonomy ) ) {
$terms = wp_get_object_terms( $current_user->ID, $this->taxonomy );
if ( empty( $terms ) ) {
$value = "";
} elseif ( count( $terms ) == 1 ) {
$value = $terms[0]->term_id;
} else {
$value = wp_list_pluck( $terms, 'term_id' );
}
} elseif($this->type == "date") {
if(isset($_REQUEST[$this->name])) {
$tempstr = intval($_REQUEST[$this->name]["m"])."/";
$tempstr .= intval($_REQUEST[$this->name]["d"])."/";
Expand Down Expand Up @@ -948,8 +1014,16 @@ function displayAtCheckout()
function displayInProfile($user_id, $edit = NULL)
{
global $current_user;
if(metadata_exists("user", $user_id, $this->meta_key))
{
if ( ! empty( $this->taxonomy ) ) {
$terms = wp_get_object_terms( $user_id, $this->taxonomy );
if ( empty( $terms ) ) {
$value = "";
} elseif ( count( $terms ) == 1 ) {
$value = $terms[0]->term_id;
} else {
$value = wp_list_pluck( $terms, 'term_id' );
}
} elseif ( metadata_exists( 'user', $user_id, $this->meta_key ) ) {
$meta = get_user_meta($user_id, $this->name, true);
if(is_array($meta) && !empty($meta['filename']))
{
Expand Down Expand Up @@ -1008,9 +1082,20 @@ function displayValue($value)
echo $value;
}

//from: http://stackoverflow.com/questions/173400/php-arrays-a-good-way-to-check-if-an-array-is-associative-or-numeric/4254008#4254008
/**
* Check if an array is associative or not.
* @param array The arary to check.
* @return bool
*/
function is_assoc($array) {
return (bool)count(array_filter(array_keys($array), 'is_string'));
// Taxonomies use the term_id as keys.
if ( ! empty( $this->taxonomy ) ) {
return true;
}

// Check for string keys.
// from: http://stackoverflow.com/questions/173400/#4254008
return (bool)count(array_filter(array_keys($array), 'is_string'));
}

static function get_checkout_box_name_for_field( $field_name ) {
Expand Down Expand Up @@ -1101,4 +1186,4 @@ function was_filled_if_needed() {
return $filled;
}
}
}
}
92 changes: 92 additions & 0 deletions pmpro-register-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,98 @@ function pmprorh_getCheckoutBoxByName($name) {
function pmprorh_end($array) { return end($array); }
}

if ( ! function_exists( 'pmprorh_add_user_taxonomy' ) ) {
/**
* Add a new User Taxonomy. You can then use this as the user_taxonomny parameter to pmprorh_add_registration_field.
*
* @param string $name The singular name for the taxonomy object.
* @param string $name_plural The plural name for the taxonomy object.
*
*/
function pmprorh_add_user_taxonomy( $name, $name_plural ) {
global $pmprorh_user_taxonomies;

// Sanitize the taxonomy $name and make sure it is less than 32 characters.
$safe_name = sanitize_key( $name );
if ( strlen( $safe_name ) > 32 ) {
$safe_name = substr( $safe_name, 0, 32 );
}

// Make sure name and plural name are less than 32 characters.
if ( strlen( $name ) > 32 ) {
$name = substr( $name, 0, 32 );
}
if ( strlen( $name_plural ) > 32 ) {
$name_plural = substr( $name_plural, 0, 32 );
}

$pmprorh_user_taxonomy_labels = array(
'name' => ucwords( $name ),
'singular_name' => ucwords( $name ),
'menu_name' => ucwords( $name_plural ),
'search_items' => sprintf( __( 'Search %s', 'pmpro-register-helper' ), ucwords( $name_plural ) ),
'popular_items' => sprintf( __( 'Popular %s', 'pmpro-register-helper' ), ucwords( $name_plural ) ),
'all_items' => sprintf( __( 'All %s', 'pmpro-register-helper' ), ucwords( $name_plural ) ),
'edit_item' => sprintf( __( 'Edit %s', 'pmpro-register-helper' ), ucwords( $name ) ),
'update_item' => sprintf( __( 'Update %s', 'pmpro-register-helper' ), ucwords( $name ) ),
'add_new_item' => sprintf( __( 'Add New %s', 'pmpro-register-helper' ), ucwords( $name ) ),
'new_item_name' => sprintf( __( 'New %s Name', 'pmpro-register-helper' ), ucwords( $name ) ),
'separate_items_with_commas' => sprintf( __( 'Separate %s with commas', 'pmpro-register-helper' ), $name_plural ),
'add_or_remove_items' => sprintf( __( 'Add or remove %s', 'pmpro-register-helper' ), $name_plural ),
'choose_from_most_used' => sprintf( __( 'Choose from the most popular %s', 'pmpro-register-helper' ), $name_plural ),
);

//TO DO: Should this filter name just be formatted as core PMPro since this will be merged?
/**
* Filter the args passed to the user taxonomy created.
*
* @param array $pmprorh_user_taxonomy_args The arguments passed to the register_taxonomy function.
*
*/
$pmprorh_user_taxonomy_args = apply_filters( 'pmprorh_user_taxonomy_args', array(
'public' => false,
'labels' => $pmprorh_user_taxonomy_labels,
'rewrite' => false,
'show_ui' => true,
'capabilities' => array(
'manage_terms' => 'edit_users',
'edit_terms' => 'edit_users',
'delete_terms' => 'edit_users',
'assign_terms' => 'read',
),
)
);
register_taxonomy( $safe_name, 'user', $pmprorh_user_taxonomy_args );

/**
* Add admin page for the registered user taxonomies.
*/
add_action( 'admin_menu', function() use ( $pmprorh_user_taxonomy_labels, $safe_name ) {
add_users_page(
esc_attr( $pmprorh_user_taxonomy_labels['menu_name'] ),
esc_attr( $pmprorh_user_taxonomy_labels['menu_name'] ),
'edit_users',
'edit-tags.php?taxonomy=' . $safe_name
);
} );

/**
* Update parent file name to fix the selected menu issue for a user taaxonomy.
*/
add_filter( 'parent_file', function( $parent_file ) use ( $safe_name ) {
global $submenu_file;
if (
isset($_GET['taxonomy']) &&
$_GET['taxonomy'] == $safe_name &&
$submenu_file == 'edit-tags.php?taxonomy=' . $safe_name
) {
$parent_file = 'users.php';
}
return $parent_file;
} );
}
}

function pmprorh_sortByOrder($a, $b)
{
if ($a->order == $b->order) {
Expand Down

0 comments on commit a6bf352

Please sign in to comment.