forked from xserver-inc/cocoon
/
utils.php
1751 lines (1527 loc) · 51.7 KB
/
utils.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
//カテゴリーラベルのスタイルタグ属性を取得
if ( !function_exists( 'get_category_label_style_attr' ) ):
function get_category_label_style_attr($cat_id){
$color = get_category_color($cat_id);
if ($color) {
return ' style="background-color: '.$color.';border-color: '.$color.';"';
}
}
endif;
//リンクのないカテゴリーの取得(複数)
if ( !function_exists( 'get_the_nolink_categories' ) ):
function get_the_nolink_categories(){
$categories = null;
foreach((get_the_category()) as $category){
$categories .= '<span class="entry-category">'.$category->cat_name.'</span>';
}
return $categories;
}
endif;
//リンクのないカテゴリーの出力(複数)
if ( !function_exists( 'the_nolink_categories' ) ):
function the_nolink_categories(){
echo get_the_nolink_categories();
}
endif;
//カテゴリリンクの取得
if ( !function_exists( 'get_the_category_links' ) ):
function get_the_category_links(){
$categories = null;
foreach((get_the_category()) as $category){
$style = null;//get_category_label_style_attr($category->cat_ID);
$categories .= '<a class="cat-link cat-link-'.$category->cat_ID.'" href="'.get_category_link( $category->cat_ID ).'"'.$style.'>'.$category->cat_name.'</a>';
}
return $categories;
}
endif;
//カテゴリリンクの出力
if ( !function_exists( 'the_category_links' ) ):
function the_category_links(){
echo get_the_category_links();
}
endif;
//リンクのないカテゴリーの取得
if ( !function_exists( 'get_the_nolink_category' ) ):
function get_the_nolink_category(){
$categories = get_the_category();
//var_dump($categories);
if ( isset($categories[0]) ) {
$category = $categories[0];
return '<span class="cat-label cat-label-'.$category->cat_ID.'">'.$category->cat_name.'</span>';
}
}
endif;
//リンクのないカテゴリーの出力
if ( !function_exists( 'the_nolink_category' ) ):
function the_nolink_category(){
echo get_the_nolink_category();
}
endif;
//タグリンクの取得
if ( !function_exists( 'get_the_tag_links' ) ):
function get_the_tag_links(){
$tags = null;
$posttags = get_the_tags();
if ( $posttags ) {
foreach(get_the_tags() as $tag){
$tags .= '<a class="tag-link tag-link-'.$tag->term_id.'" href="'.get_tag_link( $tag->term_id ).'">'.$tag->name.'</a>';
}
}
return $tags;
}
endif;
//タグリンクの出力
if ( !function_exists( 'the_tag_links' ) ):
function the_tag_links(){
echo get_the_tag_links();
}
endif;
//コメントが許可されているか
if ( !function_exists( 'is_comment_allow' ) ):
function is_comment_allow(){
global $post;
if ( isset($post->comment_status) ) {
return $post->comment_status == 'open';
}
return false;
}
endif;
//現在のカテゴリをカンマ区切りテキストで取得する
if ( !function_exists( 'get_category_ids' ) ):
function get_category_ids(){
if ( is_single() ) {//投稿ページでは全カテゴリー取得
$categories = get_the_category();
$category_IDs = array();
foreach($categories as $category):
array_push( $category_IDs, $category -> cat_ID);
endforeach ;
return $category_IDs;
} elseif ( is_category() ) {//カテゴリページではトップカテゴリーのみ取得
// $categories = get_the_category();
// //_v($categories);
// $cat_now = $categories[0];
// //_v($cat_now->cat_ID);
// return array( $cat_now->cat_ID );
// global $cat;
// return $cat;
// $cat_info = get_category( $cat );
// _v($cat_info);
$obj = get_queried_object();
return array( $obj->cat_ID );
}
return null;
}
endif;
//AdSense用のフォーマットに変換
if ( !function_exists( 'to_adsense_format' ) ):
function to_adsense_format($format){
switch ($format) {
case AD_FORMAT_SINGLE_RECTANGLE:
$format = DATA_AD_FORMAT_RECTANGLE;
break;
case AD_FORMAT_DABBLE_RECTANGLE:
$format = DATA_AD_FORMAT_RECTANGLE;
break;
// case 'none':
// $format = DATA_AD_FORMAT_RECTANGLE;
// break;
}
// switch ($format) {
// case DATA_AD_FORMAT_AUTO:
// $format = DATA_AD_FORMAT_AUTO;
// break;
// case DATA_AD_FORMAT_RECTANGLE:
// $format = DATA_AD_FORMAT_RECTANGLE;
// break;
// case DATA_AD_FORMAT_HORIZONTAL:
// $format = DATA_AD_FORMAT_HORIZONTAL;
// break;
// case DATA_AD_FORMAT_VERTICAL:
// $format = DATA_AD_FORMAT_VERTICAL;
// break;
// case DATA_AD_FORMAT_AUTORELAXED:
// $format = DATA_AD_FORMAT_AUTORELAXED;
// break;
// case DATA_AD_FORMAT_FLUID:
// $format = DATA_AD_FORMAT_FLUID;
// break;
// case DATA_AD_FORMAT_LINK:
// $format = DATA_AD_FORMAT_LINK;
// break;
// default:
// $format = DATA_AD_FORMAT_RECTANGLE;
// break;
// }
return $format;
}
endif;
//フォーマットを指定して広告テンプレートファイル呼び出す
if ( !function_exists( 'get_template_part_with_ad_format' ) ):
function get_template_part_with_ad_format($format = DATA_AD_FORMAT_AUTO, $wrap_class = null, $label_visible = 1, $ad_code = null){
// if ($wrap_class) {
// echo '<div class="'.$wrap_class.'">';
// }
if (isset($wrap_class)) {
$wrap_class = ' '.trim($wrap_class).' ad-'.$format;
}
if ($label_visible) {
$wrap_class .= ' ad-label-visible';
} else {
$wrap_class .= ' ad-label-invisible';
}
//var_dump($format);
//$format変数をテンプレートファイルに渡す
set_query_var('format', $format);
//$wrap_class変数をテンプレートファイルに渡す
set_query_var('wrap_class', $wrap_class);
//$ad_code変数をテンプレートファイルに渡す
set_query_var('ad_code', $ad_code);
//広告テンプレートの呼び出し
get_template_part('tmp/ad');
// if ($wrap_class) {
// echo '</div>';
// }
}
endif;
//オプション付きのテンプレート呼び出し
if ( !function_exists( 'get_template_part_with_option' ) ):
function get_template_part_with_option($slug, $option = null){
//$option変数をテンプレートファイルに渡す
set_query_var('option', $option);
//広告テンプレートの呼び出し
get_template_part($slug);
}
endif;
//オプションの値をデータベースに保存する
if ( !function_exists( 'update_theme_option' ) ):
function update_theme_option($option_name){
$opt_val = isset($_POST[$option_name]) ? $_POST[$option_name] : '';
//update_option($option_name, $opt_val);
set_theme_mod($option_name, $opt_val);
}
endif;
//オプションの値をデータベースから取得する
if ( !function_exists( 'get_theme_option' ) ):
function get_theme_option($option_name, $default = null){
return get_theme_mod($option_name, $default);
}
endif;
//highlight-jsのCSS URLを取得
if ( !function_exists( 'get_highlight_js_css_url' ) ):
function get_highlight_js_css_url(){
return get_template_directory_uri() . '/plugins/highlight-js/styles/'.get_code_highlight_style().'.css';
}
endif;
//Font Awesomeの読み込み
if ( !function_exists( 'wp_enqueue_style_font_awesome' ) ):
function wp_enqueue_style_font_awesome(){
wp_enqueue_style( 'font-awesome-style', FONT_AWESOME_CDN_URL );
}
endif;
//jQueryファイルの読み込み
if ( !function_exists( 'wp_enqueue_script_jquery_js' ) ):
function wp_enqueue_script_jquery_js(){
wp_deregister_script('jquery');
//wp_deregister_script('jquery-core');
wp_deregister_script('jquery-migrate');
wp_register_script('jquery', false, array('jquery-core', 'jquery-migrate'), '1.12.4', true);
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-core', '//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', array(), '1.12.4', true);
wp_enqueue_script('jquery-migrate', '//cdnjs.cloudflare.com/ajax/libs/jquery-migrate/1.4.1/jquery-migrate.min.js', array(), '1.4.1', true);
}
endif;
//ソースコードのハイライト表示に必要なリソースの読み込み
if ( !function_exists( 'wp_enqueue_highlight_js' ) ):
function wp_enqueue_highlight_js(){
//global $pagenow;
if ( (is_code_highlight_enable() && is_singular()) || is_admin_php_page() ) {
//ソースコードハイライト表示用のスタイル
wp_enqueue_style( 'code-highlight-style', get_highlight_js_css_url() );
wp_enqueue_script( 'code-highlight-js', get_template_directory_uri() . '/plugins/highlight-js/highlight.min.js', array( 'jquery' ), false, true );
if (is_admin_php_page()) {
$selector = '.entry-content pre';
} else {
$selector = get_code_highlight_css_selector();
}
$data = minify_js('
(function($){
$("'.$selector.'").each(function(i, block) {
hljs.highlightBlock(block);
});
})(jQuery);
');
wp_add_inline_script( 'code-highlight-js', $data, 'after' ) ;
}
}
endif;
// //LightboxプラグインURLの取得
// if ( !function_exists( 'get_lightbox_css_url' ) ):
// function get_lightbox_css_url(){
// return get_template_directory_uri() . '/plugins/lightbox2/dist/css/lightbox.min.css';
// }
// endif;
// //画像ズームエフェクト用のスタイルタグを出力(管理画面用)
// if ( !function_exists( 'the_zoom_effect_link_tag' ) ):
// function the_zoom_effect_link_tag(){
// if (is_admin_php_page()) {
// if (is_lightbox_effect_enable()) {
// echo '<link rel="stylesheet" href="'.get_lightbox_css_url().'" />';
// }
// }
// }
// endif;
//Lightboxの読み込み
if ( !function_exists( 'wp_enqueue_lightbox' ) ):
function wp_enqueue_lightbox(){
//_v(get_image_zoom_effect());
if ( ((is_lightbox_effect_enable() && (is_singular() || is_category())) || is_admin_php_page()) ) {
//Lightboxスタイルの呼び出し
wp_enqueue_style( 'lightbox-style', get_template_directory_uri() . '/plugins/lightbox2/dist/css/lightbox.min.css' );
//Lightboxスクリプトの呼び出し
wp_enqueue_script( 'lightbox-js', get_template_directory_uri() . '/plugins/lightbox2/dist/js/lightbox.min.js', array( 'jquery' ), false, true );
}
}
endif;
//lityの読み込み
if ( !function_exists( 'wp_enqueue_lity' ) ):
function wp_enqueue_lity(){
if ( ((is_lity_effect_enable() && (is_singular() || is_category())) || is_admin_php_page()) ) {
//lityスタイルの呼び出し
wp_enqueue_style( 'lity-style', get_template_directory_uri() . '/plugins/lity/dist/lity.min.css' );
//lityスクリプトの呼び出し
wp_enqueue_script( 'lity-js', get_template_directory_uri() . '/plugins/lity/dist/lity.min.js', array( 'jquery' ), false, true );
}
}
endif;
//baguetteboxの読み込み
if ( !function_exists( 'wp_enqueue_baguettebox' ) ):
function wp_enqueue_baguettebox(){
if ( ((is_baguettebox_effect_enable() && (is_singular() || is_category())) || is_admin_php_page()) ) {
//baguetteboxスタイルの呼び出し
wp_enqueue_style( 'baguettebox-style', get_template_directory_uri() . '/plugins/baguettebox/dist/baguetteBox.min.css' );
//baguetteboxスクリプトの呼び出し
wp_enqueue_script( 'baguettebox-js', get_template_directory_uri() . '/plugins/baguettebox/dist/baguetteBox.min.js', array( 'jquery' ), false, true );
if (is_singular() || is_category()) {
$selector = '.entry-content';
} else {
$selector = '.entry-demo';
}
$data = minify_js('
(function($){
baguetteBox.run("'.$selector.'");
})(jQuery);
');
wp_add_inline_script( 'baguettebox-js', $data, 'after' ) ;
}
}
endif;
//clingifyの読み込み
if ( !function_exists( 'wp_enqueue_clingify' ) ):
function wp_enqueue_clingify(){
$browser_info = get_browser_info();
$is_ie = $browser_info['browser_name'] == 'IE';
$is_edge_version_under_16 = ($browser_info['browser_name'] == 'IE') && (intval($browser_info['browser_version']) < 16);
//グローバルナビ追従が有効な時
if ( is_global_navi_fixed() || is_scrollable_sidebar_enable() ) {
//clingifyスタイルの呼び出し
//wp_enqueue_style( 'clingify-style', get_template_directory_uri() . '/plugins/clingify/clingify.css' );
//clingifyスクリプトの呼び出し
wp_enqueue_script( 'clingify-js', get_template_directory_uri() . '/plugins/clingify/jquery.clingify.min.js', array( 'jquery' ), false, true );
if (is_global_navi_fixed()) {
switch (get_header_layout_type()) {
case 'center_logo':
$selector = '.navi';
break;
default:
$selector = '.header-container';
break;
}
//$selector = '.sidebar-scroll';
$data = minify_js('
(function($){
$("'.$selector.'").clingify();
})(jQuery);
');
wp_add_inline_script( 'clingify-js', $data, 'after' );
}
//position: sticky;に対応していないブラウザの場合はclingifyを実行
if (is_scrollable_sidebar_enable() && ($is_ie || $is_edge_version_under_16)) {
$data = minify_js('
(function($){
$(".sidebar-scroll").clingify();
})(jQuery);
');
wp_add_inline_script( 'clingify-js', $data, 'after' );
}
if (is_scrollable_main_enable() && ($is_ie || $is_edge_version_under_16)) {
$data = minify_js('
(function($){
$(".main-scroll").clingify();
})(jQuery);
');
wp_add_inline_script( 'clingify-js', $data, 'after' );
}
}
}
endif;
//Slickの読み込み
if ( !function_exists( 'wp_enqueue_slick' ) ):
function wp_enqueue_slick(){
//wp_enqueue_style( 'slick-style', get_template_directory_uri() . '/plugins/slick/slick.css' );
wp_enqueue_style( 'slick-theme-style', get_template_directory_uri() . '/plugins/slick/slick-theme.css' );
//Slickスクリプトの呼び出し
wp_enqueue_script( 'slick-js', get_template_directory_uri() . '/plugins/slick/slick.min.js', array( 'jquery' ), false, true );
$autoplay = null;
if (is_carousel_autoplay_enable()) {
$autoplay = 'autoplay: true,';
}
$data = minify_js('
(function($){
$(".carousel-content").slick({
dots: true,'.
$autoplay.
'autoplaySpeed: '.strval(intval(get_carousel_autoplay_interval())*1000).',
infinite: true,
slidesToShow: 6,
slidesToScroll: 6,
responsive: [
{
breakpoint: 1240,
settings: {
slidesToShow: 5,
slidesToScroll: 5
}
},
{
breakpoint: 1030,
settings: {
slidesToShow: 4,
slidesToScroll: 4
}
},
{
breakpoint: 768,
settings: {
slidesToShow: 3,
slidesToScroll: 3
}
},
{
breakpoint: 480,
settings: {
slidesToShow: 2,
slidesToScroll: 2
}
}
]
});
})(jQuery);
');
wp_add_inline_script( 'slick-js', $data, 'after' ) ;
}
endif;
//SlickNav
if ( !function_exists( 'wp_enqueue_slicknav' ) ):
function wp_enqueue_slicknav(){
if (is_slicknav_visible() || is_admin_php_page()) {
//wp_enqueue_style( 'slicknav-style', get_template_directory_uri() . '/plugins/slicknav/slicknav.css' );
//SlickNavスクリプトの呼び出し
wp_enqueue_script( 'slicknav-js', get_template_directory_uri() . '/plugins/slicknav/jquery.slicknav.min.js', array( 'jquery' ), false, true );
$data = minify_js('
(function($){
$(".menu-header").slicknav();
})(jQuery);
');
wp_add_inline_script( 'slicknav-js', $data, 'after' ) ;
}
}
endif;
//clingifyの読み込み
if ( !function_exists( 'wp_enqueue_stickyfill' ) ):
function wp_enqueue_stickyfill(){
$browser_info = get_browser_info();
$is_ie = $browser_info['browser_name'] == 'IE';
$is_edge_version_under_16 = ($browser_info['browser_name'] == 'IE') && (intval($browser_info['browser_version']) < 16);
//グローバルナビ追従が有効な時
if ( is_scrollable_sidebar_enable() || is_scrollable_main_enable() ) {
//stickyfillスクリプトの呼び出し
wp_enqueue_script( 'stickyfill-js', get_template_directory_uri() . '/plugins/stickyfill/dist/stickyfill.min.js', array( 'jquery' ), false, true );
//position: sticky;に対応していないブラウザの場合はstickyfillを実行
if (is_scrollable_sidebar_enable() && ($is_ie || $is_edge_version_under_16)) {
$data = minify_js('
(function($){
var elements = $(".sidebar-scroll");
Stickyfill.add(elements);
})(jQuery);
');
wp_add_inline_script( 'stickyfill-js', $data, 'after' );
}
if (is_scrollable_main_enable() && ($is_ie || $is_edge_version_under_16)) {
$data = minify_js('
(function($){
var elements = $(".main-scroll");
Stickyfill.add(elements);
})(jQuery);
');
wp_add_inline_script( 'stickyfill-js', $data, 'after' );
}
}
}
endif;
//Masonryの読み込み
if ( !function_exists( 'wp_enqueue_jquery_masonry' ) ):
function wp_enqueue_jquery_masonry(){
if ((is_entry_card_type_tile_card() || is_admin()) && !is_singular()) {
//wp_deregister_script('jquery-masonry');
wp_register_script('jquery-masonry', false, array('jquery'), false, true);
wp_enqueue_script('jquery-masonry');
$common_code = '
$("#list.ect-tile-card").masonry({
itemSelector: ".entry-card-wrap",
isAnimated: true
});
';
$admin_code = null;
if (1 || is_admin()) {
$admin_code = '
$(function(){
setInterval(function(){'.
$common_code
.'},1000);
});
';
}
//実行コードの記入
$code = minify_js('
(function($){'.
$common_code.
$admin_code
.'})(jQuery);
');
wp_add_inline_script( 'jquery-masonry', $code, 'after' );
}
}
endif;
//投稿画面ポスト時の確認ダイアログ
if ( !function_exists( 'wp_enqueue_confirmation_before_publish' ) ):
function wp_enqueue_confirmation_before_publish(){
if (is_confirmation_before_publish()) {
$post_text = __( '公開', THEME_NAME );
$confirm_text = __( '記事を公開してもよろしいですか?', THEME_NAME );
$data = <<< EOM
window.onload = function() {
var id = document.getElementById('publish');
if (id.value.indexOf("$post_text", 0) != -1) {
id.onclick = publish_confirm;
}
}
function publish_confirm() {
if (window.confirm("$confirm_text")) {
return true;
} else {
var elements = document.getElementsByTagName('span');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
if (element.className.indexOf("spinner", 0) != -1) {
element.classList.remove('spinner');
}
}
document.getElementById('publish').classList.remove('button-primary-disabled');
document.getElementById('save-post').classList.remove('button-disabled');
return false;
}
}
EOM;
wp_add_inline_script( 'admin-javascript', $data, 'after' ) ;
}
}
endif;
//Google Fontsの読み込み
if ( !function_exists( 'wp_enqueue_google_fonts' ) ):
function wp_enqueue_google_fonts(){
if (!is_site_font_family_local()) {
wp_enqueue_style( 'google-fonts-'.get_site_font_source(), get_site_font_source_url() );
}
}
endif;
//設定変更CSSを読み込む
if ( !function_exists( 'wp_add_css_custome_to_inline_style' ) ):
function wp_add_css_custome_to_inline_style(){
ob_start();//バッファリング
get_template_part('tmp/css-custom');
$css_custom = ob_get_clean();
//CSSの縮小化
$css_custom = minify_css($css_custom);
//HTMLにインラインでスタイルを書く
wp_add_inline_style( THEME_NAME.'-style', $css_custom );
}
endif;
//投稿を1つランダム取得
if ( !function_exists( 'get_random_posts' ) ):
function get_random_posts($count = 1){
$count = intval($count);
$posts = get_posts('numberposts='.$count.'&orderby=rand');
if ($count == 1) {
foreach( $posts as $post ) {
return $post;
}
} else {
return $posts;
}
}
endif;
//最新の投稿を取得
if ( !function_exists( 'get_latest_posts' ) ):
function get_latest_posts($count = 1){
$count = intval($count);
$posts = get_posts('numberposts='.$count);
if ($couut == 1) {
foreach( $posts as $post ) {
return $post;
}
} else {
return $posts;
}
}
endif;
//更新日の取得(更新日がない場合はnullを返す)
if ( !function_exists( 'get_update_time' ) ):
function get_update_time($format = null) {
if (empty($format)) {
$format = get_site_date_format();
}
$mtime = get_the_modified_time('Ymd');
$ptime = get_the_time('Ymd');
if ($ptime > $mtime) {
return get_the_time($format);
} elseif ($ptime === $mtime) {
return null;
} else {
return get_the_modified_time($format);
}
}
endif;
//ターゲットに文字列が含まれているか
if ( !function_exists( 'includes_string' ) ):
function includes_string($target, $searchstr){
if (strpos($target, $searchstr) === false) {
return false;
} else {
return true;
}
}
endif;
//サイトアドレスが含まれているか
if ( !function_exists( 'includes_site_url' ) ):
function includes_site_url($url){
//URLにサイトアドレスが含まれていない場合
if (strpos($url, site_url()) === false) {
return false;
} else {
return true;
}
}
endif;
//ホームアドレスが含まれているか
if ( !function_exists( 'includes_home_url' ) ):
function includes_home_url($url){
//URLにホームアドレスが含まれていない場合
if (strpos($url, home_url()) === false) {
return false;
} else {
return true;
}
}
endif;
//Wordpressインストールフォルダが含まれているか
if ( !function_exists( 'includes_abspath' ) ):
function includes_abspath($local){
//URLにサイトアドレスが含まれていない場合
if (strpos($local, ABSPATH) === false) {
return false;
} else {
return true;
}
}
endif;
//内部URLをローカルパスに変更
if ( !function_exists( 'url_to_local' ) ):
function url_to_local($url){
//URLにサイトアドレスが含まれていない場合
if (!includes_site_url($url)) {
return false;
}
$path = str_replace(site_url(), ABSPATH, $url);
$path = str_replace('//', '/', $path);
$path = str_replace('\\', '/', $path);
// $path = str_replace(content_url(), WP_CONTENT_DIR, $url);
// $path = str_replace('\\', '/', $path);
return $path;
}
endif;
//ローカルパスを内部URLに変更
if ( !function_exists( 'local_to_url' ) ):
function local_to_url($local){
//URLにサイトアドレスが含まれていない場合
if (!includes_abspath($local)) {
return false;
}
$url = str_replace(ABSPATH, site_url().'/', $local);
//$url = str_replace('//', '/', $url);
$url = str_replace('\\', '/', $url);
// _v($local);
// _v(ABSPATH);
// _v(site_url());
// _v($url);
return $url;
}
endif;
//テーマのリソースディレクトリ
if ( !function_exists( 'get_theme_resources_dir' ) ):
function get_theme_resources_dir(){
$dir = WP_CONTENT_DIR.'/uploads/'.THEME_NAME.'-resources/';
if (!file_exists($dir)) mkdir($dir, 0777, true);
return $dir;
}
endif;
//テーマの汎用キャッシュディレクトリ
if ( !function_exists( 'get_theme_cache_dir' ) ):
function get_theme_cache_dir(){
$dir = get_theme_resources_dir().'cache/';
if (!file_exists($dir)) mkdir($dir, 0777, true);
return $dir;
}
endif;
//テーマのブログカードキャッシュディレクトリ
if ( !function_exists( 'get_theme_blog_card_cache_dir' ) ):
function get_theme_blog_card_cache_dir(){
$dir = get_theme_resources_dir().'blog-card-cache/';
if (!file_exists($dir)) mkdir($dir, 0777, true);
return $dir;
}
endif;
//テーマのCSSキャッシュディレクトリ
if ( !function_exists( 'get_theme_css_cache_dir' ) ):
function get_theme_css_cache_dir(){
$dir = get_theme_resources_dir().'css-cache/';
if (!file_exists($dir)) mkdir($dir, 0777, true);
return $dir;
}
endif;
//テーマのカスタムCSSファイル
if ( !function_exists( 'get_theme_css_cache_file' ) ):
function get_theme_css_cache_file(){
$file = get_theme_css_cache_dir().'css-custom.css';
//キャッシュファイルが存在しない場合はからのファイルを生成
if (!file_exists($file)) {
wp_filesystem_put_contents($file, '');
}
return $file;
}
endif;
//テーマのカスタムCSSファイルURL
if ( !function_exists( 'get_theme_css_cache_file_url' ) ):
function get_theme_css_cache_file_url(){
$url = local_to_url(get_theme_css_cache_file());
return $url;
}
endif;
//画像URLから幅と高さを取得する(同サーバー内ファイルURLのみ)
if ( !function_exists( 'get_image_width_and_height' ) ):
function get_image_width_and_height($image_url){
//URLにサイトアドレスが含まれていない場合
if (!includes_site_url($image_url)) {
return false;
}
// $wp_upload_dir = wp_upload_dir();
// $uploads_dir = $wp_upload_dir['basedir'];
// $uploads_url = $wp_upload_dir['baseurl'];
// $image_file = str_replace($uploads_url, $uploads_dir, $image_url);
$image_file = url_to_local($image_url);
//_v($image_file);
if (file_exists($image_file)) {
$imagesize = getimagesize($image_file);
if ($imagesize) {
$res = array();
$res['width'] = $imagesize[0];
$res['height'] = $imagesize[1];
//_v($res);
return $res;
}
}
}
endif;
//テーマ設定ページか
if ( !function_exists( 'is_admin_php_page' ) ):
function is_admin_php_page(){
global $pagenow;
return $pagenow == 'admin.php';
}
endif;
//投稿新規作成ページ
if ( !function_exists( 'is_admin_post_new_php_page' ) ):
function is_admin_post_new_php_page(){
global $pagenow;
return $pagenow == 'post-new.php';
}
endif;
//投稿ページ
if ( !function_exists( 'is_admin_post_php_page' ) ):
function is_admin_post_php_page(){
global $pagenow;
return $pagenow == 'post.php';
}
endif;
//投稿・新規作成ページかどうか
if ( !function_exists( 'is_admin_post_page' ) ):
function is_admin_post_page(){
return is_admin_post_new_php_page() || is_admin_post_php_page();
}
endif;
//ウィジェットページか
if ( !function_exists( 'is_widgets_php_page' ) ):
function is_widgets_php_page(){
global $pagenow;
return $pagenow == 'widgets.php';
}
endif;
//サイトのドメインを取得
if ( !function_exists( 'get_the_site_domain' ) ):
function get_the_site_domain(){
//ドメイン情報を$results[1]に取得する
if (preg_match( '/https?:\/\/(.+?)\//i', admin_url(), $results )) {
return $results[1];
} else {
return get_domain_name(home_url());
}
}
endif;
//URLからドメインを取得
if ( !function_exists( 'get_domain_name' ) ):
function get_domain_name($url){
return parse_url($url, PHP_URL_HOST);
}
endif;
//拡張子のみを取得する
if ( !function_exists( 'get_extention' ) ):
function get_extention($filename){
return preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);
}
endif;
//ファイル名のみを取得する
if ( !function_exists( 'get_basename' ) ):
function get_basename($filename){
$p = pathinfo($filename);
return basename ( $filename, ".{$p['extension']}" );
}
endif;
//bbPressがインストールされているか
if ( !function_exists( 'is_bbpress_exist' ) ):
function is_bbpress_exist(){
return class_exists( 'bbPress' );
}
endif;
//bbPressのページかどうか
if ( !function_exists( 'is_bbpress_page' ) ):
function is_bbpress_page(){
if (is_bbpress_exist()) {
if (bbp_is_topic() ||
bbp_is_forum() ||
bbp_is_forum_archive() ||
bbp_is_single_forum() ||
bbp_is_forum_edit() ||
bbp_is_single_topic() ||
bbp_is_topic_archive() ||
bbp_is_topic_edit() ||
bbp_is_topic_tag() ||
bbp_is_topic_tag_edit() ||
bbp_is_reply()||
bbp_is_reply_edit() ||
bbp_is_single_reply() ||
bbp_is_favorites() ||
bbp_is_subscriptions()) {
return true;
}
}
}
endif;
//BuddyPressが存在するか
if ( !function_exists( 'is_buddypress_exist' ) ):
function is_buddypress_exist(){
return class_exists('BuddyPress');
}
endif;
//buddypressのページかどうか
if ( !function_exists( 'is_buddypress_page' ) ):
function is_buddypress_page(){
if (is_buddypress_exist()) {
//bp-core-template.phpファイルから
if (bp_is_my_profile()
|| bp_is_user_profile()
// || bp_is_forums_component()
// || bp_is_user_forums_started()
// || bp_is_user_forums_replied_to()
// || bp_is_group_forum()
// || bp_is_group_forum_topic()
// || bp_is_group_forum_topic_edit()
// || bp_is_user_forums()
|| (is_buddypress_exist() && is_bbpress_page())
) {
return true;
}
}
}
endif;
//wpForoが存在するか
if ( !function_exists( 'is_wpforo_exist' ) ):
function is_wpforo_exist(){
return class_exists('wpForo');
}
endif;
//wpforoのページかどうか
if ( !function_exists( 'is_wpforo_plugin_page' ) ):