PageRenderTime 66ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ongage-custom-sender.php

https://gitlab.com/itinarraylab/ongage-custom-sender
PHP | 382 lines | 242 code | 85 blank | 55 comment | 38 complexity | 76cde4c405fadb89a59af2eb94eb6013 MD5 | raw file
  1. <?php
  2. /**
  3. * Plugin Name: Ongage Custom Emails
  4. * Plugin URI:
  5. * Description: This plugin sends emails, generated by WordPress using Ongage. It also adds new emails to email list.
  6. * Version: 0.0.1
  7. * Author: Kostiantyn Aleksieiev
  8. * Author URI: http://itinarray.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * @author Kostiantyn Aleksieiev
  21. * @version 0.0.1
  22. * @package ITinArrayLab
  23. * @copyright Copyright (c) 2016, Kostiantyn Aleksieiev
  24. * @link http://itinarray.com
  25. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  26. */
  27. define('ONGAGE_PLUGIN_PATH', plugin_dir_path(__FILE__));
  28. require plugin_dir_path(__FILE__) . '/dashboard/admin_panel.php';
  29. final class ongageCustomPlugin
  30. {
  31. static $conflict;
  32. static $error;
  33. static $settings;
  34. static $user_id;
  35. /**
  36. * Initialize all the things
  37. *
  38. * @since 0.0.1
  39. */
  40. static function init()
  41. {
  42. self::$conflict = false;
  43. self::$user_id = false;
  44. if (function_exists('wp_mail')) {
  45. self::$conflict = true;
  46. add_action('admin_notices', array(__CLASS__, 'adminNotices'));
  47. return;
  48. }
  49. if (self::isConfigured()) {
  50. self::$settings = self::getSettings();
  51. function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
  52. {
  53. try {
  54. $user = get_user_by('email', $to);
  55. $first_name = '';
  56. $last_name = '';
  57. if (is_object($user)) {
  58. if (property_exists($user, 'ID')) {
  59. $user_meta = get_user_meta($user->ID);
  60. if (isset($user_meta['business_email'][0]) && ongageCustomPlugin::validateEmail($user_meta['business_email'][0])) {
  61. $to = $user_meta['business_email'][0];
  62. }
  63. //to be compatible with woocommerce plugin
  64. if (isset($user_meta['billing_email'][0]) && ongageCustomPlugin::validateEmail($user_meta['billing_email'][0])) {
  65. $to = $user_meta['billing_email'][0];
  66. }
  67. $first_name = (isset($user_meta['first_name'][0])) ? $user_meta['first_name'][0] : '';
  68. $last_name = (isset($user_meta['last_name'][0])) ? $user_meta['last_name'][0] : '';
  69. }
  70. if (isset($_SESSION['user_id'])) {
  71. // ongageCustomPlugin::subscribe($to, $first_name, $last_name);
  72. unset($_SESSION['user_id']);
  73. }
  74. }
  75. $sent = ongageCustomPlugin::mail($to, $subject, $message, $headers, $attachments);
  76. $sent = json_decode($sent['response']);
  77. if ($sent->metadata->error) {
  78. return ongageCustomPlugin::wp_mail_native($to, $subject, $message, $headers, $attachments);
  79. }
  80. return true;
  81. } catch (Exception $e) {
  82. return ongageCustomPlugin::wp_mail_native($to, $subject, $message, $headers, $attachments);
  83. }
  84. }
  85. add_action('user_register', 'ongage_custom_subscribe', 10, 1);
  86. function ongage_custom_subscribe($user_id)
  87. {
  88. $_SESSION['user_id'] = $user_id;
  89. }
  90. }
  91. }
  92. /**
  93. * Load the textdomain so we can support other languages
  94. *
  95. * @since 0.0.1
  96. */
  97. static function adminNotices()
  98. {
  99. if (self::$conflict) {
  100. 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>';
  101. }
  102. }
  103. /**
  104. * Load the textdomain so we can support other languages
  105. *
  106. * @since 0.0.1
  107. */
  108. static function adminNoticesSubscribe()
  109. {
  110. if (self::$conflict) {
  111. echo '<div class="error"><p>' . __('You are testing subscription but "Subscribe New Users" mode is disabled', 'ongage') . '</p></div>';
  112. }
  113. }
  114. /**
  115. * @return boolean
  116. */
  117. static function isConfigured()
  118. {
  119. if (
  120. self::getOption('list_id')
  121. && self::getOption('mailing_id')
  122. && self::getOption('esp_id')
  123. && self::getOption('username')
  124. && self::getOption('password')
  125. && self::getOption('account_code')
  126. && !self::$conflict
  127. )
  128. return true;
  129. return false;
  130. }
  131. static function getSettings()
  132. {
  133. $settings['language_iso'] = self::getOption('language_iso', 'en');
  134. $settings['account_code'] = self::getOption('account_code');
  135. $settings['debug_mode'] = (self::getOption('debug_mode')) ? true : false;
  136. $settings['subscribe_mode'] = (self::getOption('subscribe_mode')) ? true : false;
  137. $settings['password'] = self::getOption('password');
  138. $settings['username'] = self::getOption('username');
  139. $settings['type'] = self::getOption('type', 'email');
  140. $settings['domain'] = self::getOption('domain', 'default');
  141. $settings['esp_id'] = self::getOption('esp_id');
  142. $settings['percentage'] = self::getOption('percentage', 100);
  143. $settings['from_address'] = self::getOption('from_address', 'ongage@wordpress.com');
  144. $settings['reply_address'] = self::getOption('reply_address', 'ongage@wordpress.com');
  145. $settings['from_name'] = self::getOption('from_name', 'Ongage Wordpress Plugin');
  146. $settings['mailing_id'] = self::getOption('mailing_id');
  147. $settings['list_id'] = self::getOption('list_id');
  148. $settings['name'] = self::getOption('name', '');
  149. $settings['description'] = self::getOption('description', '');
  150. return $settings;
  151. }
  152. private static function getOption($name, $default = false)
  153. {
  154. /**
  155. * @return mixed
  156. */
  157. $options = get_option('ongage_ongage');
  158. if (isset($options[$name]))
  159. return $options[$name];
  160. return $default;
  161. }
  162. /**
  163. * @return boolean
  164. */
  165. static function wp_mail_native($to, $subject, $message, $headers = '', $attachments = array())
  166. {
  167. error_log("\nwp-ongageCustomPlugin::wp_mail_native: $to ($subject)\n");
  168. require plugin_dir_path(__FILE__) . '/legacy/function.wp_mail.php';
  169. }
  170. /**
  171. * Working Horse
  172. */
  173. static function sendTest($email)
  174. {
  175. if (self::isConfigured()) {
  176. $subject = "You are testing Ongage plugin installation";
  177. $success = false;
  178. $html = "<html><body><p>If you can see this email, it means you configured plugin correctly.</p></body></html>";
  179. $test_debug_mode = self::$settings['debug_mode'];
  180. $result = self::mail($email, $subject, $html, $test_debug_mode);
  181. $errno = "Message not sent. Check your settings";
  182. if (isset($result['response'])) {
  183. $array_response = json_decode($result['response'], true);
  184. $errno = $array_response['metadata']['error'];
  185. if ($errno === false) {
  186. $success = true;
  187. }
  188. }
  189. $log = "<b>Send Test Email Result</b>:<br/>";
  190. if ($errno) {
  191. $log .= "<b>error</b>: " . $errno . "<br/>+++++++++++++++++++++++<br/>";
  192. } else if ($success) {
  193. $log .= "<b>Success</b>: Message Sent Successfully. Check Your Inbox in a Few Minutes.<br/>";
  194. } else {
  195. echo "there is no errors in response, but no success" . PHP_EOL;
  196. var_dump($result);
  197. }
  198. return $log;
  199. }
  200. }
  201. static function subscribe($email, $first_name, $last_name, $test_mode=false)
  202. {
  203. if (self::isConfigured()) {
  204. if (!self::$settings['subscribe_mode'] && !$test_mode) {
  205. return false;
  206. }
  207. $list_id = self::$settings['list_id'];
  208. $request['email'] = $email;
  209. $request['list_id'] = $list_id;
  210. $request['fields']['email'] = $first_name;
  211. $request['fields']['fname'] = $first_name;
  212. $request['fields']['lname'] = $last_name;
  213. $link = $list_id . '/api/contacts/';
  214. include_once(plugin_dir_path(__FILE__) . '/lib/Ongage.php');
  215. $ongage = new Ongage(self::$settings);
  216. $response_raw = $ongage->postRequest($request, $link);
  217. $result = json_encode($response_raw);
  218. if ($test_mode) {
  219. if (!self::$settings['subscribe_mode']) {
  220. //@TODO Make response nicer for testEmail and Test Subscribtion
  221. echo '<h3 style="color: red">You are testing subscription but Subscribe New Users mode is disabled</h3>';
  222. }
  223. if (self::$settings['debug_mode'])
  224. var_dump($result);
  225. }
  226. return $result;
  227. }
  228. return false;
  229. }
  230. static function mail($to, $subject, $html, $test_debug=false, $headers = '', $attachments = [], $content_text = '')
  231. {
  232. $request = array(
  233. 'language_iso' => self::$settings['language_iso'],
  234. 'type' => self::$settings['type'],
  235. 'distribution' =>
  236. array(
  237. 0 =>
  238. array(
  239. 'esp_id' => (string)self::$settings['esp_id'],
  240. 'domain' => self::$settings['domain'],
  241. 'percent' => self::$settings['percentage'],
  242. ),
  243. ),
  244. 'email_message' =>
  245. array(
  246. 'addresses' =>
  247. array(
  248. self::$settings['esp_id'] =>
  249. array(
  250. 'from_address' => self::$settings['from_address'],
  251. 'reply_address' => self::$settings['reply_address'],
  252. 'from_name' => self::$settings['from_name'],
  253. 'esp_connection_id' => self::$settings['esp_id']
  254. ),
  255. ),
  256. 'subject' => $subject,
  257. 'content_html' => $html,
  258. 'content_text' => $content_text,
  259. ),
  260. 'mailing_id' => (string)self::$settings['mailing_id'],
  261. 'list_id' => (string)self::$settings['list_id'],
  262. 'name' => self::$settings['name'],
  263. 'description' => self::$settings['description'],
  264. );
  265. $request["recipients"] = [$to];
  266. $request["disable_unsubscribe"] = true;
  267. include_once(plugin_dir_path(__FILE__) . '/lib/Ongage.php');
  268. $ongage = new Ongage(self::$settings);
  269. $link = "api/notify_transactions";
  270. $response_raw = $ongage->postRequest($request, $link);
  271. $result = json_decode($response_raw);
  272. if (isset($result->metadata->error) && $result->metadata->error == FALSE) {
  273. $ret = array(
  274. 'response' => $response_raw,
  275. );
  276. } else {
  277. $ret = array(
  278. 'response' => $response_raw,
  279. );
  280. }
  281. return $ret;
  282. }
  283. static function validateEmail($email)
  284. {
  285. $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,}))$/";
  286. if (!preg_match($pattern, $email)) {
  287. return false;
  288. }
  289. return true;
  290. }
  291. }
  292. ongageCustomPlugin::init();
  293. register_deactivation_hook(__FILE__, 'ongage_custom_plugin_deactivation');
  294. function ongage_custom_plugin_deactivation()
  295. {
  296. delete_option('ongage_ongage');
  297. }