Skip to content

Instantly share code, notes, and snippets.

@vfontjr
Last active April 19, 2024 19:41
Show Gist options
  • Save vfontjr/263cdf33d5ea5a84e0f0601406ff99b9 to your computer and use it in GitHub Desktop.
Save vfontjr/263cdf33d5ea5a84e0f0601406ff99b9 to your computer and use it in GitHub Desktop.
Masterminds user role add/remove
<?php
/**
* masterminds_update_user_roles function.
*
* This is the main callback function for the
* frm_after_create_entry and frm_after_update_entry actions
* It's purpose is to process Developers Directory registrations
* and grant various WordPress roles based on user responses
*
* @access public
* @param mixed $entry_id
* @param mixed $form_id
* @return void
*/
function masterminds_update_user_roles($entry_id, $form_id) {
/* get form id from key */
$target_form = FrmForm::get_id_by_key( "masterminds-directory" );
if ( $form_id == $target_form ) {
/* retrieve the pertinent field values from the PHP $_POST array */
/* start with the user id */
$user_id = $_POST['frm_user_id'];
if ( $user_id == '1' ) {
return;
}
$user = get_userdata( $user_id );
/* get the fields ids from their keys for transportable code */
$devdir_display_name = FrmField::get_id_by_key( "devdir_display_name" );
$devdir_digital_store = FrmField::get_id_by_key( "devdir_digital_store" );
$devdir_digital_store_policy = FrmField::get_id_by_key( "devdir_digital_store_policy" );
$devdir_codex_volunteer = FrmField::get_id_by_key( "devdir_codex_volunteer" );
$devdir_codex_policy = FrmField::get_id_by_key( "devdir_codex_policy" );
$devdir_content_contributor = FrmField::get_id_by_key( "devdir_content_contributor" );
$devdir_content_policy = FrmField::get_id_by_key( "devdir_content_policy" );
$devdir_name_of_store = FrmField::get_id_by_key( "devdir_name_of_store" );
$devdir_store_contact_form_email_email = FrmField::get_id_by_key( "devdir_store_contact_form_email_email" );
/* developer role */
$user_has_developer_role = masterminds_does_user_have_role("developer", $user->roles );
/* fields specific to EDD */
$name_of_store = ( isset( $_POST['item_meta'][$devdir_name_of_store]) && !empty( $_POST['item_meta'][$devdir_name_of_store] ) ) ? $_POST['item_meta'][$devdir_name_of_store] : $_POST['item_meta'][$devdir_display_name];
$email_to_use_for_contact_form = ( isset( $_POST['item_meta'][$devdir_store_contact_form_email_email]) && !empty( $_POST['item_meta'][$devdir_store_contact_form_email_email] ) ) ? $_POST['item_meta'][$devdir_store_contact_form_email_email] : $user->data->user_email;
/* vendor store role */
$dev_store = $_POST['item_meta'][$devdir_digital_store];
$dev_store_agree = $_POST['item_meta'][$devdir_digital_store_policy];
$user_has_vendor_role = masterminds_does_user_have_role( "developer_store_owner", $user->roles );
/* codex volunteer */
$codex_volunteer = $_POST['item_meta'][$devdir_codex_volunteer];
$codex_volunteer_agree = $_POST['item_meta'][$devdir_codex_policy];
$user_has_codex_role = masterminds_does_user_have_role( "codex", $user->roles );
/* contributor role */
$create_content = $_POST['item_meta'][$devdir_content_contributor];
$create_content_agree = $_POST['item_meta'][$devdir_content_policy];
$user_has_contributor_role = masterminds_does_user_have_role( "dev_contributor", $user->roles );
/* now for the "working" code */
/* all registered users get the developer role */
if ( ! $user_has_developer_role ) {
masterminds_add_or_remove_user_role( $user, "developer", "add");
}
if ( !$user_has_vendor_role && $dev_store == 'Yes' && $dev_store_agree == 'Yes' ) {
masterminds_add_or_remove_user_role( $user, "developer_store_owner", "add");
masterminds_open_or_close_store($user, "open");
masterminds_maybe_add_or_delete_fes_user_meta( "add", $user_id, $name_of_store, $email_to_use_for_contact_form );
} elseif ( $user_has_vendor_role && ( $dev_store == 'No' || $dev_store_agree == 'No' || $dev_store_agree == '' ) ) {
masterminds_add_or_remove_user_role( $user, "developer_store_owner", "remove");
masterminds_open_or_close_store($user, "close");
masterminds_maybe_add_or_delete_fes_user_meta( "delete", $user_id, $name_of_store, $email_to_use_for_contact_form );
}
if ( !$user_has_codex_role && $codex_volunteer == 'Yes' && $codex_volunteer_agree == 'Yes' ) {
masterminds_add_or_remove_user_role( $user, "codex", "add");
} elseif ( $user_has_codex_role && ( $codex_volunteer == 'No' || $codex_volunteer_agree == 'No' || $codex_volunteer_agree == '' ) ) {
masterminds_add_or_remove_user_role( $user, "codex", "remove");
}
if ( !$user_has_contributor_role && $create_content == 'Yes' && $create_content_agree == 'Yes' ) {
masterminds_add_or_remove_user_role( $user, "dev_contributor", "add");
} elseif ( $user_has_contributor_role && ( $create_content == 'No' || $create_content_agree == 'No' || $create_content_agree == '' ) ) {
masterminds_add_or_remove_user_role( $user, "dev_contributor", "remove");
}
}
}
add_action('frm_after_create_entry', 'masterminds_update_user_roles', 30, 2);
add_action('frm_after_update_entry', 'masterminds_update_user_roles', 10, 2);
/**
* masterminds_reset_user_roles_after_email_verification function.
*
* We hate duplicating working code, but this is necessary to
* work around Formidable's use of set_role() when
* Email Moderation is enabled. set_role() wipes all
* Custom WordPress user roles added to a user profile
* by Formidable hooks
*
* @access public
* @param numeric $user_id
* @param mixed $args
* @return void
*/
function masterminds_reset_user_roles_after_email_verification( $user_id, $args ) {
/* get the fields ids from their keys for transportable code */
$devdir_digital_store = FrmField::get_id_by_key( "devdir_digital_store" );
$devdir_digital_store_policy = FrmField::get_id_by_key( "devdir_digital_store_policy" );
$devdir_codex_volunteer = FrmField::get_id_by_key( "devdir_codex_volunteer" );
$devdir_codex_policy = FrmField::get_id_by_key( "devdir_codex_policy" );
$devdir_content_contributor = FrmField::get_id_by_key( "devdir_content_contributor" );
$devdir_content_policy = FrmField::get_id_by_key( "devdir_content_policy" );
/* get the user object */
$user = get_userdata( $user_id );
/* get entry id */
$entry_id = $args['entry']->id;
/* get the field values from the frm_item_metas */
/* check for roles - all of these should be false after Formidable's call to set_role() */
$user_has_developer_role = masterminds_does_user_have_role("developer", $user->roles );
if ( ! $user_has_developer_role ) {
masterminds_add_or_remove_user_role( $user, "developer", "add");
}
/* vendor store role */
$dev_store = FrmEntryMeta::get_entry_meta_by_field( $entry_id, $devdir_digital_store );
$dev_store_agree = FrmEntryMeta::get_entry_meta_by_field( $entry_id, $devdir_digital_store_policy );
$user_has_vendor_role = masterminds_does_user_have_role( "developer_store_owner", $user->roles );
if ( !$user_has_vendor_role && $dev_store == 'Yes' && $dev_store_agree == 'Yes' ) {
/* masterminds_add_or_remove_user_role( $user, "developer_store_owner", "add");
* masterminds_open_or_close_store($user, "open"); */
} elseif ( $user_has_vendor_role && ( $dev_store == 'No' || $dev_store_agree == 'No' || $dev_store_agree == '' ) ) {
/* masterminds_add_or_remove_user_role( $user, "developer_store_owner", "remove");
* masterminds_open_or_close_store($user, "close"); */
}
/* codex volunteer */
$codex_volunteer = FrmEntryMeta::get_entry_meta_by_field( $entry_id, $devdir_codex_volunteer );
$codex_volunteer_agree = FrmEntryMeta::get_entry_meta_by_field( $entry_id, $devdir_codex_policy );
$user_has_codex_role = masterminds_does_user_have_role( "codex", $user->roles );
if ( !$user_has_codex_role && $codex_volunteer == 'Yes' && $codex_volunteer_agree == 'Yes' ) {
masterminds_add_or_remove_user_role( $user, "codex", "add");
} elseif ( $user_has_codex_role && ( $codex_volunteer == 'No' || $codex_volunteer_agree == 'No' || $codex_volunteer_agree == '' ) ) {
masterminds_add_or_remove_user_role( $user, "codex", "remove");
}
/* contributor role */
$create_content = FrmEntryMeta::get_entry_meta_by_field( $entry_id, $devdir_content_contributor );
$create_content_agree = FrmEntryMeta::get_entry_meta_by_field( $entry_id, $devdir_content_policy );
$user_has_contributor_role = masterminds_does_user_have_role( "dev_contributor", $user->roles );
if ( !$user_has_contributor_role && $create_content == 'Yes' && $create_content_agree == 'Yes' ) {
masterminds_add_or_remove_user_role( $user, "dev_contributor", "add");
} elseif ( $user_has_contributor_role && ( $create_content == 'No' || $create_content_agree == 'No' || $create_content_agree == '' ) ) {
masterminds_add_or_remove_user_role( $user, "dev_contributor", "remove");
}
}
add_action('frmreg_after_create_user', 'masterminds_reset_user_roles_after_email_verification', 10, 2);
/**
* masterminds_add_or_remove_user_role function.
*
* @access public
* @param WordPress User Object $user
* @param string $user_role
* @param string $action
* @return void
*/
function masterminds_add_or_remove_user_role( $user, $user_role, $action ) {
if ( $user && $user_role ) {
if ( ! $user->has_cap('administrator') ) {
if ( $action == "add") {
$user->add_role( $user_role );
} else {
$user->remove_role( $user_role );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment