PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.32.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.32.1
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmReviews.php

FrmReviews.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.32.1, at classes/models/FrmReviews.php

258 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmReviews {
7
8 /**
9 * @var string
10 */
11 private $option_name = 'frm_reviewed';
12
13 /**
14 * @var array
15 */
16 private $review_status = array();
17
18 /**
19 * @var string
20 */
21 private $inbox_key = 'review';
22
23 /**
24 * Add admin notices as needed for reviews
25 *
26 * @since 3.04.03
27 *
28 * @return void
29 */
30 public function review_request() {
31 // Only show the review request to high-level users on Formidable pages
32 if ( ! current_user_can( 'frm_change_settings' ) || ! FrmAppHelper::is_formidable_admin() ) {
33 return;
34 }
35
36 // Verify that we can do a check for reviews
37 $this->set_review_status();
38
39 // Check if it has been dismissed or if we can ask later
40 $dismissed = $this->review_status['dismissed'];
41
42 if ( $dismissed === 'later' && $this->review_status['asked'] < 3 ) {
43 $dismissed = false;
44 }
45
46 $week_ago = $this->review_status['time'] + WEEK_IN_SECONDS <= time();
47
48 if ( ! $dismissed && $week_ago ) {
49 $this->review();
50 }
51 }
52
53 /**
54 * When was the review request last dismissed?
55 *
56 * @since 3.04.03
57 *
58 * @return void
59 */
60 private function set_review_status() {
61 $user_id = get_current_user_id();
62 $review = get_user_meta( $user_id, $this->option_name, true );
63 $default = array(
64 'time' => time(),
65 'dismissed' => false,
66 'asked' => 0,
67 );
68
69 if ( ! $review ) {
70 // Set the review request to show in a week
71 update_user_meta( $user_id, $this->option_name, $default );
72 }
73
74 $review = array_merge( $default, (array) $review );
75 $review['asked'] = (int) $review['asked'];
76 $this->review_status = $review;
77 }
78
79 /**
80 * Maybe show review request
81 *
82 * @since 3.04.03
83 *
84 * @return void
85 */
86 private function review() {
87 // Show the review request 3 times, depending on the number of entries
88 $show_intervals = array( 50, 200, 500 );
89 $asked = $this->review_status['asked'];
90
91 if ( ! isset( $show_intervals[ $asked ] ) ) {
92 return;
93 }
94
95 $entries = FrmEntry::getRecordCount();
96 $count = $show_intervals[ $asked ];
97 $user = wp_get_current_user();
98
99 // Only show review request if the site has collected enough entries
100 if ( $entries < $count ) {
101 // Check the entry count again in a week
102 $this->review_status['time'] = time();
103 update_user_meta( $user->ID, $this->option_name, $this->review_status );
104
105 return;
106 }
107
108 $entries = $entries <= 100 ? floor( $entries / 10 ) * 10 : floor( $entries / 50 ) * 50;
109 $name = $user->first_name;
110
111 if ( $name ) {
112 $name = ' ' . $name;
113 }
114
115 $title = sprintf(
116 /* translators: %1$d: number of entries */
117 esc_html__( 'You have collected %1$d form submissions.', 'formidable' ),
118 absint( $entries )
119 );
120
121 $this->add_to_inbox( $title, $name, $asked );
122
123 // We have a candidate! Output a review message.
124 include FrmAppHelper::plugin_path() . '/classes/views/shared/review.php';
125 }
126
127 /**
128 * @param string $title
129 * @param string $name
130 * @param int $asked
131 *
132 * @return void
133 */
134 private function add_to_inbox( $title, $name, $asked ) {
135 $message = new FrmInbox();
136 $requests = $message->get_messages();
137 $key = $this->inbox_key . ( $asked ? $asked : '' );
138
139 if ( isset( $requests[ $key ] ) ) {
140 return;
141 }
142
143 // Remove previous requests.
144 if ( $asked > 0 ) {
145 $message->remove( $this->inbox_key );
146 }
147
148 if ( $asked > 1 ) {
149 $message->remove( $this->inbox_key . '1' );
150 }
151
152 if ( $this->has_later_request( $requests, $asked ) ) {
153 // Don't add a request that has already been passed.
154 return;
155 }
156
157 $message->add_message(
158 array(
159 'key' => $key,
160 'message' => __( 'If you are enjoying Formidable, could you do me a BIG favor and give us a review to help me grow my little business and boost our motivation?', 'formidable' ) . '<br/>' . // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
161 '- Steph Wells<br/>' .
162 '<span>' . esc_html__( 'Co-Founder and CTO of Formidable Forms', 'formidable' ) . '<span>',
163 'subject' => str_replace( $name, '', $title ),
164 'cta' => '<a href="https://wordpress.org/support/plugin/formidable/reviews/?filter=5#new-post" class="frm-dismiss-review-notice frm-review-out button frm-button-secondary" data-link="yes" target="_blank" rel="noopener noreferrer">' . // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
165 esc_html__( 'Ok, you deserve it', 'formidable' ) . '</a>',
166 'type' => 'feedback',
167 )
168 );
169 }
170
171 /**
172 * If there are already later requests, don't add it to the inbox again.
173 *
174 * @since 4.05.02
175 *
176 * @param array $requests Array of requests.
177 * @param int $asked Number of times the review has been asked.
178 *
179 * @return bool
180 */
181 private function has_later_request( $requests, $asked ) {
182 return isset( $requests[ $this->inbox_key . ( $asked + 1 ) ] ) || isset( $requests[ $this->inbox_key . ( $asked + 2 ) ] );
183 }
184
185 /**
186 * @since 4.05.02
187 *
188 * @return array
189 */
190 private function inbox_keys() {
191 return array(
192 $this->inbox_key,
193 $this->inbox_key . '1',
194 $this->inbox_key . '2',
195 );
196 }
197
198 /**
199 * @since 4.05.01
200 *
201 * @return void
202 */
203 private function set_inbox_dismissed() {
204 $message = new FrmInbox();
205
206 foreach ( $this->inbox_keys() as $key ) {
207 $message->dismiss( $key );
208 }
209 }
210
211 /**
212 * @since 4.05.01
213 *
214 * @return void
215 */
216 private function set_inbox_read() {
217 $message = new FrmInbox();
218
219 foreach ( $this->inbox_keys() as $key ) {
220 $message->mark_read( $key );
221 }
222 }
223
224 /**
225 * Save the request to hide the review
226 *
227 * @since 3.04.03
228 *
229 * @return void
230 */
231 public function dismiss_review() {
232 FrmAppHelper::permission_check( 'frm_change_settings' );
233 check_ajax_referer( 'frm_ajax', 'nonce' );
234
235 $user_id = get_current_user_id();
236 $review = get_user_meta( $user_id, $this->option_name, true );
237
238 if ( ! $review ) {
239 $review = array();
240 }
241
242 if ( isset( $review['dismissed'] ) && $review['dismissed'] === 'done' ) {
243 // If feedback was submitted, don't update it again when the review is dismissed
244 $this->set_inbox_dismissed();
245 wp_die();
246 }
247
248 $dismissed = FrmAppHelper::get_post_param( 'link', 'no', 'sanitize_text_field' );
249 $review['time'] = time();
250 $review['dismissed'] = $dismissed === 'done' ? true : 'later';
251 $review['asked'] = isset( $review['asked'] ) ? $review['asked'] + 1 : 1;
252
253 update_user_meta( $user_id, $this->option_name, $review );
254 $this->set_inbox_read();
255 wp_die();
256 }
257 }
258