-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzkribers.php
279 lines (241 loc) · 9.88 KB
/
zkribers.php
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
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* Plugin Name: Zkribers
* Plugin URI: https://github.com/zynexiz/zkribers
* Description: Allows your subscribers to get a notification by email on new posts.
* Version: v0.3 BETA
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Michael Rydén
* Author URI: https://github.com/zynexiz
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
* Text Domain: zkribers
* Domain Path: /lang/
**/
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ){ die; }
// Define database version, change on database structure change
if(!defined("ZKRIBERS_DB_VERSION")) define("ZKRIBERS_DB_VERSION", 2);
// Activities for cron jobb
require_once(dirname(__FILE__) . '/includes/send_mail.php');
// Add plugin activation and deactivation hook
register_activation_hook( __FILE__, 'zkribers_activate' );
register_deactivation_hook (__FILE__, 'zkribers_deactivate');
// Define the widget
add_action( 'widgets_init', 'zkribers_load_widget' );
if (is_admin()) {
// Add plugin to Admin panel
require_once(dirname(__FILE__) . '/includes/admin-functions.php');
add_action('admin_menu', 'setup_zkribers_admin_page');
}
/**
* Initial setup when plugin is activated
*
*/
function zkribers_activate() {
$opt = get_option('zkribers_options');
if (!$opt) {
add_option( 'zkribers_options', array(), false );
$opt = array(
'zkribers_db_version' => ZKRIBERS_DB_VERSION,
'row_per_page' => 10,
'cron_schedule' => 60,
'post_type' => array('post','page'),
'last_send' => current_time('mysql'),
'send_interval' => '+1 day',
'hostname' => '',
'authentication' => '',
'port' => '587',
'login' => '',
'pwd' => '',
'protocol' => 'tls',
'email' => '',
'name' => ''
);
update_option( 'zkribers_options', $opt);
}
// Import functions for dbDelta()
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
// Check is databases exist or table need updating
$table_name = $wpdb->prefix . "zkribers_subscribers";
$db_try = $wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name;
if($db_try || $opt['zkribers_db_version'] != ZKRIBERS_DB_VERSION) {
$sql = "CREATE TABLE $table_name (
id tinyint NOT NULL AUTO_INCREMENT,
uuid tinytext NULL,
name tinytext NOT NULL,
email tinytext NOT NULL,
time datetime NOT NULL,
verified tinyint(1) NOT NULL,
purge_date datetime NULL,
UNIQUE KEY id (id)
) $charset_collate;";
dbDelta( $sql );
}
$table_name = $wpdb->prefix . "zkribers_templates";
$db_try = $wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name;
if($db_try || $opt['zkribers_db_version'] != ZKRIBERS_DB_VERSION) {
$sql = "CREATE TABLE $table_name (
id tinyint NOT NULL AUTO_INCREMENT,
slug char(10) NOT NULL,
title tinytext NOT NULL,
subject tinytext NOT NULL,
description tinytext NOT NULL,
template mediumtext NOT NULL,
active tinyint(1) NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
dbDelta( $sql );
$templates_dir = dirname(__FILE__,1) . '/includes/templates/' . strstr(get_locale(),'_',true);
if ($db_try) {
$wpdb->insert( $table_name,
array(
'title' => 'Verification template',
'description' => 'Template for opt-in verification before activating user',
'active' => true,
'subject' => base64_encode('Verify your subscription for #sitename#'),
'template' => base64_encode(file_get_contents($templates_dir.'/verification.php')),
'slug' => 'VT'),
array( '%s', '%s', '%d', '%s', '%s' ) );
$wpdb->insert( $table_name,
array(
'title' => 'Welcome template',
'description' => 'Welcome message sent when user subscribed and is verified',
'active' => true,
'subject' => base64_encode('Welcome to #sitename#'),
'template' => base64_encode(file_get_contents($templates_dir.'/welcome.php')),
'slug' => 'WT'),
array( '%s', '%s', '%d', '%s', '%s' ) );
$wpdb->insert( $table_name,
array(
'title' => 'Posts template',
'description' => 'Main template to inform about new posts that has been published',
'active' => true,
'subject' => base64_encode('#sitename# has #newposts# new posts'),
'template' => base64_encode(file_get_contents($templates_dir.'/posts.php')),
'slug' => 'PT'),
array( '%s', '%s', '%d', '%s', '%s' ) );
$wpdb->insert( $table_name,
array(
'title' => 'Unsubscribed template',
'description' => 'Template for sending a message after user has unsubscribed',
'active' => true,
'subject' => base64_encode('Unsubscribe confirmaion'),
'template' => base64_encode(file_get_contents($templates_dir.'/unsubscribed.php')),
'slug' => 'US'),
array( '%s', '%s', '%d', '%s', '%s' ) );
}
}
// Update database version in option if changed
if ($opt['zkribers_db_version'] != ZKRIBERS_DB_VERSION) {
$opt['zkribers_db_version'] = ZKRIBERS_DB_VERSION;
update_option( 'zkribers_options', $opt);
}
if (! wp_next_scheduled ( 'zkribers_cron_jobbs' )) {
wp_schedule_event(time(), 'hourly', 'zkribers_cron_jobbs');
}
}
/**
* Clean upp after ourself when deactivated
*
*/
function zkribers_deactivate() {
$timestamp = wp_next_scheduled( 'zkribers_cron_jobbs' );
wp_unschedule_event( $timestamp, 'zkribers_cron_jobbs' );
wp_clear_scheduled_hook('zkribers_cron_jobbs');
}
/**
* Register and load the widget
*
*/
function zkribers_load_widget() {
register_widget( 'zkribers_widget' );
}
/**
* Define the widget class
*
*/
class zkribers_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Widget base ID
'zkribers_widget',
// Widget name will appear in UI
'Zkribers',
// Widget description
array( 'description' => 'Widget to let users subscribe to you posts' )
);
}
// Creating widget front-end
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
$description = apply_filters( 'widget_title', $instance['description'] );
$submitbutton = apply_filters( 'widget_title', $instance['submit'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; }
if (isset($_POST['zkribers_submitmail'])) {
submit_email();
echo '<div style="font-size: 0.9em; text-align: center; "><em>Thank you!<br>Your e-mail has been added.</em></div>';
} else {
require_once('includes/bbcode.php');
echo '<div style="text-align: center; ">'.bbcode2html($description).'</div>'; ?>
<form method="post" style="margin-top: 15px;">
<input type="text" name="zkribers_name" placeholder="Your name" required style="text-align: center; background: rgba(0,0,0,0.1); color: black; width: 100%; border: none; outline:none; height:50px;"><br>
<input type="email" name="zkribers_email" placeholder="E-mail" required style="text-align: center; background: rgba(0,0,0,0.1); color: black; width: 100%; margin-top: 10px; margin-bottom: 10px; border: none; outline:none; height:50px;"><br>
<input type="submit" name="zkribers_submitmail" value="<?php echo $submitbutton;?>" style="width: 100%; height:50px; border:0px none; font-size: 90%; border-radius: 0px; text-transform:uppercase; font-weight: bold;">
</form>
<?php }
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
$title = ( isset( $instance[ 'title' ] ) ) ? $instance[ 'title' ] : 'Subscribe';
$description = ( isset( $instance[ 'description' ] ) ) ? $instance[ 'description' ] : 'Enter your e-mail to subscribe';
$submitbutton = ( isset( $instance[ 'submit' ] ) ) ? $instance[ 'submit' ] : 'Sign up now!';
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
<label for="<?php echo $this->get_field_id( 'description' ); ?>"><?php _e( 'Description:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'description' ); ?>" name="<?php echo $this->get_field_name( 'description' ); ?>" type="text" value="<?php echo esc_attr( $description ); ?>" />
<label for="<?php echo $this->get_field_id( 'submit' ); ?>"><?php _e( 'Submit button:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'submit' ); ?>" name="<?php echo $this->get_field_name( 'submit' ); ?>" type="text" value="<?php echo esc_attr( $submitbutton ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['description'] = ( ! empty( $new_instance['description'] ) ) ? strip_tags( $new_instance['description'] ) : '';
$instance['submit'] = ( ! empty( $new_instance['submit'] ) ) ? strip_tags( $new_instance['submit'] ) : '';
return $instance;
}
}
/**
* Send out welcome or verify e-mail when new user subscribed
*
*/
function submit_email() {
global $wpdb;
$sql = "SELECT active FROM {$wpdb->prefix}zkribers_templates WHERE slug='VT'";
$query = $wpdb->get_results($sql, 'ARRAY_A');
$verify = ($query[0]['active'] == 1) ? 'VT' : 'WT';
$table_name = $wpdb->prefix . 'zkribers_subscribers';
$s_name = sanitize_text_field($_POST['zkribers_name']);
$s_mail = sanitize_email($_POST['zkribers_email']);
$wpdb->insert( $table_name,
array(
'name' => $s_name,
'email' => $s_mail,
'time' => current_time( 'mysql' ),
'verified' => ($verify=='WT') ? 2 : 1,
'purge_date' => ($verify == 'WT') ? NULL : date('Y-m-d H:i:s',strtotime("+1 week", current_time('timestamp')))),
array( '%s', '%s', '%s', '%d') );
zkribers_sendmail( array(array('name' => $s_name, 'email' => $s_mail)), $verify);
}