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

157 lines 4.3 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
59 $messages = array();
60 foreach( $post_meta as $meta_key => $meta_values ) {
61 if( strpos( $meta_key, 'hf_message_' ) === 0 ) {
62 $message_key = substr( $meta_key, strlen( 'hf_message_' ) );
63 $messages[$message_key] = (string) $meta_values[0];
64 }
65 }
66
67 $form = new Form( $post->ID );
68 $form->title = $post->post_title;
69 $form->slug = $post->post_name;
70 $form->markup = $post->post_content;
71 $form->settings = array_merge( $default_settings, $settings );
72 $form->messages = array_merge( $default_messages, $messages );
73 return $form;
74 }
75
76 /**
77 * @param $form_id
78 * @param array $args
79 * @return Submission[]
80 */
81 function hf_get_form_submissions( $form_id, array $args = array() ) {
82 $default_args = array(
83 'offset' => 0,
84 'limit' => 1000
85 );
86 $args = array_merge( $default_args, $args );
87
88 global $wpdb;
89 $table = $wpdb->prefix .'hf_submissions';
90 $results = $wpdb->get_results( $wpdb->prepare( "SELECT s.* FROM {$table} s WHERE s.form_id = %d ORDER BY s.submitted_at DESC LIMIT {$args['offset']}, {$args['limit']};", $form_id ), OBJECT_K );
91 $submissions = array();
92 foreach( $results as $key => $object ) {
93 $submission = Submission::from_object( $object );
94 $submissions[$key] = $submission;
95 }
96 return $submissions;
97 }
98
99 /**
100 * @param int $submission_id
101 * @return Submission
102 */
103 function hf_get_form_submission( $submission_id ) {
104 global $wpdb;
105 $table = $wpdb->prefix .'hf_submissions';
106 $object = $wpdb->get_row( $wpdb->prepare( "SELECT s.* FROM {$table} s WHERE s.id = %d;", $submission_id ), OBJECT );
107 $submission = Submission::from_object( $object );
108 return $submission;
109 }
110 /**
111 * @return array
112 */
113 function hf_get_settings() {
114 static $default_settings = array(
115 'load_stylesheet' => 0,
116 );
117
118 $settings = get_option( 'hf_settings', array() );
119 return array_merge( $default_settings, $settings );
120 }
121
122 function hf_array_get( $array, $key, $default = null ) {
123 if ( is_null( $key ) ) {
124 return $array;
125 }
126
127 if ( isset( $array[$key] ) ) {
128 return $array[$key];
129 }
130
131 foreach (explode( '.', $key ) as $segment) {
132 if ( ! is_array( $array ) || ! array_key_exists( $segment, $array ) ) {
133 return $default;
134 }
135
136 $array = $array[$segment];
137 }
138
139 return $array;
140 }
141
142 /**
143 * @param string $template
144 * @param array $data
145 *
146 * @return string
147 */
148 function hf_template( $template, array $data = array() ) {
149 $template = preg_replace_callback( '/\[([ -~]+)\]/', function( $matches ) use ( $data ) {
150 $key = $matches[1];
151 $replacement = hf_array_get( $data, $key, '' );
152 return $replacement;
153 }, $template );
154 return $template;
155 }
156
157