subscription.php 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  1. <?php
  2. if (!defined('ABSPATH'))
  3. exit;
  4. require_once NEWSLETTER_INCLUDES_DIR . '/module.php';
  5. class NewsletterSubscription extends NewsletterModule {
  6. const MESSAGE_CONFIRMED = 'confirmed';
  7. const OPTIN_DOUBLE = 0;
  8. const OPTIN_SINGLE = 1;
  9. static $instance;
  10. /**
  11. * @var array
  12. */
  13. var $options_profile;
  14. /**
  15. * @var array
  16. */
  17. var $options_lists;
  18. /**
  19. * @return NewsletterSubscription
  20. */
  21. static function instance() {
  22. if (self::$instance == null) {
  23. self::$instance = new NewsletterSubscription();
  24. }
  25. return self::$instance;
  26. }
  27. function __construct() {
  28. parent::__construct('subscription', '2.2.7', null, array('lists', 'template', 'profile', 'antibot'));
  29. $this->options_profile = $this->get_options('profile');
  30. $this->options_lists = $this->get_options('lists');
  31. // Must be called after the Newsletter::hook_init, since some constants are defined
  32. // there.
  33. add_action('init', array($this, 'hook_init'), 90);
  34. }
  35. function hook_init() {
  36. add_action('wp_loaded', array($this, 'hook_wp_loaded'));
  37. if (is_admin()) {
  38. add_action('admin_init', array($this, 'hook_admin_init'));
  39. } else {
  40. add_action('wp_enqueue_scripts', array($this, 'hook_wp_enqueue_scripts'));
  41. add_shortcode('newsletter', array($this, 'shortcode_newsletter'));
  42. add_shortcode('newsletter_form', array($this, 'shortcode_newsletter_form'));
  43. add_shortcode('newsletter_field', array($this, 'shortcode_newsletter_field'));
  44. }
  45. }
  46. function hook_admin_init() {
  47. if (isset($_GET['page']) && $_GET['page'] === 'newsletter_subscription_forms') {
  48. header('X-XSS-Protection: 0');
  49. }
  50. if (function_exists('register_block_type')) {
  51. // Add custom blocks to Gutenberg
  52. wp_register_script('tnp-blocks', NEWSLETTER_URL . '/includes/tnp-blocks.js', array('wp-blocks', 'wp-element', 'wp-editor'), NEWSLETTER_VERSION);
  53. register_block_type('tnp/minimal', array('editor_script' => 'tnp-blocks'));
  54. }
  55. }
  56. function hook_wp_enqueue_scripts() {
  57. wp_enqueue_script('newsletter-subscription', plugins_url('newsletter') . '/subscription/validate.js', array(), NEWSLETTER_VERSION, true);
  58. $options = $this->get_options('profile', $this->get_current_language());
  59. $data = array();
  60. $data['messages'] = array();
  61. if (isset($options['email_error'])) {
  62. $data['messages']['email_error'] = $options['email_error'];
  63. }
  64. if (isset($options['name_error'])) {
  65. $data['messages']['name_error'] = $options['name_error'];
  66. }
  67. if (isset($options['surname_error'])) {
  68. $data['messages']['surname_error'] = $options['surname_error'];
  69. }
  70. if (isset($options['profile_error'])) {
  71. $data['messages']['profile_error'] = $options['profile_error'];
  72. }
  73. if (isset($options['privacy_error'])) {
  74. $data['messages']['privacy_error'] = $options['privacy_error'];
  75. }
  76. $data['profile_max'] = NEWSLETTER_PROFILE_MAX;
  77. wp_localize_script('newsletter-subscription', 'newsletter', $data);
  78. }
  79. function ip_match($ip, $range) {
  80. if (strpos($range, '/')) {
  81. list ($subnet, $bits) = explode('/', $range);
  82. $ip = ip2long($ip);
  83. $subnet = ip2long($subnet);
  84. $mask = -1 << (32 - $bits);
  85. $subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned
  86. return ($ip & $mask) == $subnet;
  87. } else {
  88. return strpos($range, $ip) === 0;
  89. }
  90. }
  91. function is_address_blacklisted($email) {
  92. // TODO: Optimize!
  93. $options = $this->get_options('antibot');
  94. if (empty($options['address_blacklist'])) {
  95. return false;
  96. }
  97. $this->logger->debug('Address blacklist check');
  98. $rev_email = strrev($email);
  99. foreach ($options['address_blacklist'] as $item) {
  100. if (strpos($rev_email, strrev($item)) === 0) {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. function is_ip_blacklisted($ip) {
  107. // TODO: Optimize!
  108. $options = $this->get_options('antibot');
  109. if (empty($options['ip_blacklist'])) {
  110. return false;
  111. }
  112. $this->logger->debug('IP blacklist check');
  113. foreach ($options['ip_blacklist'] as $item) {
  114. if ($this->ip_match($ip, $item)) {
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. function is_missing_domain_mx($email) {
  121. // Actually not fully implemented
  122. return false;
  123. if (empty($this->options['domain_check'])) {
  124. return false;
  125. }
  126. $this->logger->debug('Domain MX check');
  127. list($local, $domain) = explode('@', $email);
  128. $hosts = array();
  129. if (!getmxrr($domain, $hosts)) {
  130. return true;
  131. }
  132. return false;
  133. }
  134. function is_flood($email, $ip) {
  135. global $wpdb;
  136. // TODO: Optimize!
  137. $options = $this->get_options('antibot');
  138. if (empty($options['antiflood'])) {
  139. return false;
  140. }
  141. $this->logger->debug('Antiflood check');
  142. $updated = $wpdb->get_var($wpdb->prepare("select updated from " . NEWSLETTER_USERS_TABLE . " where ip=%s or email=%s order by updated desc limit 1", $ip, $email));
  143. if ($updated && time() - $updated < $options['antiflood']) {
  144. return true;
  145. }
  146. return false;
  147. }
  148. function is_spam_text($text) {
  149. if (stripos($text, 'http://') !== false || stripos($text, 'https://') !== false) {
  150. return true;
  151. }
  152. if (stripos($text, 'www.') !== false) {
  153. return true;
  154. }
  155. return false;
  156. }
  157. function is_spam_by_akismet($email, $name, $ip, $agent, $referrer) {
  158. // TODO: Optimize!
  159. $options = $this->get_options('antibot');
  160. if (empty($options['akismet'])) {
  161. return false;
  162. }
  163. if (!class_exists('Akismet')) {
  164. return false;
  165. }
  166. $this->logger->debug('Akismet check');
  167. $request = 'blog=' . urlencode(home_url()) . '&referrer=' . urlencode($referrer) .
  168. '&user_agent=' . urlencode($agent) .
  169. '&comment_type=signup' .
  170. '&comment_author_email=' . urlencode($email) .
  171. '&user_ip=' . urlencode($ip);
  172. if (!empty($name)) {
  173. $request .= '&comment_author=' . urlencode($name);
  174. }
  175. $response = Akismet::http_post($request, 'comment-check');
  176. if ($response && $response[1] == 'true') {
  177. return true;
  178. }
  179. return false;
  180. }
  181. /**
  182. *
  183. * @global wpdb $wpdb
  184. * @return mixed
  185. */
  186. function hook_wp_loaded() {
  187. global $wpdb;
  188. $newsletter = Newsletter::instance();
  189. switch ($newsletter->action) {
  190. case 'profile-change':
  191. if ($this->antibot_form_check()) {
  192. $user = $this->get_user_from_request();
  193. if (!$user || $user->status != 'C') {
  194. die('Subscriber not found or not active.');
  195. }
  196. $email = $this->get_email_from_request();
  197. if (!$email) {
  198. die('Newsletter not found');
  199. }
  200. if (isset($_REQUEST['list'])) {
  201. $list_id = (int) $_REQUEST['list'];
  202. // Check if the list is public
  203. $list = $this->get_list($list_id);
  204. if (!$list || $list->status == 0) {
  205. die('Private list.');
  206. }
  207. $url = $_REQUEST['redirect'];
  208. $this->set_user_list($user, $list_id, $_REQUEST['value']);
  209. $user = $this->get_user($user->id);
  210. $this->add_user_log($user, 'cta');
  211. NewsletterStatistics::instance()->add_click($url, $user->id, $email->id);
  212. wp_redirect($url);
  213. die();
  214. }
  215. } else {
  216. $this->request_to_antibot_form('Continue');
  217. }
  218. die();
  219. case 'm':
  220. case 'message':
  221. include dirname(__FILE__) . '/page.php';
  222. die();
  223. // normal subscription
  224. case 's':
  225. case 'subscribe':
  226. $ip = $this->get_remote_ip();
  227. $email = $this->normalize_email($_REQUEST['ne']);
  228. $first_name = '';
  229. if (isset($_REQUEST['nn']))
  230. $first_name = $this->normalize_name($_REQUEST['nn']);
  231. $last_name = '';
  232. if (isset($_REQUEST['ns']))
  233. $last_name = $this->normalize_name($_REQUEST['ns']);
  234. $full_name = trim($first_name . ' ' . $last_name);
  235. $antibot_logger = new NewsletterLogger('antibot');
  236. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  237. $antibot_logger->fatal($email . ' - ' . $ip . ' - HTTP method invalid');
  238. die('Invalid');
  239. }
  240. $options_antibot = $this->get_options('antibot');
  241. $captcha = !empty($options_antibot['captcha']);
  242. if (!empty($options_antibot['disabled']) || $this->antibot_form_check($captcha)) {
  243. if ($this->is_spam_text($full_name)) {
  244. $antibot_logger->fatal($email . ' - ' . $ip . ' - Name with http: ' . $full_name);
  245. header("HTTP/1.0 404 Not Found");
  246. die();
  247. }
  248. // Cannot check for administrator here, too early.
  249. if (true) {
  250. $this->logger->debug('Subscription of: ' . $email);
  251. // 404 is returned to attempt to make the bot believe the url has been changed
  252. if ($this->is_missing_domain_mx($email)) {
  253. $antibot_logger->fatal($email . ' - ' . $ip . ' - MX check failed');
  254. header("HTTP/1.0 404 Not Found");
  255. die();
  256. }
  257. if ($this->is_ip_blacklisted($ip)) {
  258. $antibot_logger->fatal($email . ' - ' . $ip . ' - IP blacklisted');
  259. header("HTTP/1.0 404 Not Found");
  260. die();
  261. }
  262. if ($this->is_address_blacklisted($email)) {
  263. $antibot_logger->fatal($email . ' - ' . $ip . ' - Address blacklisted');
  264. header("HTTP/1.0 404 Not Found");
  265. die();
  266. }
  267. // Akismet check
  268. if ($this->is_spam_by_akismet($email, $full_name, $ip, $_SERVER['HTTP_USER_AGENT'], $_SERVER['HTTP_REFERER'])) {
  269. $antibot_logger->fatal($email . ' - ' . $ip . ' - Akismet blocked');
  270. header("HTTP/1.0 404 Not Found");
  271. die();
  272. }
  273. // Flood check
  274. if ($this->is_flood($email, $ip)) {
  275. $antibot_logger->fatal($email . ' - ' . $ip . ' - Antiflood triggered');
  276. header("HTTP/1.0 404 Not Found");
  277. die('Too quick');
  278. }
  279. $user = $this->subscribe();
  280. if ($user->status == 'E')
  281. $this->show_message('error', $user);
  282. if ($user->status == 'C')
  283. $this->show_message('confirmed', $user);
  284. if ($user->status == 'A')
  285. $this->show_message('already_confirmed', $user);
  286. if ($user->status == 'S')
  287. $this->show_message('confirmation', $user);
  288. }
  289. } else {
  290. // Temporary store data
  291. //$data_key = wp_generate_password(16, false, false);
  292. //set_transient('newsletter_' . $data_key, $_REQUEST, 60);
  293. //$this->antibot_redirect($data_key);
  294. $this->request_to_antibot_form('Subscribe', $captcha);
  295. }
  296. die();
  297. // AJAX subscription
  298. case 'ajaxsub':
  299. $user = $this->subscribe();
  300. if ($user->status == 'E')
  301. $key = 'error';
  302. if ($user->status == 'C')
  303. $key = 'confirmed';
  304. if ($user->status == 'A')
  305. $key = 'already_confirmed';
  306. if ($user->status == 'S')
  307. $key = 'confirmation';
  308. $module = NewsletterSubscription::instance();
  309. $message = $newsletter->replace($module->options[$key . '_text'], $user);
  310. if (isset($module->options[$key . '_tracking'])) {
  311. $message .= $module->options[$key . '_tracking'];
  312. }
  313. echo $message;
  314. die();
  315. case 'c':
  316. case 'confirm':
  317. if ($this->antibot_form_check()) {
  318. $user = $this->confirm();
  319. if ($user->status == 'E') {
  320. $this->show_message('error', $user->id);
  321. } else {
  322. setcookie('newsletter', $user->id . '-' . $user->token, time() + 60 * 60 * 24 * 365, '/');
  323. $this->show_message('confirmed', $user);
  324. }
  325. } else {
  326. $this->request_to_antibot_form('Confirm');
  327. }
  328. die();
  329. break;
  330. default:
  331. return;
  332. }
  333. }
  334. function upgrade() {
  335. global $wpdb, $charset_collate;
  336. // Possible migration
  337. $options_antibot = $this->get_options('antibot');
  338. if (empty($options_antibot)) {
  339. $options = $this->get_options();
  340. foreach (array('address_blacklist', 'ip_blacklist', 'akismet', 'captcha', 'antiflood') as $key) {
  341. if (isset($options[$key])) {
  342. $options_antibot[$key] = $options[$key];
  343. }
  344. }
  345. if (isset($options['antibot_disable'])) {
  346. $options_antibot['disabled'] = $options['antibot_disable'];
  347. } else {
  348. $options_antibot['disabled'] = 0;
  349. }
  350. $this->save_options($options_antibot, 'antibot');
  351. }
  352. parent::upgrade();
  353. $newsletter = Newsletter::instance();
  354. $lists_options = $this->get_options('lists');
  355. $profile_options = $this->get_options('profile');
  356. if (empty($lists_options)) {
  357. foreach ($profile_options as $key => $value) {
  358. if (strpos($key, 'list_') === 0) {
  359. $lists_options[$key] = $value;
  360. }
  361. }
  362. }
  363. for ($i = 1; $i <= NEWSLETTER_LIST_MAX; $i++) {
  364. // Options migration to the new set
  365. if (!empty($profile_options['list_' . $i]) && empty($lists_options['list_' . $i])) {
  366. $lists_options['list_' . $i] = $profile_options['list_' . $i];
  367. $lists_options['list_' . $i . '_checked'] = $profile_options['list_' . $i . '_checked'];
  368. $lists_options['list_' . $i . '_forced'] = $profile_options['list_' . $i . '_forced'];
  369. }
  370. if (!isset($profile_options['list_' . $i . '_forced'])) {
  371. $profile_options['list_' . $i . '_forced'] = empty($this->options['preferences_' . $i]) ? 0 : 1;
  372. $lists_options['list_' . $i . '_forced'] = empty($this->options['preferences_' . $i]) ? 0 : 1;
  373. }
  374. }
  375. $this->save_options($profile_options, 'profile');
  376. $this->save_options($lists_options, 'lists');
  377. $default_options = $this->get_default_options();
  378. if (empty($this->options['error_text'])) {
  379. $this->options['error_text'] = $default_options['error_text'];
  380. $this->save_options($this->options);
  381. }
  382. if ($this->old_version < '2.0.0') {
  383. if (!isset($this->options['url']) && !empty($newsletter->options['url'])) {
  384. $this->options['url'] = $newsletter->options['url'];
  385. $this->save_options($this->options);
  386. }
  387. $options_template = $this->get_options('template');
  388. if (empty($options_template) && isset($this->options['template'])) {
  389. $options_template['enabled'] = isset($this->options['template_enabled']) ? 1 : 0;
  390. $options_template['template'] = $this->options['template'];
  391. add_option('newsletter_subscription_template', $options_template, null, 'no');
  392. }
  393. if (isset($this->options['template'])) {
  394. unset($this->options['template']);
  395. unset($this->options['template_enabled']);
  396. $this->save_options($this->options);
  397. }
  398. }
  399. $this->init_options('template', false);
  400. global $wpdb, $charset_collate;
  401. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  402. $sql = "CREATE TABLE `" . $wpdb->prefix . "newsletter_user_logs` (
  403. `id` int(11) NOT NULL AUTO_INCREMENT,
  404. `user_id` int(11) NOT NULL DEFAULT 0,
  405. `ip` varchar(50) NOT NULL DEFAULT '',
  406. `source` varchar(50) NOT NULL DEFAULT '',
  407. `data` longtext,
  408. `created` int(11) NOT NULL DEFAULT 0,
  409. PRIMARY KEY (`id`)
  410. ) $charset_collate;";
  411. dbDelta($sql);
  412. return true;
  413. }
  414. function first_install() {
  415. }
  416. function admin_menu() {
  417. $this->add_menu_page('options', 'List building');
  418. $this->add_menu_page('antibot', 'Security');
  419. $this->add_admin_page('profile', 'Subscription Form');
  420. $this->add_admin_page('forms', 'Forms');
  421. $this->add_admin_page('lists', 'Lists');
  422. $this->add_admin_page('template', 'Template');
  423. }
  424. /**
  425. * This method has been redefined for compatibility with the old options naming. It would
  426. * be better to change them instead. The subscription options should be named
  427. * "newsletter_subscription" while the form field options, actually named
  428. * "newsletter_profile", should be renamed "newsletter_subscription_profile" (since
  429. * they are retrived with get_options('profile')) or "newsletter_subscription_fields" or
  430. * "newsletter_subscription_form".
  431. *
  432. * @param array $options
  433. * @param string $sub
  434. */
  435. function save_options($options, $sub = '', $autoload = null, $language = '') {
  436. if (empty($sub) && empty($language)) {
  437. // For compatibility the options are wrongly named
  438. return update_option('newsletter', $options, $autoload);
  439. }
  440. if (empty($sub) && !empty($language)) {
  441. return update_option('newsletter_' . $language, $options, $autoload);
  442. }
  443. if ($sub == 'profile') {
  444. if (empty($language)) {
  445. $this->options_profile = $options;
  446. return update_option('newsletter_profile', $options, $autoload);
  447. } else {
  448. return update_option('newsletter_profile_' . $language, $options, $autoload);
  449. }
  450. // For compatibility the options are wrongly named
  451. }
  452. if ($sub == 'forms') {
  453. // For compatibility the options are wrongly named
  454. return update_option('newsletter_forms', $options, $autoload);
  455. }
  456. if ($sub == 'lists') {
  457. $this->options_lists = $options;
  458. }
  459. return parent::save_options($options, $sub, $autoload, $language);
  460. }
  461. function get_options($sub = '', $language = '') {
  462. if ($sub == '') {
  463. // For compatibility the options are wrongly named
  464. if ($language) {
  465. $options = get_option('newsletter_' . $language, array());
  466. $options = array_merge(get_option('newsletter', array()), $options);
  467. } else {
  468. $options = get_option('newsletter', array());
  469. }
  470. if (!is_array($options)) {
  471. $options = array();
  472. }
  473. return $options;
  474. }
  475. if ($sub == 'profile') {
  476. if ($language) {
  477. $options = get_option('newsletter_profile_' . $language, array());
  478. $options = array_merge(get_option('newsletter_profile', array()), $options);
  479. } else {
  480. $options = get_option('newsletter_profile', array());
  481. }
  482. // For compatibility the options are wrongly named
  483. return $options;
  484. }
  485. if ($sub == 'forms') {
  486. // For compatibility the options are wrongly named
  487. return get_option('newsletter_forms', array());
  488. }
  489. return parent::get_options($sub, $language);
  490. }
  491. function set_updated($user, $time = 0, $ip = '') {
  492. global $wpdb;
  493. if (!$time) {
  494. $time = time();
  495. }
  496. if (!$ip) {
  497. $ip = $this->get_remote_ip();
  498. }
  499. $ip = $this->process_ip($ip);
  500. if (is_object($user)) {
  501. $id = $user->id;
  502. } else if (is_array($user)) {
  503. $id = $user['id'];
  504. }
  505. $id = (int) $id;
  506. $wpdb->update(NEWSLETTER_USERS_TABLE, array('updated' => $time, 'ip' => $ip, 'geo' => 0), array('id' => $id));
  507. }
  508. /**
  509. * Return the subscribed user.
  510. *
  511. * @param bool $registration If invoked from the registration process
  512. * @global Newsletter $newsletter
  513. */
  514. function subscribe($status = null, $emails = true) {
  515. $opt_in = (int) $this->options['noconfirmation']; // 0 - double, 1 - single
  516. if (!empty($this->options['optin_override']) && isset($_REQUEST['optin'])) {
  517. switch ($_REQUEST['optin']) {
  518. case 'single': $opt_in = self::OPTIN_SINGLE;
  519. break;
  520. case 'double': $opt_in = self::OPTIN_DOUBLE;
  521. break;
  522. }
  523. }
  524. if ($status != null) {
  525. // If a status is forced and it is requested to be "confirmed" is like a single opt in
  526. // $status here can only be confirmed or not confirmed
  527. // TODO: Add a check on status values
  528. if ($status == Newsletter::STATUS_CONFIRMED) {
  529. $opt_in = self::OPTIN_SINGLE;
  530. } else {
  531. $opt_in = self::OPTIN_DOUBLE;
  532. }
  533. }
  534. $email = $this->normalize_email(stripslashes($_REQUEST['ne']));
  535. // Shound never reach this point without a valid email address
  536. if ($email == null) {
  537. die('Wrong email');
  538. }
  539. $user = $this->get_user($email);
  540. if ($user != null) {
  541. // Email already registered in our database
  542. $this->logger->info('Subscription of an address with status ' . $user->status);
  543. // Bounced
  544. // TODO: Manage other cases when added
  545. if ($user->status == 'B') {
  546. // Non persistent status to decide which message to show (error)
  547. $user->status = 'E';
  548. return $user;
  549. }
  550. // Is there any relevant data change? If so we can proceed otherwise if repeated subscriptions are disabled
  551. // show an already subscribed message
  552. if (empty($this->options['multiple'])) {
  553. $user->status = 'E';
  554. return $user;
  555. }
  556. if ($this->options['multiple'] == 2) {
  557. $lists_changed = false;
  558. if (isset($_REQUEST['nl']) && is_array($_REQUEST['nl'])) {
  559. foreach ($_REQUEST['nl'] as $list_id) {
  560. $list_id = (int) $list_id;
  561. if ($list_id <= 0 || $list_id > NEWSLETTER_LIST_MAX)
  562. continue;
  563. $field = 'list_' . $list_id;
  564. if ($user->$field == 0) {
  565. $lists_changed = true;
  566. break;
  567. }
  568. }
  569. }
  570. if (!$lists_changed) {
  571. $user->status = 'E';
  572. return $user;
  573. }
  574. }
  575. // If the subscriber is confirmed, we cannot change his data in double opt in mode, we need to
  576. // temporary store and wait for activation
  577. if ($user->status == Newsletter::STATUS_CONFIRMED && $opt_in == self::OPTIN_DOUBLE) {
  578. set_transient($this->get_user_key($user), $_REQUEST, 3600 * 48);
  579. // This status is *not* stored it indicate a temporary status to show the correct messages
  580. $user->status = 'S';
  581. $this->send_message('confirmation', $user);
  582. return $user;
  583. }
  584. }
  585. // Here we have a new subscription or we can process the subscription even with a pre-existant user for example
  586. // because it is not confirmed
  587. if ($user != null) {
  588. $this->logger->info("Email address subscribed but not confirmed");
  589. $user = array('id' => $user->id);
  590. } else {
  591. $this->logger->info("New email address");
  592. $user = array('email' => $email);
  593. }
  594. $user = $this->update_user_from_request($user);
  595. $user['token'] = $this->get_token();
  596. $ip = $this->get_remote_ip();
  597. $ip = $this->process_ip($ip);
  598. $user['ip'] = $ip;
  599. $user['geo'] = 0;
  600. $user['status'] = $opt_in == self::OPTIN_SINGLE ? Newsletter::STATUS_CONFIRMED : Newsletter::STATUS_NOT_CONFIRMED;
  601. $user['updated'] = time();
  602. $user = apply_filters('newsletter_user_subscribe', $user);
  603. $user = $this->save_user($user);
  604. $this->add_user_log($user, 'subscribe');
  605. // Notification to admin (only for new confirmed subscriptions)
  606. if ($user->status == Newsletter::STATUS_CONFIRMED) {
  607. do_action('newsletter_user_confirmed', $user);
  608. $this->notify_admin($user, 'Newsletter subscription');
  609. setcookie('newsletter', $user->id . '-' . $user->token, time() + 60 * 60 * 24 * 365, '/');
  610. }
  611. if ($emails) {
  612. $this->send_message(($user->status == Newsletter::STATUS_CONFIRMED) ? 'confirmed' : 'confirmation', $user);
  613. }
  614. return $user;
  615. }
  616. function add_microdata($message) {
  617. return $message . '<span itemscope itemtype="http://schema.org/EmailMessage"><span itemprop="description" content="Email address confirmation"></span><span itemprop="action" itemscope itemtype="http://schema.org/ConfirmAction"><meta itemprop="name" content="Confirm Subscription"><span itemprop="handler" itemscope itemtype="http://schema.org/HttpActionHandler"><meta itemprop="url" content="{subscription_confirm_url}"><link itemprop="method" href="http://schema.org/HttpRequestMethod/POST"></span></span></span>';
  618. }
  619. /**
  620. * Processes the request and fill in the *array* representing a subscriber with submitted values
  621. * (filtering when necessary).
  622. *
  623. * @param array $user An array partially filled with subscriber data
  624. * @return array The filled array representing a subscriber
  625. */
  626. function update_user_from_request($user) {
  627. if (isset($_REQUEST['nn'])) {
  628. $user['name'] = $this->normalize_name(stripslashes($_REQUEST['nn']));
  629. }
  630. // TODO: required checking
  631. if (isset($_REQUEST['ns'])) {
  632. $user['surname'] = $this->normalize_name(stripslashes($_REQUEST['ns']));
  633. }
  634. // TODO: required checking
  635. if (!empty($_REQUEST['nx'])) {
  636. $user['sex'] = $this->normalize_sex($_REQUEST['nx'][0]);
  637. }
  638. // TODO: valid values check
  639. if (isset($_REQUEST['nr'])) {
  640. $user['referrer'] = strip_tags(trim($_REQUEST['nr']));
  641. }
  642. $language = '';
  643. if (!empty($_REQUEST['nlang'])) {
  644. $language = strtolower(strip_tags($_REQUEST['nlang']));
  645. // TODO: Check if it's an allowed language code
  646. $user['language'] = $language;
  647. }
  648. // From the antibot form
  649. if (isset($_REQUEST['nhr'])) {
  650. $user['http_referer'] = strip_tags(trim($_REQUEST['nhr']));
  651. } else if (isset($_SERVER['HTTP_REFERER'])) {
  652. $user['http_referer'] = strip_tags(trim($_SERVER['HTTP_REFERER']));
  653. }
  654. if (strlen($user['http_referer']) > 200) {
  655. $user['http_referer'] = mb_substr($user['http_referer'], 0, 200);
  656. }
  657. // New profiles
  658. for ($i = 1; $i <= NEWSLETTER_PROFILE_MAX; $i++) {
  659. // If the profile cannot be set by subscriber, skip it.
  660. if ($this->options_profile['profile_' . $i . '_status'] == 0) {
  661. continue;
  662. }
  663. if (isset($_REQUEST['np' . $i])) {
  664. $user['profile_' . $i] = trim(stripslashes($_REQUEST['np' . $i]));
  665. }
  666. }
  667. // Preferences (field names are nl[] and values the list number so special forms with radio button can work)
  668. if (isset($_REQUEST['nl']) && is_array($_REQUEST['nl'])) {
  669. $lists = $this->get_lists_public();
  670. //$this->logger->debug($_REQUEST['nl']);
  671. foreach ($lists as $list) {
  672. if (in_array('' . $list->id, $_REQUEST['nl'])) {
  673. $user['list_' . $list->id] = 1;
  674. }
  675. }
  676. } else {
  677. $this->logger->debug('No lists received');
  678. }
  679. // Forced lists (general or by language)
  680. $lists = $this->get_lists();
  681. foreach ($lists as $list) {
  682. if ($list->forced) {
  683. $user['list_' . $list->id] = 1;
  684. }
  685. if (in_array($language, $list->languages)) {
  686. $user['list_' . $list->id] = 1;
  687. }
  688. }
  689. // TODO: should be removed!!!
  690. if (defined('NEWSLETTER_FEED_VERSION')) {
  691. $options_feed = get_option('newsletter_feed', array());
  692. if ($options_feed['add_new'] == 1) {
  693. $user['feed'] = 1;
  694. }
  695. }
  696. return $user;
  697. }
  698. /**
  699. * Sends a service message applying the template.
  700. *
  701. * @param TNP_User $user
  702. * @param string $subject
  703. * @param string $message
  704. * @return type
  705. */
  706. function mail($user, $subject, $message) {
  707. $language = $this->get_user_language($user);
  708. $options_template = $this->get_options('template', $language);
  709. $template = trim($options_template['template']);
  710. if (empty($template) || strpos($template, '{message}') === false) {
  711. $template = '{message}';
  712. }
  713. $message = str_replace('{message}', $message, $template);
  714. $headers = array('Auto-Submitted' => 'auto-generated');
  715. // Replaces tags from the template
  716. $message = $this->replace($message, $user);
  717. $subject = $this->replace($subject, $user);
  718. return Newsletter::instance()->mail($user->email, $subject, $message, $headers);
  719. }
  720. /**
  721. * Confirms a subscription changing the user status and, possibly, merging the
  722. * temporary data if present.
  723. *
  724. * @param int $user_id Optional. If null the user is extracted from the request.
  725. * @return TNP_User
  726. */
  727. function confirm($user_id = null, $emails = true) {
  728. if ($user_id == null) {
  729. $user = $this->get_user_from_request(true);
  730. // Is there any temporary data from a subscription to be confirmed?
  731. $data = get_transient($this->get_user_key($user));
  732. if ($data !== false) {
  733. $_REQUEST = $data;
  734. // Update the user profile since it's now confirmed
  735. $user = $this->update_user_from_request((array) $user);
  736. $user = $this->save_user($user);
  737. delete_transient($this->get_user_key($user));
  738. // Forced a fake status so the welcome email is sent
  739. $user->status = Newsletter::STATUS_NOT_CONFIRMED;
  740. }
  741. } else {
  742. $user = $this->get_user($user_id);
  743. if ($user == null) {
  744. die('No subscriber found.');
  745. }
  746. }
  747. $this->update_user_last_activity($user);
  748. setcookie('newsletter', $user->id . '-' . $user->token, time() + 60 * 60 * 24 * 365, '/');
  749. if ($user->status == TNP_User::STATUS_CONFIRMED) {
  750. $this->add_user_log($user, 'activate');
  751. do_action('newsletter_user_confirmed', $user);
  752. return $user;
  753. }
  754. $this->set_user_status($user, TNP_User::STATUS_CONFIRMED);
  755. $user = $this->get_user($user);
  756. $this->add_user_log($user, 'activate');
  757. do_action('newsletter_user_confirmed', $user);
  758. $this->notify_admin($user, 'Newsletter subscription');
  759. if ($emails) {
  760. $this->send_message('confirmed', $user);
  761. }
  762. return $user;
  763. }
  764. /**
  765. * Sends a message (activation, welcome, cancellation, ...) with the correct template
  766. * and checking if the message itself is disabled
  767. *
  768. * @param string $type
  769. * @param TNP_User $user
  770. */
  771. function send_message($type, $user, $force = false) {
  772. if (!$force && !empty($this->options[$type . '_disabled'])) {
  773. return true;
  774. }
  775. $language = $this->get_user_language($user);
  776. $options = $this->get_options('', $language);
  777. $message = $options[$type . '_message'];
  778. if ($user->status == Newsletter::STATUS_NOT_CONFIRMED) {
  779. $message = $this->add_microdata($message);
  780. }
  781. $subject = $options[$type . '_subject'];
  782. return $this->mail($user, $subject, $message);
  783. }
  784. /**
  785. * Saves the subscriber data.
  786. *
  787. * @return type
  788. */
  789. function save_profile() {
  790. return NewsletterProfile::instance()->save_profile();
  791. }
  792. function is_double_optin() {
  793. return $this->options['noconfirmation'] == 0;
  794. }
  795. /**
  796. * Sends the activation email without conditions.
  797. *
  798. * @param stdClass $user
  799. * @return bool
  800. */
  801. function send_activation_email($user) {
  802. return $this->send_message('confirmation', $user, true);
  803. }
  804. /**
  805. * Finds the right way to show the message identified by $key (welcome, unsubscription, ...) redirecting the user to the
  806. * WordPress page or loading the configured url or activating the standard page.
  807. */
  808. function show_message($key, $user, $alert = '', $email = null) {
  809. $url = '';
  810. if (isset($_REQUEST['ncu'])) {
  811. // Custom URL from the form
  812. $url = $_REQUEST['ncu'];
  813. } else {
  814. // Per message custom URL from configuration (language variants could not be supported)
  815. $options = $this->get_options('', $this->get_user_language($user));
  816. if (!empty($options[$key . '_url'])) {
  817. $url = $options[$key . '_url'];
  818. }
  819. }
  820. $url = Newsletter::instance()->build_message_url($url, $key, $user, $email, $alert);
  821. wp_redirect($url);
  822. die();
  823. }
  824. function get_message_key_from_request() {
  825. if (empty($_GET['nm'])) {
  826. return 'subscription';
  827. }
  828. $key = $_GET['nm'];
  829. switch ($key) {
  830. case 's': return 'confirmation';
  831. case 'c': return 'confirmed';
  832. case 'u': return 'unsubscription';
  833. case 'uc': return 'unsubscribed';
  834. case 'p':
  835. case 'pe':
  836. return 'profile';
  837. default: return $key;
  838. }
  839. }
  840. var $privacy_url = false;
  841. function get_privacy_url() {
  842. if (!$this->privacy_url) {
  843. if (!empty($this->options_profile['privacy_use_wp_url']) && function_exists('get_privacy_policy_url')) {
  844. $this->privacy_url = get_privacy_policy_url();
  845. } else {
  846. $this->privacy_url = $this->options_profile['privacy_url'];
  847. }
  848. }
  849. return $this->privacy_url;
  850. }
  851. function get_form_javascript() {
  852. $buffer = "\n\n";
  853. $buffer .= '<script type="text/javascript">' . "\n";
  854. $buffer .= '//<![CDATA[' . "\n";
  855. $buffer .= 'if (typeof newsletter_check !== "function") {' . "\n";
  856. $buffer .= 'window.newsletter_check = function (f) {' . "\n";
  857. $buffer .= ' var re = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-]{1,})+\.)+([a-zA-Z0-9]{2,})+$/;' . "\n";
  858. $buffer .= ' if (!re.test(f.elements["ne"].value)) {' . "\n";
  859. $buffer .= ' alert("' . addslashes($this->options_profile['email_error']) . '");' . "\n";
  860. $buffer .= ' return false;' . "\n";
  861. $buffer .= ' }' . "\n";
  862. if ($this->options_profile['name_status'] == 2 && $this->options_profile['name_rules'] == 1) {
  863. $buffer .= ' if (f.elements["nn"] && (f.elements["nn"].value == "" || f.elements["nn"].value == f.elements["nn"].defaultValue)) {' . "\n";
  864. $buffer .= ' alert("' . addslashes($this->options_profile['name_error']) . '");' . "\n";
  865. $buffer .= ' return false;' . "\n";
  866. $buffer .= ' }' . "\n";
  867. }
  868. if ($this->options_profile['surname_status'] == 2 && $this->options_profile['surname_rules'] == 1) {
  869. $buffer .= ' if (f.elements["ns"] && (f.elements["ns"].value == "" || f.elements["ns"].value == f.elements["ns"].defaultValue)) {' . "\n";
  870. $buffer .= ' alert("' . addslashes($this->options_profile['surname_error']) . '");' . "\n";
  871. $buffer .= ' return false;' . "\n";
  872. $buffer .= ' }' . "\n";
  873. }
  874. $buffer .= ' for (var i=1; i<' . NEWSLETTER_PROFILE_MAX . '; i++) {' . "\n";
  875. $buffer .= ' if (f.elements["np" + i] && f.elements["np" + i].required && f.elements["np" + i].value == "") {' . "\n";
  876. $buffer .= ' alert("' . addslashes($this->options_profile['profile_error']) . '");' . "\n";
  877. $buffer .= ' return false;' . "\n";
  878. $buffer .= ' }' . "\n";
  879. $buffer .= ' }' . "\n";
  880. $buffer .= ' if (f.elements["ny"] && !f.elements["ny"].checked) {' . "\n";
  881. $buffer .= ' alert("' . addslashes($this->options_profile['privacy_error']) . '");' . "\n";
  882. $buffer .= ' return false;' . "\n";
  883. $buffer .= ' }' . "\n";
  884. $buffer .= ' return true;' . "\n";
  885. $buffer .= '}' . "\n";
  886. $buffer .= '}' . "\n";
  887. $buffer .= '//]]>' . "\n";
  888. $buffer .= '</script>' . "\n\n";
  889. return $buffer;
  890. }
  891. function shortcode_subscription($attrs, $content) {
  892. if (!is_array($attrs)) {
  893. $attrs = array();
  894. }
  895. $attrs = array_merge(array('class' => 'newsletter', 'style' => ''), $attrs);
  896. $action = esc_attr($this->build_action_url('s'));
  897. $class = esc_attr($attrs['class']);
  898. $style = esc_attr($attrs['style']);
  899. $buffer = '<form method="post" action="' . $action . '" class="' . $class . '" style="' . $style . '">' . "\n";
  900. $language = $this->get_current_language();
  901. $buffer .= '<input type="hidden" name="nlang" value="' . esc_attr($language) . '">' . "\n";
  902. if (isset($attrs['referrer'])) {
  903. $buffer .= '<input type="hidden" name="nr" value="' . esc_attr($attrs['referrer']) . '">' . "\n";
  904. }
  905. if (isset($attrs['confirmation_url'])) {
  906. $buffer .= "<input type='hidden' name='ncu' value='" . esc_attr($attrs['confirmation_url']) . "'>\n";
  907. }
  908. if (isset($attrs['list'])) {
  909. $arr = explode(',', $attrs['list']);
  910. foreach ($arr as $a) {
  911. $buffer .= "<input type='hidden' name='nl[]' value='" . esc_attr(trim($a)) . "'>\n";
  912. }
  913. }
  914. //$content = str_replace("\r\n", "", $content);
  915. $buffer .= do_shortcode($content);
  916. if (isset($attrs['button_label'])) {
  917. $label = $attrs['button_label'];
  918. } else {
  919. $label = $this->options_profile['subscribe'];
  920. }
  921. if (!empty($label)) {
  922. $buffer .= '<div class="tnp-field tnp-field-button">';
  923. if (strpos($label, 'http') === 0) {
  924. $buffer .= '<input class="tnp-button-image" type="image" src="' . $label . '">';
  925. } else {
  926. $buffer .= '<input class="tnp-button" type="submit" value="' . $label . '">';
  927. }
  928. $buffer .= '</div>';
  929. }
  930. $buffer .= '</form>';
  931. return $buffer;
  932. }
  933. function _shortcode_label($name, $attrs, $suffix = null) {
  934. $options_profile = $this->get_options('profile', $this->get_current_language());
  935. if (!$suffix) {
  936. $suffix = $name;
  937. }
  938. $buffer = '<label for="tnp-' . $suffix . '">';
  939. if (isset($attrs['label'])) {
  940. if (empty($attrs['label'])) {
  941. return;
  942. } else {
  943. $buffer .= esc_html($attrs['label']);
  944. }
  945. } else {
  946. $buffer .= esc_html($options_profile[$name]);
  947. }
  948. $buffer .= "</label>\n";
  949. return $buffer;
  950. }
  951. function shortcode_newsletter_field($attrs, $content) {
  952. $name = $attrs['name'];
  953. $buffer = '';
  954. if ($name == 'email') {
  955. $buffer .= '<div class="tnp-field tnp-field-email">';
  956. $buffer .= $this->_shortcode_label('email', $attrs);
  957. $buffer .= '<input class="tnp-email" type="email" name="ne" value=""';
  958. if (isset($attrs['placeholder']))
  959. $buffer .= ' placeholder="' . esc_attr($attrs['placeholder']) . '"';
  960. $buffer .= 'required>';
  961. if (isset($attrs['button_label'])) {
  962. $label = $attrs['button_label'];
  963. if (strpos($label, 'http') === 0) {
  964. $buffer .= ' <input class="tnp-submit-image" type="image" src="' . esc_attr(esc_url_raw($label)) . '">';
  965. } else {
  966. $buffer .= ' <input class="tnp-submit" type="submit" value="' . esc_attr($label) . '" style="width: 29%">';
  967. }
  968. }
  969. $buffer .= "</div>\n";
  970. return $buffer;
  971. }
  972. if ($name == 'first_name' || $name == 'name') {
  973. $buffer .= '<div class="tnp-field tnp-field-firstname">';
  974. $buffer .= $this->_shortcode_label('name', $attrs);
  975. $buffer .= '<input class="tnp-name" type="text" name="nn" value=""';
  976. if (isset($attrs['placeholder']))
  977. $buffer .= ' placeholder="' . esc_attr($attrs['placeholder']) . '"';
  978. if ($this->options_profile['name_rules'] == 1) {
  979. $buffer .= ' required';
  980. }
  981. $buffer .= '>';
  982. $buffer .= "</div>\n";
  983. return $buffer;
  984. }
  985. if ($name == 'last_name' || $name == 'surname') {
  986. $buffer .= '<div class="tnp-field tnp-field-surname">';
  987. $buffer .= $this->_shortcode_label('surname', $attrs);
  988. $buffer .= '<input class="tnp-surname" type="text" name="ns" value=""';
  989. if (isset($attrs['placeholder']))
  990. $buffer .= ' placeholder="' . esc_attr($attrs['placeholder']) . '"';
  991. if ($this->options_profile['surname_rules'] == 1) {
  992. $buffer .= ' required';
  993. }
  994. $buffer .= '>';
  995. $buffer .= '</div>';
  996. return $buffer;
  997. }
  998. if ($name == 'preference' || $name == 'list') {
  999. $list = $this->get_list($attrs['number']);
  1000. if (!$list || $list->status == 0 || $list->forced) {
  1001. return;
  1002. }
  1003. if (isset($attrs['hidden'])) {
  1004. return '<input type="hidden" name="nl[]" value="' . esc_attr($list->id) . '">';
  1005. }
  1006. $buffer .= '<div class="tnp-field tnp-field-checkbox tnp-field-list"><label for="nl' . esc_attr($list->id) . '">';
  1007. $buffer .= '<input type="checkbox" id="nl' . esc_attr($list->id) . '" name="nl[]" value="' . esc_attr($list->id) . '"';
  1008. if (isset($attrs['checked'])) {
  1009. $buffer .= ' checked';
  1010. }
  1011. $buffer .= '>';
  1012. if (isset($attrs['label'])) {
  1013. if ($attrs['label'] != '')
  1014. $buffer .= '&nbsp;' . esc_html($attrs['label']) . '</label>';
  1015. } else {
  1016. $buffer .= '&nbsp;' . esc_html($list->name) . '</label>';
  1017. }
  1018. $buffer .= "</div>\n";
  1019. return $buffer;
  1020. }
  1021. // All the lists
  1022. if ($name == 'lists' || $name == 'preferences') {
  1023. $tmp = '';
  1024. $lists = $this->get_lists();
  1025. foreach ($lists as $list) {
  1026. if ($list->status != 2 || $list->forced) {
  1027. continue;
  1028. }
  1029. //die('ddd');
  1030. $tmp .= '<div class="tnp-field tnp-field-checkbox tnp-field-list"><label for="nl' . $list->id . '">';
  1031. $tmp .= '<input type="checkbox" id="nl' . $list->id . '" name="nl[]" value="' . $list->id . '"';
  1032. if ($list->checked)
  1033. $tmp .= ' checked';
  1034. $tmp .= '>&nbsp;' . esc_html($list->name) . '</label>';
  1035. $tmp .= "</div>\n";
  1036. }
  1037. return $tmp;
  1038. }
  1039. // TODO: add the "not specified"
  1040. if ($name == 'sex' || $name == 'gender') {
  1041. $buffer .= '<div class="tnp-field tnp-field-gender">';
  1042. if (isset($attrs['label'])) {
  1043. if ($attrs['label'] != '')
  1044. $buffer .= '<label for="">' . esc_html($attrs['label']) . '</label>';
  1045. } else {
  1046. $buffer .= '<label for="">' . esc_html($this->options_profile['sex']) . '</label>';
  1047. }
  1048. $buffer .= '<select name="nx" class="tnp-gender">';
  1049. $buffer .= '<option value="m">' . esc_html($this->options_profile['sex_male']) . '</option>';
  1050. $buffer .= '<option value="f">' . esc_html($this->options_profile['sex_female']) . '</option>';
  1051. $buffer .= '</select>';
  1052. $buffer .= "</div>\n";
  1053. return $buffer;
  1054. }
  1055. if ($name == 'profile' && isset($attrs['number'])) {
  1056. $number = (int) $attrs['number'];
  1057. $type = $this->options_profile['profile_' . $number . '_type'];
  1058. $size = isset($attrs['size']) ? $attrs['size'] : '';
  1059. $buffer .= '<div class="tnp-field tnp-field-profile">';
  1060. if (isset($attrs['label'])) {
  1061. if ($attrs['label'] != '') {
  1062. $buffer .= '<label>' . esc_html($attrs['label']) . '</label>';
  1063. }
  1064. } else {
  1065. $buffer .= '<label>' . esc_html($this->options_profile['profile_' . $number]) . '</label>';
  1066. }
  1067. $placeholder = isset($attrs['placeholder']) ? $attrs['placeholder'] : $this->options_profile['profile_' . $number . '_placeholder'];
  1068. $required = $this->options_profile['profile_' . $number . '_rules'] == 1;
  1069. // Text field
  1070. if ($type == 'text') {
  1071. $buffer .= '<input class="tnp-profile tnp-profile-' . $number . '" type="text" size="' . esc_attr($size) . '" name="np' . $number . '" placeholder="' . esc_attr($placeholder) . '"';
  1072. if ($required) {
  1073. $buffer .= ' required';
  1074. }
  1075. $buffer .= '>';
  1076. }
  1077. // Select field
  1078. if ($type == 'select') {
  1079. $buffer .= '<select class="tnp-profile tnp-profile-' . $number . '" name="np' . $number . '"';
  1080. if ($required) {
  1081. $buffer .= ' required';
  1082. }
  1083. $buffer .= '>';
  1084. if (!empty($placeholder)) {
  1085. $buffer .= '<option value="">' . esc_html($placeholder) . '</option>';
  1086. }
  1087. $opts = explode(',', $this->options_profile['profile_' . $number . '_options']);
  1088. for ($j = 0; $j < count($opts); $j++) {
  1089. $buffer .= '<option>' . esc_html(trim($opts[$j])) . '</option>';
  1090. }
  1091. $buffer .= "</select>\n";
  1092. }
  1093. $buffer .= "</div>\n";
  1094. return $buffer;
  1095. }
  1096. if (strpos($name, 'privacy') === 0) {
  1097. $options_profile = $this->get_options('profile', $this->get_current_language());
  1098. if (!isset($attrs['url'])) {
  1099. $attrs['url'] = $this->get_privacy_url();
  1100. }
  1101. if (!isset($attrs['label'])) {
  1102. $attrs['label'] = $options_profile['privacy'];
  1103. }
  1104. $buffer .= '<div class="tnp-field tnp-field-checkbox tnp-field-privacy">';
  1105. $buffer .= '<input type="checkbox" name="ny" required class="tnp-privacy" id="tnp-privacy"> ';
  1106. $buffer .= '<label for="tnp-privacy">';
  1107. if (!empty($attrs['url'])) {
  1108. $buffer .= '<a target="_blank" href="' . esc_attr($attrs['url']) . '">';
  1109. }
  1110. $buffer .= $attrs['label'];
  1111. if (!empty($attrs['url'])) {
  1112. $buffer .= '</a>';
  1113. }
  1114. $buffer .= '</label>';
  1115. $buffer .= '</div>';
  1116. return $buffer;
  1117. }
  1118. }
  1119. /**
  1120. * Returns the form html code for subscription.
  1121. *
  1122. * @return string The html code of the subscription form
  1123. */
  1124. function get_subscription_form_html5($referrer = null, $action = null, $attrs = array()) {
  1125. return $this->get_subscription_form($referrer, $action, $attrs);
  1126. }
  1127. function get_privacy_field() {
  1128. $options_profile = $this->get_options('profile', $this->get_current_language());
  1129. $privacy_status = (int) $options_profile['privacy_status'];
  1130. if (empty($privacy_status))
  1131. return false;
  1132. $buffer = '<label>';
  1133. if ($privacy_status === 1) {
  1134. $buffer .= '<input type="checkbox" name="ny" required class="tnp-privacy">&nbsp;';
  1135. }
  1136. $url = $this->get_privacy_url();
  1137. if (!empty($url)) {
  1138. $buffer .= '<a target="_blank" href="' . esc_attr($url) . '">';
  1139. $buffer .= esc_attr($options_profile['privacy']) . '</a>';
  1140. } else {
  1141. $buffer .= esc_html($options_profile['privacy']);
  1142. }
  1143. $buffer .= "</label>";
  1144. return $buffer;
  1145. }
  1146. /**
  1147. * The new standard form.
  1148. *
  1149. * @param type $referrer
  1150. * @param type $action
  1151. * @param type $attrs
  1152. * @return string
  1153. */
  1154. function get_subscription_form($referrer = null, $action = null, $attrs = array()) {
  1155. $language = $this->get_current_language();
  1156. $options_profile = $this->get_options('profile', $language);
  1157. if (isset($attrs['action'])) {
  1158. $action = $attrs['action'];
  1159. }
  1160. if (isset($attrs['referrer'])) {
  1161. $referrer = $attrs['referrer'];
  1162. }
  1163. $buffer = '';
  1164. if (empty($action)) {
  1165. $action = $this->build_action_url('s');
  1166. }
  1167. if ($referrer != 'widget') {
  1168. if (isset($attrs['class'])) {
  1169. $buffer .= '<div class="tnp tnp-subscription ' . $attrs['class'] . '">' . "\n";
  1170. } else {
  1171. $buffer .= '<div class="tnp tnp-subscription">' . "\n";
  1172. }
  1173. }
  1174. $buffer .= '<form method="post" action="' . esc_attr($action) . '" onsubmit="return newsletter_check(this)">' . "\n\n";
  1175. $buffer .= '<input type="hidden" name="nlang" value="' . esc_attr($language) . '">' . "\n";
  1176. if (!empty($referrer)) {
  1177. $buffer .= '<input type="hidden" name="nr" value="' . esc_attr($referrer) . '">' . "\n";
  1178. }
  1179. if (isset($attrs['confirmation_url'])) {
  1180. $buffer .= "<input type='hidden' name='ncu' value='" . esc_attr($attrs['confirmation_url']) . "'>\n";
  1181. }
  1182. // Compatibility
  1183. if (isset($attrs['list'])) {
  1184. $attrs['lists'] = $attrs['list'];
  1185. }
  1186. // Hidden lists
  1187. if (isset($attrs['lists'])) {
  1188. $arr = explode(',', $attrs['lists']);
  1189. foreach ($arr as $a) {
  1190. $buffer .= "<input type='hidden' name='nl[]' value='" . ((int) trim($a)) . "'>\n";
  1191. }
  1192. }
  1193. if ($options_profile['name_status'] == 2) {
  1194. $buffer .= '<div class="tnp-field tnp-field-firstname"><label>' . esc_html($options_profile['name']) . '</label>';
  1195. $buffer .= '<input class="tnp-firstname" type="text" name="nn" ' . ($options_profile['name_rules'] == 1 ? 'required' : '') . '></div>';
  1196. $buffer .= "\n";
  1197. }
  1198. if ($options_profile['surname_status'] == 2) {
  1199. $buffer .= '<div class="tnp-field tnp-field-lastname"><label>' . esc_html($options_profile['surname']) . '</label>';
  1200. $buffer .= '<input class="tnp-lastname" type="text" name="ns" ' . ($options_profile['surname_rules'] == 1 ? 'required' : '') . '></div>';
  1201. $buffer .= "\n";
  1202. }
  1203. $buffer .= '<div class="tnp-field tnp-field-email"><label>' . esc_html($options_profile['email']) . '</label>';
  1204. $buffer .= '<input class="tnp-email" type="email" name="ne" required></div>';
  1205. $buffer .= "\n";
  1206. if (isset($options_profile['sex_status']) && $options_profile['sex_status'] == 2) {
  1207. $buffer .= '<div class="tnp-field tnp-field-gender"><label>' . esc_html($options_profile['sex']) . '</label>';
  1208. $buffer .= '<select name="nx" class="tnp-gender"';
  1209. if ($options_profile['sex_rules'] == 1) {
  1210. $buffer .= ' required><option value=""></option>';
  1211. } else {
  1212. $buffer .= '><option value="n">' . esc_html($options_profile['sex_none']) . '</option>';
  1213. }
  1214. $buffer .= '<option value="m">' . esc_html($options_profile['sex_male']) . '</option>';
  1215. $buffer .= '<option value="f">' . esc_html($options_profile['sex_female']) . '</option>';
  1216. $buffer .= '</select></div>';
  1217. $buffer .= "\n";
  1218. }
  1219. $tmp = '';
  1220. $lists = $this->get_lists_for_subscription($language);
  1221. if (!empty($attrs['lists_field_layout']) && $attrs['lists_field_layout'] == 'dropdown') {
  1222. foreach ($lists as $list) {
  1223. $tmp .= '<option value="' . $list->id . '"';
  1224. if ($list->checked) {
  1225. $tmp .= ' selected';
  1226. }
  1227. $tmp .= '>' . esc_html($list->name) . '</option>';
  1228. $tmp .= "\n";
  1229. }
  1230. if (!empty($attrs['lists_field_empty_label'])) {
  1231. $tmp = '<option value="">' . $attrs['lists_field_empty_label'] . '</option>' . $tmp;
  1232. }
  1233. if (!empty($tmp)) {
  1234. $tmp = '<select class="tnp-lists" name="nl[]" required>' . $tmp . '</select>';
  1235. }
  1236. if (!empty($tmp)) {
  1237. $buffer .= '<div class="tnp-field tnp-lists">';
  1238. if (!empty($attrs['lists_field_label'])) {
  1239. $buffer .= '<label>' . $attrs['lists_field_label'] . '</label>';
  1240. }
  1241. $buffer .= $tmp . '</div>';
  1242. }
  1243. } else {
  1244. foreach ($lists as $list) {
  1245. $tmp .= '<div class="tnp-field tnp-field-list"><label><input class="tnp-preference" type="checkbox" name="nl[]" value="' . $list->id . '"';
  1246. if ($list->checked) {
  1247. $tmp .= ' checked';
  1248. }
  1249. $tmp .= '/>&nbsp;' . esc_html($list->name) . '</label></div>';
  1250. }
  1251. if (!empty($tmp)) {
  1252. $buffer .= '<div class="tnp-lists">';
  1253. if (!empty($attrs['lists_field_label'])) {
  1254. $buffer .= '<label>' . $attrs['lists_field_label'] . '</label>';
  1255. }
  1256. $buffer .= $tmp . '</div>';
  1257. }
  1258. }
  1259. // Extra profile fields
  1260. for ($i = 1; $i <= NEWSLETTER_PROFILE_MAX; $i++) {
  1261. // Not for subscription form
  1262. if ($options_profile['profile_' . $i . '_status'] != 2) {
  1263. continue;
  1264. }
  1265. $buffer .= '<div class="tnp-field tnp-field-profile"><label>' .
  1266. esc_html($options_profile['profile_' . $i]) . '</label>';
  1267. // Text field
  1268. if ($options_profile['profile_' . $i . '_type'] == 'text') {
  1269. $buffer .= '<input class="tnp-profile tnp-profile-' . $i . '" type="text"' . ($options_profile['profile_' . $i . '_rules'] == 1 ? ' required' : '') . ' name="np' . $i . '">';
  1270. }
  1271. // Select field
  1272. if ($options_profile['profile_' . $i . '_type'] == 'select') {
  1273. $buffer .= '<select class="tnp-profile tnp-profile-' . $i . '" name="np' . $i . '" ' . ($options_profile['profile_' . $i . '_rules'] == 1 ? ' required' : '') . '>' . "\n";
  1274. $buffer .= "<option></option>\n";
  1275. $opts = explode(',', $options_profile['profile_' . $i . '_options']);
  1276. for ($j = 0; $j < count($opts); $j++) {
  1277. $buffer .= "<option>" . esc_html(trim($opts[$j])) . "</option>\n";
  1278. }
  1279. $buffer .= "</select>\n";
  1280. }
  1281. $buffer .= '</div>';
  1282. }
  1283. $extra = apply_filters('newsletter_subscription_extra', array());
  1284. foreach ($extra as $x) {
  1285. $label = $x['label'];
  1286. if (empty($label)) {
  1287. $label = '&nbsp;';
  1288. }
  1289. $name = '';
  1290. if (!empty($x['name'])) {
  1291. $name = $x['name'];
  1292. }
  1293. $buffer .= '<div class="tnp-field tnp-field-' . $name . '"><label>' . $label . "</label>";
  1294. $buffer .= $x['field'] . "</div>\n";
  1295. }
  1296. $privacy_status = (int) $options_profile['privacy_status'];
  1297. if ($privacy_status === 1 || $privacy_status === 2) {
  1298. $buffer .= '<div class="tnp-field tnp-field-privacy">';
  1299. $buffer .= '<label>';
  1300. if ($privacy_status === 1) {
  1301. $buffer .= '<input type="checkbox" name="ny" required class="tnp-privacy">&nbsp;';
  1302. }
  1303. $url = $this->get_privacy_url();
  1304. if (!empty($url)) {
  1305. $buffer .= '<a target="_blank" href="' . esc_attr($url) . '">';
  1306. $buffer .= esc_attr($options_profile['privacy']) . '</a>';
  1307. } else {
  1308. $buffer .= esc_html($options_profile['privacy']);
  1309. }
  1310. $buffer .= "</label></div>\n";
  1311. }
  1312. $buffer .= '<div class="tnp-field tnp-field-button">';
  1313. $button_style = '';
  1314. if (!empty($attrs['button_color'])) {
  1315. $button_style = 'style="background-color:' . esc_attr($attrs['button_color']) . '"';
  1316. }
  1317. if (strpos($options_profile['subscribe'], 'http') === 0) {
  1318. $buffer .= '<input class="tnp-submit-image" type="image" src="' . esc_attr($options_profile['subscribe']) . '">' . "\n";
  1319. } else {
  1320. $buffer .= '<input class="tnp-submit" type="submit" value="' . esc_attr($options_profile['subscribe']) . '" ' . $button_style . '>' . "\n";
  1321. }
  1322. $buffer .= "</div>\n</form>\n";
  1323. if ($referrer != 'widget') {
  1324. $buffer .= "</div>\n";
  1325. }
  1326. return $buffer;
  1327. }
  1328. function get_profile_form($user) {
  1329. return NewsletterProfile::instance()->get_profile_form($user);
  1330. }
  1331. function get_form($number) {
  1332. $options = get_option('newsletter_forms');
  1333. $form = $options['form_' . $number];
  1334. $form = do_shortcode($form);
  1335. $action = $this->build_action_url('s');
  1336. if (stripos($form, '<form') === false) {
  1337. $form = '<form method="post" action="' . $action . '">' . $form . '</form>';
  1338. }
  1339. // For compatibility
  1340. $form = str_replace('{newsletter_url}', $action, $form);
  1341. $form = $this->replace_lists($form);
  1342. return $form;
  1343. }
  1344. /** Replaces on passed text the special tag {lists} that can be used to show the preferences as a list of checkbox.
  1345. * They are called lists but on configuration panel they are named preferences!
  1346. *
  1347. * @param string $buffer
  1348. * @return string
  1349. */
  1350. function replace_lists($buffer) {
  1351. $checkboxes = '';
  1352. $lists = $this->get_lists_for_subscription($this->get_current_language());
  1353. foreach ($lists as $list) {
  1354. $checkboxes .= '<input type="checkbox" name="nl[]" value="' . $list->id . '"/>&nbsp;' . $list->name . '<br />';
  1355. }
  1356. $buffer = str_replace('{lists}', $checkboxes, $buffer);
  1357. $buffer = str_replace('{preferences}', $checkboxes, $buffer);
  1358. return $buffer;
  1359. }
  1360. function notify_admin($user, $subject) {
  1361. if (empty($this->options['notify'])) {
  1362. return;
  1363. }
  1364. $message = "Subscriber details:\n\n" .
  1365. "email: " . $user->email . "\n" .
  1366. "first name: " . $user->name . "\n" .
  1367. "last name: " . $user->surname . "\n" .
  1368. "gender: " . $user->sex . "\n";
  1369. $lists = $this->get_lists();
  1370. foreach ($lists as $list) {
  1371. $field = 'list_' . $list->id;
  1372. $message .= $list->name . ': ' . (empty($user->$field) ? "NO" : "YES") . "\n";
  1373. }
  1374. for ($i = 0; $i < NEWSLETTER_PROFILE_MAX; $i++) {
  1375. if (empty($this->options_profile['profile_' . $i])) {
  1376. continue;
  1377. }
  1378. $field = 'profile_' . $i;
  1379. $message .= $this->options_profile['profile_' . $i] . ': ' . $user->$field . "\n";
  1380. }
  1381. $message .= "token: " . $user->token . "\n" .
  1382. "status: " . $user->status . "\n";
  1383. $email = trim($this->options['notify_email']);
  1384. if (empty($email)) {
  1385. $email = get_option('admin_email');
  1386. }
  1387. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1388. Newsletter::instance()->mail($email, '[' . $blogname . '] ' . $subject, array('text' => $message));
  1389. }
  1390. function get_subscription_form_minimal($attrs) {
  1391. $language = $this->get_current_language();
  1392. if (!is_array($attrs)) {
  1393. $attrs = array();
  1394. }
  1395. $options_profile = $this->get_options('profile', $language);
  1396. $attrs = array_merge(array('class' => '', 'referrer' => 'minimal',
  1397. 'button' => $options_profile['subscribe'], 'button_color' => '',
  1398. 'button_radius' => '', 'placeholder' => $options_profile['email']), $attrs);
  1399. $form = '';
  1400. $form .= '<div class="tnp tnp-subscription-minimal ' . $attrs['class'] . '">';
  1401. $form .= '<form action="' . esc_attr($this->build_action_url('s')) . '" method="post">';
  1402. if (isset($attrs['lists'])) {
  1403. $arr = explode(',', $attrs['lists']);
  1404. foreach ($arr as $a) {
  1405. $form .= "<input type='hidden' name='nl[]' value='" . ((int) trim($a)) . "'>\n";
  1406. }
  1407. }
  1408. $form .= '<input type="hidden" name="nr" value="' . esc_attr($attrs['referrer']) . '">';
  1409. $form .= '<input type="hidden" name="nlang" value="' . esc_attr($language) . '">' . "\n";
  1410. $form .= '<input class="tnp-email" type="email" required name="ne" value="" placeholder="' . esc_attr($attrs['placeholder']) . '">';
  1411. $form .= '<input class="tnp-submit" type="submit" value="' . esc_attr($attrs['button']) . '"'
  1412. . ' style="background-color:' . esc_attr($attrs['button_color']) . '">';
  1413. $privacy_field = $this->get_privacy_field();
  1414. if (!empty($privacy_field)) {
  1415. $form .= '<div class="tnp-privacy-field">' . $privacy_field . '</div>';
  1416. }
  1417. $form .= "</form></div>\n";
  1418. return $form;
  1419. }
  1420. function shortcode_newsletter_form($attrs, $content) {
  1421. if (isset($attrs['type']) && $attrs['type'] == 'minimal') {
  1422. return NewsletterSubscription::instance()->get_subscription_form_minimal($attrs);
  1423. }
  1424. if (!empty($content)) {
  1425. return NewsletterSubscription::instance()->shortcode_subscription($attrs, $content);
  1426. }
  1427. if (isset($attrs['form'])) {
  1428. return NewsletterSubscription::instance()->get_form((int) $attrs['form']);
  1429. } else if (isset($attrs['number'])) {
  1430. return NewsletterSubscription::instance()->get_form((int) $attrs['number']);
  1431. } else {
  1432. if (isset($attrs['layout']) && $attrs['layout'] == 'table') {
  1433. return NewsletterSubscription::instance()->get_subscription_form(null, null, $attrs);
  1434. } else {
  1435. return NewsletterSubscription::instance()->get_subscription_form_html5(null, null, $attrs);
  1436. }
  1437. }
  1438. }
  1439. /**
  1440. *
  1441. * @global wpdb $wpdb
  1442. * @param array $attrs
  1443. * @param string $content
  1444. * @return string
  1445. */
  1446. function shortcode_newsletter($attrs, $content) {
  1447. global $wpdb;
  1448. $message_key = $this->get_message_key_from_request();
  1449. if ($message_key == 'confirmation') {
  1450. $user = $this->get_user_from_request(false, 'preconfirm');
  1451. } else {
  1452. $user = $this->get_user_from_request();
  1453. }
  1454. $message = apply_filters('newsletter_page_text', '', $message_key, $user);
  1455. $options = $this->get_options('', $this->get_current_language($user));
  1456. if (empty($message)) {
  1457. $message = $options[$message_key . '_text'];
  1458. // TODO: the if can be removed
  1459. if ($message_key == 'confirmed') {
  1460. $message .= $options[$message_key . '_tracking'];
  1461. }
  1462. }
  1463. // Now check what form must be added
  1464. if ($message_key == 'subscription') {
  1465. // Compatibility check
  1466. if (stripos($message, '<form') !== false) {
  1467. $message .= $this->get_form_javascript();
  1468. $message = str_ireplace('<form', '<form method="post" action="' . esc_attr($this->get_subscribe_url()) . '" onsubmit="return newsletter_check(this)"', $message);
  1469. } else {
  1470. if (strpos($message, '{subscription_form') === false) {
  1471. $message .= '{subscription_form}';
  1472. }
  1473. if (isset($attrs['form'])) {
  1474. $message = str_replace('{subscription_form}', $this->get_form($attrs['form']), $message);
  1475. } else {
  1476. $message = str_replace('{subscription_form}', $this->get_subscription_form('page'), $message);
  1477. }
  1478. }
  1479. }
  1480. $email = NewsletterSubscription::instance()->get_email_from_request();
  1481. $message = $this->replace($message, $user, $email, 'page');
  1482. $message = do_shortcode($message);
  1483. if (isset($_REQUEST['alert'])) {
  1484. // slashes are already added by wordpress!
  1485. $message .= '<script>alert("' . strip_tags($_REQUEST['alert']) . '");</script>';
  1486. }
  1487. return $message;
  1488. }
  1489. }
  1490. NewsletterSubscription::instance();
  1491. // Compatibility code
  1492. function newsletter_form($number = null) {
  1493. if ($number != null) {
  1494. echo NewsletterSubscription::instance()->get_form($number);
  1495. } else {
  1496. echo NewsletterSubscription::instance()->get_subscription_form();
  1497. }
  1498. }