From d2cdff27e3f2959651c353cf7c60ae12a2c20c90 Mon Sep 17 00:00:00 2001 From: Artem Bondarenko Date: Fri, 19 Feb 2021 16:17:11 +0200 Subject: [PATCH] version update (#114) Co-authored-by: Artem Bondarenko --- mailgun.php | 960 ++++++++++++++++++++++++++-------------------------- readme.txt | 7 +- 2 files changed, 482 insertions(+), 485 deletions(-) diff --git a/mailgun.php b/mailgun.php index 7a4c4a3..3da8e8a 100755 --- a/mailgun.php +++ b/mailgun.php @@ -1,486 +1,486 @@ options = get_option('mailgun'); - $this->plugin_file = __FILE__; - $this->plugin_basename = plugin_basename($this->plugin_file); - - // Either override the wp_mail function or configure PHPMailer to use the - // Mailgun SMTP servers - // When using SMTP, we also need to inject a `wp_mail` filter to make "from" settings - // work properly. Fixes issues with 1.5.7+ - if ($this->get_option('useAPI') || (defined('MAILGUN_USEAPI') && MAILGUN_USEAPI)): - if (!function_exists('wp_mail')): - if (!include dirname(__FILE__) . '/includes/wp-mail-api.php'): - self::deactivate_and_die(dirname(__FILE__) . '/includes/wp-mail-api.php'); - endif; - endif; - else: - // Using SMTP, include the SMTP filter - if (!function_exists('mg_smtp_mail_filter')): - if (!include dirname(__FILE__) . '/includes/wp-mail-smtp.php'): - self::deactivate_and_die(dirname(__FILE__) . '/includes/wp-mail-smtp.php'); - endif; - endif; - add_filter('wp_mail', 'mg_smtp_mail_filter'); - add_action('phpmailer_init', array(&$this, 'phpmailer_init')); - add_action('wp_mail_failed', 'wp_mail_failed'); - endif; - } - - /** - * Get specific option from the options table. - * - * @param string $option Name of option to be used as array key for retrieving the specific value - * @param array $options Array to iterate over for specific values - * @param bool $default False if no options are set - * - * @return mixed - * - * @since 0.1 - */ - public function get_option($option, $options = null, $default = false) - { - if (is_null($options)): - $options = &$this->options; - endif; - if (isset($options[ $option ])): - return $options[ $option ]; - else: - return $default; - endif; - } - - /** - * Hook into phpmailer to override SMTP based configurations - * to use the Mailgun SMTP server. - * - * @param object $phpmailer The PHPMailer object to modify by reference - * - * @return void - * - * @since 0.1 - */ - public function phpmailer_init(&$phpmailer) - { - $username = (defined('MAILGUN_USERNAME') && MAILGUN_USERNAME) ? MAILGUN_USERNAME : $this->get_option('username'); - $domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $this->get_option('domain'); - $username = preg_replace('/@.+$/', '', $username) . "@{$domain}"; - $secure = (defined('MAILGUN_SECURE') && MAILGUN_SECURE) ? MAILGUN_SECURE : $this->get_option('secure'); - $sectype = (defined('MAILGUN_SECTYPE') && MAILGUN_SECTYPE) ? MAILGUN_SECTYPE : $this->get_option('sectype'); - $password = (defined('MAILGUN_PASSWORD') && MAILGUN_PASSWORD) ? MAILGUN_PASSWORD : $this->get_option('password'); - $region = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $this->get_option('region'); - - $smtp_endpoint = mg_smtp_get_region($region); - $smtp_endpoint = (bool) $smtp_endpoint ? $smtp_endpoint : 'smtp.mailgun.org'; - - $phpmailer->Mailer = 'smtp'; - $phpmailer->Host = $smtp_endpoint; - - if ('ssl' === $sectype): - // For SSL-only connections, use 465 - $phpmailer->Port = 465; - else: - // Otherwise, use 587. - $phpmailer->Port = 587; - endif; - - $phpmailer->SMTPAuth = true; - $phpmailer->Username = $username; - $phpmailer->Password = $password; - - $phpmailer->SMTPSecure = (bool) $secure ? $sectype : ''; - // Without this line... wp_mail for SMTP-only will always return false. But why? :( - $phpmailer->Debugoutput = 'mg_smtp_debug_output'; - $phpmailer->SMTPDebug = 2; - - // Emit some logging for SMTP connection - mg_smtp_debug_output(sprintf("PHPMailer configured to send via %s:%s", $phpmailer->Host, $phpmailer->Port), - 'DEBUG'); - } - - /** - * Deactivate this plugin and die. - * Deactivate the plugin when files critical to it's operation cannot be loaded - * - * @param $file Files critical to plugin functionality - * - * @return void - * - * @since 0.1 - */ - public function deactivate_and_die($file) - { - load_plugin_textdomain('mailgun', false, 'mailgun/languages'); - $message = sprintf(__('Mailgun has been automatically deactivated because the file %s is missing. Please reinstall the plugin and reactivate.'), - $file); - if (!function_exists('deactivate_plugins')): - include ABSPATH . 'wp-admin/includes/plugin.php'; - endif; - deactivate_plugins(__FILE__); - wp_die($message); - } - - /** - * Make a Mailgun api call. - * - * @param string $uri The endpoint for the Mailgun API - * @param array $params Array of parameters passed to the API - * @param string $method The form request type - * - * @return array - * - * @since 0.1 - */ - public function api_call($uri, $params = array(), $method = 'POST') - { - $options = get_option('mailgun'); - $getRegion = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $options[ 'region' ]; - $apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $options[ 'apiKey' ]; - $domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $options[ 'domain' ]; - - $region = mg_api_get_region($getRegion); - $this->api_endpoint = ($region) ? $region : 'https://api.mailgun.net/v3/'; - - $time = time(); - $url = $this->api_endpoint . $uri; - $headers = array( - 'Authorization' => 'Basic ' . base64_encode("api:{$apiKey}"), - ); - - switch ($method) { - case 'GET': - $params[ 'sess' ] = ''; - $querystring = http_build_query($params); - $url = $url . '?' . $querystring; - $params = ''; - break; - case 'POST': - case 'PUT': - case 'DELETE': - $params[ 'sess' ] = ''; - $params[ 'time' ] = $time; - $params[ 'hash' ] = sha1(date('U')); - break; - } - - // make the request - $args = array( - 'method' => $method, - 'body' => $params, - 'headers' => $headers, - 'sslverify' => true, - ); - - // make the remote request - $result = wp_remote_request($url, $args); - if (!is_wp_error($result)): - return $result[ 'body' ]; - else: - return $result->get_error_message(); - endif; - } - - /** - * Get account associated lists. - * - * @return array - * - * @since 0.1 - */ - public function get_lists() - { - $results = array(); - - $lists_json = $this->api_call('lists', array(), 'GET'); - $lists_arr = json_decode($lists_json, true); - if (isset($lists_arr[ 'items' ]) && !empty($lists_arr[ 'items' ])): - $results = $lists_arr[ 'items' ]; - endif; - - return $results; - } - - /** - * Handle add list ajax post. - * - * @return string json - * - * @since 0.1 - */ - public function add_list() - { - $response = array(); - - $name = isset($_POST[ 'name' ]) ? $_POST[ 'name' ] : null; - $email = isset($_POST[ 'email' ]) ? $_POST[ 'email' ] : null; - - $list_addresses = $_POST[ 'addresses' ]; - - if (!empty($list_addresses)): - foreach ($list_addresses as $address => $val): - $response[] = $this->api_call( - "lists/{$address}/members", - array( - 'address' => $email, - 'name' => $name, - ) - ); - endforeach; - - echo json_encode(array('status' => 200, 'message' => 'Thank you!')); - else: - echo json_encode(array( - 'status' => 500, - 'message' => 'Uh oh. We weren\'t able to add you to the list' . count($list_addresses) ? 's.' : '. Please try again.' - )); - endif; - - wp_die(); - } - - /** - * Frontend List Form. - * - * @param string $list_address Mailgun address list id - * @param array $args widget arguments - * @param array $instance widget instance params - * - * @since 0.1 - */ - public function list_form($list_address, $args = array(), $instance = array()) - { - $widget_class_id = "mailgun-list-widget-{$args['widget_id']}"; - $form_class_id = "list-form-{$args['widget_id']}"; - - // List addresses from the plugin config - $list_addresses = array_map('trim', explode(',', $list_address)); - - // All list info from the API; used for list info when more than one list is available to subscribe to - $all_list_addresses = $this->get_lists(); - ?> -
-
-
- -
-

- -

-
- - -
-

- -

-
- - -

- Name: - -

- -

- Email: - -

-
- - '1'): ?> -
    - -
  • - -
  • - -
- - - - - - - -
- -
- - - - list_form($atts[ 'id' ], $args); - $output_string = ob_get_contents(); - ob_end_clean(); - - return $output_string; - else: - ?> - Mailgun list ID needed to render form! -
- Example : [mailgun id="[your list id]"] - + + list_form($atts[ 'id' ], $args); + $output_string = ob_get_contents(); + ob_end_clean(); + + return $output_string; + else: + ?> + Mailgun list ID needed to render form! +
+ Example : [mailgun id="[your list id]"] +