Passed
Push — master ( 02fe7f...406f6c )
by
unknown
10:53
created

submit_feedback()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 0
loc 18
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * @author David Paternina
6
 */
7
class MonsterInsights_Feature_Feedback
8
{
9
    /**
10
     * Feedback registered features
11
     */
12
    const FEEDBACK_TRACKED_FEATURES = 'monsterinsights_feedback_tracked_features';
13
14
    /**
15
     * Feedback submitted
16
     */
17
    const FEEDBACK_SUBMITTED = 'monsterinsights_feedback_submitted';
18
19
    public function __construct()
20
    {
21
        $this->init_hooks();
22
    }
23
24
    /**
25
     * Init hooks
26
     * @return void
27
     */
28
    private function init_hooks()
29
    {
30
        //  Rest API
31
        add_action( 'rest_api_init', [$this, 'register_ajax_endpoints'] );
32
33
        //  Cron to clear expired feedback tracking
34
        add_action( 'monsterinsights_feature_feedback_clear_expired', [$this, 'clear_expired_feedback'] );
35
36
        if ( ! wp_next_scheduled( 'monsterinsights_feature_feedback_clear_expired' ) ) {
37
            wp_schedule_event( time(), 'weekly', 'monsterinsights_feature_feedback_clear_expired' );
38
        }
39
40
        //  Cron to send data to Relay
41
        add_action( 'monsterinsights_feature_feedback_checkin', [$this, 'feature_feedback_checkin'] );
42
43
        if ( ! wp_next_scheduled( 'monsterinsights_feature_feedback_checkin' ) ) {
44
            wp_schedule_event( time(), 'daily', 'monsterinsights_feature_feedback_checkin' );
45
        }
46
    }
47
48
    public static function get_settings()
49
    {
50
        $tracked_features = get_option( self::FEEDBACK_TRACKED_FEATURES, [] );
51
        return [
52
            'tracked_features' => $tracked_features
53
        ];
54
    }
55
56
    /**
57
     * Register the AJAX endpoints
58
     *
59
     * @return void
60
     */
61
    public function register_ajax_endpoints()
62
    {
63
        register_rest_route( 'monsterinsights/v1', '/feedback', array(
64
            'methods'               => WP_REST_Server::CREATABLE,
65
            'callback'              => [$this, 'submit_feedback'],
66
            'permission_callback'   => [$this, 'monsterinsights_permissions_callback'],
67
        ));
68
    }
69
70
    /**
71
     * Clear expired feedback
72
     * @return void
73
     */
74
    public function clear_expired_feedback()
75
    {
76
        $tracked_features = get_option( self::FEEDBACK_TRACKED_FEATURES, [] );
77
78
        $now = time();
79
80
        foreach ($tracked_features as $feature_key => $expires) {
81
            if ($now > $expires) {
82
                unset($tracked_features[$feature_key]);
83
            }
84
        }
85
86
        update_option( self::FEEDBACK_TRACKED_FEATURES, $tracked_features );
87
    }
88
89
    /**
90
     * Checkin to send feedback to Relay
91
     * @return void
92
     */
93
    public function feature_feedback_checkin()
94
    {
95
        $feedback = get_option( self::FEEDBACK_SUBMITTED, [] );
96
97
        if ( empty($feedback) ) {
98
            return;
99
        }
100
101
        $data = [];
102
103
        foreach ($feedback as $feature_key => $feedback_data) {
104
            $data[] = [
105
                'feature_key'   => $feature_key,
106
                'rating'        => $feedback_data['rating'],
107
                'feedback'      => $feedback_data['message'],
108
            ];
109
        }
110
111
        $api = new MonsterInsights_API_Request( 'feature-feedback', [], 'POST' );
112
113
        $result = $api->request([
114
            'feedback' => $data,
115
        ]);
116
117
        if ( !is_wp_error( $result ) ) {
118
            //  Clear option
119
            delete_option( self::FEEDBACK_SUBMITTED );
120
        }
121
    }
122
123
    /**
124
     * Process insights feedback
125
     * @param $request
126
     * @return WP_Error|WP_HTTP_Response|WP_REST_Response
127
     */
128
    public function submit_feedback($request )
129
    {
130
        if (empty($request['rating']) || empty($request['feature_key'])) {
131
            return new WP_Error('rest_invalid_param', 'Invalid parameter', ['status' => 400]);
132
        }
133
134
        //  Store feedback to send to Relay later
135
        $feature_key = $request['feature_key'];
136
        $feedback = [
137
            'rating'        => $request['rating'],
138
            'message'       => $request['message'],
139
        ];
140
141
        $this->save_feedback($feature_key, $feedback);
142
143
        //  Store to tracked features option
144
        $expires = time() + MONTH_IN_SECONDS;
145
        $this->add_tracked_feature($feature_key, $expires);
146
    }
147
148
    /**
149
     *
150
     * @param $feature_key
151
     * @param $feedback
152
     * @return void
153
     */
154
    private function save_feedback($feature_key, $feedback)
155
    {
156
        $stored_feedback = get_option( self::FEEDBACK_SUBMITTED, [] );
157
        $stored_feedback[$feature_key] = $feedback;
158
159
        update_option( self::FEEDBACK_SUBMITTED, $stored_feedback );
160
    }
161
162
    /**
163
     * @param $feature_key
164
     * @param $expires
165
     * @return void
166
     */
167
    private function add_tracked_feature($feature_key, $expires)
168
    {
169
        $tracked_features = get_option( self::FEEDBACK_TRACKED_FEATURES, [] );
170
        $tracked_features[$feature_key] = $expires;
171
172
        update_option( self::FEEDBACK_TRACKED_FEATURES, $tracked_features );
173
    }
174
175
    /**
176
     * Check if the user has the required permissions
177
     *
178
     * @return bool
179
     */
180
    public function monsterinsights_permissions_callback()
181
    {
182
        // Check if the user has the required permissions
183
        return current_user_can( 'monsterinsights_save_settings' );
184
    }
185
}
186
187
new MonsterInsights_Feature_Feedback();