-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdisciple-tools-webform.php
423 lines (372 loc) · 14.1 KB
/
disciple-tools-webform.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
<?php
/**
*Plugin Name: Disciple.Tools - Webform
* Plugin URI: https://github.com/DiscipleTools/disciple-tools-webform
* Description: Build forms for you websites. Submitted forms will create contacts in Disciple.Tools.
* Version: 6.6.2
* Text Domain: disciple-tools-webform
* Domain Path: /languages
* Author name: Disciple.Tools
* Author URI: https://github.com/DiscipleTools
* GitHub Plugin URI: https://github.com/DiscipleTools/disciple-tools-webform
* Requires at least: 4.7.0
* (Requires 4.7+ because of the integration of the REST API at 4.7 and the security requirements of this milestone version.)
* Tested up to: 5.6
*
* @package Disciple_Tools
* @link https://github.com/DiscipleTools
* @license GPL-2.0 or later
* https://www.gnu.org/licenses/gpl-2.0.html
*
* @version 4.5 Bug fixes
* @version 4.5.1 Minor updated to allow html in descriptions and headers.
*/
if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly
use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
$dt_webform_required_dt_theme_version = '1.34.1';
require_once( 'includes/functions.php' );
require_once( 'includes/shortcodes.php' );
/*******************************************************************************************************************
* MIGRATION ENGINE
******************************************************************************************************************/
require_once( 'includes/class-migration-engine.php' );
try {
DT_Webform_Migration_Engine::migrate( DT_Webform_Migration_Engine::$migration_number );
} catch ( Throwable $e ) {
$migration_error = new WP_Error( 'migration_error', 'Migration engine for webform failed to migrate.', [ 'error' => $e ] );
if ( function_exists( 'dt_write_log' ) ) {
dt_write_log( maybe_serialize( $migration_error ) );
} else {
error_log( 'Migration engine for webform failed to migrate.' );
}
}
/*******************************************************************************************************************/
/**
* Gets the instance of the `DT_Webform` class. This function is useful for quickly grabbing data
* used throughout the plugin.
*
* @since 0.1
* @access public
* @return object|bool
*/
function dt_webform() {
global $dt_webform_required_dt_theme_version;
if ( is_this_dt() ) {
$wp_theme = wp_get_theme();
$version = $wp_theme->version;
if ( $version < $dt_webform_required_dt_theme_version ) {
if ( ! is_multisite() ) {
add_action( 'admin_notices', 'dt_webform_no_disciple_tools_theme_found' );
add_action( 'wp_ajax_dismissed_notice_handler', 'dt_hook_ajax_notice_handler' );
}
return new WP_Error( 'current_theme_not_dt', 'Please upgrade Disciple.Tools Theme to ' . $dt_webform_required_dt_theme_version . ' to use this plugin.' );
}
}
$is_rest = dt_is_rest();
if ( $is_rest && ( ( strpos( dt_get_url_path(), 'webform' ) !== false ) || ( strpos( dt_get_url_path(), 'site_link_check' ) !== false ) || ( strpos( dt_get_url_path(), 'site_link_server_check' ) !== false ) || ( strpos( dt_get_url_path(), 'server_ip' ) !== false ) ) ) {
return DT_Webform::get_instance();
}
if ( is_admin() ) {
return DT_Webform::get_instance();
}
if ( function_exists( 'dt_get_url_path' ) && strpos( dt_get_url_path(), 'webform/ml' ) !== false ) {
return DT_Webform::get_instance();
}
return false;
}
add_action( 'after_setup_theme', 'dt_webform' );
/**
* Singleton class for setting up the plugin.
*
* @since 0.1
* @access public
*/
class DT_Webform {
/**
* Declares public variables
*
* @since 0.1
* @access public
* @return object
*/
public static $token = 'dt_webform';
public $version;
public $dir_path;
public $dir_uri;
public $assets_uri;
public $img_uri;
public $js_uri;
public $css_uri;
public $includes_path;
public $admin_path;
public $home_path;
public $remote_path;
public $assets_path;
public $public_uri;
public $includes_uri;
/**
* Returns the instance.
*
* @since 0.1
* @access public
* @return object
*/
public static function get_instance() {
static $instance = null;
if ( is_null( $instance ) ) {
$instance = new DT_Webform();
$instance->setup();
$instance->setup_actions();
}
return $instance;
}
/**
* Sets up globals.
*
* @since 0.1
* @access public
* @return void
*/
private function setup() {
// Main plugin directory path and URI.
$this->dir_path = trailingslashit( plugin_dir_path( __FILE__ ) );
$this->dir_uri = trailingslashit( plugin_dir_url( __FILE__ ) );
// Plugin directory paths.
$this->includes_path = trailingslashit( $this->dir_path . 'includes' );
$this->admin_path = trailingslashit( $this->includes_path . 'admin' );
$this->home_path = trailingslashit( $this->includes_path . 'home' );
$this->remote_path = trailingslashit( $this->includes_path . 'remote' );
$this->assets_path = trailingslashit( $this->includes_path . 'assets' );
// Plugin directory URIs.
$this->includes_uri = trailingslashit( $this->dir_uri . 'includes' );
$this->assets_uri = trailingslashit( $this->dir_uri . 'includes/assets' );
$this->public_uri = trailingslashit( $this->dir_uri . 'public' );
$this->js_uri = trailingslashit( $this->assets_uri . 'js' );
$this->css_uri = trailingslashit( $this->assets_uri . 'css' );
// Admin and settings variables
$this->version = '4.0';
// Not Disciple.Tools : remote support files
if ( ! is_this_dt() ) {
require_once( 'dt-mapping/geocode-api/mapbox-api.php' );
if ( !class_exists( 'Site_Link_System' ) ){
require_once( 'includes/site-link-post-type.php' );
Site_Link_System::instance();
}
add_action( 'init', [ $this, 'dt_set_permalink_structure' ] );
add_action( 'update_option_permalink_structure', [ $this, 'dt_permalink_structure_changed_callback' ] );
}
require_once( 'includes/create-contact.php' );
require_once( 'includes/utilities.php' );
require_once( 'includes/post-type-active-forms.php' );
if ( is_this_dt() ){
require_once( 'magic-link/magic-link-webform-app.php' );
}
// Admin area
if ( is_admin() ) {
// Admin and tabs menu
require_once( 'includes/tables.php' );
require_once( 'includes/menu-and-tabs.php' ); // main wp-admin menu and ui
}
}
/**
* Sets up main plugin actions and filters.
*
* @since 0.1
* @access public
* @return void
*/
private function setup_actions() {
// Internationalize the text strings used.
add_action( 'plugins_loaded', array( $this, 'i18n' ), 2 );
if ( is_admin() ) {
// adds links to the plugin description area in the plugin admin list.
add_filter( 'plugin_row_meta', [ $this, 'plugin_description_links' ], 10, 4 );
}
}
/**
* Filters the array of row meta for each/specific plugin in the Plugins list table.
* Appends additional links below each/specific plugin on the plugins page.
*
* @access public
* @param array $links_array An array of the plugin's metadata
* @param string $plugin_file_name Path to the plugin file
* @param array $plugin_data An array of plugin data
* @param string $status Status of the plugin
* @return array $links_array
*/
public function plugin_description_links( $links_array, $plugin_file_name, $plugin_data, $status ) {
if ( strpos( $plugin_file_name, basename( __FILE__ ) ) ) {
// You can still use `array_unshift()` to add links at the beginning.
$links_array[] = '<a href="https://github.com/DiscipleTools/disciple-tools-webform">Github</a>';
$links_array[] = '<a href="https://www.youtube.com/watch?v=lBOwgrOSUkU">Video Tutorial</a>';
// add other links here
}
return $links_array;
}
/**
* Method that runs only when the plugin is activated.
*
* @since 0.1
* @access public
* @return void
*/
public static function activation() {
// Confirm 'Administrator' has 'manage_dt' privilege. This is key in 'remote' configuration when Disciple.Tools theme is not installed.
$role = get_role( 'administrator' );
if ( !empty( $role ) ) {
$role->add_cap( 'manage_dt' ); // gives access to dt plugin options
}
}
/**
* Method that runs only when the plugin is deactivated.
*
* @since 0.1
* @access public
* @return void
*/
public static function deactivation() {
delete_option( 'dt_webform_options' );
delete_option( 'dt_webform_state' );
delete_option( 'external_updates-disciple-tools-webform' );
}
public function dt_warn_user_about_permalink_settings() {
?>
<div class="error notices">
<p>You may only set your permalink settings to "Post name"'</p>
</div>
<?php
}
/**
* Notification that 'posttype' is the only permalink structure available.
*
* @param $permalink_structure
*/
public function dt_permalink_structure_changed_callback( $permalink_structure ) {
global $wp_rewrite;
if ( $permalink_structure !== '/%postname%/' ) {
add_action( 'admin_notices', [ $this, 'dt_warn_user_about_permalink_settings' ] );
}
}
public function dt_set_permalink_structure() {
global $wp_rewrite;
$wp_rewrite->set_permalink_structure( '/%postname%/' );
flush_rewrite_rules();
}
/**
* Constructor method.
*
* @since 0.1
* @access private
* @return void
*/
private function __construct() {
}
/**
* Loads the translation files.
*
* @since 0.1
* @access public
* @return void
*/
public function i18n() {
load_plugin_textdomain( 'dt_webform', false, trailingslashit( dirname( plugin_basename( __FILE__ ) ) ). 'languages' );
}
/**
* Magic method to output a string if trying to use the object as a string.
*
* @since 0.1
* @access public
* @return string
*/
public function __toString() {
return 'dt_webform';
}
/**
* Magic method to keep the object from being cloned.
*
* @since 0.1
* @access public
* @return void
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Whoah, partner!', 'dt_webform' ), '0.1' );
}
/**
* Magic method to keep the object from being unserialized.
*
* @since 0.1
* @access public
* @return void
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Whoah, partner!', 'dt_webform' ), '0.1' );
}
/**
* Magic method to prevent a fatal error when calling a method that doesn't exist.
*
* @since 0.1
* @access public
* @return null
*/
public function __call( $method = '', $args = array() ) {
// @codingStandardsIgnoreLine
_doing_it_wrong( "dt_webform::{$method}", esc_html__( 'Method does not exist.', 'dt_webform' ), '0.1' );
unset( $method, $args );
return null;
}
/**
* @return string
*/
public static function get_real_ip_address() {
$ip = '';
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) //check ip from share internet
{
// @codingStandardsIgnoreLine
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) //to check ip is pass from proxy
{
// @codingStandardsIgnoreLine
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
elseif ( ! empty( $_SERVER['REMOTE_ADDR'] ) )
{
// @codingStandardsIgnoreLine
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
}
// End of main class
// Activation and De-activation Hooks
register_activation_hook( __FILE__, [ 'DT_Webform', 'activation' ] );
register_deactivation_hook( __FILE__, [ 'DT_Webform', 'deactivation' ] );
/**
* Check for plugin updates even when the active theme is not Disciple.Tools
*
* Below is the publicly hosted .json file that carries the version information. This file can be hosted
* anywhere as long as it is publicly accessible. You can download the version file listed below and use it as
* a template.
* Also, see the instructions for version updating to understand the steps involved.
* @see https://github.com/DiscipleTools/disciple-tools-version-control/wiki/How-to-Update-the-Starter-Plugin
*/
add_action( 'plugins_loaded', function (){
if ( is_admin() && !( is_multisite() && class_exists( 'DT_Multisite' ) ) || wp_doing_cron() ){
// Check for plugin updates
if ( !class_exists( '\YahnisElsts\PluginUpdateChecker\v5\PucFactory' ) ) {
$dir_path = trailingslashit( plugin_dir_path( __FILE__ ) );
$includes_path = trailingslashit( $dir_path . 'includes' );
$admin_path = trailingslashit( $includes_path . 'admin' );
if ( file_exists( $admin_path . 'libraries/plugin-update-checker/plugin-update-checker.php' ) ){
require( $admin_path . 'libraries/plugin-update-checker/plugin-update-checker.php' );
}
}
if ( class_exists( '\YahnisElsts\PluginUpdateChecker\v5\PucFactory' ) ){
PucFactory::buildUpdateChecker(
'https://raw.githubusercontent.com/DiscipleTools/disciple-tools-webform/master/version-control.json',
__FILE__,
'disciple-tools-webform'
);
}
}
} );