PluginProbe
Comments Extra Fields For Post,Pages and CPT / 1.0
Comments Extra Fields For Post,Pages and CPT v1.0
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 / nm-framework.php

nm-framework.php in Comments Extra Fields For Post,Pages and CPT 1.0, at classes/nm-framework.php

657 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Detail: This is main framework class for N-Media plugins
4 * Author: Najeeb Ahmad
5 *
6 * =============== Properties ===============
7 * $plugin_meta An array contains all plugin properties/meta
8 * $wp_shipped_scripts array containing array of shipped scripts
9 * $plugin_scripts array containing arrays of plugin scripts
10 * $plugin_settings plugin settings/options saved in backend
11 * $ajax_callbacks array containing all ajax callbacks
12 * =============== HOOKS =============
13 * All hooks (add_actions) will be registered in this class
14 *
15 * ============== Model ==============
16 * All DB related function are defined in this class
17 * ----- get_single_data ($select, $where_data, $switch="AND", $debug = false)
18 * ----- get_row_data ($select, $where_data, $switch="AND", $debug = false)
19 * ----- get_rows_data ($select, $where_data = NULL, $switch="AND", $debug = false)
20 * ----- insert_table ($table, $data, $format, $debug = false)
21 * ----- update_table ($table, $data, $where, $format, $where_format,$debug = false)
22 * ----- delete_data ($table, $where_data, $switch = "AND", $debug)
23 */
24
25
26 class NM_Framwork_V1_WPComment{
27
28
29 /*
30 * An array contains all plugin properties/meta
31 */
32 var $plugin_meta;
33
34
35 /*
36 * load scripts shipped with WP
37 * single array
38 */
39 var $wp_shipped_scripts;
40
41 /*
42 * JS scripts and styles to loaded FRONT END
43 * Arrays of scripts to be loaded e.g:
44 * array('script_name' => 'scripts',
45 'script_source' => '/js/script.js',
46 'localized' => true,
47 'type' => 'js'
48 )
49 */
50 var $plugin_scripts;
51
52 /*
53 * localized var for js script
54 */
55 var $localized_vars;
56
57 /*
58 * plugin settings/options saved in backend
59 */
60 var $plugin_settings;
61
62
63 /*
64 * following array are functions name and ajax callback handlers
65 */
66 var $ajax_callbacks;
67
68
69 /*
70 * validation array: contains error record when function called v();
71 * added on 3 Feb, 2013
72 */
73 var $validation = array();
74
75
76 /*
77 * following function is registering all actions being set by plugin
78 */
79
80 function load_scripts(){
81
82 // shipped scripts
83 if($this -> wp_shipped_scripts){
84 foreach($this -> wp_shipped_scripts as $handler){
85 wp_enqueue_script($handler);
86 }
87 }
88
89
90 //front end scripts
91 if($this -> plugin_scripts){
92 foreach($this -> plugin_scripts as $script){
93
94 //checking if it is style
95 if( $script['type'] == 'js'){
96 wp_enqueue_script($this->plugin_meta['shortname'].'-'.$script['script_name'], $this->plugin_meta['url'].$script['script_source'], __FILE__);
97
98 //if localized
99 if( $script['localized'] )
100 wp_localize_script( $this->plugin_meta['shortname'].'-'.$script['script_name'], $this->plugin_meta['shortname'].'_vars', $this -> localized_vars);
101 }else{
102
103 wp_enqueue_style($this->plugin_meta['shortname'].'-'.$script['script_name'], $this->plugin_meta['url'].$script['script_source'], __FILE__);
104 }
105 }
106 }
107 }
108
109
110 /*
111 * registering callback
112 */
113 function do_callbacks(){
114
115 foreach ($this -> ajax_callbacks as $callback){
116 add_action( 'wp_ajax_'.$this->plugin_meta['shortname'].'_'.$callback, array($this, $callback) );
117 add_action( 'wp_ajax_nopriv_'.$this->plugin_meta['shortname'].'_'.$callback, array($this, $callback) );
118 }
119 }
120
121
122 /*
123 * for multi language support
124 */
125 function wpp_textdomain() {
126 load_plugin_textdomain($this->plugin_meta['shortname'], false, $this->plugin_meta['path'].'/locale/');
127 }
128
129 /*
130 * ===================== DB related functions ========================
131 *
132 * ====================================================================
133 */
134
135 /*
136 * This functnio returning Single field value
137 */
138
139 function get_single_data($select, $where_data, $switch="AND", $debug = false)
140 {
141
142 global $wpdb;
143
144 $requested_col = '';
145 foreach ($select as $table => $col){
146
147 $requested_col = $col;
148
149 $this -> qry .= 'SELECT '.$col.' FROM
150 '.$wpdb -> prefix . $table;
151
152 }
153
154 $where = ' WHERE ';
155 $format = array();
156 foreach ($where_data as $type => $pair){
157
158 if ($type == 's'){
159 foreach ($pair as $f => $d){
160 $where .= " $f = %s";
161 $format[] = $d;
162 }
163 }else if ($type == 'd'){
164 foreach ($pair as $f => $d){
165 $where .= " $f = %d";
166 $format[] = $d;
167 }
168 }
169
170
171 $where .= " $switch ";
172 }
173
174
175 $where = substr_replace($where,"",-4);
176
177 $this -> qry .= $where;
178
179 $res = $wpdb -> get_results($wpdb -> prepare($this -> qry, $format));
180 //pa($res);
181
182 if($debug)
183 $this->print_query(__FUNCTION__);
184
185 $this -> qry = '';
186 //clearing the cache
187 $wpdb -> flush();
188
189 return $res[0] -> $requested_col;
190 }
191
192
193 /*
194 * this function returning the single row:
195 * return is OBJECT
196 */
197 function get_row_data($select, $where_data, $switch="AND", $debug = false)
198 {
199
200 global $wpdb;
201
202
203 $requested_col = '';
204 foreach ($select as $table => $cols){
205
206 if (is_array($cols))
207 $cols = implode(',', $cols);
208
209 $this -> qry .= 'SELECT '.$cols.' FROM
210 '.$wpdb -> prefix . $table;
211
212 }
213
214 $qry = "SELECT ".$column_name."
215 FROM `".$table."`";
216
217 $where = ' WHERE ';
218 $format = array();
219 foreach ($where_data as $type => $pair){
220
221 if ($type == 's'){
222 foreach ($pair as $f => $d){
223 $where .= " $f = %s";
224 $format[] = $d;
225 }
226 }else if ($type == 'd'){
227 foreach ($pair as $f => $d){
228 $where .= " $f = %d";
229 $format[] = $d;
230 }
231 }
232
233
234 $where .= " $switch ";
235 }
236
237
238 $where = substr_replace($where,"",-4);
239
240 $this -> qry .= $where;
241
242
243 $res = $wpdb -> get_results($wpdb -> prepare($this -> qry, $format));
244
245 //$this -> setQuery($sql, 'getSingleData');
246
247 if($debug)
248 $this->print_query(__FUNCTION__);
249
250 $this -> qry = '';
251 //clearing the cache
252 $wpdb -> flush();
253
254 return $res[0];
255 }
256
257
258 /*
259 * this function returning the ROWS set:
260 * return is OBJECT
261 */
262 function get_rows_data($select, $where_data = NULL, $switch="AND", $debug = false)
263 {
264
265 global $wpdb;
266
267
268 $requested_col = '';
269 foreach ($select as $table => $cols){
270
271 if (is_array($cols))
272 $cols = implode(',', $cols);
273
274 $this -> qry .= 'SELECT '.$cols.' FROM
275 '.$wpdb -> prefix . $table;
276
277 }
278
279 $qry = "SELECT ".$column_name."
280 FROM `".$table."`";
281
282 //if criteria is false then no need below
283 if($where_data != NULL){
284
285 $where = ' WHERE ';
286 $format = array();
287 foreach ($where_data as $type => $pair){
288
289 if ($type == 's'){
290 foreach ($pair as $f => $d){
291 $where .= " $f = %s";
292 $format[] = $d;
293 }
294 }else if ($type == 'd'){
295 foreach ($pair as $f => $d){
296 $where .= " $f = %d";
297 $format[] = $d;
298 }
299 }
300
301
302 $where .= " $switch ";
303 }
304
305
306 $where = substr_replace($where,"",-4);
307
308 $this -> qry .= $where;
309 }else{
310
311 $where = ' WHERE 1 = %d';
312 $format[] = 1;
313 $this -> qry .= $where;
314 }
315
316
317
318 $res = $wpdb -> get_results($wpdb -> prepare($this -> qry, $format));
319
320 //$this -> setQuery($sql, 'getSingleData');
321
322 if($debug)
323 $this->print_query(__FUNCTION__);
324
325
326 $this -> qry = '';
327 //clearing the cache
328 $wpdb -> flush();
329
330
331
332 return $res;
333 }
334
335 /*
336 * DB injection function [insert, update, deletee]
337 */
338
339 // INSERT ==========================================================
340 public function insert_table($table, $data, $format, $debug = false)
341 {
342 global $wpdb;
343
344 $wpdb->insert($wpdb->prefix.$table, $data, $format);
345
346 if($debug)
347 $this -> print_query(__FUNCTION__);
348
349 if($wpdb -> insert_id)
350 return true;
351 else
352 return false;
353 }
354
355
356
357 // UPDATE ==========================================================
358 public function update_table($table, $data, $where, $format, $where_format,$debug = false)
359 {
360 global $wpdb;
361
362 $rows_effected = $wpdb->update($wpdb ->prefix.$table, $data, $where, $format = null, $where_format = null);
363
364 if($debug)
365 $this -> print_query(__FUNCTION__);
366
367 if($rows_effected)
368 return true;
369 else
370 return false;
371 }
372
373 // DELETE ==========================================================
374 function delete_data($table, $where_data, $switch = "AND", $debug){
375
376 global $wpdb;
377
378 $this -> qry = "DELETE FROM `".$wpdb -> prefix . $table."`";
379
380 $where = ' WHERE ';
381 $format = array();
382 foreach ($where_data as $type => $pair){
383
384 if ($type == 's'){
385 foreach ($pair as $f => $d){
386 $where .= " $f = %s";
387 $format[] = $d;
388 }
389 }else if ($type == 'd'){
390 foreach ($pair as $f => $d){
391 $where .= " $f = %d";
392 $format[] = $d;
393 }
394 }
395
396
397 $where .= " $switch ";
398 }
399
400
401 $where = substr_replace($where,"",-4);
402 $this -> qry .= $where;
403
404 $wpdb -> query($wpdb -> prepare($this -> qry, $format));
405
406 //$this -> setQuery($sql, 'getSingleData');
407
408 if($debug)
409 $this->print_query(__FUNCTION__);
410
411 $this -> qry = '';
412 //clearing the cache
413 $wpdb -> flush();
414
415 }
416
417 /*
418 * print query
419 * and error iF ANY
420 */
421
422 public function print_query($caller_function)
423 {
424 global $wpdb;
425
426 echo '<br> The query is being called from '.$caller_function.'<br>';
427
428 $wpdb->show_errors();
429 $wpdb->print_error();
430
431 if($this->kill_script)
432 {
433 echo '------- Exiting the script --------';
434 exit;
435 }
436
437 }
438
439 /*
440 This function checks for duplicate entry in database, returns True/False.
441 */
442 /* public function isAlreadyExist($table, $field, $value)
443 {
444
445 $duplicateRec = $this-> _getSingleData($table, 'COUNT(*)', "$field = '$value'");
446 if($duplicateRec > 0)
447 {
448 return true;
449 }
450 else
451 {
452 return false;
453 }
454
455 } */
456
457 /*
458 * ===================== ENDS DB related functions ========================
459 *
460 * =========================================================================
461 */
462
463
464 /*
465 * ================================ SOME COMMON FUNCTION ===================
466 * ==========================================================================
467 */
468
469 /*
470 * this function is extrating single
471 * option from nm_plugin_settings serialzied object/data
472 * key must be prefixed e.g: _key
473 */
474 function get_option($key){
475
476 //HINT: $key should be under schore (_) prefix
477
478 $full_key = $this->plugin_meta['shortname'] . $key;
479 $the_option = $this -> plugin_settings[ $full_key ];
480
481 if (is_array($the_option))
482 return $the_option;
483 else
484 return stripcslashes( $the_option );
485
486 }
487
488
489 function load_template($file_name, $variables=array('')){
490
491 extract($variables);
492
493 $file_path = $this -> plugin_meta['path'] . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $file_name;
494 if (file_exists($file_path))
495 include_once($file_path);
496 else
497 die('Could not load file '.$file_path);
498 }
499
500 /*
501 * print array in readable form
502 */
503 function pa($arr){
504
505 echo '<pre>';
506 print_r($arr);
507 echo '</pre>';
508 }
509
510
511 /*
512 * generate url with query strings
513 */
514 function nm_plugin_fix_request_uri($vars){
515 $uri = str_replace( '%7E', '~', $_SERVER['REQUEST_URI']);
516 $parts = explode("?", $uri);
517
518 $qsArr = array();
519 if(isset($parts[1])){ ////// query string present explode it
520 $qsStr = explode("&", $parts[1]);
521 foreach($qsStr as $qv){
522 $p = explode("=",$qv);
523 $qsArr[$p[0]] = $p[1];
524 }
525 }
526
527 //////// updatig query string
528 foreach($vars as $key=>$val){
529 if($val==NULL) unset($qsArr[$key]); else $qsArr[$key]=$val;
530 }
531
532 ////// rejoin query string
533 $qsStr="";
534 foreach($qsArr as $key=>$val){
535 $qsStr.=$key."=".$val."&";
536 }
537 if($qsStr!="") $qsStr=substr($qsStr,0,strlen($qsStr)-1);
538 $uri = $parts[0];
539 if($qsStr!="") $uri.="?".$qsStr;
540 return $uri;
541 }
542
543
544 /*
545 * return timestamp into time interval like 2 days ago etc
546 */
547 function nm_plugin_time_difference($date)
548 {
549 if(empty($date)) {
550 return "No date provided";
551 }
552
553 $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
554 $lengths = array("60","60","24","7","4.35","12","10");
555
556 $now = time();
557 $unix_date = strtotime($date);
558
559 // check validity of date
560 if(empty($unix_date)) {
561 return "Bad date";
562 }
563
564 // is it future date or past date
565 if($now > $unix_date) {
566 $difference = $now - $unix_date;
567 $tense = "ago";
568
569 } else {
570 $difference = $unix_date - $now;
571 $tense = "from now";
572 }
573
574 for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
575 $difference /= $lengths[$j];
576 }
577
578 $difference = round($difference);
579
580 if($difference != 1) {
581 $periods[$j].= "s";
582 }
583
584 return "$difference $periods[$j] {$tense}";
585 }
586
587 /*
588 * ================================ SOME COMMON FUNCTION =======================================
589 * =============================================================================================
590 */
591
592
593 /*
594 * ================================ VALIDATION FUNCTION =======================================
595 * =============================================================================================
596 */
597
598 function validate($validate)
599 {
600
601 foreach ($validate as $item){
602
603 if($item['data'] == '')
604 $this -> validation[] .= $item['title'].' cannot be empty.<br />';
605
606 switch ($item['type']){
607
608 case 'email':
609 if(!is_email($item['data']))
610 $this -> validation[] .= $item['title'].' must be valid E-mail.<br />';
611 break;
612
613 case 'number':
614 if(!$this -> is_number($item['data']))
615 $this -> validation[] .= $item['title'].' must be valid Number.<br />';
616 break;
617
618 case 'integer':
619 if(!$this -> is_integer($item['data']))
620 $this -> validation[] .= $item['title'].' must be valid Integer.<br />';
621 break;
622 }
623
624 if($item['data2'] != null && !$this -> is_matched($item['data'], $item['data2']))
625 $this -> validation[] .= $item['title'].' are not matching, please recheck '.$item['title'].'<br />';
626 }
627
628 }
629
630
631
632 function is_matched($p1, $p2)
633 {
634 if(strcmp($p1, $p2) == 0)
635 return true;
636 else
637 return false;
638 }
639
640 function is_integer($integer)
641 {
642 if(ctype_digit($integer) == 1)
643 return true;
644 else
645 return false;
646 }
647
648 function is_number($number)
649 {
650 if(is_numeric($number))
651 return true;
652 else
653 return false;
654 }
655
656
657 }