From 0126d704524858e7187a5d80eeb8bcebb5d40f08 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Sat, 18 Jul 2015 18:16:01 +0300 Subject: [PATCH 01/19] New Component WP_Form_Element_Wrapper for wrapping forms parts. --- classes/elements/WP_Form_Element_Wrapper.php | 81 ++++++++++++++++++++ classes/views/WP_Form_View_Wrapper.php | 60 +++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 classes/elements/WP_Form_Element_Wrapper.php create mode 100644 classes/views/WP_Form_View_Wrapper.php diff --git a/classes/elements/WP_Form_Element_Wrapper.php b/classes/elements/WP_Form_Element_Wrapper.php new file mode 100644 index 0000000..0369c3e --- /dev/null +++ b/classes/elements/WP_Form_Element_Wrapper.php @@ -0,0 +1,81 @@ +get_name(); + } + if ( empty($key) ) { + throw new InvalidArgumentException(__('Cannot add nameless element to a wrapper', 'wp-forms')); + } + $this->elements[$key] = $element; + return $this; + } + + /** + * Remove a child component + * + * @param string $key + * @return $this + */ + public function remove_element( $key ) { + if ( isset($this->elements[$key]) ) { + unset($this->elements[$key]); + } + return $this; + } + + /** + * Get the element with the given key + * + * @param string $key + * + * @return WP_Form_Component|NULL + */ + public function get_element( $key ) { + if ( !empty($this->elements[$key]) ) { + return $this->elements[$key]; + } + foreach ( $this->elements as $e ) { + if ( $e instanceof WP_Form_Aggregate ) { + $child = $e->get_element($key); + if ( !empty($child) ) { + return $child; + } + } + } + return NULL; + } + + /** + * Get an array of all child components, sorted by priority. + * + * @return WP_Form_Component[] + */ + public function get_children() { + $elements = WP_Form_Element::sort_elements($this->elements); + return $elements; + } +} diff --git a/classes/views/WP_Form_View_Wrapper.php b/classes/views/WP_Form_View_Wrapper.php new file mode 100644 index 0000000..bc99683 --- /dev/null +++ b/classes/views/WP_Form_View_Wrapper.php @@ -0,0 +1,60 @@ +get_type(); + if ( method_exists( $this, $type ) ) { + return call_user_func( array( $this, $type ), $element ); + } elseif ( $element instanceof WP_Form_Element_Wrapper ) { + return $this->wrapper($element); // fallback to generic
+ } + return ''; + } + + protected function wrapper( WP_Form_Element_Wrapper $element ) { + $children = $this->render_children($element); + + $attributes = $element->get_all_attributes(); + // Because this is just a tag (div, span) we don't need this attrs in tag. + unset($attributes['name'], $attributes['tag_name'], $attributes['type'], $attributes['value']); + $attributes = WP_Form_View::prepare_attributes($attributes); + + $tag_name = $element->get_attribute('tag_name'); + if( empty( $tag_name ) ) { + $tag_name = 'div'; + } + $output = sprintf( + '<%1$s %2$s>%3$s', + $tag_name, + $attributes, + $children + ); + return $output; + } + + /** + * Walk through each child and render it + * + * @param WP_Form_Aggregate $fieldset + * @return string + */ + protected function render_children( WP_Form_Aggregate $wrapper ) { + $children = ''; + foreach ( $wrapper->get_children() as $child ) { + $children .= $this->render_child($child); + } + return $children; + } + + /** + * @param WP_Form_Component $element + * @return string + */ + protected function render_child( WP_Form_Component $element ) { + return $element->render(); + } + +} From b47f38a67ad741b13b8857212cf8e74a8db7df39 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Sat, 18 Jul 2015 19:57:14 +0300 Subject: [PATCH 02/19] Errors Decorator now can change the position, classes and other stuff --- .../decorators/WP_Form_Decorator_Errors.php | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/classes/views/decorators/WP_Form_Decorator_Errors.php b/classes/views/decorators/WP_Form_Decorator_Errors.php index b6b9ba1..d4c8700 100644 --- a/classes/views/decorators/WP_Form_Decorator_Errors.php +++ b/classes/views/decorators/WP_Form_Decorator_Errors.php @@ -1,16 +1,42 @@ get_errors(); $output = ''; if ( $errors ) { - $output = '
    '; - foreach ( $errors as $e ) { - $output .= '
  • '.$e.'
  • '; + $args = wp_parse_args( + $this->args, + array( + 'tag' => 'ul', + 'tag_single' => 'li', + 'class_single' => 'error', + 'attributes' => array(), + 'position' => self::POSITION_AFTER + ) + ); + + $output = '<' . $args['tag'] . ' ' . WP_Form_View::prepare_attributes( $args['attributes'] ) . '>'; + foreach ( $errors as $error ) { + $output .= '<' . $args['tag_single'] . ' class="' . $args['class_single']. '">' . $error . ''; } - $output .= '
'; + $output .= ''; + + switch ( $args['position'] ) { + case self::POSITION_AFTER: + return $this->component_view->render($element) . $output; + break; + + case self::POSITION_BEFORE: + default: + return $output . $this->component_view->render($element); + break; + } + + } - return $output . $this->component_view->render($element); } } From a67bc903f2a115154e098c5fd443d0bfe24cb8e0 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Sat, 18 Jul 2015 19:59:08 +0300 Subject: [PATCH 03/19] 0.4.1 --- wp-forms.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-forms.php b/wp-forms.php index 66502be..7493c77 100644 --- a/wp-forms.php +++ b/wp-forms.php @@ -5,7 +5,7 @@ Description: An API for creating, altering, validating, and submitting forms via code. Author: Flightless Author URI: http://flightless.us/ -Version: 0.4 +Version: 0.4.1 */ /* Copyright (c) 2013 Flightless, Inc. http://flightless.us/ From ef535798e977dc5819252d9a9767bc179b329cc9 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Sun, 19 Jul 2015 03:30:27 +0300 Subject: [PATCH 04/19] Recreate a logic in Errors decorator --- .../views/decorators/WP_Form_Decorator_Errors.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/classes/views/decorators/WP_Form_Decorator_Errors.php b/classes/views/decorators/WP_Form_Decorator_Errors.php index d4c8700..4b5d281 100644 --- a/classes/views/decorators/WP_Form_Decorator_Errors.php +++ b/classes/views/decorators/WP_Form_Decorator_Errors.php @@ -6,8 +6,8 @@ class WP_Form_Decorator_Errors extends WP_Form_Decorator { public function render( WP_Form_Component $element ) { $errors = $element->get_errors(); - $output = ''; if ( $errors ) { + $output = ''; $args = wp_parse_args( $this->args, array( @@ -19,11 +19,15 @@ public function render( WP_Form_Component $element ) { ) ); - $output = '<' . $args['tag'] . ' ' . WP_Form_View::prepare_attributes( $args['attributes'] ) . '>'; foreach ( $errors as $error ) { $output .= '<' . $args['tag_single'] . ' class="' . $args['class_single']. '">' . $error . ''; } - $output .= ''; + $output = sprintf( + '<%1$s %2$s>%3$s', + $args['tag'], + WP_Form_View::prepare_attributes( $args['attributes'] ), + $output + ); switch ( $args['position'] ) { case self::POSITION_AFTER: @@ -35,8 +39,8 @@ public function render( WP_Form_Component $element ) { return $output . $this->component_view->render($element); break; } - - } + + return $this->component_view->render($element); } } From 61cdf2f9c7a26401f91a312192fcabbd13ab45f7 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Sun, 19 Jul 2015 03:31:23 +0300 Subject: [PATCH 05/19] 0.4.1.1 --- wp-forms.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-forms.php b/wp-forms.php index 7493c77..449bf67 100644 --- a/wp-forms.php +++ b/wp-forms.php @@ -5,7 +5,7 @@ Description: An API for creating, altering, validating, and submitting forms via code. Author: Flightless Author URI: http://flightless.us/ -Version: 0.4.1 +Version: 0.4.1.1 */ /* Copyright (c) 2013 Flightless, Inc. http://flightless.us/ From 8648a6f78062816be472c8abc4dd4deeb096ee48 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Mon, 20 Jul 2015 15:36:31 +0300 Subject: [PATCH 06/19] start a 0.5 --- wp-forms.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-forms.php b/wp-forms.php index 449bf67..9f2b556 100644 --- a/wp-forms.php +++ b/wp-forms.php @@ -5,7 +5,7 @@ Description: An API for creating, altering, validating, and submitting forms via code. Author: Flightless Author URI: http://flightless.us/ -Version: 0.4.1.1 +Version: 0.5 */ /* Copyright (c) 2013 Flightless, Inc. http://flightless.us/ From 8747db812573b0e858e4e6107b076eb28a2226cd Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Tue, 21 Jul 2015 16:15:01 +0300 Subject: [PATCH 07/19] =?UTF-8?q?=D0=A1=D0=BB=D1=83=D1=88=D0=B0=D0=BB?= =?UTF-8?q?=D0=BA=D0=B0=20AJAX-=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- classes/WP_Form_Listener.php | 33 +++++++++++++++++++++++++++++++++ classes/WP_Form_Submission.php | 21 +++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/classes/WP_Form_Listener.php b/classes/WP_Form_Listener.php index eeb5468..04baf2c 100644 --- a/classes/WP_Form_Listener.php +++ b/classes/WP_Form_Listener.php @@ -6,6 +6,8 @@ class WP_Form_Listener { private function add_hooks() { add_action( 'init', array( $this, 'check_form_submission' ), 111, 0 ); + add_action( 'wp_ajax_wp_forms', array( $this, 'check_form_submission_ajax' ) ); + add_action( 'wp_ajax_nopriv_wp_forms', array( $this, 'check_form_submission_ajax' ) ); } public function check_form_submission() { @@ -25,6 +27,37 @@ public function check_form_submission() { $submission->redirect(); } + /** + * Similar to check_form_submission() but for AJAX requests. + * + * @see check_form_submission() + * + * @throws Exception + */ + public function check_form_submission_ajax() { + $kk = ''; + if ( empty($_REQUEST['data']['wp_form_id']) || empty($_REQUEST['data']['wp_form_nonce']) ) { + return; + } + if ( !wp_verify_nonce($_REQUEST['data']['wp_form_nonce'], $_REQUEST['data']['wp_form_id']) ) { + return; + } + + $form = wp_get_form($_REQUEST['data']['wp_form_id']); + $submission = new WP_Form_Submission($form, $_REQUEST['data']); + if ( !$submission->is_valid() ) { + /** + * похоже здесь это не надо вызывать, т к возможно эта штука втыкает ошибки в хтмл, + * а нам нужно просто подготовить ошибки и отправить их + */ + $submission->prepare_form(); + $submission->send_ajax_answer(); + return; + } + $submission->submit_ajax(); + $submission->send_ajax_answer(); + } + /********** Singleton *************/ /** diff --git a/classes/WP_Form_Submission.php b/classes/WP_Form_Submission.php index 959629e..c327654 100644 --- a/classes/WP_Form_Submission.php +++ b/classes/WP_Form_Submission.php @@ -34,6 +34,10 @@ public function submit() { } } + public function sumbit_ajax() { + $this->submit(); + } + /** * @param string $key The form element name, as it would be displayed * in the HTML. @@ -92,6 +96,23 @@ public function set_redirect( $url = '' ) { $this->redirect = $url; } + public function send_ajax_answer() { + $kk = ''; + if ( !$this->is_valid() ) { + wp_send_json_error( $this->errors ); + } + else { + wp_send_json_success( ); + } + + /*foreach( $this->errors as $error ) { + foreach( $error as $element_name => $element_error ) { + $kk = ''; + + } + }*/ + } + /** * Set values and errors on the form * to prepare it for rendering From d28971fea4ada65fd4e55411f7d17d0cac18e827 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Tue, 21 Jul 2015 17:55:52 +0300 Subject: [PATCH 08/19] New Element type for markup --- classes/elements/WP_Form_Element_Markup.php | 7 ++++++ classes/views/WP_Form_View_Markup.php | 26 +++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 classes/elements/WP_Form_Element_Markup.php create mode 100644 classes/views/WP_Form_View_Markup.php diff --git a/classes/elements/WP_Form_Element_Markup.php b/classes/elements/WP_Form_Element_Markup.php new file mode 100644 index 0000000..89b1846 --- /dev/null +++ b/classes/elements/WP_Form_Element_Markup.php @@ -0,0 +1,7 @@ +get_type(); + if ( method_exists( $this, $type ) ) { + return call_user_func( array( $this, $type ), $element ); + } elseif ( $element instanceof WP_Form_Element ) { + return $this->markup($element); // fallback to generic + } + return ''; + } + + protected function markup( WP_Form_Element $element ) { + $value = $element->get_value(); + + $attributes = $element->get_all_attributes(); + + unset($attributes['value'], $attributes['type']); + $attributes = WP_Form_View::prepare_attributes($attributes); + + $template = '
%s
'; + return sprintf( $template, $attributes, $value ); + } + +} From 9452be12d06346b18c6efd8aad35884bed87f194 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Tue, 21 Jul 2015 23:00:53 +0300 Subject: [PATCH 09/19] Ah. Send anwser with AJAX --- classes/WP_Form.php | 65 ++++++++++++++++++++++++++++++++++ classes/WP_Form_Submission.php | 13 ++----- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/classes/WP_Form.php b/classes/WP_Form.php index dc9fc07..fc8cb13 100644 --- a/classes/WP_Form.php +++ b/classes/WP_Form.php @@ -74,6 +74,71 @@ public function remove_element( $key ) { return $this; } + public function get_elements_as_array() { + $result = array(); + + $result = $this->get_elements_as_array_prepare( $this->elements ); + + return $result; + } + + public function get_elements_as_array_prepare( array $elements ) { + $result = array(); + + foreach( $elements as $name => $element ) { + if( $element instanceof WP_Form_Aggregate ) { + //foreach( $element->get_children() as $child ) { + $result[$name]['elements'] = $this->get_elements_as_array_prepare( $element->get_children() ); + //} + } + + $result[$name]['priority'] = $element->get_priority(); + $result[$name]['label'] = $element->get_label(); + $result[$name]['description'] = $element->get_description(); + $result[$name]['attributes'] = $element->get_all_attributes(); + } + + + + + + + + + + + + + /*$result['priority'] = $element->get_priority(); + $result['label'] = $element->get_label(); + $result['description'] = $element->get_description(); + $result['attributes'] = $element->get_all_attributes();*/ + + //$empty_or = empty( $element->elements ); + //$array_or = is_array( $element->elements ); + + /*if( !empty( $element->elements ) && is_array( $element->elements ) ) { + foreach( $element->elements as $inner_name => $inner_element ) { + $result['elements'][$inner_name] = $this->get_element_as_array( $inner_element ); + } + }*/ + + /*if( $element instanceof WP_Form_Aggregate ) { + foreach ( $element->get_children() as $child ) { + $this->prepare_form_values( $child ); + } + }*/ + + /*if( $element instanceof WP_Form_Aggregate ) { + foreach( $element->get_children() as $child) { + $child; + $result[$name]['elements'][] = $this->get_element_as_array( $child ); + } + }*/ + + return $result; + } + /** * @param $key * @return null|WP_Form_Component diff --git a/classes/WP_Form_Submission.php b/classes/WP_Form_Submission.php index c327654..bf74c2b 100644 --- a/classes/WP_Form_Submission.php +++ b/classes/WP_Form_Submission.php @@ -97,20 +97,13 @@ public function set_redirect( $url = '' ) { } public function send_ajax_answer() { - $kk = ''; + $elements = $this->form->get_elements_as_array(); if ( !$this->is_valid() ) { - wp_send_json_error( $this->errors ); + wp_send_json_error( $elements ); } else { - wp_send_json_success( ); + wp_send_json_success( $elements ); } - - /*foreach( $this->errors as $error ) { - foreach( $error as $element_name => $element_error ) { - $kk = ''; - - } - }*/ } /** From 71a734ece343760cc6e41588982863dc6e87aaf8 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Tue, 21 Jul 2015 23:11:53 +0300 Subject: [PATCH 10/19] Send via AJAX one type of data (arrays, not objects and arrays) --- classes/WP_Form.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/classes/WP_Form.php b/classes/WP_Form.php index fc8cb13..a87a7aa 100644 --- a/classes/WP_Form.php +++ b/classes/WP_Form.php @@ -83,19 +83,19 @@ public function get_elements_as_array() { } public function get_elements_as_array_prepare( array $elements ) { + $elements = array_values( $elements ); $result = array(); - foreach( $elements as $name => $element ) { - if( $element instanceof WP_Form_Aggregate ) { - //foreach( $element->get_children() as $child ) { - $result[$name]['elements'] = $this->get_elements_as_array_prepare( $element->get_children() ); - //} + $number_of = count( $elements ); + for( $i = 0; $i < $number_of; $i++ ) { + if( $elements[$i] instanceof WP_Form_Aggregate ) { + $result[$i]['elements'] = $this->get_elements_as_array_prepare( $elements[$i]->get_children() ); } - $result[$name]['priority'] = $element->get_priority(); - $result[$name]['label'] = $element->get_label(); - $result[$name]['description'] = $element->get_description(); - $result[$name]['attributes'] = $element->get_all_attributes(); + $result[$i]['priority'] = $elements[$i]->get_priority(); + $result[$i]['label'] = $elements[$i]->get_label(); + $result[$i]['description'] = $elements[$i]->get_description(); + $result[$i]['attributes'] = $elements[$i]->get_all_attributes(); } From 5677267461532c70bfa1be61e5626fd4dbdbdcf6 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Tue, 21 Jul 2015 23:12:22 +0300 Subject: [PATCH 11/19] Remove garbage --- classes/WP_Form.php | 39 --------------------------------------- 1 file changed, 39 deletions(-) diff --git a/classes/WP_Form.php b/classes/WP_Form.php index a87a7aa..8b99277 100644 --- a/classes/WP_Form.php +++ b/classes/WP_Form.php @@ -97,45 +97,6 @@ public function get_elements_as_array_prepare( array $elements ) { $result[$i]['description'] = $elements[$i]->get_description(); $result[$i]['attributes'] = $elements[$i]->get_all_attributes(); } - - - - - - - - - - - - - /*$result['priority'] = $element->get_priority(); - $result['label'] = $element->get_label(); - $result['description'] = $element->get_description(); - $result['attributes'] = $element->get_all_attributes();*/ - - //$empty_or = empty( $element->elements ); - //$array_or = is_array( $element->elements ); - - /*if( !empty( $element->elements ) && is_array( $element->elements ) ) { - foreach( $element->elements as $inner_name => $inner_element ) { - $result['elements'][$inner_name] = $this->get_element_as_array( $inner_element ); - } - }*/ - - /*if( $element instanceof WP_Form_Aggregate ) { - foreach ( $element->get_children() as $child ) { - $this->prepare_form_values( $child ); - } - }*/ - - /*if( $element instanceof WP_Form_Aggregate ) { - foreach( $element->get_children() as $child) { - $child; - $result[$name]['elements'][] = $this->get_element_as_array( $child ); - } - }*/ - return $result; } From 090184b5cab0d5eab2492e1257d7ba8242fc2b45 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Wed, 22 Jul 2015 01:39:21 +0300 Subject: [PATCH 12/19] Simplify the function code --- classes/WP_Form.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/classes/WP_Form.php b/classes/WP_Form.php index 8b99277..0cf4026 100644 --- a/classes/WP_Form.php +++ b/classes/WP_Form.php @@ -75,10 +75,7 @@ public function remove_element( $key ) { } public function get_elements_as_array() { - $result = array(); - - $result = $this->get_elements_as_array_prepare( $this->elements ); - + $result = (array)$this->get_elements_as_array_prepare( $this->elements ); return $result; } From ba229e1a5d9d8cad0efba908c8567b64b8c95b4e Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Wed, 22 Jul 2015 14:37:38 +0300 Subject: [PATCH 13/19] Send Errors fields to AJAX too --- classes/WP_Form.php | 1 + 1 file changed, 1 insertion(+) diff --git a/classes/WP_Form.php b/classes/WP_Form.php index 0cf4026..53131a0 100644 --- a/classes/WP_Form.php +++ b/classes/WP_Form.php @@ -93,6 +93,7 @@ public function get_elements_as_array_prepare( array $elements ) { $result[$i]['label'] = $elements[$i]->get_label(); $result[$i]['description'] = $elements[$i]->get_description(); $result[$i]['attributes'] = $elements[$i]->get_all_attributes(); + $result[$i]['errors'] = $elements[$i]->get_errors(); } return $result; } From b2ef92b1121aa29952ee8a0046f8f23b777fe62c Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Wed, 22 Jul 2015 18:37:57 +0300 Subject: [PATCH 14/19] Wrong function name --- classes/WP_Form_Submission.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/WP_Form_Submission.php b/classes/WP_Form_Submission.php index bf74c2b..17035c1 100644 --- a/classes/WP_Form_Submission.php +++ b/classes/WP_Form_Submission.php @@ -34,7 +34,7 @@ public function submit() { } } - public function sumbit_ajax() { + public function submit_ajax() { $this->submit(); } From 3c072ce75444fe26a2af1c041cb4906e454a3555 Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Wed, 22 Jul 2015 20:32:41 +0300 Subject: [PATCH 15/19] set_content() in WP_Form_Element API --- classes/elements/WP_Form_Element.php | 10 ++++++++++ classes/views/WP_Form_View_Markup.php | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/classes/elements/WP_Form_Element.php b/classes/elements/WP_Form_Element.php index cc2ef3a..b434efb 100644 --- a/classes/elements/WP_Form_Element.php +++ b/classes/elements/WP_Form_Element.php @@ -21,6 +21,7 @@ class WP_Form_Element implements WP_Form_Component, WP_Form_Attributes_Interface protected $value = ''; protected $description = ''; protected $errors = array(); + protected $content = ''; /** @var WP_Form_Attributes_Interface */ protected $attributes = NULL; @@ -196,6 +197,15 @@ public function clear_errors() { return $this; } + public function set_content( $content ) { + $this->content = $content; + return $this; + } + + public function get_content() { + return $this->content; + } + /** * @return string */ diff --git a/classes/views/WP_Form_View_Markup.php b/classes/views/WP_Form_View_Markup.php index 6a5ee0d..e9ce71d 100644 --- a/classes/views/WP_Form_View_Markup.php +++ b/classes/views/WP_Form_View_Markup.php @@ -12,7 +12,7 @@ public function render( WP_Form_Component $element ) { } protected function markup( WP_Form_Element $element ) { - $value = $element->get_value(); + $content = $element->get_content(); $attributes = $element->get_all_attributes(); @@ -20,7 +20,7 @@ protected function markup( WP_Form_Element $element ) { $attributes = WP_Form_View::prepare_attributes($attributes); $template = '
%s
'; - return sprintf( $template, $attributes, $value ); + return sprintf( $template, $attributes, $content ); } } From 79e8e9e23995ae8a1332a4f0f154fbf75ec0c8ce Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Sun, 26 Jul 2015 20:58:27 +0300 Subject: [PATCH 16/19] Prepare form and redirect after --- classes/WP_Form_Listener.php | 1 + 1 file changed, 1 insertion(+) diff --git a/classes/WP_Form_Listener.php b/classes/WP_Form_Listener.php index 04baf2c..d87999e 100644 --- a/classes/WP_Form_Listener.php +++ b/classes/WP_Form_Listener.php @@ -24,6 +24,7 @@ public function check_form_submission() { return; } $submission->submit(); + $submission->prepare_form(); $submission->redirect(); } From f4e3b2d0018e5a3d36f5464979fc812d4301fb7b Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Sat, 1 Aug 2015 23:40:27 +0300 Subject: [PATCH 17/19] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B1=D0=BB=D0=B5?= =?UTF-8?q?=D0=BC=D0=B0=20=D1=81=20=D0=BD=D0=B5=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D1=8F=D0=B5=D0=BC=D1=8B=D0=BC=D0=B8=20=D1=87=D0=B5?= =?UTF-8?q?=D0=BA=D0=B1=D0=BE=D0=BA=D1=81=D0=B0=D0=BC=D0=B8=20=D1=80=D0=B5?= =?UTF-8?q?=D1=88=D0=B5=D0=BD=D0=B0=20=D1=81=20=D0=BF=D0=BE=D0=BC=D0=BE?= =?UTF-8?q?=D1=89=D1=8C=D1=8E=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=20=D0=B8=D0=B7=20=D0=B4=D1=80=D1=83=D0=B3=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BA=D0=B0=20https://github.com/jbrinley/wp?= =?UTF-8?q?-forms/issues/8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- classes/elements/WP_Form_Element_Checkboxes.php | 7 +++++++ classes/views/WP_Form_View_Checkboxes.php | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/classes/elements/WP_Form_Element_Checkboxes.php b/classes/elements/WP_Form_Element_Checkboxes.php index c2ef0e9..15ceffb 100644 --- a/classes/elements/WP_Form_Element_Checkboxes.php +++ b/classes/elements/WP_Form_Element_Checkboxes.php @@ -3,4 +3,11 @@ class WP_Form_Element_Checkboxes extends WP_Form_Element_Multiple { protected $type = 'checkboxes'; protected $default_view = 'WP_Form_View_Checkboxes'; + + public function get_selected() { + if ( !empty($this->value) ) { + return $this->value; + } + return $this->default_value; + } } diff --git a/classes/views/WP_Form_View_Checkboxes.php b/classes/views/WP_Form_View_Checkboxes.php index 806e4de..21d218a 100644 --- a/classes/views/WP_Form_View_Checkboxes.php +++ b/classes/views/WP_Form_View_Checkboxes.php @@ -7,14 +7,19 @@ protected function checkboxes( WP_Form_Element_Checkboxes $element ) { throw new LogicException(__('Cannot render checkbox group without a name', 'wp-forms')); } $options = $element->get_options(); + $selected = (array) $element->get_selected(); $output = ''; foreach ( $options as $key => $label ) { - $output .= $this->checkbox( $key, $label, $attributes ); + $checked = false; + if ( in_array( $key, $selected ) ) { + $checked = true; + } + $output .= $this->checkbox( $key, $label, $attributes, $checked ); } return $output; } - protected function checkbox( $key, $label, $attributes ) { + protected function checkbox( $key, $label, $attributes, $checked ) { $checkbox = WP_Form_Element::create('checkbox') ->set_name($attributes['name'].'[]') ->set_label($label) @@ -32,6 +37,9 @@ protected function checkbox( $key, $label, $attributes ) { foreach ( $attributes as $att => $value ) { $checkbox->set_attribute($att, $value); } + if( $checked ) { + $checkbox->set_attribute( 'checked', 'checked' ); + } do_action('wp_form_checkbox_group_member', $checkbox); return $checkbox->render(); } From 0be5812077f0d74416d0b18835aea412b4a83d7e Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Mon, 3 Aug 2015 18:16:05 +0300 Subject: [PATCH 18/19] do esc_textarea for textarea content --- classes/views/WP_Form_View_Textarea.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/views/WP_Form_View_Textarea.php b/classes/views/WP_Form_View_Textarea.php index 76ad0e3..d801337 100644 --- a/classes/views/WP_Form_View_Textarea.php +++ b/classes/views/WP_Form_View_Textarea.php @@ -15,7 +15,7 @@ protected function textarea( WP_Form_Element $element ) { $attributes = $element->get_all_attributes(); $value = ''; if ( isset($attributes['value']) ) { - $value = $attributes['value']; + $value = esc_textarea( $attributes['value'] ); unset($attributes['value']); } $attributes = WP_Form_View::prepare_attributes($attributes); From 042158a108716747070ddae8c1cd9545aa48c9de Mon Sep 17 00:00:00 2001 From: Kolya Korobochkin Date: Mon, 3 Aug 2015 18:40:04 +0300 Subject: [PATCH 19/19] 0.5.1 --- wp-forms.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-forms.php b/wp-forms.php index 9f2b556..3cb7afa 100644 --- a/wp-forms.php +++ b/wp-forms.php @@ -5,7 +5,7 @@ Description: An API for creating, altering, validating, and submitting forms via code. Author: Flightless Author URI: http://flightless.us/ -Version: 0.5 +Version: 0.5.1 */ /* Copyright (c) 2013 Flightless, Inc. http://flightless.us/