| 1 |
<?php |
| 2 |
/** |
| 3 |
* Frontend Class |
| 4 |
**/ |
| 5 |
|
| 6 |
class WPComment_Frontend { |
| 7 |
|
| 8 |
/** |
| 9 |
* the static object instace |
| 10 |
*/ |
| 11 |
private static $ins = null; |
| 12 |
|
| 13 |
|
| 14 |
public static function get_instance() |
| 15 |
{ |
| 16 |
// create a new object if it doesn't exist. |
| 17 |
is_null(self::$ins) && self::$ins = new self; |
| 18 |
return self::$ins; |
| 19 |
} |
| 20 |
|
| 21 |
function __construct() { |
| 22 |
|
| 23 |
/* |
| 24 |
* Action hooks for comment fields |
| 25 |
*/ |
| 26 |
add_action( 'comment_form_after_fields', array( $this, 'render_input_fields' ) ); |
| 27 |
add_action( 'comment_form_logged_in_after', array( $this, 'render_input_fields' ) ); |
| 28 |
|
| 29 |
/** |
| 30 |
* Saving comment extra fields |
| 31 |
**/ |
| 32 |
add_action( 'comment_post', array( $this, 'save_comment_meta_fields' ) ); |
| 33 |
|
| 34 |
/** |
| 35 |
* adding comment meta in front view |
| 36 |
*/ |
| 37 |
//add_filter( 'comment_text', array( $this, 'render_comment_meta_front_legacy' ), 100 ); |
| 38 |
add_filter( 'comment_text', array( $this, 'render_comment_meta_front' ), 100, 2 ); |
| 39 |
|
| 40 |
// ajax callbacks |
| 41 |
add_action('wp_ajax_wpcomment_upload_file', 'wpcomment_upload_file'); |
| 42 |
add_action('wp_ajax_nopriv_wpcomment_upload_file', 'wpcomment_upload_file'); |
| 43 |
add_action('wp_ajax_wpcomment_delete_file', 'wpcomment_delete_file'); |
| 44 |
add_action('wp_ajax_nopriv_wpcomment_delete_file', 'wpcomment_delete_file'); |
| 45 |
} |
| 46 |
|
| 47 |
// rendering fields above the Comment form as input |
| 48 |
function render_input_fields() { |
| 49 |
|
| 50 |
$allow_posts = apply_filters('wpcomment_allowed_posts', ['post']); |
| 51 |
|
| 52 |
if ( ! in_array(get_post_type(), $allow_posts) && ! is_admin() ) return; |
| 53 |
|
| 54 |
$form_obj = new WPComment_Form; |
| 55 |
|
| 56 |
// wpcomment_pa($form_obj::$fields); |
| 57 |
|
| 58 |
// Check if PPOM fields is empty |
| 59 |
if( ! $form_obj::$fields ) return ''; |
| 60 |
|
| 61 |
$wpcomment_html = ''; |
| 62 |
$template_vars = ['form_obj' => $form_obj]; |
| 63 |
|
| 64 |
ob_start(); |
| 65 |
wpcomment_load_input_templates( 'frontend/wpcomment-fields.php', $template_vars ); |
| 66 |
$wpcomment_html .= ob_get_clean(); |
| 67 |
|
| 68 |
echo apply_filters('wpcomment_fields_html', $wpcomment_html, $form_obj); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
function save_comment_meta_fields($comment_id){ |
| 73 |
|
| 74 |
// wpcomment_pa($_POST); exit; |
| 75 |
|
| 76 |
if( !isset($_POST['wpcomment']) ) return ''; |
| 77 |
|
| 78 |
add_comment_meta( $comment_id, 'wpcomment_fields', $_POST['wpcomment'] ); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
function render_comment_meta_front( $comment_text, $comment ) { |
| 83 |
|
| 84 |
// if admin disable frontend extra fields |
| 85 |
if( wpcomment_disable_extra_fields() ) return $comment_text; |
| 86 |
|
| 87 |
$comment_meta_text = $this->get_comment_meta_by_comment_id($comment->comment_ID); |
| 88 |
if($comment_meta_text){ |
| 89 |
$comment_text .= $comment_meta_text; |
| 90 |
} |
| 91 |
|
| 92 |
return $comment_text; |
| 93 |
} |
| 94 |
|
| 95 |
// getting comment meta by comment id for global use |
| 96 |
function get_comment_meta_by_comment_id($comment_id){ |
| 97 |
|
| 98 |
$comment_meta = get_comment_meta( $comment_id, 'wpcomment_fields', true ); |
| 99 |
|
| 100 |
$comment_text = ''; |
| 101 |
|
| 102 |
// if wpcomment_fields found then it is latest version |
| 103 |
if( isset($comment_meta['fields']) ) { |
| 104 |
$comment_text = $this->comment_fields($comment_meta); |
| 105 |
}else{ |
| 106 |
$comment_text = $this->comment_fields_legacy(); |
| 107 |
} |
| 108 |
|
| 109 |
return apply_filters('wpcomment_comment_meta', $comment_text); |
| 110 |
} |
| 111 |
|
| 112 |
function comment_fields($comment_meta) { |
| 113 |
|
| 114 |
// wpcomment_pa($comment_meta); |
| 115 |
|
| 116 |
$comment = ''; |
| 117 |
if ( $comment_meta ) { |
| 118 |
$comment_meta_heading = WPCOMMENT_ADMIN()::get_option('wpcomment_heading'); |
| 119 |
if ( $comment_meta_heading != '' ) { |
| 120 |
$comment .= sprintf(__('<p class="wpcomment-meta">%s</p>', 'wp-comment-fields'), $comment_meta_heading); |
| 121 |
} |
| 122 |
|
| 123 |
$comment .= wpcomment_render_comment_meta($comment_meta); |
| 124 |
} |
| 125 |
|
| 126 |
return $comment; |
| 127 |
} |
| 128 |
|
| 129 |
function comment_fields_legacy() { |
| 130 |
|
| 131 |
$comment_meta = get_option( 'wpcomment_meta' ); |
| 132 |
|
| 133 |
// ppom_pa($comment_meta); |
| 134 |
|
| 135 |
$comment = ''; |
| 136 |
|
| 137 |
if($comment_meta){ |
| 138 |
$comment_meta_heading = WPCOMMENT_ADMIN()::get_option('wpcomment_heading'); |
| 139 |
if($comment_meta_heading != '') |
| 140 |
$comment = '<p><strong>'.sprintf(__('%s'), $comment_meta_heading).'</strong></p>'; |
| 141 |
|
| 142 |
$comment .= '<ul>'; |
| 143 |
foreach ($comment_meta as $index => $meta){ |
| 144 |
|
| 145 |
$comment_meta_key = $meta['data_name']; |
| 146 |
$comment_meta_type = $meta['type']; |
| 147 |
$comment_title = isset($meta['title']) ? stripslashes($meta['title']) : 'NO TITLE'; |
| 148 |
$comment_meta_val = get_comment_meta(get_comment_ID(), $comment_meta_key, true); |
| 149 |
|
| 150 |
if($comment_meta_val != ''){ |
| 151 |
|
| 152 |
switch( $comment_meta_type ) { |
| 153 |
case 'file': |
| 154 |
$comment_files = explode(',', $comment_meta_val); |
| 155 |
|
| 156 |
foreach($comment_files as $file) { |
| 157 |
if( wpcomment_is_file_image($file) ){ |
| 158 |
echo wpcomment_render_images_legacy($file); |
| 159 |
}else{ |
| 160 |
echo wpcomment_file_link_legacy($file); |
| 161 |
} |
| 162 |
} |
| 163 |
break; |
| 164 |
|
| 165 |
default: |
| 166 |
$comment .= '<li><strong>'.sprintf(__('%s: '), esc_attr($comment_title)).'</strong>'; |
| 167 |
$comment .= sprintf(__('%s'), esc_attr($comment_meta_val)).'</li>'; |
| 168 |
break; |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
//$comment .= ', '; |
| 173 |
} |
| 174 |
} |
| 175 |
$comment .= '</ul>'; |
| 176 |
//$comment = substr($comment, 0, -2); |
| 177 |
} |
| 178 |
|
| 179 |
return $comment; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
function WPCOMMENT_FRONTEND(){ |
| 184 |
return WPComment_Frontend::get_instance(); |
| 185 |
} |
| 186 |
|
| 187 |
WPCOMMENT_FRONTEND(); |