PluginProbe
Comments Extra Fields For Post,Pages and CPT / 1.6
Comments Extra Fields For Post,Pages and CPT v1.6
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
wp-comment-fields / classes / plugin.class.php

plugin.class.php in Comments Extra Fields For Post,Pages and CPT 1.6, at classes/plugin.class.php

397 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
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]);
207 }
208 add_comment_meta( $comment_id, $comment_meta_key, $comment_meta_val );
209
210 }
211
212 }
213
214
215
216 /*
217 * Verify comment meta
218 */
219 function verify_comment_meta_data( $commentdata ) {
220
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
227 $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 wp_die( sprintf(__('%s'), $message) );
235 }
236 }
237 }
238
239
240 return $commentdata;
241
242
243 }
244
245 /**
246 * rendering comment meta BOX in comments admin page
247 */
248 function render_comment_meta_admin_box($comment)
249 {
250
251 add_meta_box('nm_comment_meta_box', __('Comment Meta'), array($this, 'render_comment_meta_admin'), 'comment', 'normal');
252 }
253
254 /**
255 * rendering comment META in comments admin page
256 */
257 function render_comment_meta_admin($comment) {
258
259 $comment_meta = get_option('wpcomment_meta');
260 ?>
261 <table class="form-table editcomment comment_xtra">
262 <tbody>
263 <?php foreach ($comment_meta as $index => $meta){
264
265 $comment_meta_key = $meta['data_name'];
266 $comment_meta_val = get_comment_meta($comment -> comment_ID, $comment_meta_key, true);
267
268 if($comment_meta_val != ''){
269 ?>
270 <tr valign="top">
271 <td class="first"><?php printf(__('%s'), esc_attr($meta['title'])); ?></td>
272 <td><?php echo esc_attr($comment_meta_val); ?></td>
273 </tr>
274
275 <?php
276 }
277 }
278 ?>
279
280 </tbody>
281 </table>
282 <?php
283 }
284
285
286 /**
287 * adding comment meta in front view
288 */
289 function render_comment_meta_front($comment){
290
291 $comment_meta = get_option('wpcomment_meta');
292
293 if($comment_meta){
294 $comment_meta_heading = $this->get_option ( '_comment_heading' );
295 if($comment_meta_heading != '')
296 $comment .= '<p><strong>'.sprintf(__('%s'), $comment_meta_heading).'</strong></p>';
297
298 $comment .= '<ul>';
299 foreach ($comment_meta as $index => $meta){
300
301 $comment_meta_key = $meta['data_name'];
302 $comment_meta_val = get_comment_meta(get_comment_ID(), $comment_meta_key, true);
303
304 if($comment_meta_val != ''){
305 $comment .= '<li><strong>'.sprintf(__('%s: '), esc_attr($meta['title'])).'</strong>';
306 $comment .= sprintf(__('%s'), esc_attr($comment_meta_val)).'</li>';
307 //$comment .= ', ';
308 }
309 }
310 $comment .= '</ul>';
311 //$comment = substr($comment, 0, -2);
312 }
313
314 return $comment;
315
316 }
317
318 /*
319 * returning NM_Inputs object
320 */
321 private function get_all_inputs() {
322 if (! class_exists ( 'NM_Inputs_wpcomment' )) {
323 $_inputs = $this->plugin_meta ['path'] . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'input.class.php';
324 if (file_exists ( $_inputs ))
325 include_once ($_inputs);
326 else
327 die ( 'Reen, Reen, BUMP! not found ' . $_inputs );
328 }
329
330 $nm_inputs = new NM_Inputs_wpcomment ();
331 // filemanager_pa($this->plugin_meta);
332
333 // registering all inputs here
334
335 return array (
336
337 'text' => $nm_inputs->get_input ( 'text' ),
338 'select' => $nm_inputs->get_input ( 'select' ),
339 'radio' => $nm_inputs->get_input ( 'radio' ),
340 'checkbox' => $nm_inputs->get_input ( 'checkbox' ),
341 );
342
343 // return new NM_Inputs($this->plugin_meta);
344 }
345
346 /*
347 * saving admin setting in wp option data table
348 */
349 function save_settings(){
350
351 //pa($_REQUEST);
352 $existingOptions = get_option($this->plugin_meta['shortname'].'_settings');
353 //pa($existingOptions);
354
355 update_option($this->plugin_meta['shortname'].'_settings', $_REQUEST);
356 _e('All options are updated', $this->plugin_meta['shortname']);
357 die(0);
358 }
359
360
361 /*
362 * saving form meta in admin call
363 */
364 function save_file_meta() {
365
366 //print_r($_REQUEST); exit;
367 global $wpdb;
368
369 update_option('wpcomment_meta', $_REQUEST['file_meta']);
370
371 $resp = array (
372 'message' => __ ( 'Form added successfully', 'nm-wpcomments' ),
373 'status' => 'success',
374 'form_id' => $res_id
375 );
376
377 echo json_encode ( $resp );
378
379
380 die ( 0 );
381 }
382
383
384 // ================================ SOME HELPER FUNCTIONS =========================================
385
386
387 function activate_plugin(){
388
389 //do nothing so far.
390
391 }
392
393 function deactivate_plugin(){
394
395 //do nothing so far.
396 }
397 }