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