PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.8.9
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.8.9
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 3.8.9, at includes/Shortcodes/FeedbackForm.php

139 lines 4.5 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 check_ajax_referer( 'betterdocs_submit_data', 'security' );
31
32 $postID = isset( $_POST['postID'] ) ? $_POST['postID'] : null;
33 $article = get_the_title( $postID );
34 $name = isset( $_POST['message_name'] ) ? sanitize_text_field( stripslashes( $_POST['message_name'] ) ) : '';
35 $email = isset( $_POST['message_email'] ) ? sanitize_email( stripslashes( $_POST['message_email'] ) ) : '';
36 $message_text = isset( $_POST['message_text'] ) ? sanitize_textarea_field( stripslashes( $_POST['message_text'] ) ) : '';
37
38 $_errors = [];
39
40 //validate presence of name
41 if ( empty( $name ) ) {
42 $_errors['name'] = __( 'Please enter your name.', 'betterdocs' );
43 }
44
45 //validate email
46 if ( empty( $email ) || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
47 $_errors['email'] = __( 'Enter a valid email address.', 'betterdocs' );
48 }
49
50 //validate presence of message
51 if ( empty( $message_text ) ) {
52 $_errors['message'] = __( 'Please write your message.', 'betterdocs' );
53 }
54
55 if ( ! empty( $_errors ) ) {
56 $_errors = [
57 'success' => false,
58 'message' => $_errors
59 ];
60
61 wp_send_json_error( $_errors );
62 }
63
64 //php mailer variables
65 $to = $this->settings->get( 'email_address', get_option( 'admin_email' ) );
66 $subject = wp_sprintf( '%s %s', __( 'Feedback message from', 'betterdocs' ), get_bloginfo( 'name' ) );
67
68 $headers = [
69 'Content-Type: text/html; charset=UTF-8',
70 "From: $name <{$email}>",
71 "Reply-To: $email"
72 ];
73
74 $_response = [
75 'success' => true
76 ];
77
78 ob_start();
79 betterdocs()->views->get(
80 'admin/email/feedback-email',
81 [
82 'name' => $name,
83 'article' => $article,
84 'message_text' => $message_text
85 ]
86 );
87 $message = ob_get_clean();
88
89 // @todo: check strip tags: wp_mail( $to, $subject, strip_tags( $message ), $headers )
90
91 if ( wp_mail( $to, $subject, $message, $headers ) ) {
92 $_response['message'] = __( 'Thanks! Your message has been sent.', 'betterdocs' );
93 } else {
94 $_response['success'] = false;
95 $_response['message'] = __( 'Message was not sent. Try Again.', 'betterdocs' );
96 }
97
98 wp_send_json_success( $_response );
99 }
100
101 public function get_name() {
102 return 'betterdocs_feedback_form';
103 }
104
105 /**
106 * Summary of default_attributes
107 * @return array
108 */
109 public function default_attributes() {
110 return [
111 'button_text' => __( 'Send', 'betterdocs' ),
112 'feedback_form_name_label_text' => __( 'Name:', 'betterdocs' ),
113 'feedback_form_email_label_text' => __( 'Email:', 'betterdocs' ),
114 'feedback_form_message_label_text' => __( 'Message:', 'betterdocs' )
115 ];
116 }
117
118 public function render( $atts, $content = null ) {
119 $this->views( 'shortcodes/feedback-form' );
120 }
121
122 public function view_params() {
123 $params = $this->attributes;
124 $name = '';
125 $email = '';
126
127 if ( is_user_logged_in() ) {
128 $userdata = get_userdata( get_current_user_id() );
129 $name = $userdata->first_name . ' ' . $userdata->last_name;
130 $email = $userdata->user_email;
131 }
132
133 $params['name'] = $name;
134 $params['email'] = $email;
135
136 return $params;
137 }
138 }
139