PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/php/commands/comment.php

https://gitlab.com/Blueprint-Marketing/wp-cli
PHP | 458 lines | 429 code | 4 blank | 25 comment | 1 complexity | 2480330835fdb4890bf8b9ba4a279edb MD5 | raw file
  1. <?php
  2. /**
  3. * Manage comments.
  4. *
  5. * ## EXAMPLES
  6. *
  7. * # delete all spam comments.
  8. * wp comment delete $(wp comment list --status=spam --format=ids)
  9. *
  10. * @package wp-cli
  11. */
  12. class Comment_Command extends \WP_CLI\CommandWithDBObject {
  13. protected $obj_type = 'comment';
  14. protected $obj_id_key = 'comment_ID';
  15. protected $obj_fields = array(
  16. 'comment_ID',
  17. 'comment_post_ID',
  18. 'comment_date',
  19. 'comment_approved',
  20. 'comment_author',
  21. 'comment_author_email',
  22. );
  23. public function __construct() {
  24. $this->fetcher = new \WP_CLI\Fetchers\Comment;
  25. }
  26. /**
  27. * Insert a comment.
  28. *
  29. * ## OPTIONS
  30. *
  31. * --<field>=<value>
  32. * : Associative args for the new comment. See wp_insert_comment().
  33. *
  34. * [--porcelain]
  35. * : Output just the new comment id.
  36. *
  37. * ## EXAMPLES
  38. *
  39. * wp comment create --comment_post_ID=15 --comment_content="hello blog" --comment_author="wp-cli"
  40. */
  41. public function create( $args, $assoc_args ) {
  42. parent::_create( $args, $assoc_args, function ( $params ) {
  43. $post_id = $params['comment_post_ID'];
  44. $post = get_post( $post_id );
  45. if ( !$post ) {
  46. return new WP_Error( 'no_post', "Can't find post $post_id." );
  47. }
  48. // We use wp_insert_comment() instead of wp_new_comment() to stay at a low level and
  49. // avoid wp_die() formatted messages or notifications
  50. $comment_id = wp_insert_comment( $params );
  51. if ( !$comment_id ) {
  52. return new WP_Error( 'db_error', 'Could not create comment.' );
  53. }
  54. return $comment_id;
  55. } );
  56. }
  57. /**
  58. * Update one or more comments.
  59. *
  60. * ## OPTIONS
  61. *
  62. * <id>...
  63. * : One or more IDs of comments to update.
  64. *
  65. * --<field>=<value>
  66. * : One or more fields to update. See wp_update_comment().
  67. *
  68. * ## EXAMPLES
  69. *
  70. * wp comment update 123 --comment_author='That Guy'
  71. */
  72. public function update( $args, $assoc_args ) {
  73. parent::_update( $args, $assoc_args, function ( $params ) {
  74. if ( !wp_update_comment( $params ) ) {
  75. return new WP_Error( 'Could not update comment.' );
  76. }
  77. return true;
  78. } );
  79. }
  80. /**
  81. * Get a single comment.
  82. *
  83. * ## OPTIONS
  84. *
  85. * <id>
  86. * : The comment to get.
  87. *
  88. * [--field=<field>]
  89. * : Instead of returning the whole comment, returns the value of a single field.
  90. *
  91. * [--fields=<fields>]
  92. * : Limit the output to specific fields. Defaults to all fields.
  93. *
  94. * [--format=<format>]
  95. * : Accepted values: table, json, csv. Default: table
  96. *
  97. * ## EXAMPLES
  98. *
  99. * wp comment get 1 --field=content
  100. */
  101. public function get( $args, $assoc_args ) {
  102. $comment_id = (int)$args[0];
  103. $comment = get_comment( $comment_id );
  104. if ( empty( $comment ) ) {
  105. WP_CLI::error( "Invalid comment ID." );
  106. }
  107. if ( empty( $assoc_args['fields'] ) ) {
  108. $comment_array = get_object_vars( $comment );
  109. $assoc_args['fields'] = array_keys( $comment_array );
  110. }
  111. $formatter = $this->get_formatter( $assoc_args );
  112. $formatter->display_item( $comment );
  113. }
  114. /**
  115. * Get a list of comments.
  116. *
  117. * ## OPTIONS
  118. *
  119. * [--<field>=<value>]
  120. * : One or more args to pass to WP_Comment_Query.
  121. *
  122. * [--field=<field>]
  123. * : Prints the value of a single field for each comment.
  124. *
  125. * [--fields=<fields>]
  126. * : Limit the output to specific object fields.
  127. *
  128. * [--format=<format>]
  129. * : Accepted values: table, csv, json, count. Default: table
  130. *
  131. * ## AVAILABLE FIELDS
  132. *
  133. * These fields will be displayed by default for each comment:
  134. *
  135. * * comment_ID
  136. * * comment_post_ID
  137. * * comment_date
  138. * * comment_approved
  139. * * comment_author
  140. * * comment_author_email
  141. *
  142. * These fields are optionally available:
  143. *
  144. * * comment_author_url
  145. * * comment_author_IP
  146. * * comment_date_gmt
  147. * * comment_content
  148. * * comment_karma
  149. * * comment_agent
  150. * * comment_type
  151. * * comment_parent
  152. * * user_id
  153. *
  154. * ## EXAMPLES
  155. *
  156. * wp comment list --field=ID
  157. *
  158. * wp comment list --post_id=2
  159. *
  160. * wp comment list --number=20 --status=approve
  161. *
  162. * @subcommand list
  163. */
  164. public function list_( $_, $assoc_args ) {
  165. $formatter = $this->get_formatter( $assoc_args );
  166. if ( 'ids' == $formatter->format )
  167. $assoc_args['fields'] = 'comment_ID';
  168. $query = new WP_Comment_Query();
  169. $comments = $query->query( $assoc_args );
  170. if ( 'ids' == $formatter->format ) {
  171. $comments = wp_list_pluck( $comments, 'comment_ID' );
  172. }
  173. $formatter->display_items( $comments );
  174. }
  175. /**
  176. * Delete a comment.
  177. *
  178. * ## OPTIONS
  179. *
  180. * <id>...
  181. * : One or more IDs of comments to delete.
  182. *
  183. * [--force]
  184. * : Skip the trash bin.
  185. *
  186. * ## EXAMPLES
  187. *
  188. * wp comment delete 1337 --force
  189. *
  190. * wp comment delete 1337 2341 --force
  191. */
  192. public function delete( $args, $assoc_args ) {
  193. parent::_delete( $args, $assoc_args, function ( $comment_id, $assoc_args ) {
  194. $r = wp_delete_comment( $comment_id, isset( $assoc_args['force'] ) );
  195. if ( $r ) {
  196. return array( 'success', "Deleted comment $comment_id." );
  197. } else {
  198. return array( 'error', "Failed deleting comment $comment_id" );
  199. }
  200. } );
  201. }
  202. private function call( $args, $status, $success, $failure ) {
  203. list( $comment_id ) = $args;
  204. $func = sprintf( 'wp_%s_comment', $status );
  205. if ( $func( $comment_id ) ) {
  206. WP_CLI::success( "$success comment $comment_id." );
  207. } else {
  208. WP_CLI::error( "$failure comment $comment_id" );
  209. }
  210. }
  211. private function set_status( $args, $status, $success ) {
  212. $comment = $this->fetcher->get_check( $args[0] );
  213. $r = wp_set_comment_status( $comment->comment_ID, $status, true );
  214. if ( is_wp_error( $r ) ) {
  215. WP_CLI::error( $r );
  216. } else {
  217. WP_CLI::success( "$success comment $comment->comment_ID" );
  218. }
  219. }
  220. /**
  221. * Trash a comment.
  222. *
  223. * ## OPTIONS
  224. *
  225. * <id>
  226. * : The ID of the comment to trash.
  227. *
  228. * ## EXAMPLES
  229. *
  230. * wp comment trash 1337
  231. */
  232. public function trash( $args, $assoc_args ) {
  233. $this->call( $args, __FUNCTION__, 'Trashed', 'Failed trashing' );
  234. }
  235. /**
  236. * Untrash a comment.
  237. *
  238. * ## OPTIONS
  239. *
  240. * <id>
  241. * : The ID of the comment to untrash.
  242. *
  243. * ## EXAMPLES
  244. *
  245. * wp comment untrash 1337
  246. */
  247. public function untrash( $args, $assoc_args ) {
  248. $this->call( $args, __FUNCTION__, 'Untrashed', 'Failed untrashing' );
  249. }
  250. /**
  251. * Spam a comment.
  252. *
  253. * ## OPTIONS
  254. *
  255. * <id>
  256. * : The ID of the comment to mark as spam.
  257. *
  258. * ## EXAMPLES
  259. *
  260. * wp comment spam 1337
  261. */
  262. public function spam( $args, $assoc_args ) {
  263. $this->call( $args, __FUNCTION__, 'Marked as spam', 'Failed marking as spam' );
  264. }
  265. /**
  266. * Unspam a comment.
  267. *
  268. * ## OPTIONS
  269. *
  270. * <id>
  271. * : The ID of the comment to unmark as spam.
  272. *
  273. * ## EXAMPLES
  274. *
  275. * wp comment unspam 1337
  276. */
  277. public function unspam( $args, $assoc_args ) {
  278. $this->call( $args, __FUNCTION__, 'Unspammed', 'Failed unspamming' );
  279. }
  280. /**
  281. * Approve a comment.
  282. *
  283. * ## OPTIONS
  284. *
  285. * <id>
  286. * : The ID of the comment to approve.
  287. *
  288. * ## EXAMPLES
  289. *
  290. * wp comment approve 1337
  291. */
  292. public function approve( $args, $assoc_args ) {
  293. $this->set_status( $args, 'approve', "Approved" );
  294. }
  295. /**
  296. * Unapprove a comment.
  297. *
  298. * ## OPTIONS
  299. *
  300. * <id>
  301. * : The ID of the comment to unapprove.
  302. *
  303. * ## EXAMPLES
  304. *
  305. * wp comment unapprove 1337
  306. */
  307. public function unapprove( $args, $assoc_args ) {
  308. $this->set_status( $args, 'hold', "Unapproved" );
  309. }
  310. /**
  311. * Count comments, on whole blog or on a given post.
  312. *
  313. * ## OPTIONS
  314. *
  315. * [<post-id>]
  316. * : The ID of the post to count comments in.
  317. *
  318. * ## EXAMPLES
  319. *
  320. * wp comment count
  321. * wp comment count 42
  322. */
  323. public function count( $args, $assoc_args ) {
  324. $post_id = isset( $args[0] ) ? $args[0] : 0;
  325. $count = wp_count_comments( $post_id );
  326. // Move total_comments to the end of the object
  327. $total = $count->total_comments;
  328. unset( $count->total_comments );
  329. $count->total_comments = $total;
  330. foreach ( $count as $status => $count ) {
  331. WP_CLI::line( str_pad( "$status:", 17 ) . $count );
  332. }
  333. }
  334. /**
  335. * Get status of a comment.
  336. *
  337. * ## OPTIONS
  338. *
  339. * <id>
  340. * : The ID of the comment to check.
  341. *
  342. * ## EXAMPLES
  343. *
  344. * wp comment status 1337
  345. */
  346. public function status( $args, $assoc_args ) {
  347. list( $comment_id ) = $args;
  348. $status = wp_get_comment_status( $comment_id );
  349. if ( false === $status ) {
  350. WP_CLI::error( "Could not check status of comment $comment_id." );
  351. } else {
  352. WP_CLI::line( $status );
  353. }
  354. }
  355. /**
  356. * Verify whether a comment exists.
  357. *
  358. * ## OPTIONS
  359. *
  360. * <id>
  361. * : The ID of the comment to check.
  362. *
  363. * ## EXAMPLES
  364. *
  365. * wp comment exists 1337
  366. */
  367. public function exists( $args ) {
  368. if ( $this->fetcher->get( $args[0] ) ) {
  369. WP_CLI::success( "Comment with ID $args[0] exists." );
  370. }
  371. }
  372. /**
  373. * Get comment url
  374. *
  375. * ## OPTIONS
  376. *
  377. * <id>...
  378. * : One or more IDs of comments to get the URL.
  379. *
  380. * ## EXAMPLES
  381. *
  382. * wp comment url 123
  383. */
  384. public function url( $args ) {
  385. parent::_url( $args, 'get_comment_link' );
  386. }
  387. }
  388. /**
  389. * Manage comment custom fields.
  390. *
  391. * ## OPTIONS
  392. *
  393. * --format=json
  394. * : Encode/decode values as JSON.
  395. *
  396. * ## EXAMPLES
  397. *
  398. * wp comment meta set 123 description "Mary is a WordPress developer."
  399. */
  400. class Comment_Meta_Command extends \WP_CLI\CommandWithMeta {
  401. protected $meta_type = 'comment';
  402. /**
  403. * Check that the comment ID exists
  404. *
  405. * @param int
  406. */
  407. protected function check_object_id( $object_id ) {
  408. $fetcher = new \WP_CLI\Fetchers\Comment;
  409. $comment = $fetcher->get_check( $object_id );
  410. return $comment->comment_ID;
  411. }
  412. }
  413. WP_CLI::add_command( 'comment', 'Comment_Command' );
  414. WP_CLI::add_command( 'comment meta', 'Comment_Meta_Command' );