PluginProbe
Comments Extra Fields For Post,Pages and CPT / 1.7
Comments Extra Fields For Post,Pages and CPT v1.7
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.7, at classes/plugin.class.php

398 lines 9.5 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 $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)) );
236 }
237 }
238 }
239
240
241 return $commentdata;
242
243
244 }
245
246 /**
247 * rendering comment meta BOX in comments admin page
248 */
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');
253 }
254
255 /**
256 * rendering comment META in comments admin page
257 */
258 function render_comment_meta_admin($comment) {
259
260 $comment_meta = get_option('wpcomment_meta');
261 ?>
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
284 }
285
286
287 /**
288 * adding comment meta in front view
289 */
290 function render_comment_meta_front($comment){
291
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
299 $comment .= '<ul>';
300 foreach ($comment_meta as $index => $meta){
301
302 $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 }
310 }
311 $comment .= '</ul>';
312 //$comment = substr($comment, 0, -2);
313 }
314
315 return $comment;
316
317 }
318
319 /*
320 * returning NM_Inputs object
321 */
322 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 );
329 }
330
331 $nm_inputs = new NM_Inputs_wpcomment ();
332 // filemanager_pa($this->plugin_meta);
333
334 // 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' ),
342 );
343
344 // return new NM_Inputs($this->plugin_meta);
345 }
346
347 /*
348 * saving admin setting in wp option data table
349 */
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);
359 }
360
361
362 /*
363 * saving form meta in admin call
364 */
365 function save_file_meta() {
366
367 //print_r($_REQUEST); exit;
368 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 );
382 }
383
384
385 // ================================ SOME HELPER FUNCTIONS =========================================
386
387
388 function activate_plugin(){
389
390 //do nothing so far.
391
392 }
393
394 function deactivate_plugin(){
395
396 //do nothing so far.
397 }
398 }