-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
4750 lines (4267 loc) · 170 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
/**
* @package FN_Extras
* @version 1.2.5
*/
define( 'FRIDAY_NEXT_EXTRAS_VERSION', '1.2.8' );
/*** Ensuring AJAX Requests use SSL ****/
add_filter( 'https_local_ssl_verify', '__return_true' );
/********************* ACF JSON *********************/
add_filter( 'acf/settings/save_json', 'my_acf_json_save_point' );
function my_acf_json_save_point( $path ) {
// update path
$path = dirname( __FILE__ ) . '/private/acf-json';
// return
return $path;
}
add_filter( 'acf/settings/load_json', 'my_acf_json_load_point' );
function my_acf_json_load_point( $paths ) {
// remove original path (optional)
unset( $paths[0] );
// append path
$paths[] = dirname( __FILE__ ) . '/private/acf-json';
// return
return $paths;
}
/* Remove admin bar for editors (Actually, EVERYONE - need to do a check later for only editors) */
if ( isset( wp_get_current_user()->ID ) && is_array( wp_get_current_user()->roles ) ) {
//check for admins
if ( in_array( 'vendor', wp_get_current_user()->roles ) || in_array( 'editor', wp_get_current_user()->roles ) ) {
// redirect them to the default place
show_admin_bar( false );
}
}
add_action( 'wp_print_styles', 'fn_enqueue_styles' );
add_action( 'wp_enqueue_scripts', 'fn_enqueue_scripts' );
/**
* Proper ob_end_flush() for all levels
*
* This replaces the WordPress `wp_ob_end_flush_all()` function
* with a replacement that doesn't cause PHP notices.
*/
remove_action( 'shutdown', 'wp_ob_end_flush_all', 1 );
add_action( 'shutdown', function () {
while ( @ob_end_flush() ) {
;
}
} );
/* Adding Google Maps API Key for ACF Form Fields */
function my_acf_init() {
acf_update_setting( 'google_api_key', 'AIzaSyA1SQ_pAK657gUt1SrStTNFIuIiwgf5I3w' );
}
add_action( 'acf/init', 'my_acf_init' );
function fn_enqueue_styles() {
wp_register_style( 'fn_default_styles', plugins_url( 'public/css/default-min.css', __FILE__ ), array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'fn_default_styles' );
wp_register_style( 'swiper_style', 'https://unpkg.com/swiper/swiper-bundle.min.css', array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'swiper_style' );
wp_register_style( 'header_style', plugins_url( 'public/css/header-min.css', __FILE__ ), array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'header_style' );
wp_register_style( 'footer_style', plugins_url( 'public/css/footer.css', __FILE__ ), array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'footer_style' );
wp_register_style( 'archive_style', plugins_url( 'public/css/archive-min.css', __FILE__ ), array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'archive_style' );
wp_register_style( 'jquery-ui-style', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css', array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'jquery-ui-style' );
// For jQuery Tables on Admin pages
// TODO: Do a check for what page we're on, and only enqueue these if we're in the admin section!!!
wp_register_style( 'datatables_style', '//cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css', array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_register_style( 'datatables_buttons_style', '//cdn.datatables.net/buttons/1.6.2/css/buttons.dataTables.min.css', array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_register_style( 'datatables_custom', plugins_url( 'public/css/table.css', __FILE__ ), array(
'datatables_style',
'datatables_buttons_style'
), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'datatables_custom' );
wp_register_style( 'admin-styles', plugins_url( 'public/css/admin.css', __FILE__ ), array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'admin-styles' );
wp_register_style( 'client-admin-styles', plugins_url( 'public/css/client-admin-min.css', __FILE__ ), array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'client-admin-styles' );
// TODO: Only render these styles for the article pages!
wp_register_style( 'article_styles', plugins_url( 'public/css/article-min.css', __FILE__ ), array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'article_styles' );
wp_register_style( 'open_sans_light', '//fonts.googleapis.com/css2?family=Encode+Sans+Condensed:wght@100;300;700&family=Encode+Sans:wght@100;300;700&family=Open+Sans+Condensed:wght@700&display=swap', array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'open_sans_light' );
// 'Vendor List' Page Style
if ( get_post_field( 'post_name', get_post() ) == 'vendor-list' ) {
wp_register_style( 'vendor_list_style', plugins_url( 'public/css/vendor-style.css', __FILE__ ), array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'vendor_list_style' );
}
// 'Vendor Profile' Styles
if ( get_post_type() == 'vendor_profile' ) {
wp_register_style( 'vendor_profile_style', plugins_url( 'public/css/vendor-profile.css', __FILE__ ), array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'vendor_profile_style' );
}
// Homepage
if ( is_page( 'home' ) ) {
wp_register_style( 'homepage_style', plugins_url( 'public/css/homepage.css', __FILE__ ), array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'homepage_style' );
}
wp_register_style( 'jpopup', plugins_url( 'public/css/jpopup.min.css', __FILE__ ), array(), FRIDAY_NEXT_EXTRAS_VERSION );
wp_enqueue_style( 'jpopup' );
}
//********************************* WP_ENQUEUE_SCRIPTS *******************************//
function fn_enqueue_scripts() {
// Scripts
// wp_register_script( 'facebook_share', 'https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v7.0', array(), null, true );
// wp_enqueue_script( 'facebook_share' );
wp_register_script( 'pinterest_share', '//assets.pinterest.com/js/pinit.js', array(), null, true );
// wp_enqueue_script( 'pinterest_share' );
// For jQuery Tables on Admin pages
// TODO: Do a check for what page we're on, and only enqueue these if we're in the admin section!!!
wp_register_script( 'datatables_script', '//cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js', array(), null, true );
wp_register_script( 'datatables_buttons_script', '//cdn.datatables.net/buttons/1.6.2/js/dataTables.buttons.min.js', array(), null, true );
wp_localize_script( 'datatables_script', 'datatablesajax', array( 'url' => admin_url( 'admin-ajax.php?action=article_datatables' ) ) );
wp_enqueue_script( 'datatables_script' );
wp_enqueue_script( 'datatables_buttons_script' );
wp_register_script( 'jpopup_modal', plugins_url( 'public/js/jpopup.min.js', __FILE__ ) );
// Just for the Vendor Profile Page (save bandwidth elsewhere)
if ( get_post_type() == 'vendor_profile' || is_page( 'client-admin' ) ) {
wp_register_script( 'swiper_slider', '//unpkg.com/swiper/swiper-bundle.min.js', array(), null, false );
wp_register_script( 'popper', '//unpkg.com/@popperjs/core@2', array(), null, true );
wp_register_script( 'micromodal', plugins_url( 'public/js/micromodal.min.js', __FILE__ ), array(), null, true );
wp_register_script( 'sticky_bits', plugins_url( 'public/js/jquery.stickybits.min.js', __FILE__ ), array(
'swiper_slider',
'micromodal',
'jquery-ui-tabs',
'popper'
), FRIDAY_NEXT_EXTRAS_VERSION, true );
wp_enqueue_script( 'sticky_bits' );
}
// For all archive pages
if ( is_archive() || is_home() || is_front_page() ) {
wp_register_script( 'swiper_slider', '//unpkg.com/swiper/swiper-bundle.min.js' );
wp_enqueue_script( 'swiper_slider' );
}
wp_register_script( 'fn_scripts', plugins_url( 'public/js/scripts.js', __FILE__ ), array(
'jquery',
'jpopup_modal',
// 'facebook_share',
'pinterest_share',
'jquery-ui-core',
'jquery-ui-tabs',
'datatables_script',
'datatables_buttons_script'
), FRIDAY_NEXT_EXTRAS_VERSION, true );
$fn_nonce = wp_create_nonce( 'fn_noncy' );
wp_localize_script( 'fn_scripts', 'fnajax', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => $fn_nonce,
) );
wp_enqueue_script( 'fn_scripts' );
}
add_action( 'get_header', 'acf_reqs' );
function acf_reqs() {
// TODO: Check to see if admin page, then add this!
acf_form_head();
}
/**
* Enable unfiltered_html capability for Editors.
*/
function allow_editors_to_html( $allow_unfiltered_html ) {
return true;
}
add_filter( 'acf/allow_unfiltered_html', 'allow_editors_to_html' );
// Login Redirect
function my_login_redirect( $redirect_to, $request, $user ) {
//is there a user to check?
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
//check for admins
if ( in_array( 'administrator', $user->roles ) ) {
// redirect them to the default place
return $redirect_to;
} elseif ( in_array( 'editor', $user->roles ) ) {
// Redirect editors to the saw-admin section!
return get_page_link( 6433 );
} elseif ( in_array( 'vendor', $user->roles ) ) {
// Redirect vendors to the vendor admin section!
// TODO: RETURN VENDORS TO THEIR VENDOR ADMIN PAGE!!!!!
$profile_args = array(
'post_type' => 'vendor_profile',
'meta_key' => 'linked_user_account_user',
'meta_value' => $user->ID,
'posts_per_page' => 1 // stop at the first match
);
$matching_profile = get_posts( $profile_args );
if ( sizeof( $matching_profile ) > 0 ) {
// Set the session variable so we don't have to do ridiculous URL shenanigans
$_SESSION['vendor'] = $matching_profile[0]->ID;
// we found a match, so let's send them to their profile page
return home_url( '/client-admin?ven_id=' . $matching_profile[0]->ID );
}
return home_url();
} else {
// logged in user that is not admin, editor, or vendor
return home_url();
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
// Custom Login Logo
function saw_login_logo() { ?>
<style type="text/css">
#login h1 a, .login h1 a {
background-image: url(<?php echo plugins_url('public/img/saw-login-logo.png', __FILE__); ?>);
height: 65px;
width: 320px;
background-size: 320px 65px;
background-repeat: no-repeat;
padding-bottom: 30px;
}
</style>
<?php }
add_action( 'login_enqueue_scripts', 'saw_login_logo' );
// add async / defer to facebook script
function add_async_attribute( $tag, $handle ) {
if ( ( 'facebook_share' == $handle ) || ( 'pinterest_share' == $handle ) ) {
return str_replace( ' src', ' async="async" defer="defer" crossorigin="anonymous" src', $tag );
}
return $tag;
}
add_filter( 'script_loader_tag', 'add_async_attribute', 10, 2 );
// Vendor Profile Sidebar
function vendor_profile_sidebar() {
register_sidebar(
array(
'name' => __( 'Vendor Profile', 'fn_extras' ),
'id' => 'vendor-profile-sidebar',
'description' => __( 'Vendor Profile Sidebar', 'fn_extras' ),
'before_widget' => '<div class="vendor-profile-sidebar-content">',
'after_widget' => "</div>",
'before_title' => '<div class="vendor-profile-sidebar-title">',
'after_title' => '</div>',
)
);
}
add_action( 'widgets_init', 'vendor_profile_sidebar' );
/* Create Vendor User Role */
add_role(
'vendor', // System name of the role.
__( 'Vendor' ), // Display name of the role.
array(
'read' => true,
'delete_posts' => true,
'delete_published_posts' => true,
'edit_posts' => true,
'publish_posts' => true,
'upload_files' => true,
'edit_pages' => true,
'edit_published_pages' => true,
'publish_pages' => true,
'delete_published_pages' => false, // This user will NOT be able to delete published pages.
)
);
add_filter( 'et_project_posttype_args', 'mytheme_et_project_posttype_args', 10, 1 );
function mytheme_et_project_posttype_args( $args ) {
return array_merge( $args, array(
'public' => false,
'exclude_from_search' => false,
'publicly_queryable' => false,
'show_in_nav_menus' => false,
'show_ui' => false
) );
}
// ***** Add Vendor Column to Special Offers CPT Page ******
add_filter( 'manage_special_offers_posts_columns', 'set_custom_edit_special_offers_columns' );
function set_custom_edit_special_offers_columns( $columns ) {
// Add expiration date, Vendor to columns
$columns['vendor'] = __( 'Vendor', 'fn_extras' );
$columns['expiration'] = __( 'Expires', 'fn_extras' );
return $columns;
}
// ***** Get text for Vendor and Expires date columns *****
add_action( 'manage_special_offers_posts_custom_column', 'special_offers_columns', 10, 2 );
function special_offers_columns( $column, $post_id ) {
switch ( $column ) {
case 'vendor':
$vendor_profile_obj = get_field( 'vendor', $post_id );
$vendor_name = get_the_title( $vendor_profile_obj );
if ( is_string( $vendor_name ) ) {
echo '<a href="' . get_edit_post_link( $vendor_profile_obj ) . '">' . $vendor_name . '</a>';
} else {
echo "No Vendor Selected";
}
break;
case 'expiration':
$offer_exp = get_field( "offer_end_date" );
if ( $offer_exp == null ) {
echo "Permanent";
} else {
echo $offer_exp;
}
break;
}
}
// ****** Make the columns sortable ****** //
add_filter( 'manage_edit-special_offers_sortable_columns', 'saw_sortable_offers' );
function saw_sortable_offers( $columns ) {
$columns['vendor'] = 'vendor';
return $columns;
}
// Add custom meta box with Special Offer items in Vendor profile
add_action( 'add_meta_boxes_vendor_profile', 'add_special_offers_box' );
function add_special_offers_box( $post ) {
add_meta_box( 'special-offer-box', 'Special Offers', 'special_offer_format', 'vendor_profile', 'side', 'default' );
}
function special_offer_format( $post, $args ) {
$vendor_id = $post->ID;
// store matching vendor
$matching_offers = array();
// go find all special offers with this vendor as their matching vendor
$args = array(
'post_type' => 'special_offers',
'posts_per_page' => - 1
);
$all_offers = get_posts( $args );
foreach ( $all_offers as $offer ) {
$this_offer_id = $offer->ID;
// get custom field with Vendor from Special Offers CPT
$this_vendor = get_field( "vendor", $offer->ID );
if ( $this_vendor->ID == $vendor_id ) {
$matching_offers[] = $this_offer_id;
}
}
echo '<p class="special-offers" style="line-height:2.3em;">';
if ( ! empty( $matching_offers ) ) {
// Loop through the Special Offers, and print out links to them
foreach ( $matching_offers as $offer_id ) {
echo '<a href="' . get_edit_post_link( $offer_id ) . '" target="_blank">' . get_the_title( $offer_id ) . '</a><br>';
}
}
}
// TODO: Change this to be for the profile page
add_filter( 'single_template', 'vendor_profile_template' );
function vendor_profile_template( $single_template ) {
global $post;
if ( $post->post_type == 'vendor_profile' ) {
if ( file_exists( plugin_dir_path( __FILE__ ) . 'templates/single-vendor_profile.php' ) ) {
return plugin_dir_path( __FILE__ ) . 'templates/single-vendor_profile.php';
}
}
return $single_template;
}
// Could use this to populate archive pages - will see!
// [vendors]
function vendors_func( $atts ) {
// Get all vendors and list here
// Get a count, divide by four, and use count(vendors) / 4, per column
// $vendor_args = array(
// 'post_type' => 'vendor',
// 'posts_per_page' => -1,
// 'post_status' => 'publish',
// 'orderby' => 'title'
// );
// $vendors = query_posts( $vendor_args );
//
// $num_vendors = count( $vendors );
// $num_per_column = float;
// $num_per_column = $num_vendors / 4;
// $num_per_column = round( $num_per_column );
//
// // Print Info
// $vendor_html = '<div class="wf-column><ul>';
// foreach ($vendors as $key => $vendor) {
// $website = get_field( 'website', $vendor->ID );
// $phone_number = get_field( 'phone_number', $vendor->ID );
//
// $vendor_html .= '<li><a href="' . $website . '" target="_blank">' . $vendor->post_title . '</a>
// ' . $phone_number . '</li>';
// if( ($num_vendors % $num_per_column == 0) && ($num_vendors / $num_per_column !== 4) ) {
// $vendor_html .= '</ul></div><div class="wf-column"><ul>';
// }
// }
// $vendor_html .= '</ul></div>';
// return $vendor_html;
}
// add_shortcode( 'vendors', 'vendors_func' );
function photographer_func( $atts ) {
if ( isset( $_GET['ssid'] ) ) {
$post_id = $_GET['ssid'];
} else {
$post_id = get_the_ID();
}
if ( true == get_field( 'non-existing', $post_id ) ) {
if ( get_field( 'photographer-manual', $post_id ) ) {
// non-existing vendor, with a manual name
$return_string = 'Photography by: ' . get_field( 'photographer-manual', $post_id );
if ( get_field( 'author', $post_id ) ) {
$auth_pres = true;
$return_string .= ' | Written by: ' . get_field( 'author', $post_id );
}
return $return_string;
} else if ( get_field( 'author', $post_id ) ) {
// no photographer, maybe an author, though!
return 'Written by: ' . get_field( 'author', $post_id );
} else {
return '';
}
} else {
if ( get_field( 'photographer', $post_id ) ) {
// Got a Vendor - now check for URL
$photographer = get_field( 'photographer', $post_id );
if ( get_field( 'website', $photographer->ID ) ) {
$return_string = 'Photography by: <a href="' . get_permalink( $photographer->ID ) . '" alt="' . get_the_title( $photographer->ID ) . '" style="color:inherit;" target="_blank">' . get_the_title( $photographer->ID ) . '</a>';
} else {
// Vendor doesn't have a website URL in their profile yet
$return_string = 'Photography by: ' . get_field( 'photographer', $post_id )->title;
}
if ( get_field( 'author', $post_id ) ) {
$return_string .= ' | Written by: ' . get_field( 'author', $post_id );
}
return $return_string;
} else {
// There's only an author maybe? let's check that
if ( get_field( 'author', $post_id ) ) {
$return_string = 'Written by: ' . get_field( 'author', $post_id );
return $return_string;
}
return '';
}
}
}
add_shortcode( 'photographer', 'photographer_func' );
/************************* AJAX FOR DATATABLES *********************************/
// Add Post title to custom meta
add_action( 'transition_post_status', 'duplicate_title', 10, 3 );
function duplicate_title( $new, $old, $post ) {
if ( $post->post_type == 'post' || $post->post_type == 'spotlight' || $post->post_type == 'styled_shoot' || $post->post_type == 'wedding_story' ) {
update_post_meta( $post->ID, 'd_title', $post->post_title );
}
}
add_action( 'wp_ajax_article_datatables', 'my_ajax_getpostsfordatatables' );
add_action( 'wp_ajax_nopriv_article_datatables', 'my_ajax_getpostsfordatatables' );
function my_ajax_getpostsfordatatables() {
header( "Content-Type: application/json" );
$request = $_GET;
$columns = array(
0 => 'featuredImage',
1 => 'type',
2 => 'title',
3 => 'author',
4 => 'clickCountLastClicked',
5 => 'viewCountLastViewed',
6 => 'postDate',
7 => 'isActive',
8 => 'action'
);
$args = array(
'post_type' => array( 'spotlight', 'styled_shoot', 'wedding_story', 'post' ),
'posts_per_page' => $request['length'],
'offset' => $request['start'],
'order' => $request['order'][0]['dir']
);
if ( $request['order'][0]['column'] == 1 || $request['order'][0]['column'] == 2 || $request['order'][0]['column'] == 3 || $request['order'][0]['column'] == 5 ) {
$args['orderby'] = $columns[ $request['order'][0]['column'] ];
} elseif ( $request['order'][0]['column'] == 4 ) {
$args['orderby'] = 'date';
} else {
$args['orderby'] = 'date';
$args['order'] = 'DESC';
}
//$request['search']['value'] <= Value from search
if ( ! empty( $request['search']['value'] ) ) { // When datatables search is used
$args['s'] = $request['search']['value'];
}
$article_query = new WP_Query( $args );
$totalData = $article_query->found_posts;
if ( $article_query->have_posts() ) {
$data = array();
while ( $article_query->have_posts() ) {
$article_query->the_post();
$active_class = 'deactivate-post';
$active_text = 'Deactivate';
if ( ! get_field( 'is_active' ) ) {
$active_class = 'activate-post';
$active_text = 'Activate';
}
$image = get_the_post_thumbnail( get_the_ID(), array( 90, 90 ) );
$nestedData = array();
$nestedData[] = $image;
$nestedData[] = get_post_type();
$nestedData[] = '<a href="' . get_the_permalink() . '" alt="' . get_the_title() . '" target="_blank">' . get_the_title() . '</a>';
$nestedData[] = get_field( 'author' );
$nestedData[] = '<strong>' . get_field( 'banner_click_count' ) . '</strong><br />' . ( get_field( 'last_clicked' ) ? get_field( 'last_clicked' ) : 'N.A.' );
$nestedData[] = '<strong>' . get_field( 'view_count' ) . '</strong><br />' . ( get_field( 'last_viewed' ) ? get_field( 'last_viewed' ) : 'N.A.' );
$nestedData[] = get_the_date( "m/d/Y" ) . '<br>' . get_the_date( "g:i A" );
$nestedData[] = ( get_field( 'is_active' ) ? "Yes" : "No" );
$nestedData[] = '<div class="vmenu-container">
<button class="vmenu-button" type="button">
<i class="fas fa-chevron-down"></i>
</button>
<ul class="vmenu-dropdown">
<li><a href="/saw-admin/edit-article?a_id=' . get_the_ID() . '">Edit</a></li>
<li><a href="#" class="' . $active_class . '" id="' . get_the_ID() . '">' . $active_text . '</a></li>
<li><a href="' . get_delete_post_link() . '" alt="Delete this Article">Delete</a></li>
</ul>
</div>';
$data[] = $nestedData;
}
wp_reset_query();
$json_data = array(
"draw" => intval( $request['draw'] ),
"recordsTotal" => intval( $totalData ),
"recordsFiltered" => intval( $totalData ),
"data" => $data
);
echo json_encode( $json_data );
} else {
$json_data = array(
"data" => array()
);
echo json_encode( $json_data );
}
wp_die();
}
add_action( 'wp_ajax_vendor_datatables', 'render_vendors' );
add_action( 'wp_ajax_nopriv_vendor_datatables', 'render_vendors' );
function render_vendors() {
header( "Content-Type: application/json" );
$request = $_GET;
$columns = array(
0 => 'companyName',
1 => 'categories',
2 => 'url_slug',
3 => 'group',
4 => 'localFavesClickCountLastClicked',
5 => 'profilePageViewCountLastViewed',
6 => 'premium',
7 => 'isActive',
8 => 'action'
);
$args = array(
'post_type' => 'vendor_profile',
'posts_per_page' => $request['length'],
'offset' => $request['start'],
'order' => $request['order'][0]['dir']
);
if ( $request['order'][0]['column'] == 1 || $request['order'][0]['column'] == 3 || $request['order'][0]['column'] == 5 ) {
$args['orderby'] = $columns[ $request['order'][0]['column'] ];
} elseif ( $request['order'][0]['column'] == 4 ) {
$args['orderby'] = 'date';
} else {
$args['orderby'] = 'title';
}
//$request['search']['value'] <= Value from search
if ( ! empty( $request['search']['value'] ) ) { // When datatables search is used
$args['s'] = $request['search']['value'];
}
$vendor_query = new WP_Query( $args );
$totalData = $vendor_query->found_posts;
if ( $vendor_query->have_posts() ) {
$data = array();
while ( $vendor_query->have_posts() ) {
$vendor_query->the_post();
$active_class = 'deactivate-post';
$active_text = 'Deactivate';
if ( ! get_field( 'is_active' ) ) {
$active_class = 'activate-post';
$active_text = 'Activate';
}
$nestedData = array();
$nestedData[] = '<a href="' . get_the_permalink() . '" alt="' . get_the_title() . '" target="_blank">' . get_the_title() . '</a>';
$cats = get_field( 'field_5ef380bcbbd1b' );
$cat_array = array();
if ( $cats ) {
foreach ( $cats as $cat ) {
if ( $cat['category'] != '' ) {
$category = get_term( $cat['category'] );
$cat_array[] = $category->name;
}
}
}
$nestedData[] = join( ', ', $cat_array );
$nestedData[] = get_post_field( 'post_name' );
$group_text = '';
if ( get_field( 'group' ) ) {
$group = get_field( 'group' );
$group_text = esc_html( $group->name );
}
$nestedData[] = $group_text; // TODO: filter out individual taxonomy
$nestedData[] = get_field( 'local_faves_click_count' ) . '<br \>' . get_field( 'local_faves_last_viewed' ); // Local Faves Last Clicked
$nestedData[] = get_field( 'profile_page_view_count' ) . '<br \>' . get_field( 'profile_page_last_viewed' ); // Profile Page Views and Last Viewed
$premium_listings = get_field( 'premium_listings' );
if ( $premium_listings ) {
$nestedData[] = $premium_listings[0]['level'];
} else {
$nestedData[] = '';
}
$nestedData[] = ( get_field( 'is_active' ) ? "Yes" : "No" );
$nestedData[] = '<div class="vmenu-container">
<button class="vmenu-button" type="button">
<i class="fas fa-chevron-down"></i>
</button>
<ul class="vmenu-dropdown">
<li><a href="/saw-admin/edit-advertiser?ven_id=' . get_the_ID() . '">Edit</a></li>
<li><a href="#" class="' . $active_class . '" id="' . get_the_ID() . '">' . $active_text . '</a></li>
<li><a href="' . get_delete_post_link() . '" alt="Delete this Vendor">Delete</a></li>
</ul>
</div>';
$data[] = $nestedData;
}
wp_reset_query();
$json_data = array(
"draw" => intval( $request['draw'] ),
"recordsTotal" => intval( $totalData ),
"recordsFiltered" => intval( $totalData ),
"data" => $data
);
echo json_encode( $json_data );
} else {
$json_data = array(
"data" => array()
);
echo json_encode( $json_data );
}
wp_die();
}
add_action( 'wp_ajax_deactivate_post', 'my_ajax_deactivate_post' );
function my_ajax_deactivate_post() {
$post_id = intval( $_POST['article_id'] );
// this isn't a post. let's check to see if it's a category
if ( get_term( $post_id, 'category' ) ) {
// it's a category
if ( get_term_meta( $post_id, 'is_active', true ) ) {
// it's active
update_term_meta( $post_id, 'is_active', false );
$resp = array(
'title' => 'Activation Status',
'content' => 'Category was deactivated!',
'post_type' => 'category'
);
wp_send_json( $resp );
wp_die();
}
} else {
// change the 'isActive' custom field to 'No' (aka false)
if ( get_field( 'is_active', $post_id ) ) {
update_field( 'is_active', false, $post_id );
}
$resp = array(
'title' => 'Activation Status',
'content' => 'Post was deactivated!',
'post_type' => get_post_type( $post_id )
);
wp_send_json( $resp );
wp_die();
}
}
add_action( 'wp_ajax_activate_post', 'my_ajax_activate_post' );
function my_ajax_activate_post() {
$post_id = intval( $_POST['article_id'] );
// Let's check to see if it's a category
if ( get_term( $post_id, 'category' ) ) {
// it's a category
if ( ! get_term_meta( $post_id, 'is_active', true ) ) {
// it's not active
update_term_meta( $post_id, 'is_active', true );
$resp = array(
'title' => 'Activation Status',
'content' => 'Category was Activated!',
'post_type' => 'category'
);
wp_send_json( $resp );
wp_die();
}
} else {
// change the 'isActive' custom field to 'Yes' (aka true)
update_field( 'is_active', true, $post_id );
$resp = array(
'title' => 'Activation Status',
'content' => 'Post was deactivated!',
'post_type' => get_post_type( $post_id )
);
wp_send_json( $resp );
wp_die();
}
}
/* Get Post Type for Javascript */
add_action( 'wp_ajax_get_post_type', 'my_ajax_get_post_type' );
function my_ajax_get_post_type() {
if ( in_array( get_post_type(), [ 'wedding_story', 'styled_shoot', 'post', 'spotlight' ] ) ) {
$resp = array(
'title' => 'Post Type',
'content' => 'Found Article',
'post_type' => get_post_type()
);
wp_send_json( $resp );
wp_die();
} else {
$resp = array(
'title' => 'Post Type',
'content' => 'Not Wedding Story',
'post_type' => get_the_ID()
);
wp_send_json( $resp );
wp_die();
}
}
/* Vendors Table Shortcode */
function vendor_table_func( $atts ) {
$vtable = '<table id="vendor_table" class="dataTable compact display" data-page-length="30">
<thead>
<tr>
<th>Company Name</th>
<th>Categories</th>
<th>URL Permalink</th>
<th>Group</th>
<th>Local Faves<br>Click Count &<br>Last Clicked</th>
<th>Profile Page<br>View Count &<br>Last Viewed</th>
<th>Premium Level</th>
<th>Is Active?</th>
<th>Action</th>
</tr>
</thead>
</table>';
return $vtable;
}
add_shortcode( 'vendors_table', 'vendor_table_func' );
function article_table_func( $atts ) {
return '<table id="article_table" class="dataTable compact display" data-page-length="30">
<thead>
<tr>
<th>Thumbnail</th>
<th>Type</th>
<th>Title</th>
<th>Author</th>
<th>Banners & Links<br />Click Count &<br />Last Clicked</th>
<th>Article Page<br />View Count &<br />Last Viewed</th>
<th>Post Date</th>
<th>Is Active?</th>
<th>Action</th>
</tr>
</thead>
</table>';
}
add_shortcode( 'articles_table', 'article_table_func' );
function category_table_func( $atts ) {
$table = '<table id="category_table" class="dataTable compact display responsive" data-page-length="30">
<thead>
<tr>
<th>Name</th>
<th>URL slug</th>
<th>Keywords</th>
<th>Description</th>
<th>Is Active?</th>
<th>Action</th>
</tr>
</thead>
</table>';
return $table;
}
add_shortcode( 'categories_table', 'category_table_func' );
add_action( 'wp_ajax_category_datatables', 'render_categories' );
add_action( 'wp_ajax_nopriv_category_datatables', 'render_categories' );
function render_categories() {
header( "Content-Type: application/json" );
$request = $_GET;
$columns = array(
0 => 'categoryName',
1 => 'slug',
2 => 'keywords',
3 => 'description',
4 => 'isActive',
5 => 'action'
);
$args = array(
'hide_empty' => 0,
'order' => $request['order'][0]['dir']
);
if ( $request['order'][0]['column'] == 0 || $request['order'][0]['column'] == 1 || $request['order'][0]['column'] == 4 ) {
$args['orderby'] = $columns[ $request['order'][0]['column'] ];
} else {
$args['orderby'] = 'title';
}
//$request['search']['value'] <= Value from search
if ( ! empty( $request['search']['value'] ) ) { // When datatables search is used
$args['search'] = $request['search']['value'];
}
// grab all categories to display on the table
$categories = get_categories( $args );
$totalData = count( $categories );
$data = array(); // will store all nested data
// check for offset and post limit
$num_cats = $request['length'];
$offset = $request['start'];
for ( $i = ( 0 + $offset ); $i < ( $num_cats + $offset ); $i ++ ) {
if ( $i >= $totalData ) {
break;
} // don't keep going if we've reached the end
$category = $categories[ $i ];
$nested_data = array();
$cid = $category->term_id;
$is_active = get_term_meta( $cid, 'is_active', true );
$nested_data[] = $category->name;
$nested_data[] = $category->slug;
$keyword_string = '';
$keywords = get_term_meta( $cid, 'meta_keywords', true );
if ( count( $keywords ) > 0 ) {
$keyword_arr = array();
foreach ( $keywords as $keyword ) {
$this_tag = get_tag( $keyword );
$keyword_arr[] = $this_tag->name;
}
$keyword_string = join( ', ', $keyword_arr );
}
$nested_data[] = $keyword_string;
$nested_data[] = $category->description;
$nested_data[] = ( $is_active ? "Yes" : "No" );
$nested_data[] = '<div class="vmenu-container">
<button class="vmenu-button" type="button">
<i class="fas fa-chevron-down"></i>
</button>
<ul class="vmenu-dropdown">
<li><a href="/saw-admin/edit-category?cid=' . $cid . '">Edit</a></li>
<li><a href="#" class="' . ( $is_active ? "deactivate-post" : "activate-post" ) . '" id="' . $cid . '">' . ( $is_active ? "Deactivate" : "Activate" ) . '</a></li>
<li><a href="#">Delete</a></li>
</ul>
</div>';
$data[] = $nested_data;
}
$json_data = array(
"draw" => intval( $request['draw'] ),
"recordsTotal" => $totalData,
"recordsFiltered" => $totalData,
"data" => $data
);
echo json_encode( $json_data );
wp_die();
}
function vendor_admin_form_func( $atts ) {
if ( isset( $_GET['ven_id'] ) ) {
$vendor_id = $_GET['ven_id'];
$args = array(
'post_id' => $vendor_id,
'post_title' => true,
'updated_message' => 'Advertiser successfully updated!',
'instruction_placement' => 'field',
'kses' => false
);
$html = '<h2>' . get_the_title( $vendor_id ) . '</h1>';
ob_start();
acf_form( $args );
$html .= ob_get_contents();
ob_end_clean();
return $html;
} else {
//Handle the case where there is no parameter
return 'No advertiser selected. Head back to the <a href="/saw-admin/advertisers" alt="SAW Advertisers">Advertisers table</a> and try again!';
}
}
add_shortcode( 'vendor_edit_form', 'vendor_admin_form_func' );
/* Change Advertiser Title label to 'Company Name' */
function change_post_title_name( $field ) {
if ( is_page( array( 'add-advertiser', 'advertisers', 'edit-advertiser' ) ) ) { // if on the vendor page
$field['label'] = 'Company Name';
} elseif ( is_page( array( 'add-article', 'articles', 'edit-article' ) ) ) {
$field['label'] = 'URL Title';
} elseif ( is_page( array( 'add-special-offer', 'special-offers', 'edit-special-offer' ) ) ) {
$field['label'] = 'Special Offer Title';
}
return $field;
}
add_filter( 'acf/load_field/name=_post_title', 'change_post_title_name' );
function vendor_admin_add_form_func( $atts ) {
$args = array(
'post_id' => 'new_post',
'post_title' => true,
'new_post' => array(
'post_type' => 'vendor_profile',
'post_status' => 'publish'
),
'submit_value' => 'Create new Advertiser',