PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.0.0-beta.3
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.0.0-beta.3
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / admin / class-form-template.php

class-form-template.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.0.0-beta.3, at includes/admin/class-form-template.php

138 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The template manager
5 */
6 class WeForms_Form_Template {
7
8 public function __construct() {
9 add_filter( 'wp_ajax_weforms_contact_form_template', array( $this, 'create_contact_form_from_template' ) );
10 }
11
12 /**
13 * Get a template object by name from the registry
14 *
15 * @param string $template
16 *
17 * @return boolean|WPUF_Post_Form_Template
18 */
19 public function get_template_object( $template ) {
20 $registry = weforms_get_form_templates();
21
22 if ( ! array_key_exists( $template, $registry ) ) {
23 return false;
24 }
25
26 $template_object = $registry[ $template ];
27
28 if ( ! is_a( $template_object, 'WPUF_Post_Form_Template') ) {
29 return false;
30 }
31
32 return $template_object;
33 }
34
35 /**
36 * Create a posting form from a post template
37 *
38 * @return void
39 */
40 public function create_contact_form_from_template() {
41 check_ajax_referer( 'weforms' );
42
43 $template_name = isset( $_REQUEST['template'] ) ? sanitize_text_field( $_REQUEST['template'] ) : '';
44
45 if ( ! $template_name ) {
46 return;
47 }
48
49 if ( 'blank_form' == $template_name ) {
50 $this->create_blank_form();
51 }
52
53 $template_object = $this->get_template_object( $template_name );
54
55 if ( false === $template_object ) {
56 return;
57 }
58
59 $current_user = get_current_user_id();
60 $form_title = $template_object->get_title();
61 $has_existing = get_page_by_title( $form_title, 'OBJECT', 'wpuf_contact_form' );
62
63 $form_post_data = array(
64 'post_title' => $form_title,
65 'post_type' => 'wpuf_contact_form',
66 'post_status' => 'publish',
67 'post_author' => $current_user
68 );
69
70 $form_id = wp_insert_post( $form_post_data );
71
72 if ( is_wp_error( $form_id ) ) {
73 return;
74 }
75
76 // if there is an existing form with same name, update this one with its ID
77 if ( $has_existing ) {
78 wp_update_post( array(
79 'ID' => $form_id,
80 'post_title' => $form_title . ' (#' . $form_id . ')'
81 ) );
82 }
83
84 // form has been created, lets setup
85 update_post_meta( $form_id, 'wpuf_form_settings', $template_object->get_form_settings() );
86 update_post_meta( $form_id, 'notifications', $template_object->get_form_notifications() );
87
88 $form_fields = $template_object->get_form_fields();
89
90 if ( $form_fields ) {
91 foreach ($form_fields as $menu_order => $field) {
92 wp_insert_post( array(
93 'post_type' => 'wpuf_input',
94 'post_status' => 'publish',
95 'post_content' => maybe_serialize( $field ),
96 'post_parent' => $form_id,
97 'menu_order' => $menu_order
98 ) );
99 }
100 }
101
102 wp_send_json_success( array(
103 'id' => $form_id
104 ) );
105 }
106
107 /**
108 * Create a blank form from the contact form template setting
109 *
110 * @return void
111 */
112 public function create_blank_form() {
113 $current_user = get_current_user_id();
114
115 $form_post_data = array(
116 'post_title' => 'Blank Form',
117 'post_type' => 'wpuf_contact_form',
118 'post_status' => 'publish',
119 'post_author' => $current_user
120 );
121
122 $form_id = wp_insert_post( $form_post_data );
123 if ( is_wp_error( $form_id ) ) {
124 return;
125 }
126
127 require_once WEFORMS_INCLUDES . '/admin/form-templates/contact-form.php';
128
129 $template_object = new WPUF_Contact_Form_Template_Contact();
130 update_post_meta( $form_id, 'wpuf_form_settings', $template_object->get_form_settings() );
131 update_post_meta( $form_id, 'notifications', $template_object->get_form_notifications() );
132
133 wp_send_json_success( array(
134 'id' => $form_id
135 ) );
136 }
137 }
138