PluginProbe
JSON API User / 3.0.0
JSON API User v3.0.0
4.1.4 3.9.8 3.9.9 4.0.0 4.1.0 4.1.2 4.1.3 2.8 2.9 2.9.1 3.0.0 3.1.4 3.2.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 3.8.0 3.8.1 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 All 52 releases
json-api-user / controllers / User.php

User.php in JSON API User 3.0.0, at controllers/User.php

890 lines 23.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 Controller name: User
5 Controller description: User Registration, Authentication, User Info, User Meta, FB Login, BuddyPress xProfile Fields methods
6 Controller Author: Ali Qureshi
7 Controller Author Twitter: @parorrey
8 Controller Author Website: parorrey.com
9
10 */
11 class JSON_API_User_Controller {
12
13 /**
14 * Returns an Array with registered userid & valid cookie
15 * @param String username: username to register
16 * @param String email: email address for user registration
17 * @param String user_pass: user_pass to be set (optional)
18 * @param String display_name: display_name for user
19 */
20 public function __construct() {
21 global $json_api;
22 // allow only connection over https. because, well, you care about your passwords and sniffing.
23 // turn this sanity-check off if you feel safe inside your localhost or intranet.
24 // send an extra POST parameter: insecure=cool
25 if (empty($_SERVER['HTTPS']) ||
26 (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'off')) {
27 if (empty($_REQUEST['insecure']) || $_REQUEST['insecure'] != 'cool') {
28 $json_api->error("SSL is not enabled. Either use _https_ or provide 'insecure' var as insecure=cool to confirm you want to use http protocol.");
29 }
30 }
31
32
33 }
34
35 public function info(){
36
37 global $json_api;
38
39 return array(
40 "version" => JAU_VERSION
41 );
42
43 }
44
45 public function register(){
46
47 global $json_api;
48
49
50 if (!get_option('users_can_register')) {
51 $json_api->error("User registration is disabled. Please enable it in Settings > Gereral.");
52 }
53
54 if (!$json_api->query->username) {
55 $json_api->error("You must include 'username' var in your request. ");
56 }
57 else $username = sanitize_user( $json_api->query->username );
58
59
60 if (!$json_api->query->email) {
61 $json_api->error("You must include 'email' var in your request. ");
62 }
63 else $email = sanitize_email( $json_api->query->email );
64
65 if (!$json_api->query->nonce) {
66 $json_api->error("You must include 'nonce' var in your request. Use the 'get_nonce' Core API method. ");
67 }
68 else $nonce = sanitize_text_field( $json_api->query->nonce ) ;
69
70 if (!$json_api->query->display_name) {
71 $json_api->error("You must include 'display_name' var in your request. ");
72 }
73 else $display_name = sanitize_text_field( $json_api->query->display_name );
74
75 $user_pass = sanitize_text_field( $_REQUEST['user_pass'] );
76
77 if ($json_api->query->seconds) $seconds = (int) $json_api->query->seconds;
78
79 else $seconds = 1209600;//14 days
80
81 //Add usernames we don't want used
82
83 $invalid_usernames = array( 'admin' );
84
85 //Do username validation
86
87 $nonce_id = $json_api->get_nonce_id('user', 'register');
88
89 if( !wp_verify_nonce($json_api->query->nonce, $nonce_id) ) {
90
91 $json_api->error("Invalid access, unverifiable 'nonce' value. Use the 'get_nonce' Core API method. ");
92 }
93
94 else {
95
96 if ( !validate_username( $username ) || in_array( $username, $invalid_usernames ) ) {
97
98 $json_api->error("Username is invalid.");
99
100 }
101
102 elseif ( username_exists( $username ) ) {
103
104 $json_api->error("Username already exists.");
105
106 }
107
108 else{
109
110
111 if ( !is_email( $email ) ) {
112 $json_api->error("E-mail address is invalid.");
113 }
114 elseif (email_exists($email)) {
115
116 $json_api->error("E-mail address is already in use.");
117
118 }
119
120 else {
121
122 //Everything has been validated, proceed with creating the user
123
124 //Create the user
125
126 if( !isset($_REQUEST['user_pass']) ) {
127 $user_pass = wp_generate_password();
128 $_REQUEST['user_pass'] = $user_pass;
129 }
130
131 $_REQUEST['user_login'] = $username;
132 $_REQUEST['user_email'] = $email;
133
134 $allowed_params = array('user_login', 'user_email', 'user_pass', 'display_name', 'user_nicename', 'user_url', 'nickname', 'first_name',
135 'last_name', 'description', 'rich_editing', 'user_registered', 'role', 'jabber', 'aim', 'yim',
136 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front'
137 );
138
139
140 foreach($_REQUEST as $field => $value){
141
142 if( in_array($field, $allowed_params) ) $user[$field] = trim(sanitize_text_field($value));
143
144 }
145 $user['role'] = get_option('default_role');
146 $user_id = wp_insert_user( $user );
147
148 /*Send e-mail to admin and new user -
149 You could create your own e-mail instead of using this function*/
150
151 if( isset($_REQUEST['user_pass']) && $_REQUEST['notify']=='no') {
152 $notify = '';
153 }elseif($_REQUEST['notify']!='no') $notify = $_REQUEST['notify'];
154
155
156 if($user_id) wp_new_user_notification( $user_id, '',$notify );
157
158
159 }
160 }
161 }
162
163
164 $expiration = time() + apply_filters('auth_cookie_expiration', $seconds, $user_id, true);
165
166 $cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
167
168 return array(
169 "cookie" => $cookie,
170 "user_id" => $user_id
171 );
172
173 }
174
175 public function get_avatar(){
176
177 global $json_api;
178
179 if (function_exists('bp_is_active')) {
180
181 if (!$json_api->query->user_id) {
182 $json_api->error("You must include 'user_id' var in your request. ");
183 }
184
185 if (!$json_api->query->type) {
186 $json_api->error("You must include 'type' var in your request. possible values 'full' or 'thumb' ");
187 }
188
189
190 $avatar = bp_core_fetch_avatar ( array( 'item_id' => $json_api->query->user_id, 'type' => $json_api->query->type, 'html'=>false ));
191
192 return array('avatar'=>$avatar);
193 } else {
194
195 $json_api->error("You must install and activate BuddyPress plugin to use this method.");
196
197 }
198
199 }
200
201 public function get_userinfo(){
202
203 global $json_api;
204
205 if (!$json_api->query->user_id) {
206 $json_api->error("You must include 'user_id' var in your request. ");
207 }
208
209 $user = get_userdata($json_api->query->user_id);
210
211 preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);
212
213 return array(
214 "id" => $user->ID,
215 //"username" => $user->user_login,
216 "nicename" => $user->user_nicename,
217 //"email" => $user->user_email,
218 "url" => $user->user_url,
219 "displayname" => $user->display_name,
220 "firstname" => $user->user_firstname,
221 "lastname" => $user->last_name,
222 "nickname" => $user->nickname,
223 "avatar" => $avatar[1]
224 );
225
226 }
227
228 public function retrieve_password(){
229
230 global $wpdb, $json_api, $wp_hasher;
231
232 if (!$json_api->query->user_login) {
233
234 $json_api->error("You must include 'user_login' var in your request. ");
235
236 }
237
238 $user_login = $json_api->query->user_login;
239
240 if ( strpos( $user_login, '@' ) ) {
241
242 $user_data = get_user_by( 'email', trim( $user_login ) );
243
244 if ( empty( $user_data ) )
245
246
247
248 $json_api->error("Your email address not found! ");
249
250
251
252 } else {
253
254 $login = trim($user_login);
255
256 $user_data = get_user_by('login', $login);
257
258 }
259
260
261
262 // redefining user_login ensures we return the right case in the email
263
264 $user_login = $user_data->user_login;
265
266 $user_email = $user_data->user_email;
267
268
269 do_action('retrieve_password', $user_login);
270
271
272 $allow = apply_filters('allow_password_reset', true, $user_data->ID);
273
274 if ( ! $allow ) $json_api->error("password reset not allowed! ");
275
276 elseif ( is_wp_error($allow) ) $json_api->error("An error occured! ");
277
278
279
280 $key = wp_generate_password( 20, false );
281
282 do_action( 'retrieve_password_key', $user_login, $key );
283
284
285
286 if ( empty( $wp_hasher ) ) {
287
288 require_once ABSPATH . 'wp-includes/class-phpass.php';
289
290 $wp_hasher = new PasswordHash( 8, true );
291
292 }
293
294
295 $hashed = time() . ':' . $wp_hasher->HashPassword( $key );
296
297 $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user_login ) );
298
299 $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
300
301 $message .= network_home_url( '/' ) . "\r\n\r\n";
302
303 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
304
305 $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
306
307 $message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
308
309 $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";
310
311
312
313 if ( is_multisite() )
314
315 $blogname = $GLOBALS['current_site']->site_name;
316
317 else
318
319 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
320
321
322
323 $title = sprintf( __('[%s] Password Reset'), $blogname );
324
325
326
327 $title = apply_filters('retrieve_password_title', $title);
328
329 $message = apply_filters('retrieve_password_message', $message, $key);
330
331
332
333 if ( $message && !wp_mail($user_email, $title, $message) )
334
335 $json_api->error("The e-mail could not be sent. Possible reason: your host may have disabled the mail() function...");
336
337 else
338
339 return array(
340
341 "msg" => 'Link for password reset has been emailed to you. Please check your email.',
342
343 );
344
345 }
346
347 public function validate_auth_cookie() {
348
349 global $json_api;
350
351 if (!$json_api->query->cookie) {
352
353 $json_api->error("You must include a 'cookie' authentication cookie. Use the `create_auth_cookie` method.");
354
355 }
356
357 $valid = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in') ? true : false;
358
359 return array(
360
361 "valid" => $valid
362
363 );
364
365 }
366
367 public function generate_auth_cookie() {
368
369 global $json_api;
370
371 foreach($_POST as $k=>$val) {
372 if (isset($_POST[$k])) {
373 $json_api->query->$k = $val;
374 }
375 }
376
377
378 if (!$json_api->query->username && !$json_api->query->email) {
379
380 $json_api->error("You must include 'username' or 'email' var in your request to generate cookie.");
381
382 }
383
384
385 if (!$json_api->query->password) {
386
387 $json_api->error("You must include a 'password' var in your request.");
388
389 }
390
391 if ($json_api->query->seconds) $seconds = (int) $json_api->query->seconds;
392
393 else $seconds = 1209600;//14 days
394
395 if ( $json_api->query->email ) {
396
397
398 if ( is_email( $json_api->query->email ) ) {
399 if( !email_exists( $json_api->query->email)) {
400 $json_api->error("email does not exist.");
401 }
402 }else $json_api->error("Invalid email address.");
403
404 $user_obj = get_user_by( 'email', $json_api->query->email );
405
406
407 $user = wp_authenticate($user_obj->data->user_login, $json_api->query->password);
408 }else {
409
410 $user = wp_authenticate($json_api->query->username, $json_api->query->password);
411 }
412
413
414 if (is_wp_error($user)) {
415
416 $json_api->error("Invalid username/email and/or password.", 'error', '401');
417
418 remove_action('wp_login_failed', $json_api->query->username);
419
420 }
421
422
423 $expiration = time() + apply_filters('auth_cookie_expiration', $seconds, $user->ID, true);
424
425 $cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in');
426
427 preg_match('|src="(.+?)"|', get_avatar( $user->ID, 512 ), $avatar);
428
429 return array(
430 "cookie" => $cookie,
431 "cookie_name" => LOGGED_IN_COOKIE,
432 "user" => array(
433 "id" => $user->ID,
434 "username" => $user->user_login,
435 "nicename" => $user->user_nicename,
436 "email" => $user->user_email,
437 "url" => $user->user_url,
438 "registered" => $user->user_registered,
439 "displayname" => $user->display_name,
440 "firstname" => $user->user_firstname,
441 "lastname" => $user->last_name,
442 "nickname" => $user->nickname,
443 "description" => $user->user_description,
444 "capabilities" => $user->wp_capabilities,
445 "avatar" => $avatar[1]
446
447 ),
448 );
449 }
450
451 public function get_currentuserinfo() {
452
453 global $json_api;
454
455 if (!$json_api->query->cookie) {
456
457 $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` Auth API method.");
458
459 }
460
461 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
462
463
464 if (!$user_id) {
465 $json_api->error("Invalid authentication cookie. Use the `generate_auth_cookie` method.");
466 }
467
468 $user = get_userdata($user_id);
469
470 preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);
471
472
473
474 return array(
475
476 "user" => array(
477
478 "id" => $user->ID,
479
480 "username" => $user->user_login,
481
482 "nicename" => $user->user_nicename,
483
484 "email" => $user->user_email,
485
486 "url" => $user->user_url,
487
488 "registered" => $user->user_registered,
489
490 "displayname" => $user->display_name,
491
492 "firstname" => $user->user_firstname,
493
494 "lastname" => $user->last_name,
495
496 "nickname" => $user->nickname,
497
498 "description" => $user->user_description,
499
500 "capabilities" => $user->wp_capabilities,
501
502 "avatar" => $avatar[1]
503
504 )
505
506 );
507
508 }
509
510 public function get_user_meta() {
511
512 global $json_api;
513
514 if (!$json_api->query->cookie) {
515 $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method.");
516 }
517
518 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
519
520 if (!$user_id) $json_api->error("Invalid cookie. Use the `generate_auth_cookie` method.");
521
522 $meta_key = sanitize_text_field($json_api->query->meta_key);
523
524
525 if($meta_key) $data[$meta_key] = get_user_meta( $user_id, $meta_key);
526 else {
527 // Get all user meta data for $user_id
528 $meta = get_user_meta( $user_id );
529
530 // Filter out empty meta data
531 $data = array_filter( array_map( function( $a ) {
532 return $a[0];
533 }, $meta ) );
534
535 }
536 //d($data);
537 return $data;
538
539
540 }
541
542 public function update_user_meta() {
543
544 global $json_api;
545
546 if (!$json_api->query->cookie) {
547 $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method.");
548 }
549
550 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
551
552 if (!$user_id) $json_api->error("Invalid cookie. Use the `generate_auth_cookie` method.");
553
554
555 if (!$json_api->query->meta_key) $json_api->error("You must include a 'meta_key' var in your request.");
556
557 else $meta_key = $json_api->query->meta_key;
558
559 if (!$json_api->query->meta_value) {
560 $json_api->error("You must include a 'meta_value' var in your request. You may provide multiple values separated by comma for 'meta_value' var.");
561 }
562 else $meta_value = sanitize_text_field($json_api->query->meta_value);
563
564 if( strpos($meta_value,',') !== false ) {
565 $meta_values = explode(",", $meta_value);
566 $meta_values = array_map('trim',$meta_values);
567
568 $data['updated'] = update_user_meta( $user_id, $meta_key, $meta_values);
569 }
570 else $data['updated'] = update_user_meta( $user_id, $meta_key, $meta_value);
571
572 return $data;
573
574 }
575
576 public function delete_user_meta() {
577
578 global $json_api;
579
580 if (!$json_api->query->cookie) {
581 $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method.");
582 }
583
584 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
585
586 if (!$user_id) $json_api->error("Invalid cookie. Use the `generate_auth_cookie` method.");
587
588
589 if (!$json_api->query->meta_key) $json_api->error("You must include a 'meta_key' var in your request.");
590
591 else $meta_key = $json_api->query->meta_key;
592
593 if (!$json_api->query->meta_value) {
594 $json_api->error("You must include a 'meta_value' var in your request.");
595 }
596 else $meta_value = sanitize_text_field($json_api->query->meta_value);
597
598
599 $data['deleted'] = delete_user_meta( $user_id, $meta_key, $meta_value);
600
601 return $data;
602
603 }
604
605 public function update_user_meta_vars() {
606
607 global $json_api;
608
609 if (!$json_api->query->cookie) {
610 $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method.");
611 }
612
613 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
614 // echo '$user_id: '.$user_id;
615
616 if (!$user_id) {
617 $json_api->error("Invalid cookie. Use the `generate_auth_cookie` method.");
618 }
619
620 if( sizeof($_REQUEST) <=1) $json_api->error("You must include one or more vars in your request to add or update as user_meta. e.g. 'name', 'website', 'skills'. You must provide multiple meta_key vars in this format: &name=Ali&website=parorrey.com&skills=php,css,js,web design. If any field has the possibility to hold more than one value for any multi-select fields or check boxes, you must provide ending comma even when it has only one value so that it could be added in correct array format to distinguish it from simple string var. e.g. &skills=php,");
621
622 //d($_REQUEST);
623 foreach($_REQUEST as $field => $value){
624
625 if($field=='cookie') continue;
626
627 $field_label = str_replace('_',' ',$field);
628
629 if( strpos($value,',') !== false ) {
630 $values = explode(",", $value);
631 $values = array_map('trim',$values);
632 }
633 else $values = trim($value);
634 //echo 'field-values: '.$field.'=>'.$value;
635 //d($values);
636
637 $result[$field_label]['updated'] = update_user_meta( $user_id, $field, $values);
638
639 }
640
641 return $result;
642
643
644 }
645
646 public function xprofile() {
647
648 global $json_api;
649
650 if (function_exists('bp_is_active')) {
651
652 if (!$json_api->query->user_id) {
653 $json_api->error("You must include a 'user_id' var in your request.");
654 }
655 else $user_id = $json_api->query->user_id;
656
657
658 if (!$json_api->query->field) {
659 $json_api->error("You must include a 'field' var in your request. Use 'field=default' for all default fields.");
660 }
661 elseif ($json_api->query->field=='default') {
662 $field_label='First Name, Last Name, Bio';/*you should add your own field labels here for quick viewing*/
663 }
664 else $field_label = sanitize_text_field($json_api->query->field);
665
666
667 $fields = explode(",", $field_label);
668
669 if(is_array($fields)){
670
671 foreach($fields as $k){
672
673 $fields_data[$k] = xprofile_get_field_data( $k, $user_id );
674
675 }
676
677 return $fields_data;
678
679
680 }
681
682 }
683
684 else {
685
686 $json_api->error("You must install and activate BuddyPress plugin to use this method.");
687
688 }
689
690 }
691
692 public function xprofile_update() {
693
694 global $json_api;
695
696 if (function_exists('bp_is_active')) {
697
698 if (!$json_api->query->cookie) {
699 $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method.");
700 }
701
702 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
703 // echo '$user_id: '.$user_id;
704
705 if (!$user_id) {
706 $json_api->error("Invalid cookie. Use the `generate_auth_cookie` method.");
707 }
708
709
710 foreach($_REQUEST as $field => $value){
711
712 if($field=='cookie') continue;
713
714 $field_label = str_replace('_',' ',$field);
715
716 if( strpos($value,',') !== false ) {
717 $values = explode(",", $value);
718 $values = array_map('trim',$values);
719 }
720 else $values = trim($value);
721 //echo 'field-values: '.$field.'=>'.$value;
722 //d($values);
723
724 $result[$field_label]['updated'] = xprofile_set_field_data( $field_label, $user_id, $values, $is_required = true );
725
726 }
727
728 return $result;
729 }
730
731 else {
732
733 $json_api->error("You must install and activate BuddyPress plugin to use this method.");
734
735 }
736
737 }
738
739 public function fb_connect(){
740
741 global $json_api;
742
743 if ($json_api->query->fields) {
744
745 $fields = $json_api->query->fields;
746
747 }else $fields = 'id,name,first_name,last_name,email';
748
749 if ($json_api->query->ssl) {
750 $enable_ssl = $json_api->query->ssl;
751 }else $enable_ssl = true;
752
753 if (!$json_api->query->access_token) {
754 $json_api->error("You must include a 'access_token' variable. Get the valid access_token for this app from Facebook API.");
755 }else{
756
757 $url='https://graph.facebook.com/me/?fields='.$fields.'&access_token='.$json_api->query->access_token;
758
759 // Initiate curl
760 $ch = curl_init();
761 // Enable SSL verification
762 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $enable_ssl);
763 // Will return the response, if false it print the response
764 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
765 // Set the url
766 curl_setopt($ch, CURLOPT_URL,$url);
767 // Execute
768 $result=curl_exec($ch);
769 // Closing
770 curl_close($ch);
771
772 $result = json_decode($result, true);
773
774 if(isset($result["email"])){
775
776 $user_email = $result["email"];
777 $email_exists = email_exists($user_email);
778
779 if($email_exists) {
780 $user = get_user_by( 'email', $user_email );
781 $user_id = $user->ID;
782 $user_name = $user->user_login;
783 }
784
785
786
787 if ( !$user_id && $email_exists == false ) {
788
789 $user_name = strtolower($result['first_name'].'.'.$result['last_name']);
790
791 while(username_exists($user_name)){
792 $i++;
793 $user_name = strtolower($result['first_name'].'.'.$result['last_name']).'.'.$i;
794
795 }
796
797 $random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
798 $userdata = array(
799 'user_login' => $user_name,
800 'user_email' => $user_email,
801 'user_pass' => $random_password,
802 'display_name' => $result["name"],
803 'first_name' => $result['first_name'],
804 'last_name' => $result['last_name']
805 );
806
807 $user_id = wp_insert_user( $userdata ) ;
808 if($user_id) $user_account = 'user registered.';
809
810 } else {
811
812 if($user_id) $user_account = 'user logged in.';
813 }
814
815 $expiration = time() + apply_filters('auth_cookie_expiration', 1209600, $user_id, true);
816 $cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
817
818 $response['msg'] = $user_account;
819 $response['wp_user_id'] = $user_id;
820 $response['cookie'] = $cookie;
821 $response['user_login'] = $user_name;
822
823 }
824 else {
825 $response['msg'] = "Your 'access_token' did not return email of the user. Without 'email' user can't be logged in or registered. Get user email extended permission while joining the Facebook app.";
826
827 }
828
829 }
830
831 return $response;
832
833 }
834
835 public function post_comment(){
836 global $json_api;
837
838 if (!$json_api->query->cookie) {
839 $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method.");
840 }
841
842 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
843
844 if (!$user_id) {
845 $json_api->error("Invalid cookie. Use the `generate_auth_cookie` method.");
846 }
847
848 if ( !$json_api->query->post_id ) {
849 $json_api->error("No post specified. Include 'post_id' var in your request.");
850 } elseif (!$json_api->query->content ) {
851 $json_api->error("Please include 'content' var in your request.");
852 }
853
854 if (!isset($json_api->query->comment_status) ) {
855 $json_api->error("Please include 'comment_status' var in your request. Possible values are comment_status=1 (approved) or comment_status=hold (not-approved)");
856 }else $comment_status = $json_api->query->comment_status;
857
858 if($comment_status=='hold') $comment_status = 0;
859
860 $user_info = get_userdata( $user_id );
861
862 $time = current_time('mysql');
863 $agent = $_SERVER['HTTP_USER_AGENT'];
864 $ip=$_SERVER['REMOTE_ADDR'];
865
866 $data = array(
867 'comment_post_ID' => $json_api->query->post_id,
868 'comment_author' => $user_info->user_login,
869 'comment_author_email' => $user_info->user_email,
870 'comment_author_url' => $user_info->user_url,
871 'comment_content' => $json_api->query->content,
872 'comment_type' => '',
873 'comment_parent' => 0,
874 'user_id' => $user_info->ID,
875 'comment_author_IP' => $ip,
876 'comment_agent' => $agent,
877 'comment_date' => $time,
878 'comment_approved' => $comment_status,
879 );
880
881 //print_r($data);
882
883 $comment_id = wp_insert_comment($data);
884
885 return array(
886 "comment_id" => $comment_id
887 );
888 }
889
890 }