Ajax()

Description #

A class to be used as a base for all ajax classes.

Changelog #

VersionDescription
4.1.8Introduced.

Source #

File: classes/ajax.php

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
abstract class Ajax extends \AnsPress\Singleton {
    /**
     * The response array.
     *
     * @var array
     */
    private $res = array();
 
    /**
     * The ajax action.
     *
     * @var string
     */
    public $action = '';
 
    /**
     * Ajax status.
     *
     * @var boolean
     */
    public $success = false;
 
    /**
     * The default nonce key.
     *
     * @var string
     */
    public $nonce_key = 'ap_default';
 
    /**
     * All request values.
     *
     * @var array
     */
    public $req;
 
    /**
     * Class constructor.
     */
    protected function __construct() {
        $this->set_action();
        $this->verify_nonce();
        $this->verify_permission();
 
        if ( is_user_logged_in() ) {
            $this->logged_in();
        } else {
            $this->nopriv();
        }
 
        $this->send();
    }
 
    /**
     * Set ajax action.
     */
    protected function set_action() {
        $this->action = 'ap_' . strtolower( ( new \ReflectionClass( $this ) )->getShortName() );
    }
 
    /**
     * Method for logged in users.
     *
     * @return void
     */
    public function logged_in() {
    }
 
    /**
     * Method for non logged in users.
     *
     * @return void
     */
    public function nopriv() {
    }
 
    /**
     * Add response key => value pair.
     *
     * @param string $key Key.
     * @param mixed  $val Value.
     * @return void
     */
    public function add_res( $key, $val ) {
        $this->res[ $key ] = $val;
    }
 
    /**
     * Verify nonce and die if failed.
     *
     * Nonce key is stored in `$nonce_key` property. To bypass verification
     * simply set `$nonce_key` to an empty value.
     *
     * @return void
     */
    private function verify_nonce() {
        if ( empty( $this->nonce_key ) ) {
            return;
        }
 
        $nonce = ap_sanitize_unslash( '__nonce', 'r' );
 
        // Verify nonce.
        if ( ! wp_verify_nonce( $nonce, $this->nonce_key ) ) {
            $this->snackbar( __( 'Trying to cheat?!', 'anspress-question-answer' ) );
            $this->send();
        }
    }
 
    /**
     * Verify permission of a user.
     *
     * By default this method need to override from child class.
     * If not overridden then ajax will always fail at this point.
     *
     * @return void
     */
    protected function verify_permission() {
        $this->set_fail();
        $this->snackbar( __( 'You don\'t have enough permissions to do this action.', 'anspress-question-answer' ) );
        $this->send();
    }
 
    /**
     * Set ajax as success.
     *
     * @return void
     */
    public function set_success() {
        $this->success = true;
    }
 
    /**
     * Set ajax as fail.
     *
     * @return void
     */
    public function set_fail() {
        $this->success = false;
    }
 
    /**
     * Add snackbar.
     *
     * @param string $msg Snackbar message.
     * @return void
     */
    public function snackbar( $msg ) {
        $this->res['snackbar'] = array(
            'message' => $msg,
        );
    }
 
    /**
     * Get and set request.
     *
     * @param string $key Request key.
     * @param mixed  $val Request value.
     * @return mixed
     */
    public function req( $key, $val = null ) {
        if ( null === $val && ! isset( $this->req[ $key ] ) ) {
            return;
        }
 
        if ( null === $val ) {
            return $this->req[ $key ];
        }
 
        $this->req[ $key ] = $val;
    }
 
    /**
     * Send ajax response.
     *
     * @return void
     */
    public function send() {
        $this->add_res( 'success', $this->success );
        $this->add_res( 'action', $this->action );
 
        // Add snackbar message by default if not success.
        if ( false === $this->success && empty( $this->res['snackbar'] ) ) {
            $this->snackbar( __( 'Something went wrong.', 'anspress-question-answer' ) );
        }
 
        ap_send_json( $this->res );
    }
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment