Created
March 13, 2012 20:42
-
-
Save nathanbriggs/2031438 to your computer and use it in GitHub Desktop.
GR for s2member
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
Plugin Name: GetResponse integration for s2member | |
Description: s2Member® meets GetResponse | |
*/ | |
add_action('wp_ajax_ncb_s2member_getresponse_registration', 'ncb_s2member_getresponse_registration'); | |
add_action('wp_ajax_nopriv_ncb_s2member_getresponse_registration', 'ncb_s2member_getresponse_registration'); | |
if ( is_admin() ){ // admin actions | |
add_action('admin_init', 'ncb_s2member_getresponse_admin_init' ); | |
add_action('admin_print_styles-s2member_page_ws-plugin--s2member-els-ops', 'ncb_s2member_getresponse_admin_scripts'); | |
} else { | |
// non-admin enqueues, actions, and filters | |
} | |
function ncb_s2member_getresponse_admin_init() { | |
wp_register_script( 'ncb_s2member_getresponse_list-servers', plugins_url('/js/list-servers.js', __FILE__), array( 'jquery-ui-dialog' ), 0.1 ); | |
} | |
function ncb_s2member_getresponse_admin_scripts() { | |
wp_enqueue_script( 'ncb_s2member_getresponse_list-servers' ); | |
wp_enqueue_style( 'wp-jquery-ui-dialog' ); | |
} | |
function ncb_s2member_getresponse_registration() { | |
if( false !== ( $email = filter_input( INPUT_GET, 'email', FILTER_SANITIZE_EMAIL ) ) ) { | |
$name = filter_input( INPUT_GET, 'name', FILTER_SANITIZE_STRING ); | |
$webform = filter_input( INPUT_GET, 'webform', FILTER_SANITIZE_NUMBER_INT ); | |
$api = new NCB_RG_GetResponse( '***********YOUR GET RESPONSE API KEY ****************' ); | |
//var_dump($api->get_lists()); | |
$response = $api->add_subscriber( 'VA1W', $email, $name ); //todo: check error states & send admin email on failure? | |
//wp_mail('nath@nathanbriggs.com','diagnostics',print_r($response,1)); | |
} | |
} | |
class NCB_RG_GetResponse { | |
private $api_key; | |
const api_endpoint = 'http://api2.getresponse.com'; | |
public function __construct( $api_key ) { | |
$this->api_key = $api_key; | |
} | |
public function is_valid_key() { | |
$response = self::api_call( 'ping' ); | |
return $response === false ? false : true; // we don't need to interrogate the response (if it isn't false, it must be array( 'ping' => 'pong' ) ) | |
} | |
public function get_lists() { | |
$lists = $this->api_call( 'get_campaigns' ); | |
return $lists; | |
} | |
public function add_subscriber( $list, $email, $name = '' ) { | |
$params = array( 'campaign' => $list, 'email' => $email ); | |
if( isset( $name ) && strlen($name)>0) { | |
$params['name'] = $name; | |
} | |
var_dump($params); | |
return $this->api_call( 'add_contact', $params ); | |
} | |
public function api_call( $method, $params = array() ) { //todo: make this protected. expose dedicated functions for supported api calls | |
/* | |
if empty($response), Error (malformed query or not understood) | |
Decode json: | |
if is_null: error | |
if is_array($response) | |
if array_key_exists( 'error' ) !== null: some sort of error | |
if array_key_exists( 'response') is empty(): error | |
otherwise: | |
ok | |
*/ | |
$api_key = $this->api_key; | |
if( !empty( $params ) ) { | |
$the_params = array( $this->api_key, $params ); | |
} else { | |
$the_params = array( $this->api_key ); | |
} | |
$call_params = array( 'headers' => array( 'Content-type' => 'application/json' ), 'body' => json_encode( array( "method" => $method, "params" => $the_params ) ) ); | |
$response = wp_remote_post( self::api_endpoint, $call_params ); | |
if( is_wp_error( $response ) ) { | |
return false; | |
} else { | |
$body = wp_remote_retrieve_body( $response ); | |
var_dump($body); | |
if( is_null( $body ) ) { | |
return false; | |
} | |
$data = json_decode( $body, true ); // get the data as an array | |
if( is_null( $data ) ) { | |
return false; | |
} | |
if( !is_null( $data[ 'error' ] ) ) { | |
return false; | |
} else { | |
return $data[ 'result' ]; | |
} | |
} | |
} | |
} | |
add_action( 'ws_plugin__s2member_during_els_ops_page_after_left_sections', 'ncb_s2member_getresponse_admin_section' ); | |
function ncb_s2member_getresponse_admin_section( $vars ) { | |
extract( $vars ); | |
do_action("ws_plugin__s2member_during_els_ops_page_during_left_sections_before_ncb_getresponse", get_defined_vars()); | |
echo "<div class='ncb_s2member_getresponse_wrap'>\n"; | |
/**/ | |
echo '<div class="ws-menu-page-group" title="GetResponse® Integration">'."\n"; | |
/**/ | |
echo '<div class="ws-menu-page-section ws-plugin--s2member-ncb-getresponse-section">'."\n"; | |
echo '<a href="http://getresponse.com/?a=nathanbriggs&c=s2member-plugin-settings" target="_blank"><img src="'.plugins_url( 'images/getresponse-125.jpg', __FILE__ ).'" class="ws-menu-page-right" style="width:125px; height:125px; border:0;" alt="." /></a>'."\n"; | |
echo '<h3>GetResponse® List Server Integration ( optional )</h3>'."\n"; | |
echo '<p>s2Member can be integrated with GetResponse®. GetResponse® is an email marketing service.</p>'."\n"; | |
echo '<p>You can have your Members automatically subscribed to your GetResponse® marketing lists <em>( e.g. newsletters / auto-responders )</em>. You\'ll need a <a href="http://getresponse.com/?a=nathanbriggs&c=s2member-plugin-settings" target="_blank" rel="external">GetResponse® account</a> and your <a href="https://app.getresponse.com/my_api_key.html">GetResponse® API Key</a>.</p>'."\n"; | |
do_action("ws_plugin__s2member_during_els_ops_page_during_left_sections_during_ncb_getresponse", get_defined_vars()); | |
/**/ | |
echo '<table class="form-table">'."\n"; | |
echo '<tbody>'."\n"; | |
echo '<tr>'."\n"; | |
/**/ | |
echo '<th>'."\n"; | |
echo '<label for="ws-plugin--s2member-ncb-getresponse-api-key">'."\n"; | |
echo 'GetResponse® API Key:'."\n"; | |
echo '</label>'."\n"; | |
echo '</th>'."\n"; | |
echo "</tr>\n"; | |
/**/ | |
echo "<tr>\n"; | |
echo '<td>'."\n"; | |
echo '<input type="text" name="ws_plugin__s2member_ncb_getresponse_api_key" id="ws-plugin--s2member-ncb-getresponse-api-key" value="'.$GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["ncb_getresponse_api_key"].'" />'."\n"; | |
echo '</td>'."\n"; | |
/**/ | |
echo '</tr>'."\n"; | |
/**/ | |
for($n = 0; $n <= $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["levels"]; $n++) | |
{ | |
echo '<tr>'."\n"; | |
/**/ | |
echo '<th>'."\n"; | |
echo '<label for="ws-plugin--s2member-level'.$n.'-ncb-getresponse-list-ids">'."\n"; | |
echo 'List ID(s) for '.(($n === 0) ? 'Free Subscribers' : 'Level #'.$n.' Members').' ( comma-delimited ):'."\n"; | |
echo '</label>'."\n"; | |
echo '</th>'."\n"; | |
/**/ | |
echo '</tr>'."\n"; | |
echo '<tr>'."\n"; | |
/**/ | |
echo '<td>'."\n"; | |
echo '<input type="text" autocomplete="off" name="ws_plugin__s2member_level'.$n.'_ncb_getresponse_list_ids" id="ws-plugin--s2member-level'.$n.'-ncb-getresponse-list-ids" value="'.format_to_edit($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level".$n."_ncb_getresponse_list_ids"]).'" /><br />'."\n"; | |
echo 'New '.(($n === 0) ? 'Free Subscribers' : 'Level #'.$n.' Members').' will be subscribed to these List IDs.<br />'."\n"; | |
echo 'Ex: <code>mylist, myotherlist, anotherlist</code>'."\n"; | |
echo '</td>'."\n"; | |
/**/ | |
echo '</tr>'."\n"; | |
} | |
/**/ | |
echo '</tbody>'."\n"; | |
echo '</table>'."\n"; | |
echo '</div>'."\n"; | |
/**/ | |
echo '</div>'."\n"; | |
echo "</div>\n"; // end .ncb_s2member_getresponse_wrap | |
/**/ | |
do_action("ws_plugin__s2member_during_els_ops_page_during_left_sections_after_ncb_getresponse", get_defined_vars()); | |
} |
Hi netbloger,
Sorry to hear it isn't working for you. What version of WordPress and s2member are you using?
Hugs,
Nathan
I have been working on a similar project. This is looks good, however, did you ever fix the problem with saving? I saw the post on S2Member where Jason suggested a method to save, I cannot seem to figure it out! Let me know if you are willing to share the solution...
Thanks
Tom
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, Thanks for a great plugin, but I have problem with saving details. I entered api key and list IDs, and I clicked save. But it doesn't work ;(
Every fields are empty after clicking Save button
Please help me,
netbloger