1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
<?php /** * Plugin Name: MiroRi Newsletter * Description: Veľmi jednoduchý plugin na správu a odosielanie newsletterov (noviniek) * Version: 1.3 * Author: MiroR */ // Blokovanie priameho prístupu if (!defined('ABSPATH')) { exit; } // Vytvorenie tabuľky pre e-maily pri aktivácii pluginu function sn_create_table() { global $wpdb; $table_name = $wpdb->prefix . 'newsletter_subscribers'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, email varchar(255) NOT NULL, date_subscribed datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } register_activation_hook(__FILE__, 'sn_create_table'); // Pridanie formulára na registráciu function sn_subscription_form() { ob_start(); ?> <style> .newsletter-form { background-color: #f9f9f9; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); max-width: 400px; margin: auto; } .newsletter-form h2 { font-size: 24px; margin-bottom: 10px; } .newsletter-form label { font-size: 14px; margin-bottom: 5px; display: block; } .newsletter-form input[type="email"] { padding: 10px; margin-bottom: 20px; border: 1px solid #ccc; border-radius: 4px; width: 100%; box-sizing: border-box; } .newsletter-form button[type="submit"] { padding: 10px 20px; background-color: #0073aa; color: #fff; border: none; border-radius: 4px; cursor: pointer; width: 100%; } .newsletter-form button[type="submit"]:hover { background-color: #005177; } </style> <div class="newsletter-form"> <h2><?php _e('Pripojte sa k odberu nových článkov', 'mirori-newsletter'); ?></h2> <i>Vašu e-mail adresu použijeme len na zaslanie informácie o nových článkoch.</i> <form method="post" action=""> <?php wp_nonce_field('sn_subscribe_nonce', 'sn_subscribe_nonce_field'); ?> <label for="sn_email"><?php _e('Zadajte svoj e-mail:', 'mirori-newsletter'); ?></label> <input type="email" name="sn_email" id="sn_email" required> <button type="submit"><?php _e('Prihlásiť sa', 'mirori-newsletter'); ?></button> </form> </div> <?php if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['sn_email']) && check_admin_referer('sn_subscribe_nonce', 'sn_subscribe_nonce_field')) { sn_save_email(sanitize_email($_POST['sn_email'])); } return ob_get_clean(); } add_shortcode('newsletter_form', 'sn_subscription_form'); // Uloženie e-mailu do databázy function sn_save_email($email) { if (!is_email($email)) { echo '<p style="color: red;">' . __('Neplatná e-mailová adresa.', 'mirori-newsletter') . '</p>'; return; } global $wpdb; $table_name = $wpdb->prefix . 'newsletter_subscribers'; if ($wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE email = %s", $email)) > 0) { echo '<p style="color: red;">' . __('Tento e-mail je už zaregistrovaný.', 'mirori-newsletter') . '</p>'; return; } $wpdb->insert( $table_name, ['email' => $email] ); echo '<p style="color: green;">' . __('E-mail bol úspešne zaregistrovaný. Do 24hodín prebehne overenie administrátora. ', 'mirori-newsletter') . '</p>'; // Odoslanie e-mailu administrátorovi $admin_email = get_option('admin_email'); $subject = __('Nový odberateľ newsletteru', 'mirori-newsletter'); $message = __('Nový odberateľ s e-mailom ', 'mirori-newsletter') . $email . __(' sa práve zaregistroval.', 'mirori-newsletter'); $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail($admin_email, $subject, $message, $headers); } // Pridanie stránky v administrácii pre správu odberateľov function sn_admin_menu() { add_menu_page( __('Newsletter Admin', 'mirori-newsletter'), __('Newsletter Admin', 'mirori-newsletter'), 'manage_options', 'mirori-newsletter', 'sn_admin_page' ); } add_action('admin_menu', 'sn_admin_menu'); // Pridanie administrácie správy odberateľov function sn_admin_page() { global $wpdb; $table_name = $wpdb->prefix . 'newsletter_subscribers'; $subscribers = $wpdb->get_results("SELECT * FROM $table_name"); if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete' && isset($_POST['id'])) { $id = intval($_POST['id']); $wpdb->delete($table_name, ['id' => $id]); echo '<div class="notice notice-success"><p>' . __('Odberateľ bol úspešne odstránený.', 'mirori-newsletter') . '</p></div>'; $subscribers = $wpdb->get_results("SELECT * FROM $table_name"); // Refresh the list } ?> <div class="wrap"> <h1><?php _e('Správa odberateľov', 'mirori-newsletter'); ?></h1> <p style="font-size: 18px;"><strong><?php _e('Poznámka:', 'mirori-newsletter'); ?></strong> <br><?php _e('Použite shortcode', 'mirori-newsletter'); ?> <code>[newsletter_form]</code> <?php _e('na zobrazenie formulára na stránkach,', 'mirori-newsletter'); ?> <br><?php _e('e-maily odosielaj cez menu Newsletter, Odoslať e-mail.', 'mirori-newsletter'); ?></p> <table class="widefat" style="table-layout: auto; width: 50%;"> <thead> <tr> <th style="width: 5%;"><?php _e('ID', 'mirori-newsletter'); ?></th> <th style="width: 25%;"><?php _e('E-mail', 'mirori-newsletter'); ?></th> <th style="width: 20%;"><?php _e('Dátum registrácie', 'mirori-newsletter'); ?></th> <th style="width: 10%;"><?php _e('Akcia', 'mirori-newsletter'); ?></th> </tr> </thead> <tbody> <?php foreach ($subscribers as $subscriber): ?> <tr> <td><?php echo esc_html($subscriber->id); ?></td> <td><?php echo esc_html($subscriber->email); ?></td> <td><?php echo esc_html($subscriber->date_subscribed); ?></td> <td> <form method="post" style="display:inline;"> <input type="hidden" name="action" value="delete"> <input type="hidden" name="id" value="<?php echo esc_attr($subscriber->id); ?>"> <?php wp_nonce_field('sn_delete_nonce', 'sn_delete_nonce_field'); ?> <button type="submit" class="button action"><?php _e('Odstrániť', 'mirori-newsletter'); ?></button> </form> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <?php } // Funkcia na odosielanie newsletterov function sn_send_newsletter($subject, $message) { global $wpdb; $table_name = $wpdb->prefix . 'newsletter_subscribers'; $subscribers = $wpdb->get_results("SELECT email FROM $table_name"); foreach ($subscribers as $subscriber) { $unsubscribe_link = add_query_arg('unsubscribe', $subscriber->email, home_url('/')); $message .= '<br><br><a href="' . $unsubscribe_link . '">' . __('Zrušiť registráciu', 'mirori-newsletter') . '</a>'; $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail($subscriber->email, $subject, $message, $headers); } } // Pridanie stránky pre odosielanie e-mailov function sn_send_email_page() { add_submenu_page( 'mirori-newsletter', // Slug hlavnej stránky __('Odoslať e-mail', 'mirori-newsletter'), // Názov podstránky __('Odoslať e-mail', 'mirori-newsletter'), // Názov v menu 'manage_options', // Oprávnenie 'send-newsletter', // Slug podstránky 'sn_render_send_email_page' // Callback funkcia ); } add_action('admin_menu', 'sn_send_email_page'); // Zobrazenie formulára na odoslanie e-mailu function sn_render_send_email_page() { global $wpdb; $table_name = $wpdb->prefix . 'newsletter_subscribers'; $subscribers = $wpdb->get_results("SELECT * FROM $table_name"); if ($_SERVER['REQUEST_METHOD'] === 'POST' && check_admin_referer('sn_send_email_nonce')) { $selected_emails = isset($_POST['emails']) ? array_map('sanitize_email', $_POST['emails']) : []; $subject = sanitize_text_field($_POST['subject']); $message = wp_kses_post($_POST['message']); foreach ($selected_emails as $email) { $unsubscribe_link = add_query_arg('unsubscribe', $email, home_url('/')); $message .= '<br><br><a href="' . $unsubscribe_link . '">' . __('Zrušiť registráciu', 'mirori-newsletter') . '</a>'; $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail($email, $subject, $message, $headers); } echo '<div class="notice notice-success"><p>' . __('E-maily boli odoslané!', 'mirori-newsletter') . '</p></div>'; } ?> <div class="wrap"> <h1><?php _e('Odoslať e-mail odberateľom', 'mirori-newsletter'); ?></h1> <form method="post"> <?php wp_nonce_field('sn_send_email_nonce'); ?> <h2><?php _e('Vyberte odberateľov:', 'mirori-newsletter'); ?></h2> <?php foreach ($subscribers as $subscriber): ?> <label> <input type="checkbox" name="emails[]" value="<?php echo esc_attr($subscriber->email); ?>"> <?php echo esc_html($subscriber->email); ?> </label><br> <?php endforeach; ?> <h2><?php _e('Predmet:', 'mirori-newsletter'); ?></h2> <input type="text" name="subject" style="width: 100%;" required><br> <h2><?php _e('Správa:', 'mirori-newsletter'); ?></h2> <textarea name="message" rows="10" style="width: 100%;" required></textarea><br><br> <button type="submit" class="button button-primary"><?php _e('Odoslať e-mail', 'mirori-newsletter'); ?></button> </form> </div> <?php } // Funkcia na zrušenie registrácie function sn_unsubscribe() { if (isset($_GET['unsubscribe'])) { $email = sanitize_email($_GET['unsubscribe']); global $wpdb; $table_name = $wpdb->prefix . 'newsletter_subscribers'; $wpdb->delete($table_name, ['email' => $email]); echo '<p>' . __('Vaša registrácia bola úspešne zrušená.', 'mirori-newsletter') . '</p>'; } } add_action('init', 'sn_unsubscribe'); |