Skip to content

Commit

Permalink
Merge pull request #3 from RomasJankauskas/main
Browse files Browse the repository at this point in the history
Adding new functionality to create Omnisend contacts from wp users when plugin first time activated without errors
  • Loading branch information
greta-mik authored Sep 11, 2024
2 parents dbc483a + 80d32c0 commit dd76b5a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
14 changes: 14 additions & 0 deletions class-omnisend-paidmembershipsproaddon.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Omnisend\PaidMembershipsProAddon\Actions\OmnisendAddOnAction;
use Omnisend\PaidMembershipsProAddon\Service\SettingsService;
use Omnisend\PaidMembershipsProAddon\Service\ConsentService;
use Omnisend\PaidMembershipsProAddon\Service\OmnisendApiService;

if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand All @@ -31,6 +32,7 @@
add_action( 'plugins_loaded', array( 'Omnisend_PaidMembershipsProAddOn', 'check_plugin_requirements' ) );
add_action( 'admin_enqueue_scripts', array( 'Omnisend_PaidMembershipsProAddOn', 'load_custom_wp_admin_style' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( 'Omnisend_PaidMembershipsProAddOn', 'add_settings_link' ) );
register_activation_hook( __FILE__, array( 'Omnisend_PaidMembershipsProAddOn', 'pmp_plugin_activate' ) );

$omnisend_pmp_addon_settings = new SettingsService();
$omnisend_pmp_addon_consent = new ConsentService();
Expand Down Expand Up @@ -145,6 +147,18 @@ public static function paid_memberships_pro_notice() {
echo '<div class="error"><p>' . esc_html__( 'Plugin Omnisend for Paid Membership Pro Add-On is deactivated. Please install and activate Paid Memberships Pro plugin.', 'omnisend-paid-memberships-pro' ) . '</p></div>';
}

/**
* Check if addon is activated for the first time
*/
public static function pmp_plugin_activate() {
if ( is_admin() && ! get_option( 'pmp_initial_sync_made' ) ) {
$omnisend_api_service = new OmnisendApiService();
$omnisend_api_service->create_users_as_omnisend_contacts();

add_option( 'pmp_initial_sync_made', true );
}
}

/**
* Loading styles in admin.
*/
Expand Down
26 changes: 26 additions & 0 deletions includes/Mapper/class-contactmapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ public function create_contact( array $mapped_fields ): Contact {
return $contact;
}

/**
* Create all users as Omnisend contacts as non-subscribers
*
* @param array $user_info
*
* @return Contact object
*/
public function create_contact_from_user_info( array $user_info ): Contact {
$contact = new Contact();

$contact->set_email( $user_info['email'] );
$contact->set_phone( $user_info['phone'] );
$contact->set_first_name( $user_info['first_name'] );
$contact->set_last_name( $user_info['last_name'] );
$contact->set_city( $user_info['city'] );
$contact->set_state( $user_info['state'] );
$contact->set_postal_code( $user_info['zipcode'] );
$contact->set_country( $user_info['country'] );
$contact->set_address( $user_info['address1'] . ' ' . $user_info['address2'] );

$contact->add_custom_property( 'membership_level', $user_info['level_name'] );
$contact->add_tag( self::CUSTOM_PREFIX );

return $contact;
}

/**
* Update Omnisend contact on profile form change.
*
Expand Down
44 changes: 44 additions & 0 deletions includes/Service/class-omnisendapiservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,50 @@ public function create_omnisend_contact( array $form_data ): array {
);
}

/**
* Creates Omnisend contacts from existing users when plugin is activated.
*/
public function create_users_as_omnisend_contacts(): void {
$all_users = get_users();
$non_admin_users = array_filter(
$all_users,
function ( $user ) {
return ! in_array( 'administrator', $user->roles );
}
);

if ( empty( $non_admin_users ) ) {
return;
}

foreach ( $non_admin_users as $user ) {
$level = pmpro_getMembershipLevelForUser( $user->ID );

$pmpro_user_level_name = '';

if ( $level ) {
$pmpro_user_level_name = $level->name;
}

$user_info = array(
'first_name' => get_user_meta( $user->ID, 'pmpro_bfirstname', true ),
'last_name' => get_user_meta( $user->ID, 'pmpro_blastname', true ),
'address1' => get_user_meta( $user->ID, 'pmpro_baddress1', true ),
'address2' => get_user_meta( $user->ID, 'pmpro_baddress2', true ),
'city' => get_user_meta( $user->ID, 'pmpro_bcity', true ),
'state' => get_user_meta( $user->ID, 'pmpro_bstate', true ),
'zipcode' => get_user_meta( $user->ID, 'pmpro_bzipcode', true ),
'country' => get_user_meta( $user->ID, 'pmpro_bcountry', true ),
'phone' => get_user_meta( $user->ID, 'pmpro_bphone', true ),
'email' => $user->data->user_email,
'level_name' => $pmpro_user_level_name,
);

$contact = $this->contact_mapper->create_contact_from_user_info( $user_info );
$this->client->save_contact( $contact );
}
}

/**
* Update Omnisend contact by editing profile form.
*
Expand Down

0 comments on commit dd76b5a

Please sign in to comment.