Files
harpLabs_Inc/includes/acf_functions.php
2024-05-03 01:55:19 -04:00

209 lines
6.5 KiB
PHP

<?php
//ACF USER MANAGER
function my_acf_user_form_func($atts, $showForm, $register)
{
acf_link_to_tab();
$a = shortcode_atts(array(
'field_group' => ''
), $atts);
$uid = get_current_user_id();
if ($register || (!empty($a['field_group']) && !empty($uid))) {
$options = array(
'post_id' => 'user_' . $uid,
'field_groups' => array(intval($a['field_group'])),
'updated_message' => $register ? "Profile Registered" : "Profile Updated",
//'submit_value' => $register ? "Register" :"Update",
/*Choices of 'top' (Above fields) or 'left' (Beside fields) */
'label_placement' => 'left',
'html_submit_button' => '<div class="et_pb_button_module_wrapper et_pb_button_8_wrapper et_pb_button_alignment_center et_pb_module "><a style="color: #8fbb4a;" class="et_pb_button et_pb_button_0 et_pb_bg_layout_light" href="javascript:jQuery(\'#acf-form\').submit();">' . ($register ? "Save" : "Update") . '</a></div>',
//'<input type="submit" class="acf-button button button-primary button-large" value="%s" />',
/* (boolean) Whether or not to create a form element. Useful when a adding to an existing form. Defaults to true */
'form' => $showForm,
'return' => add_query_arg('updated', 'true', get_permalink())
);
ob_start();
acf_form($options);
$form = ob_get_contents();
ob_end_clean();
}
return $form;
}
function my_acf_user_form_showProfile($atts)
{
return my_acf_user_form_func($atts, false, false);
}
function my_acf_user_form_editProfile($atts)
{
return my_acf_user_form_func($atts, true, false);
}
function my_acf_user_form_register($atts)
{
return my_acf_user_form_func($atts, true, true);
}
add_shortcode('my_acf_user_form', 'my_acf_user_form_editProfile');
add_shortcode('my_acf_user_show_form', 'my_acf_user_form_showProfile');
add_shortcode('my_acf_user_register_form', 'my_acf_user_form_register');
//adding AFC form head
function add_acf_form_head()
{
global $post;
if (!empty($post) && (has_shortcode($post->post_content, 'my_acf_user_form') || has_shortcode($post->post_content, 'my_acf_user_show_form') || has_shortcode($post->post_content, 'my_acf_user_register_form'))) {
acf_form_head();
}
/*if ( !empty($post) && ( has_shortcode( $post->post_content, 'my_acf_user_register_form' )) ) {
//create user first
//prepare basic user info
$password = wp_generate_password( 10, true, false );
$username = $_POST['fields']['field_5dbca1ef8994b'];
$email = $_POST['fields']['field_5dbca1ef8994b'];
//register user
$user_id = wp_create_user( $username, $password, $email );
wp_update_user( array(
'ID' => $user_id,
'first_name' => $_POST['fields']['field_5dbc8a9f6d904'],
'last_name' => $_POST['fields']['field_5dbc8ad36d905'],
'display_name' => $_POST['fields']['field_5dbc8a9f6d904'] . ' ' . $_POST['fields']['field_5dbc8ad36d905']
) );
acf_form_head();
}*/
}
add_action('wp_head', 'add_acf_form_head', 7);
// set ACF pre-save stuff
add_filter('acf/pre_save_post', 'acf_create_new_user');
function acf_create_new_user($post_id)
{
global $post;
if (!empty($post) && (has_shortcode($post->post_content, 'my_acf_user_register_form'))) {
error_log('post id: ' . $post_id);
// // exit if user already logged in, front-end form sent his ID
// if ( $post_id != 'new' ) {
// error_log( 'post not new: ' . $post_id);
// // wp_redirect( home_url() );
// return $post_id;
// }
$field_group = acf_get_fields_by_id('4067');
//prepare basic user info
foreach ($field_group as $key => $value) {
//
"<br/>+++<br/>" . $value['name'];
//echo ":".$_POST['acf'][$value['key']]."<br/>+++<br/>";
${$value['name']} = $_POST['acf'][$value['key']];
}
$password = wp_generate_password(10, true, false);
$username = $email;
$display_name = $first_name . ' ' . $last_name;
// check if user exists, exit if so
if (email_exists($email)) {
error_log('email exists: ' . $email);
wp_redirect(home_url() . '/email-exists?email_exists&user_email=' . $email);
exit;
}
// register user
$user_id = wp_create_user($username, $password, $email);
error_log('guy just created: ' . $user_id);
// Fetch the WP_User object of our user.
$u = new WP_User($user_id);
// Replace the current role with 'editor' role
$u->set_role($role);
// update other user info
wp_update_user(array(
'ID' => $user_id,
'first_name' => $first_name,
'last_name' => $last_name,
'display_name' => $display_name
));
// update_user_meta( $user_id, '_um_status', 'pending' );
// update other user meta, prevent creation of post data
foreach ($field_group as $key => $value) {
update_user_meta($user_id, $value['name'], $_POST['acf'][$value['key']]);
unset($_POST['acf'][$value['key']]);
}
update_user_meta($user_id, 'account_status', 'awaiting_admin_review');
//wp_new_user_notification( $user_id, $password);
// redirect to login page after finish
wp_redirect(home_url() . '/registered?new_user=true&user_email=' . $email);
exit; //return 'user_' . $user_id;
}
return $post_id;
}
add_filter('acf/validate_value/name=email', 'my_acf_validate_email', 10, 4);
function my_acf_validate_email($valid, $value, $field, $input)
{
if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
return $valid;
} else {
$valid = "$value is not a valid email address";
}
return $valid;
}
function acf_link_to_tab()
{ ?>
<script>
(function($) {
acf.add_action('ready', function() {
if (location.hash.length > 1) {
var hash = location.hash.substring(1);
$('.acf-tab-wrap .acf-tab-button').each(function(i, button) {
if (hash == $(button).text().toLowerCase().replace(' ', '-')) {
$(button).trigger('click');
return false;
}
});
}
});
})(jQuery);
</script><?php
}