class.swpm-comment-form-related.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. class SwpmCommentFormRelated {
  3. public static function customize_comment_form() {
  4. $allow_comments = SwpmSettings::get_instance()->get_value('members-login-to-comment');
  5. if (empty($allow_comments)){
  6. return;
  7. }
  8. if (SwpmAuth::get_instance()->is_logged_in()){
  9. return;
  10. }
  11. //Apply a filter to the message so it can be customized using the custom message plugin
  12. $comment_form_msg = apply_filters('swpm_login_to_comment_msg', SwpmUtils::_("Please login to comment."));
  13. $comment_form_msg = '<div class="swpm-login-to-comment-msg">' . $comment_form_msg . '</div>';
  14. ?>
  15. <script type="text/javascript">
  16. jQuery(document).ready(function($) {
  17. $('#respond').html('<?php echo $comment_form_msg; ?>');
  18. });
  19. </script>
  20. <?php
  21. }
  22. public static function customize_comment_fields($fields){
  23. //Check if login to comment feature is enabled.
  24. $allow_comments = SwpmSettings::get_instance()->get_value('members-login-to-comment');
  25. if (empty($allow_comments)){//Feature is disabled
  26. return $fields;
  27. }
  28. if (SwpmAuth::get_instance()->is_logged_in()){//Member is logged-in.
  29. return $fields;
  30. }
  31. //Member is not logged-in so show the protection message.
  32. $fields = array();
  33. $login_link = SwpmUtils::_('Please Login to Comment.');
  34. $fields['comment_field'] = $login_link;
  35. $fields['title_reply'] = '';
  36. $fields['cancel_reply_link'] = '';
  37. $fields['comment_notes_before'] = '';
  38. $fields['comment_notes_after'] = '';
  39. $fields['fields'] = '';
  40. $fields['label_submit'] = '';
  41. $fields['title_reply_to'] = '';
  42. $fields['id_submit'] = '';
  43. $fields['id_form'] = '';
  44. return $fields;
  45. }
  46. /*
  47. * This function checks and restricts comment posting (via HTTP POST) to members only (if the feature is enabled)
  48. */
  49. public static function check_and_restrict_comment_posting_to_members(){
  50. $allow_comments = SwpmSettings::get_instance()->get_value('members-login-to-comment');
  51. if (empty($allow_comments)){
  52. return;
  53. }
  54. if (is_admin()) {
  55. return;
  56. }
  57. if (SwpmAuth::get_instance()->is_logged_in()){
  58. return;
  59. }
  60. $comment_id = filter_input(INPUT_POST, 'comment_post_ID');
  61. if (empty($comment_id)) {
  62. return;
  63. }
  64. //Stop this request -> 1)we are on the front-side. 2) Comment posted by a not logged in member. 3) comment_post_ID missing.
  65. $_POST = array();
  66. wp_die(SwpmUtils::_('Comments not allowed by a non-member.'));
  67. }
  68. }