From: Stephen Cameron Date: Tue, 28 Apr 2020 16:53:41 +0000 (+0200) Subject: Form processing, email generation + Elementor widget. WIP #3445 @8 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=4f4474a7ec30f55b390ed2e9215e550f7cb1c013;p=ccv-wordpress.git Form processing, email generation + Elementor widget. WIP #3445 @8 --- diff --git a/wp-content/mu-plugins/cube/src/Elementor/Setup.php b/wp-content/mu-plugins/cube/src/Elementor/Setup.php index 91d1732..cbf6fc8 100644 --- a/wp-content/mu-plugins/cube/src/Elementor/Setup.php +++ b/wp-content/mu-plugins/cube/src/Elementor/Setup.php @@ -13,6 +13,9 @@ class Setup { // Customise Elementor "section" element to allow layout options (via CSS classes applied to main section element) $this->register_customisations(); + // Register widget category with Elementor + add_action('elementor/elements/categories_registered', [$this, 'register_widgets_category']); + // Register widgets with Elementor add_action('elementor/widgets/widgets_registered', [$this, 'register_widgets']); } @@ -25,6 +28,12 @@ class Setup { } + // Ref: https://developers.elementor.com/widget-categories/ + /* @var $manager \Elementor\Elements_Manager */ + public function register_widgets_category($manager) { + $manager->add_category('cube', ['title' => 'Cubedesigners']); + } + public function register_widgets() { @@ -45,6 +54,7 @@ class Setup { $elementor->widgets_manager->register_widget_type( new Widgets\DynamicTable() ); $elementor->widgets_manager->register_widget_type( new Widgets\FancyList() ); $elementor->widgets_manager->register_widget_type( new Widgets\JustifiedList() ); + $elementor->widgets_manager->register_widget_type( new Widgets\Form() ); } protected function _customise_sections() { diff --git a/wp-content/mu-plugins/cube/src/Elementor/Widgets/Form.php b/wp-content/mu-plugins/cube/src/Elementor/Widgets/Form.php new file mode 100644 index 0000000..a712206 --- /dev/null +++ b/wp-content/mu-plugins/cube/src/Elementor/Widgets/Form.php @@ -0,0 +1,114 @@ +get_forms(); + + $this->start_controls_section( + 'section_content', + [ + 'label' => __( 'Formulaire', 'cube' ), + ] + ); + + $this->add_control( + 'form_name', + [ + 'type' => Controls_Manager::SELECT, + 'label' => __( 'Formulaire', 'cube' ), + 'label_block' => true, + 'default' => '', + 'options' => ['' => __('Sélectionner', 'cube')] + + array_combine(array_keys($forms), array_map('ucfirst', array_keys($forms))) + ] + ); + + $this->add_control( + 'destination', + [ + 'type' => Controls_Manager::TEXT, + 'label' => __("L'adresse e-mail de destination", 'cube'), + 'label_block' => true, + 'default' => '', + 'placeholder' => $form_base->get_destination(), + ] + ); + + $this->end_controls_section(); + + $this->common_controls(); + } + /** + * Render the widget output on the frontend. + * Written in PHP and used to generate the final HTML. + * + * @since 1.0.0 + * @access protected + */ + protected function render() { + + $form_name = $this->get_settings('form_name'); + $destination = $this->get_settings('destination'); + + // Serialize and encode configuration for use in hidden field + // Currently only the form name and destination address is set but this might be extended in the future + $config = base64_encode(serialize(compact('form_name', 'destination'))); + + $base_form = new BaseForm(); + $forms = $base_form->get_forms(); + + if (!empty($form_name) && array_key_exists($form_name, $forms)) { + /* @var $form BaseForm */ + $form = new $forms[$form_name]; + $form->init(); + echo view("forms.common.wrapper", compact('form', 'form_name', 'config')); + } + + echo ''; + } +} diff --git a/wp-content/mu-plugins/cube/src/Elementor/Widgets/_Base.php b/wp-content/mu-plugins/cube/src/Elementor/Widgets/_Base.php index 8f89208..8ec94b3 100644 --- a/wp-content/mu-plugins/cube/src/Elementor/Widgets/_Base.php +++ b/wp-content/mu-plugins/cube/src/Elementor/Widgets/_Base.php @@ -9,8 +9,9 @@ abstract class _Base extends Widget_Base { // Where to display the widgets in the Elementor interface // Ref: https://developers.elementor.com/widget-categories/ + // See Elementor/Setup.php for definition of the category public function get_categories() { - return [ 'theme-elements' ]; + return ['cube']; } // Wrapper for including all common controls diff --git a/wp-content/mu-plugins/cube/src/Forms/Base.php b/wp-content/mu-plugins/cube/src/Forms/Base.php index 533b4d8..4a4c5d5 100644 --- a/wp-content/mu-plugins/cube/src/Forms/Base.php +++ b/wp-content/mu-plugins/cube/src/Forms/Base.php @@ -3,11 +3,12 @@ namespace Cube\Forms; use function Roots\asset; +use function Roots\view; class Base { private $action_name = 'cube_forms_process'; // Name of the AJAX action - private $destination = 'avis-medical@ccv-montpellier.fr'; // Default, can be overridden + private $destination = 'ccv@ccv-montpellier.fr'; // Default, can be overridden private $forms = [ // All available forms 'consultation' => Consultation::class, @@ -17,6 +18,8 @@ class Base private $fields = []; private $data; + public $form_title = ''; + // Field types const BINARY = 'binary'; const CHECKBOX = 'checkbox'; @@ -117,8 +120,11 @@ class Base wp_send_json_error(new \WP_Error('nonce', 'Failed security validation')); } - // Next, check which form is being sent and if it is valid - $form_name = $_POST['_form_name'] ?? null; + // Next, get the form configuration and decode + unserialize it + $config = unserialize(base64_decode($_POST['config'])); + + //check which form is being sent and if it is valid + $form_name = $config['form_name'] ?? null; if (!($form_name && array_key_exists($form_name, $this->forms))) { wp_send_json_error(new \WP_Error('invalid_form', "Unknown form ($form_name)")); @@ -128,6 +134,7 @@ class Base /* @var $form Base */ $form = new $this->forms[$form_name]; $form->register_fields(); + $form->set_destination($config['destination']); $form->data = $this->get_form_data(); $form->process(); exit; @@ -139,16 +146,34 @@ class Base $this->pre_process(); - echo "SENDING FORM TO: {$this->destination}\n\n"; + $headers = []; + $data = []; + + $to = $this->destination; + $from = 'CCV form_title; + $content_type = 'text/html'; + $charset = get_bloginfo('charset'); + $headers[] = sprintf('Content-Type: %s; charset=%s', $content_type, $charset); + $headers[] = sprintf('From: %s', $from); + // Gather filled fields into label/value pairs foreach ($this->fields as $field_name => $field) { + // TODO: handle file upload fields - should it be removed from HTML and just attached? Or include the filename reference? if ($value = $this->data($field_name)) { - if (is_array($value)) $value = "\n- " . implode("\n- ", $value); - echo "{$field['title']} {$value}\n"; + $data[$field['title']] = $value; } } - // ... + $message = view('forms.common.email', compact('data')); + + $success = wp_mail($to, $subject, $message, $headers); + + if ($success) { + wp_send_json_success(__('Success !', 'ccv')); + } else { + wp_send_json_error(); + } $this->post_process(); } @@ -412,8 +437,15 @@ class Base return ''; } - function set_destination($address) { - $this->destination = $address; + + public function get_destination() { + return $this->destination; + } + + public function set_destination($address = '') { + if (!empty($address)) { + $this->destination = $address; + } } /** diff --git a/wp-content/mu-plugins/cube/src/Forms/Consultation.php b/wp-content/mu-plugins/cube/src/Forms/Consultation.php index fac8333..f4aed02 100644 --- a/wp-content/mu-plugins/cube/src/Forms/Consultation.php +++ b/wp-content/mu-plugins/cube/src/Forms/Consultation.php @@ -6,6 +6,10 @@ use function Roots\asset; class Consultation extends Base { + public function __construct() { + $this->form_title = __('Formulaire de consultation', 'ccv'); + } + public function register_scripts() { parent::register_scripts(); diff --git a/wp-content/mu-plugins/cube/src/Forms/Training.php b/wp-content/mu-plugins/cube/src/Forms/Training.php index 34aa422..49ae615 100644 --- a/wp-content/mu-plugins/cube/src/Forms/Training.php +++ b/wp-content/mu-plugins/cube/src/Forms/Training.php @@ -4,6 +4,10 @@ namespace Cube\Forms; class Training extends Base { + public function __construct() { + $this->form_title = __('Demande de formation', 'ccv'); + } + public function register_scripts() { parent::register_scripts(); } diff --git a/wp-content/mu-plugins/cube/src/Init.php b/wp-content/mu-plugins/cube/src/Init.php index a34dd1a..1ee6d4e 100644 --- a/wp-content/mu-plugins/cube/src/Init.php +++ b/wp-content/mu-plugins/cube/src/Init.php @@ -17,7 +17,6 @@ final class Init { // Marked as final because this class should never be extende Elementor\Setup::class, CPT\Person::class, CPT\ScientificNews::class, - Shortcodes\CCVForm::class, Forms\Base::class, ]; } diff --git a/wp-content/mu-plugins/cube/src/Shortcodes/CCVForm.php b/wp-content/mu-plugins/cube/src/Shortcodes/CCVForm.php deleted file mode 100644 index 01768a4..0000000 --- a/wp-content/mu-plugins/cube/src/Shortcodes/CCVForm.php +++ /dev/null @@ -1,43 +0,0 @@ -forms = [ - 'consultation' => Forms\Consultation::class, - 'training' => Forms\Training::class, - ]; - - // Register [ccv_form] shortcode - add_shortcode('ccv_form', [$this, 'shortcode']); - } - - public function shortcode($attributes) { - - extract(shortcode_atts([ - 'name' => '', // Name of the form template - ], $attributes)); - - if (array_key_exists($name, $this->forms)) { - /* @var $form Forms\Base */ - $form = new $this->forms[$name]; - $form->init(); - return view("forms/wrapper", compact('form', 'name')); - } - - return false; - } - -} diff --git a/wp-content/themes/CCV/app/Composers/FormEmail.php b/wp-content/themes/CCV/app/Composers/FormEmail.php new file mode 100644 index 0000000..9eaa20c --- /dev/null +++ b/wp-content/themes/CCV/app/Composers/FormEmail.php @@ -0,0 +1,24 @@ + + + + + + CCV Formulaire + + + + + @foreach($data as $label => $value) + even) bgcolor="#eeeeee" @endif> + + + + @endforeach +
{{ $label }} + @if (is_array($value)) +
    +
  • {!! implode('
  • ', $value) !!}
  • +
+ @else + {!! nl2br($value) !!} + @endif +
+ + diff --git a/wp-content/themes/CCV/resources/views/forms/common/wrapper.blade.php b/wp-content/themes/CCV/resources/views/forms/common/wrapper.blade.php new file mode 100644 index 0000000..3f46456 --- /dev/null +++ b/wp-content/themes/CCV/resources/views/forms/common/wrapper.blade.php @@ -0,0 +1,14 @@ +{{-- FORM WRAPPER--}} +
+ + + + + + @includeIf("forms/$form_name") + +
diff --git a/wp-content/themes/CCV/resources/views/forms/consultation.blade.php b/wp-content/themes/CCV/resources/views/forms/consultation.blade.php index ecf6879..b44d8c3 100644 --- a/wp-content/themes/CCV/resources/views/forms/consultation.blade.php +++ b/wp-content/themes/CCV/resources/views/forms/consultation.blade.php @@ -277,7 +277,7 @@ // Once the form is processed, this will be used to look up the e-mail address. 'options' => array_combine(array_keys($form->get_field_options('surgeon')), array_keys($form->get_field_options('surgeon'))), 'placeholder' => [ - '' => __('Sélectionner') // First select option + '' => __('Sélectionner', 'ccv') // First select option ] ]) !!} diff --git a/wp-content/themes/CCV/resources/views/forms/wrapper.blade.php b/wp-content/themes/CCV/resources/views/forms/wrapper.blade.php deleted file mode 100644 index fdf33c6..0000000 --- a/wp-content/themes/CCV/resources/views/forms/wrapper.blade.php +++ /dev/null @@ -1,14 +0,0 @@ -{{-- FORM WRAPPER--}} -
- - - - - - @includeIf("forms/$name") - -