Last active
October 7, 2018 18:21
-
-
Save danielpataki/937f025e6f989d90424f2ece2149285d to your computer and use it in GitHub Desktop.
Movie list with transients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function form( $instance ) { | |
$title = ( empty( $instance['title'] ) ) ? '' : $instance['title']; | |
$director = ( empty( $instance['director'] ) ) ? '' : $instance['director']; | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_name( 'director' ); ?>"><?php _e( 'Director:' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'director' ); ?>" name="<?php echo $this->get_field_name( 'director' ); ?>" type="text" value="<?php echo esc_attr( $director ); ?>" /> | |
</p> | |
<?php | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_movies( $director ) { | |
$url = 'http://netflixroulette.net/api/api.php?director=' . rawurlencode( $director ); | |
$response = wp_remote_get( $url ); | |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); | |
return $data; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$data = $this->get_movies( $instance['director'] ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_movies() { | |
$response = wp_remote_get( 'http://netflixroulette.net/api/api.php?director=Martin%20Scorsese', $args ); | |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); | |
return $data; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In the theme | |
$menu = get_transient( 'my-site-menu' ); | |
if( empty( $menu ) ) { | |
$menu = wp_nav_menu( array( | |
"echo" => false | |
)); | |
update_transient( 'my-site-menu', $menu, YEAR_IN_SECONDS ); | |
} | |
echo $menu; | |
// Elsewhere | |
add_action( 'wp_update_nav_menu', 'theme_update_menu' ); | |
function theme_update_menu() { | |
delete_transient( 'my-site-menu' ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: My Favorite Director | |
Plugin URI: http://danielpataki.com | |
Description: A transient project that displays movies from a favorite director | |
Author: Daniel Pataki | |
Author URI: http://danielpataki.com | |
Version: 1.1.0 | |
*/ | |
include 'MY_Favorite_Director_Widget.php'; | |
class My_Favorite_Director { | |
function __construct() { | |
add_action( 'widgets_init', array( $this, 'init_widget' ) ); | |
} | |
function init_widget() { | |
register_widget( 'My_Favorite_Director_Widget' ); | |
} | |
} | |
new My_Favorite_Director; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function widget( $args, $instance ) { | |
$data = get_transient( 'my-favorite-director' ); | |
if( empty( $data ) ) { | |
$data = $this->get_movies(); | |
set_transient( 'my-favorite-director', $data, WEEK_IN_SECONDS ); | |
} | |
if( empty( $data ) ) { | |
return; | |
} | |
// ... rest of the function is the same | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function widget( $args, $instance ) { | |
$transient = 'mfd_' . sanitize_title_with_dashes( $instance['director'] ); | |
$data = get_transient( $transient ); | |
if( empty( $data ) ) { | |
$data = $this->get_movies( $instance['director'] ); | |
set_transient( $transient , $data, WEEK_IN_SECONDS ); | |
} | |
// ... Rest of the function unchanged | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class My_Favorite_Director_Widget extends WP_Widget | |
{ | |
function __construct() | |
{ | |
$widget_details = array( | |
'classname' => 'my-favorite-director-widget', | |
'description' => 'Display movies from your favorite director.' | |
); | |
parent::__construct( 'my-favorite-director', 'My Favorite Director', $widget_details ); | |
} | |
function form( $instance ) { | |
$title = ( empty( $instance['title'] ) ) ? '' : $instance['title']; | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> | |
</p> | |
<?php | |
} | |
function update( $new_instance, $old_instance ) { | |
return $new_instance; | |
} | |
function widget( $args, $instance ) { | |
$response = wp_remote_get( 'http://netflixroulette.net/api/api.php?director=Martin%20Scorsese', $args ); | |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); | |
if( empty( $data ) ) { | |
return; | |
} | |
echo $args['before_widget']; | |
if( !empty( $instance['title'] ) ) { | |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title']; | |
} | |
echo "<ul>"; | |
foreach( $data as $movie ) { | |
echo "<li>" . $movie['show_title'] . "</li>"; | |
} | |
echo "</ul>"; | |
echo $args['after_widget']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment