PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.2.0
HTML Forms – Simple WordPress Forms Plugin v1.2.0
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.2.0, at src/functions.php

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