Advertisement
Guest User

Untitled

a guest
Mar 9th, 2012
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.35 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: MailChimp STS
  4. Description: MailChimp STS plugin sends emails, generated by WordPress, through MailChimp's Simple Transactional Service API.
  5. Author: NewClarity
  6. Author URL: http://newclarity.net/
  7. Plugin URL: http://kb.mailchimp.com/connect/wordpress-sts-plugin
  8. Version: 0.9.2
  9. Text Domain: mailchimp-sts
  10. Domain Path: /lang
  11. License Notes: This plugin is released under GPLv2 or later license. This plugin contains Raphael and gRaphael JavaScript libraries, released under GPL-compatible MIT License.
  12. */
  13.  
  14. MailChimp_STS::on_load();
  15.  
  16. class MailChimp_STS {
  17.  
  18. static $settings;
  19. static $report;
  20. static $stats;
  21.  
  22. static function on_load() {
  23.  
  24. define('MAILCHIMP_STS_API_VERSION', '1.0');
  25.  
  26. add_action('admin_init', array(__CLASS__, 'admin_init'));
  27. add_action('admin_menu', array(__CLASS__, 'admin_menu'));
  28. add_action('admin_notices', array(__CLASS__, 'settings_check'));
  29. add_filter('contextual_help', array(__CLASS__, 'contextual_help'), 10, 3);
  30. add_action('admin_print_footer_scripts', array(__CLASS__,'open_contextual_help'));
  31. load_plugin_textdomain('mailchimp-sts', false, dirname( plugin_basename( __FILE__ ) ).'/lang');
  32.  
  33. if( function_exists('wp_mail') ) {
  34. // TODO figure out, causes unexpected output on activation because pluggables are loaded already
  35. // trigger_error( __('wp_mail() was already defined by another plugin and could not be defined by MailChimp STS.', 'mailchimp-sts'), E_USER_WARNING );
  36. return;
  37. }
  38.  
  39. if( self::get_api_key() && self::get_from_email() ) {
  40.  
  41. function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
  42.  
  43. extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
  44. if( empty($headers) && empty($attachments) ) {
  45.  
  46. $sent = MailChimp_STS::wp_mail( $to, $subject, $message );
  47.  
  48. if( is_wp_error($sent) || 'sent' != $sent['status'] )
  49. return false;
  50.  
  51. return true;
  52. }
  53. else {
  54.  
  55. return MailChimp_STS::wp_mail_native( $to, $subject, $message, $headers, $attachments );
  56. }
  57. }
  58. }
  59. }
  60.  
  61. /**
  62. * @param string|array $to recipients, array or comma-separated string
  63. * @param string $subject email subject
  64. * @param string $message email body
  65. * @return array|WP_Error
  66. */
  67. static function wp_mail( $to, $subject, $message ) {
  68.  
  69. $content_type = apply_filters( 'wp_mail_content_type', 'text/plain' );
  70.  
  71. //$content_type = 'text/html';
  72.  
  73. if( 'text/html' == $content_type )
  74. $html = $message;
  75. else
  76. $text = $message;
  77.  
  78. $from_email = self::get_from_email_with_sender_name();
  79.  
  80. if( is_array($to) )
  81. $to_email = $to;
  82. else
  83. $to_email = explode(',', $to);
  84.  
  85. $message = compact('html', 'text', 'subject', 'from_name', 'from_email', 'to_email');
  86.  
  87. return self::SendEmail($message);
  88. }
  89.  
  90. /**
  91. * @return boolean
  92. */
  93. static function wp_mail_native( $to, $subject, $message, $headers = '', $attachments = array() ) {
  94.  
  95. require 'function.wp_mail.php';
  96. }
  97.  
  98. /**
  99. * @link http://apidocs.mailchimp.com/sts/1.0/sendemail.func.php
  100. *
  101. * @param array $message
  102. * @param boolean $track_opens
  103. * @param boolean $track_clicks
  104. * @param array $tags
  105. * @return array|WP_Error
  106. */
  107. static function SendEmail( $message, $track_opens = false, $track_clicks = false, $tags = array() ) {
  108.  
  109. if( !$track_opens )
  110. $track_opens = (bool)self::get_option('track_opens');
  111.  
  112. if( !$track_clicks )
  113. $track_clicks = (bool)self::get_option('track_clicks');
  114.  
  115. $tags = self::get_tags($message['subject']);
  116.  
  117. $args = compact( 'message', 'track_opens', 'track_clicks', 'tags' );
  118.  
  119. return self::request('SendEmail', $args, 'POST');
  120. }
  121.  
  122. /**
  123. * @link http://apidocs.mailchimp.com/sts/1.0/verifyemailaddress.func.php
  124. *
  125. * @param string $email
  126. * @return array|WP_Error
  127. */
  128. static function VerifyEmailAddress($email) {
  129.  
  130. return self::request('VerifyEmailAddress', array('email' => $email));
  131. }
  132.  
  133. /**
  134. * @link http://apidocs.mailchimp.com/sts/1.0/deleteverifiedemailaddress.func.php
  135. *
  136. * @param string $email
  137. * @return array|WP_Error
  138. */
  139. static function DeleteVerifiedEmailAddress($email) {
  140.  
  141. return self::request('DeleteVerifiedEmailAddress', array('email' => $email));
  142. }
  143.  
  144. /**
  145. * @link http://apidocs.mailchimp.com/sts/1.0/listverifiedemailaddresses.func.php
  146. *
  147. * @return array|WP_Error
  148. */
  149. static function ListVerifiedEmailAddresses() {
  150.  
  151. return self::request('ListVerifiedEmailAddresses');
  152. }
  153.  
  154. /**
  155. * @link http://apidocs.mailchimp.com/sts/1.0/getsendquota.func.php
  156. *
  157. * @return array|WP_Error
  158. */
  159. static function GetSendQuota() {
  160.  
  161. return self::request('GetSendQuota');
  162. }
  163.  
  164. /**
  165. * @link http://apidocs.mailchimp.com/sts/1.0/getsendstatistics.func.php
  166. *
  167. * @return array|WP_Error
  168. */
  169. static function GetSendStatistics() {
  170.  
  171. return self::request('GetSendStatistics');
  172. }
  173.  
  174. /**
  175. * @link http://apidocs.mailchimp.com/sts/1.0/
  176. *
  177. * @param string $method API method name
  178. * @param array $args query arguments
  179. * @param string $http GET or POST request type
  180. * @param string $output API response format (json,php,xml,yaml). json and xml are decoded into arrays automatically.
  181. * @return array|string|WP_Error
  182. */
  183. static function request($method, $args = array(), $http = 'GET', $output = 'json') {
  184.  
  185. if( !isset($args['apikey']) )
  186. $args['apikey'] = MailChimp_STS::get_api_key();
  187.  
  188. $datacenter = MailChimp_STS::get_datacenter($args['apikey']);
  189. $api_version = MAILCHIMP_STS_API_VERSION;
  190. $dot_output = ('json' == $output) ? '' : ".{$output}";
  191.  
  192. $url = "http://{$datacenter}.sts.mailchimp.com/{$api_version}/{$method}{$dot_output}";
  193.  
  194. switch ($http) {
  195.  
  196. case 'GET':
  197.  
  198. $url .= '?' . http_build_query($args);
  199. $response = wp_remote_get($url);
  200. break;
  201.  
  202. case 'POST':
  203.  
  204. $response = wp_remote_post($url, array('body' => $args));
  205. break;
  206.  
  207. default:
  208.  
  209. return new WP_Error('', __('Unknown request type', 'mailchimp-sts'));
  210. break;
  211. }
  212.  
  213. $response_code = wp_remote_retrieve_response_code($response);
  214. $body = wp_remote_retrieve_body($response);
  215.  
  216. switch ($output) {
  217.  
  218. case 'json':
  219.  
  220. $body = json_decode($body, true);
  221. break;
  222.  
  223. case 'php':
  224.  
  225. $body = unserialize($body);
  226. break;
  227. }
  228.  
  229. if( 200 == $response_code ) {
  230.  
  231. return $body;
  232. }
  233. else {
  234.  
  235. $message = isset( $body['message'] ) ? $body['message'] : '' ;
  236.  
  237. return new WP_Error($response_code, $message, $body );
  238. }
  239. }
  240.  
  241. /**
  242. * @return mixed
  243. */
  244. static function get_option( $name, $default = false ) {
  245.  
  246. $options = get_option('mailchimp-sts');
  247.  
  248. if( isset( $options[$name] ) )
  249. return $options[$name];
  250.  
  251. return $default;
  252. }
  253.  
  254. /**
  255. * @return boolean
  256. */
  257. static function set_option( $name, $value ) {
  258.  
  259. $options = get_option('mailchimp-sts');
  260. $options[$name] = $value;
  261.  
  262. return update_option('mailchimp-sts', $options);
  263. }
  264.  
  265. /**
  266. * @return string|boolean
  267. */
  268. static function get_api_key() {
  269.  
  270. return self::get_option('api_key');
  271. }
  272.  
  273. /**
  274. * @param string $key
  275. * @return string|boolean
  276. */
  277. static function get_datacenter($key = false) {
  278.  
  279. if( !$key )
  280. $key = self::get_api_key();
  281.  
  282. if( $key ) {
  283.  
  284. $pos = strpos($key, '-');
  285.  
  286. if( false === $pos )
  287. return false;
  288.  
  289. return substr( $key, $pos+1 );
  290. }
  291.  
  292. return false;
  293. }
  294.  
  295. static function get_from_email_with_sender_name() {
  296. return self::get_sender_name() . ' <' . self::get_option('from_email') . '>';
  297. }
  298.  
  299. /**
  300. * @return string|boolean
  301. */
  302. static function get_from_email() {
  303. return self::get_option('from_email') ;
  304. }
  305.  
  306. /**
  307. * @return string|boolean
  308. */
  309. static function get_sender_name() {
  310. return self::get_option('sender_name');
  311. }
  312.  
  313. /**
  314. * @param string $subject
  315. * @return array
  316. */
  317. static function get_tags($subject) {
  318.  
  319. $tags = array();
  320. $trace = debug_backtrace();
  321. $level = 4;
  322.  
  323. $function = $trace[$level]['function'];
  324.  
  325. if( 'include' == $function || 'require' == $function ) {
  326.  
  327. $file = basename($trace[$level]['args'][0]);
  328. $tags[] = "wp-{$file}";
  329. }
  330. else {
  331. if( isset( $trace[$level]['class'] ) )
  332. $function = $trace[$level]['class'].$trace[$level]['type'].$function;
  333. $tags[] = "wp-{$function}";
  334. }
  335.  
  336. return apply_filters('mailchimp_sts_tags', $tags);
  337. }
  338.  
  339. /**
  340. * @return boolean
  341. */
  342. static function is_plugin_page() {
  343.  
  344. return ( isset( $_GET['page'] ) && $_GET['page'] == 'mailchimp-sts' );
  345. }
  346.  
  347. /**
  348. * Sets up options page and sections.
  349. */
  350. static function admin_init() {
  351.  
  352. add_filter('plugin_action_links',array(__CLASS__,'plugin_action_links'), 10,5);
  353. add_action('admin_enqueue_scripts', array(__CLASS__,'admin_enqueue_scripts'));
  354. register_setting('mailchimp-sts', 'mailchimp-sts', array(__CLASS__,'form_validate'));
  355.  
  356. // API Settings
  357. add_settings_section('mailchimp-sts-api', __('API Settings', 'mailchimp-sts'), '__return_false', 'mailchimp-sts');
  358. add_settings_field('api-version', __('API version', 'mailchimp-sts'), array(__CLASS__, 'api_version'), 'mailchimp-sts', 'mailchimp-sts-api');
  359.  
  360. if( self::get_datacenter() )
  361. add_settings_field('api-datacenter', __('API data center', 'mailchimp-sts'), array(__CLASS__, 'api_datacenter'), 'mailchimp-sts', 'mailchimp-sts-api');
  362.  
  363. add_settings_field('api-key', __('API key', 'mailchimp-sts'), array(__CLASS__, 'api_key'), 'mailchimp-sts', 'mailchimp-sts-api');
  364.  
  365. if( self::get_api_key() ) {
  366.  
  367. // Tracking Settings
  368. add_settings_section('mailchimp-sts-tracking', __('Tracking Settings', 'mailchimp-sts'), '__return_false', 'mailchimp-sts');
  369. add_settings_field('track-opens', __('Email opens', 'mailchimp-sts'), array(__CLASS__, 'track_opens'), 'mailchimp-sts', 'mailchimp-sts-tracking');
  370. add_settings_field('track-clicks', __('Email clicks', 'mailchimp-sts'), array(__CLASS__, 'track_clicks'), 'mailchimp-sts', 'mailchimp-sts-tracking');
  371.  
  372. // Verified Addresses
  373. add_settings_section('mailchimp-sts-addresses', __('Verified Addresses', 'mailchimp-sts'), '__return_false', 'mailchimp-sts');
  374. add_settings_field('from-email', __('Email addresses', 'mailchimp-sts'), array(__CLASS__, 'from_email'), 'mailchimp-sts', 'mailchimp-sts-addresses');
  375. add_settings_field('verify-email', __('Add address', 'mailchimp-sts'), array(__CLASS__, 'verify_email'), 'mailchimp-sts', 'mailchimp-sts-addresses');
  376. add_settings_field('sender-name', __('Sender name', 'mailchimp-sts'), array(__CLASS__, 'sender_name'), 'mailchimp-sts', 'mailchimp-sts-addresses');
  377.  
  378. // Email Test
  379. register_setting('mailchimp-sts-test', 'mailchimp-sts-test', array(__CLASS__, 'test_email'));
  380. add_settings_section('mailchimp-email-test', __('Email Test', 'mailchimp-sts'), '__return_false', 'mailchimp-sts-test');
  381. add_settings_field('email-to', __('Send to', 'mailchimp-sts'), array(__CLASS__, 'email_to'), 'mailchimp-sts-test', 'mailchimp-email-test');
  382. add_settings_field('email-subject', __('Subject', 'mailchimp-sts'), array(__CLASS__, 'email_subject'), 'mailchimp-sts-test', 'mailchimp-email-test');
  383. add_settings_field('email-message', __('Message', 'mailchimp-sts'), array(__CLASS__, 'email_message'), 'mailchimp-sts-test', 'mailchimp-email-test');
  384.  
  385. }
  386. }
  387.  
  388. /**
  389. * Creates option page's entry in Settings section of menu.
  390. */
  391. static function admin_menu() {
  392.  
  393. self::$settings = add_options_page( __('MailChimp STS Settings', 'mailchimp-sts'), __('MailChimp STS', 'mailchimp-sts'), 'manage_options', 'mailchimp-sts', array(__CLASS__,'options_page'));
  394. self::$report = add_dashboard_page( __('MailChimp STS Reports', 'mailchimp-sts'),__('STS Report', 'mailchimp-sts'),'manage_options','mailchimp-sts-report', array(__CLASS__,'report_page'));
  395. add_action('admin_footer-'.self::$report, array(__CLASS__,'report_script'));
  396. }
  397.  
  398. static function admin_enqueue_scripts($hook_suffix) {
  399.  
  400. if( $hook_suffix != self::$report )
  401. return;
  402.  
  403. $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '-min';
  404.  
  405. wp_register_script('mailchimp-raphael', plugins_url("js/raphael{$suffix}.js", __FILE__), array(), null, true);
  406. wp_register_script('mailchimp-graphael', plugins_url("js/g.raphael{$suffix}.js", __FILE__), array('mailchimp-raphael'), null, true);
  407. wp_register_script('mailchimp-graphael-line', plugins_url("js/g.line{$suffix}.js", __FILE__), array('mailchimp-graphael'), null, true);
  408. wp_enqueue_script('mailchimp-graphael-line');
  409. }
  410.  
  411. /**
  412. * Generates source of contextual help panel.
  413. */
  414. static function contextual_help($contextual_help, $screen_id, $screen) {
  415.  
  416. if ($screen_id == self::$settings) {
  417.  
  418. return '<p>' . __('To use the plugin you will need:', 'mailchimp-sts') . '</p>'
  419. . '<ol>'
  420. . '<li>'. __('<strong>MailChimp account</strong> and API key.', 'mailchimp-sts') . '</li>'
  421. . '<li>' . __('<strong>Amazon SES account</strong> with production access, integrated with MailChimp.', 'mailchimp-sts') . '</li>'
  422. . '<li>' . __('<strong>Verified email address</strong> to use as sender for email.', 'mailchimp-sts') . '</li>'
  423. . '</ol>'
  424. . '<p>' . __('For detailed setup instructions please see ', 'mailchimp-sts') . '<a href="'.plugins_url('readme.html',__FILE__).'">readme.html</a>.</p>'
  425. . '<p>' . __('Also note that emails using custom headers (such as <em>cc</em> or <em>bcc</em>) and/or attachments are not supported by STS and will be sent directly.', 'mailchimp-sts') . '</p>'
  426. . '<p>' . __('Support:', 'mailchimp-sts').'</p>'
  427. . '<ul>'
  428. . '<li>' . __('Ask your <strong>How To</strong> questions on <a href="http://wordpress.stackexchange.com/questions/ask?title=MailChimp%20STS&tags=mailchimp-sts">StackExchange WordPress Answers site</a>.','mailchimp-sts') . '</li>'
  429. . '<li>' . __('Ask your <strong>Installation</strong> questions and submit <strong>Bug Reports</strong> on <a href="http://wordpress.org/tags/mailchimp-sts?forum_id=10">WordPress.org support forums</a>.','mailchimp-sts') . '</li>'
  430. . '</ul>'
  431. ;
  432. }
  433.  
  434. return $contextual_help;
  435. }
  436.  
  437. /**
  438. * Adds link to settings page in list of plugins
  439. */
  440. static function plugin_action_links($actions, $plugin_file) {
  441.  
  442. static $plugin;
  443.  
  444. if (!isset($plugin))
  445. $plugin = plugin_basename(__FILE__);
  446.  
  447. if ($plugin == $plugin_file) {
  448.  
  449. $settings = array('settings' => '<a href="options-general.php?page=mailchimp-sts">' . __('Settings', 'mailchimp-sts') . '</a>');
  450. $report = array('report' => '<a href="index.php?page=mailchimp-sts-report">' . __('Report', 'mailchimp-sts') . '</a>');
  451. $actions = array_merge($settings, $actions, $report);
  452. }
  453.  
  454. return $actions;
  455. }
  456.  
  457. /**
  458. * Generates source of options page.
  459. */
  460. static function options_page() {
  461.  
  462. if (!current_user_can('manage_options'))
  463. wp_die( __('You do not have sufficient permissions to access this page.') );
  464.  
  465. ?>
  466. <div class="wrap">
  467. <div class="icon32" style="background: url('<?php echo plugins_url('images/light_freddie.png',__FILE__); ?>');"><br /></div>
  468. <h2><?php _e('MailChimp Simple Transactional Service Settings', 'mailchimp-sts'); ?></h2>
  469.  
  470. <form method="post" action="options.php">
  471. <?php settings_fields('mailchimp-sts'); ?>
  472. <?php do_settings_sections('mailchimp-sts'); ?>
  473. <p class="submit"><input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" /></p>
  474. </form>
  475.  
  476. <form method="post" action="options.php">
  477. <?php settings_fields('mailchimp-sts-test'); ?>
  478. <?php do_settings_sections('mailchimp-sts-test'); ?>
  479.  
  480. <?php if( self::get_api_key() ) {
  481. ?><p class="submit"><input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Send Email') ?>" /></p><?php
  482. } ?>
  483. </form>
  484.  
  485. </div>
  486. <?php
  487. }
  488.  
  489. static function report_page() {
  490.  
  491. if (!current_user_can('manage_options'))
  492. wp_die( __('You do not have sufficient permissions to access this page.') );
  493.  
  494. ?>
  495. <div class="wrap">
  496. <div class="icon32" style="background: url('<?php echo plugins_url('images/light_freddie.png',__FILE__); ?>');"><br /></div>
  497. <h2><?php _e('MailChimp Simple Transactional Service Report', 'mailchimp-sts'); ?></h2>
  498.  
  499. <?php
  500.  
  501. $stats = get_transient('mailchimp-sts-stats');
  502.  
  503. if (false === $stats) {
  504.  
  505. $response = self::GetSendStatistics();
  506.  
  507. if (!is_wp_error($response)) {
  508.  
  509. $stats = $response;
  510. set_transient('mailchimp-sts-stats', $stats, 60 * 15);
  511. }
  512. else {
  513.  
  514. echo '<p>' . __('There was a problem retrieving statistics.', 'mailchimp-sts') . '</p>';
  515. echo '</div>';
  516. return;
  517. }
  518.  
  519. }
  520.  
  521. //var_dump($stats);
  522. if( empty($stats) )
  523. $stats = array();
  524.  
  525. $days = array();
  526.  
  527. foreach( $stats as $node ) {
  528.  
  529. $timestamp = $node['Timestamp'];
  530. $timestamp = strtotime($timestamp);
  531. $day = date('M j', $timestamp);
  532.  
  533. if( !isset($days[$day]) )
  534. $days[$day] = (int)$node['DeliveryAttempts'];
  535. else
  536. $days[$day] += (int)$node['DeliveryAttempts'];
  537. }
  538.  
  539. $data = array();
  540. $current = strtotime('-13 days');
  541. $end = time();
  542.  
  543. while( $current <= $end ) {
  544.  
  545. $key = date('M j', $current);
  546. $data[$key] = isset($days[$key]) ? $days[$key]: 0;
  547. $current = strtotime('+1 day', $current);
  548. }
  549.  
  550. self::$stats = $data;
  551. // var_dump($data);
  552.  
  553. ?>
  554. <h3><?php _e('Sends Over Time', 'mailchimp-sts'); ?></h3>
  555. <div id="#emails-sent"></div>
  556. </div>
  557. <?php
  558. }
  559.  
  560. static function report_script() {
  561.  
  562. $x = implode(',',range(1, count(self::$stats)));
  563. $y = implode(',',self::$stats);
  564. $labels = array_keys(self::$stats);
  565.  
  566. foreach( $labels as $key=>$label )
  567. $labels[$key] = "\"{$label}\"";
  568.  
  569. $labels = implode( ',', $labels );
  570. ?>
  571. <script type="text/javascript">
  572.  
  573. var r = Raphael('#emails-sent');
  574. var linechart = r.g.linechart(10,10,600,300,[<?php echo $x; ?>],[<?php echo $y; ?>], {"colors":["#21759B"], "symbol":"o"});
  575. r.g.axis(30,300,280,0,<?php echo max(self::$stats); ?>,5,1,"","-");
  576. r.g.axis(40,310,560,0,300,13,0,[<?php echo $labels; ?>], "-");
  577.  
  578. </script>
  579. <?php
  580. }
  581.  
  582. /**
  583. * Processes submitted settings from.
  584. */
  585. static function form_validate($input) {
  586.  
  587. self::settings_check($input['api_key']);
  588.  
  589. if( isset($input['verify_email']) && !empty( $input['verify_email'] ) ) {
  590.  
  591. $verify = self::VerifyEmailAddress( $input['verify_email'] );
  592.  
  593. if( is_wp_error($verify) )
  594. add_settings_error ('verify-email', '', __('Email verification request failed ', 'mailchimp-sts').$verify->get_error_message());
  595. else
  596. add_settings_error('verify-email', '', sprintf('Email verification request sent to <em>%s</em>', $input['verify_email']), 'updated');
  597.  
  598. unset($input['verify_email']);
  599. }
  600.  
  601. return array_map('wp_strip_all_tags', $input);
  602. }
  603.  
  604. /**
  605. * Processes submitted email test form.
  606. */
  607. static function test_email($input) {
  608.  
  609. if (isset($input['email_to']) && !empty($input['email_to'])) {
  610.  
  611. $to = $input['email_to'];
  612. $subject = isset($input['email_subject']) ? $input['email_subject'] : '';
  613. $message = isset($input['email_message']) ? $input['email_message'] : '';
  614.  
  615. $test = self::wp_mail($to, $subject, $message);
  616.  
  617. if (is_wp_error($test)) {
  618.  
  619. add_settings_error('email-to', 'email-to', __('Test email send failed. ', 'mailchimp-sts') . $test->get_error_message());
  620.  
  621. return array_map('wp_strip_all_tags', $input);
  622. }
  623. elseif ('sent' != $test['status']) {
  624.  
  625. add_settings_error('email-to', 'email-to', __('Test email send failed. ', 'mailchimp-sts') . $test['msg']);
  626.  
  627. return array_map('wp_strip_all_tags', $input);
  628. }
  629. else
  630. add_settings_error('email-to', 'email-to', __('Test email sent.', 'mailchimp-sts'), 'updated');
  631. }
  632.  
  633. return array();
  634. }
  635.  
  636. /**
  637. * Determines if key is valid, is account sandboxed and generates appropriate notices.
  638. */
  639. static function settings_check($key = false) {
  640.  
  641. if( !$key )
  642. if( !self::is_plugin_page() )
  643. return;
  644. else
  645. $key = self::get_api_key();
  646.  
  647. if( !$key )
  648. return;
  649.  
  650. if( !self::get_datacenter($key) ) {
  651.  
  652. add_settings_error('api-key', 'api-key-error', __('Cannot determine data center. Please check API key.', 'mailchimp-sts') );
  653.  
  654. return;
  655. }
  656.  
  657. $quota = self::request('GetSendQuota', array('apikey' => $key));
  658.  
  659. if( is_wp_error($quota) )
  660. add_settings_error('api-key', 'api-key-error', __('API error. ', 'mailchimp-sts').$quota->get_error_message() );
  661. else
  662. if( isset( $quota['Max24HourSend'] ) && 200 >= (int)$quota['Max24HourSend'] )
  663. add_settings_error('api-key', 'api-key-sandbox', __('Your Amazon SES account seems to be in sandbox mode. <a href="http://aws.amazon.com/ses/fullaccessrequest">Request production access</a> to be able to send emails to unverified addresses.', 'mailchimp-sts'), 'updated');
  664. }
  665.  
  666. // Following methods generate parts of settings and test forms.
  667.  
  668. static function api_version() {
  669.  
  670. echo MAILCHIMP_STS_API_VERSION;
  671. }
  672.  
  673. static function api_datacenter() {
  674.  
  675. echo self::get_datacenter();
  676. }
  677.  
  678. static function api_key() {
  679.  
  680. $key = self::get_api_key();
  681.  
  682. ?>
  683. <input id='api_key' name='mailchimp-sts[api_key]' size='45' type='text' value='<?php echo esc_attr($key); ?>' /><br />
  684. <span class="setting-description"><em><?php _e('Get by visiting <a href="http://admin.mailchimp.com/account/api-key-popup">your API dashboard</a>.'); ?></em></span>
  685. <?php
  686. }
  687.  
  688. static function track_opens() {
  689.  
  690. $track_opens = self::get_option('track_opens');
  691. ?><input id='track_opens' name='mailchimp-sts[track_opens]' type='checkbox' value='track' <?php checked($track_opens,'track') ?> /> <?php _e('track'); ?><br /><?php
  692. }
  693.  
  694. static function track_clicks() {
  695.  
  696. $track_clicks = self::get_option('track_clicks');
  697. ?><input id='track_clicks' name='mailchimp-sts[track_clicks]' type='checkbox' value='track' <?php checked($track_clicks,'track') ?> /> <?php _e('track'); ?><br /><?php
  698. }
  699.  
  700. static function from_email() {
  701.  
  702. $from_email = self::get_from_email();
  703. $addresses = self::ListVerifiedEmailAddresses();
  704.  
  705. if ( !is_wp_error($addresses) && isset($addresses['email_addresses'])) {
  706.  
  707. $addresses = $addresses['email_addresses'];
  708.  
  709. foreach( $addresses as $key => $address )
  710. if( false !== strpos($address, 'bounces.transact.mcsv.net') )
  711. unset( $addresses[$key] );
  712. }
  713.  
  714. if( is_wp_error($addresses) || empty($addresses) ) {
  715.  
  716. _e('No verified email found.');
  717.  
  718. if( $from_email )
  719. self::set_option('from_email', false);
  720.  
  721. return;
  722. }
  723.  
  724. if( !$from_email || !in_array($from_email, $addresses) ) {
  725.  
  726. $from_email = reset($addresses);
  727. self::set_option('from_email', $from_email);
  728. }
  729.  
  730. ?><span class="setting-description"><em><?php _e('Selected address will be used as sender for outgoing email:'); ?></em></span><br /><?php
  731.  
  732. foreach( $addresses as $address ) {
  733.  
  734. ?><input name="mailchimp-sts[from_email]" type="radio" value="<?php esc_attr_e($address); ?>" <?php checked($address, $from_email); ?>> <?php esc_html_e($address); ?><br /><?php
  735. }
  736. }
  737.  
  738. static function sender_name() {
  739.  
  740. $sender_name = self::get_sender_name();
  741. echo "<input id='sender_name' name='mailchimp-sts[sender_name]' size='45' type='text' value='" . esc_attr($sender_name) . "' /><br />";
  742.  
  743. }
  744.  
  745. static function verify_email() {
  746.  
  747. ?><input id='verify_email' name='mailchimp-sts[verify_email]' size='45' type='text' /><?php
  748. }
  749.  
  750. /**
  751. * @param string $field
  752. * @return string|bool
  753. */
  754. static function get_test_email($field) {
  755.  
  756. $email = get_option('mailchimp-sts-test');
  757.  
  758. if( isset( $email[$field] ) )
  759. return $email[$field];
  760.  
  761. return false;
  762. }
  763.  
  764. static function email_to() {
  765.  
  766. ?><input id='email_to' name='mailchimp-sts-test[email_to]' size='45' type='text' value="<?php esc_attr_e( self::get_test_email('email_to') ); ?>" /><?php
  767. }
  768.  
  769. static function email_subject() {
  770.  
  771. ?><input id='email_subject' name='mailchimp-sts-test[email_subject]' size='45' type='text' value="<?php esc_attr_e( self::get_test_email('email_subject') ); ?>" /><?php
  772. }
  773.  
  774. static function email_message() {
  775.  
  776. ?><textarea rows="5" cols="45" name="mailchimp-sts-test[email_message]" ><?php esc_html_e( self::get_test_email('email_message') ); ?></textarea><?php
  777. }
  778.  
  779. /**
  780. * Opens contextual help section.
  781. */
  782. static function open_contextual_help() {
  783.  
  784. if (!self::is_plugin_page() || (self::get_api_key() && self::get_from_email() ))
  785. return;
  786.  
  787. ?>
  788. <script type="text/javascript">
  789. jQuery(document).bind( 'ready', function() {
  790. jQuery('a#contextual-help-link').trigger('click');
  791. });
  792. </script>
  793. <?php
  794. }
  795. }
  796.  
  797. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement