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
wp-comment-fields / classes / plugin.class.php

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

378 lines 9.0 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 require_once wpcf_get_class_path() . '/nm-framework.php';
10 }
11
12 /*
13 * [1]
14 * TODO: change the class name of your plugin
15 */
16 class NM_PLUGIN_WPComments extends NM_Framwork_V1_WPComment {
17
18 private static $ins = null;
19
20 public static function init() {
21 add_action( 'plugins_loaded', array( self::get_instance(), '_setup' ) );
22 }
23
24 public static function get_instance() {
25 // create a new object if it doesn't exist.
26 is_null( self::$ins ) && self::$ins = new self();
27 return self::$ins;
28 }
29
30
31 var $inputs;
32
33 /*
34 * plugin constructur
35 */
36 function _setup() {
37
38 // setting plugin meta saved in config.php
39 $this->plugin_meta = get_plugin_meta_wpcomment();
40
41 // getting saved settings
42 $this->plugin_settings = get_option( $this->plugin_meta['shortname'] . '_settings' );
43
44 // populating $inputs with NM_Inputs object
45 $this->inputs = $this->get_all_inputs();
46
47 /*
48 * [2]
49 * TODO: update scripts array for SHIPPED scripts
50 * only use handlers
51 */
52 // setting shipped scripts
53 $this->wp_shipped_scripts = array( 'jquery' );
54
55 /*
56 * [3]
57 * TODO: update scripts array for custom scripts/styles
58 */
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
82 /*
83 * [4]
84 * TODO: localized array that will be used in JS files
85 * Localized object will always be your pluginshortname_vars
86 * e.g: pluginshortname_vars.ajaxurl
87 */
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
94 /*
95 * [5]
96 * TODO: this array will grow as plugin grow
97 * all functions which need to be called back MUST be in this array
98 * setting callbacks
99 */
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
106 /*
107 * plugin localization being initiated here
108 */
109 add_action( 'init', array( $this, 'wpp_textdomain' ) );
110
111 /*
112 * hooking up scripts for front-end
113 */
114 add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
115
116 /*
117 * registering callbacks
118 */
119 $this->do_callbacks();
120
121 /*
122 * Action hooks for comment fields
123 */
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
127 /*
128 * Saving comment meta
129 */
130 add_action( 'comment_post', array( $this, 'save_comment_meta_fields' ) );
131
132 /*
133 * adding meta box in comments edit page
134 */
135 add_action( 'add_meta_boxes_comment', array( $this, 'render_comment_meta_admin_box' ), 1 );
136
137 /**
138 * adding comment meta in front view
139 */
140 add_filter( 'comment_text', array( $this, 'render_comment_meta_front' ), 100 );
141
142 /**
143 * validating comments
144 */
145 add_filter( 'preprocess_comment', array( $this, 'verify_comment_meta_data' ) );
146 }
147
148
149
150 // i18n and l10n support here
151 // plugin localization
152 function wpp_textdomain() {
153 $locale_dir = $this->plugin_meta['path'] . '/locale/';
154 load_plugin_textdomain( 'nm-wpcomments', false, $locale_dir );
155 }
156
157
158 /*
159 * =============== NOW do your JOB ===========================
160 *
161 */
162
163 /*
164 * Callbacks for comments fields
165 */
166
167 function render_comments_meta_fields() {
168
169 global $post;
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 ) ) {
177 return;
178 }
179 }
180
181 $this->load_template( 'render.comment.meta.php' );
182 }
183
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
190 $comment_meta_key = $meta['data_name'];
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 ] );
196 }
197 add_comment_meta( $comment_id, $comment_meta_key, $comment_meta_val );
198
199 }
200
201 }
202
203
204
205 /*
206 * Verify comment meta
207 */
208 function verify_comment_meta_data( $commentdata ) {
209
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
215 $comment_meta_key = $meta['data_name'];
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 ) ) );
224 }
225 }
226 }
227
228 return $commentdata;
229 }
230
231 /**
232 * rendering comment meta BOX in comments admin page
233 */
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' );
237 }
238
239 /**
240 * rendering comment META in comments admin page
241 */
242 function render_comment_meta_admin( $comment ) {
243
244 $comment_meta = get_option( 'wpcomment_meta' );
245 ?>
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
269 }
270
271
272 /**
273 * adding comment meta in front view
274 */
275 function render_comment_meta_front( $comment ) {
276
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
285 $comment .= '<ul>';
286 foreach ( $comment_meta as $index => $meta ) {
287
288 $comment_meta_key = $meta['data_name'];
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 }
296 }
297 $comment .= '</ul>';
298 // $comment = substr($comment, 0, -2);
299 }
300
301 return $comment;
302
303 }
304
305
306
307 /*
308 * returning NM_Inputs object
309 */
310 private function get_all_inputs() {
311
312 if ( ! class_exists( 'NM_Inputs_wpcomment' ) ) {
313 require_once wpcf_get_class_path() . '/input.class.php';
314
315 }
316
317 $nm_inputs = new NM_Inputs_wpcomment();
318 // filemanager_pa($this->plugin_meta);
319 // registering all inputs here
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' ),
326 );
327
328 // return new NM_Inputs($this->plugin_meta);
329 }
330
331 /*
332 * saving admin setting in wp option data table
333 */
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 );
342 }
343
344
345 /*
346 * saving form meta in admin call
347 */
348 function save_file_meta() {
349
350 // print_r($_REQUEST); exit;
351 global $wpdb;
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 );
364 }
365
366
367 // ================================ SOME HELPER FUNCTIONS =========================================
368 function activate_plugin() {
369
370 // do nothing so far.
371 }
372
373 function deactivate_plugin() {
374
375 // do nothing so far.
376 }
377 }
378