Skip to content

Commit 7ec9487

Browse files
feat: add subscribe notice in dashboard (#1139)
1 parent 5a09533 commit 7ec9487

4 files changed

Lines changed: 336 additions & 23 deletions

File tree

includes/admin/feedzy-rss-feeds-admin.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,7 @@ private function setup_wizard_subscribe_process() {
23132313
if ( ! is_wp_error( $request_res ) ) {
23142314
$body = json_decode( wp_remote_retrieve_body( $request_res ) );
23152315
if ( 'success' === $body->code ) {
2316+
update_option( 'feedzy_onboarding_user_subscribed', 'yes' );
23162317
$this->feedzy_dismiss_wizard( false );
23172318
wp_send_json( $response );
23182319
}
@@ -2330,6 +2331,74 @@ private function setup_wizard_subscribe_process() {
23302331
}
23312332
}
23322333

2334+
/**
2335+
* AJAX method to subscribe user to Feedzy newsletter via dashboard notice.
2336+
*
2337+
* @since 5.1.0
2338+
* @access public
2339+
* @return void
2340+
*/
2341+
public function feedzy_dashboard_subscribe() {
2342+
check_ajax_referer( 'feedzy_subscribe_nonce', '_wpnonce' );
2343+
2344+
$email = ! empty( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : '';
2345+
$skip = ! empty( $_POST['skip'] ) ? sanitize_text_field( wp_unslash( $_POST['skip'] ) ) : '';
2346+
2347+
if ( 'yes' === $skip ) {
2348+
$this->dismiss_subscribe_notice();
2349+
wp_send_json_success();
2350+
}
2351+
2352+
if ( empty( $email ) || ! is_email( $email ) ) {
2353+
wp_send_json_error(
2354+
array(
2355+
'message' => __( 'Validation failed', 'feedzy-rss-feeds' ),
2356+
)
2357+
);
2358+
}
2359+
2360+
update_option( 'feedzy_rss_feeds_logger_flag', 'yes' );
2361+
2362+
$request_res = wp_remote_post(
2363+
FEEDZY_SUBSCRIBE_API,
2364+
array(
2365+
'timeout' => 100, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
2366+
'headers' => array(
2367+
'Content-Type' => 'application/json',
2368+
),
2369+
'body' => wp_json_encode(
2370+
array(
2371+
'slug' => 'feedzy-rss-feeds',
2372+
'site' => home_url(),
2373+
'email' => $email,
2374+
'data' => array( 'source' => 'dashboard-notice' ),
2375+
)
2376+
),
2377+
)
2378+
);
2379+
2380+
if ( ! is_wp_error( $request_res ) ) {
2381+
$this->dismiss_subscribe_notice();
2382+
wp_send_json_success();
2383+
} else {
2384+
wp_send_json_error(
2385+
array(
2386+
'message' => $request_res->get_error_message(),
2387+
)
2388+
);
2389+
}
2390+
}
2391+
2392+
/**
2393+
* Dismiss subscribe notice.
2394+
*
2395+
* @since 5.1.0
2396+
* @return void
2397+
*/
2398+
public function dismiss_subscribe_notice() {
2399+
update_option( 'feedzy_dismiss_subscribe_notice_dashboard', 'yes' );
2400+
}
2401+
23332402
/**
23342403
* Create draft page.
23352404
*

includes/feedzy-rss-feeds.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ private function define_admin_hooks() {
204204
self::$instance->loader->add_action( 'current_screen', self::$instance->admin, 'handle_legacy' );
205205
self::$instance->loader->add_action( 'init', self::$instance->admin, 'register_settings' );
206206
self::$instance->loader->add_action( 'wp_ajax_feedzy_validate_feed', self::$instance->admin, 'validate_feed' );
207+
self::$instance->loader->add_action( 'wp_ajax_feedzy_dashboard_subscribe', self::$instance->admin, 'feedzy_dashboard_subscribe' );
207208

208209
// do not load this with the loader as this will need a corresponding remove_filter also.
209210
add_filter( 'update_post_metadata', array( self::$instance->admin, 'validate_category_feeds' ), 10, 5 );
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
<?php
2+
/**
3+
* Subscribe notice layout.
4+
*
5+
* @package Feedzy
6+
* @since 5.1.0
7+
*/
8+
9+
?>
10+
<div class="feedzy-container">
11+
<div class="feedzy-helper-notice" style="padding: 30px; position: relative">
12+
<button
13+
type="button"
14+
class="feedzy-notice-dismiss"
15+
aria-label="<?php esc_attr_e( 'Dismiss this notice', 'feedzy-rss-feeds' ); ?>"
16+
>
17+
&times;
18+
</button>
19+
20+
<h2 class="feedzy-notice-title">
21+
<?php esc_html_e( 'Welcome to Feedzy!', 'feedzy-rss-feeds' ); ?>
22+
</h2>
23+
24+
<p class="feedzy-notice-subtitle">
25+
<?php
26+
$users_number = number_format_i18n( 50000 );
27+
echo esc_html(
28+
sprintf(
29+
// translators: %s is the number of users.
30+
__( 'Join %s+ users aggregating RSS feeds effortlessly', 'feedzy-rss-feeds' ),
31+
$users_number
32+
)
33+
);
34+
?>
35+
</p>
36+
37+
<form id="feedzy-subscribe-form" method="post" action="">
38+
<?php wp_nonce_field( 'feedzy_subscribe_nonce', 'feedzy_subscribe_nonce_field' ); ?>
39+
40+
<input
41+
type="email"
42+
id="fz_subscribe_email"
43+
name="feedzy_email"
44+
value="<?php echo esc_attr( get_option( 'admin_email' ) ); ?>"
45+
class="feedzy-helper-subscribe"
46+
required
47+
>
48+
49+
<div class="mb-20">
50+
<div class="feedzy-helper-info">
51+
<?php esc_html_e( 'Get tips, updates & unlock exclusive guides', 'feedzy-rss-feeds' ); ?>
52+
</div>
53+
<div style="font-size: 16px;">
54+
<?php esc_html_e( 'Help improve Feedzy with anonymous usage insights', 'feedzy-rss-feeds' ); ?>
55+
</div>
56+
</div>
57+
58+
<button
59+
type="button"
60+
name="feedzy_subscribe_button"
61+
class="feedzy-subscribe"
62+
>
63+
<?php esc_html_e( 'Get Started', 'feedzy-rss-feeds' ); ?>
64+
</button>
65+
</form>
66+
67+
<div class="feedzy-notice-error"></div>
68+
</div>
69+
</div>
70+
<style>
71+
.feedzy-notice-dismiss {
72+
position: absolute;
73+
top: 10px;
74+
right: 10px;
75+
background: transparent;
76+
border: none;
77+
cursor: pointer;
78+
font-size: 24px;
79+
}
80+
81+
.feedzy-notice-title {
82+
font-size: clamp(28px, 5vw, 42px);
83+
margin: 0 0 10px 0;
84+
}
85+
86+
.feedzy-helper-notice .feedzy-notice-subtitle {
87+
font-size: 20px;
88+
margin-bottom: 20px;
89+
}
90+
91+
.feedzy-wrap .feedzy-helper-subscribe {
92+
width: 100%;
93+
max-width: 600px;
94+
padding: 15px;
95+
font-size: 16px;
96+
border: none;
97+
border-radius: 5px;
98+
margin-bottom: 20px;
99+
}
100+
101+
.feedzy-helper-info {
102+
font-size: 18px;
103+
font-weight: 600;
104+
margin-bottom: 8px;
105+
}
106+
107+
.feedzy-helper-notice .feedzy-subscribe{
108+
background: white;
109+
padding: 12px 30px;
110+
font-size: 18px;
111+
font-weight: 600;
112+
border: none;
113+
border-radius: 5px;
114+
cursor: pointer;
115+
}
116+
117+
.feedzy-notice-error {
118+
display: none;
119+
color: #d63638;
120+
margin-top: 10px;
121+
padding: 8px;
122+
background-color: #fcf0f1;
123+
border: 1px solid #d63638;
124+
border-radius: 4px;
125+
}
126+
</style>
127+
<script>
128+
document.addEventListener('DOMContentLoaded', () => {
129+
const showError = (message) => {
130+
const errorDiv = document.querySelector('.feedzy-notice-error');
131+
errorDiv.textContent = message;
132+
errorDiv.style.display = 'block';
133+
};
134+
135+
const hideError = () => {
136+
const errorDiv = document.querySelector('.feedzy-notice-error');
137+
errorDiv.style.display = 'none';
138+
errorDiv.textContent = '';
139+
};
140+
141+
const send = async (formData) => {
142+
return await fetch(window.ajaxurl, {
143+
method: 'POST',
144+
body: formData,
145+
credentials: 'same-origin'
146+
});
147+
}
148+
149+
const dismissNotice = async (button) => {
150+
const notice = button.closest('.feedzy-helper-notice');
151+
try {
152+
button.disabled = true;
153+
const nonceField = document.getElementById('feedzy_subscribe_nonce_field');
154+
155+
const formData = new FormData();
156+
formData.append('action', 'feedzy_dashboard_subscribe');
157+
formData.append('_wpnonce', nonceField.value);
158+
formData.append('skip', 'yes');
159+
160+
await send(formData);
161+
} catch (error) {
162+
console.error('Error dismissing notice:', error);
163+
} finally {
164+
button.disabled = false;
165+
jQuery(notice).fadeOut(400, function() {
166+
jQuery(this).remove();
167+
});
168+
}
169+
};
170+
171+
const handleSubscription = async (button) => {
172+
try {
173+
hideError();
174+
const subscriptionForm = document.getElementById('feedzy-subscribe-form');
175+
176+
if ( !subscriptionForm.checkValidity() ) {
177+
subscriptionForm.reportValidity();
178+
return;
179+
}
180+
181+
const emailElement = document.getElementById('fz_subscribe_email');
182+
const nonceField = document.getElementById('feedzy_subscribe_nonce_field');
183+
184+
button.disabled = true;
185+
186+
const formData = new FormData();
187+
formData.append('action', 'feedzy_dashboard_subscribe');
188+
formData.append('_wpnonce', nonceField.value);
189+
formData.append('with_subscribe', '1');
190+
formData.append('email', emailElement.value);
191+
192+
const response = await send(formData);
193+
const result = await response.json();
194+
195+
if (result.success) {
196+
const notice = document.querySelector('.feedzy-helper-notice');
197+
jQuery(notice).fadeOut(400, function() {
198+
jQuery(this).remove();
199+
});
200+
201+
if (result.data?.redirect_to) {
202+
window.location.href = result.data.redirect_to;
203+
}
204+
} else {
205+
const errorMessage = result.data?.message || 'Unknown';
206+
showError(errorMessage);
207+
}
208+
} catch (error) {
209+
showError(error);
210+
} finally {
211+
button.disabled = false;
212+
}
213+
};
214+
215+
const dismissButton = document.querySelector('.feedzy-notice-dismiss');
216+
const subscribeButton = document.querySelector('.feedzy-subscribe');
217+
218+
if (dismissButton) {
219+
dismissButton.addEventListener('click', (event) => {
220+
event.preventDefault();
221+
dismissNotice(event.target);
222+
});
223+
}
224+
225+
if (subscribeButton) {
226+
subscribeButton.addEventListener('click', (event) => {
227+
event.preventDefault();
228+
handleSubscription(event.target);
229+
});
230+
}
231+
});
232+
</script>

0 commit comments

Comments
 (0)