Completed
Push — master ( 508f1a...b564aa )
by Stephanie
14s queued 10s
created

FrmReviews::review()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 0
dl 0
loc 47
rs 8.8452
c 0
b 0
f 0
1
<?php
2
3
class FrmReviews {
4
5
	private $option_name = 'frm_reviewed';
6
7
	private $review_status = array();
8
9
	/**
10
	 * Add admin notices as needed for reviews
11
	 *
12
	 * @since 3.04.03
13
	 */
14
	public function review_request() {
15
16
		// Only show the review request to high-level users on Formidable pages
17
		if ( ! current_user_can( 'frm_change_settings' ) || ! FrmAppHelper::is_formidable_admin() ) {
18
			return;
19
		}
20
21
		// Verify that we can do a check for reviews
22
		$this->set_review_status();
23
24
		// Check if it has been dismissed or if we can ask later
25
		$dismissed = $this->review_status['dismissed'];
26
		if ( $dismissed === 'later' && $this->review_status['asked'] < 3 ) {
27
			$dismissed = false;
28
		}
29
30
		$week_ago = ( $this->review_status['time'] + WEEK_IN_SECONDS ) <= time();
31
32
		if ( empty( $dismissed ) && $week_ago ) {
33
			$this->review();
34
		}
35
	}
36
37
	/**
38
	 * When was the review request last dismissed?
39
	 *
40
	 * @since 3.04.03
41
	 */
42
	private function set_review_status() {
43
		$user_id = get_current_user_id();
44
		$review  = get_user_meta( $user_id, $this->option_name, true );
45
		$default = array(
46
			'time'      => time(),
47
			'dismissed' => false,
48
			'asked'     => 0,
49
		);
50
51
		if ( empty( $review ) ) {
52
			// Set the review request to show in a week
53
			update_user_meta( $user_id, $this->option_name, $default );
54
		}
55
56
		$review              = array_merge( $default, (array) $review );
57
		$review['asked']     = (int) $review['asked'];
58
		$this->review_status = $review;
59
	}
60
61
	/**
62
	 * Maybe show review request
63
	 *
64
	 * @since 3.04.03
65
	 */
66
	private function review() {
67
68
		// show the review request 3 times, depending on the number of entries
69
		$show_intervals = array( 50, 200, 500 );
70
		$asked          = $this->review_status['asked'];
71
72
		if ( ! isset( $show_intervals[ $asked ] ) ) {
73
			return;
74
		}
75
76
		$entries = FrmEntry::getRecordCount();
77
		$count   = $show_intervals[ $asked ];
78
		$user    = wp_get_current_user();
79
80
		// Only show review request if the site has collected enough entries
81
		if ( $entries < $count ) {
82
			// check the entry count again in a week
83
			$this->review_status['time'] = time();
84
			update_user_meta( $user->ID, $this->option_name, $this->review_status );
85
86
			return;
87
		}
88
89
		if ( $entries <= 100 ) {
90
			// round to the nearest 10
91
			$entries = floor( $entries / 10 ) * 10;
92
		} else {
93
			// round to the nearest 50
94
			$entries = floor( $entries / 50 ) * 50;
95
		}
96
		$name = $user->first_name;
97
		if ( ! empty( $name ) ) {
98
			$name = ' ' . $name;
99
		}
100
101
		$title = sprintf(
102
			/* translators: %s: User name, %2$d: number of entries */
103
			esc_html__( 'Congratulations%1$s! You have collected %2$d form submissions.', 'formidable' ),
104
			esc_html( $name ),
105
			absint( $entries )
106
		);
107
108
		$this->add_to_inbox( $title );
109
110
		// We have a candidate! Output a review message.
111
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/review.php' );
112
	}
113
114
	private function add_to_inbox( $title ) {
115
		$message = new FrmInbox();
116
		$message->add_message(
117
			array(
118
				'key'     => 'review',
119
				'force'   => true,
120
				'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/>' .
121
					'- Steph Wells<br/>' .
122
					'<span>' . esc_html__( 'Founder and Lead Developer of Formidable Forms', 'formidable' ) . '<span>',
123
				'subject' => $title,
124
				'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">' .
125
					esc_html__( 'Ok, you deserve it', 'formidable' ) . '</a>',
126
			)
127
		);
128
	}
129
130
	/**
131
	 * Save the request to hide the review
132
	 *
133
	 * @since 3.04.03
134
	 */
135
	public function dismiss_review() {
136
		FrmAppHelper::permission_check( 'frm_change_settings' );
137
		check_ajax_referer( 'frm_ajax', 'nonce' );
138
139
		$user_id = get_current_user_id();
140
		$review  = get_user_meta( $user_id, $this->option_name, true );
141
		if ( empty( $review ) ) {
142
			$review = array();
143
		}
144
145
		if ( isset( $review['dismissed'] ) && $review['dismissed'] === 'done' ) {
146
			// if feedback was submitted, don't update it again when the review is dismissed
147
			wp_die();
148
		}
149
150
		$dismissed           = FrmAppHelper::get_post_param( 'link', 'no', 'sanitize_text_field' );
151
		$review['time']      = time();
152
		$review['dismissed'] = $dismissed === 'done' ? true : 'later';
153
		$review['asked']     = isset( $review['asked'] ) ? $review['asked'] + 1 : 1;
154
155
		update_user_meta( $user_id, $this->option_name, $review );
156
		wp_die();
157
	}
158
}
159