| 1 |
<?php |
| 2 |
/* |
| 3 |
* working behind the seen |
| 4 |
*/ |
| 5 |
|
| 6 |
/* |
| 7 |
**========== Direct access not allowed =========== |
| 8 |
*/ |
| 9 |
if( ! defined('ABSPATH') ) die('Not Allowed'); |
| 10 |
|
| 11 |
class WPComment_Admin { |
| 12 |
|
| 13 |
private static $ins; |
| 14 |
private static $options; |
| 15 |
|
| 16 |
function __construct() { |
| 17 |
|
| 18 |
add_action ( 'admin_menu', array ( |
| 19 |
$this, |
| 20 |
'add_comment_menu' |
| 21 |
) ); |
| 22 |
|
| 23 |
self::$options = get_option('wpcomment_settings', true); |
| 24 |
|
| 25 |
// Saving settings and fields |
| 26 |
add_action('wp_ajax_wpcomment_save_form_meta', array($this, 'save_meta_fields')); |
| 27 |
add_action('wp_ajax_wpcomment_save_settings', array($this, 'save_form_settings')); |
| 28 |
|
| 29 |
/* |
| 30 |
* adding meta box in comments edit page |
| 31 |
*/ |
| 32 |
add_action( 'add_meta_boxes_comment', array( $this, 'comment_meta_box' ), 1 ); |
| 33 |
|
| 34 |
|
| 35 |
// adding wpml support for PPOM Settings |
| 36 |
// add_filter('woocommerce_admin_settings_sanitize_option', array($this, 'wpcomment_setting_wpml'), 10, 3); |
| 37 |
|
| 38 |
// add_action( 'woocommerce_admin_field_wpcomment_multi_select', array( $this, 'wpcomment_multi_select_role_setting' ),2,10 ); |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
public static function get_instance() { |
| 43 |
// create a new object if it doesn't exist. |
| 44 |
is_null(self::$ins) && self::$ins = new self; |
| 45 |
return self::$ins; |
| 46 |
} |
| 47 |
|
| 48 |
public static function get_option($k, $default=null){ |
| 49 |
|
| 50 |
return isset(self::$options[$k]) ? self::$options[$k] : $default; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
/* |
| 55 |
* creating menu page for this plugin |
| 56 |
*/ |
| 57 |
function add_comment_menu() { |
| 58 |
|
| 59 |
add_comments_page( __( 'Comment Fields', 'wp-comment-fields' ), |
| 60 |
__( 'Comment Fields', 'wp-comment-fields' ), |
| 61 |
'manage_options', 'wpcomment', |
| 62 |
array($this, 'comment_meta') ); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/* |
| 67 |
* CALLBACKS |
| 68 |
*/ |
| 69 |
function comment_meta() { |
| 70 |
|
| 71 |
$wpcomment_inputs = $this->get_all_inputs(); |
| 72 |
|
| 73 |
ob_start(); |
| 74 |
wpcomment_load_template ( 'admin/settings-home.php', ['wpcomment_inputs'=>$wpcomment_inputs] ); |
| 75 |
echo ob_get_clean(); |
| 76 |
} |
| 77 |
|
| 78 |
/* |
| 79 |
* returning NM_Inputs object |
| 80 |
*/ |
| 81 |
function get_all_inputs() { |
| 82 |
|
| 83 |
$nm_inputs = WPComment_Inputs(); |
| 84 |
// registering all inputs here |
| 85 |
|
| 86 |
$all_inputs = array ( |
| 87 |
|
| 88 |
'text' => $nm_inputs->get_input ( 'text' ), |
| 89 |
'textarea' => $nm_inputs->get_input ( 'textarea' ), |
| 90 |
'select' => $nm_inputs->get_input ( 'select' ), |
| 91 |
'radio' => $nm_inputs->get_input ( 'radio' ), |
| 92 |
'checkbox' => $nm_inputs->get_input ( 'checkbox' ), |
| 93 |
); |
| 94 |
|
| 95 |
return apply_filters('wpcomment_all_inputs', $all_inputs, $nm_inputs); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
/* |
| 100 |
* saving form meta in admin call |
| 101 |
*/ |
| 102 |
function save_meta_fields() { |
| 103 |
// Verify nonce |
| 104 |
if ( ! isset( $_REQUEST['wpcomment_nonce'] ) || ! wp_verify_nonce( $_REQUEST['wpcomment_nonce'], 'wpcomment_save_form_meta_nonce' ) ) { |
| 105 |
wp_send_json_error( 'Invalid nonce.' ); |
| 106 |
} |
| 107 |
|
| 108 |
// Check user capabilities |
| 109 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 110 |
wp_send_json_error( 'Insufficient permissions.' ); |
| 111 |
} |
| 112 |
|
| 113 |
// Proceed with processing the request |
| 114 |
$wpcomment_meta = isset( $_REQUEST['wpcomment'] ) ? $_REQUEST['wpcomment'] : ''; |
| 115 |
$wpcomment_meta = apply_filters( 'wpcomment_meta_data_saving', $wpcomment_meta ); |
| 116 |
$wpcomment_meta = wpcomment_sanitize_array_data( $wpcomment_meta ); |
| 117 |
|
| 118 |
// Save meta fields |
| 119 |
update_option( 'wpcomment_meta_fields', json_encode( $wpcomment_meta ) ); |
| 120 |
|
| 121 |
// Prepare response |
| 122 |
$args = array( 'page' => 'wpcomment' ); |
| 123 |
$redirect_to = add_query_arg( $args, admin_url( 'admin.php' ) ); |
| 124 |
|
| 125 |
$resp = array( |
| 126 |
'message' => __( 'Fields added successfully', 'wp-comment-fields' ), |
| 127 |
'status' => 'success', |
| 128 |
'redirect_to' => $redirect_to, |
| 129 |
); |
| 130 |
|
| 131 |
wp_send_json( $resp ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
// Saving Global settings |
| 136 |
function save_form_settings(){ |
| 137 |
|
| 138 |
// Verify nonce |
| 139 |
if ( ! isset( $_REQUEST['wpcomment_nonce'] ) || ! wp_verify_nonce( $_REQUEST['wpcomment_nonce'], 'wpcomment_save_form_meta_nonce' ) ) { |
| 140 |
wp_send_json_error( 'Invalid nonce.' ); |
| 141 |
} |
| 142 |
|
| 143 |
// Check user capabilities |
| 144 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 145 |
wp_send_json_error( 'Insufficient permissions.' ); |
| 146 |
} |
| 147 |
|
| 148 |
$keys = ['wpcomment_heading','wpcomment_cpt','wpcomment_disable_frontend']; |
| 149 |
|
| 150 |
$settings = []; |
| 151 |
foreach($keys as $k){ |
| 152 |
if( ! isset($_POST[$k]) ) continue; |
| 153 |
$settings[$k] = sanitize_text_field($_POST[$k]); |
| 154 |
} |
| 155 |
|
| 156 |
update_option('wpcomment_settings', $settings); |
| 157 |
|
| 158 |
$resp = array ( |
| 159 |
'message' => __ ( 'Settings saved successfully', 'wp-comment-fields' ), |
| 160 |
'status' => 'success', |
| 161 |
); |
| 162 |
|
| 163 |
wp_send_json($resp); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* rendering comment meta BOX in comments admin page |
| 168 |
*/ |
| 169 |
function comment_meta_box( $comment ) { |
| 170 |
|
| 171 |
add_meta_box( 'wpcomment_mb_show', __( 'Comment Extra Fields' ), array( $this, 'show_comment_fields' ), 'comment', 'normal' ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* rendering comment META in comments admin page |
| 176 |
*/ |
| 177 |
function show_comment_fields( $comment ) { |
| 178 |
|
| 179 |
$comment_output = WPCOMMENT_FRONTEND()->get_comment_meta_by_comment_id(get_comment_ID()); |
| 180 |
|
| 181 |
// $comment_meta = get_comment_meta( get_comment_ID(), 'wpcomment_fields', true ); |
| 182 |
// $comment_output = wpcomment_render_comment_meta($comment_meta); |
| 183 |
|
| 184 |
if($comment_output){ |
| 185 |
echo apply_filters('wpcomment_meta_admin', $comment_output, $comment); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
function wpcomment_setting_wpml($value, $option, $raw_value) { |
| 192 |
|
| 193 |
if( isset($option['type']) && isset($option['type']) == 'text' ) { |
| 194 |
$value = wpcomment_wpml_translate($value, 'PPOM'); |
| 195 |
} |
| 196 |
|
| 197 |
return $value; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
function WPCOMMENT_ADMIN(){ |
| 202 |
return WPComment_Admin::get_instance(); |
| 203 |
} |
| 204 |
if( is_admin() ) { |
| 205 |
WPCOMMENT_ADMIN(); |
| 206 |
} |