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

/wp-content/plugins/social/lib/social/comment/form.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net
PHP | 411 lines | 219 code | 47 blank | 145 comment | 16 complexity | 5ef388f480f148399c8d06b3620c1804 MD5 | raw file
  1. <?php
  2. /**
  3. * Just a singleton for filter methods to live under.
  4. *
  5. * @uses Social
  6. * @package Social
  7. * @subpackage comment
  8. */
  9. final class Social_Comment_Form {
  10. /**
  11. * @var array comment form instances
  12. */
  13. protected static $instances = array();
  14. /**
  15. * Loads an instance of the comment form.
  16. *
  17. * @static
  18. * @param int $post_id
  19. * @param array $args
  20. * @return Social_Comment_Form
  21. */
  22. public static function instance($post_id, array $args = array()) {
  23. if (!isset(self::$instances[$post_id])) {
  24. self::$instances[$post_id] = new self($post_id, $args);
  25. }
  26. return self::$instances[$post_id];
  27. }
  28. /**
  29. * @var object post
  30. */
  31. protected $post = null;
  32. /**
  33. * @var int post ID
  34. */
  35. protected $post_id = 0;
  36. /**
  37. * @var array arguments to pass into the comment form
  38. */
  39. protected $args = array();
  40. /**
  41. * @var bool is logged in flag
  42. */
  43. protected $is_logged_in = false;
  44. /**
  45. * @var WP_User current user object
  46. */
  47. protected $current_user = null;
  48. /**
  49. * Initializes the comment form.
  50. *
  51. * @param int $post_id
  52. * @param array $args
  53. */
  54. public function __construct($post_id, array $args = array()) {
  55. global $post;
  56. $this->post_id = $post_id;
  57. $this->args = $args;
  58. $this->is_logged_in = is_user_logged_in();
  59. $this->current_user = wp_get_current_user();
  60. if ($post === null) {
  61. $post = get_post($this->post_id);
  62. }
  63. $this->post = $post;
  64. }
  65. /**
  66. * Magic method to render the form on echo.
  67. *
  68. * @return string
  69. */
  70. public function __toString() {
  71. return $this->render();
  72. }
  73. /**
  74. * Renders the comment form HTML.
  75. *
  76. * @static
  77. * @return string
  78. */
  79. public function render() {
  80. ob_start();
  81. try {
  82. $this->attach_hooks();
  83. comment_form($this->args, $this->post_id);
  84. $this->remove_hooks();
  85. }
  86. catch (Exception $e) {
  87. ob_end_clean();
  88. throw $e;
  89. }
  90. $comment_form = ob_get_clean();
  91. return preg_replace('/<h3 id="reply-title">(.+)<\/h3>/', '<h3 id="reply-title"><span>$1</span></h3>', $comment_form);
  92. }
  93. /**
  94. * Attaches hooks before rending the comment form.
  95. *
  96. * @return void
  97. */
  98. private function attach_hooks() {
  99. add_action('comment_form_top', array($this, 'top'));
  100. add_action('comment_form_defaults', array($this, 'configure_args'));
  101. add_action('comment_form', array($this, 'comment_form_wrapper_open'), 0);
  102. add_action('comment_form', array($this, 'comment_form_wrapper_close'), 99999);
  103. add_filter('comment_form_logged_in', array($this, 'logged_in_as'));
  104. add_filter('comment_id_fields', array($this, 'comment_id_fields'), 10, 3);
  105. }
  106. /**
  107. * Removes the hooks after rending the comment form.
  108. *
  109. * @return void
  110. */
  111. private function remove_hooks() {
  112. remove_action('comment_form_top', array($this, 'top'));
  113. remove_action('comment_form_defaults', array($this, 'configure_args'));
  114. remove_action('comment_form', array($this, 'comment_form_wrapper_open'), 0);
  115. remove_action('comment_form', array($this, 'comment_form_wrapper_close'), 99999);
  116. remove_filter('comment_form_logged_in', array($this, 'logged_in_as'));
  117. remove_filter('comment_id_fields', array($this, 'comment_id_fields'), 10, 3);
  118. }
  119. /**
  120. * Echo opening wrapper around comment_form action.
  121. *
  122. * @return void
  123. */
  124. public function comment_form_wrapper_open() {
  125. echo '<div id="commentform-extras">';
  126. }
  127. /**
  128. * Echo closing wrapper around comment_form action.
  129. *
  130. * @return void
  131. */
  132. public function comment_form_wrapper_close() {
  133. echo '</div>';
  134. }
  135. /**
  136. * Creates a fieldgroup.
  137. *
  138. * @param string $label
  139. * @param string $id
  140. * @param string $tag
  141. * @param string $text
  142. * @param array $attr1
  143. * @param array $attr2
  144. * @param string $help_text
  145. * @return string
  146. */
  147. public function to_field_group($label, $id, $tag, $text, $attr1 = array(), $attr2 = array(), $help_text = '') {
  148. $attr = array_merge($attr1, $attr2);
  149. $label = $this->to_tag('label', $label, array(
  150. 'for' => $id,
  151. 'class' => 'social-label'
  152. ));
  153. $input_defaults = array(
  154. 'id' => $id,
  155. 'name' => $id,
  156. 'class' => 'social-input'
  157. );
  158. $input = $this->to_tag($tag, $text, $input_defaults, $attr);
  159. $help = '';
  160. if ($help_text) {
  161. $help = $this->to_tag('small', $help_text, array('class' => 'social-help'));
  162. }
  163. return $this->to_tag('p', $label.$input.$help, array(
  164. 'class' => 'social-input-row social-input-row-'.$id
  165. ));
  166. }
  167. /**
  168. * Helper for generating input row HTML
  169. *
  170. * @param string $label
  171. * @param int $id
  172. * @param string $value
  173. * @param bool $req
  174. * @param string $help_text
  175. * @return string
  176. * @uses Social::to_tag()
  177. */
  178. public function to_input_group($label, $id, $value, $req = false, $help_text = '') {
  179. $maybe_req = ($req ? array('required' => 'required') : array());
  180. return $this->to_field_group($label, $id, 'input', false, $maybe_req, array(
  181. 'type' => 'text',
  182. 'value' => $value
  183. ), $help_text);
  184. }
  185. /**
  186. * Creates a textarea.
  187. *
  188. * @param string $label
  189. * @param string $id
  190. * @param string $value
  191. * @param bool $req
  192. * @return string
  193. */
  194. public function to_textarea_group($label, $id, $value, $req = true) {
  195. $maybe_req = ($req ? array('required' => 'required') : array());
  196. return $this->to_field_group($label, $id, 'textarea', $value, $maybe_req);
  197. }
  198. /**
  199. * @param string $result
  200. * @param string $id
  201. * @param string $replytoid
  202. * @return string
  203. */
  204. public function comment_id_fields($result, $id, $replytoid) {
  205. $html = $this->get_also_post_to_controls();
  206. $html .= $result;
  207. $hidden = array('type' => 'hidden');
  208. $html .= $this->to_tag('input', false, $hidden, array(
  209. 'id' => 'use_twitter_reply',
  210. 'name' => 'use_twitter_reply',
  211. 'value' => 0
  212. ));
  213. $html .= $this->to_tag('input', false, $hidden, array(
  214. 'id' => 'in_reply_to_status_id',
  215. 'name' => 'in_reply_to_status_id',
  216. 'value' => ''
  217. ));
  218. return $html;
  219. }
  220. /**
  221. * @param array $default_args
  222. * @return array
  223. */
  224. public function configure_args(array $default_args) {
  225. $commenter = wp_get_current_commenter();
  226. $req = get_option('require_name_email');
  227. $fields = array(
  228. 'author' => $this->to_input_group(__('Name', 'social'), 'author', $commenter['comment_author'], $req),
  229. 'email' => $this->to_input_group(__('Email', 'social'), 'email', $commenter['comment_author_email'], $req, __('Not published', 'social')),
  230. 'url' => $this->to_input_group(__('Website', 'social'), 'url', $commenter['comment_author_url'])
  231. );
  232. $args = array(
  233. 'label_submit' => __('Post It', 'social'),
  234. 'title_reply' => __('Profile', 'social'),
  235. 'title_reply_to' => __('Post a Reply to %s', 'social'),
  236. 'cancel_reply_link' => __('cancel', 'social'),
  237. 'comment_notes_after' => '',
  238. 'comment_notes_before' => '',
  239. 'fields' => $fields,
  240. 'comment_field' => $this->to_textarea_group(__('Comment', 'social'), 'comment', '', true, 'textarea')
  241. );
  242. if ($this->is_logged_in) {
  243. $override = array(
  244. 'title_reply' => __('Post a Comment', 'social')
  245. );
  246. $args = array_merge($args, $override);
  247. }
  248. return array_merge($default_args, $args);
  249. }
  250. /**
  251. * Outputs checkboxes for cross-posting
  252. *
  253. * @uses Social::to_tag()
  254. * @return string
  255. */
  256. public function get_also_post_to_controls() {
  257. if ($this->is_logged_in and $this->post->post_status != 'private') {
  258. $id = 'post_to_service';
  259. $label_base = array(
  260. 'for' => $id,
  261. 'id' => 'post_to'
  262. );
  263. $checkbox = $this->to_tag('input', false, array(
  264. 'type' => 'checkbox',
  265. 'name' => $id,
  266. 'id' => $id,
  267. 'value' => 1
  268. ));
  269. if (current_user_can('manage_options')) {
  270. $text = sprintf(__('Also post to %s', 'social'), '<span></span>');
  271. $post_to = $this->to_tag('label', $checkbox.' '.$text, $label_base, array('style' => 'display:none;'));
  272. }
  273. else {
  274. $post_to = '';
  275. foreach (Social::instance()->services() as $key => $service) {
  276. if (count($service->accounts())) {
  277. Social::log(print_r($service->accounts(), true));
  278. foreach ($service->accounts() as $account) {
  279. if ($account->personal()) {
  280. $text = sprintf(__('Also post to %s', 'social'), $service->title());
  281. $post_to .= $this->to_tag('label', $checkbox . ' ' . $text, $label_base);
  282. break;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. return $post_to;
  289. }
  290. return '';
  291. }
  292. /**
  293. * Hook for 'comment_form_top' action.
  294. *
  295. * @return void
  296. */
  297. public function top() {
  298. if (!$this->is_logged_in) {
  299. echo Social_View::factory('comment/top', array(
  300. 'services' => Social::instance()->services()
  301. ));
  302. }
  303. }
  304. /**
  305. * Hook for 'comment_form_logged_in' action.
  306. *
  307. * @return string
  308. */
  309. public function logged_in_as() {
  310. $services = Social::instance()->services();
  311. $accounts = array();
  312. foreach ($services as $key => $service) {
  313. if (count($service->accounts())) {
  314. $accounts[$key] = $service->accounts();
  315. }
  316. }
  317. return Social_View::factory('comment/logged_in_as', array(
  318. 'services' => $services,
  319. 'accounts' => $accounts,
  320. 'current_user' => $this->current_user,
  321. ))->render();
  322. }
  323. /**
  324. * Helper for creating HTML tag from strings and arrays of attributes.
  325. *
  326. * @param string $tag
  327. * @param string $text
  328. * @param array $attr1
  329. * @param array $attr2
  330. * @return string
  331. */
  332. private function to_tag($tag, $text = '', $attr1 = array(), $attr2 = array()) {
  333. if (function_exists('esc_attr')) {
  334. $tag = esc_attr($tag);
  335. }
  336. $attrs = $this->to_attr($attr1, $attr2);
  337. if ($text !== false) {
  338. return '<'.$tag.' '.$attrs.'>'.$text.'</'.$tag.'>';
  339. }
  340. // No text == self closing tag
  341. else {
  342. return '<'.$tag.' '.$attrs.' />';
  343. }
  344. }
  345. /**
  346. * Helper: Turn an array or two into HTML attribute string
  347. *
  348. * @param array $arr1
  349. * @param array $arr2
  350. * @return string
  351. */
  352. private function to_attr($arr1 = array(), $arr2 = array()) {
  353. $attrs = array();
  354. $arr = array_merge($arr1, $arr2);
  355. foreach ($arr as $key => $value) {
  356. if (function_exists('esc_attr')) {
  357. $key = esc_attr($key);
  358. $value = esc_attr($value);
  359. }
  360. $attrs[] = $key.'="'.$value.'"';
  361. }
  362. return implode(' ', $attrs);
  363. }
  364. } // End Social_Comment_Form