| @@ -1,112 +1,136 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | - | |
| 4 | -namespace Jet_Form_Builder\Form_Messages; | |
| 5 | - | |
| 6 | -// If this file is called directly, abort. | |
| 7 | -use Jet_Form_Builder\Classes\Get_Template_Trait; | |
| 8 | - | |
| 9 | -if ( ! defined( 'WPINC' ) ) { | |
| 10 | - die; | |
| 11 | -} | |
| 12 | - | |
| 13 | -/** | |
| 14 | - * Form messages class | |
| 15 | - */ | |
| 16 | -class Builder { | |
| 17 | - | |
| 18 | - use Get_Template_Trait; | |
| 19 | - | |
| 20 | - private $form_id; | |
| 21 | - private $status; | |
| 22 | - | |
| 23 | - | |
| 24 | - public $manager; | |
| 25 | - | |
| 26 | - /** | |
| 27 | - * Class constructor | |
| 28 | - * | |
| 29 | - * @param $data | |
| 30 | - */ | |
| 31 | - public function __construct( $data ) { | |
| 32 | - $this->form_id = $data->form_id; | |
| 33 | - $this->manager = new Manager( $this->form_id, $data->actions ); | |
| 34 | - } | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * Set form submittion status | |
| 38 | - * | |
| 39 | - * @param [type] $status [description] | |
| 40 | - * | |
| 41 | - * @return Builder | |
| 42 | - */ | |
| 43 | - public function set_form_status( $status ) { | |
| 44 | - $this->status = $status; | |
| 45 | - | |
| 46 | - return $this; | |
| 47 | - } | |
| 48 | - | |
| 49 | - /** | |
| 50 | - * Get form submitting status | |
| 51 | - */ | |
| 52 | - public function get_form_status() { | |
| 53 | - if ( ! $this->status ) { | |
| 54 | - $this->status = isset( $_REQUEST['status'] ) ? esc_attr( $_REQUEST['status'] ) : null; | |
| 55 | - } | |
| 56 | - | |
| 57 | - return $this->status; | |
| 58 | - } | |
| 59 | - | |
| 60 | - public function render_empty_field_message() { | |
| 61 | - $message_content = $this->manager->get_message( 'empty_field' ); | |
| 62 | - | |
| 63 | - include $this->get_global_template( 'common/field-message.php' ); | |
| 64 | - } | |
| 65 | - | |
| 66 | - /** | |
| 67 | - * Render form messages | |
| 68 | - * | |
| 69 | - * @return void | |
| 70 | - */ | |
| 71 | - public function render_messages() { | |
| 72 | - $status = $this->get_form_status(); | |
| 73 | - | |
| 74 | - if ( ! $status ) { | |
| 75 | - return; | |
| 76 | - } | |
| 77 | - | |
| 78 | - $message_content = $this->manager->get_message( $status ); | |
| 79 | - | |
| 80 | - $class = 'jet-form-builder-message'; | |
| 81 | - $class .= ' jet-form-builder-message--' . $this->manager->get_status_class( $status ); | |
| 82 | - | |
| 83 | - include $this->get_global_template( 'common/messages.php' ); | |
| 84 | - } | |
| 85 | - | |
| 86 | - /** | |
| 87 | - * Render message samples for editor | |
| 88 | - * | |
| 89 | - * @return [type] [description] | |
| 90 | - */ | |
| 91 | - public function render_messages_samples() { | |
| 92 | - // Render success sample | |
| 93 | - $this->set_form_status( 'success' ); | |
| 94 | - $this->render_messages(); | |
| 95 | - | |
| 96 | - // Render error sample | |
| 97 | - $this->set_form_status( 'failed' ); | |
| 98 | - $this->render_messages(); | |
| 99 | - | |
| 100 | - // Reset status | |
| 101 | - $this->set_form_status( null ); | |
| 102 | - | |
| 103 | - } | |
| 104 | - | |
| 105 | - public function get_rendered_messages() { | |
| 106 | - ob_start(); | |
| 107 | - $this->render_messages(); | |
| 108 | - return ob_get_clean(); | |
| 109 | - } | |
| 110 | - | |
| 111 | - | |
| 112 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | + | |
| 4 | +namespace Jet_Form_Builder\Form_Messages; | |
| 5 | + | |
| 6 | +// If this file is called directly, abort. | |
| 7 | +use Jet_Form_Builder\Classes\Get_Template_Trait; | |
| 8 | + | |
| 9 | +if ( ! defined( 'WPINC' ) ) { | |
| 10 | + die; | |
| 11 | +} | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Form messages class | |
| 15 | + */ | |
| 16 | +class Builder { | |
| 17 | + | |
| 18 | + use Get_Template_Trait; | |
| 19 | + | |
| 20 | + private $status; | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * @var Message_Content_Processor | |
| 24 | + */ | |
| 25 | + private $content_processor; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Set form submittion status | |
| 29 | + * | |
| 30 | + * @param [type] $status [description] | |
| 31 | + * | |
| 32 | + * @return Builder | |
| 33 | + */ | |
| 34 | + public function set_form_status( $status ) { | |
| 35 | + $this->status = $status; | |
| 36 | + | |
| 37 | + return $this; | |
| 38 | + } | |
| 39 | + | |
| 40 | + public function get_manager( $data = array() ) { | |
| 41 | + return jet_form_builder()->msg_router->get_manager( $data ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Get form submitting status | |
| 46 | + */ | |
| 47 | + public function get_form_status() { | |
| 48 | + if ( ! $this->status ) { | |
| 49 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 50 | + $this->status = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : null; | |
| 51 | + } | |
| 52 | + | |
| 53 | + return $this->status; | |
| 54 | + } | |
| 55 | + | |
| 56 | + public function prepare_message_content( string $message ): string { | |
| 57 | + return $this->get_content_processor()->prepare_macros( | |
| 58 | + $message, | |
| 59 | + jet_fb_context()->get_request() | |
| 60 | + ); | |
| 61 | + } | |
| 62 | + | |
| 63 | + public function render_empty_field_message() { | |
| 64 | + $message_content = $this->get_content_processor()->prepare_trusted( | |
| 65 | + $this->get_manager()->get_message( 'empty_field' ), | |
| 66 | + jet_fb_context()->get_request() | |
| 67 | + ); | |
| 68 | + | |
| 69 | + include $this->get_global_template( 'common/field-message.php' ); | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Render form messages | |
| 74 | + * | |
| 75 | + * @return void | |
| 76 | + */ | |
| 77 | + public function render_messages() { | |
| 78 | + $status = $this->get_form_status(); | |
| 79 | + | |
| 80 | + if ( ! $status ) { | |
| 81 | + return; | |
| 82 | + } | |
| 83 | + | |
| 84 | + $info = new Status_Info( $status ); | |
| 85 | + $manager = $this->get_manager(); | |
| 86 | + $message = $manager->get_message_by_info( $info ); | |
| 87 | + | |
| 88 | + // Only a registered key resolves to a template stored in form/action settings. | |
| 89 | + if ( ! $info->is_dynamic() && $manager->isset_message_type( $info->get_message() ) ) { | |
| 90 | + $message_content = $this->get_content_processor()->prepare_trusted( | |
| 91 | + $message, | |
| 92 | + jet_fb_context()->get_request() | |
| 93 | + ); | |
| 94 | + } else { | |
| 95 | + $message_content = $this->get_content_processor()->prepare_untrusted( $message ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + $class = 'jet-form-builder-message'; | |
| 99 | + $class .= ' jet-form-builder-message--' . $info->get_css_class(); | |
| 100 | + | |
| 101 | + include $this->get_global_template( 'common/messages.php' ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Render message samples for editor | |
| 106 | + */ | |
| 107 | + public function render_messages_samples() { | |
| 108 | + // Render success sample | |
| 109 | + $this->set_form_status( 'success' ); | |
| 110 | + $this->render_messages(); | |
| 111 | + | |
| 112 | + // Render error sample | |
| 113 | + $this->set_form_status( 'failed' ); | |
| 114 | + $this->render_messages(); | |
| 115 | + | |
| 116 | + // Reset status | |
| 117 | + $this->set_form_status( null ); | |
| 118 | + } | |
| 119 | + | |
| 120 | + public function get_rendered_messages() { | |
| 121 | + ob_start(); | |
| 122 | + $this->render_messages(); | |
| 123 | + | |
| 124 | + return ob_get_clean(); | |
| 125 | + } | |
| 126 | + | |
| 127 | + public function get_content_processor(): Message_Content_Processor { | |
| 128 | + if ( is_null( $this->content_processor ) ) { | |
| 129 | + $this->content_processor = new Message_Content_Processor(); | |
| 130 | + } | |
| 131 | + | |
| 132 | + return $this->content_processor; | |
| 133 | + } | |
| 134 | + | |
| 135 | + | |
| 136 | +} | |