comment.php 14.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
<?php

/*
Plugin Name: Comments
Description:
Version: 1.0
Author: Janis Elsts

ModuleID: comment
ModuleCategory: container
ModuleClassName: blcCommentManager
*/

class blcComment extends blcContainer {

	/**
	 * Retrieve the comment wrapped by this container.
	 * The fetched object will also be cached in the $wrapped_object variable.
	 *
	 * @access protected
	 *
	 * @param bool $ensure_consistency
	 * @return object The comment.
	 */
	function get_wrapped_object( $ensure_consistency = false ) {
		if ( $ensure_consistency || is_null( $this->wrapped_object ) ) {
			$this->wrapped_object = get_comment( $this->container_id );
		}
		return $this->wrapped_object;
	}

	/**
	 * Update the comment wrapped by the container with values currently in the $wrapped_object.
	 *
	 * @access protected
	 *
	 * @return bool|WP_Error True on success, an error if something went wrong.
	 */
	function update_wrapped_object() {
		if ( is_null( $this->wrapped_object ) ) {
			return new WP_Error(
				'no_wrapped_object',
				__( 'Nothing to update', 'broken-link-checker' )
			);
		}

		$data = (array) $this->wrapped_object;
		if ( wp_update_comment( $data ) ) {
			return true;
		} else {
			return new WP_Error(
				'update_failed',
				sprintf( __( 'Updating comment %d failed', 'broken-link-checker' ), $this->container_id )
			);
		}
	}

	/**
	 * Delete the comment corresponding to this container.
	 * This will actually move the comment to the trash in newer versions of WP.
	 *
	 * @return bool|WP_error
	 */
	function delete_wrapped_object() {
		if ( EMPTY_TRASH_DAYS ) {
			return $this->trash_wrapped_object();
		} else {
			if ( wp_delete_comment( $this->container_id, true ) ) {
				return true;
			} else {
				return new WP_Error(
					'delete_failed',
					sprintf(
						__( 'Failed to delete comment %d', 'broken-link-checker' ),
						$this->container_id
					)
				);
			}
		}
	}

	/**
	 * Delete the comment corresponding to this container.
	 * This will actually move the comment to the trash in newer versions of WP.
	 *
	 * @return bool|WP_error
	 */
	function trash_wrapped_object() {
		if ( wp_trash_comment( $this->container_id ) ) {
			return true;
		} else {
			return new WP_Error(
				'trash_failed',
				sprintf(
					__( 'Can\'t move comment %d to the trash', 'broken-link-checker' ),
					$this->container_id
				)
			);
		}
	}

	/**
	 * Check if the current user can delete/trash this comment.
	 *
	 * @return bool
	 */
	function current_user_can_delete() {
		//TODO: Fix for custom post types? WP itself doesn't care, at least in 3.0.
		$comment = $this->get_wrapped_object();
		return current_user_can( 'edit_post', $comment->comment_post_ID );
	}

	function can_be_trashed() {
		return defined( 'EMPTY_TRASH_DAYS' ) && EMPTY_TRASH_DAYS;
	}

	/**
	 * Get the default link text to use for links found in a specific container field.
	 * For links in the comment body there is no default link text. For author links,
	 * the link text will be equal to the author name + comment type (if any).
	 *
	 * @param string $field
	 * @return string
	 */
	function default_link_text( $field = '' ) {

		if ( 'comment_author_url' == $field ) {
			$w = $this->get_wrapped_object();
			if ( ! is_null( $w ) ) {
				$text = $w->comment_author;

				//This lets us identify pingbacks & trackbacks.
				if ( ! empty( $w->comment_type ) ) {
					$text .= sprintf( ' [%s]', $w->comment_type );
				}

				return $text;
			}
		}

		return '';
	}

	function ui_get_action_links( $container_field ) {
		$actions = array();

		$comment = $this->get_wrapped_object();
		$post    = get_post( $comment->comment_post_ID ); /* @var StdClass $post */

		//If the post type no longer exists, we can't really do anything with this comment.
		//WordPress will just throw errors if we try.
		if ( ! post_type_exists( get_post_type( $post ) ) ) {
			return $actions;
		}

		//Display Edit & Delete/Trash links only if the user has the right caps.
		$user_can = current_user_can( 'edit_post', $comment->comment_post_ID );
		if ( $user_can ) {
			$actions['edit'] = "<a href='" . $this->get_edit_url() . "' title='" . esc_attr__( 'Edit comment' ) . "'>" . __( 'Edit' ) . '</a>';

			$del_nonce  = esc_html( '_wpnonce=' . wp_create_nonce( "delete-comment_$comment->comment_ID" ) );
			$trash_url  = esc_url( admin_url( "comment.php?action=trashcomment&p=$post->ID&c=$comment->comment_ID&$del_nonce" ) );
			$delete_url = esc_url( admin_url( "comment.php?action=deletecomment&p=$post->ID&c=$comment->comment_ID&$del_nonce" ) );

			if ( ! constant( 'EMPTY_TRASH_DAYS' ) ) {
				$actions['delete'] = "<a href='$delete_url' class='delete:the-comment-list:comment-$comment->comment_ID::delete=1 delete vim-d vim-destructive submitdelete'>" . __( 'Delete Permanently' ) . '</a>';
			} else {
				$actions['trash'] = "<a href='$trash_url' class='delete:the-comment-list:comment-$comment->comment_ID::trash=1 delete vim-d vim-destructive submitdelete' title='" . esc_attr__( 'Move this comment to the trash' ) . "'>" . _x( 'Trash', 'verb' ) . '</a>';
			}
		}

		$actions['view'] = '<span class="view"><a href="' . get_comment_link( $this->container_id ) . '" title="' . esc_attr( __( 'View comment', 'broken-link-checker' ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';

		return $actions;
	}

	function ui_get_source( $container_field = '', $context = 'display' ) {
		//Display a comment icon.
		if ( 'comment_author_url' == $container_field ) {
			$image = 'font-awesome/font-awesome-user.png';
		} else {
			$image = 'font-awesome/font-awesome-comment-alt.png';
		}

		$image = sprintf(
			'<img src="%s/broken-link-checker/legacy/images/%s" class="blc-small-image" title="%3$s" alt="%3$s"> ',
			WP_PLUGIN_URL,
			$image,
			__( 'Comment', 'broken-link-checker' )
		);

		$comment = $this->get_wrapped_object();

		//Display a small text sample from the comment
		$text_sample = strip_tags( $comment->comment_content );
		$text_sample = blcUtility::truncate( $text_sample, 65 );

		$html = sprintf(
			'<a href="%s" title="%s"><b>%s</b> &mdash; %s</a>',
			$this->get_edit_url(),
			esc_attr__( 'Edit comment' ),
			esc_attr( $comment->comment_author ),
			$text_sample
		);

		//Don't show the image in email notifications.
		if ( 'email' != $context ) {
			$html = $image . $html;
		}

		return $html;
	}

	function get_edit_url() {
		return esc_url( admin_url( "comment.php?action=editcomment&c={$this->container_id}" ) );
	}

	function base_url() {
		$comment_permalink = get_comment_link( $this->container_id );
		return substr( $comment_permalink, 0, strpos( $comment_permalink, '#' ) );
	}
}

class blcCommentManager extends blcContainerManager {
	var $container_class_name = 'blcComment';

	var $fields = array(
		'comment_author_url' => 'url_field',
		'comment_content'    => 'html',
	);

	function init() {
		parent::init();

		add_action( 'post_comment', array( $this, 'hook_post_comment' ), 10, 2 );
		add_action( 'edit_comment', array( $this, 'hook_edit_comment' ) );
		add_action( 'transition_comment_status', array( $this, 'hook_comment_status' ), 10, 3 );

		add_action( 'trashed_post_comments', array( $this, 'hook_trashed_post_comments' ), 10, 2 );
		add_action( 'untrash_post_comments', array( $this, 'hook_untrash_post_comments' ) );
	}

	function hook_post_comment( $comment_id, $comment_status ) {
		if ( '1' == $comment_status ) {
			$container = blcContainerHelper::get_container( array( $this->container_type, $comment_id ) );
			$container->mark_as_unsynched();
		}
	}

	function hook_edit_comment( $comment_id ) {
		if ( 'approved' == wp_get_comment_status( $comment_id ) ) {
			$container = blcContainerHelper::get_container( array( $this->container_type, $comment_id ) );
			$container->mark_as_unsynched();
		}
	}

	function hook_comment_status( $new_status, $old_status, $comment ) {
		//We only care about approved comments.
		if ( ( 'approved' == $new_status ) || ( 'approved' == $old_status ) ) {
			$container = blcContainerHelper::get_container( array( $this->container_type, $comment->comment_ID ) );
			if ( 'approved' == $new_status ) {
				$container->mark_as_unsynched();
			} else {
				$container->delete();
				blc_cleanup_links();
			}
		}
	}

	function hook_trashed_post_comments( /** @noinspection PhpUnusedParameterInspection */$post_id, $statuses ) {
		foreach ( $statuses as $comment_id => $comment_status ) {
			if ( '1' == $comment_status ) {
				$container = blcContainerHelper::get_container( array( $this->container_type, $comment_id ) );
				$container->delete();
			}
		}
		blc_cleanup_links();
	}

	function hook_untrash_post_comments( $post_id ) {
		//Unlike with the 'trashed_post_comments' hook, WP doesn't pass the list of (un)trashed
		//comments to callbacks assigned to the 'untrash_post_comments' and 'untrashed_post_comments'
		//actions. Therefore, we must read it from the appropriate metadata entry.
		$statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true );
		if ( empty( $statuses ) || ! is_array( $statuses ) ) {
			return;
		}

		foreach ( $statuses as $comment_id => $comment_status ) {
			if ( '1' == $comment_status ) { //if approved
				$container = blcContainerHelper::get_container( array( $this->container_type, $comment_id ) );
				$container->mark_as_unsynched();
			}
		}
	}

	/**
	 * Create or update synchronization records for all comments.
	 *
	 * @param bool $forced If true, assume that all synch. records are gone and will need to be recreated from scratch.
	 * @return void
	 */
	function resynch( $forced = false ) {
		global $wpdb; /* @var wpdb $wpdb */
		global $blclog;

		if ( $forced ) {
			//Create new synchronization records for all comments.
			$blclog->log( '...... Creating synch. records for comments' );
			$start = microtime( true );
			$q     = "INSERT INTO {$wpdb->prefix}blc_synch(container_id, container_type, synched)
				  SELECT comment_ID, '{$this->container_type}', 0
				  FROM {$wpdb->comments}
				  WHERE
				  	{$wpdb->comments}.comment_approved = '1'";
			$wpdb->query( $q ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			$blclog->log( sprintf( '...... %d rows inserted in %.3f seconds', $wpdb->rows_affected, microtime( true ) - $start ) );
		} else {
			//Delete synch records corresponding to comments that no longer exist
			//or have been trashed/spammed/unapproved.
			$blclog->log( '...... Deleting synch. records for removed comments' );
			$start = microtime( true );
			$q     = "DELETE synch.*
				  FROM
					 {$wpdb->prefix}blc_synch AS synch LEFT JOIN {$wpdb->comments} AS comments
					 ON comments.comment_ID = synch.container_id
				  WHERE
					 synch.container_type = '{$this->container_type}'
					 AND (comments.comment_ID IS NULL OR comments.comment_approved <> '1')";
			$wpdb->query( $q ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			$blclog->log( sprintf( '...... %d rows deleted in %.3f seconds', $wpdb->rows_affected, microtime( true ) - $start ) );

			//Create synch. records for comments that don't have them.
			$blclog->log( '...... Creating synch. records for new comments' );
			$start = microtime( true );
			$q     = "INSERT INTO {$wpdb->prefix}blc_synch(container_id, container_type, synched)
				  SELECT comment_ID, '{$this->container_type}', 0
				  FROM
				    {$wpdb->comments} AS comments LEFT JOIN {$wpdb->prefix}blc_synch AS synch
					ON (synch.container_id = comments.comment_ID and synch.container_type='{$this->container_type}')
				  WHERE
				  	comments.comment_approved = '1'
					AND synch.container_id IS NULL";
			$wpdb->query( $q ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			$blclog->log( sprintf( '...... %d rows inserted in %.3f seconds', $wpdb->rows_affected, microtime( true ) - $start ) );

			/*
			Note that there is no way to detect comments that were *edited* (not added - those
			will be caught by the above query) while the plugin was inactive. Unlike with posts,
			WP doesn't track comment modification times.
			*/
		}
	}

	/**
	 * Get the message to display after $n comments have been deleted.
	 *
	 * @param int $n Number of deleted comments.
	 * @return string A delete confirmation message, e.g. "5 comments were deleted"
	 */
	function ui_bulk_delete_message( $n ) {
		if ( EMPTY_TRASH_DAYS ) {
			return $this->ui_bulk_trash_message( $n );
		} else {
			return sprintf(
				_n(
					'%d comment has been deleted.',
					'%d comments have been deleted.',
					$n,
					'broken-link-checker'
				),
				$n
			);
		}
	}

	/**
	 * Get the message to display after $n comments have been moved to the trash.
	 *
	 * @param int $n Number of trashed comments.
	 * @return string A delete confirmation message, e.g. "5 comments were moved to trash"
	 */
	function ui_bulk_trash_message( $n ) {
		return sprintf(
			_n(
				'%d comment moved to the Trash.',
				'%d comments moved to the Trash.',
				$n,
				'broken-link-checker'
			),
			$n
		);
	}

	/**
	 * Instantiate multiple containers of the container type managed by this class.
	 *
	 * @param array $containers Array of assoc. arrays containing container data.
	 * @param string $purpose An optional code indicating how the retrieved containers will be used.
	 * @param bool $load_wrapped_objects Preload wrapped objects regardless of purpose.
	 *
	 * @return array of blcPostContainer indexed by "container_type|container_id"
	 */
	function get_containers( $containers, $purpose = '', $load_wrapped_objects = false ) {
		global $wpdb; /* @var wpdb $wpdb */

		$containers = $this->make_containers( $containers );

		//Preload comment data if it is likely to be useful later
		$preload = $load_wrapped_objects || in_array( $purpose, array( BLC_FOR_DISPLAY, BLC_FOR_PARSING ) );
		if ( $preload ) {
			$comment_ids = array();
			foreach ( $containers as $container ) { /* @var blcContainer $container */
				$comment_ids[] = $container->container_id;
			}

			//There's no WP function for retrieving multiple comments by their IDs,
			//so we query the DB directly.
			$q        = "SELECT * FROM {$wpdb->comments} WHERE comment_ID IN (" . implode( ', ', $comment_ids ) . ')';
			$comments = $wpdb->get_results( $q ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

			foreach ( $comments as $comment ) {
				//Cache the comment in the internal WP object cache
				$comment = get_comment( $comment ); /* @var StdClass $comment */

				//Attach it to the container
				$key = $this->container_type . '|' . $comment->comment_ID;
				if ( isset( $containers[ $key ] ) ) {
					$containers[ $key ]->wrapped_object = $comment;
				}
			}
		}

		return $containers;
	}
}