PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/usernoise-pro/inc/controller.php

https://github.com/petergibbons/OpenCounterWP
PHP | 204 lines | 184 code | 20 blank | 0 comment | 15 complexity | d855818c3b1d0bb674a0a1a6fb8bafd3 MD5 | raw file
  1. <?php
  2. class UNPRO_Controller{
  3. function __construct(){
  4. global $un_controller;
  5. add_action('un_after_feedback_form', array(&$this, 'action_un_after_feedback_form'));
  6. add_action('un_before_feedback_form', array(&$this, 'action_un_before_feedback_form'));
  7. add_action('un_feedback_content_top', array(&$this, 'action_un_feedback_content_top'));
  8. add_action('wp_ajax_unpro_get_feedback', array(&$this, 'action_un_get_feedback'));
  9. add_action('wp_ajax_nopriv_unpro_get_feedback', array(&$this, 'action_un_get_feedback'));
  10. add_action('wp_ajax_unpro_like', array(&$this, 'action_like'));
  11. add_action('wp_ajax_nopriv_unpro_like', array(&$this, 'action_like'));
  12. add_action('wp_ajax_unpro_submit_comment', array(&$this, 'action_unpro_submit_comment'));
  13. add_action('wp_ajax_nopriv_unpro_submit_comment', array(&$this, 'action_unpro_submit_comment'));
  14. add_action('wp_ajax_nopriv_unpro_get_comments', array(&$this, 'action_unpro_get_comments'));
  15. add_action('wp_ajax_unpro_get_comments', array(&$this, 'action_unpro_get_comments'));
  16. remove_action('un_feedback_form_body', array(&$un_controller, 'action_un_feedback_form_body'), 100);
  17. add_action('un_feedback_form_body', array(&$this, 'action_un_feedback_form_body'));
  18. add_action('un_after_feedback_wrapper', array(&$this, '_un_after_feedback_wrapper'));
  19. add_filter('un_window_class', array(&$this, '_un_window_class'));
  20. add_action('un_head', array(&$this, '_un_head'));
  21. }
  22. public function action_like(){
  23. $id = (int)$_REQUEST['id'];
  24. global $unpro_model;
  25. setcookie('likes', (isset($_COOKIE['likes']) ? $_COOKIE['likes'] . "," : '') . $id,
  26. time() + 86400 * 365 * 10,'/');
  27. echo $unpro_model->add_like($id);
  28. exit;
  29. }
  30. public function _un_head(){?>
  31. <link rel="stylesheet" href="<?php echo esc_attr(usernoisepro_url('/css/usernoise-pro.css')) ?>" type="text/css">
  32. <link rel="stylesheet" href="<?php echo esc_attr(usernoisepro_url('/css/fixes.css')) ?>" type="text/css">
  33. <script src="<?php echo esc_attr(usernoisepro_url('/vendor/jquery.tinyscrollbar.js')) ?>"></script>
  34. <script src="<?php echo esc_attr(usernoisepro_url('/vendor/jquery.resize.js')) ?>"></script>
  35. <script>
  36. var pro = <?php echo json_encode(array(
  37. 'popular_feedback' => __('Popular feedback', 'usernoise-pro'),
  38. 'popular_idea' => __('Popular ideas', 'usernoise-pro'),
  39. 'popular_problem' => __('Popular problems', 'usernoise-pro'),
  40. 'popular_question' => __('Popular questions', 'usernoise-pro'),
  41. 'popular_praise' => __('Popular praises', 'usernoise-pro'),
  42. 'enable_discussions' => un_get_option(UNPRO_ENABLE_DISCUSSIONS) ? 'true' : 'false',
  43. 'ajaxurl' => admin_url('admin-ajax.php')
  44. )); ?>
  45. </script>
  46. <script src="<?php echo esc_attr(usernoisepro_url('/js/pro.js')) ?>"></script>
  47. <?php
  48. }
  49. public function _un_window_class($classes){
  50. $classes[]= 'pro';
  51. return $classes;
  52. }
  53. public function _un_after_feedback_wrapper(){
  54. global $un_h;
  55. $h = $un_h;
  56. require usernoisepro_path('/html/pro.php');
  57. }
  58. public function action_un_feedback_form_body(){
  59. if (un_get_option(UN_SHOW_POWERED_BY)) require_once(usernoisepro_path('/html/powered-by.php'));
  60. }
  61. public function action_un_get_feedback(){
  62. global $post, $unpro_model;
  63. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  64. $page = (int)$_REQUEST['page'];
  65. $params = array('post_type' => FEEDBACK, 'post_status' => 'publish',
  66. 'paged' => $page, 'posts_per_page' => 5,
  67. 'feedback_type_slug' => $type,
  68. 'orderby' => 'likes', 'order' => 'DESC');
  69. ob_start();
  70. $query = new WP_Query($params);
  71. if ($query->have_posts()){
  72. while($query->have_posts()): $query->the_post();
  73. require(usernoisepro_path('/html/feedback-item.php'));
  74. endwhile;
  75. } else {
  76. $type =
  77. require(usernoisepro_path('/html/feedback-blank-slate.php'));
  78. }
  79. echo json_encode(array(
  80. 'next_page' => $query->max_num_pages > $page ? $page + 1 : null,
  81. 'html' => ob_get_clean()
  82. ));
  83. exit;
  84. }
  85. public function action_unpro_submit_comment(){
  86. global $unpro_model;
  87. $post_id = $_REQUEST['post_id'];
  88. $post = get_post($post_id);
  89. if ($post->post_type != FEEDBACK)
  90. wp_die(__('Hackin, huh?'));
  91. add_action('comment_duplicate_trigger', array(&$this, 'action_comment_duplicate_trigger'));
  92. $comment = array(
  93. 'comment_post_ID' => $post_id,
  94. 'comment_author' => isset($_REQUEST['name']) && stripslashes($_REQUEST['name']) == __('Name') ? '' : (isset($_REQUEST['name']) ? stripslashes($_REQUEST['name']) : ''),
  95. 'comment_author_email' => isset($_REQUEST['email']) && stripslashes($_REQUEST['email']) == __('Your email (will not be published)', 'usernoise-pro') ? '' : (isset($_REQUEST['email']) ? stripslashes($_REQUEST['email']) : ''),
  96. 'comment_content' => stripslashes($_REQUEST['comment']) == _x( 'Comment', 'noun') ? '' : stripslashes($_REQUEST['comment']),
  97. 'comment_author_url' => '',
  98. 'comment_type' => null,
  99. 'user_id' => get_current_user_id()
  100. );
  101. $errors = $unpro_model->validate_comment($comment);
  102. if (!empty($errors)){
  103. echo json_encode(array('errors' => join('<br>', $errors)));
  104. exit;
  105. }
  106. add_action('comment_post', array(&$this, '_comment_post'), 10, 2);
  107. $comment_id = wp_new_comment( $comment );
  108. remove_action('comment_post', array(&$this, '_comment_post'), 10, 2);
  109. global $comment;
  110. $comment = get_comment($comment_id);
  111. ob_start();
  112. require(usernoisepro_path('/html/comment.php'));
  113. $html = ob_get_clean();
  114. $query = new WP_Query(array('p' => $post_id, 'post_type' => FEEDBACK));
  115. $query->the_post();
  116. ob_start();
  117. comments_number(__('No Responses', 'usernoise-pro'), __('One Response', 'usernoise-pro'),
  118. __('% Responses', 'usernoise-pro'));
  119. $count = ob_get_clean();
  120. echo json_encode(array('success' => true, 'html' => $html, 'count' => $count));
  121. exit;
  122. }
  123. public function _comment_post($comment_id, $approved){
  124. global $unpro_model;
  125. if (!$approved) return;
  126. $unpro_model->notify_feedback_author_on_comment($comment_id);
  127. }
  128. public function action_unpro_get_comments(){
  129. global $wp_query, $unpro_model, $un_model, $un_h;
  130. $h = $un_h;
  131. add_filter('comment_feed_limits', array(&$this, 'filter_unpro_comment_feed_limits'));
  132. add_filter('comment_feed_orderby', array(&$this, 'filter_unpro_comment_feed_orderby'));
  133. $query = new WP_Query(array('p' => $_REQUEST['post_id'], 'post_type' => FEEDBACK,
  134. 'withcomments' => 1, 'feed' => true));
  135. ob_start();
  136. $query->the_post();
  137. $type = $un_model->get_feedback_type(get_the_ID());
  138. $type = $type->slug;
  139. $id_or_email = $unpro_model->author_email(get_the_ID());
  140. require(usernoisepro_path('/html/feedback-description.php'));
  141. if ($query->have_comments()){
  142. while($query->have_comments()){
  143. $query->the_comment();
  144. require(usernoisepro_path('/html/comment.php'));
  145. }
  146. } else {
  147. require(usernoisepro_path('/html/comments-blank-slate.php'));
  148. }
  149. $comments = ob_get_clean();
  150. ob_start();
  151. comments_number(__('No Responses', 'usernoise-pro'), __('One Response', 'usernoise-pro'),
  152. __('% Responses', 'usernoise-pro'));
  153. $count = ob_get_clean();
  154. echo json_encode(array('comments' => $comments, 'count' => $count));
  155. exit;
  156. }
  157. function filter_unpro_comment_feed_orderby(){
  158. return 'comment_date_gmt ASC';
  159. }
  160. function filter_unpro_comment_feed_limits(){
  161. return '';
  162. }
  163. public function action_comment_duplicate_trigger($commentdata){
  164. die(json_encode(array('errors' => __('Duplicate comment detected', 'usernoise-pro'))));
  165. }
  166. public static function filter_un_controller_class(){
  167. return 'UNPRO_Controller';
  168. }
  169. public function action_un_after_feedback_form(){
  170. global $unpro_model, $un_h;
  171. $h = $un_h;
  172. require(usernoisepro_path('/html/pro.php'));
  173. }
  174. public function action_un_before_feedback_form(){
  175. echo '<div id="feedback-blocks-wrapper">';
  176. }
  177. public function action_un_feedback_content_top(){
  178. ?><a href="#" id="un-feedback-close"><img src="<?php echo usernoise_url("/vendor/facebox/closelabel.png") ?>" title="close" class="close_image" ></a><?php
  179. }
  180. }
  181. global $unpro_controller;
  182. if (un_get_option(UNPRO_ENABLE_DISCUSSIONS)){
  183. $unpro_controller = new UNPRO_Controller;
  184. }