Changeset 3377250
- Timestamp:
- 10/13/2025 06:31:09 AM (6 months ago)
- Location:
- yournotify/trunk
- Files:
-
- 3 edited
-
includes/class-yournotify-optin.php (modified) (4 diffs)
-
readme.txt (modified) (1 diff)
-
yournotify.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
yournotify/trunk/includes/class-yournotify-optin.php
r3377124 r3377250 2 2 if (!defined('ABSPATH')) exit; 3 3 4 /** Shortcode: [yournotify_subscribe] — AJAX, no page redirect */ 4 /** 5 * Shortcode: [yournotify_subscribe] 6 * - Fields: name (required), email (required), telephone (optional) 7 * - AJAX submit with jQuery (matches v2.0.0 behavior), fetch() fallback 8 * - Does NOT alter settings, SMTP, or other plugin behavior 9 */ 5 10 add_action('init', function(){ 6 11 if (!shortcode_exists('yournotify_subscribe')) { … … 13 18 ], $atts, 'yournotify_subscribe'); 14 19 15 // Safer IDs and values 16 $action = admin_url('admin-ajax.php'); 17 $nonce = wp_create_nonce('yournotify_subscribe'); 18 $form_id = 'yn_sub_' . uniqid(); // avoid any dependency 20 $form_id = 'yn_sub_' . uniqid(); 21 $action = admin_url('admin-ajax.php'); 22 $nonce = wp_create_nonce('yournotify_subscribe'); 19 23 $redirect = get_option('yournotify_success_redirect',''); 20 24 … … 22 26 $html .= '<input type="hidden" name="action" value="yournotify_subscribe_submit">'; 23 27 $html .= '<input type="hidden" name="_wpnonce" value="'.esc_attr($nonce).'">'; 24 25 // Name (required) 26 $html .= '<p><input type="text" name="name" required placeholder="'.esc_attr($atts['name_placeholder']).'"></p>'; 27 28 // Email (required) 29 $html .= '<p><input type="email" name="email" required placeholder="'.esc_attr($atts['email_placeholder']).'"></p>'; 30 31 // Telephone (optional) 32 $html .= '<p><input type="tel" name="telephone" placeholder="'.esc_attr($atts['tel_placeholder']).'"></p>'; 33 34 $html .= '<p><button type="submit">'.esc_html($atts['button_text']).'</button></p>'; 28 $html .= '<p class="yn-field yn-name"><input type="text" name="name" required placeholder="'.esc_attr($atts['name_placeholder']).'"></p>'; 29 $html .= '<p class="yn-field yn-email"><input type="email" name="email" required placeholder="'.esc_attr($atts['email_placeholder']).'"></p>'; 30 $html .= '<p class="yn-field yn-telephone"><input type="tel" name="telephone" placeholder="'.esc_attr($atts['tel_placeholder']).'"></p>'; 31 $html .= '<p class="yn-actions"><button type="submit">'.esc_html($atts['button_text']).'</button></p>'; 35 32 $html .= '<div class="yournotify-response" id="'.esc_attr($form_id).'_resp" aria-live="polite" style="margin-top:6px;"></div>'; 36 33 $html .= '</form>'; 37 34 38 // Minimal JS: intercept submit, POST via fetch, show message inline39 $html .= '<script>(function(){ try{'35 // Submit: prefer jQuery.ajax like v2.0.0; fallback to fetch 36 $html .= '<script>(function(){' 40 37 .'var f=document.getElementById("'.esc_js($form_id).'"); if(!f) return;' 41 38 .'var r=document.getElementById("'.esc_js($form_id).'_resp");' 42 39 .'var redir=f.getAttribute("data-redirect")||"";' 43 .'f.addEventListener("submit",function(e){e.preventDefault();' 44 .'if(r) r.textContent="'.esc_js(__('Sending…','yournotify')).'";' 40 .'function ok(msg){ if(redir){window.location=redir;return;} if(r){r.textContent=msg||"'.esc_js(__('Subscribed','yournotify')).'";} }' 41 .'function fail(msg){ if(r){r.textContent=msg||"'.esc_js(__('Subscription failed. Please try again.','yournotify')).'";} }' 42 .'f.addEventListener("submit",function(e){ e.preventDefault(); if(r){r.textContent="'.esc_js(__('Sending…','yournotify')).'";}' 45 43 .'var fd=new FormData(f);' 46 .'fetch(f.action,{method:"POST",credentials:"same-origin",body:fd})' 47 .'.then(function(res){return res.json();})' 48 .'.then(function(j){' 49 .'if(j&&j.success){ if(redir){window.location=redir;return;}' 50 .'r.textContent=(j.data&&j.data.message)?j.data.message:"'.esc_js(__('Subscribed','yournotify')).'";}' 51 .'else{ var msg=(j&&j.data&&j.data.message)?j.data.message:"'.esc_js(__('Subscription failed. Please try again.','yournotify')).'"; r.textContent=msg; }' 52 .'})' 53 .'.catch(function(){ if(r) r.textContent="'.esc_js(__('Request failed. Please try again.','yournotify')).'"; });' 44 .'if(window.jQuery){' 45 .'jQuery.ajax({url:f.action,method:"POST",data:fd,processData:false,contentType:false,success:function(j){try{if(typeof j==="string"){j=JSON.parse(j);} }catch(e){} if(j&&j.success){ok(j.data&&j.data.message);}else{fail(j&&j.data&&j.data.message);} },error:function(){fail();}});' 46 .'}else{' 47 .'fetch(f.action,{method:"POST",credentials:"same-origin",body:fd}).then(function(res){return res.json();}).then(function(j){ if(j&&j.success){ok(j.data&&j.data.message);}else{fail(j&&j.data&&j.data.message);} }).catch(function(){fail();});' 48 .'}' 54 49 .'});' 55 .'} catch(e){/* noop */}})();</script>';50 .'})();</script>'; 56 51 57 52 return $html; … … 59 54 } 60 55 }); 56 57 /** 58 * Keep existing server-side behavior: 59 * - yournotify__inject_required_fields (api_key, list_id) 60 * - yournotify__normalize_submit_fields (telephone -> phone) 61 * - yournotify_subscribe_submit handlers 62 * 63 * If these helpers do not exist in your build, add them below (no-op otherwise). 64 */ 65 if (!function_exists('yournotify__inject_required_fields')) { 66 function yournotify__inject_required_fields(){ 67 if (empty($_POST['list_id'])) { 68 $lid = get_option('yournotify_list_id',''); 69 if ($lid) $_POST['list_id'] = $lid; 70 } 71 if (empty($_POST['api_key'])) { 72 $k = get_option('yournotify_api_key',''); 73 if ($k) $_POST['api_key'] = $k; 74 } 75 } 76 add_action('wp_ajax_yournotify_subscribe_submit', 'yournotify__inject_required_fields', 1); 77 add_action('wp_ajax_nopriv_yournotify_subscribe_submit', 'yournotify__inject_required_fields', 1); 78 } 79 80 if (!function_exists('yournotify__normalize_submit_fields')) { 81 function yournotify__normalize_submit_fields(){ 82 if (!empty($_POST['telephone']) && empty($_POST['phone'])) { 83 $_POST['phone'] = $_POST['telephone']; 84 } 85 if (isset($_POST['name'])) $_POST['name'] = sanitize_text_field($_POST['name']); 86 if (isset($_POST['email'])) $_POST['email'] = sanitize_email($_POST['email']); 87 if (isset($_POST['telephone'])) $_POST['telephone'] = preg_replace('/[^0-9+\-\s\(\)]/', '', $_POST['telephone']); 88 if (isset($_POST['phone'])) $_POST['phone'] = preg_replace('/[^0-9+\-\s\(\)]/', '', $_POST['phone']); 89 } 90 add_action('wp_ajax_yournotify_subscribe_submit', 'yournotify__normalize_submit_fields', 2); 91 add_action('wp_ajax_nopriv_yournotify_subscribe_submit', 'yournotify__normalize_submit_fields', 2); 92 } -
yournotify/trunk/readme.txt
r3377124 r3377250 4 4 Requires at least: 4.6 5 5 Tested up to: 6.7 6 Stable tag: 2.0. 46 Stable tag: 2.0.5 7 7 License: GPLv3 or later 8 8 -
yournotify/trunk/yournotify.php
r3377124 r3377250 4 4 * Plugin URI: https://yournotify.com 5 5 * Description: Yournotify WP Plugin — Subscriber Form and SMTP override. 6 * Version: 2.0. 46 * Version: 2.0.5 7 7 * Author: Yournotify 8 8 * Author URI: https://yournotify.com
Note: See TracChangeset
for help on using the changeset viewer.