PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.19
HTML Forms – Simple WordPress Forms Plugin v1.3.19
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 / class-form.php

class-form.php in HTML Forms – Simple WordPress Forms Plugin 1.3.19, at src/class-form.php

185 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace HTML_Forms;
4
5 class Form {
6
7 public $ID = 0;
8 public $title = '';
9 public $slug = '';
10 public $markup = '';
11 public $messages = array();
12 public $settings = array();
13
14 /**
15 * Form constructor.
16 *
17 * @param $id
18 */
19 public function __construct( $id ) {
20 $this->ID = $id;
21 }
22
23 /**
24 * Magic method for accessing unexisting properties, eg lowercase "id".
25 * @param string $property
26 * @return mixed
27 */
28 public function __get( $property ) {
29 if ( $property === 'id' ) {
30 return $this->ID;
31 }
32 }
33
34 public function get_html() {
35 $form = $this;
36
37 /**
38 * Filters the CSS classes to be added to this form's class attribute.
39 *
40 * @param array $form_classes
41 * @param Form $form
42 */
43 $form_classes_attr = apply_filters( 'hf_form_element_class_attr', '', $form );
44
45 /**
46 * Filters the action attribute for this form.
47 *
48 * @param string $form_action
49 * @param Form $form
50 */
51 $form_action = apply_filters( 'hf_form_element_action_attr', null, $form );
52 $form_action_attr = is_null( $form_action ) ? '' : sprintf( 'action="%s"', $form_action );
53
54 $data_attributes = $this->get_data_attributes();
55
56 $html = '';
57 $html .= sprintf( '<!-- HTML Forms v%s - %s -->', HTML_FORMS_VERSION, 'https://wordpress.org/plugins/html-forms/' );
58 $html .= sprintf( '<form method="post" %s class="hf-form hf-form-%d %s" %s>', $form_action_attr, $this->ID, esc_attr( $form_classes_attr ), $data_attributes );
59
60 $html .= sprintf( '<input type="hidden" name="_hf_form_id" value="%d" />', $this->ID );
61 $html .= sprintf( '<div style="display: none;"><input type="text" name="_hf_h%d" value="" /></div>', $this->ID );
62 $html .= '<div class="hf-fields-wrap">';
63 $html .= $this->get_markup();
64 $html .= '<noscript>' . __( 'Please enable JavaScript for this form to work.', 'html-forms' ) . '</noscript>';
65 $html .= '</div>'; // end field wrap
66 $html .= '</form>';
67 $html .= '<!-- / HTML Forms -->';
68
69 // ensure JS script is enqueued whenever this function is called
70 if ( function_exists( 'wp_enqueue_script' ) ) {
71 wp_enqueue_script( 'html-forms' );
72 }
73
74 /**
75 * Filters the resulting HTML for this form.
76 *
77 * @param string $html
78 * @param Form $form
79 */
80 $html = apply_filters( 'hf_form_html', $html, $form );
81 return $html;
82 }
83
84 public function get_data_attributes() {
85 $form = $this;
86 $attributes = array(
87 'id' => $this->ID,
88 'title' => $this->title,
89 'slug' => $this->slug,
90 );
91
92 // add messages
93 foreach ( $this->messages as $key => $message ) {
94 $key = str_replace( '_', '-', $key );
95 $attributes[ 'message-' . $key ] = $message;
96 }
97
98 /**
99 * Filters the data attributes to be added to the form attribute.
100 *
101 * @param array $attributes
102 * @param Form $form
103 */
104 $attributes = apply_filters( 'hf_form_element_data_attributes', $attributes, $form );
105
106 // create string of attribute key-value pairs
107 $string = '';
108 foreach ( $attributes as $attr => $value ) {
109 // prefix all attributes with data-
110 if ( substr( $attr, 0, 5 ) !== 'data-' ) {
111 $attr = 'data-' . $attr;
112 }
113
114 $string .= sprintf( '%s="%s" ', $attr, esc_attr( $value ) );
115 }
116 $string = rtrim( $string, ' ' );
117
118 return $string;
119 }
120
121
122 /**
123 * @return string
124 */
125 public function __toString() {
126 return $this->get_html();
127 }
128
129 /**
130 * @return string
131 */
132 public function get_markup() {
133 return apply_filters( 'hf_form_markup', $this->markup );
134 }
135
136 /**
137 * @return array
138 */
139 public function get_required_fields() {
140 if ( empty( $this->settings['required_fields'] ) ) {
141 return array();
142 }
143
144 $required_fields = explode( ',', $this->settings['required_fields'] );
145 return $required_fields;
146 }
147
148 /**
149 * @return array
150 */
151 public function get_email_fields() {
152 if ( empty( $this->settings['email_fields'] ) ) {
153 return array();
154 }
155
156 $email_fields = explode( ',', $this->settings['email_fields'] );
157 return $email_fields;
158 }
159
160 /**
161 * @param string $code
162 * @return string
163 */
164 public function get_message( $code ) {
165 $form = $this;
166 $message = isset( $this->messages[ $code ] ) ? $this->messages[ $code ] : '';
167
168 /**
169 * @param string $message
170 * @param Form $form
171 */
172 $message = apply_filters( 'hf_form_message_' . $code, $message, $form );
173 return $message;
174 }
175
176 /**
177 * @return int The number of named fields in the form
178 */
179 public function get_field_count() {
180 $count = substr_count( strtolower( $this->get_html() ), ' name=' );
181 $count++; // Add one for 'was-required'
182 return $count;
183 }
184 }
185