Last active
December 20, 2015 12:59
-
-
Save gmazzap/6135216 to your computer and use it in GitHub Desktop.
A plugin for Wordpress and NexGen Gallery that use a metabox to select a NextGEN Gallery and use this gallery Preview Image as Post Featured Image
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 | |
/** | |
* @package GMNextGenPreviewImage | |
* @version 0.1.0 | |
*/ | |
/** | |
Plugin Name: GM NextGen Preview Image | |
* | |
Author: Giuseppe Mazzapica | |
Version: 0.1.0 | |
Description: Use a metabox to select a NextGEN Gallery and use this gallery Preview Image as Post Featured Image | |
*/ | |
/** | |
* GMNextGenPreviewImage class | |
* | |
* @package GMNextGenPreviewImage | |
* @author Giuseppe Mazzapica | |
* | |
*/ | |
class GMNextGenPreviewImage { | |
/** | |
* Plugin version | |
* | |
* @since 0.1.0 | |
* | |
* @var string | |
*/ | |
protected $version = '0.1.0'; | |
/** | |
* A prefix | |
* | |
* @since 0.1.0 | |
* | |
* @var string | |
*/ | |
protected static $meta = '_GMNpreview'; | |
/** | |
* Retrieve the thumbnail from meta field and save option | |
* | |
* @since 0.1.0 | |
* | |
* @access protected | |
* @return null | |
*/ | |
protected static function get_thumb($post_id = 0, $gallery_id = '') { | |
global $wpdb; | |
$galleries = $wpdb->prefix . 'ngg_gallery'; | |
$images = $wpdb->prefix . 'ngg_pictures'; | |
$gallery = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $galleries WHERE gid = %d", $gallery_id) ); | |
if ( $gallery ) { | |
if ( intval($gallery->previewpic) ) { | |
$thumb = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $images WHERE pid = %d", $gallery->previewpic) ); | |
} else { | |
$thumb = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $images WHERE galleryid = %d ORDER BY sortorder ASC LIMIT 1", $gallery_id) ); | |
} | |
if ( $thumb ) { | |
$url = site_url( trailingslashit($gallery->path) . $thumb->filename ); | |
$title = $thumb->alttext; | |
self::save_thumb($post_id, $url, $title); | |
} | |
} | |
} | |
/** | |
* Upload the image and set it as thumbnail | |
* | |
* @since 0.1.0 | |
* | |
* @access protected | |
* @return null | |
*/ | |
protected static function save_thumb ($post_id = 0, $url = '', $title = '') { | |
if ( empty($post_id) || empty($url) || ! filter_var($url, FILTER_VALIDATE_URL) ) return; | |
$saved = (array)get_option('GMNextGenPreviewImage'); | |
if ( $saved && in_array($url, array_values($saved) ) ) { | |
$thumbnail_id = array_search ($url , $saved, true); | |
} else { | |
// download and save thumb | |
$get = wp_remote_get( $url ); | |
$mime_type = wp_remote_retrieve_header( $get, 'content-type' ); | |
if ( ! substr_count($mime_type, 'image') ) return false; | |
$bits = wp_upload_bits( basename($url), '', wp_remote_retrieve_body( $get ) ); | |
if ( ! empty($bits['error']) ) return false; | |
$thumb_data = array( | |
'post_title'=> $title ? : __('Thumbnail'), 'post_mime_type' => $mime_type, 'post_content' => '' | |
); | |
$thumbnail_id = wp_insert_attachment( $thumb_data, $bits['file'], $post_id ); | |
if ( $thumbnail_id ) { | |
$saved[$thumbnail_id] = $url; | |
update_option('GMNextGenPreviewImage', $saved); | |
require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
$metadata = wp_generate_attachment_metadata( $thumbnail_id, $bits['file'] ); | |
wp_update_attachment_metadata( $thumbnail_id, $metadata ); | |
} | |
} | |
if ($thumbnail_id) set_post_thumbnail( $post_id, $thumbnail_id ); | |
} | |
/** | |
* Register the metabox | |
* | |
* @since 0.1.0 | |
* | |
* @access public | |
* @return null | |
*/ | |
static function metabox() { | |
global $post; | |
$types = (array)apply_filters('gmngpi_meta_key', array('post') ); | |
if ( ! has_post_thumbnail($post->ID) && in_array($post->post_type, $types) ) { | |
add_meta_box('gmngpi_meta_box', __('Choose Gallery'), array(__CLASS__, '_metabox'), $post->post_type ); | |
} | |
} | |
/** | |
* Print the metabox html | |
* | |
* @since 0.1.0 | |
* | |
* @access public | |
* @return null | |
*/ | |
static function _metabox( $post ) { | |
if ( ! has_post_thumbnail($post->ID) ) { | |
wp_nonce_field( plugin_basename( __FILE__ ), self::$meta . '_nonce' ); | |
global $wpdb; | |
$galleries = $wpdb->get_results( "SELECT gid, name FROM " . $wpdb->prefix . 'ngg_gallery' ); | |
if ( ! empty( $galleries) ) { | |
echo '<select id="' . self::$meta . '_meta_id" name="' . self::$meta . '">'; | |
echo '<option value="" selected="selected">---</option>'; | |
foreach ( $galleries as $gallery ) { | |
printf('<option value="%d">%s</option>', $gallery->gid, $gallery->name); | |
} | |
echo '</select>'; | |
} | |
} else { | |
_e('The post already has a Featured Image'); | |
} | |
} | |
/** | |
* Save the metadata | |
* | |
* @since 0.1.0 | |
* | |
* @access public | |
* @return null | |
*/ | |
static function set_thumb( $post_id ) { | |
if ( ! has_post_thumbnail($post_id ) ) { | |
if ( ! isset( $_POST[ self::$meta . '_nonce' ] ) || ! wp_verify_nonce( $_POST[ self::$meta . '_nonce' ], plugin_basename( __FILE__ ) ) ) return; | |
$types = (array)apply_filters('gmngpi_meta_key', array('post') ); | |
if ( ! in_array($_REQUEST['post_type'], $types) || ! current_user_can( 'edit_posts', $post_id ) ) return; | |
$posted = is_numeric($_POST[self::$meta]) ? intval($_POST[self::$meta]) : null; | |
if ( ! empty($posted) ) self::get_thumb($post_id, $posted); | |
} | |
} | |
/** | |
* Register hook to inizializate plugin on loop start | |
* | |
* @since 0.1.0 | |
* | |
* @access public | |
* @return null | |
*/ | |
static function init() { | |
add_action( 'add_meta_boxes', array(__CLASS__, 'metabox') ); | |
add_action( 'save_post', array(__CLASS__, 'set_thumb') ); | |
} | |
/** | |
* Remove option | |
* | |
* @since 0.1.0 | |
* | |
* @access public | |
* @return null | |
*/ | |
static function uninstall() { | |
delete_option('GMNextGenPreviewImage'); | |
} | |
/** | |
* Constructor. do nothing. | |
* | |
* @since 0.1.0 | |
* | |
* @access public | |
* @return null | |
*/ | |
function __construct( ) { | |
} | |
} | |
/** | |
* Init function. Check for NextGen 2.x and if there inizialize plugin. | |
* | |
* @since 0.1.0 | |
* | |
* @access public | |
* @return null | |
*/ | |
function initGMNextGenPreviewImage() { | |
if( class_exists('C_NextGEN_Bootstrap') ) { | |
GMNextGenPreviewImage::init(); | |
} | |
} | |
/** | |
* Once plugins_loaded is the earliest hook, use lower priority to easy remove | |
* | |
*/ | |
add_action('plugins_loaded', 'initGMNextGenPreviewImage', 100); | |
/** | |
* Unistall And deactivation hooks | |
* | |
*/ | |
register_deactivation_hook(__FILE__, array('GMNextGenPreviewImage', 'uninstall')); | |
register_uninstall_hook(__FILE__, array('GMNextGenPreviewImage', 'uninstall')); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment