-
Notifications
You must be signed in to change notification settings - Fork 46
/
mw-wp-form.php
265 lines (237 loc) · 7.88 KB
/
mw-wp-form.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
<?php
/**
* Plugin Name: MW WP Form
* Plugin URI: https://mw-wp-form.web-soudan.co.jp
* Description: MW WP Form is shortcode base contact form plugin. This plugin have many features. For example you can use many validation rules, inquiry data saving, and chart aggregation using saved inquiry data.
* Version: 5.0.1
* Requires PHP: 7.4
* Author: websoudan
* Author URI: https://web-soudan.co.jp/
* Original Author: inc2734
* Original Author URI: https://2inc.org
* Text Domain: mw-wp-form
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* @package mw-wp-form
* @author websoudan
* @license GPL-2.0+
*/
/**
* Include files.
*/
include_once( plugin_dir_path( __FILE__ ) . 'classes/functions.php' );
include_once( plugin_dir_path( __FILE__ ) . 'classes/config.php' );
include_once( plugin_dir_path( __FILE__ ) . 'classes/deprecated.php' );
class MW_WP_Form {
/**
* Constructor.
*/
public function __construct() {
add_action( 'plugins_loaded', array( $this, '_load_initialize_files' ), 9 );
add_action( 'plugins_loaded', array( $this, '_initialize' ), 11 );
register_uninstall_hook( __FILE__, array( __CLASS__, '_uninstall' ) );
}
/**
* Load classes.
*/
public function _load_initialize_files() {
$plugin_dir_path = plugin_dir_path( __FILE__ );
$includes = array(
'/classes/abstract',
'/classes/controllers',
'/classes/models',
'/classes/services',
'/classes/validation-rules',
'/classes/form-fields',
);
foreach ( $includes as $include ) {
foreach ( glob( $plugin_dir_path . $include . '/*.php' ) as $file ) {
require_once( $file );
}
}
}
/**
* Load text domain, The starting point of the process.
*/
public function _initialize() {
load_plugin_textdomain( 'mw-wp-form' );
MW_WP_Form_Csrf::save_token();
add_action( 'after_setup_theme', array( $this, '_after_setup_theme' ), 11 );
add_action( 'init', array( $this, '_register_post_type' ) );
add_action( 'template_redirect', array( $this, '_do_empty_temp_dir' ) );
}
/**
* Initialize each screens.
*/
public function _after_setup_theme() {
if ( current_user_can( MWF_Config::CAPABILITY ) && is_admin() ) {
add_action( 'admin_enqueue_scripts', array( $this, '_admin_enqueue_scripts' ) );
add_action( 'admin_menu', array( $this, '_admin_menu_for_chart' ) );
add_action( 'admin_menu', array( $this, '_admin_menu_for_inquiry_data_list' ) );
add_action( 'current_screen', array( $this, '_current_screen' ) );
} elseif ( ! is_admin() ) {
new MW_WP_Form_Main_Controller();
}
}
/**
* Enqueue assets.
*/
public function _admin_enqueue_scripts() {
$url = plugins_url( MWF_Config::NAME );
wp_enqueue_style( MWF_Config::NAME . '-admin-common', $url . '/css/admin-common.css' );
}
/**
* Add admin menu for chart.
*/
public function _admin_menu_for_chart() {
$contact_data_post_types = MW_WP_Form_Contact_Data_Setting::get_form_post_types();
if ( empty( $contact_data_post_types ) ) {
return;
}
add_submenu_page(
'edit.php?post_type=' . MWF_Config::NAME,
esc_html__( 'Chart', 'mw-wp-form' ),
esc_html__( 'Chart', 'mw-wp-form' ),
MWF_Config::CAPABILITY,
MWF_Config::NAME . '-chart',
'__return_false'
);
}
/**
* Add admin menu for saved inquiry data.
*/
public function _admin_menu_for_inquiry_data_list() {
$contact_data_post_types = MW_WP_Form_Contact_Data_Setting::get_form_post_types();
if ( empty( $contact_data_post_types ) ) {
return;
}
add_submenu_page(
'edit.php?post_type=' . MWF_Config::NAME,
__( 'Inquiry data', 'mw-wp-form' ),
__( 'Inquiry data', 'mw-wp-form' ),
MWF_Config::CAPABILITY,
MWF_Config::NAME . '-save-data',
'__return_false'
);
}
/**
* Front controller.
*
* @param WP_Screen $screen WP_Screen object.
*/
public function _current_screen( $screen ) {
if ( MWF_Config::NAME === $screen->id ) {
new MW_WP_Form_Admin_Controller();
} elseif ( 'edit-' . MWF_Config::NAME === $screen->id ) {
new MW_WP_Form_Admin_List_Controller();
} elseif ( MWF_Functions::is_contact_data_post_type( $screen->id ) ) {
new MW_WP_Form_Contact_Data_Controller();
} elseif ( preg_match( '/^edit-' . MWF_Config::DBDATA . '\d+$/', $screen->id ) ) {
new MW_WP_Form_Contact_Data_List_Controller();
} elseif ( MWF_Config::NAME . '_page_' . MWF_Config::NAME . '-chart' === $screen->id ) {
new MW_WP_Form_Chart_Controller();
} elseif ( MWF_Config::NAME . '_page_' . MWF_Config::NAME . '-save-data' === $screen->id ) {
new MW_WP_Form_Stores_Inquiry_Data_Form_List_Controller();
}
}
/**
* Register post types for MW WP Form and inquiry data.
*/
public function _register_post_type() {
if ( ! current_user_can( MWF_Config::CAPABILITY ) && is_admin() ) {
return;
}
// MW WP Form のフォーム設定を管理する投稿タイプ
register_post_type(
MWF_Config::NAME,
array(
'label' => 'MW WP Form',
'labels' => array(
'name' => 'MW WP Form',
'singular_name' => 'MW WP Form',
'add_new_item' => __( 'Add New Form', 'mw-wp-form' ),
'edit_item' => __( 'Edit Form', 'mw-wp-form' ),
'new_item' => __( 'New Form', 'mw-wp-form' ),
'view_item' => __( 'View Form', 'mw-wp-form' ),
'search_items' => __( 'Search Forms', 'mw-wp-form' ),
'not_found' => __( 'No Forms found', 'mw-wp-form' ),
'not_found_in_trash' => __( 'No Forms found in Trash', 'mw-wp-form' ),
),
'capability_type' => 'page',
'public' => false,
'show_ui' => true,
)
);
$admin = new MW_WP_Form_Admin();
$forms = $admin->get_forms_using_database();
foreach ( $forms as $form ) {
$post_type = MWF_Functions::get_contact_data_post_type_from_form_id( $form->ID );
register_post_type(
$post_type,
array(
'label' => $form->post_title,
'labels' => array(
'name' => $form->post_title,
'singular_name' => $form->post_title,
'edit_item' => __( 'Edit ', 'mw-wp-form' ) . ':' . $form->post_title,
'view_item' => __( 'View', 'mw-wp-form' ) . ':' . $form->post_title,
'search_items' => __( 'Search', 'mw-wp-form' ) . ':' . $form->post_title,
'not_found' => __( 'No data found', 'mw-wp-form' ),
'not_found_in_trash' => __( 'No data found in Trash', 'mw-wp-form' ),
),
'capability_type' => 'page',
'public' => false,
'show_ui' => true,
'show_in_menu' => false,
'supports' => array( 'title' ),
)
);
}
}
/**
* Uninstall processes.
*/
public static function _uninstall() {
$plugin_dir_path = plugin_dir_path( __FILE__ );
include_once( $plugin_dir_path . 'classes/models/class.admin.php' );
include_once( $plugin_dir_path . 'classes/models/class.file.php' );
$admin = new MW_WP_Form_Admin();
$forms = $admin->get_forms();
$data_post_ids = array();
foreach ( $forms as $form ) {
$data_post_ids[] = $form->ID;
wp_delete_post( $form->ID, true );
}
foreach ( $data_post_ids as $data_post_id ) {
delete_option( MWF_Config::NAME . '-chart-' . $data_post_id );
$data_posts = get_posts(
array(
'post_type' => MWF_Functions::get_contact_data_post_type_from_form_id( $data_post_id ),
'posts_per_page' => -1,
)
);
if ( empty( $data_posts ) ) {
continue;
}
foreach ( $data_posts as $data_post ) {
wp_delete_post( $data_post->ID, true );
}
}
try {
MW_WP_Form_Directory::do_empty( MW_WP_Form_Directory::get(), true );
MW_WP_Form_Directory::remove( MW_WP_Form_Directory::get( false ) );
} catch ( \Exception $e ) {
error_log( $e->getMessage() );
}
delete_option( MWF_Config::NAME );
}
public function _do_empty_temp_dir() {
try {
MW_WP_Form_Directory::do_empty( MW_WP_Form_Directory::get() );
} catch ( \Exception $e ) {
error_log( $e->getMessage() );
}
}
}
new MW_WP_Form();