Skip to content

Commit 8985742

Browse files
committed
Cache the remote API call in the file lookup method.
1 parent c2fca39 commit 8985742

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

includes/class-gistpress.php

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public function shortcode( array $rawattr ) {
183183
// This is set to true when posts are updated.
184184
if ( $this->delete_shortcode_transients ) {
185185
delete_transient( $this->transient_key( $shortcode_hash ) );
186+
delete_transient( $this->gist_files_transient_key( $attr['id'] ) );
186187
return;
187188
}
188189

@@ -659,8 +660,6 @@ protected function standardize_attributes( array $rawattr ) {
659660
* can use the shortcode approach, which allows a specific file name to be
660661
* used.
661662
*
662-
*
663-
*
664663
* @since 2.1.0
665664
*
666665
* @param string $sanitized_filename Sanitized filename, such as foo-bar-php.
@@ -670,25 +669,36 @@ protected function standardize_attributes( array $rawattr ) {
670669
* @return string Filename, or empty string if it couldn't be determined.
671670
*/
672671
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.
674673
if ( '_' === $delimiter ) {
675674
return $sanitized_filename;
676675
}
677676

678677
// New style bookmark - filename had . replaced with -
679678
// 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+
}
692702
}
693703
}
694704

@@ -744,6 +754,19 @@ protected function transient_key( $identifier ) {
744754
return 'gist_html_' . $identifier;
745755
}
746756

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+
747770
/**
748771
* String to identify a failure when retrieving a Gist's HTML.
749772
*

0 commit comments

Comments
 (0)