-
Notifications
You must be signed in to change notification settings - Fork 4
/
fmpress-forms.php
503 lines (434 loc) · 11.4 KB
/
fmpress-forms.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<?php
/**
* Plugin Name: FMPress Forms
* Plugin URI: https://www.emic.co.jp/products/
* Description: Addon for Contact Form 7.
* Version: 1.3.1
* Author: Emic Corporation
* Author URI: https://www.emic.co.jp/
* License: GPLv2 or later
* Text Domain: fmpress-forms
*
* @package WordPress
*/
namespace Emic\FMPress\Forms;
defined( 'ABSPATH' ) || die( 'Access denied.' );
define( 'FMPRESS_FORMS_PLUGIN_DIR', untrailingslashit( dirname( __FILE__ ) ) );
define( 'FMPRESS_FORMS_CF7_SETTINGS_KEY', 'fmpress_connect_settings_data' );
use Emic\FMPress\Connect as Core;
require_once ABSPATH . 'wp-admin/includes/plugin.php';
/**
* FMPress Forms
*
* @since 1.0.0
*/
final class FMPress_Forms {
/**
* Name of this plugin
*
* @since 1.0.0
* @var string
*/
const PLUGIN_NAME = 'FMPress Forms';
/**
* Version of this plugin
*
* @since 1.0.0
* @var string
*/
const VERSION = '1.3.1';
/**
* Minimum Version of PHP
*
* @since 1.0.0
* @var string
*/
const MINIMUM_PHP_VERSION = '7.4.0';
/**
* Minimum Version of Contact Form 7
*
* @since 1.0.0
* @var string
*/
const MINIMUM_CF7_VERSION = '5.5';
/**
* Special Mail Tags of Contact Form 7
*
* @since 1.2.0
* @var array
*/
const CF7_SPECAIL_MAIL_TAGS = array(
'_remote_ip',
'_user_agent',
);
/**
* Class constructor
*
* @since 1.0.0
* @access public
*/
public function __construct() {
add_action( 'init', array( $this, 'fmpress_load_plugin_textdomain' ) );
add_action( 'plugins_loaded', array( $this, 'fmpress_plugin_init' ) );
add_action( 'admin_notices', array( $this, 'is_plugin_requirements' ) );
add_filter( 'script_loader_tag', array( $this, 'fmpress_load_js_as_module' ), 10, 3 );
}
/**
* Load Text Domain
*
* @since 1.0.0
* @access public
*/
public function fmpress_load_plugin_textdomain() {
load_plugin_textdomain(
'fmpress-forms',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages'
);
}
/**
* Initialize
*
* @since 1.0.0
* @access public
*/
public function fmpress_plugin_init() {
if ( ! did_action( 'fmpress_forms_pro_loaded' ) &&
( is_plugin_active( 'fmpress-connect/fmpress-connect.php' ) || is_plugin_active( 'fmpress-pro/fmpress-connect.php' ) ) ) {
// Exit if FMPress Pro and FMPress Forms are activated.
return;
}
// Add a link to the settings on the plugin page.
add_filter(
'plugin_action_links_' . plugin_basename( __FILE__ ),
array( $this, 'fmpress_add_link_to_settings' )
);
if ( did_action( 'fmpress_forms_loaded' ) || did_action( 'fmpress_forms_pro_loaded' ) ) {
// Require plugin files.
$this->fmpress_require_files();
// Create class instances.
new Admin();
new AdminAjax();
new Forms();
}
if ( did_action( 'fmpress_forms_loaded' ) ) {
new Core\Datasources();
}
// Enqueue files.
$this->fmpress_enqueue_files();
}
/**
* Check if the plugin meets the specifications
*
* @since 1.0.0
* @access public
*
* @return bool
*/
public function is_plugin_requirements() {
$errors = array();
// Admin notice for minimum PHP version.
$errors[] = $this->is_require_php_version( self::MINIMUM_PHP_VERSION );
// Admin notice for check for the existence of constants.
$errors[] = $this->is_defined_encrypt_key();
// Admin notice for check for the existence of constants.
$errors[] = $this->is_defined_encrypt_iv();
// Check if CF7 installed and activated.
$errors[] = $this->is_require_cf7_version( self::MINIMUM_CF7_VERSION );
// Exit if FMPress Forms Pro is activated.
$errors[] = $this->is_activated_fmpress_forms();
// Check if FMPress Pro installed and activated.
$errors[] = $this->is_not_activated_fmpress_core();
// Exit if FMPress Pro is activated.
$errors[] = $this->is_activated_fmpress_core();
if ( in_array( false, $errors, true ) ) {
$this->fmpress_deactivate_plugin();
return false;
}
return true;
}
/**
* Add a link to the settings on the plugin page
*
* @since 1.0.0
* @access public
* @param string $links .
* @return string
*/
public function fmpress_add_link_to_settings( $links ) {
$links[] = '<a href="' .
admin_url( 'admin.php?page=wpcf7' ) .
'">' . __( 'Settings' ) . '</a>';
return $links;
}
/**
* Require plugin files
*
* @since 1.0.0
* @access private
*/
private function fmpress_require_files() {
require_once FMPRESS_FORMS_PLUGIN_DIR . '/admin/class-admin.php';
require_once FMPRESS_FORMS_PLUGIN_DIR . '/admin/class-admin-ajax.php';
require_once FMPRESS_FORMS_PLUGIN_DIR . '/includes/class-forms.php';
require_once FMPRESS_FORMS_PLUGIN_DIR . '/includes/class-fm-value-list.php';
if ( did_action( 'fmpress_forms_loaded' ) ) {
// For FMPress Forms.
define( 'FMPRESS_CONNECT_NAMEPREFIX', 'fmpress_connect' );
require_once FMPRESS_FORMS_PLUGIN_DIR . '/admin/class-datasources.php';
require_once FMPRESS_FORMS_PLUGIN_DIR . '/admin/class-settings.php';
require_once FMPRESS_FORMS_PLUGIN_DIR . '/includes/class-utils.php';
require_once FMPRESS_FORMS_PLUGIN_DIR . '/includes/drivers/class-database.php';
require_once FMPRESS_FORMS_PLUGIN_DIR . '/includes/drivers/class-fmdapi.php';
} elseif ( did_action( 'fmpress_forms_pro_loaded' ) ) {
// For FMPress Forms Pro.
require_once FMPRESS_FORMS_PLUGIN_DIR . '/includes/class-update-form.php';
}
}
/**
* Enqueue files
*
* @since 1.0.0
* @access private
*/
private function fmpress_enqueue_files() {
// Loading admin CSS.
add_action(
'admin_menu',
function() {
wp_enqueue_style(
'fmpress-connect-admin',
plugins_url( '/admin/css/admin.css', __FILE__ ),
array(),
gmdate( 'U' )
);
}
);
// Loading admin JS (in header).
add_action(
'admin_enqueue_scripts',
function() {
if ( did_action( 'fmpress_forms_loaded' ) ) {
wp_enqueue_script(
'fmpress-connect-admin',
plugins_url( '/admin/js/admin.min.js', __FILE__ ),
array(),
gmdate( 'U' ),
false
);
}
wp_enqueue_script(
'module-fmpress-form-editor',
plugins_url( '/admin/js/form-editor/form-editor.js', __FILE__ ),
array( 'fmpress-connect-admin' ),
gmdate( 'U' ),
false
);
wp_localize_script(
'fmpress-connect-admin',
'localize',
array(
'fmpressAjaxNonce' => wp_create_nonce( 'fmpress_ajax_nonce' ),
)
);
}
);
}
/**
* Deactivate the plugin
*
* @since 1.0.0
* @access public
*/
public function fmpress_deactivate_plugin() {
$this->unset_get_parameter( 'activate' );
deactivate_plugins( plugin_basename( __FILE__ ) );
}
/**
* Remove value from GET parameter
*
* @since 1.0.0
* @access private
* @param string $key parameter name of $_GET.
*/
private function unset_get_parameter( $key ) {
// phpcs:disable WordPress.Security.NonceVerification
if ( isset( $_GET[ $key ] ) ) {
unset( $_GET[ $key ] );
}
// phpcs:enable
}
/**
* Show notice
* Can not using html tag in message
*
* @since 1.0.0
* @access private
* @param string $message .
*/
private static function fmpress_show_notice( $message ) {
if ( current_action() === 'admin_notices' ) {
echo '<div class="notice notice-warning is-dismissible"><p>',
esc_html( $message ),
'</p></div>', PHP_EOL;
}
}
/**
* Admin notice for minimum PHP version
*
* @param string $php_version .
* @return bool
*/
public function is_require_php_version( $php_version ) {
if ( version_compare( PHP_VERSION, $php_version, '<' ) ) {
$message = sprintf(
/* translators: 1: Plugin name 2: PHP 3: Required PHP version */
__( '"%1$s" requires "%2$s" version %3$s or greater.', 'fmpress-forms' ),
self::PLUGIN_NAME,
'PHP',
$php_version
);
self::fmpress_show_notice( $message );
return false;
}
return true;
}
/**
* Admin notice for check for the existence of constants
*
* @return bool
*/
public function is_defined_encrypt_key() {
$name = 'FMPRESS_CONNECT_ENCRYPT_KEY';
if ( ! defined( $name ) ) {
$value = wp_generate_password( 32, true, false );
$message = sprintf(
/* translators: 1: constant 2: value */
__( 'Requires %1$s in "%2$s" file.', 'fmpress-forms' ),
'define( \'' . $name . '\', \'' . $value . '\' );',
'wp-config.php'
);
self::fmpress_show_notice( $message );
return false;
}
return true;
}
/**
* Admin notice for check for the existence of constants
*
* @return bool
*/
public function is_defined_encrypt_iv() {
$name = 'FMPRESS_CONNECT_ENCRYPT_IV';
if ( ! defined( $name ) ) {
$value = bin2hex(
openssl_random_pseudo_bytes(
openssl_cipher_iv_length( 'aes-256-gcm' )
)
);
$message = sprintf(
/* translators: 1: constant 2: value */
__( 'Requires %1$s in "%2$s" file.', 'fmpress-forms' ),
'define( \'' . $name . '\', \'' . $value . '\' );',
'wp-config.php'
);
self::fmpress_show_notice( $message );
return false;
}
return true;
}
/**
* Check if CF7 installed and activated
*
* @param string $cf7_version .
* @return bool
*/
public function is_require_cf7_version( $cf7_version ) {
if ( ! defined( 'WPCF7_VERSION' ) ||
version_compare( WPCF7_VERSION, $cf7_version, '<' ) ) {
$message = sprintf(
/* translators: 1: Plugin name 2: PHP 3: Required PHP version */
__( '"%1$s" requires "%2$s" version %3$s or greater.', 'fmpress-forms' ),
self::PLUGIN_NAME,
'Contact Form 7',
$cf7_version
);
self::fmpress_show_notice( $message );
return false;
}
return true;
}
/**
* Exit if FMPress Forms Pro is activated
*
* @return bool
*/
public static function is_activated_fmpress_forms() {
if ( did_action( 'fmpress_forms_loaded' ) && did_action( 'fmpress_forms_pro_loaded' ) ) {
$message = sprintf(
/* translators: 1: Plugin name 2: FMPress Pro */
__( '"%1$s" and "%2$s" cannot be activated simultaneously.', 'fmpress-forms' ),
'FMPress Forms',
self::PLUGIN_NAME
);
self::fmpress_show_notice( $message );
return false;
}
return true;
}
/**
* Check if FMPress Pro installed and activated
*
* @return bool
*/
public function is_not_activated_fmpress_core() {
if ( did_action( 'fmpress_forms_pro_loaded' ) && ! did_action( 'fmpress_connect_loaded' ) ) {
$message = sprintf(
/* translators: 1: Plugin name 2: FMPress Pro */
__( '"%1$s" requires "%2$s" to be installed and activated.', 'fmpress-forms' ),
self::PLUGIN_NAME,
'FMPress Pro'
);
self::fmpress_show_notice( $message );
return false;
}
return true;
}
/**
* Exit if FMPress Pro is activated
*
* @return bool
*/
public static function is_activated_fmpress_core() {
if ( did_action( 'fmpress_forms_loaded' ) && did_action( 'fmpress_connect_loaded' ) ) {
$message = sprintf(
/* translators: 1: Plugin name 2: FMPress Pro */
__( '"%1$s" and "%2$s" cannot be activated simultaneously.', 'fmpress-forms' ),
'FMPress Forms',
'FMPress Pro'
);
self::fmpress_show_notice( $message );
return false;
}
return true;
}
/**
* Handle JavaScript files as module
*
* @since 1.3.0
* @param string $tag .
* @param string $handle .
* @param string $source .
* @return string
*/
public function fmpress_load_js_as_module( $tag, $handle, $source ) {
if ( 0 === strpos( $handle, 'module-' ) ) {
// phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript
$tag = '<script type="module" src="' . esc_url( $source ) . '"></script>' . PHP_EOL;
// phpcs:enable
}
return $tag;
}
}
do_action( 'fmpress_forms_loaded' );
new FMPress_Forms();