forked from weDevsOfficial/wp-user-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform-template.php
217 lines (177 loc) · 6.6 KB
/
form-template.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
<?php
/**
* Admin form template handler
*
* Create forms based on form templates
*
* @since 2.4
*/
class WPUF_Admin_Form_Template {
public function __construct() {
add_action( 'admin_enqueue_scripts', array($this, 'enqueue_scripts') );
// post form templates
add_action( 'admin_footer', array( $this, 'render_post_form_templates' ) );
add_filter( 'admin_action_wpuf_post_form_template', array( $this, 'create_post_form_from_template' ) );
// form settings
add_action( 'wpuf_form_setting', array( $this, 'post_form_settings' ), 8, 2 );
// frontend insert/update
add_action( 'wpuf_add_post_after_insert', array( $this, 'post_form_submission' ), 10, 3 );
add_action( 'wpuf_edit_post_after_update', array( $this, 'post_form_submission' ), 10, 3 );
}
/**
* Should a form displayed or sciprt enqueued?
*
* @return boolean
*/
public function should_display() {
$current_screen = get_current_screen();
if ( in_array( $current_screen->id, array( 'user-frontend_page_wpuf-post-forms' ) ) ) {
return true;
}
return false;
}
public function enqueue_scripts() {
if ( ! $this->should_display() ) {
return;
}
wp_enqueue_style( 'wpuf-formbuilder', WPUF_ASSET_URI . '/css/wpuf-form-builder.css' );
}
/**
* Render the forms in the modal
*
* @return void
*/
public function render_post_form_templates() {
if ( ! $this->should_display() ) {
return;
}
$registry = wpuf_get_post_form_templates();
$blank_form_url = admin_url( 'admin.php?page=wpuf-post-forms&action=add-new' );
$action_name = 'wpuf_post_form_template';
$footer_help = sprintf( __( 'Want a new integration? <a href="%s" target="_blank">Let us know</a>.', 'wp-user-frontend'), 'mailto:[email protected]?subject=WPUF Custom Post Template Integration Request' );
if ( ! $registry ) {
return;
}
include dirname( __FILE__ ) . '/html/modal.php';
}
/**
* Get a template object by name from the registry
*
* @param string $template
*
* @return boolean|WPUF_Post_Form_Template
*/
public function get_template_object( $template ) {
$registry = wpuf_get_post_form_templates();
if ( ! array_key_exists( $template, $registry ) ) {
return false;
}
$template_object = $registry[ $template ];
if ( ! is_a( $template_object, 'WPUF_Post_Form_Template') ) {
return false;
}
return $template_object;
}
/**
* Create a posting form from a post template
*
* @since 2.4
*
* @return void
*/
public function create_post_form_from_template() {
check_admin_referer( 'wpuf_create_from_template' );
$template_name = isset( $_GET['template'] ) ? sanitize_text_field( $_GET['template'] ) : '';
if ( ! $template_name ) {
return;
}
$template_object = $this->get_template_object( $template_name );
if ( false === $template_object ) {
return;
}
$current_user = get_current_user_id();
$form_post_data = array(
'post_title' => $template_object->get_title(),
'post_type' => 'wpuf_forms',
'post_status' => 'publish',
'post_author' => $current_user
);
$form_id = wp_insert_post( $form_post_data );
if ( is_wp_error( $form_id ) ) {
return;
}
// form has been created, lets setup
update_post_meta( $form_id, 'wpuf_form_settings', $template_object->get_form_settings() );
update_post_meta( $form_id, 'wpuf_form_version', WPUF_VERSION );
$form_fields = $template_object->get_form_fields();
if ( ! $form_fields ) {
return;
}
foreach ($form_fields as $menu_order => $field) {
wp_insert_post( array(
'post_type' => 'wpuf_input',
'post_status' => 'publish',
'post_content' => maybe_serialize( $field ),
'post_parent' => $form_id,
'menu_order' => $menu_order
) );
}
wp_redirect( admin_url( 'admin.php?page=wpuf-post-forms&action=edit&id=' . $form_id ) );
exit;
}
/**
* Add settings field to override a form template
*
* @param array $form_settings
* @param object $post
*
* @return void
*/
public function post_form_settings( $form_settings, $post ) {
$registry = wpuf_get_post_form_templates();
$selected = isset( $form_settings['form_template'] ) ? $form_settings['form_template'] : '';
?>
<tr>
<th><?php _e( 'Form Template', 'wp-user-frontend' ); ?></th>
<td>
<select name="wpuf_settings[form_template]">
<option value=""><?php echo __( '— No Template —', 'wp-user-frontend' ); ?></option>
<?php
if ( $registry ) {
foreach ($registry as $key => $template) {
printf( '<option value="%s"%s>%s</option>' . "\n", $key, selected( $selected, $key, false ), $template->get_title() );
}
}
?>
</select>
<p class="description"><?php _e( 'If selected a form template, it will try to execute that integration options when new post created and updated.', 'wp-user-frontend' ); ?></p>
</td>
</tr>
<?php
}
/**
* Call the integration functions on form submission/update
*
* @param int $post_id
* @param int $form_id
* @param array $form_settings
*
* @return void
*/
public function post_form_submission( $post_id, $form_id, $form_settings ) {
$template = isset( $form_settings['form_template'] ) ? $form_settings['form_template'] : '';
if ( !$template ) {
return;
}
$template_object = $this->get_template_object( $template );
if ( false === $template_object ) {
return;
}
$current_action = current_action();
if ( $current_action == 'wpuf_add_post_after_insert' ) {
$template_object->after_insert( $post_id, $form_id, $form_settings );
} elseif ( $current_action == 'wpuf_edit_post_after_update' ) {
$template_object->after_update( $post_id, $form_id, $form_settings );
}
}
}