/ongage-custom-sender.php
PHP | 382 lines | 242 code | 85 blank | 55 comment | 38 complexity | 76cde4c405fadb89a59af2eb94eb6013 MD5 | raw file
- <?php
- /**
- * Plugin Name: Ongage Custom Emails
- * Plugin URI:
- * Description: This plugin sends emails, generated by WordPress using Ongage. It also adds new emails to email list.
- * Version: 0.0.1
- * Author: Kostiantyn Aleksieiev
- * Author URI: http://itinarray.com
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * @author Kostiantyn Aleksieiev
- * @version 0.0.1
- * @package ITinArrayLab
- * @copyright Copyright (c) 2016, Kostiantyn Aleksieiev
- * @link http://itinarray.com
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
- */
- define('ONGAGE_PLUGIN_PATH', plugin_dir_path(__FILE__));
- require plugin_dir_path(__FILE__) . '/dashboard/admin_panel.php';
- final class ongageCustomPlugin
- {
- static $conflict;
- static $error;
- static $settings;
- static $user_id;
- /**
- * Initialize all the things
- *
- * @since 0.0.1
- */
- static function init()
- {
- self::$conflict = false;
- self::$user_id = false;
- if (function_exists('wp_mail')) {
- self::$conflict = true;
- add_action('admin_notices', array(__CLASS__, 'adminNotices'));
- return;
- }
- if (self::isConfigured()) {
- self::$settings = self::getSettings();
- function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
- {
- try {
- $user = get_user_by('email', $to);
- $first_name = '';
- $last_name = '';
- if (is_object($user)) {
- if (property_exists($user, 'ID')) {
- $user_meta = get_user_meta($user->ID);
- if (isset($user_meta['business_email'][0]) && ongageCustomPlugin::validateEmail($user_meta['business_email'][0])) {
- $to = $user_meta['business_email'][0];
- }
- //to be compatible with woocommerce plugin
- if (isset($user_meta['billing_email'][0]) && ongageCustomPlugin::validateEmail($user_meta['billing_email'][0])) {
- $to = $user_meta['billing_email'][0];
- }
- $first_name = (isset($user_meta['first_name'][0])) ? $user_meta['first_name'][0] : '';
- $last_name = (isset($user_meta['last_name'][0])) ? $user_meta['last_name'][0] : '';
- }
- if (isset($_SESSION['user_id'])) {
- // ongageCustomPlugin::subscribe($to, $first_name, $last_name);
- unset($_SESSION['user_id']);
- }
- }
- $sent = ongageCustomPlugin::mail($to, $subject, $message, $headers, $attachments);
- $sent = json_decode($sent['response']);
- if ($sent->metadata->error) {
- return ongageCustomPlugin::wp_mail_native($to, $subject, $message, $headers, $attachments);
- }
- return true;
- } catch (Exception $e) {
- return ongageCustomPlugin::wp_mail_native($to, $subject, $message, $headers, $attachments);
- }
- }
- add_action('user_register', 'ongage_custom_subscribe', 10, 1);
- function ongage_custom_subscribe($user_id)
- {
- $_SESSION['user_id'] = $user_id;
- }
- }
- }
- /**
- * Load the textdomain so we can support other languages
- *
- * @since 0.0.1
- */
- static function adminNotices()
- {
- if (self::$conflict) {
- echo '<div class="error"><p>' . __('Ongage: wp_mail has been declared by another process or plugin, so you won\'t be able to use Ongage until the problem is solved.', 'ongage') . '</p></div>';
- }
- }
- /**
- * Load the textdomain so we can support other languages
- *
- * @since 0.0.1
- */
- static function adminNoticesSubscribe()
- {
- if (self::$conflict) {
- echo '<div class="error"><p>' . __('You are testing subscription but "Subscribe New Users" mode is disabled', 'ongage') . '</p></div>';
- }
- }
- /**
- * @return boolean
- */
- static function isConfigured()
- {
- if (
- self::getOption('list_id')
- && self::getOption('mailing_id')
- && self::getOption('esp_id')
- && self::getOption('username')
- && self::getOption('password')
- && self::getOption('account_code')
- && !self::$conflict
- )
- return true;
- return false;
- }
- static function getSettings()
- {
- $settings['language_iso'] = self::getOption('language_iso', 'en');
- $settings['account_code'] = self::getOption('account_code');
- $settings['debug_mode'] = (self::getOption('debug_mode')) ? true : false;
- $settings['subscribe_mode'] = (self::getOption('subscribe_mode')) ? true : false;
- $settings['password'] = self::getOption('password');
- $settings['username'] = self::getOption('username');
- $settings['type'] = self::getOption('type', 'email');
- $settings['domain'] = self::getOption('domain', 'default');
- $settings['esp_id'] = self::getOption('esp_id');
- $settings['percentage'] = self::getOption('percentage', 100);
- $settings['from_address'] = self::getOption('from_address', 'ongage@wordpress.com');
- $settings['reply_address'] = self::getOption('reply_address', 'ongage@wordpress.com');
- $settings['from_name'] = self::getOption('from_name', 'Ongage Wordpress Plugin');
- $settings['mailing_id'] = self::getOption('mailing_id');
- $settings['list_id'] = self::getOption('list_id');
- $settings['name'] = self::getOption('name', '');
- $settings['description'] = self::getOption('description', '');
- return $settings;
- }
- private static function getOption($name, $default = false)
- {
- /**
- * @return mixed
- */
- $options = get_option('ongage_ongage');
- if (isset($options[$name]))
- return $options[$name];
- return $default;
- }
- /**
- * @return boolean
- */
- static function wp_mail_native($to, $subject, $message, $headers = '', $attachments = array())
- {
- error_log("\nwp-ongageCustomPlugin::wp_mail_native: $to ($subject)\n");
- require plugin_dir_path(__FILE__) . '/legacy/function.wp_mail.php';
- }
- /**
- * Working Horse
- */
- static function sendTest($email)
- {
- if (self::isConfigured()) {
- $subject = "You are testing Ongage plugin installation";
- $success = false;
- $html = "<html><body><p>If you can see this email, it means you configured plugin correctly.</p></body></html>";
- $test_debug_mode = self::$settings['debug_mode'];
- $result = self::mail($email, $subject, $html, $test_debug_mode);
- $errno = "Message not sent. Check your settings";
- if (isset($result['response'])) {
- $array_response = json_decode($result['response'], true);
- $errno = $array_response['metadata']['error'];
- if ($errno === false) {
- $success = true;
- }
- }
- $log = "<b>Send Test Email Result</b>:<br/>";
- if ($errno) {
- $log .= "<b>error</b>: " . $errno . "<br/>+++++++++++++++++++++++<br/>";
- } else if ($success) {
- $log .= "<b>Success</b>: Message Sent Successfully. Check Your Inbox in a Few Minutes.<br/>";
- } else {
- echo "there is no errors in response, but no success" . PHP_EOL;
- var_dump($result);
- }
- return $log;
- }
- }
- static function subscribe($email, $first_name, $last_name, $test_mode=false)
- {
- if (self::isConfigured()) {
- if (!self::$settings['subscribe_mode'] && !$test_mode) {
- return false;
- }
- $list_id = self::$settings['list_id'];
- $request['email'] = $email;
- $request['list_id'] = $list_id;
- $request['fields']['email'] = $first_name;
- $request['fields']['fname'] = $first_name;
- $request['fields']['lname'] = $last_name;
- $link = $list_id . '/api/contacts/';
- include_once(plugin_dir_path(__FILE__) . '/lib/Ongage.php');
- $ongage = new Ongage(self::$settings);
- $response_raw = $ongage->postRequest($request, $link);
- $result = json_encode($response_raw);
- if ($test_mode) {
- if (!self::$settings['subscribe_mode']) {
- //@TODO Make response nicer for testEmail and Test Subscribtion
- echo '<h3 style="color: red">You are testing subscription but Subscribe New Users mode is disabled</h3>';
- }
- if (self::$settings['debug_mode'])
- var_dump($result);
- }
- return $result;
- }
- return false;
- }
- static function mail($to, $subject, $html, $test_debug=false, $headers = '', $attachments = [], $content_text = '')
- {
- $request = array(
- 'language_iso' => self::$settings['language_iso'],
- 'type' => self::$settings['type'],
- 'distribution' =>
- array(
- 0 =>
- array(
- 'esp_id' => (string)self::$settings['esp_id'],
- 'domain' => self::$settings['domain'],
- 'percent' => self::$settings['percentage'],
- ),
- ),
- 'email_message' =>
- array(
- 'addresses' =>
- array(
- self::$settings['esp_id'] =>
- array(
- 'from_address' => self::$settings['from_address'],
- 'reply_address' => self::$settings['reply_address'],
- 'from_name' => self::$settings['from_name'],
- 'esp_connection_id' => self::$settings['esp_id']
- ),
- ),
- 'subject' => $subject,
- 'content_html' => $html,
- 'content_text' => $content_text,
- ),
- 'mailing_id' => (string)self::$settings['mailing_id'],
- 'list_id' => (string)self::$settings['list_id'],
- 'name' => self::$settings['name'],
- 'description' => self::$settings['description'],
- );
- $request["recipients"] = [$to];
- $request["disable_unsubscribe"] = true;
- include_once(plugin_dir_path(__FILE__) . '/lib/Ongage.php');
- $ongage = new Ongage(self::$settings);
- $link = "api/notify_transactions";
- $response_raw = $ongage->postRequest($request, $link);
- $result = json_decode($response_raw);
- if (isset($result->metadata->error) && $result->metadata->error == FALSE) {
- $ret = array(
- 'response' => $response_raw,
- );
- } else {
- $ret = array(
- 'response' => $response_raw,
- );
- }
- return $ret;
- }
- static function validateEmail($email)
- {
- $pattern = "/^(([^<>()\[\]\\.,;:\s@\"]+(\.[^<>()\[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/";
- if (!preg_match($pattern, $email)) {
- return false;
- }
- return true;
- }
- }
- ongageCustomPlugin::init();
- register_deactivation_hook(__FILE__, 'ongage_custom_plugin_deactivation');
- function ongage_custom_plugin_deactivation()
- {
- delete_option('ongage_ongage');
- }