WordPress.org

Make WordPress Core

Changeset 8543


Ignore:
Timestamp:
08/04/2008 10:29:37 PM (10 years ago)
Author:
ryan
Message:

XML-RPC comments API. see #7446

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/comment-template.php

    r8497 r8543  
    321321 * @uses $comment
    322322 *
     323 * @param object|string|int $comment Comment to retrieve.
    323324 * @return string The permalink to the current comment
    324325 */
    325 function get_comment_link() {
    326     global $comment;
     326function get_comment_link($comment = null) {
     327    $comment = get_comment($comment);
    327328    return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
    328329}
  • trunk/wp-includes/comment.php

    r8091 r8543  
    44 *
    55 * @package WordPress
     6 * @subpackage Comment
    67 */
    78
     
    163164
    164165/**
    165  * Retrieve an array of comment data about comment $comment_ID.
    166  *
    167  * get_comment() technically does the same thing as this function. This function
    168  * also appears to reference variables and then not use them or not update them
    169  * when needed. It is advised to switch to get_comment(), since this function
    170  * might be deprecated in favor of using get_comment().
    171  *
    172  * @deprecated Use get_comment()
    173  * @see get_comment()
    174  * @since 0.71
    175  *
    176  * @uses $postc Comment cache, might not be used any more
    177  * @uses $id
    178  * @uses $wpdb Database Object
    179  *
    180  * @param int $comment_ID The ID of the comment
    181  * @param int $no_cache Whether to use the cache or not (casted to bool)
    182  * @param bool $include_unapproved Whether to include unapproved comments or not
    183  * @return array The comment data
    184  */
    185 function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) {
    186     global $postc, $wpdb;
    187     if ( $no_cache ) {
    188         $query = $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d", $comment_ID);
    189         if ( false == $include_unapproved )
    190             $query .= " AND comment_approved = '1'";
    191         $myrow = $wpdb->get_row($query, ARRAY_A);
     166 * Retrieve a list of comments
     167 *
     168 * {@internal Missing Long Description}}
     169 *
     170 * @package WordPress
     171 * @subpackage Comment
     172 * @since 2.7
     173 * @uses $wpdb
     174 *
     175 * @param mixed $args Optional. Array or string of options
     176 * @return array List of comments matching defaults or $args
     177 */
     178function get_comments( $args = '' ) {
     179    global $wpdb;
     180
     181    $defaults = array('status' => '', 'orderby' => 'comment_date_gmt', 'order' => 'DESC', 'number' => '', 'offset' => '', 'post_id' => 0);
     182
     183    $r = wp_parse_args( $args, $defaults );
     184    extract( $r, EXTR_SKIP );
     185
     186    $post_id = absint($post_id);
     187
     188    if ( 'hold' == $status )
     189        $approved = "comment_approved = '0'";
     190    elseif ( 'approve' == $status )
     191        $approved = "comment_approved = '1'";
     192    elseif ( 'spam' == $status )
     193        $approved = "comment_approved = 'spam'";
     194    else
     195        $approved = "( comment_approved = '0' OR comment_approved = '1' )";
     196
     197    if ( 'ASC' != $order )
     198        $order = 'DESC';
     199
     200    $orderby = 'comment_date_gmt';  // Hard code for now
     201
     202    $number = absint($number);
     203    $offset = absint($offset);
     204
     205    if ( !empty($number) ) {
     206        if ( $offset )
     207            $number = 'LIMIT ' . $offset . ',' . $number;
     208        else
     209            $number = 'LIMIT ' . $number;
     210
    192211    } else {
    193         $myrow['comment_ID']           = $postc->comment_ID;
    194         $myrow['comment_post_ID']      = $postc->comment_post_ID;
    195         $myrow['comment_author']       = $postc->comment_author;
    196         $myrow['comment_author_email'] = $postc->comment_author_email;
    197         $myrow['comment_author_url']   = $postc->comment_author_url;
    198         $myrow['comment_author_IP']    = $postc->comment_author_IP;
    199         $myrow['comment_date']         = $postc->comment_date;
    200         $myrow['comment_content']      = $postc->comment_content;
    201         $myrow['comment_karma']        = $postc->comment_karma;
    202         $myrow['comment_approved']     = $postc->comment_approved;
    203         $myrow['comment_type']         = $postc->comment_type;
    204     }
    205     return $myrow;
    206 }
     212        $number = '';
     213    }
     214
     215    if ( ! empty($post_id) )
     216        $post_where = "comment_post_ID = $post_id AND";
     217    else
     218        $post_where = '';
     219
     220    return $wpdb->get_results( "SELECT * FROM $wpdb->comments USE INDEX (comment_date_gmt) WHERE $post_where $approved ORDER BY $orderby $order $number" );
     221}
     222
     223/**
     224 * Retrieve all of the WordPress supported comment statuses.
     225 *
     226 * Comments have a limited set of valid status values, this provides the
     227 * comment status values and descriptions.
     228 *
     229 * @package WordPress
     230 * @subpackage Post
     231 * @since 2.7
     232 *
     233 * @return array List of comment statuses.
     234 */
     235function get_comment_statuses( ) {
     236    $status = array(
     237        'hold'      => __('Unapproved'),
     238        'approve'   => __('Approved'),
     239        'spam'      => __('Spam'),
     240    );
     241
     242    return $status;
     243}
     244
    207245
    208246/**
     
    822860
    823861    $comment_date_gmt = get_gmt_from_date($comment_date);
     862
     863    if ( empty($comment_approved) )
     864        $comment_approved = 1;
     865    else if ( 'hold' == $comment_approved )
     866        $comment_approved = 0;
     867    else if ( 'approve' == $comment_approved )
     868        $comment_approved = 1;
    824869
    825870    $wpdb->query( $wpdb->prepare("UPDATE $wpdb->comments SET
  • trunk/wp-includes/deprecated.php

    r8285 r8543  
    12841284    return false;
    12851285}
     1286
     1287/**
     1288 * Retrieve an array of comment data about comment $comment_ID.
     1289 *
     1290 * @deprecated Use get_comment()
     1291 * @see get_comment()
     1292 * @since 0.71
     1293 *
     1294 * @uses $id
     1295 * @uses $wpdb Database Object
     1296 *
     1297 * @param int $comment_ID The ID of the comment
     1298 * @param int $no_cache Whether to use the cache or not (casted to bool)
     1299 * @param bool $include_unapproved Whether to include unapproved comments or not
     1300 * @return array The comment data
     1301 */
     1302function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) {
     1303    _deprecated_function( __FUNCTION__, '2.7', 'get_comment()' );
     1304    return get_comment($comment_ID, ARRAY_A);
     1305}
     1306
    12861307?>
  • trunk/xmlrpc.php

    r8444 r8543  
    130130            'wp.getOptions'         => 'this:wp_getOptions',
    131131            'wp.setOptions'         => 'this:wp_setOptions',
     132            'wp.getComment'         => 'this:wp_getComment',
     133            'wp.getComments'        => 'this:wp_getComments',
     134            'wp.deleteComment'      => 'this:wp_deleteComment',
     135            'wp.editComment'        => 'this:wp_editComment',
     136            'wp.newComment'         => 'this:wp_newComment',
     137            'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
    132138
    133139            // Blogger API
     
    830836    }
    831837
     838    function wp_getComment($args) {
     839        $this->escape($args);
     840
     841        $blog_id    = (int) $args[0];
     842        $username   = $args[1];
     843        $password   = $args[2];
     844        $comment_id = (int) $args[3];
     845
     846        if ( !$this->login_pass_ok( $username, $password ) )
     847            return $this->error;
     848
     849        set_current_user( 0, $username );
     850        if ( !current_user_can( 'moderate_comments' ) )
     851            return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this blog.' ) );
     852
     853        do_action('xmlrpc_call', 'wp.getComment');
     854
     855        if ( ! $comment = get_comment($comment_id) )
     856            return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
     857
     858        // Format page date.
     859        $comment_date = mysql2date("Ymd\TH:i:s", $comment->comment_date);
     860        $comment_date_gmt = mysql2date("Ymd\TH:i:s", $comment->comment_date_gmt);
     861
     862        if ( 0 == $comment->comment_approved )
     863            $comment_status = 'hold';
     864        else if ( 'spam' == $comment->comment_approved )
     865            $comment_status = 'spam';
     866        else
     867            $comment_status = 'approve';
     868
     869        $link = get_comment_link($comment);
     870
     871        $comment_struct = array(
     872            "date_created_gmt"      => new IXR_Date($comment_date_gmt),
     873            "user_id"               => $comment->user_id,
     874            "comment_id"            => $comment->comment_ID,
     875            "parent"                => $comment->comment_parent,
     876            "status"                => $comment_status,
     877            "content"               => $comment->comment_content,
     878            "link"                  => $link,
     879            "post_id"               => $comment->comment_post_ID,
     880            "post_title"            => get_the_title($comment->comment_post_ID),
     881            "author"                => $author->comment_author,
     882            "author_url"            => $comment->comment_author_url,
     883            "author_email"          => $comment->comment_author_email,
     884            "author_ip"             => $comment->comment_author_IP,
     885        );
     886
     887        return $comment_struct;
     888    }
     889
     890    function wp_getComments($args) {
     891        $this->escape($args);
     892
     893        $blog_id    = (int) $args[0];
     894        $username   = $args[1];
     895        $password   = $args[2];
     896        $struct     = $args[3];
     897
     898        if ( !$this->login_pass_ok($username, $password) )
     899            return($this->error);
     900
     901        set_current_user( 0, $username );
     902        if ( !current_user_can( 'moderate_comments' ) )
     903            return new IXR_Error( 401, __( 'Sorry, you can not edit comments.' ) );
     904
     905        do_action('xmlrpc_call', 'wp.getComments');
     906
     907        if ( isset($struct['status']) )
     908            $status = $struct['status'];
     909        else
     910            $status = '';
     911
     912        $post_id = '';
     913        if ( isset($struct['post_id']) )
     914            $post_id = absint($struct['post_id']);
     915
     916        $offset = 0;
     917        if ( isset($struct['offset']) )
     918            $offset = absint($struct['offset']);
     919
     920        $number = 10;
     921        if ( isset($struct['number']) )
     922            $number = absint($struct['number']);
     923
     924        $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
     925        $num_comments = count($comments);
     926
     927        if ( ! $num_comments )
     928            return array();
     929
     930        $comments_struct = array();
     931
     932        for ( $i = 0; $i < $num_comments; $i++ ) {
     933            $comment = wp_xmlrpc_server::wp_getComment(array(
     934                $blog_id, $username, $password, $comments[$i]->comment_ID,
     935            ));
     936            $comments_struct[] = $comment;
     937        }
     938
     939        return $comments_struct;
     940    }
     941
     942    function wp_deleteComment($args) {
     943        $this->escape($args);
     944
     945        $blog_id    = (int) $args[0];
     946        $username   = $args[1];
     947        $password   = $args[2];
     948        $comment_ID = (int) $args[3];
     949
     950        if ( !$this->login_pass_ok( $username, $password ) )
     951            return $this->error;
     952
     953        set_current_user( 0, $username );
     954        if ( !current_user_can( 'moderate_comments' ) )
     955            return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this blog.' ) );
     956
     957        do_action('xmlrpc_call', 'wp.deleteComment');
     958
     959        if ( ! get_comment($comment_ID) )
     960            return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
     961
     962        return wp_delete_comment($comment_ID);
     963    }
     964
     965    function wp_editComment($args) {
     966        $this->escape($args);
     967
     968        $blog_id    = (int) $args[0];
     969        $username   = $args[1];
     970        $password   = $args[2];
     971        $comment_ID = (int) $args[3];
     972        $content_struct = $args[4];
     973
     974        if ( !$this->login_pass_ok( $username, $password ) )
     975            return $this->error;
     976
     977        set_current_user( 0, $username );
     978        if ( !current_user_can( 'moderate_comments' ) )
     979            return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this blog.' ) );
     980
     981        do_action('xmlrpc_call', 'wp.editComment');
     982
     983        if ( ! get_comment($comment_ID) )
     984            return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
     985
     986        if ( isset($content_struct['status']) ) {
     987            $statuses = get_comment_statuses();
     988            $statuses = array_keys($statuses);
     989
     990            if ( ! in_array($content_struct['status'], $statuses) )
     991                return new IXR_Error( 401, __( 'Invalid comment status.' ) );
     992            $comment_approved = $content_struct['status'];
     993        }
     994
     995        // Do some timestamp voodoo
     996        if ( !empty( $content_struct['date_created_gmt'] ) ) {
     997            $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
     998            $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
     999            $comment_date_gmt = iso8601_to_datetime($dateCreated, GMT);
     1000        }
     1001
     1002        if ( isset($content_struct['content']) )
     1003            $comment_content = $content_struct['content'];
     1004
     1005        if ( isset($content_struct['author']) )
     1006            $comment_author = $content_struct['author'];
     1007
     1008        if ( isset($content_struct['author_url']) )
     1009            $comment_author_url = $content_struct['author_url'];
     1010
     1011        if ( isset($content_struct['author_email']) )
     1012            $comment_author_email = $content_struct['author_email'];
     1013
     1014        // We've got all the data -- post it:
     1015        $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
     1016       
     1017        $result = wp_update_comment($comment);
     1018        if ( is_wp_error( $result ) )
     1019            return new IXR_Error(500, $result->get_error_message());
     1020
     1021        if ( !$result )
     1022            return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
     1023
     1024        return true;
     1025    }
     1026
     1027    function wp_newComment($args) {
     1028        global $wpdb;
     1029
     1030        $this->escape($args);
     1031
     1032        $blog_id    = (int) $args[0];
     1033        $username   = $args[1];
     1034        $password   = $args[2];
     1035        $post       = $args[3];
     1036        $content_struct = $args[4];
     1037
     1038        $allow_anon = apply_filters('xmlrpc_allow_anonymous_comments', false);
     1039
     1040        if ( !$this->login_pass_ok( $username, $password ) ) {
     1041            $logged_in = false;
     1042            if ( $allow_anon && get_option('comment_registration') )
     1043                return new IXR_Error( 403, __( 'You must be registered to comment' ) );
     1044            else if ( !$allow_anon )
     1045                return $this->error;
     1046        } else {
     1047            $logged_in = true;
     1048            set_current_user( 0, $username );
     1049            if ( !current_user_can( 'moderate_comments' ) )
     1050                return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this blog.' ) );
     1051        }
     1052
     1053        if ( is_numeric($post) )
     1054            $post_id = absint($post);
     1055        else
     1056            $post_id = url_to_postid($post);
     1057
     1058        if ( ! $post_id )
     1059            return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     1060
     1061        if ( ! get_post($post_id) )
     1062            return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     1063
     1064        $comment['comment_post_ID'] = $post_id;
     1065
     1066        if ( $logged_in ) {
     1067            $user = wp_get_current_user();
     1068            $comment['comment_author'] = $wpdb->escape( $user->display_name );
     1069            $comment['comment_author_email'] = $wpdb->escape( $user->user_email );
     1070            $comment['comment_author_url'] = $wpdb->escape( $user->user_url );
     1071            $comment['user_ID'] = $user->ID;
     1072        } else {
     1073            $comment['comment_author'] = '';
     1074            if ( isset($content_struct['author']) )
     1075                $comment['comment_author'] = $content_struct['author'];
     1076            $comment['comment_author_email'] = '';
     1077            if ( isset($content_struct['author']) )
     1078                $comment['comment_author_email'] = $content_struct['author_email'];
     1079            $comment['comment_author_url'] = '';
     1080            if ( isset($content_struct['author']) )
     1081                $comment['comment_author_url'] = $content_struct['author_url'];
     1082            $comment['user_ID'] = 0;
     1083
     1084            if ( get_option('require_name_email') ) {
     1085                if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
     1086                    return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
     1087                elseif ( !is_email($comment['comment_author_email']) )
     1088                    return new IXR_Error( 403, __( 'A valid email address is required' ) );
     1089            }
     1090        }
     1091
     1092        $comment['comment_content'] = $content_struct['content'];
     1093
     1094        do_action('xmlrpc_call', 'wp.newComment');
     1095       
     1096        return wp_new_comment($comment);
     1097    }
     1098
     1099    function wp_getCommentStatusList($args) {
     1100        $this->escape( $args );
     1101
     1102        $blog_id    = (int) $args[0];
     1103        $username   = $args[1];
     1104        $password   = $args[2];
     1105
     1106        if ( !$this->login_pass_ok( $username, $password ) )
     1107            return $this->error;
     1108
     1109        set_current_user( 0, $username );
     1110        if ( !current_user_can( 'moderate_comments' ) )
     1111            return new IXR_Error( 403, __( 'You are not allowed access to details about this blog.' ) );
     1112
     1113        do_action('xmlrpc_call', 'wp.getCommentStatusList');
     1114
     1115        return get_comment_statuses( );
     1116    }
     1117
    8321118    function wp_getCommentCount( $args ) {
    8331119        $this->escape($args);
     
    8571143        );
    8581144    }
    859 
    8601145
    8611146    function wp_getPostStatusList( $args ) {
Note: See TracChangeset for help on using the changeset viewer.