/
functions.php
1770 lines (1448 loc) · 54.7 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
session_start();
/**
* Functions I use to aid myself in the development of a website.
*
* Disclaimer:
* Use it at your own risk, most functions were tested fully,
* some have not been tested completely, and not all of them
* log errors, feel free to collaborate if you find any bugs.
*
* @author Johnny Calderon
*
* CONTENT:
* - Constants and Variables
* - Configuration Functions
* - Site Data Functions
* - Markup Generator Functions
* - File Handler Functions
* - Analytic Functions
* - Social Media Functions
* - Article / Blog Functions
* - Classes
* - Error Handler Functions
*/
/*******************************************/
/* CONSTANTS AND VARIABLES */
/*******************************************/
/**
* Used for checking processing time of your whole website.
* Echo get_process-time() in the last line of your last
* included file to get the processing time as whole.
*/
define( 'START_TIME', microtime( true ) );
/**
* ---- IMPORTANT!!!!!!!!!
* Set PRODUCTION to true when deploying the site to production
*/
define( 'PRODUCTION', false );
/**
* Store configuration parameters in variable
*/
$config_params = break_config();
/**
* The root directory of your website will be chosen according
* to PRODUCTION constant, production path on true, development path on false
*/
$root = ( PRODUCTION ) ? dirname( __FILE__ ) : $config_params['dev_url'];
/**
* Associative array which stores all the errors produced during
* runtime
*/
$site_errors = array();
/****************************************/
/* CONFIGURATION FUNCTIONS */
/****************************************/
/**
* This function creates an xml config file to set constant elements of the website
* You may use this file once in the index.php file at the top, before any other
* function; once the file is ran, you can remove the function from your file.
*
* NOTE: Only few functions require the configuration file directly, however other make use of the config file indirectly, check the
* documentation. It is recommended to create the file at least with the site
* url.
*
* @param string $action Defaults to 'info' display configuration in HTML format,
* use 'create' to create the file and add settings, use 'update'
* to add, remove or update values.
* @param array $settings Site's settings' parameters and values
*/
function site_config ( $action = 'info', $settings = array() ) {
global $site_errors;
$output = '';
if ( $action == 'create' ) {
// Check for existing files
if ( ! file_exists( 'site-config.json' ) ) {
// Whether user wants json or xml
/* JSON SETUP */
$site_settings = array ( 'site_configuration' => array ( $settings ) );
$json_file = fopen( 'site-config.json' , 'w' );
if ( fwrite( $json_file, json_encode ( $site_settings ) ) ) {
$output = 'site-config.json created succesfully';
} else {
$output = 'Error writing info into site-config.json';
}
fclose( $json_file );
} else {
$site_errors['site_config() found a site-congif.json file already in the system'] = 'CREATE_CONFIG_ERROR';
}
} elseif ( $action == 'update' ) {
/* JSON CONFIGURATION */
$site_data = json_decode ( file_get_contents ( 'site-config.json' ), true );
$cur_settings = $site_data['site_configuration'];
foreach ( $settings as $setting => $value ) :
$cur_settings[$setting] = $value;
endforeach;
$json_file = fopen( 'site-config.json' , 'w' );
if ( fwrite( $json_file, json_encode ( $cur_settings ) ) ) {
$output = 'site-config.json updated successfully';
} else {
$site_errors['site_config() could not update the file'] = 'UPDATE_CONFIG_ERROR';
}
fclose( $json_file );
} elseif ( $action == 'info' ) {
/* DATA FROM JSON CONFIG FILE */
if ( file_exists ( 'site-config.json' ) ) {
$site_data = json_decode ( file_get_contents ( 'site-config.json' ), true );
$output .= '<div class="site-settings"><h1>SITE SETTINGS</h1><table class="settings-table" border="1"><tr><th>Setting</th><th>Value</th></tr>';
foreach ( $site_data['site_configuration'] as $site_config ) :
foreach ( $site_config as $setting => $value ) :
$output .= sprintf( '<tr><td>%1$s</td><td>%2$s</td></tr>', $setting, $value );
endforeach;
endforeach;
$output .= '</table></div><!-- .site-settings -->';
} else {
$site_errors['site_config() Could not find site-config.json'] = 'INFO_CONFIG_ERROR';
}
}
return $output;
}
/**
* Gets all parameters from site-config.json into an Assoc array
* @return [Array]
*/
function break_config() {
$output;
$config = json_decode( file_get_contents( 'site-config.json' ), true );
$output = $config['site_configuration'];
return $output;
}
/**
* GZIP ompression function
*/
function gzip_comp() {
if ( substr_count ( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) ) :
ob_start("ob_gzhandler");
else :
ob_start();
endif;
}
/**************************************/
/* SITE DATA FUNCTIONS */
/**************************************/
/**
* Get the language of the site from config file
*
* @return Language from json o xml file
*/
function get_site_lang () {
global $site_errors;
$output = '';
if ( file_exists( 'site-config.json' ) ) {
$settings = json_decode ( file_get_contents( 'site-config.json' ) );
$output = $settings->site_configuration->lang;
} else {
$site_errors['get_site_lang() Could not find site-config.json file.'] = 'CONFIG_ERROR';
}
return $output;
}
/**
* Get the current file displayed by the browser
*
* @return string Returns the file name
*/
function get_current_file () {
$file = basename ( $_SERVER['PHP_SELF'] );
return $file;
}
/**
* Returns the site URL from site-config.xml
*
* @param string $action use 'echo' to display the result, defaults to return.
*/
function get_site_url () {
global $site_errors;
$output = '';
if ( file_exists ( 'site-config.json' ) ) {
$settings = json_decode ( file_get_contents( 'site-config.json' ) );
if ( isset( $settings->site_configuration->url ) && ! empty( $settings->site_configuration->url ) ) {
$output = $settings->site_configuration->url;
} else {
$site_errors['URL parameter not found in configuration file'] = 'CONFIG_ERROR';
}
} else {
$site_errors['get_site_url() Could not find site-config.json file.'] = 'CONFIG_ERROR';
}
return $output;
}
/**
* Gets the requested url and returns it or echos it
*/
function get_requested_url () {
$protocol = ( ! empty ( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ) ? "https://" : "http://";
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
return $protocol . $url;
}
/**
* Gets the file name without the extension to pass it as an ID
* returns the result
*/
function get_page_id () {
$file = get_current_file();
$id = preg_replace ( '/\.php|\.html|\.css|\.js/', '', $file );
return $id;
}
/**
* Get the actual file name and displays it as the title followed by the site name
*
* @param string $title Defines the title for the home page if not entered will
* display Home, and it will change accross the pages
* @param string $site Defines the site name if not passed a text version of the
* Domain will be used, this won't change accross the pages
*/
function get_page_title ( $title, $site ) {
$output = '';
if ( 'index' == get_page_id() ) {
if ( $title == '' ) {
$page = 'Home';
} else {
$page = $title;
}
} else {
$raw_url = explode ( '/', get_requested_url() );
$last_el = count ( $raw_url ) - 1;
$raw_title = ucfirst ( str_replace ( array ( '-', '_' ), ' ', $raw_url[$last_el] ) );
if ( preg_match ( '/(.php|.ph|.html|.htm)$/', $raw_title ) ) {
$ar_title = explode ( '.', $raw_title );
$page = $ar_title[0];
} else {
$page = $raw_title;
}
//$page = ucfirst ( str_replace ( array( '-', '_' ), ' ', get_page_id() ) );
}
$rpc = array( 'http://', 'https://', 'www.', '.com', '.net', '.biz', '.org', '.info', '.tk', '.it', '.ec' );
$domain = ( $site == null || empty ( $site ) ) ? str_replace( $rpc, '', $_SERVER['HTTP_HOST'] ) : $site;
$output = $domain . ' | ' . $page;
return $output;
}
/**
* Includes header file for the theme
*
* @param string $file The suffix of the file to be included
*/
function get_header ( $file = '' ) {
global $root, $site_errors;
$header_file = ( $file == null || $file == '' ) ? 'header.php' : 'header-' . $file . '.php';
if ( file_exists ( $header_file ) ) {
include $root . '/' . $header_file;
} else {
$site_errors['Header file could not be found'] = 'GET_ERROR';
}
}
/**
* Includes Footer file for the theme
*
* @param string $file The suffix of the file to be included
*/
function get_footer ( $file = '' ) {
global $root, $site_errors;
$footer_file = ( $file == null || $file == '' ) ? 'footer.php' : 'footer-' . $file . '.php';
if ( file_exists ( $footer_file ) ) {
include $root . '/' . $footer_file;
} else {
$site_errors['Footer file could not be found.'] = 'GET_ERROR';
}
}
/**
* Includes a sidebar file whereever it is called
*
* @param string $file The suffix of the file to be included e.g. sidebar-home.php
*/
function get_sidebar ( $file = '' ) {
global $root, $site_errors;
$sidebar_file = ( isset( $file ) && ! empty( $file ) ) ? 'sidebar-' . $file . '.php' : 'sidebar.php';
if ( file_exists ( $sidebar_file ) ) {
include $root . '/' . $sidebar_file;
} else {
$site_errors['Sidebar file could not be found'] = 'GET_ERROR';
}
}
/****************************************/
/* MARKUP GENERATOR FUNCTIONS */
/****************************************/
/**
* Add the default meta tags and any other custom meta tag to the document
*
* @param array $tags indicates the value and attribute of the meta tag
* @param boolean $overwrite true: overwrites the defaults, false: display defaults
*/
function add_meta_tags ( $tags = array(), $overwrite = false ) {
// didplay defaults if overwrite false
if ( $overwrite == false ) {
$defaults = array (
array ( 'utf-8' => 'charset' ),
array ( 'viewport' => 'name', 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0' => 'content' )
);
array_unshift ( $tags, $defaults[0], $defaults[1] );
}
foreach ( $tags as $tag => $content ) {
printf ( '<meta' );
foreach ( $content as $desc => $attr ) {
printf ( ' %1$s="%2$s"', $attr, $desc );
}
printf ( '/>' );
}
}
/**
* Add default link tags and any other custom link tags to the document
*
* @param array $tags indicates the file and the rel attribute of the link tags
* @param boolea $overwrite true: overwrites default, false: display defaults
*/
function add_style_tags ( $tags = array(), $overwrite = false ) {
if ( $overwrite == false ) {
$tags = array( 'style.css' => 'stylesheet' ) + $tags;
}
foreach ( $tags as $tag => $rel ) {
if ( preg_match ( '/^http/', $tag ) ) {
printf ( '<link rel="%1$s" href="%2$s"/>', $rel, $tag );
} else {
printf ( '<link rel="%1$s" href="%2$s"/>', $rel, get_site_url() . '/' . $tag );
}
}
}
/**
* Add default scripts files plus any other custom script files
* It also adds the html5shiv.js for backward html5 compatibility for IE browsers
* which cannot be overwritten
*
* @param array $files defines javascript files to use in the document and whether
* they are local or remote, when remote full path must be passed
* @param boolean $overwrite true: overwrites default files, false: display defaults
*/
function add_script_tags ( $files = array() ) {
$output = '';
$output .= sprintf ( '<!--[if lt IE 9]><script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->' );
$files = array( 'https://www.google.com/recaptcha/api.js' => 'remote', ) + $files;
foreach ( $files as $file => $location ) {
if( $location == 'remote' ) {
$output .= sprintf ( '<script type="text/javascript" src="%s"></script>', $file );
} else {
$output .= sprintf ( '<script type="text/javascript" src="%s"></script>', get_site_url() . '/js/' . $file );
}
}
echo $output;
}
/**
* Creates a navigation bar where applied, if class 'main-nav' is applied the function will echo a navigation toggle element
* @param [array] $args Containing the available options for the navigation bar
* @return [type] [description]
*/
function add_navbar ( $args ) {
$output = '';
// setting defaults
// Position: main-nav{default}, footer-nav, side-nav, content-nav
$position = ( isset ( $args['position'] ) && ! empty ( $args['position'] ) ) ? $args['position'] : 'main-nav';
// Toggle_type: text{default}, icon
$toggle_type = ( isset ( $args['toggle_type'] ) && ! empty ( $args['toggle_type'] ) ) ? $args['toggle_type'] : 'text';
$a_class = ( isset ( $args['link_class'] ) && ! empty ( $args['link_class'] ) ) ? $args['link_class'] : '';
$tgl_txt_class = ( isset ( $args['toggle_class'] ) && ! empty ( $args['toggle_class'] ) ) ? 'class="' . $args['toggle_class'] . '"' : '';
$search_bar = ( isset( $args['search_bar'] ) ) ? $args['search_bar'] : true;
// Creating the actual navbar
$output .= sprintf ( '<nav class="%s">', $position );
if ( $position == 'main-nav' ) {
$toggle_id = ( empty( $args['toggle_id'] ) || $args['toggle_id'] == null ) ? '' : 'id="' . $args['toggle_id'] . '"';
$toggle_class = 'toggle-menu';
if ( $toggle_type == 'text' ) {
$toggle_txt = 'MENU';
} else {
$toggle_txt = '';
$toggle_class .= ' toggle-icon';
}
$output .= sprintf ( '<div %1$s class="%2$s"><a %4$s >%3$s</a></div>', $toggle_id, $toggle_class, $toggle_txt, $tgl_txt_class );
}
$nav_id = ( isset( $args['nav_id'] ) && ! empty ( $args['nav_id'] ) ) ? 'id="'. $args['nav_id'] .'"' : '';
$output .= sprintf ( '<ul %s class="nav-container">', $nav_id );
foreach ( $args['nav_links'] as $nav_item => $file ) {
/**
* Compares each file to the current file to apply current class to item
*/
$comp_file = ( $file == '/' || empty ( $file ) || $file == '#') ? 'index.php' : $file;
$current_class = ( get_current_file() == $comp_file ) ? 'current-nav' : '';
/**
* Set classes if any
*/
if ( ! empty( $current_class ) || ! empty( $a_class ) ) {
$link_class = 'class="';
$link_class .= $current_class;
$link_class .= ( empty( $current_class) ) ? '' : ' ';
$link_class .= $a_class;
$link_class .= '"';
} else {
$link_class = '';
}
$output .= sprintf ( '<li><a %1$s href="%2$s">%3$s</a></li>', $link_class, get_site_url() . '/' . $file, $nav_item );
}
if ( $search_bar !== false ) {
$output .= '<li class="main-search-bar">
<form action="search-page.php" method="get">
<input type="search" name="search" placeholder="Search...">
<input class="search-button" type="submit" name="submit" value="">
</form>
</li>';
}
$output .= sprintf ( '</ul></nav><!-- .%s -->', $position );
echo $output;
}
/**
* Add a single image to an HTML page.
* Image will be wrapped inside a figure tag
*
* @param array $args multiple settings that will define the image element, parent and siblings
*
*/
function add_image ( $args ) {
/*
'path' => '',
'class' => '',
'img_attr' => array(),
'link' => '',
'link_title' => '',
'caption' => '',
'caption_attr' => array(),
'caption_pos' => 'bottom'
*/
$output = '';
// Default Variables
$i_vals = '';
$c_vals = '';
$caption_pos = ( isset( $arg['caption_pos'] ) && ! empty( $arg['caption_pos'] ) ) ? $arg['caption_pos'] : 'bottom';
// Set ID for image container
$id = ( !empty ( $args['id'] ) ) ? 'id="' . $args['id'] . '"' : '';
// Set Class for image container
$class = ( ! empty ( $args['class'] ) ) ? 'class="' . $args['class'] .'"' : '';
// setup the img attributes
if ( ! empty ( $args['img_attr'] ) ) {
foreach ( $args['img_attr'] as $i_attr => $i_val ) {
$i_vals .= ' ' . $i_attr . '="' . $i_val . '"';
}
}
// setup caption attributes
if ( ! empty ( $args['caption_attr'] ) ) {
foreach ( $args['caption_attr'] as $c_attr => $c_val ) {
$c_vals .= ' ' . $c_attr . '="' . $c_val . '"';
}
}
// set the link titles if any
$link_title = '';
if ( ! empty ( $args['link'] ) ) {
$link_title = str_replace( array( '-', '_' ), '', $args['link'] );
}
// print the whole set containing figure, img and figcation tags
$output .= sprintf ( '<figure %1$s %2$s>', $id, $class );
// top caption
if ( $caption_pos == 'top' ) {
if ( ! empty ( $args['caption'] ) ) {
$output .= sprintf ( '<figcaption %1$s><span>%2$s</span></caption>', $c_vals, $args['caption'] );
}
}
// Actual image tag
if ( ! empty ( $args['link'] ) ) {
$output .= sprintf ( '<a class="img-link" href="%3$s" title="%4$s"><img %1$s src="%2$s"/></a>', $i_vals, $args['path'], $args['link'], $link_title );
} else {
$output .= sprintf ( '<img %1$s src="%2$s"/>', $i_vals, $args['path'] );
}
// bottom caption
if ( $caption_pos == 'bottom' ) {
if ( ! empty ( $args['caption'] ) ) {
$output .= sprintf ( '<figcaption %1$s><span>%2$s</span></caption>', $c_vals, $args['caption'] );
}
}
$output .= sprintf ( '</figure>' );
echo $output;
}
/**
* Embed an SVG file into an html document.
* IMPORTANT: The framework is built to support modern browsers including IE9 and older
* There is no fallback for VSG since IE9 supports SVG pretty good
* @param string $file the relative or full path of the image
*/
function embed_svg ( $args ) {
$output = '';
$path = $args['path'];
$class = ( isset( $args['class'] ) || ! empty( $args['class'] ) ) ? 'class="' . $args['class'] . '"' : '';
$link = ( isset( $args['link'] ) || ! empty( $args['link'] ) ) ? $args['link'] : '';
// Get SVG file content
$svg_array = preg_grep( '/^\<\?xml/', file( $args['path'], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ), PREG_GREP_INVERT );
$svg_content = implode( '', $svg_array );
$output .= sprintf ( '<figure %s>', $class );
if ( ! empty( $link ) ) {
$output .= sprintf( '<a href="%s">', $link );
}
$output .= $svg_content;
if ( ! empty( $link ) ) {
$output .= '</a>';
}
$output .= '</figure>';
echo $output;
}
/**
* Creates a gallery from an especific folder in your image server's folder
*
* @param args array,
* @param int $limit the number of images to display, defaults to 10
* @param string $sort sort images in an especific order or random, defaults to ASC-name
*/
function create_gallery_from ( $args = array() ) {
$output = '';
// Set Counter, Variables, Patterns and Replaces
$c = 0;
$pattern = array( '/-thumb|(\.png|\.jpg|\.jpeg|\.gif|\.tiff|\.bmp|\.raw|\.jfif)/', '/-|_/', '/\&/' );
$replace = array( '', ' ', '<br/>' );
$image_ext = '/(\.png|\.jpg|\.jpeg|\.gif|\.tiff|\.bmp|\.raw|\.jfif)/';
$thumb_ext = '/-thumb(\.png|\.jpg|\.jpeg|\.gif|\.tiff|\.bmp|\.raw|\.jfif)/';
$scan_path = ( empty ( $args['folder'] ) || $args['folder'] == null ) ? dirname ( __FILE__ ) . '/images' : dirname ( __FILE__ ) . '/images/' . $args['folder'];
$img_path = ( empty ( $args['folder'] ) || $args['folder'] == null ) ? get_site_url() . '/images' : get_site_url() . '/images/' . $args['folder'];
$raw_images = array_diff( scandir ( $scan_path ), array( '.', '..' ) );
$limit = ( empty ( $args['limit'] ) || $args['limit'] == null ) ? count( $raw_images ) : $args['limit'];
$images = array();
// If $args['size'] is randome, randomize images array first
if ( $args['order'] == 'random' ) {
shuffle ( $raw_images );
}
// Filtering the array using size and limit arguments
foreach ( $raw_images as $type_img ) :
if ( $args['size'] == 'thumbnail' ) {
if ( preg_match ( $thumb_ext, $type_img ) ) {
if ( $c < $limit ) {
$images[$c] = $type_img;
$c++;
}
}
} else {
if ( preg_match ( $image_ext, $type_img ) ) {
if ( $c < $limit ) {
$images[$c] = $type_img;
$c++;
}
}
}
endforeach;
// Sort array of images
if ( $args['order'] != 0 ) {
arsort ( $images );
}
// Defined figcaption if true
if ( $args['caption'] == true ) {
$caption = '<figcaption class="caption-box">
<p class="caption-text">%s</p>
</figcaption>';
}
// Finally Print it out
foreach ( $images as $image ) :
printf ( '<figure class="%s">', $args['class'] );
if ( ! empty ( $args['before'] ) ) {
printf ( $args['before'] );
}
$image_name = preg_replace ( $pattern, $replace, $image );
// Top Caption
if ( $args['caption_pos'] == 'top' ) {
printf ( $caption, $image_name );
}
// actual image
printf ( '<img src="%1$s/%2$s" title="%3$s" alt="%3$s"/>', $img_path, $image, $image_name );
// Bottom Caption
if ( $args['caption_pos'] == 'bottom' ) {
printf ( $caption, $image_name );
}
if ( ! empty ( $args['after'] ) ) {
printf ( $args['before'] );
}
printf ( '</figure>' );
endforeach;
}
/**
* Adds a custom or predefined contact form.
* This form does not support drop down input tags such as option yet
* @param array $args Set of options to set the contact form
* email : Recipient email is mandatory
* class : Class of the contact form
* textarea_placeholder : Placeholder text of textarea
* defaults to "Type your message"
* g_captcha : activate google captcha, defaults to true
* captcha_options : is an array to set up options as per
* google reCaptcha docs:
* - data_sitekey
* - data_type
* - data_theme
* - data_callback
* fields : an array to set up the custom input fields
* if assoc array used, the keys will be used to
* set <label> tags, otherwise no <label> tags
* will be used
* textarea : activate textarea field, defaults to true
* submit_text : text shown submit button, defaults to "submit"
*
*
*/
function add_contact_form ( $args = array() ) {
global $root;
//Set default variables
$output = '';
$str_p = '';
$end_p = '';
$has_label = false; // Set <label> tags off by default
$form_class = ( isset ( $args['class'] ) && ! empty ( $args['class'] ) ) ? $args['class'] : '';
$txtarea_plcholder = ( isset ( $args['textarea_placeholder'] ) && ! empty ( $args['textarea_placeholder'] ) ) ? $args['textarea_placeholder'] : 'Type your message';
// Enable Google reCaptcha by default
$g_captcha = ( isset ( $args['g_captcha'] ) ) ? $args['g_captcha'] : true;
// Google reCaptcha options
$captcha_sitekey = ( ! empty( $args['captcha_options']['data_sitekey'] ) ) ? $args['captcha_options']['data_sitekey'] : 'YOUR SITE KEY';
$captcha_type = ( ! empty( $args['captcha_options']['data_type'] ) ) ? 'data-type="' . $args['captcha_options']['data_type'] . '"' : '';
$captcha_theme = ( ! empty( $args['captcha_options']['data_theme'] ) ) ? 'data-theme="' . $args['captcha_options']['data_theme'] . '"' : '';
$captcha_callback = ( ! empty( $args['captcha_options']['data_callback'] ) ) ? 'data-callback="' . $args['captcha_options']['data_callback'] . '"' : '';
// Get data for hidden fields used to check google recaptcha response
$hidden_fields = array(
'visitor_ip' => $_SERVER['REMOTE_ADDR'],
'visitor_useragent' => $_SERVER['HTTP_USER_AGENT']
);
// Default fields
$fields = ( isset ( $args['fields'] ) && is_array( $args['fields'] ) ) ? $args['fields'] : array (
array ( 'name' => 'name', 'type' => 'text', 'placeholder' => 'Name', 'required' => 'required' ),
array ( 'name' => 'email', 'type' => 'email', 'placeholder' => 'e-mail', 'required' => 'required' ),
array ( 'name' => 'subject', 'type' => 'text', 'placeholder' => 'Subject', 'required' => 'required' ),
);
// If form has been submitted include the message-script
if ( isset ( $_POST['submit'] ) ) {
include $root . '/message-script.php';
}
// Check if email address has been passed, if not advice the admin
if ( ! isset ( $args['email'] ) || empty ( $args['email'] ) ) {
$output = '<p>You have not declare an email, messages won't be sent</p>';
}
// if form has been submitted, store the message that was sent by the form
if ( ! empty ( $message ) ) {
$output .= sprintf ( $message, $_POST['name'] );
}
$output .= sprintf ( '<form class="mail-form %1$s" method="POST" action="%2$s" >', $form_class, get_requested_url() );
// Add all the input fields into the form
foreach ( $fields as $type => $field ) {
$str_p = ( $field['type'] == 'hidden') ? '' : '<p>';
$end_p = ( $field['type'] == 'hidden') ? '' : '</p>';
if ( is_int ( $type ) ) {
$output .= $str_p;
$output .= '<input';
foreach ( $field as $attr => $val ) {
$output .= sprintf ( ' %1$s="%2$s"', $attr, $val );
}
$output .= '/>';
$output .= $end_p;
} else {
$has_label = true;
$output .= $str_p;
$output .= sprintf ( '<label class="form-label">%s</label><input', $type );
foreach ( $field as $attr => $val ) {
$output .= sprintf ( ' %1$s="%2$s"', $attr, $val );
}
$output .= '/>';
$output .= $end_p;
}
}
// Include textarea if enabled
if ( ! isset ( $args['textarea'] ) || $args['textarea'] == true ) {
if ( $has_label ) {
$output .= sprintf( '<p><label>%s</label>', $txtarea_plcholder );
$txtarea_plcholder = '';
}
$output .= sprintf ( '<textarea name="message" placeholder="%s"></textarea>', $txtarea_plcholder );
if ( $has_label ) {
$output .= '</p>';
}
}
// Add hidden fields
foreach ($hidden_fields as $key => $value) {
$output .= sprintf( '<input name="%1$s" type="hidden" value="%2$s"/>', $key, $value );
}
// Enable Google no Captcha ReCaptcha
if ( $g_captcha ) {
$output .= sprintf( '<div class="g-recaptcha" data-sitekey="%1$s" %2$s %3$s %4$s></div>', $captcha_sitekey, $captcha_type, $captcha_theme, $captcha_callback );
}
$submit_txt = ( empty ( $args['submit_text'] ) ) ? 'Send Message' : $args['submit_text'];
$output .= sprintf ( '<input id="submit-form" name="submit" type="submit" value="%s"/>', $submit_txt );
$output .= '</form>';
echo $output;
}
/*************************************/
/* FILE HANDLER FUNCTIONS */
/*************************************/
/**
* Get Images File name and path and returns an array
* @param array $args folder: String, Path to get the images from
* sort: String, Sort images by Alpha-Num or Last-Mod.
* limit: Integer, Limit the array output.
* @return array $output Array of image files.
*/
function get_images_from ( $args = array() ) {
global $root;
$output;
$img_files = array();
// Folder defaults to images folder in your root directory.
$scan_path = ( isset( $args['folder'] ) && ! empty ( $args['folder'] ) ) ? $root . '/images/' . $args['folder'] : $root . '/images';
$rel_path = ( isset( $args['folder'] ) && ! empty ( $args['folder'] ) ) ? get_site_url() . '/images/' . $args['folder'] : get_site_url() . '/images';
$raw_files = array_diff( scandir ( $scan_path ), array( '.', '..' ) );
$sort = ( isset( $args['sort'] ) && ! empty( $args['sort'] ) ) ? $args['sort'] : 'LMOD_DESC';
if ( isset( $args['regex'] ) && ! empty( $args['regex'] ) ) {
$raw_files = preg_grep( $args['regex'] , $raw_files );
}
// Add last date modified to array if sorted by last modified date.
if ( $sort == 'LMOD_ASC' || $sort == 'LMOD_DESC' ) {
foreach ( $raw_files as $file ) {
$img_key = $rel_path . '/' . $file;
$img_files[$img_key] = filemtime( $scan_path . '/' . $file );
}
} else {
foreach ( $raw_files as $img_name ) {
$img_path = $rel_path . '/' . $img_name;
array_push( $img_files, $img_path );
}
}
// LMOD_ASC: Last Modified date descendant
// LMOD_DESC (Default): Last Modified date ascendant
// ASC: Name Numeric-Alpha Ascendant
// DESC: Name Numeric-Alpha Descendant
switch ( $sort ) {
case 'LMOD_ASC':
asort( $img_files );
$img_files = array_keys( $img_files );
break;
case 'ASC':
sort( $img_files );
break;
case 'DESC':
rsort( $img_files );
break;
default:
arsort( $img_files );
$img_files = array_keys( $img_files );
break;
}
// Get the files needed by limit
if ( isset( $args['limit'] ) && ! empty( $args['limit'] ) ) {
$output = array_slice( $img_files, 0, $args['limit']);
} else {
$output = $img_files;
}
return ( empty( $output ) ) ? false : $output;
}
/**
* Get Json File Content
* Returns an array of the json file content limited by the limit option;
*
* @param $args array Set of options to customize the output
*/
function get_json_content( $args = array() ) {
global $root;
// Set the absolute path of the file
$file = $root . '/' . $args['path'];
$output_obj = array();
$file_content = json_decode ( file_get_contents ( $file ), true );
// Return array contained by the json object or
// return the full object
if ( isset( $args['object'] ) && ! empty( $args['object'] ) ) {
$obj = $args['object'];
return $output_obj[$obj];
} else {
return $output_obj;
}
}
/*************************************/
/* ANALYTICS FUNCTIONS */
/*************************************/
/**
* Add Google Analytics
*/
function add_analytics ( $args = array() ) {
$ggl_ID = $args['google_ID'];
$ggl_domain = $args['site_domain'];
$analytics = '<script>
(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","//www.google-analytics.com/analytics.js","ga");
ga("create", "%1$s", "%2$s");
ga("send", "pageview");
</script>';
printf( $analytics, $ggl_ID, $ggl_domain );
}
/*****************************************/
/* SOCIAL MEDIA FUNCTIONS */
/*****************************************/
/**
* facebook url to get counts
* https://api.facebook.com/method/fql.query?query={select total_count,like_count,comment_count,share_count,click_count from link_stat where url='http//domain.com'}&format=json"
* Code to get counts
* $query = "select total_count,like_count,comment_count,share_count,click_count from link_stat where url='{$url}'";
* $call = "https://api.facebook.com/method/fql.query?query=" . rawurlencode($query) . "&format=json";
* $ch = curl_init();
* curl_setopt($ch, CURLOPT_URL, $call);
* curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
* $output = curl_exec($ch);
* curl_close($ch);
* return json_decode($output);
*/
/**
* Twitter URL to get counts for URL's
* http://urls.api.twitter.com/1/urls/count.json?url=yoururl.com
*/
/**
* Social Widgets Class
*/
function social_init( $args = array() ) {
// Facebook SDK Sccript
$fb_script = '<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/%1$s/sdk.js#xfbml=1&appId=%2$s&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "facebook-jssdk"));</script><!-- facebook script -->';
// Google Script
$gg_script = '<script type="text/javascript">