PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.1.1
HTML Forms – Simple WordPress Forms Plugin v1.1.1
trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 All 66 releases
html-forms / src / functions.php

functions.php in HTML Forms – Simple WordPress Forms Plugin 1.1.1, at src/functions.php

211 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use HTML_Forms\Form;
4 use HTML_Forms\Submission;
5
6 /**
7 * @param $form_id_or_slug int|string
8 * @return Form
9 * @throws Exception
10 */
11 function hf_get_form( $form_id_or_slug ) {
12
13 if( is_numeric( $form_id_or_slug ) ) {
14 $post = get_post( $form_id_or_slug );
15
16 if( ! $post || $post->post_type !== 'html-form' ) {
17 throw new Exception( "Invalid form ID" );
18 }
19 } else {
20 $posts = get_posts(
21 array(
22 'post_type' => 'html-form',
23 'name' => $form_id_or_slug,
24 'post_status' => 'publish',
25 'numberposts' => 1,
26 )
27 );
28
29 if( empty( $posts ) ) {
30 throw new Exception( 'Invalid form slug' );
31 }
32 $post = $posts[0];
33 }
34
35 static $default_messages;
36 if( $default_messages === null ) {
37 $default_messages = array(
38 'success' => __('Thank you! We will be in touch soon.', 'html-forms'),
39 'invalid_email' => __( 'Sorry, that email address looks invalid.', 'html-forms' ),
40 'required_field_missing' => __( "Please fill in the required fields.", "html-forms" ),
41 'error' => __( 'Oops. An error occurred.', 'html-forms' ),
42 );
43 }
44
45 static $default_settings = array(
46 'hide_after_success' => 0,
47 'redirect_url' => '',
48 'required_fields' =>'',
49 'email_fields' => '',
50 );
51
52 $post_meta = get_post_meta( $post->ID );
53
54 $settings = array();
55 if( ! empty( $post_meta['_hf_settings'][0] ) ) {
56 $settings = (array) maybe_unserialize( $post_meta['_hf_settings'][0] );
57 }
58 $settings = array_merge( $default_settings, $settings );
59
60 $messages = array();
61 foreach( $post_meta as $meta_key => $meta_values ) {
62 if( strpos( $meta_key, 'hf_message_' ) === 0 ) {
63 $message_key = substr( $meta_key, strlen( 'hf_message_' ) );
64 $messages[$message_key] = (string) $meta_values[0];
65 }
66 }
67 $messages = array_merge( $default_messages, $messages );
68
69 $form = new Form( $post->ID );
70 $form->title = $post->post_title;
71 $form->slug = $post->post_name;
72 $form->markup = $post->post_content;
73 $form->settings = $settings;
74 $form->messages = $messages;
75 return $form;
76 }
77
78 /**
79 * @param $form_id
80 * @param array $args
81 * @return Submission[]
82 */
83 function hf_get_form_submissions( $form_id, array $args = array() ) {
84 $default_args = array(
85 'offset' => 0,
86 'limit' => 1000,
87 );
88 $args = array_merge( $default_args, $args );
89
90 global $wpdb;
91 $table = $wpdb->prefix .'hf_submissions';
92 $results = $wpdb->get_results( $wpdb->prepare( "SELECT s.* FROM {$table} s WHERE s.form_id = %d ORDER BY s.submitted_at DESC LIMIT %d, %d;", $form_id, $args['offset'], $args['limit'] ), OBJECT_K );
93 $submissions = array();
94 foreach( $results as $key => $object ) {
95 $submission = Submission::from_object( $object );
96 $submissions[$key] = $submission;
97 }
98 return $submissions;
99 }
100
101 /**
102 * @param int $submission_id
103 * @return Submission
104 */
105 function hf_get_form_submission( $submission_id ) {
106 global $wpdb;
107 $table = $wpdb->prefix .'hf_submissions';
108 $object = $wpdb->get_row( $wpdb->prepare( "SELECT s.* FROM {$table} s WHERE s.id = %d;", $submission_id ), OBJECT );
109 $submission = Submission::from_object( $object );
110 return $submission;
111 }
112 /**
113 * @return array
114 */
115 function hf_get_settings() {
116 static $default_settings = array(
117 'load_stylesheet' => 0,
118 );
119
120 $settings = get_option( 'hf_settings', array() );
121 return array_merge( $default_settings, $settings );
122 }
123
124 /**
125 * Get element from array, allows for dot notation eg: "foo.bar"
126 *
127 * @param array $array
128 * @param string $key
129 * @param mixed $default
130 * @return mixed
131 */
132 function hf_array_get( $array, $key, $default = null ) {
133 if ( is_null( $key ) ) {
134 return $array;
135 }
136
137 if ( isset( $array[$key] ) ) {
138 return $array[$key];
139 }
140
141 foreach (explode( '.', $key ) as $segment) {
142 if ( ! is_array( $array ) || ! array_key_exists( $segment, $array ) ) {
143 return $default;
144 }
145
146 $array = $array[$segment];
147 }
148
149 return $array;
150 }
151
152 /**
153 * Processes template tags like {{user.user_email}}
154 *
155 * @param string $template
156 *
157 * @return string
158 */
159 function hf_template( $template ) {
160 $replacers = new HTML_Forms\TagReplacers();
161 $tags = array(
162 'user' => array( $replacers, 'user' ),
163 'post' => array( $replacers, 'post'),
164 'url_params' => array( $replacers, 'url_params' ),
165 );
166
167 /**
168 * Filters the available tags in HTML Forms templates, like {{user.user_email}}.
169 *
170 * Can be used to add simple scalar replacements or more advanced replacement functions that accept a parameter.
171 *
172 * @param array $tags
173 */
174 $tags = apply_filters( 'hf_template_tags', $tags );
175
176 $template = preg_replace_callback( '/\{\{ *(\w+)(?:\.([\w\.]+))? *(?:\|\| *(\w+))? *\}\}/', function( $matches ) use ( $tags ) {
177 $tag = $matches[1];
178 $param = ! isset( $matches[2] ) ? "" : $matches[2];
179 $default = ! isset( $matches[3] ) ? "" : $matches[3];
180 $value = "";
181
182 // do not change anything if we have no replacer with that key, could be custom user logic or another plugin.
183 if( ! isset( $tags[ $tag] ) ) {
184 return $matches[0];
185 }
186
187 $replacement = $tags[$tag];
188 $value = is_callable( $replacement ) ? call_user_func_array( $replacement, array( $param ) ) : $replacement;
189 return ! empty( $value ) ? $value : $default;
190 }, $template );
191
192 return $template;
193 }
194
195 /**
196 * @param string $string
197 * @param array $data
198 *
199 * @return string
200 */
201 function hf_replace_data_variables( $string, $data = array() ) {
202 $string = preg_replace_callback( '/\[([a-zA-Z0-9\-\._]+)\]/', function( $matches ) use ( $data ) {
203 $key = $matches[1];
204 $replacement = hf_array_get( $data, $key, '' );
205 $replacement = is_array( $replacement ) ? join( ', ', $replacement ) : $replacement;
206 return $replacement;
207 }, $string );
208 return $string;
209 }
210
211