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

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