-
Notifications
You must be signed in to change notification settings - Fork 1
/
wpfeather.php
368 lines (322 loc) · 8.37 KB
/
wpfeather.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
/**
* Plugin Name: WPFeather - Floating Contact Form for WordPress
* Plugin URI: https://wordpress.org/plugins/wpfeather/
* Description: A very lightweight floating contact form for WordPress with built-in spam protection & Cloudflare Turnstile integration
* Version: 1.0.0
* Author: weDevs
* Author URI: https://wedevs.com/
* Text Domain: wpfeather
* Domain Path: /languages/
* License: GPL2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
/*
* Copyright (c) 2019 weDevs (email: [email protected]). All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* **********************************************************************
*/
// don't call the file directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
exit;
}
require_once __DIR__ . '/vendor/autoload.php';
// include background process file
require_once __DIR__ . '/vendor/woocommerce/action-scheduler/action-scheduler.php';
/**
* WPfeather class
*
* @class WPfeather The class that holds the entire plugin
*/
final class WPfeather {
/**
* Plugin version
*
* @var string
*
* @since 1.0.0
*/
const VERSION = '1.0.0';
/**
* Holds various class instances.
*
* @var array
*
* @since 1.0.0
*/
private $container = [];
/**
* Constructor for the class.
*
* Sets up all the appropriate hooks and actions
* within our plugin.
*
* @since 1.0.0
*/
private function __construct() {
$this->define_constants();
register_activation_hook( __FILE__, [ $this, 'activate' ] );
register_deactivation_hook( __FILE__, [ $this, 'deactivate' ] );
add_action( 'plugins_loaded', [ $this, 'init_plugin' ] );
add_action( 'wpfeather_mail_frontend_form_submission', [ $this, 'send_mail' ], 10, 3 );
}
/**
* Initializes the WPfeather() class.
*
* Checks for an existing WPfeather() instance
* and if it doesn't find one, creates it.
*
* @since 0.1
*
* @return WPfeather|bool
*/
public static function init() {
static $instance = false;
if ( ! $instance ) {
$instance = new WPfeather();
}
return $instance;
}
/**
* Magic getter to bypass referencing plugin.
*
* @param $prop
*
* @since 0.1
*
* @return mixed
*/
public function __get( $prop ) {
if ( array_key_exists( $prop, $this->container ) ) {
return $this->container[ $prop ];
}
return $this->{$prop};
}
/**
* Magic isset to bypass referencing plugin.
*
* @param $prop
*
* @since 0.1
*
* @return mixed
*/
public function __isset( $prop ) {
return isset( $this->{$prop} ) || isset( $this->container[ $prop ] );
}
/**
* Define the constants.
*
* @since 0.1
*
* @return void
*/
public function define_constants() {
$this->define( 'WPFEATHER_VERSION', self::VERSION );
$this->define( 'WPFEATHER_FILE', __FILE__ );
$this->define( 'WPFEATHER_PATH', dirname( WPFEATHER_FILE ) );
$this->define( 'WPFEATHER_INCLUDES', WPFEATHER_PATH . '/includes' );
$this->define( 'WPFEATHER_URL', plugins_url( '', WPFEATHER_FILE ) );
$this->define( 'WPFEATHER_ASSETS', WPFEATHER_URL . '/assets' );
$this->define( 'WPFEATHER_BASE_NAME', plugin_basename( __FILE__ ) );
$options = get_option( 'wpfeather_options' );
// define the ajax key suffix. it will append after the ajax action name
if ( ! empty( $options['secret_keys'] ) && ! empty( $options['secret_keys'][0] ) ) {
$this->define( 'WPFEATHER_AJAX_KEY', sanitize_key( $options['secret_keys'][0] ) );
} else {
$this->define( 'WPFEATHER_AJAX_KEY', '' );
}
}
/**
* Define constant if not already set.
*
* @since 1.0.0
*
* @param string $name Constant name.
* @param string|bool $value Constant value.
*/
private function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* Load the plugin after all plugis are loaded.
*
* @since 1.0.0
*
* @return void
*/
public function init_plugin() {
$this->init_hooks();
}
/**
* Placeholder for activation function.
*
* Nothing being called here yet.
*
* @since 0.1
*
* @return void
*/
public function activate() {
$installer = new WeDevs\WpFeather\Installer();
$installer->run();
}
/**
* Placeholder for deactivation function.
*
* Nothing being called here yet.
*
* @since 0.1
*/
public function deactivate() {}
/**
* Initialize the hooks.
*
* @since 0.1
*
* @return void
*/
public function init_hooks() {
add_action( 'after_setup_theme', [ $this, 'init_classes' ] );
// Localize our plugin
add_action( 'init', [ $this, 'localization_setup' ] );
}
/**
* Instantiate the required classes.
*
* @since 0.1
*
* @return void
*/
public function init_classes() {
if ( $this->is_request( 'admin' ) ) {
$this->container['admin'] = new WeDevs\WpFeather\Admin();
}
if ( $this->is_request( 'frontend' ) ) {
$this->container['frontend'] = new WeDevs\WpFeather\Frontend();
}
if ( $this->is_request( 'ajax' ) ) {
$this->container['ajax'] = new WeDevs\WpFeather\Ajax();
}
}
/**
* Initialize plugin for localization.
*
* @uses load_plugin_textdomain()
*
* @since 0.1
*
* @return void
*/
public function localization_setup() {
load_plugin_textdomain( 'wpfeather', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* What type of request is this?
*
* @param string $type admin, ajax, cron or frontend
*
* @since 0.1
*
* @return bool
*/
private function is_request( $type ) {
switch ( $type ) {
case 'admin':
return is_admin();
case 'ajax':
return defined( 'DOING_AJAX' );
case 'rest':
return defined( 'REST_REQUEST' );
case 'cron':
return defined( 'DOING_CRON' );
case 'frontend':
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
}
}
/**
* Send frontend form submitted data to a mail
*
* @param $name
* @param $email
* @param $message
*
* @return void
*/
public function send_mail( $name, $email, $message ) {
$email_to = wpfeather_get_option( 'recipient', 'wpfeather_settings' );
$email_subject = sprintf( esc_html__( 'Message from %s', 'wpfeather' ), get_bloginfo( 'name' ) );
// no email recipient setup yet. we will mail the site admin as the fallback
if ( empty( $email_to ) ) {
$email_to = get_bloginfo( 'admin_email' );
}
/* translators: Do not translate USERNAME, URL_DELETE, SITENAME, SITEURL: those are placeholders. */
$mail_body = __(
"Howdy,
Someone submitted a form through WPFeather from ###SITEURL###. The form details below:<br>
<b>Name:</b> $name<br>
<b>e-mail:</b> $email<br>
<b>Message:</b> $message<br>
Thanks for using WPFeather"
);
/**
* Filters the text for the email sent when WPFeather frontend form is submitted.
*
* @since 1.0.0
*
* @param string $mail_body The email text.
*/
$mail_body = apply_filters( 'wpfeather_frontend_form_email_content', $mail_body );
$mail_body = str_replace( '###SITEURL###', network_home_url(), $mail_body );
$headers = [
'Content-Type: text/html; charset=UTF-8',
'From: "' . esc_attr( get_bloginfo( 'name' ) ) . '" <' . sanitize_email( $email_to ) . '>',
'Reply-To: <'.sanitize_email( $email ).'>'
];
$result = wp_mail( $email_to, $email_subject, $mail_body, $headers );
if ( ! $result ) {
error_log( '[WPFeather]: cannot send mail to admin.' );
}
}
}
/**
* Initialize the main plugin.
*
* @since 0.1
*
* @return \WPfeather
*/
function wpfeather() {
return WPfeather::init();
}
/*
* Kick-off the plugin.
*
* @since 0.1
*/
wpfeather();