/app/core/classes/Comment.php

https://github.com/anrw/classy · PHP · 334 lines · 136 code · 50 blank · 148 comment · 33 complexity · 664a0fe945364139a19312b252392c82 MD5 · raw file

  1. <?php
  2. /**
  3. * Wrapper and helper for WP_Comment class.
  4. *
  5. * @package Classy
  6. */
  7. namespace Classy;
  8. /**
  9. * Class Comment.
  10. */
  11. class Comment extends Basis {
  12. /**
  13. * Comment ID.
  14. *
  15. * @var int
  16. */
  17. public $comment_ID;
  18. /**
  19. * Comment post ID.
  20. *
  21. * @var int
  22. */
  23. public $comment_post_ID;
  24. /**
  25. * Comment Author name.
  26. *
  27. * @var string
  28. */
  29. public $comment_author;
  30. /**
  31. * Comment author email.
  32. *
  33. * @var string
  34. */
  35. public $comment_author_email;
  36. /**
  37. * Comment author link.
  38. *
  39. * @var string
  40. */
  41. public $comment_author_url;
  42. /**
  43. * Comment.
  44. *
  45. * @var string
  46. */
  47. public $comment_content;
  48. /**
  49. * Comment approved.
  50. *
  51. * @var boolean
  52. */
  53. public $comment_approved;
  54. /**
  55. * Comment date.
  56. *
  57. * @var string
  58. */
  59. public $comment_date;
  60. /**
  61. * User ID.
  62. *
  63. * @var int
  64. */
  65. public $user_id;
  66. /**
  67. * Comment nested level.
  68. *
  69. * @var int
  70. */
  71. public $level;
  72. /**
  73. * Comment Parent ID.
  74. *
  75. * @var int
  76. */
  77. public $comment_parent;
  78. /**
  79. * Child comments.
  80. *
  81. * @var array
  82. */
  83. protected $children = array();
  84. /**
  85. * Checks if provided arg is instance of WP_Comment and init it.
  86. *
  87. * @param \WP_Comment $item WP_Comment object.
  88. */
  89. public function __construct( $item ) {
  90. if ( is_a( $item, '\WP_Comment' ) ) {
  91. $this->import( $item );
  92. }
  93. }
  94. /**
  95. * Returns User object of comment author.
  96. *
  97. * @return \Classy\Models\User
  98. */
  99. public function author() {
  100. if ( ! $this->user_id ) {
  101. $author = new Models\User( 0 );
  102. if ( isset( $this->comment_author ) && $this->comment_author ) {
  103. $author->name = $this->comment_author;
  104. }
  105. return $author;
  106. }
  107. return new Models\User( $this->user_id );
  108. }
  109. /**
  110. * Returns comment content.
  111. *
  112. * @return string
  113. */
  114. public function content() {
  115. return apply_filters( 'get_comment_text ', $this->comment_content );
  116. }
  117. /**
  118. * Return true if comment is approved.
  119. *
  120. * @return boolean
  121. */
  122. public function approved() {
  123. return $this->comment_approved;
  124. }
  125. /**
  126. * Returns comment date.
  127. *
  128. * @param string $date_format Optional. PHP date format defaults to the date_format option if not specified.
  129. *
  130. * @return string
  131. */
  132. public function date( $date_format = '' ) {
  133. $df = $date_format ? $date_format : get_option( 'date_format' );
  134. $the_date = (string) mysql2date( $df, $this->comment_date );
  135. return apply_filters( 'get_comment_date ', $the_date, $df );
  136. }
  137. /**
  138. * Returns true if comment has parent.
  139. *
  140. * @return boolean
  141. */
  142. public function has_parent() {
  143. return $this->comment_parent > 0;
  144. }
  145. /**
  146. * Shows gravatar.
  147. *
  148. * @param integer $size avatar image size
  149. * @param string $default
  150. *
  151. * @return string
  152. */
  153. public function avatar( $size = 92, $default = '' ) {
  154. if ( ! get_option( 'show_avatars' ) ) {
  155. return false;
  156. }
  157. if ( ! is_numeric( $size ) ) { $size = '92'; }
  158. $email = $this->get_avatar_email();
  159. $email_hash = '';
  160. if ( ! empty( $email ) ) {
  161. $email_hash = md5( strtolower( trim( $email ) ) );
  162. }
  163. $host = $this->get_avatar_host( $email_hash );
  164. $default = $this->get_default_avatar( $default, $email, $size, $host );
  165. if ( ! empty( $email ) ) {
  166. $avatar = $this->get_avatar_url( $default, $host, $email_hash, $size );
  167. } else {
  168. $avatar = $default;
  169. }
  170. return $avatar;
  171. }
  172. /**
  173. * Returns email address that will be used for generating avatar.
  174. *
  175. * @return string
  176. */
  177. protected function get_avatar_email() {
  178. $id = (int) $this->user_id;
  179. $user = get_userdata( $id );
  180. if ( $user ) {
  181. $email = $user->user_email;
  182. } else {
  183. $email = $this->comment_author_email;
  184. }
  185. return $email;
  186. }
  187. /**
  188. * Returns default avatar url.
  189. *
  190. * @param string $default
  191. * @param string $email
  192. * @param int $size
  193. * @param string $host
  194. * @return string
  195. */
  196. protected function get_default_avatar( $default, $email, $size, $host ) {
  197. if ( '/' === substr( $default, 0, 1 ) ) {
  198. $default = home_url() . $default;
  199. }
  200. if ( empty( $default ) ) {
  201. $avatar_default = get_option( 'avatar_default' );
  202. if ( empty( $avatar_default ) ) {
  203. $default = 'mystery';
  204. } else {
  205. $default = $avatar_default;
  206. }
  207. }
  208. if ( 'mystery' === $default ) {
  209. $default = $host . '/avatar/ad516503a11cd5ca435acc9bb6523536?s=' . $size;
  210. // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
  211. } else if ( 'blank' === $default ) {
  212. $default = $email ? 'blank' : includes_url( 'images/blank.gif' );
  213. } else if ( ! empty( $email ) && 'gravatar_default' === $default ) {
  214. $default = '';
  215. } else if ( 'gravatar_default' === $default ) {
  216. $default = $host . '/avatar/?s=' . $size;
  217. } else if ( empty( $email ) && ! strstr( $default, 'http://' ) ) {
  218. $default = $host . '/avatar/?d=' . $default . '&amp;s=' . $size;
  219. }
  220. return $default;
  221. }
  222. /**
  223. * Returns gravatar host.
  224. *
  225. * @param string $email_hash
  226. * @return string
  227. */
  228. protected function get_avatar_host( $email_hash ) {
  229. if ( is_ssl() ) {
  230. $host = 'https://secure.gravatar.com';
  231. } else {
  232. if ( ! empty( $email_hash ) ) {
  233. $host = sprintf( 'http://%d.gravatar.com', (hexdec( $email_hash[0] ) % 2) );
  234. } else {
  235. $host = 'http://0.gravatar.com';
  236. }
  237. }
  238. return $host;
  239. }
  240. /**
  241. * Returns avatar url
  242. *
  243. * @param string $default
  244. * @param string $host
  245. * @param string $email_hash
  246. * @param int $size
  247. * @return string
  248. */
  249. protected function get_avatar_url( $default, $host, $email_hash, $size ) {
  250. $_return = $host . '/avatar/' . $email_hash . '?s=' . $size . '&amp;d=' . urlencode( $default );
  251. $rating = get_option( 'avatar_rating' );
  252. if ( ! empty( $rating ) ) {
  253. $_return .= '&amp;r=' . $rating;
  254. }
  255. return str_replace( '&#038;', '&amp;', esc_url( $_return ) );
  256. }
  257. /**
  258. * Adds child to current Comment
  259. *
  260. * @param Comment $comment
  261. */
  262. public function add_child( $comment ) {
  263. $this->children[] = $comment;
  264. $item->level = $this->level + 1;
  265. if ( $item->children ) {
  266. $this->update_child_levels();
  267. }
  268. }
  269. /**
  270. * Updates children nesting level param
  271. *
  272. * @return boolean
  273. */
  274. protected function update_child_levels() {
  275. if ( is_array( $this->children ) ) {
  276. foreach ( $this->children as $child ) {
  277. $child->level = $this->level + 1;
  278. $child->update_child_levels();
  279. }
  280. return true;
  281. }
  282. return false;
  283. }
  284. }