| 1 |
<?php |
| 2 |
/* |
| 3 |
* this is main plugin class |
| 4 |
*/ |
| 5 |
|
| 6 |
|
| 7 |
/* ======= the model main class =========== */ |
| 8 |
if(!class_exists('NM_Framwork_V1_WPComment')){ |
| 9 |
$_framework = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'nm-framework.php'; |
| 10 |
if( file_exists($_framework)) |
| 11 |
include_once($_framework); |
| 12 |
else |
| 13 |
die('Reen, Reen, BUMP! not found '.$_framework); |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
/* |
| 18 |
* [1] |
| 19 |
* TODO: change the class name of your plugin |
| 20 |
*/ |
| 21 |
class NM_PLUGIN_WPComments extends NM_Framwork_V1_WPComment{ |
| 22 |
|
| 23 |
private static $ins = null; |
| 24 |
|
| 25 |
public static function init() |
| 26 |
{ |
| 27 |
add_action('plugins_loaded', array(self::get_instance(), '_setup')); |
| 28 |
} |
| 29 |
|
| 30 |
public static function get_instance() |
| 31 |
{ |
| 32 |
// create a new object if it doesn't exist. |
| 33 |
is_null(self::$ins) && self::$ins = new self; |
| 34 |
return self::$ins; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
var $inputs; |
| 39 |
|
| 40 |
/* |
| 41 |
* plugin constructur |
| 42 |
*/ |
| 43 |
function _setup(){ |
| 44 |
|
| 45 |
//setting plugin meta saved in config.php |
| 46 |
$this -> plugin_meta = get_plugin_meta_wpcomment(); |
| 47 |
|
| 48 |
//getting saved settings |
| 49 |
$this -> plugin_settings = get_option($this->plugin_meta['shortname'].'_settings'); |
| 50 |
|
| 51 |
|
| 52 |
// populating $inputs with NM_Inputs object |
| 53 |
$this->inputs = $this->get_all_inputs (); |
| 54 |
|
| 55 |
|
| 56 |
/* |
| 57 |
* [2] |
| 58 |
* TODO: update scripts array for SHIPPED scripts |
| 59 |
* only use handlers |
| 60 |
*/ |
| 61 |
//setting shipped scripts |
| 62 |
$this -> wp_shipped_scripts = array('jquery'); |
| 63 |
|
| 64 |
|
| 65 |
/* |
| 66 |
* [3] |
| 67 |
* TODO: update scripts array for custom scripts/styles |
| 68 |
*/ |
| 69 |
//setting plugin settings |
| 70 |
$this -> plugin_scripts = array(array( 'script_name' => 'scripts', |
| 71 |
'script_source' => '/js/script.js', |
| 72 |
'localized' => true, |
| 73 |
'type' => 'js' |
| 74 |
), |
| 75 |
array( 'script_name' => 'styles', |
| 76 |
'script_source' => '/plugin.styles.css', |
| 77 |
'localized' => false, |
| 78 |
'type' => 'style' |
| 79 |
), |
| 80 |
|
| 81 |
array( 'script_name' => 'bootstrap-grid', |
| 82 |
'script_source' => '/assets/css/bootstrap-grid.css', |
| 83 |
'localized' => false, |
| 84 |
'type' => 'style' |
| 85 |
), |
| 86 |
); |
| 87 |
|
| 88 |
/* |
| 89 |
* [4] |
| 90 |
* TODO: localized array that will be used in JS files |
| 91 |
* Localized object will always be your pluginshortname_vars |
| 92 |
* e.g: pluginshortname_vars.ajaxurl |
| 93 |
*/ |
| 94 |
$this -> localized_vars = array('ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 95 |
'plugin_url' => $this->plugin_meta['url'], |
| 96 |
'settings' => $this -> plugin_settings); |
| 97 |
|
| 98 |
|
| 99 |
/* |
| 100 |
* [5] |
| 101 |
* TODO: this array will grow as plugin grow |
| 102 |
* all functions which need to be called back MUST be in this array |
| 103 |
* setting callbacks |
| 104 |
*/ |
| 105 |
//following array are functions name and ajax callback handlers |
| 106 |
$this -> ajax_callbacks = array('save_settings', //do not change this action, is for admin |
| 107 |
'save_file_meta',); |
| 108 |
|
| 109 |
/* |
| 110 |
* plugin localization being initiated here |
| 111 |
*/ |
| 112 |
add_action('init', array($this, 'wpp_textdomain')); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
/* |
| 117 |
* hooking up scripts for front-end |
| 118 |
*/ |
| 119 |
add_action('wp_enqueue_scripts', array($this, 'load_scripts')); |
| 120 |
|
| 121 |
|
| 122 |
/* |
| 123 |
* registering callbacks |
| 124 |
*/ |
| 125 |
$this -> do_callbacks(); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
/* |
| 130 |
* Action hooks for comment fields |
| 131 |
*/ |
| 132 |
add_action('comment_form_after_fields', array($this, 'render_comments_meta_fields')); |
| 133 |
add_action('comment_form_logged_in_after', array($this, 'render_comments_meta_fields')); |
| 134 |
|
| 135 |
/* |
| 136 |
* Saving comment meta |
| 137 |
*/ |
| 138 |
add_action( 'comment_post', array($this, 'save_comment_meta_fields') ); |
| 139 |
|
| 140 |
|
| 141 |
/* |
| 142 |
* adding meta box in comments edit page |
| 143 |
*/ |
| 144 |
add_action('add_meta_boxes_comment', array($this, 'render_comment_meta_admin_box'), 1); |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* adding comment meta in front view |
| 149 |
*/ |
| 150 |
add_filter('comment_text', array($this, 'render_comment_meta_front'),100); |
| 151 |
|
| 152 |
/** |
| 153 |
* validating comments |
| 154 |
*/ |
| 155 |
add_filter( 'preprocess_comment', array($this, 'verify_comment_meta_data') ); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
// i18n and l10n support here |
| 161 |
// plugin localization |
| 162 |
function wpp_textdomain() { |
| 163 |
$locale_dir = $this->plugin_meta['path'] . '/locale/'; |
| 164 |
load_plugin_textdomain('nm-wpcomments', false, $locale_dir); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
/* |
| 169 |
* =============== NOW do your JOB =========================== |
| 170 |
* |
| 171 |
*/ |
| 172 |
|
| 173 |
/* |
| 174 |
* Callbacks for comments fields |
| 175 |
*/ |
| 176 |
|
| 177 |
function render_comments_meta_fields(){ |
| 178 |
|
| 179 |
global $post; |
| 180 |
|
| 181 |
$custom_posttypes = $this->get_option ( '_comment_posttypes' ); |
| 182 |
if( $custom_posttypes != ''){ |
| 183 |
|
| 184 |
$postTypes = explode(',', $custom_posttypes); |
| 185 |
$postTypes = array_map('trim', $postTypes); |
| 186 |
if( ! in_array($post->post_type, $postTypes) ) |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
$this -> load_template('render.comment.meta.php'); |
| 191 |
} |
| 192 |
|
| 193 |
function save_comment_meta_fields($comment_id){ |
| 194 |
|
| 195 |
$comment_meta = get_option('wpcomment_meta'); |
| 196 |
//wpcomment_pa($_POST); exit; |
| 197 |
|
| 198 |
foreach ($comment_meta as $index => $meta){ |
| 199 |
|
| 200 |
$comment_meta_key = $meta['data_name']; |
| 201 |
$comment_meta_val = esc_attr($_POST[$comment_meta_key]); |
| 202 |
add_comment_meta( $comment_id, $comment_meta_key, $comment_meta_val ); |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
/* |
| 211 |
* Verify comment meta |
| 212 |
*/ |
| 213 |
function verify_comment_meta_data( $commentdata ) { |
| 214 |
|
| 215 |
$comment_meta = get_option('wpcomment_meta'); |
| 216 |
//wpcomment_pa($_POST); exit; |
| 217 |
|
| 218 |
if($comment_meta){ |
| 219 |
foreach ($comment_meta as $index => $meta){ |
| 220 |
|
| 221 |
$comment_meta_key = $meta['data_name']; |
| 222 |
$comment_meta_val = esc_attr($_POST[$comment_meta_key]); |
| 223 |
|
| 224 |
if($meta['required'] == 'on' && $comment_meta_val == ''){ |
| 225 |
|
| 226 |
$default_message = sprintf(__('%s is a required field'), esc_attr($meta['title']) ); |
| 227 |
$message = ($meta['error_message'] != '' ? esc_attr($meta['error_message']) : $default_message); |
| 228 |
wp_die( sprintf(__('%s'), $message) ); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
return $commentdata; |
| 235 |
|
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* rendering comment meta BOX in comments admin page |
| 241 |
*/ |
| 242 |
function render_comment_meta_admin_box($comment) |
| 243 |
{ |
| 244 |
|
| 245 |
add_meta_box('nm_comment_meta_box', __('Comment Meta'), array($this, 'render_comment_meta_admin'), 'comment', 'normal'); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* rendering comment META in comments admin page |
| 250 |
*/ |
| 251 |
function render_comment_meta_admin($comment) { |
| 252 |
|
| 253 |
$comment_meta = get_option('wpcomment_meta'); |
| 254 |
?> |
| 255 |
<table class="form-table editcomment comment_xtra"> |
| 256 |
<tbody> |
| 257 |
<?php foreach ($comment_meta as $index => $meta){ |
| 258 |
|
| 259 |
$comment_meta_key = $meta['data_name']; |
| 260 |
$comment_meta_val = get_comment_meta($comment -> comment_ID, $comment_meta_key, true); |
| 261 |
|
| 262 |
if($comment_meta_val != ''){ |
| 263 |
?> |
| 264 |
<tr valign="top"> |
| 265 |
<td class="first"><?php printf(__('%s'), esc_attr($meta['title'])); ?></td> |
| 266 |
<td><?php echo esc_attr($comment_meta_val); ?></td> |
| 267 |
</tr> |
| 268 |
|
| 269 |
<?php |
| 270 |
} |
| 271 |
} |
| 272 |
?> |
| 273 |
|
| 274 |
</tbody> |
| 275 |
</table> |
| 276 |
<?php |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
/** |
| 281 |
* adding comment meta in front view |
| 282 |
*/ |
| 283 |
function render_comment_meta_front($comment){ |
| 284 |
|
| 285 |
$comment_meta = get_option('wpcomment_meta'); |
| 286 |
|
| 287 |
if($comment_meta){ |
| 288 |
$comment_meta_heading = $this->get_option ( '_comment_heading' ); |
| 289 |
if($comment_meta_heading != '') |
| 290 |
$comment .= '<p><strong>'.sprintf(__('%s'), $comment_meta_heading).'</strong></p>'; |
| 291 |
|
| 292 |
$comment .= '<ul>'; |
| 293 |
foreach ($comment_meta as $index => $meta){ |
| 294 |
|
| 295 |
$comment_meta_key = $meta['data_name']; |
| 296 |
$comment_meta_val = get_comment_meta(get_comment_ID(), $comment_meta_key, true); |
| 297 |
|
| 298 |
if($comment_meta_val != ''){ |
| 299 |
$comment .= '<li><strong>'.sprintf(__('%s: '), esc_attr($meta['title'])).'</strong>'; |
| 300 |
$comment .= sprintf(__('%s'), esc_attr($comment_meta_val)).'</li>'; |
| 301 |
//$comment .= ', '; |
| 302 |
} |
| 303 |
} |
| 304 |
$comment .= '</ul>'; |
| 305 |
//$comment = substr($comment, 0, -2); |
| 306 |
} |
| 307 |
|
| 308 |
return $comment; |
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
/* |
| 313 |
* returning NM_Inputs object |
| 314 |
*/ |
| 315 |
private function get_all_inputs() { |
| 316 |
if (! class_exists ( 'NM_Inputs_wpcomment' )) { |
| 317 |
$_inputs = $this->plugin_meta ['path'] . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'input.class.php'; |
| 318 |
if (file_exists ( $_inputs )) |
| 319 |
include_once ($_inputs); |
| 320 |
else |
| 321 |
die ( 'Reen, Reen, BUMP! not found ' . $_inputs ); |
| 322 |
} |
| 323 |
|
| 324 |
$nm_inputs = new NM_Inputs_wpcomment (); |
| 325 |
// filemanager_pa($this->plugin_meta); |
| 326 |
|
| 327 |
// registering all inputs here |
| 328 |
|
| 329 |
return array ( |
| 330 |
|
| 331 |
'text' => $nm_inputs->get_input ( 'text' ), |
| 332 |
'select' => $nm_inputs->get_input ( 'select' ), |
| 333 |
'radio' => $nm_inputs->get_input ( 'radio' ), |
| 334 |
'checkbox' => $nm_inputs->get_input ( 'checkbox' ), |
| 335 |
); |
| 336 |
|
| 337 |
// return new NM_Inputs($this->plugin_meta); |
| 338 |
} |
| 339 |
|
| 340 |
/* |
| 341 |
* saving admin setting in wp option data table |
| 342 |
*/ |
| 343 |
function save_settings(){ |
| 344 |
|
| 345 |
//pa($_REQUEST); |
| 346 |
$existingOptions = get_option($this->plugin_meta['shortname'].'_settings'); |
| 347 |
//pa($existingOptions); |
| 348 |
|
| 349 |
update_option($this->plugin_meta['shortname'].'_settings', $_REQUEST); |
| 350 |
_e('All options are updated', $this->plugin_meta['shortname']); |
| 351 |
die(0); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
/* |
| 356 |
* saving form meta in admin call |
| 357 |
*/ |
| 358 |
function save_file_meta() { |
| 359 |
|
| 360 |
//print_r($_REQUEST); exit; |
| 361 |
global $wpdb; |
| 362 |
|
| 363 |
update_option('wpcomment_meta', $_REQUEST['file_meta']); |
| 364 |
|
| 365 |
$resp = array ( |
| 366 |
'message' => __ ( 'Form added successfully', 'nm-wpcomments' ), |
| 367 |
'status' => 'success', |
| 368 |
'form_id' => $res_id |
| 369 |
); |
| 370 |
|
| 371 |
echo json_encode ( $resp ); |
| 372 |
|
| 373 |
|
| 374 |
die ( 0 ); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
// ================================ SOME HELPER FUNCTIONS ========================================= |
| 379 |
|
| 380 |
|
| 381 |
function activate_plugin(){ |
| 382 |
|
| 383 |
//do nothing so far. |
| 384 |
|
| 385 |
} |
| 386 |
|
| 387 |
function deactivate_plugin(){ |
| 388 |
|
| 389 |
//do nothing so far. |
| 390 |
} |
| 391 |
} |