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

154 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 'post_name' => $form_id_or_slug,
24 'numberposts' => 1,
25 )
26 );
27
28 if( empty( $posts ) ) {
29 throw new Exception( 'Invalid form slug' );
30 }
31 $post = $posts[0];
32 }
33
34 static $default_messages;
35 if( $default_messages === null ) {
36 $default_messages = array(
37 'success' => __('Thank you! We will be in touch soon.', 'html-forms'),
38 'invalid_email' => __( 'Sorry, that email address looks invalid.', 'html-forms' ),
39 'required_field_missing' => __( "Please fill in the required fields.", "html-forms" ),
40 'error' => __( 'Oops. An error occurred.', 'html-forms' ),
41 );
42 }
43
44 static $default_settings = array(
45 'hide_after_success' => 0,
46 'redirect_url' => '',
47 'required_fields' =>'',
48 'email_fields' => '',
49 );
50
51 $post_meta = get_post_meta( $post->ID );
52
53 $settings = array();
54 if( ! empty( $post_meta['_hf_settings'][0] ) ) {
55 $settings = (array) maybe_unserialize( $post_meta['_hf_settings'][0] );
56 }
57
58 $messages = array();
59 foreach( $post_meta as $meta_key => $meta_values ) {
60 if( strpos( $meta_key, 'hf_message_' ) === 0 ) {
61 $message_key = substr( $meta_key, strlen( 'hf_message_' ) );
62 $messages[$message_key] = (string) $meta_values[0];
63 }
64 }
65
66 $form = new Form( $post->ID );
67 $form->title = $post->post_title;
68 $form->slug = $post->post_name;
69 $form->markup = $post->post_content;
70 $form->settings = array_merge( $default_settings, $settings );
71 $form->messages = array_merge( $default_messages, $messages );
72 return $form;
73 }
74
75 /**
76 * @param $form_id
77 * @param array $args
78 * @return Submission[]
79 */
80 function hf_get_form_submissions( $form_id, array $args = array() ) {
81 $default_args = array(
82 'offset' => 0,
83 'limit' => 1000
84 );
85 $args = array_merge( $default_args, $args );
86
87 global $wpdb;
88 $table = $wpdb->prefix .'hf_submissions';
89 $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 );
90 $submissions = array();
91 foreach( $results as $key => $object ) {
92 $submission = Submission::from_object( $object );
93 $submissions[$key] = $submission;
94 }
95 return $submissions;
96 }
97
98 /**
99 * @param int $submission_id
100 * @return Submission
101 */
102 function hf_get_form_submission( $submission_id ) {
103 global $wpdb;
104 $table = $wpdb->prefix .'hf_submissions';
105 $object = $wpdb->get_row( $wpdb->prepare( "SELECT s.* FROM {$table} s WHERE s.id = %d;", $submission_id ), OBJECT );
106 $submission = Submission::from_object( $object );
107 return $submission;
108 }
109 /**
110 * @return array
111 */
112 function hf_get_settings() {
113 static $default_settings = array(
114 'load_stylesheet' => 0,
115 );
116
117 $settings = get_option( 'hf_settings', array() );
118 return array_merge( $default_settings, $settings );
119 }
120
121 function hf_array_get( $array, $key, $default = null ) {
122 if ( is_null( $key ) ) {
123 return $array;
124 }
125
126 if ( isset( $array[$key] ) ) {
127 return $array[$key];
128 }
129
130 foreach (explode( '.', $key ) as $segment) {
131 if ( ! is_array( $array ) || ! array_key_exists( $segment, $array ) ) {
132 return $default;
133 }
134
135 $array = $array[$segment];
136 }
137
138 return $array;
139 }
140
141 /**
142 * @param string $template
143 * @param array $data
144 *
145 * @return string
146 */
147 function hf_template( $template, array $data = array() ) {
148 $template = preg_replace_callback( '/\[([ -~]+)\]/', function( $matches ) use ( $data ) {
149 $key = $matches[1];
150 $replacement = hf_array_get( $data, $key, '' );
151 return $replacement;
152 }, $template );
153 return $template;
154 }