-
Notifications
You must be signed in to change notification settings - Fork 26
feat: add subscribe notice in dashboard #1139
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5e073fb
add: dismissable notice in dashboard
RaduCristianPopescu a41a59b
fix: linter and phpstan
RaduCristianPopescu 13b88ee
fix: add translation
RaduCristianPopescu a5af910
chore: re-structure
Soare-Robert-Daniel 5d3a511
chore: review
Soare-Robert-Daniel 530e011
chore: format
Soare-Robert-Daniel e94c4bd
chore: format
Soare-Robert-Daniel 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2160,6 +2160,7 @@ private function setup_wizard_subscribe_process() { | |
| if ( ! is_wp_error( $request_res ) ) { | ||
| $body = json_decode( wp_remote_retrieve_body( $request_res ) ); | ||
| if ( 'success' === $body->code ) { | ||
| update_option( 'feedzy_onboarding_user_subscribed', 'yes' ); | ||
| $this->feedzy_dismiss_wizard( false ); | ||
| wp_send_json( $response ); | ||
| } | ||
|
|
@@ -2177,6 +2178,74 @@ private function setup_wizard_subscribe_process() { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * AJAX method to subscribe user to Feedzy newsletter via dashboard notice. | ||
| * | ||
| * @since 5.1.0 | ||
| * @access public | ||
| * @return void | ||
| */ | ||
| public function feedzy_dashboard_subscribe() { | ||
| check_ajax_referer( 'feedzy_subscribe_nonce', '_wpnonce' ); | ||
|
|
||
| $email = ! empty( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : ''; | ||
| $skip = ! empty( $_POST['skip'] ) ? sanitize_text_field( wp_unslash( $_POST['skip'] ) ) : ''; | ||
|
|
||
| if ( 'yes' === $skip ) { | ||
| $this->dismiss_subscribe_notice(); | ||
| wp_send_json_success(); | ||
| } | ||
|
|
||
| if ( empty( $email ) || ! is_email( $email ) ) { | ||
| wp_send_json_error( | ||
| array( | ||
| 'message' => __( 'Validation failed', 'feedzy-rss-feeds' ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| update_option( 'feedzy_rss_feeds_logger_flag', 'yes' ); | ||
|
|
||
| $request_res = wp_remote_post( | ||
| FEEDZY_SUBSCRIBE_API, | ||
| array( | ||
| 'timeout' => 100, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout | ||
| 'headers' => array( | ||
| 'Content-Type' => 'application/json', | ||
| ), | ||
| 'body' => wp_json_encode( | ||
| array( | ||
| 'slug' => 'feedzy-rss-feeds', | ||
| 'site' => home_url(), | ||
| 'email' => $email, | ||
| 'data' => array( 'source' => 'dashboard-notice' ), | ||
| ) | ||
| ), | ||
| ) | ||
| ); | ||
|
|
||
| if ( ! is_wp_error( $request_res ) ) { | ||
| $this->dismiss_subscribe_notice(); | ||
| wp_send_json_success(); | ||
| } else { | ||
| wp_send_json_error( | ||
| array( | ||
| 'message' => $request_res->get_error_message(), | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
||
|
|
||
| /** | ||
| * Dismiss subscribe notice. | ||
| * | ||
| * @since 5.1.0 | ||
| * @return void | ||
| */ | ||
| public function dismiss_subscribe_notice() { | ||
| update_option( 'feedzy_dismiss_subscribe_notice_dashboard', 'yes' ); | ||
| } | ||
|
|
||
| /** | ||
| * Create draft page. | ||
| * | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| <?php | ||
| /** | ||
| * Subscribe notice layout. | ||
| * | ||
| * @package Feedzy | ||
| * @since 5.1.0 | ||
| */ | ||
|
|
||
| ?> | ||
| <div class="feedzy-container"> | ||
| <div class="feedzy-helper-notice" style="padding: 30px; position: relative"> | ||
| <button | ||
| type="button" | ||
| class="feedzy-notice-dismiss" | ||
| aria-label="<?php esc_attr_e( 'Dismiss this notice', 'feedzy-rss-feeds' ); ?>" | ||
| > | ||
| × | ||
| </button> | ||
|
|
||
| <h2 class="feedzy-notice-title"> | ||
| <?php esc_html_e( 'Welcome to Feedzy!', 'feedzy-rss-feeds' ); ?> | ||
| </h2> | ||
|
|
||
| <p class="feedzy-notice-subtitle"> | ||
| <?php | ||
| $users_number = number_format_i18n( 50000 ); | ||
| echo esc_html( | ||
| sprintf( | ||
| // translators: %s is the number of users. | ||
| __( 'Join %s+ users aggregating RSS feeds effortlessly', 'feedzy-rss-feeds' ), | ||
| $users_number | ||
| ) | ||
| ); | ||
| ?> | ||
| </p> | ||
|
|
||
| <form id="feedzy-subscribe-form" method="post" action=""> | ||
| <?php wp_nonce_field( 'feedzy_subscribe_nonce', 'feedzy_subscribe_nonce_field' ); ?> | ||
|
|
||
| <input | ||
| type="email" | ||
| id="fz_subscribe_email" | ||
| name="feedzy_email" | ||
| value="<?php echo esc_attr( get_option( 'admin_email' ) ); ?>" | ||
| class="feedzy-helper-subscribe" | ||
| required | ||
| > | ||
|
|
||
| <div class="mb-20"> | ||
| <div class="feedzy-helper-info"> | ||
| <?php esc_html_e( 'Get tips, updates & unlock exclusive guides', 'feedzy-rss-feeds' ); ?> | ||
| </div> | ||
| <div style="font-size: 16px;"> | ||
| <?php esc_html_e( 'Help improve Feedzy with anonymous usage insights', 'feedzy-rss-feeds' ); ?> | ||
| </div> | ||
| </div> | ||
|
|
||
| <button | ||
| type="button" | ||
| name="feedzy_subscribe_button" | ||
| class="feedzy-subscribe" | ||
| > | ||
| <?php esc_html_e( 'Get Started', 'feedzy-rss-feeds' ); ?> → | ||
| </button> | ||
| </form> | ||
|
|
||
| <div class="feedzy-notice-error"></div> | ||
| </div> | ||
| </div> | ||
| <style> | ||
| .feedzy-notice-dismiss { | ||
| position: absolute; | ||
| top: 10px; | ||
| right: 10px; | ||
| background: transparent; | ||
| border: none; | ||
| cursor: pointer; | ||
| font-size: 24px; | ||
| } | ||
|
|
||
| .feedzy-notice-title { | ||
| font-size: clamp(28px, 5vw, 42px); | ||
| margin: 0 0 10px 0; | ||
| } | ||
|
|
||
| .feedzy-helper-notice .feedzy-notice-subtitle { | ||
| font-size: 20px; | ||
| margin-bottom: 20px; | ||
| } | ||
|
|
||
| .feedzy-wrap .feedzy-helper-subscribe { | ||
| width: 100%; | ||
| max-width: 600px; | ||
| padding: 15px; | ||
| font-size: 16px; | ||
| border: none; | ||
| border-radius: 5px; | ||
| margin-bottom: 20px; | ||
| } | ||
|
|
||
| .feedzy-helper-info { | ||
| font-size: 18px; | ||
| font-weight: 600; | ||
| margin-bottom: 8px; | ||
| } | ||
|
|
||
| .feedzy-helper-notice .feedzy-subscribe{ | ||
| background: white; | ||
| padding: 12px 30px; | ||
| font-size: 18px; | ||
| font-weight: 600; | ||
| border: none; | ||
| border-radius: 5px; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| .feedzy-notice-error { | ||
| display: none; | ||
| color: #d63638; | ||
| margin-top: 10px; | ||
| padding: 8px; | ||
| background-color: #fcf0f1; | ||
| border: 1px solid #d63638; | ||
| border-radius: 4px; | ||
| } | ||
| </style> | ||
| <script> | ||
| document.addEventListener('DOMContentLoaded', () => { | ||
| const showError = (message) => { | ||
| const errorDiv = document.querySelector('.feedzy-notice-error'); | ||
| errorDiv.textContent = message; | ||
| errorDiv.style.display = 'block'; | ||
| }; | ||
|
|
||
| const hideError = () => { | ||
| const errorDiv = document.querySelector('.feedzy-notice-error'); | ||
| errorDiv.style.display = 'none'; | ||
| errorDiv.textContent = ''; | ||
| }; | ||
|
|
||
| const send = async (formData) => { | ||
| return await fetch(window.ajaxurl, { | ||
| method: 'POST', | ||
| body: formData, | ||
| credentials: 'same-origin' | ||
| }); | ||
| } | ||
|
|
||
| const dismissNotice = async (button) => { | ||
| const notice = button.closest('.feedzy-helper-notice'); | ||
| try { | ||
| button.disabled = true; | ||
| const nonceField = document.getElementById('feedzy_subscribe_nonce_field'); | ||
|
|
||
| const formData = new FormData(); | ||
| formData.append('action', 'feedzy_dashboard_subscribe'); | ||
| formData.append('_wpnonce', nonceField.value); | ||
| formData.append('skip', 'yes'); | ||
|
|
||
| await send(formData); | ||
| } catch (error) { | ||
| console.error('Error dismissing notice:', error); | ||
| } finally { | ||
| button.disabled = false; | ||
| jQuery(notice).fadeOut(400, function() { | ||
| jQuery(this).remove(); | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| const handleSubscription = async (button) => { | ||
| try { | ||
| hideError(); | ||
| const subscriptionForm = document.getElementById('feedzy-subscribe-form'); | ||
|
|
||
| if ( !subscriptionForm.checkValidity() ) { | ||
| subscriptionForm.reportValidity(); | ||
| return; | ||
| } | ||
|
|
||
| const emailElement = document.getElementById('fz_subscribe_email'); | ||
| const nonceField = document.getElementById('feedzy_subscribe_nonce_field'); | ||
|
|
||
| button.disabled = true; | ||
|
|
||
| const formData = new FormData(); | ||
| formData.append('action', 'feedzy_dashboard_subscribe'); | ||
| formData.append('_wpnonce', nonceField.value); | ||
| formData.append('with_subscribe', '1'); | ||
| formData.append('email', emailElement.value); | ||
|
|
||
| const response = await send(formData); | ||
| const result = await response.json(); | ||
|
|
||
| if (result.success) { | ||
| const notice = document.querySelector('.feedzy-helper-notice'); | ||
| jQuery(notice).fadeOut(400, function() { | ||
| jQuery(this).remove(); | ||
| }); | ||
|
|
||
| if (result.data?.redirect_to) { | ||
| window.location.href = result.data.redirect_to; | ||
| } | ||
| } else { | ||
| const errorMessage = result.data?.message || 'Unknown'; | ||
| showError(errorMessage); | ||
| } | ||
| } catch (error) { | ||
| showError(error); | ||
| } finally { | ||
| button.disabled = false; | ||
| } | ||
| }; | ||
|
|
||
| const dismissButton = document.querySelector('.feedzy-notice-dismiss'); | ||
| const subscribeButton = document.querySelector('.feedzy-subscribe'); | ||
|
|
||
| if (dismissButton) { | ||
| dismissButton.addEventListener('click', (event) => { | ||
| event.preventDefault(); | ||
| dismissNotice(event.target); | ||
| }); | ||
| } | ||
|
|
||
| if (subscribeButton) { | ||
| subscribeButton.addEventListener('click', (event) => { | ||
| event.preventDefault(); | ||
| handleSubscription(event.target); | ||
| }); | ||
| } | ||
| }); | ||
| </script> |
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.
Direct access to $_POST without proper sanitization. Consider using sanitize_text_field() or wp_unslash() before casting to boolean for consistency with WordPress security practices.