@@ -265,7 +265,7 @@ protected function extractFromComment( $comment ) {
265
265
$ this ->download_url = trim ( $ matches [1 ] );
266
266
}
267
267
268
- if ( empty ( $ this ->buy_url ) && $ comment && preg_match ( '#donate[-\s]?url [:\s]*([^\r\n]*)#i ' , $ comment , $ matches ) ) {
268
+ if ( empty ( $ this ->buy_url ) && $ comment && preg_match ( '#donate[-\s]?link [:\s]*([^\r\n]*)#i ' , $ comment , $ matches ) ) {
269
269
$ this ->buy_url = trim ( $ matches [1 ] );
270
270
} elseif ( empty ( $ this ->buy_url ) ) {
271
271
$ this ->buy_url = $ this ->download_url ;
@@ -546,6 +546,33 @@ public function file( $file = '' ) {
546
546
return trailingslashit ( dirname ( $ this ->file ) ) . ltrim ( "$ file " , '/ ' );
547
547
}
548
548
549
+ /**
550
+ * @param string $name Handle name
551
+ * @param string $js Filename (optional extension)
552
+ * @param array $require
553
+ * @param bool $localize
554
+ * @param bool $in_footer
555
+ * @param string $async_defer
556
+ *
557
+ * @return bool|string Handle name if registered false otherwise
558
+ */
559
+ public function addExternalScript ( $ name , $ js , $ require = array (), $ localize = false , $ in_footer = false , $ async_defer = 'async ' ) {
560
+ if ( ! did_action ( 'init ' ) ) {
561
+ add_action ( 'init ' , function () use ( $ name , $ js , $ require , $ localize , $ in_footer ) {
562
+ $ this ->addExternalScript ( $ name , $ js , $ require , $ localize , $ in_footer );
563
+ } );
564
+
565
+ return false ;
566
+ }
567
+ $ name = $ this ->registerExternalScript ( $ name , $ js , $ require , $ localize , $ in_footer , $ async_defer );
568
+
569
+ if ( $ name ) {
570
+ wp_enqueue_script ( $ name );
571
+ }
572
+
573
+ return $ name ;
574
+ }
575
+
549
576
/**
550
577
* @param string $js Filename (optional extension)
551
578
* @param array $require
@@ -563,6 +590,13 @@ public function addScript( $js, $require = array(), $localize = false, $in_foote
563
590
564
591
return false ;
565
592
}
593
+ if ( did_action ( 'wp_head ' ) ) {
594
+ $ in_footer = true ;
595
+ }
596
+
597
+ if ( $ in_footer ) {
598
+ $ async_defer = '' ;
599
+ }
566
600
$ name = $ this ->registerScript ( $ js , $ require , $ localize , $ in_footer , $ async_defer );
567
601
568
602
if ( $ name ) {
@@ -573,31 +607,33 @@ public function addScript( $js, $require = array(), $localize = false, $in_foote
573
607
}
574
608
575
609
public function getHandleName ( $ script ) {
576
- return $ this ->getSlug () . basename ( $ script );
610
+ return $ this ->getSlug () . ' - ' . basename ( $ script );
577
611
}
578
612
579
613
public function getJsName ( $ script ) {
580
614
return str_replace ( array ( '- ' , '. ' ), array ( '_ ' , '_ ' ), basename ( $ script ) );
581
615
}
582
616
583
617
/**
584
- * @param $js
618
+ * @param string $name Handle name
619
+ * @param string $js Name of the file without extension and js folder
585
620
* @param array $require
586
- * @param bool $localize
621
+ * @param bool|array $localize
587
622
* @param bool $in_footer
588
623
* @param string $async_defer
589
624
*
590
625
* @return bool|string Handle name if registered false otherwise
591
626
*/
592
- public function registerScript ( $ js , $ require = array (), $ localize = false , $ in_footer = false , $ async_defer = 'async ' ) {
627
+ public function registerExternalScript ( $ name , $ js , $ require = array (), $ localize = false , $ in_footer = false , $ async_defer = 'async ' ) {
593
628
if ( ! did_action ( 'init ' ) ) {
594
- add_action ( 'init ' , function () use ( $ js , $ require , $ localize , $ in_footer ) {
595
- $ this ->registerScript ( $ js , $ require , $ localize , $ in_footer );
629
+ //Poor performances so better to not use this massively
630
+ add_action ( 'init ' , function () use ( $ name , $ js , $ require , $ localize , $ in_footer ) {
631
+ $ this ->registerExternalScript ( $ name , $ js , $ require , $ localize , $ in_footer );
596
632
} );
597
633
598
634
return false ;
599
635
}
600
- $ name = $ this ->getHandleName ( $ js );
636
+ $ name = $ this ->getHandleName ( $ name );
601
637
if ( ! wp_script_is ( $ name , 'registered ' ) ) {
602
638
if ( $ file = $ this ->searchFile ( $ js , 'js ' , true , 'js ' ) ) {
603
639
wp_register_script ( $ name , $ file , $ require , $ this ->version , $ in_footer );
@@ -606,6 +642,8 @@ public function registerScript( $js, $require = array(), $localize = false, $in_
606
642
if ( wp_script_is ( $ name , 'registered ' ) ) {
607
643
if ( ! empty ( $ async_defer ) ) {
608
644
$ this ->addAsyncDeferAttribute ( $ name , $ async_defer );
645
+ } else {
646
+ $ this ->removeAsyncDeferAttribute ( $ name );
609
647
}
610
648
if ( ! empty ( $ localize ) ) {
611
649
$ this ->localizeScript ( $ js , $ localize );
@@ -617,6 +655,19 @@ public function registerScript( $js, $require = array(), $localize = false, $in_
617
655
return false ;
618
656
}
619
657
658
+ /**
659
+ * @param string $js Name of the file without extension and js folder
660
+ * @param array $require
661
+ * @param bool|array $localize
662
+ * @param bool $in_footer
663
+ * @param string $async_defer
664
+ *
665
+ * @return bool|string Handle name if registered false otherwise
666
+ */
667
+ public function registerScript ( $ js , $ require = array (), $ localize = false , $ in_footer = false , $ async_defer = 'async ' ) {
668
+ return $ this ->registerExternalScript ( $ js , $ js , $ require , $ localize , $ in_footer , $ async_defer );
669
+ }
670
+
620
671
public function localizeScript ( $ js , $ localize ) {
621
672
global $ wpp_localize_scripts ;
622
673
@@ -638,6 +689,13 @@ public function localizeScript( $js, $localize ) {
638
689
wp_localize_script ( $ this ->slug , $ this ->getJsName ( $ this ->slug ), $ wpp_localize_scripts [ $ this ->slug ] );
639
690
}
640
691
692
+ public function removeAsyncDeferAttribute ( $ handle ) {
693
+ global $ wpp_script_handles ;
694
+
695
+ if ( isset ( $ wpp_script_handles [ $ handle ] ) ) {
696
+ unset( $ wpp_script_handles [ $ handle ] );
697
+ }
698
+ }
641
699
642
700
public function addAsyncDeferAttribute ( $ handle , $ async_defer = 'async ' ) {
643
701
global $ wpp_script_handles ;
@@ -687,14 +745,25 @@ public function addStyle( $css, $media = 'all' ) {
687
745
* @return bool|string Handle name if registered false otherwise
688
746
*/
689
747
public function registerStyle ( $ css , $ media = 'all ' ) {
748
+ return $ this ->registerExternalStyle ( $ css , $ css , $ media );
749
+ }
750
+
751
+ /**
752
+ * @param string $name Handle name
753
+ * @param string $css Filename (extension is optional)
754
+ * @param string $media
755
+ *
756
+ * @return bool|string Handle name if registered false otherwise
757
+ */
758
+ public function registerExternalStyle ( $ name , $ css , $ media = 'all ' ) {
690
759
if ( ! did_action ( 'init ' ) ) {
691
- add_action ( 'init ' , function () use ( $ css , $ media ) {
692
- $ this ->registerStyle ( $ css , $ media );
760
+ add_action ( 'init ' , function () use ( $ name , $ css , $ media ) {
761
+ $ this ->registerExternalStyle ( $ name , $ css , $ media );
693
762
} );
694
763
695
764
return false ;
696
765
}
697
- $ name = $ this ->getHandleName ( $ css );
766
+ $ name = $ this ->getHandleName ( $ name );
698
767
if ( ! wp_style_is ( $ name , 'registered ' ) ) {
699
768
if ( $ file = $ this ->searchFile ( $ css , 'css ' , true , 'css ' ) ) {
700
769
wp_register_style ( $ name , $ file , array (), $ this ->version , $ media );
0 commit comments