PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.20
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.20
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.20, at classes/models/FrmReviews.php

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