Skip to content

Instantly share code, notes, and snippets.

@jayj
Last active August 29, 2015 14:01
Show Gist options
  • Save jayj/82e3880ff027a54b06e2 to your computer and use it in GitHub Desktop.
Save jayj/82e3880ff027a54b06e2 to your computer and use it in GitHub Desktop.
<?php
/**
* Jayj_I_Make_Themes
*
* Gets the theme readme from WordPress.org.
*
* Modified version of Mark Jaquith's I Make Plugins class
* @link http://wordpress.org/plugins/i-make-plugins/
*/
class Jayj_I_Make_Themes {
static $instance;
var $readme;
function __construct() {
self::$instance = $this;
}
function get_theme_slug( $post ) {
$post = get_post( $post );
$postmeta_slug = get_post_meta( $post->ID, 'theme-slug', true );
$slug = ( $postmeta_slug ) ? $postmeta_slug : $post->post_name;
return $slug;
}
function get_theme_readme( $theme_id, $force_update = false ) {
$slug = $this->get_theme_slug( $theme_id );
if ( false == $slug ) {
return;
}
if ( ! $force_update ) :
// First, try in-memory cache
if ( isset( $this->cache[$slug] ) )
return $this->cache[$slug];
// Next, try postmeta cache
$ts = get_post_meta( $theme_id, '_theme_readme_timestamp', true );
$rm = get_post_meta( $theme_id, '_theme_readme', true );
// We force a dynamic update after 24 hours
if ( $rm && $ts && $ts > time() - DAY_IN_SECONDS ) { // fresh
$this->cache[$slug] = $rm;
return $this->cache[$slug];
}
endif;
// Fetch via API
require_once( ABSPATH . '/wp-admin/includes/theme.php' );
$readme = themes_api( 'theme_information', array( 'slug' => $slug ) );
if ( is_wp_error( $readme ) ) {
return false;
}
$this->cache[$slug] = $readme;
update_post_meta( $theme_id, '_theme_readme', $readme );
update_post_meta( $theme_id, '_theme_readme_timestamp', time() );
return $readme;
}
}
/*
$theme = new Jayj_I_Make_Themes;
$meta = $theme->get_theme_readme( get_the_ID() );
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment