Last active
May 15, 2018 11:32
-
-
Save sjardim/2d834ebb0bd66d4da1ac16072f4075cd to your computer and use it in GitHub Desktop.
A ProcessWire module to handle API call to a Sendy installation.
This file contains hidden or 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 namespace ProcessWire; | |
class ProcessSendyAPI extends WireData implements Module, ConfigurableModule | |
{ | |
public static function getModuleInfo() | |
{ | |
return array( | |
'title' => __('Process Sendy API'), | |
'summary' => __('Handle API calls to a Sendy installation'), | |
'author' => 'Sérgio Jardim', | |
'version' => '001', | |
'singular' => true, | |
'autoload' => false, | |
'icon' => 'envelope' | |
); | |
} | |
/** | |
* Data as used by the get/set functions | |
* | |
*/ | |
protected $data = array(); | |
/** | |
* Default configuration for module | |
* | |
*/ | |
static public function getDefaultData() { | |
return array( | |
"sendy_api_key" => '', | |
"sendy_installation_url" => 'http://www.example.com/sendy' | |
); | |
} | |
/** | |
* Populate the default config data | |
* | |
*/ | |
public function __construct() { | |
foreach(self::getDefaultData() as $key => $value) { | |
$this->$key = $value; | |
} | |
} | |
public static function getModuleConfigInputfields(array $data) | |
{ | |
$data = array_merge(self::getDefaultData(), $data); | |
$wrapper = new InputfieldWrapper(); | |
$f = wire('modules')->get('InputfieldText'); | |
$f->attr('name', 'sendy_api_key'); | |
$f->label = __('Sendy API Key', __FILE__); | |
$f->description = __('Further instructions at https://sendy.co/api', __FILE__); | |
$f->notes = __('Get your key at http://your_sendy_installation/settings.', __FILE__); | |
$f->value = $data['sendy_api_key']; | |
$wrapper->add($f); | |
$f = wire('modules')->get('InputfieldURL'); | |
$f->attr('name', 'sendy_installation_url'); | |
$f->label = __('Sendy instalation URL', __FILE__); | |
$f->description = __('Your Sendy installation URL without a trailing slash', __FILE__); | |
$f->notes = 'http://www.example.com/sendy'; | |
$f->value = $data['sendy_installation_url']; | |
$wrapper->add($f); | |
return $wrapper; | |
} | |
/** | |
* [subscribeUserOrGuest description] | |
* @param [type] $name [description] | |
* @param [type] $email [description] | |
* @param [type] $list_id [description] | |
* @param [type] $success_url [description] | |
* @param [type] $fail_url [description] | |
* @return [type] [description] | |
*/ | |
public function subscribeInSendy($name, $email, $list_id, $success_url = null, $fail_url = null) | |
{ | |
$api_key = $this->data['sendy_api_key']; | |
$sendy_url = $this->data['sendy_installation_url']; | |
$postdata = http_build_query( | |
array( | |
'name' => $name, | |
'email' => $email, | |
'list' => $list_id, | |
'boolean' => 'true' //set this to "true" so that you'll get a plain text response | |
) | |
); | |
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata)); | |
$context = stream_context_create($opts); | |
$result = file_get_contents($sendy_url.'/subscribe', false, $context); | |
//check result and redirect | |
if($result) { | |
$this->wire('log')->save("newsletter", 'A new user subscribed to the site mailing list: '.$email); | |
if($success_url) { | |
header("Location: $success_url"); | |
} | |
} else { | |
$this->wire('log')->save("error", 'Error occurred on subscribing '.$email); | |
if($fail_url) { | |
header("Location: $fail_url"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment