@@ -183,6 +183,7 @@ public function shortcode( array $rawattr ) {
183
183
// This is set to true when posts are updated.
184
184
if ( $ this ->delete_shortcode_transients ) {
185
185
delete_transient ( $ this ->transient_key ( $ shortcode_hash ) );
186
+ delete_transient ( $ this ->gist_files_transient_key ( $ attr ['id ' ] ) );
186
187
return ;
187
188
}
188
189
@@ -659,8 +660,6 @@ protected function standardize_attributes( array $rawattr ) {
659
660
* can use the shortcode approach, which allows a specific file name to be
660
661
* used.
661
662
*
662
- *
663
- *
664
663
* @since 2.1.0
665
664
*
666
665
* @param string $sanitized_filename Sanitized filename, such as foo-bar-php.
@@ -670,25 +669,36 @@ protected function standardize_attributes( array $rawattr ) {
670
669
* @return string Filename, or empty string if it couldn't be determined.
671
670
*/
672
671
protected function get_file_name ( $ sanitized_filename , $ delimiter , $ id ) {
673
- // Old style link - filename wasn't actually changed
672
+ // Old style link - filename wasn't actually changed.
674
673
if ( '_ ' === $ delimiter ) {
675
674
return $ sanitized_filename ;
676
675
}
677
676
678
677
// New style bookmark - filename had . replaced with -
679
678
// Means we have to go and look up what the filename could have been.
680
- $ url = 'https://gist.github.com/ ' . $ id . '.json ' ;
681
- $ json = $ this ->fetch_gist ( $ url );
682
-
683
- /**
684
- * @todo If a gist has foo.bar.php and foo-bar.php, then we can't yet determine which was actually wanted,
685
- * since both give the same bookmark URL.
686
- *
687
- * Here, we just return the first one we find.
688
- */
689
- foreach ( $ json ->files as $ file ) {
690
- if ( str_replace ( '. ' , '- ' , $ file ) === $ sanitized_filename ) {
691
- return $ file ;
679
+ $ transient_key = $ this ->gist_files_transient_key ( $ id );
680
+ $ gist_files = get_transient ( $ transient_key );
681
+
682
+ if ( ! $ gist_files ) {
683
+ $ url = 'https://gist.github.com/ ' . $ id . '.json ' ;
684
+ $ json = $ this ->fetch_gist ( $ url );
685
+
686
+ if ( $ json && ! empty ( $ json ->files ) ) {
687
+ $ gist_files = $ json ->files ;
688
+ set_transient ( $ transient_key , $ gist_files , 604800 ); // 60 * 60 * 24 * 7 = 1 week
689
+ } else {
690
+ set_transient ( $ transient_key , array (), 900 ); // 60 * 15 = 15 minutes
691
+ }
692
+ }
693
+
694
+ // If a gist has foo.bar.php and foo-bar.php, then we can't yet
695
+ // determine which was actually wanted, since both give the same
696
+ // bookmark URL. Here, we just return the first one we find.
697
+ if ( ! empty ( $ gist_files ) ) {
698
+ foreach ( $ gist_files as $ file ) {
699
+ if ( str_replace ( '. ' , '- ' , $ file ) === $ sanitized_filename ) {
700
+ return $ file ;
701
+ }
692
702
}
693
703
}
694
704
@@ -744,6 +754,19 @@ protected function transient_key( $identifier ) {
744
754
return 'gist_html_ ' . $ identifier ;
745
755
}
746
756
757
+ /**
758
+ * Get the transient key for a list of a Gist's files.
759
+ *
760
+ * @since 2.1.0
761
+ *
762
+ * @param string $id The Gist id.
763
+ *
764
+ * @return string Transient key name.
765
+ */
766
+ protected function gist_files_transient_key ( $ gist_id ) {
767
+ return 'gist_files_ ' . md5 ( $ gist_id );
768
+ }
769
+
747
770
/**
748
771
* String to identify a failure when retrieving a Gist's HTML.
749
772
*
0 commit comments