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

249 lines 6.2 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: %s: User name, %2$d: number of entries */
114 esc_html__( 'Congratulations%1$s! You have collected %2$d form submissions.', 'formidable' ),
115 esc_html( $name ),
116 absint( $entries )
117 );
118
119 $this->add_to_inbox( $title, $name, $asked );
120
121 // We have a candidate! Output a review message.
122 include FrmAppHelper::plugin_path() . '/classes/views/shared/review.php';
123 }
124
125 /**
126 * @param string $title
127 * @param string $name
128 * @param int $asked
129 *
130 * @return void
131 */
132 private function add_to_inbox( $title, $name, $asked ) {
133 $message = new FrmInbox();
134 $requests = $message->get_messages();
135 $key = $this->inbox_key . ( $asked ? $asked : '' );
136
137 if ( isset( $requests[ $key ] ) ) {
138 return;
139 }
140
141 // Remove previous requests.
142 if ( $asked > 0 ) {
143 $message->remove( $this->inbox_key );
144 }
145 if ( $asked > 1 ) {
146 $message->remove( $this->inbox_key . '1' );
147 }
148
149 if ( $this->has_later_request( $requests, $asked ) ) {
150 // Don't add a request that has already been passed.
151 return;
152 }
153
154 $message->add_message(
155 array(
156 'key' => $key,
157 '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/>' .
158 '- Steph Wells<br/>' .
159 '<span>' . esc_html__( 'Co-Founder and CTO of Formidable Forms', 'formidable' ) . '<span>',
160 'subject' => str_replace( $name, '', $title ),
161 '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">' .
162 esc_html__( 'Ok, you deserve it', 'formidable' ) . '</a>',
163 'type' => 'feedback',
164 )
165 );
166 }
167
168 /**
169 * If there are already later requests, don't add it to the inbox again.
170 *
171 * @since 4.05.02
172 *
173 * @return bool
174 */
175 private function has_later_request( $requests, $asked ) {
176 return isset( $requests[ $this->inbox_key . ( $asked + 1 ) ] ) || isset( $requests[ $this->inbox_key . ( $asked + 2 ) ] );
177 }
178
179 /**
180 * @since 4.05.02
181 *
182 * @return array
183 */
184 private function inbox_keys() {
185 return array(
186 $this->inbox_key,
187 $this->inbox_key . '1',
188 $this->inbox_key . '2',
189 );
190 }
191
192 /**
193 * @since 4.05.01
194 *
195 * @return void
196 */
197 private function set_inbox_dismissed() {
198 $message = new FrmInbox();
199 foreach ( $this->inbox_keys() as $key ) {
200 $message->dismiss( $key );
201 }
202 }
203
204 /**
205 * @since 4.05.01
206 *
207 * @return void
208 */
209 private function set_inbox_read() {
210 $message = new FrmInbox();
211 foreach ( $this->inbox_keys() as $key ) {
212 $message->mark_read( $key );
213 }
214 }
215
216 /**
217 * Save the request to hide the review
218 *
219 * @since 3.04.03
220 *
221 * @return void
222 */
223 public function dismiss_review() {
224 FrmAppHelper::permission_check( 'frm_change_settings' );
225 check_ajax_referer( 'frm_ajax', 'nonce' );
226
227 $user_id = get_current_user_id();
228 $review = get_user_meta( $user_id, $this->option_name, true );
229 if ( empty( $review ) ) {
230 $review = array();
231 }
232
233 if ( isset( $review['dismissed'] ) && $review['dismissed'] === 'done' ) {
234 // if feedback was submitted, don't update it again when the review is dismissed
235 $this->set_inbox_dismissed();
236 wp_die();
237 }
238
239 $dismissed = FrmAppHelper::get_post_param( 'link', 'no', 'sanitize_text_field' );
240 $review['time'] = time();
241 $review['dismissed'] = $dismissed === 'done' ? true : 'later';
242 $review['asked'] = isset( $review['asked'] ) ? $review['asked'] + 1 : 1;
243
244 update_user_meta( $user_id, $this->option_name, $review );
245 $this->set_inbox_read();
246 wp_die();
247 }
248 }
249