PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/ratings.php

https://gitlab.com/Magi1053/Extra
PHP | 221 lines | 161 code | 52 blank | 8 comment | 27 complexity | e46ff659af04dc8712745b104a976658 MD5 | raw file
  1. <?php
  2. // Prevent file from being loaded directly
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. die( '-1' );
  5. }
  6. function extra_new_rating() {
  7. if ( ! wp_verify_nonce( $_POST['extra_rating_nonce'], 'extra_rating_nonce' ) ) {
  8. die( -1 );
  9. }
  10. $post_id = absint( sanitize_text_field( $_POST['extra_post_id'] ) );
  11. $rating = floatval( sanitize_text_field( $_POST['extra_rating'] ) );
  12. $result = extra_add_post_rating( $post_id, $rating );
  13. echo json_encode( $result );
  14. die();
  15. }
  16. add_action( 'wp_ajax_extra_new_rating', 'extra_new_rating' );
  17. add_action( 'wp_ajax_nopriv_extra_new_rating', 'extra_new_rating' );
  18. function extra_add_post_rating( $post_id, $rating ) {
  19. if ( extra_get_user_post_rating( $post_id ) ) {
  20. return array();
  21. }
  22. $commentdata = array(
  23. 'comment_type' => EXTRA_RATING_COMMENT_TYPE,
  24. 'comment_author' => '',
  25. 'comment_author_url' => '',
  26. 'comment_author_email' => '',
  27. 'comment_post_ID' => absint( $post_id ),
  28. 'comment_content' => abs( floatval( $rating ) ),
  29. );
  30. $user = wp_get_current_user();
  31. if ( $user->exists() ) {
  32. $commentdata['comment_author'] = wp_slash( $user->display_name );
  33. $commentdata['user_ID'] = $user->ID;
  34. }
  35. // prevent notifications
  36. add_filter( 'extra_rating_notify_intercept', '__return_zero' );
  37. wp_new_comment( $commentdata );
  38. return array(
  39. 'rating' => $rating,
  40. 'average' => extra_set_post_rating_average( $post_id ),
  41. );
  42. }
  43. function extra_rating_notify_intercept( $option ) {
  44. $intercepted = apply_filters( 'extra_rating_notify_intercept', false );
  45. return false !== $intercepted ? $intercepted : false;
  46. }
  47. add_filter( 'pre_option_comments_notify', 'extra_rating_notify_intercept' );
  48. function extra_rating_pre_comment_approved( $approved, $commentdata ) {
  49. if ( !empty( $commentdata['comment_type'] ) && EXTRA_RATING_COMMENT_TYPE == $commentdata['comment_type'] ) {
  50. $approved = 1;
  51. }
  52. return $approved;
  53. }
  54. add_filter( 'pre_comment_approved', 'extra_rating_pre_comment_approved', 10, 2 );
  55. function extra_rating_pre_comment_user_ip() {
  56. return extra_get_user_ip();
  57. }
  58. add_filter( 'pre_comment_user_ip', 'extra_rating_pre_comment_user_ip' );
  59. function extra_set_post_rating_average( $post_id ) {
  60. $ratings = get_comments( array(
  61. 'type' => EXTRA_RATING_COMMENT_TYPE,
  62. 'post_id' => $post_id,
  63. 'status' => 'approve',
  64. 'parent' => 0,
  65. ) );
  66. if ( empty( $ratings ) ) {
  67. $rating_avg = 0;
  68. update_post_meta( $post_id, '_extra_rating_average', 0 );
  69. update_post_meta( $post_id, '_extra_rating_count', 0 );
  70. return;
  71. }
  72. $rating_values = array();
  73. foreach ( $ratings as $rating ) {
  74. $rating_values[] = floatval( trim( $rating->comment_content ) );
  75. }
  76. $num = array_sum( $rating_values ) / count( $rating_values );
  77. $ceil = ceil( $num );
  78. $half = $ceil - 0.5;
  79. if ( $num >= $half + 0.25 ) {
  80. $rating_average = $ceil;
  81. } else if ( $num < $half - 0.25 ) {
  82. $rating_average = floor( $num );
  83. } else {
  84. $rating_average = $half;
  85. }
  86. $rating_count = count( $rating_values );
  87. update_post_meta( $post_id, '_extra_rating_average', $rating_average );
  88. update_post_meta( $post_id, '_extra_rating_count', $rating_count );
  89. return compact( 'rating_average', 'rating_count' );
  90. }
  91. function extra_get_post_ratings_count( $post_id = '' ) {
  92. $post_id = empty( $post_id ) ? get_the_ID() : $post_id;
  93. return get_comments( array(
  94. 'type' => EXTRA_RATING_COMMENT_TYPE,
  95. 'post_id' => $post_id,
  96. 'status' => 'approve',
  97. 'parent' => 0,
  98. 'count' => true,
  99. ) );
  100. }
  101. function extra_get_post_rating( $post_id = 0 ) {
  102. $post_id = empty( $post_id ) ? get_the_ID() : $post_id;
  103. $rating = get_post_meta( $post_id, '_extra_rating_average', true );
  104. return $rating ? $rating : 0;
  105. }
  106. function extra_get_user_ip() {
  107. if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
  108. $ip = $_SERVER['HTTP_CLIENT_IP'];
  109. } else if ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
  110. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  111. } else {
  112. $ip = $_SERVER['REMOTE_ADDR'];
  113. }
  114. // disallow non white listed characters
  115. preg_replace( '/[^0-9a-fA-F:., ]/', '', $ip );
  116. if ( '::1' == $ip ) {
  117. $ip = '127.0.0.1';
  118. }
  119. return $ip;
  120. }
  121. function extra_get_user_post_rating( $post_id = 0 ) {
  122. $post_id = empty( $post_id ) ? get_the_ID() : $post_id;
  123. $args = array(
  124. 'type' => EXTRA_RATING_COMMENT_TYPE,
  125. 'post_id' => $post_id,
  126. 'status' => 'approve',
  127. 'parent' => 0,
  128. 'number' => 1,
  129. );
  130. // If the user is logged in
  131. $user = wp_get_current_user();
  132. if ( $user->exists() ) {
  133. $args['user_id'] = $user->ID;
  134. } else {
  135. $args['comment_author_IP'] = extra_get_user_ip();
  136. }
  137. $rating = get_comments( $args );
  138. return !empty( $rating ) ? $rating[0]->comment_content : false;
  139. }
  140. function extra_rating_get_comment_author_ip( $clauses, $wp_comment_query ) {
  141. global $wpdb;
  142. if ( $wp_comment_query->query_vars['type'] == EXTRA_RATING_COMMENT_TYPE && !empty( $wp_comment_query->query_vars['comment_author_IP'] ) ) {
  143. $clauses['where'] .= $wpdb->prepare( ' AND comment_author_IP = "%s"', $wp_comment_query->query_vars['comment_author_IP'] );
  144. }
  145. return $clauses;
  146. }
  147. add_filter( 'comments_clauses', 'extra_rating_get_comment_author_ip', 10, 2 );
  148. function extra_exclude_ratings_from_comments_list( $clauses ) {
  149. global $wpdb;
  150. if ( ! is_admin() ) {
  151. return $clauses;
  152. }
  153. $screen = get_current_screen();
  154. if ( ! isset( $screen->id ) || ( isset( $screen->id ) && 'edit-comments' !== $screen->id ) ) {
  155. return $clauses;
  156. }
  157. $clauses['where'] .= $wpdb->prepare( ' AND comment_type <> "%s"', EXTRA_RATING_COMMENT_TYPE );
  158. return $clauses;
  159. }
  160. add_filter( 'comments_clauses', 'extra_exclude_ratings_from_comments_list' );
  161. /**
  162. * Post types which use rating
  163. * @return array
  164. */
  165. function extra_get_rating_post_types() {
  166. return apply_filters( 'extra_rating_post_types', array(
  167. 'post',
  168. ) );
  169. }