/includes/plugins/timber-library/lib/timber-comment.php

https://gitlab.com/aristath/maera · PHP · 255 lines · 159 code · 26 blank · 70 comment · 45 complexity · 548293cdf519d22f19daf6d86f16b1b9 MD5 · raw file

  1. <?php
  2. class TimberComment extends TimberCore implements TimberCoreInterface {
  3. public $PostClass = 'TimberPost';
  4. public $object_type = 'comment';
  5. public static $representation = 'comment';
  6. public $ID;
  7. public $id;
  8. public $comment_author_email;
  9. public $comment_content;
  10. public $comment_date;
  11. public $comment_ID;
  12. public $user_id;
  13. public $comment_author;
  14. public $children = array();
  15. /**
  16. * @param int $cid
  17. */
  18. function __construct($cid) {
  19. $this->init($cid);
  20. }
  21. function __toString(){
  22. return $this->content();
  23. }
  24. /**
  25. * @param integer $cid
  26. */
  27. function init($cid) {
  28. $comment_data = $cid;
  29. if (is_integer($cid)) {
  30. $comment_data = get_comment($cid);
  31. }
  32. $this->import($comment_data);
  33. $this->ID = $this->comment_ID;
  34. $this->id = $this->comment_ID;
  35. $comment_meta_data = $this->get_meta_fields($this->ID);
  36. $this->import($comment_meta_data);
  37. }
  38. /**
  39. * @return TimberUser
  40. */
  41. public function author() {
  42. if ($this->user_id) {
  43. return new TimberUser($this->user_id);
  44. } else {
  45. $author = new TimberUser(0);
  46. if (isset($this->comment_author) && $this->comment_author) {
  47. $author->name = $this->comment_author;
  48. } else {
  49. $author->name = 'Anonymous';
  50. }
  51. }
  52. return $author;
  53. }
  54. /**
  55. * @param int $size
  56. * @param string $default
  57. * @return bool|mixed|string
  58. */
  59. public function avatar($size = 92, $default = '') {
  60. // Fetches the Gravatar
  61. // use it like this
  62. // {{comment.avatar(36,template_uri~"/img/dude.jpg")}}
  63. if (!get_option('show_avatars')) {
  64. return false;
  65. }
  66. if (!is_numeric($size)) {
  67. $size = '92';
  68. }
  69. $email = $this->avatar_email();
  70. $email_hash = '';
  71. if (!empty($email)) {
  72. $email_hash = md5(strtolower(trim($email)));
  73. }
  74. $host = $this->avatar_host($email_hash);
  75. $default = $this->avatar_default($default, $email, $size, $host);
  76. if (!empty($email)) {
  77. $avatar = $this->avatar_out($email, $default, $host, $email_hash, $size);
  78. } else {
  79. $avatar = $default;
  80. }
  81. return $avatar;
  82. }
  83. /**
  84. * @return string
  85. */
  86. public function content() {
  87. return $this->comment_content;
  88. }
  89. /**
  90. * @return string
  91. */
  92. public function status() {
  93. return $this->comment_status;
  94. }
  95. /**
  96. * @return string
  97. */
  98. public function date() {
  99. return $this->comment_date;
  100. }
  101. /**
  102. * @param string $field_name
  103. * @return mixed
  104. */
  105. public function meta($field_name) {
  106. return $this->get_meta_field($field_name);
  107. }
  108. /**
  109. * @return bool
  110. */
  111. public function is_child() {
  112. return $this->comment_parent > 0;
  113. }
  114. /**
  115. * @param int $comment_id
  116. * @return mixed
  117. */
  118. private function get_meta_fields($comment_id = null) {
  119. if ($comment_id === null) {
  120. $comment_id = $this->ID;
  121. }
  122. //Could not find a WP function to fetch all comment meta data, so I made one.
  123. apply_filters('timber_comment_get_meta_pre', array(), $comment_id);
  124. $comment_metas = get_comment_meta($comment_id);
  125. foreach ($comment_metas as &$cm) {
  126. if (is_array($cm) && count($cm) == 1) {
  127. $cm = $cm[0];
  128. }
  129. }
  130. $comment_metas = apply_filters('timber_comment_get_meta', $comment_metas, $comment_id);
  131. return $comment_metas;
  132. }
  133. /**
  134. * @param string $field_name
  135. * @return mixed
  136. */
  137. private function get_meta_field($field_name) {
  138. $value = apply_filters('timber_comment_get_meta_field_pre', null, $this->ID, $field_name, $this);
  139. if ($value === null) {
  140. $value = get_comment_meta($this->ID, $field_name, true);
  141. }
  142. $value = apply_filters('timber_comment_get_meta_field', $value, $this->ID, $field_name, $this);
  143. return $value;
  144. }
  145. /* AVATAR Stuff
  146. ======================= */
  147. /**
  148. * @return string
  149. */
  150. private function avatar_email() {
  151. $id = (int)$this->user_id;
  152. $user = get_userdata($id);
  153. if ($user) {
  154. $email = $user->user_email;
  155. } else {
  156. $email = $this->comment_author_email;
  157. }
  158. return $email;
  159. }
  160. /**
  161. * @param string $email_hash
  162. * @return string
  163. */
  164. private function avatar_host($email_hash) {
  165. if (is_ssl()) {
  166. $host = 'https://secure.gravatar.com';
  167. } else {
  168. if (!empty($email_hash)) {
  169. $host = sprintf("http://%d.gravatar.com", (hexdec($email_hash[0]) % 2));
  170. } else {
  171. $host = 'http://0.gravatar.com';
  172. }
  173. }
  174. return $host;
  175. }
  176. /**
  177. * @param string $default
  178. * @param string $email
  179. * @param string $size
  180. * @param string $host
  181. * @return string
  182. */
  183. private function avatar_default($default, $email, $size, $host) {
  184. # what if its relative.
  185. if (substr($default, 0, 1) == '/') {
  186. $default = home_url() . $default;
  187. }
  188. if (empty($default)) {
  189. $avatar_default = get_option('avatar_default');
  190. if (empty($avatar_default)) {
  191. $default = 'mystery';
  192. } else {
  193. $default = $avatar_default;
  194. }
  195. }
  196. if ('mystery' == $default) {
  197. $default = $host . '/avatar/ad516503a11cd5ca435acc9bb6523536?s=' . $size;
  198. // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
  199. } else if ('blank' == $default) {
  200. $default = $email ? 'blank' : includes_url('images/blank.gif');
  201. } else if (!empty($email) && 'gravatar_default' == $default) {
  202. $default = '';
  203. } else if ('gravatar_default' == $default) {
  204. $default = $host . '/avatar/?s=' . $size;
  205. } else if (empty($email) && !strstr($default, 'http://')) {
  206. $default = $host . '/avatar/?d=' . $default . '&amp;s=' . $size;
  207. } else if (strpos($default, 'http://') === 0) {
  208. //theyre just linking to an image so don't do anything else
  209. //$default = add_query_arg( 's', $size, $default );
  210. }
  211. return $default;
  212. }
  213. /**
  214. * @param string $email
  215. * @param string $default
  216. * @param string $host
  217. * @param string $email_hash
  218. * @param string $size
  219. * @return mixed
  220. */
  221. private function avatar_out($email, $default, $host, $email_hash, $size) {
  222. $out = $host . '/avatar/' . $email_hash . '?s=' . $size . '&amp;d=' . urlencode($default);
  223. $rating = get_option('avatar_rating');
  224. if (!empty($rating)) {
  225. $out .= '&amp;r=' . $rating;
  226. }
  227. return str_replace('&#038;', '&amp;', esc_url($out));
  228. }
  229. }