Completed
Push — master ( 649509...5121e2 )
by Stephanie
03:07
created

FrmReviews::review_request()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 5
nop 0
dl 0
loc 22
rs 8.6346
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  = array();//get_user_meta( $user_id, $this->option_name, true );
45
		$default = array(
46
			'time'      => time() - WEEK_IN_SECONDS,
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
	 * @param int $asked
0 ignored issues
show
Bug introduced by
There is no parameter named $asked. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
66
	 */
67
	private function review() {
68
69
		// show the review request 3 times, depending on the number of entries
70
		$show_intervals = array( 50, 200, 500 );
71
		$asked = $this->review_status['asked'];
72
73
		if ( ! isset( $show_intervals[ $asked ] ) ) {
74
			return;
75
		}
76
77
		$entries = FrmEntry::getRecordCount();
78
		$count   = $show_intervals[ $asked ];
79
		$user    = wp_get_current_user();
80
81
		// Only show review request if the site has collected enough entries
82
		if ( $entries < $count ) {
83
			// check the entry count again in a week
84
			$this->review_status['time'] = time();
85
			update_user_meta( $user->ID, $this->option_name, $this->review_status );
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
		// We have a candidate! Output a review message.
102
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/review.php' );
103
	}
104
105
	/**
106
	 * Save the request to hide the review
107
	 *
108
	 * @since 3.04.03
109
	 */
110
	public function dismiss_review() {
111
		FrmAppHelper::permission_check( 'frm_change_settings' );
112
		check_ajax_referer( 'frm_ajax', 'nonce' );
113
114
		$user_id = get_current_user_id();
115
		$review  = get_user_meta( $user_id, $this->option_name, true );
116
		if ( empty( $review ) ) {
117
			$review = array();
118
		}
119
120
		$dismissed = FrmAppHelper::get_post_param( 'link', 'no', 'sanitize_text_field' );
121
		$review['time']      = time();
122
		$review['dismissed'] = $dismissed === 'done' ? true : 'later';
123
		$review['asked']     = isset( $review['asked'] ) ? $review['asked'] + 1 : 1;
124
125
		update_user_meta( $user_id, $this->option_name, $review );
126
		wp_die();
127
	}
128
}
129