Skip to content

Commit 19a8f17

Browse files
committed
Remove globals in favor of API wrappers. Fixes bradyvercher#44
1 parent a400048 commit 19a8f17

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

includes/class-gistpress.php

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ public function wp_embed_handler( array $matches, array $attr, $url, array $rawa
171171
* @return string HTML content to display the Gist.
172172
*/
173173
public function shortcode( array $rawattr ) {
174-
global $post;
175-
176174
$shortcode = $this->rebuild_shortcode( $rawattr );
177175

178176
$attr = $this->standardize_attributes( $rawattr );
@@ -199,35 +197,33 @@ public function shortcode( array $rawattr ) {
199197
$url = 'https://gist.github.com/' . $attr['id'];
200198
$json_url = $url . '.json';
201199

202-
if ( isset( $post->ID ) ) {
203-
if ( is_feed() ) {
204-
$html = sprintf( '<a href="%s" target="_blank"><em>%s</em></a>', esc_url( $url ), __( 'View this code snippet on GitHub.', 'gistpress' ) );
205-
return apply_filters( 'gistpress_feed_html', $html );
206-
}
207-
208-
$html = $this->get_gist_html( $json_url, $attr );
200+
if ( is_feed() ) {
201+
$html = sprintf( '<a href="%s" target="_blank"><em>%s</em></a>', esc_url( $url ), __( 'View this code snippet on GitHub.', 'gistpress' ) );
202+
return apply_filters( 'gistpress_feed_html', $html );
203+
}
209204

210-
if ( $this->unknown() === $html ) {
211-
return make_clickable( $url );
212-
}
205+
$html = $this->get_gist_html( $json_url, $attr );
213206

214-
// If there was a result, return it.
215-
if ( $html ) {
216-
if ( $attr['embed_stylesheet'] ) {
217-
wp_enqueue_style( 'gistpress' );
218-
}
207+
if ( $this->unknown() === $html ) {
208+
return make_clickable( $url );
209+
}
219210

220-
$html = apply_filters( 'gistpress_html', $html, $url, $attr, $post->ID );
211+
// If there was a result, return it.
212+
if ( $html ) {
213+
if ( $attr['embed_stylesheet'] ) {
214+
wp_enqueue_style( 'gistpress' );
215+
}
221216

222-
foreach ( $attr as $key => $value ) {
223-
$message = '<strong>' . $key . __(' (shortcode attribute)', 'gistpress') . ':</strong> ';
224-
$message .= is_scalar( $value ) ? $value : print_r( $value, true );
225-
$this->debug_log( $message, $shortcode_hash );
226-
}
227-
$this->debug_log( '<strong>Gist:</strong><br />' . $html, $shortcode_hash );
217+
$html = apply_filters( 'gistpress_html', $html, $url, $attr, get_the_ID() );
228218

229-
return $html;
219+
foreach ( $attr as $key => $value ) {
220+
$message = '<strong>' . $key . __(' (shortcode attribute)', 'gistpress') . ':</strong> ';
221+
$message .= is_scalar( $value ) ? $value : print_r( $value, true );
222+
$this->debug_log( $message, $shortcode_hash );
230223
}
224+
$this->debug_log( '<strong>Gist:</strong><br />' . $html, $shortcode_hash );
225+
226+
return $html;
231227
}
232228

233229
return '';
@@ -339,8 +335,6 @@ public function parse_line_number_arg( $line_numbers ) {
339335
* @return string Gist HTML or {{unknown}} if it couldn't be determined.
340336
*/
341337
public function get_gist_html( $url, array $args ) {
342-
global $post;
343-
344338
// Add a specific file from a Gist to the URL.
345339
if ( ! empty( $args['file'] ) ) {
346340
$url = add_query_arg( 'file', urlencode( $args['file'] ), $url );
@@ -368,7 +362,7 @@ public function get_gist_html( $url, array $args ) {
368362

369363
// Update the post meta fallback.
370364
// @link http://core.trac.wordpress.org/ticket/21767
371-
update_post_meta( $post->ID, $raw_key, addslashes( $json->div ) );
365+
update_post_meta( get_the_ID(), $raw_key, addslashes( $json->div ) );
372366

373367
$html = $this->process_gist_html( $json->div, $args );
374368

@@ -385,7 +379,7 @@ public function get_gist_html( $url, array $args ) {
385379
// Failures are cached, too. Update the post to attempt to fetch again.
386380
$html = ( $html ) ? $html : $this->unknown();
387381

388-
if ( $this->unknown() == $html && ( $fallback = get_post_meta( $post->ID, $raw_key, true ) ) ) {
382+
if ( $this->unknown() == $html && ( $fallback = get_post_meta( get_the_ID(), $raw_key, true ) ) ) {
389383
// Return the fallback instead of the string representing unknown.
390384
$html = $this->process_gist_html( $fallback, $args );
391385

0 commit comments

Comments
 (0)