-
Notifications
You must be signed in to change notification settings - Fork 90
Elementor widgets #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iftakharul-islam
wants to merge
4
commits into
develop
Choose a base branch
from
elementor-widgets
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Elementor widgets #324
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8833f35
Add Elementor widgets and AJAX handlers
iftakharul-islam acf3a3e
Add Elementor widgets: nav, breadcrumb, menu
iftakharul-islam 6360a3b
Unify feedback meta keys and Elementor previews
iftakharul-islam d9f96a3
Add Elementor template import and fixes
iftakharul-islam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,18 @@ public function __construct() { | |
| // Handle load more for DocsGrid widget | ||
| add_action('wp_ajax_wedocs_load_more_docs', [$this, 'load_more_docs']); | ||
| add_action('wp_ajax_nopriv_wedocs_load_more_docs', [$this, 'load_more_docs']); | ||
|
|
||
| // Handle "Was This Helpful" votes | ||
| add_action('wp_ajax_wedocs_helpful_vote', [$this, 'handle_helpful_vote']); | ||
| add_action('wp_ajax_nopriv_wedocs_helpful_vote', [$this, 'handle_helpful_vote']); | ||
|
|
||
| // Handle "Was This Helpful" feedback | ||
| add_action('wp_ajax_wedocs_helpful_feedback', [$this, 'handle_helpful_feedback']); | ||
| add_action('wp_ajax_nopriv_wedocs_helpful_feedback', [$this, 'handle_helpful_feedback']); | ||
|
|
||
| // Handle "Need More Help" form submission | ||
| add_action('wp_ajax_wedocs_need_help_submit', [$this, 'handle_need_help_submit']); | ||
| add_action('wp_ajax_nopriv_wedocs_need_help_submit', [$this, 'handle_need_help_submit']); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -574,4 +586,176 @@ public function quick_search() { | |
| wp_send_json_success( $results ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handle "Was This Helpful" vote. | ||
| */ | ||
| public function handle_helpful_vote() { | ||
| check_ajax_referer('wedocs_helpful_vote', 'nonce'); | ||
|
|
||
| $post_id = intval($_POST['post_id'] ?? 0); | ||
| $vote = sanitize_text_field($_POST['vote'] ?? ''); | ||
|
|
||
| if (!$post_id || !in_array($vote, ['yes', 'no'], true)) { | ||
| wp_send_json_error(['message' => __('Invalid vote.', 'wedocs')]); | ||
| } | ||
|
|
||
| $meta_key = $vote === 'yes' ? 'positive' : 'negative'; | ||
| $current = (int) get_post_meta($post_id, $meta_key, true); | ||
| update_post_meta($post_id, $meta_key, $current + 1); | ||
|
|
||
| wp_send_json_success([ | ||
| 'yes' => (int) get_post_meta($post_id, 'positive', true), | ||
| 'no' => (int) get_post_meta($post_id, 'negative', true), | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Handle "Was This Helpful" negative feedback text. | ||
| */ | ||
| public function handle_helpful_feedback() { | ||
| check_ajax_referer('wedocs_helpful_vote', 'nonce'); | ||
|
|
||
| $post_id = intval($_POST['post_id'] ?? 0); | ||
| $feedback = sanitize_textarea_field($_POST['feedback'] ?? ''); | ||
|
|
||
| if (!$post_id || empty($feedback)) { | ||
| wp_send_json_error(['message' => __('Invalid feedback.', 'wedocs')]); | ||
| } | ||
|
|
||
| $existing = get_post_meta($post_id, '_wedocs_helpful_feedback', true); | ||
| if (!is_array($existing)) { | ||
| $existing = []; | ||
| } | ||
|
|
||
| $existing[] = [ | ||
| 'feedback' => $feedback, | ||
| 'date' => current_time('mysql'), | ||
| 'ip' => sanitize_text_field($_SERVER['REMOTE_ADDR'] ?? ''), | ||
| ]; | ||
|
|
||
| update_post_meta($post_id, '_wedocs_helpful_feedback', $existing); | ||
| wp_send_json_success(); | ||
| } | ||
|
Comment on lines
+616
to
+639
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add post type validation to prevent feedback on non-docs posts. Similar to 🛡️ Proposed fix $post_id = intval($_POST['post_id'] ?? 0);
$feedback = sanitize_textarea_field($_POST['feedback'] ?? '');
if (!$post_id || empty($feedback)) {
wp_send_json_error(['message' => __('Invalid feedback.', 'wedocs')]);
}
+ if (get_post_type($post_id) !== 'docs') {
+ wp_send_json_error(['message' => __('Invalid post type.', 'wedocs')]);
+ }
+
$existing = get_post_meta($post_id, '_wedocs_helpful_feedback', true);🤖 Prompt for AI Agents |
||
|
|
||
| /** | ||
| * Handle "Need More Help" contact form submission. | ||
| */ | ||
| public function handle_need_help_submit() { | ||
| $widget_id = sanitize_text_field($_POST['widget_id'] ?? ''); | ||
|
|
||
| if (!wp_verify_nonce($_POST['nonce'] ?? '', 'wedocs_need_help_' . $widget_id)) { | ||
| wp_send_json_error(['message' => __('Security check failed.', 'wedocs')]); | ||
| } | ||
|
|
||
| $name = sanitize_text_field($_POST['name'] ?? ''); | ||
| $email = sanitize_email($_POST['email'] ?? ''); | ||
| $subject = sanitize_text_field($_POST['subject'] ?? ''); | ||
| $message = sanitize_textarea_field($_POST['message'] ?? ''); | ||
| $page_url = esc_url_raw($_POST['page_url'] ?? ''); | ||
| $page_title = sanitize_text_field($_POST['page_title'] ?? ''); | ||
| $recipient = sanitize_email($_POST['recipient'] ?? get_option('admin_email')); | ||
| $save_to_elementor = sanitize_text_field($_POST['save_to_elementor'] ?? ''); | ||
| $post_id = intval($_POST['post_id'] ?? 0); | ||
|
|
||
| if (empty($message)) { | ||
| wp_send_json_error(['message' => __('Message is required.', 'wedocs')]); | ||
| } | ||
|
|
||
| if (empty($recipient) || !is_email($recipient)) { | ||
| $recipient = get_option('admin_email'); | ||
| } | ||
|
|
||
| // Send email | ||
| $email_subject = !empty($subject) ? $subject : sprintf(__('[weDocs] Support request from %s', 'wedocs'), $page_title); | ||
|
|
||
| $body = sprintf(__("Name: %s\n", 'wedocs'), $name ?: __('Not provided', 'wedocs')); | ||
| $body .= sprintf(__("Email: %s\n", 'wedocs'), $email ?: __('Not provided', 'wedocs')); | ||
| $body .= sprintf(__("Page: %s (%s)\n\n", 'wedocs'), $page_title, $page_url); | ||
| $body .= sprintf(__("Message:\n%s", 'wedocs'), $message); | ||
|
|
||
| $headers = ['Content-Type: text/plain; charset=UTF-8']; | ||
| if (!empty($email) && is_email($email)) { | ||
| $headers[] = 'Reply-To: ' . ($name ? "$name <$email>" : $email); | ||
| } | ||
|
|
||
| $sent = wp_mail($recipient, $email_subject, $body, $headers); | ||
|
|
||
| // Save to Elementor Pro submissions if enabled | ||
| if ($save_to_elementor === 'yes') { | ||
| $this->save_to_elementor_submissions($widget_id, $post_id, $page_url, $page_title, [ | ||
| 'name' => $name, | ||
| 'email' => $email, | ||
| 'subject' => $subject, | ||
| 'message' => $message, | ||
| ]); | ||
| } | ||
|
|
||
| if ($sent) { | ||
| wp_send_json_success(); | ||
| } else { | ||
| wp_send_json_error(['message' => __('Failed to send email. Please try again.', 'wedocs')]); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Save form data to Elementor Pro submissions table. | ||
| * | ||
| * @param string $widget_id The Elementor widget ID. | ||
| * @param int $post_id The post/page ID where the form was submitted. | ||
| * @param string $page_url The page URL (referer). | ||
| * @param string $page_title The page title. | ||
| * @param array $fields Associative array of field id => value. | ||
| */ | ||
| private function save_to_elementor_submissions($widget_id, $post_id, $page_url, $page_title, $fields) { | ||
| // Check if Elementor Pro submissions are available | ||
| if (!class_exists('\ElementorPro\Modules\Forms\Submissions\Database\Query')) { | ||
| return; | ||
| } | ||
|
|
||
| $query = \ElementorPro\Modules\Forms\Submissions\Database\Query::get_instance(); | ||
|
|
||
| $fields_data = []; | ||
| foreach ($fields as $id => $value) { | ||
| if (empty($value)) { | ||
| continue; | ||
| } | ||
|
|
||
| $type = 'text'; | ||
| if ($id === 'email') { | ||
| $type = 'email'; | ||
| } elseif ($id === 'message') { | ||
| $type = 'textarea'; | ||
| } | ||
|
|
||
| $fields_data[] = [ | ||
| 'id' => $id, | ||
| 'value' => $value, | ||
| 'type' => $type, | ||
| ]; | ||
| } | ||
|
|
||
| if (empty($fields_data)) { | ||
| return; | ||
| } | ||
|
|
||
| $submission_data = [ | ||
| 'post_id' => $post_id ?: 0, | ||
| 'referer' => $page_url, | ||
| 'referer_title' => $page_title, | ||
| 'element_id' => $widget_id, | ||
| 'form_name' => __('weDocs - Need More Help', 'wedocs'), | ||
| 'campaign_id' => 0, | ||
| 'user_id' => get_current_user_id() ?: null, | ||
| 'user_ip' => sanitize_text_field($_SERVER['REMOTE_ADDR'] ?? ''), | ||
| 'user_agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT'] ?? ''), | ||
| 'actions_count' => 1, | ||
| 'actions_succeeded_count' => 1, | ||
| 'meta' => wp_json_encode([ | ||
| 'edit_post_id' => $post_id ?: 0, | ||
| ]), | ||
| ]; | ||
|
|
||
| $query->add_submission($submission_data, $fields_data); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing duplicate vote prevention allows unlimited vote manipulation.
Unlike the existing
handle_helpful_feedback_vote()method (lines 434-481) which implements cookie-based tracking, user meta, and IP meta checks to prevent duplicate votes, this handler has no such protection. Any user can call this endpoint repeatedly to artificially inflate positive or negative counts.Additionally, there's no validation that
$post_idrefers to adocspost type (compare to line 417 in the existing handler).🔒 Proposed fix: Add duplicate vote prevention
public function handle_helpful_vote() { check_ajax_referer('wedocs_helpful_vote', 'nonce'); $post_id = intval($_POST['post_id'] ?? 0); $vote = sanitize_text_field($_POST['vote'] ?? ''); if (!$post_id || !in_array($vote, ['yes', 'no'], true)) { wp_send_json_error(['message' => __('Invalid vote.', 'wedocs')]); } + // Verify this is a docs post + if (get_post_type($post_id) !== 'docs') { + wp_send_json_error(['message' => __('Invalid post type.', 'wedocs')]); + } + + // Check for duplicate vote via cookie + $previous = isset($_COOKIE['wedocs_response']) ? explode(',', $_COOKIE['wedocs_response']) : []; + if (in_array($post_id, $previous, false)) { + wp_send_json_error([ + 'already_voted' => true, + 'message' => __('You have already voted on this article.', 'wedocs'), + ]); + } + $meta_key = $vote === 'yes' ? 'positive' : 'negative'; $current = (int) get_post_meta($post_id, $meta_key, true); update_post_meta($post_id, $meta_key, $current + 1); + // Set cookie to prevent duplicate votes + $previous[] = $post_id; + setcookie('wedocs_response', implode(',', $previous), time() + WEEK_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN); + wp_send_json_success([ 'yes' => (int) get_post_meta($post_id, 'positive', true), 'no' => (int) get_post_meta($post_id, 'negative', true), ]); }🤖 Prompt for AI Agents