PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.2.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.2.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Shortcodes / FeedbackForm.php

FeedbackForm.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.2.0, at includes/Shortcodes/FeedbackForm.php

143 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Shortcodes;
4
5 use WPDeveloper\BetterDocs\Core\Query;
6 use WPDeveloper\BetterDocs\Utils\Helper;
7 use WPDeveloper\BetterDocs\Core\Settings;
8 use WPDeveloper\BetterDocs\Core\Shortcode;
9 use WPDeveloper\BetterDocs\Admin\Customizer\Defaults;
10
11 class FeedbackForm extends Shortcode {
12 protected $html_attributes = [];
13
14 public function __construct( Settings $settings, Query $query, Helper $helper, Defaults $defaults ) {
15 parent::__construct( $settings, $query, $helper, $defaults );
16
17 add_action( 'wp_ajax_nopriv_betterdocs_feedback_form_submit', [$this, 'submit'] );
18 add_action( 'wp_ajax_betterdocs_feedback_form_submit', [$this, 'submit'] );
19 }
20
21 public function get_style_depends() {
22 return ['betterdocs-feedback-form'];
23 }
24
25 public function get_script_depends() {
26 return ['betterdocs'];
27 }
28
29 public function submit() {
30 if ( ! betterdocs()->settings->get( 'email_feedback' ) ) {
31 wp_send_json_error( __( 'Email feedback is disabled.', 'betterdocs' ) );
32 }
33
34 check_ajax_referer( 'betterdocs_submit_data', 'security' );
35
36 $postID = isset( $_POST['postID'] ) ? $_POST['postID'] : null;
37 $article = get_the_title( $postID );
38 $name = isset( $_POST['message_name'] ) ? sanitize_text_field( stripslashes( $_POST['message_name'] ) ) : '';
39 $email = isset( $_POST['message_email'] ) ? sanitize_email( stripslashes( $_POST['message_email'] ) ) : '';
40 $message_text = isset( $_POST['message_text'] ) ? sanitize_textarea_field( stripslashes( $_POST['message_text'] ) ) : '';
41
42 $_errors = [];
43
44 //validate presence of name
45 if ( empty( $name ) ) {
46 $_errors['name'] = __( 'Please enter your name.', 'betterdocs' );
47 }
48
49 //validate email
50 if ( empty( $email ) || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
51 $_errors['email'] = __( 'Enter a valid email address.', 'betterdocs' );
52 }
53
54 //validate presence of message
55 if ( empty( $message_text ) ) {
56 $_errors['message'] = __( 'Please write your message.', 'betterdocs' );
57 }
58
59 if ( ! empty( $_errors ) ) {
60 $_errors = [
61 'success' => false,
62 'message' => $_errors
63 ];
64
65 wp_send_json_error( $_errors );
66 }
67
68 //php mailer variables
69 $to = $this->settings->get( 'email_address', get_option( 'admin_email' ) );
70 $subject = wp_sprintf( '%s %s', __( 'Feedback message from', 'betterdocs' ), get_bloginfo( 'name' ) );
71
72 $headers = [
73 'Content-Type: text/html; charset=UTF-8',
74 "From: $name <{$email}>",
75 "Reply-To: $email"
76 ];
77
78 $_response = [
79 'success' => true
80 ];
81
82 ob_start();
83 betterdocs()->views->get(
84 'admin/email/feedback-email',
85 [
86 'name' => $name,
87 'article' => $article,
88 'message_text' => $message_text
89 ]
90 );
91 $message = ob_get_clean();
92
93 // @todo: check strip tags: wp_mail( $to, $subject, strip_tags( $message ), $headers )
94
95 if ( wp_mail( $to, $subject, $message, $headers ) ) {
96 $_response['message'] = __( 'Thanks! Your message has been sent.', 'betterdocs' );
97 } else {
98 $_response['success'] = false;
99 $_response['message'] = __( 'Message was not sent. Try Again.', 'betterdocs' );
100 }
101
102 wp_send_json_success( $_response );
103 }
104
105 public function get_name() {
106 return 'betterdocs_feedback_form';
107 }
108
109 /**
110 * Summary of default_attributes
111 * @return array
112 */
113 public function default_attributes() {
114 return [
115 'button_text' => __( 'Send', 'betterdocs' ),
116 'feedback_form_name_label_text' => __( 'Name:', 'betterdocs' ),
117 'feedback_form_email_label_text' => __( 'Email:', 'betterdocs' ),
118 'feedback_form_message_label_text' => __( 'Message:', 'betterdocs' )
119 ];
120 }
121
122 public function render( $atts, $content = null ) {
123 $this->views( 'shortcodes/feedback-form' );
124 }
125
126 public function view_params() {
127 $params = $this->attributes;
128 $name = '';
129 $email = '';
130
131 if ( is_user_logged_in() ) {
132 $userdata = get_userdata( get_current_user_id() );
133 $name = $userdata->first_name . ' ' . $userdata->last_name;
134 $email = $userdata->user_email;
135 }
136
137 $params['name'] = $name;
138 $params['email'] = $email;
139
140 return $params;
141 }
142 }
143