diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-07-27 10:05:23 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-07-27 10:05:23 +0200 |
| commit | 5bf66662a9bdd62c5bccab15e607cd95cfb8fcab (patch) | |
| tree | 39a1a4629749056191c05dfd899f931701b7acf3 /srcs/wordpress/wp-includes/comment.php | |
| parent | 5afd237bbd22028b85532b8c0b3fcead49a00764 (diff) | |
| download | ft_server-master.tar.gz ft_server-master.tar.bz2 ft_server-master.zip | |
Removed wordpress and phpmyadmin, my server doesn't handle it well and it brings shame on my famillyHEADmaster
Diffstat (limited to 'srcs/wordpress/wp-includes/comment.php')
| -rw-r--r-- | srcs/wordpress/wp-includes/comment.php | 3626 |
1 files changed, 0 insertions, 3626 deletions
diff --git a/srcs/wordpress/wp-includes/comment.php b/srcs/wordpress/wp-includes/comment.php deleted file mode 100644 index b451c5b..0000000 --- a/srcs/wordpress/wp-includes/comment.php +++ /dev/null @@ -1,3626 +0,0 @@ -<?php -/** - * Core Comment API - * - * @package WordPress - * @subpackage Comment - */ - -/** - * Check whether a comment passes internal checks to be allowed to add. - * - * If manual comment moderation is set in the administration, then all checks, - * regardless of their type and whitelist, will fail and the function will - * return false. - * - * If the number of links exceeds the amount in the administration, then the - * check fails. If any of the parameter contents match the blacklist of words, - * then the check fails. - * - * If the comment author was approved before, then the comment is automatically - * whitelisted. - * - * If all checks pass, the function will return true. - * - * @since 1.2.0 - * - * @global wpdb $wpdb WordPress database abstraction object. - * - * @param string $author Comment author name. - * @param string $email Comment author email. - * @param string $url Comment author URL. - * @param string $comment Content of the comment. - * @param string $user_ip Comment author IP address. - * @param string $user_agent Comment author User-Agent. - * @param string $comment_type Comment type, either user-submitted comment, - * trackback, or pingback. - * @return bool If all checks pass, true, otherwise false. - */ -function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) { - global $wpdb; - - // If manual moderation is enabled, skip all checks and return false. - if ( 1 == get_option( 'comment_moderation' ) ) { - return false; - } - - /** This filter is documented in wp-includes/comment-template.php */ - $comment = apply_filters( 'comment_text', $comment, null, array() ); - - // Check for the number of external links if a max allowed number is set. - $max_links = get_option( 'comment_max_links' ); - if ( $max_links ) { - $num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out ); - - /** - * Filters the number of links found in a comment. - * - * @since 3.0.0 - * @since 4.7.0 Added the `$comment` parameter. - * - * @param int $num_links The number of links found. - * @param string $url Comment author's URL. Included in allowed links total. - * @param string $comment Content of the comment. - */ - $num_links = apply_filters( 'comment_max_links_url', $num_links, $url, $comment ); - - /* - * If the number of links in the comment exceeds the allowed amount, - * fail the check by returning false. - */ - if ( $num_links >= $max_links ) { - return false; - } - } - - $mod_keys = trim( get_option( 'moderation_keys' ) ); - - // If moderation 'keys' (keywords) are set, process them. - if ( ! empty( $mod_keys ) ) { - $words = explode( "\n", $mod_keys ); - - foreach ( (array) $words as $word ) { - $word = trim( $word ); - - // Skip empty lines. - if ( empty( $word ) ) { - continue; - } - - /* - * Do some escaping magic so that '#' (number of) characters in the spam - * words don't break things: - */ - $word = preg_quote( $word, '#' ); - - /* - * Check the comment fields for moderation keywords. If any are found, - * fail the check for the given field by returning false. - */ - $pattern = "#$word#i"; - if ( preg_match( $pattern, $author ) ) { - return false; - } - if ( preg_match( $pattern, $email ) ) { - return false; - } - if ( preg_match( $pattern, $url ) ) { - return false; - } - if ( preg_match( $pattern, $comment ) ) { - return false; - } - if ( preg_match( $pattern, $user_ip ) ) { - return false; - } - if ( preg_match( $pattern, $user_agent ) ) { - return false; - } - } - } - - /* - * Check if the option to approve comments by previously-approved authors is enabled. - * - * If it is enabled, check whether the comment author has a previously-approved comment, - * as well as whether there are any moderation keywords (if set) present in the author - * email address. If both checks pass, return true. Otherwise, return false. - */ - if ( 1 == get_option( 'comment_whitelist' ) ) { - if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) { - $comment_user = get_user_by( 'email', wp_unslash( $email ) ); - if ( ! empty( $comment_user->ID ) ) { - $ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved FROM $wpdb->comments WHERE user_id = %d AND comment_approved = '1' LIMIT 1", $comment_user->ID ) ); - } else { - // expected_slashed ($author, $email) - $ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved FROM $wpdb->comments WHERE comment_author = %s AND comment_author_email = %s and comment_approved = '1' LIMIT 1", $author, $email ) ); - } - if ( ( 1 == $ok_to_comment ) && - ( empty( $mod_keys ) || false === strpos( $email, $mod_keys ) ) ) { - return true; - } else { - return false; - } - } else { - return false; - } - } - return true; -} - -/** - * Retrieve the approved comments for post $post_id. - * - * @since 2.0.0 - * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query. - * - * @param int $post_id The ID of the post. - * @param array $args Optional. See WP_Comment_Query::__construct() for information on accepted arguments. - * @return int|array $comments The approved comments, or number of comments if `$count` - * argument is true. - */ -function get_approved_comments( $post_id, $args = array() ) { - if ( ! $post_id ) { - return array(); - } - - $defaults = array( - 'status' => 1, - 'post_id' => $post_id, - 'order' => 'ASC', - ); - $parsed_args = wp_parse_args( $args, $defaults ); - - $query = new WP_Comment_Query; - return $query->query( $parsed_args ); -} - -/** - * Retrieves comment data given a comment ID or comment object. - * - * If an object is passed then the comment data will be cached and then returned - * after being passed through a filter. If the comment is empty, then the global - * comment variable will be used, if it is set. - * - * @since 2.0.0 - * - * @global WP_Comment $comment Global comment object. - * - * @param WP_Comment|string|int $comment Comment to retrieve. - * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to - * a WP_Comment object, an associative array, or a numeric array, respectively. Default OBJECT. - * @return WP_Comment|array|null Depends on $output value. - */ -function get_comment( &$comment = null, $output = OBJECT ) { - if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) { - $comment = $GLOBALS['comment']; - } - - if ( $comment instanceof WP_Comment ) { - $_comment = $comment; - } elseif ( is_object( $comment ) ) { - $_comment = new WP_Comment( $comment ); - } else { - $_comment = WP_Comment::get_instance( $comment ); - } - - if ( ! $_comment ) { - return null; - } - - /** - * Fires after a comment is retrieved. - * - * @since 2.3.0 - * - * @param mixed $_comment Comment data. - */ - $_comment = apply_filters( 'get_comment', $_comment ); - - if ( $output == OBJECT ) { - return $_comment; - } elseif ( $output == ARRAY_A ) { - return $_comment->to_array(); - } elseif ( $output == ARRAY_N ) { - return array_values( $_comment->to_array() ); - } - return $_comment; -} - -/** - * Retrieve a list of comments. - * - * The comment list can be for the blog as a whole or for an individual post. - * - * @since 2.7.0 - * - * @param string|array $args Optional. Array or string of arguments. See WP_Comment_Query::__construct() - * for information on accepted arguments. Default empty. - * @return int|array List of comments or number of found comments if `$count` argument is true. - */ -function get_comments( $args = '' ) { - $query = new WP_Comment_Query; - return $query->query( $args ); -} - -/** - * Retrieve all of the WordPress supported comment statuses. - * - * Comments have a limited set of valid status values, this provides the comment - * status values and descriptions. - * - * @since 2.7.0 - * - * @return array List of comment statuses. - */ -function get_comment_statuses() { - $status = array( - 'hold' => __( 'Unapproved' ), - 'approve' => _x( 'Approved', 'comment status' ), - 'spam' => _x( 'Spam', 'comment status' ), - 'trash' => _x( 'Trash', 'comment status' ), - ); - - return $status; -} - -/** - * Gets the default comment status for a post type. - * - * @since 4.3.0 - * - * @param string $post_type Optional. Post type. Default 'post'. - * @param string $comment_type Optional. Comment type. Default 'comment'. - * @return string Expected return value is 'open' or 'closed'. - */ -function get_default_comment_status( $post_type = 'post', $comment_type = 'comment' ) { - switch ( $comment_type ) { - case 'pingback': - case 'trackback': - $supports = 'trackbacks'; - $option = 'ping'; - break; - default: - $supports = 'comments'; - $option = 'comment'; - break; - } - - // Set the status. - if ( 'page' === $post_type ) { - $status = 'closed'; - } elseif ( post_type_supports( $post_type, $supports ) ) { - $status = get_option( "default_{$option}_status" ); - } else { - $status = 'closed'; - } - - /** - * Filters the default comment status for the given post type. - * - * @since 4.3.0 - * - * @param string $status Default status for the given post type, - * either 'open' or 'closed'. - * @param string $post_type Post type. Default is `post`. - * @param string $comment_type Type of comment. Default is `comment`. - */ - return apply_filters( 'get_default_comment_status', $status, $post_type, $comment_type ); -} - -/** - * The date the last comment was modified. - * - * @since 1.5.0 - * @since 4.7.0 Replaced caching the modified date in a local static variable - * with the Object Cache API. - * - * @global wpdb $wpdb WordPress database abstraction object. - * - * @param string $timezone Which timezone to use in reference to 'gmt', 'blog', or 'server' locations. - * @return string|false Last comment modified date on success, false on failure. - */ -function get_lastcommentmodified( $timezone = 'server' ) { - global $wpdb; - - $timezone = strtolower( $timezone ); - $key = "lastcommentmodified:$timezone"; - - $comment_modified_date = wp_cache_get( $key, 'timeinfo' ); - if ( false !== $comment_modified_date ) { - return $comment_modified_date; - } - - switch ( $timezone ) { - case 'gmt': - $comment_modified_date = $wpdb->get_var( "SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" ); - break; - case 'blog': - $comment_modified_date = $wpdb->get_var( "SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" ); - break; - case 'server': - $add_seconds_server = gmdate( 'Z' ); - - $comment_modified_date = $wpdb->get_var( $wpdb->prepare( "SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1", $add_seconds_server ) ); - break; - } - - if ( $comment_modified_date ) { - wp_cache_set( $key, $comment_modified_date, 'timeinfo' ); - - return $comment_modified_date; - } - - return false; -} - -/** - * Retrieves the total comment counts for the whole site or a single post. - * - * Unlike wp_count_comments(), this function always returns the live comment counts without caching. - * - * @since 2.0.0 - * - * @global wpdb $wpdb WordPress database abstraction object. - * - * @param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that - * comment counts for the whole site will be retrieved. - * @return array() { - * The number of comments keyed by their status. - * - * @type int|string $approved The number of approved comments. - * @type int|string $awaiting_moderation The number of comments awaiting moderation (a.k.a. pending). - * @type int|string $spam The number of spam comments. - * @type int|string $trash The number of trashed comments. - * @type int|string $post-trashed The number of comments for posts that are in the trash. - * @type int $total_comments The total number of non-trashed comments, including spam. - * @type int $all The total number of pending or approved comments. - * } - */ -function get_comment_count( $post_id = 0 ) { - global $wpdb; - - $post_id = (int) $post_id; - - $where = ''; - if ( $post_id > 0 ) { - $where = $wpdb->prepare( 'WHERE comment_post_ID = %d', $post_id ); - } - - $totals = (array) $wpdb->get_results( - " - SELECT comment_approved, COUNT( * ) AS total - FROM {$wpdb->comments} - {$where} - GROUP BY comment_approved - ", - ARRAY_A - ); - - $comment_count = array( - 'approved' => 0, - 'awaiting_moderation' => 0, - 'spam' => 0, - 'trash' => 0, - 'post-trashed' => 0, - 'total_comments' => 0, - 'all' => 0, - ); - - foreach ( $totals as $row ) { - switch ( $row['comment_approved'] ) { - case 'trash': - $comment_count['trash'] = $row['total']; - break; - case 'post-trashed': - $comment_count['post-trashed'] = $row['total']; - break; - case 'spam': - $comment_count['spam'] = $row['total']; - $comment_count['total_comments'] += $row['total']; - break; - case '1': - $comment_count['approved'] = $row['total']; - $comment_count['total_comments'] += $row['total']; - $comment_count['all'] += $row['total']; - break; - case '0': - $comment_count['awaiting_moderation'] = $row['total']; - $comment_count['total_comments'] += $row['total']; - $comment_count['all'] += $row['total']; - break; - default: - break; - } - } - - return $comment_count; -} - -// -// Comment meta functions -// - -/** - * Add meta data field to a comment. - * - * @since 2.9.0 - * @link https://developer.wordpress.org/reference/functions/add_comment_meta/ - * - * @param int $comment_id Comment ID. - * @param string $meta_key Metadata name. - * @param mixed $meta_value Metadata value. - * @param bool $unique Optional, default is false. Whether the same key should not be added. - * @return int|bool Meta ID on success, false on failure. - */ -function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) { - return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique ); -} - -/** - * Remove metadata matching criteria from a comment. - * - * You can match based on the key, or key and value. Removing based on key and - * value, will keep from removing duplicate metadata with the same key. It also - * allows removing all metadata matching key, if needed. - * - * @since 2.9.0 - * @link https://developer.wordpress.org/reference/functions/delete_comment_meta/ - * - * @param int $comment_id comment ID - * @param string $meta_key Metadata name. - * @param mixed $meta_value Optional. Metadata value. - * @return bool True on success, false on failure. - */ -function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) { - return delete_metadata( 'comment', $comment_id, $meta_key, $meta_value ); -} - -/** - * Retrieve comment meta field for a comment. - * - * @since 2.9.0 - * @link https://developer.wordpress.org/reference/functions/get_comment_meta/ - * - * @param int $comment_id Comment ID. - * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys. - * @param bool $single Whether to return a single value. - * @return mixed Will be an array if $single is false. Will be value of meta data field if $single - * is true. - */ -function get_comment_meta( $comment_id, $key = '', $single = false ) { - return get_metadata( 'comment', $comment_id, $key, $single ); -} - -/** - * Update comment meta field based on comment ID. - * - * Use the $prev_value parameter to differentiate between meta fields with the - * same key and comment ID. - * - * If the meta field for the comment does not exist, it will be added. - * - * @since 2.9.0 - * @link https://developer.wordpress.org/reference/functions/update_comment_meta/ - * - * @param int $comment_id Comment ID. - * @param string $meta_key Metadata key. - * @param mixed $meta_value Metadata value. - * @param mixed $prev_value Optional. Previous value to check before removing. - * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. - */ -function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) { - return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value ); -} - -/** - * Queues comments for metadata lazy-loading. - * - * @since 4.5.0 - * - * @param WP_Comment[] $comments Array of comment objects. - */ -function wp_queue_comments_for_comment_meta_lazyload( $comments ) { - // Don't use `wp_list_pluck()` to avoid by-reference manipulation. - $comment_ids = array(); - if ( is_array( $comments ) ) { - foreach ( $comments as $comment ) { - if ( $comment instanceof WP_Comment ) { - $comment_ids[] = $comment->comment_ID; - } - } - } - - if ( $comment_ids ) { - $lazyloader = wp_metadata_lazyloader(); - $lazyloader->queue_objects( 'comment', $comment_ids ); - } -} - -/** - * Sets the cookies used to store an unauthenticated commentator's identity. Typically used - * to recall previous comments by this commentator that are still held in moderation. - * - * @since 3.4.0 - * @since 4.9.6 The `$cookies_consent` parameter was added. - * - * @param WP_Comment $comment Comment object. - * @param WP_User $user Comment author's user object. The user may not exist. - * @param boolean $cookies_consent Optional. Comment author's consent to store cookies. Default true. - */ -function wp_set_comment_cookies( $comment, $user, $cookies_consent = true ) { - // If the user already exists, or the user opted out of cookies, don't set cookies. - if ( $user->exists() ) { - return; - } - - if ( false === $cookies_consent ) { - // Remove any existing cookies. - $past = time() - YEAR_IN_SECONDS; - setcookie( 'comment_author_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN ); - setcookie( 'comment_author_email_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN ); - setcookie( 'comment_author_url_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN ); - - return; - } - - /** - * Filters the lifetime of the comment cookie in seconds. - * - * @since 2.8.0 - * - * @param int $seconds Comment cookie lifetime. Default 30000000. - */ - $comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', 30000000 ); - $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) ); - setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); - setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); - setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); -} - -/** - * Sanitizes the cookies sent to the user already. - * - * Will only do anything if the cookies have already been created for the user. - * Mostly used after cookies had been sent to use elsewhere. - * - * @since 2.0.4 - */ -function sanitize_comment_cookies() { - if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) { - /** - * Filters the comment author's name cookie before it is set. - * - * When this filter hook is evaluated in wp_filter_comment(), - * the comment author's name string is passed. - * - * @since 1.5.0 - * - * @param string $author_cookie The comment author name cookie. - */ - $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE[ 'comment_author_' . COOKIEHASH ] ); - $comment_author = wp_unslash( $comment_author ); - $comment_author = esc_attr( $comment_author ); - $_COOKIE[ 'comment_author_' . COOKIEHASH ] = $comment_author; - } - - if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) { - /** - * Filters the comment author's email cookie before it is set. - * - * When this filter hook is evaluated in wp_filter_comment(), - * the comment author's email string is passed. - * - * @since 1.5.0 - * - * @param string $author_email_cookie The comment author email cookie. - */ - $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ); - $comment_author_email = wp_unslash( $comment_author_email ); - $comment_author_email = esc_attr( $comment_author_email ); - $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] = $comment_author_email; - } - - if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) { - /** - * Filters the comment author's URL cookie before it is set. - * - * When this filter hook is evaluated in wp_filter_comment(), - * the comment author's URL string is passed. - * - * @since 1.5.0 - * - * @param string $author_url_cookie The comment author URL cookie. - */ - $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ); - $comment_author_url = wp_unslash( $comment_author_url ); - $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] = $comment_author_url; - } -} - -/** - * Validates whether this comment is allowed to be made. - * - * @since 2.0.0 - * @since 4.7.0 The `$avoid_die` parameter was added, allowing the function to - * return a WP_Error object instead of dying. - * - * @global wpdb $wpdb WordPress database abstraction object. - * - * @param array $commentdata Contains information on the comment. - * @param bool $avoid_die When true, a disallowed comment will result in the function - * returning a WP_Error object, rather than executing wp_die(). - * Default false. - * @return int|string|WP_Error Allowed comments return the approval status (0|1|'spam'). - * If `$avoid_die` is true, disallowed comments return a WP_Error. - */ -function wp_allow_comment( $commentdata, $avoid_die = false ) { - global $wpdb; - - // Simple duplicate check - // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content) - $dupe = $wpdb->prepare( - "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ", - wp_unslash( $commentdata['comment_post_ID'] ), - wp_unslash( $commentdata['comment_parent'] ), - wp_unslash( $commentdata['comment_author'] ) - ); - if ( $commentdata['comment_author_email'] ) { - $dupe .= $wpdb->prepare( - 'AND comment_author_email = %s ', - wp_unslash( $commentdata['comment_author_email'] ) - ); - } - $dupe .= $wpdb->prepare( - ') AND comment_content = %s LIMIT 1', - wp_unslash( $commentdata['comment_content'] ) - ); - - $dupe_id = $wpdb->get_var( $dupe ); - - /** - * Filters the ID, if any, of the duplicate comment found when creating a new comment. - * - * Return an empty value from this filter to allow what WP considers a duplicate comment. - * - * @since 4.4.0 - * - * @param int $dupe_id ID of the comment identified as a duplicate. - * @param array $commentdata Data for the comment being created. - */ - $dupe_id = apply_filters( 'duplicate_comment_id', $dupe_id, $commentdata ); - - if ( $dupe_id ) { - /** - * Fires immediately after a duplicate comment is detected. - * - * @since 3.0.0 - * - * @param array $commentdata Comment data. - */ - do_action( 'comment_duplicate_trigger', $commentdata ); - - /** - * Filters duplicate comment error message. - * - * @since 5.2.0 - * - * @param string $comment_duplicate_message Duplicate comment error message. - */ - $comment_duplicate_message = apply_filters( 'comment_duplicate_message', __( 'Duplicate comment detected; it looks as though you’ve already said that!' ) ); - - if ( true === $avoid_die ) { - return new WP_Error( 'comment_duplicate', $comment_duplicate_message, 409 ); - } else { - if ( wp_doing_ajax() ) { - die( $comment_duplicate_message ); - } - - wp_die( $comment_duplicate_message, 409 ); - } - } - - /** - * Fires immediately before a comment is marked approved. - * - * Allows checking for comment flooding. - * - * @since 2.3.0 - * @since 4.7.0 The `$avoid_die` parameter was added. - * - * @param string $comment_author_IP Comment author's IP address. - * @param string $comment_author_email Comment author's email. - * @param string $comment_date_gmt GMT date the comment was posted. - * @param bool $avoid_die Whether to prevent executing wp_die() - * or die() if a comment flood is occurring. - */ - do_action( - 'check_comment_flood', - $commentdata['comment_author_IP'], - $commentdata['comment_author_email'], - $commentdata['comment_date_gmt'], - $avoid_die - ); - - /** - * Filters whether a comment is part of a comment flood. - * - * The default check is wp_check_comment_flood(). See check_comment_flood_db(). - * - * @since 4.7.0 - * - * @param bool $is_flood Is a comment flooding occurring? Default false. - * @param string $comment_author_IP Comment author's IP address. - * @param string $comment_author_email Comment author's email. - * @param string $comment_date_gmt GMT date the comment was posted. - * @param bool $avoid_die Whether to prevent executing wp_die() - * or die() if a comment flood is occurring. - */ - $is_flood = apply_filters( - 'wp_is_comment_flood', - false, - $commentdata['comment_author_IP'], - $commentdata['comment_author_email'], - $commentdata['comment_date_gmt'], - $avoid_die - ); - - if ( $is_flood ) { - /** This filter is documented in wp-includes/comment-template.php */ - $comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) ); - - return new WP_Error( 'comment_flood', $comment_flood_message, 429 ); - } - - if ( ! empty( $commentdata['user_id'] ) ) { - $user = get_userdata( $commentdata['user_id'] ); - $post_author = $wpdb->get_var( - $wpdb->prepare( - "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", - $commentdata['comment_post_ID'] - ) - ); - } - - if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) { - // The author and the admins get respect. - $approved = 1; - } else { - // Everyone else's comments will be checked. - if ( check_comment( - $commentdata['comment_author'], - $commentdata['comment_author_email'], - $commentdata['comment_author_url'], - $commentdata['comment_content'], - $commentdata['comment_author_IP'], - $commentdata['comment_agent'], - $commentdata['comment_type'] - ) ) { - $approved = 1; - } else { - $approved = 0; - } - - if ( wp_blacklist_check( - $commentdata['comment_author'], - $commentdata['comment_author_email'], - $commentdata['comment_author_url'], - $commentdata['comment_content'], - $commentdata['comment_author_IP'], - $commentdata['comment_agent'] - ) ) { - $approved = EMPTY_TRASH_DAYS ? 'trash' : 'spam'; - } - } - - /** - * Filters a comment's approval status before it is set. - * - * @since 2.1.0 - * @since 4.9.0 Returning a WP_Error value from the filter will shortcircuit comment insertion and - * allow skipping further processing. - * - * @param int|string|WP_Error $approved The approval status. Accepts 1, 0, 'spam' or WP_Error. - * @param array $commentdata Comment data. - */ - $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata ); - return $approved; -} - -/** - * Hooks WP's native database-based comment-flood check. - * - * This wrapper maintains backward compatibility with plugins that expect to - * be able to unhook the legacy check_comment_flood_db() function from - * 'check_comment_flood' using remove_action(). - * - * @since 2.3.0 - * @since 4.7.0 Converted to be an add_filter() wrapper. - */ -function check_comment_flood_db() { - add_filter( 'wp_is_comment_flood', 'wp_check_comment_flood', 10, 5 ); -} - -/** - * Checks whether comment flooding is occurring. - * - * Won't run, if current user can manage options, so to not block - * administrators. - * - * @since 4.7.0 - * - * @global wpdb $wpdb WordPress database abstraction object. - * - * @param bool $is_flood Is a comment flooding occurring? - * @param string $ip Comment author's IP address. - * @param string $email Comment author's email address. - * @param string $date MySQL time string. - * @param bool $avoid_die When true, a disallowed comment will result in the function - * returning a WP_Error object, rather than executing wp_die(). - * Default false. - * @return bool Whether comment flooding is occurring. - */ -function wp_check_comment_flood( $is_flood, $ip, $email, $date, $avoid_die = false ) { - - global $wpdb; - - // Another callback has declared a flood. Trust it. - if ( true === $is_flood ) { - return $is_flood; - } - - // don't throttle admins or moderators - if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) { - return false; - } - $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ); - - if ( is_user_logged_in() ) { - $user = get_current_user_id(); - $check_column = '`user_id`'; - } else { - $user = $ip; - $check_column = '`comment_author_IP`'; - } - - $sql = $wpdb->prepare( - "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( $check_column = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1", - $hour_ago, - $user, - $email - ); - $lasttime = $wpdb->get_var( $sql ); - if ( $lasttime ) { - $time_lastcomment = mysql2date( 'U', $lasttime, false ); - $time_newcomment = mysql2date( 'U', $date, false ); - /** - * Filters the comment flood status. - * - * @since 2.1.0 - * - * @param bool $bool Whether a comment flood is occurring. Default false. - * @param int $time_lastcomment Timestamp of when the last comment was posted. - * @param int $time_newcomment Timestamp of when the new comment was posted. - */ - $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment ); - if ( $flood_die ) { - /** - * Fires before the comment flood message is triggered. - * - * @since 1.5.0 - * - * @param int $time_lastcomment Timestamp of when the last comment was posted. - * @param int $time_newcomment Timestamp of when the new comment was posted. - */ - do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment ); - - if ( true === $avoid_die ) { - return true; - } else { - /** - * Filters the comment flood error message. - * - * @since 5.2.0 - * - * @param string $comment_flood_message Comment flood error message. - */ - $comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) ); - - if ( wp_doing_ajax() ) { - die( $comment_flood_message ); - } - - wp_die( $comment_flood_message, 429 ); - } - } - } - - return false; -} - -/** - * Separates an array of comments into an array keyed by comment_type. - * - * @since 2.7.0 - * - * @param WP_Comment[] $comments Array of comments - * @return WP_Comment[] Array of comments keyed by comment_type. - */ -function separate_comments( &$comments ) { - $comments_by_type = array( - 'comment' => array(), - 'trackback' => array(), - 'pingback' => array(), - 'pings' => array(), - ); - $count = count( $comments ); - for ( $i = 0; $i < $count; $i++ ) { - $type = $comments[ $i ]->comment_type; - if ( empty( $type ) ) { - $type = 'comment'; - } - $comments_by_type[ $type ][] = &$comments[ $i ]; - if ( 'trackback' == $type || 'pingback' == $type ) { - $comments_by_type['pings'][] = &$comments[ $i ]; - } - } - - return $comments_by_type; -} - -/** - * Calculate the total number of comment pages. - * - * @since 2.7.0 - * - * @uses Walker_Comment - * - * @global WP_Query $wp_query WordPress Query object. - * - * @param WP_Comment[] $comments Optional. Array of WP_Comment objects. Defaults to $wp_query->comments. - * @param int $per_page Optional. Comments per page. - * @param bool $threaded Optional. Control over flat or threaded comments. - * @return int Number of comment pages. - */ -function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) { - global $wp_query; - - if ( null === $comments && null === $per_page && null === $threaded && ! empty( $wp_query->max_num_comment_pages ) ) { - return $wp_query->max_num_comment_pages; - } - - if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) ) { - $comments = $wp_query->comments; - } - - if ( empty( $comments ) ) { - return 0; - } - - if ( ! get_option( 'page_comments' ) ) { - return 1; - } - - if ( ! isset( $per_page ) ) { - $per_page = (int) get_query_var( 'comments_per_page' ); - } - if ( 0 === $per_page ) { - $per_page = (int) get_option( 'comments_per_page' ); - } - if ( 0 === $per_page ) { - return 1; - } - - if ( ! isset( $threaded ) ) { - $threaded = get_option( 'thread_comments' ); - } - - if ( $threaded ) { - $walker = new Walker_Comment; - $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page ); - } else { - $count = ceil( count( $comments ) / $per_page ); - } - - return $count; -} - -/** - * Calculate what page number a comment will appear on for comment paging. - * - * @since 2.7.0 - * - * @global wpdb $wpdb WordPress database abstraction object. - * - * @param int $comment_ID Comment ID. - * @param array $args { - * Array of optional arguments. - * @type string $type Limit paginated comments to those matching a given type. Accepts 'comment', - * 'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'. - * Default is 'all'. - * @type int $per_page Per-page count to use when calculating pagination. Defaults to the value of the - * 'comments_per_page' option. - * @type int|string $max_depth If greater than 1, comment page will be determined for the top-level |
