/** * Output the metabox used to view relationships * * @param object $user */ function wp_user_parents_metabox($user = false) { // Bail if no rental if (empty($user)) { return; } // Get user IDs $parent_ids = wp_get_user_parents($user->ID); $child_ids = wp_get_user_children($user->ID); ?> <table class="form-table"> <?php // Start an output buffer ob_start(); // Parents row wp_user_profiles_do_row(array('user' => $user, 'type' => 'parents', 'parent_ids' => $parent_ids, 'child_ids' => $child_ids)); // Children row wp_user_profiles_do_row(array('user' => $user, 'type' => 'children', 'parent_ids' => $parent_ids, 'child_ids' => $child_ids)); // Get the rows $rows = ob_get_clean(); // Output rows, or message why there are no rows if (!empty($rows)) { echo $rows; } else { ?> <tr><td colspan="2"><?php esc_html_e('This account currently has no available relationship options.', 'wp-user-parents'); ?> </td></tr><?php } ?> </table> <?php }
/** * Update user parents * * @since 0.1.0 */ function wp_user_parents_save_meta_data($user_id = 0) { // Bail if no page if (empty($_GET['page'])) { return; } // Bail if not saving the 'account' section if (sanitize_key($_GET['page']) !== 'account') { return; } /** Parents ***************************************************************/ // Attempt to save parents $posted_parent_ids = !empty($_POST['wp_user_parents']) ? wp_parse_id_list($_POST['wp_user_parents']) : array(); // Delete user parents delete_user_meta($user_id, 'user_parent'); // Add user metas foreach ($posted_parent_ids as $id) { if (!wp_is_user_parent_of_user($id, $user_id)) { add_user_meta($user_id, 'user_parent', $id, false); } } /** Children **************************************************************/ // Attempt to save parents $posted_children_ids = !empty($_POST['wp_user_children']) ? wp_parse_id_list($_POST['wp_user_children']) : array(); // Get parent user IDs $children_ids = wp_get_user_children($user_id); foreach ($children_ids as $child_id) { delete_user_meta($child_id, 'user_parent', $user_id); } // Add user metas foreach ($posted_children_ids as $id) { if (!wp_is_user_child_of_user($user_id, $id)) { add_user_meta($id, 'user_parent', $user_id, false); } } }