Skip to content

Instantly share code, notes, and snippets.

@sunriseweb
Created October 2, 2013 13:30
Show Gist options
  • Save sunriseweb/6793807 to your computer and use it in GitHub Desktop.
Save sunriseweb/6793807 to your computer and use it in GitHub Desktop.
AutoChimp WordPress Profile Builder plugin - when placed in the /wp-content/plugins/autochimp/plugins directory will allow sync of extra user fields created with Profile Builder.
<?php
/* Plugin that syncs with WordPress Profile Builder Plugin fields */
define( 'WP88_MC_SYNC_WPPB', 'wp88_mc_sync_wppb' );
define( 'WP88_WPPB_FIELD_MAPPING', 'wp88_mc_wppb_' );
class SyncProfileBuilderPro extends ACSyncPlugin
{
public function SyncProfileBuilderPro()
{
}
public static function GetInstalled()
{
return class_exists( 'PB_Admin' );
}
public static function GetUsePlugin()
{
return get_option( WP88_MC_SYNC_WPPB );
}
public static function GetSyncVarName()
{
return 'on_sync_wppb';
}
public static function GetSyncDBVarName()
{
return WP88_MC_SYNC_WPPB;
}
public function ShowPluginSettings()
{
// Get settings
$syncWPPB = SyncProfileBuilderPro::GetUsePlugin();
$varName = SyncProfileBuilderPro::GetSyncVarName();
$staticText = get_option( WP88_MC_STATIC_TEXT );
// Start outputting UI
print '<p><strong>You are using the <a target="_blank" href="http://wordpress.org/plugins/profile-builder/">Profile Builder</a></strong> plugin. With AutoChimp, you can automatically synchronize your Profile Builder User Fields with your selected MailChimp mailing list as users join your site and update their profile. Please ensure that only one list is selected.</p>';
print '<fieldset style="margin-left: 20px;">';
// Create a hidden field just to signal that the user can save their preferences
// even if the sync button isn't checked
print '<input type="hidden" name="wppb_running" />';
print "<p><input type=CHECKBOX value=\"$varName\" name=\"$varName\" ";
if ( '1' === $syncWPPB )
print 'checked';
print '> Automatically sync Profile Builder User Extra Fields with MailChimp.</p>';
print '</fieldset>';
}
//
// The key to AutoChimp's efficiency is that the name of the select box and
// the option_name field in the database are the same.
//
public function GenerateMappingsUI( $tableWidth, $mergeVars )
{
$pb_options = get_option('wppb_custom_fields');
if (is_array($pb_options)){
// Create a hidden field just to signal that the user can save their preferences
print '<br />'.PHP_EOL.'<input type="hidden" name="wppb_running" />'.PHP_EOL;
foreach ($pb_options as $key => $single_option){
$fieldNameTag = AC_EncodeUserOptionName( WP88_WPPB_FIELD_MAPPING, $single_option['item_metaName'] );
$selectBox = AC_GenerateSelectBox( $fieldNameTag, WP88_IGNORE_FIELD_TEXT, $mergeVars );
$rowCode .= '<tr class="alternate">' . PHP_EOL . '<td width="35%">' . $single_option['item_metaName'] . '</td>' . PHP_EOL . '<td width="35%">' . $selectBox . '</td>' . PHP_EOL . '</tr>' . PHP_EOL;
}
print( AC_GenerateFieldMappingCode( 'WPPB', $rowCode ) );
}
}
//
// This function uses the global $_POST variable, so only call it at the appropriate times.
// Consider refactoring this function to make it not dependent on $_POST.
//
public function SaveMappings()
{
$pb_options = get_option('wppb_custom_fields');
foreach ($pb_options as $key => $single_option)
{
// Encode the name of the field
$selectName = AC_EncodeUserOptionName( WP88_WPPB_FIELD_MAPPING, $single_option['item_metaName'] );
// Now dereference the selection
$selection = $_POST[ $selectName ];
// Save the selection
update_option( $selectName, $selection );
}
}
//
// Looks up the user's Profile Builder data and returns a meaningful array of
// associations to the user based on what the AutoChimp needs to sync.
//
public function FetchMappedData( $userID )
{
// User data array
$dataArray = array();
// Need to query data in the WordPress options table
global $wpdb;
// Generate table names
$optionTable = $wpdb->prefix . 'options';
// Now, see which Profile Builder fields the user wants to sync.
$sql = "SELECT option_name,option_value FROM $optionTable WHERE option_name LIKE '" .
WP88_WPPB_FIELD_MAPPING .
"%' AND option_value != '" .
WP88_IGNORE_FIELD_TEXT . "'";
$fieldNames = $wpdb->get_results( $sql, ARRAY_A );
// Get the stored data
$metadata = get_metadata( 'user', $userID );
// And finally, get the user data for each field name.
foreach( $fieldNames as $field )
{
$optionName = AC_DecodeUserOptionName( WP88_WPPB_FIELD_MAPPING, $field['option_name'] );
// $optionName = strtolower($field['option_value']);
// The data is in the 0th element of an array belonging to the hash.
$value = $metadata[strtolower($field['option_value'])][0];
$dataArray[] = array( "name" => $optionName,
"tag" => $field['option_value'],
"value" => $value );
}
return $dataArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment