Dominik Menke
Author: Vicky Arulsingam
Version: 1.2.2
Author URI: http://garinungkadol.com
*/
//! Additions/new comments marked with "//!"
//! Code is poetry: added PHPDoc-Style
/**
* OPTIONS
*/
$dir = ABSPATH . get_option('gklpa_mydir');
$siteurl = get_settings('siteurl');
$gkl_myAvatarDir = str_replace('/', DIRECTORY_SEPARATOR, $dir); // Updated absolute path to images folder (takes into account Win servers)
$gkl_AvatarURL = trailingslashit($siteurl) . get_option('gklpa_mydir'); // URL to images folder
$gkl_ShowAvatarInPost = get_option('gklpa_showinwritepage'); // Show image in Write Page?
$gkl_ScanRecursive = get_option('gklpa_scanrecursive'); // Recursive scan of the images?
$gkl_ShowInContent = get_option('gklpa_showincontent'); // Show avatar automatically in content?
/**
* Load Text-Domain
*/
load_plugin_textdomain('gklpa', 'wp-content/plugins/post-avatar/languages/');
if ( isset($_POST['gkl_postavatar_options']) && function_exists('check_admin_referer'))
check_admin_referer('gkl_postavatar_form');
/**
* Get list of directory
*
* @param string $dir
* @param boolean $recursive
* @return array
*/
function gkl_readdir($dir, $recursive = true) {
global $gkl_myAvatarDir;
// Cut of the myAvatarDir from the output
$dir2 = $gkl_myAvatarDir .'/';
// Init
$array_items = array();
$handle = opendir($dir);
while (false !== ($file = readdir($handle))) {
// Bad for recursive to scan the current folder again and again and again...
// ...also bad to scan the parent folder
if ( $file != '.' && $file != ".." ) {
// if is_file
if (!is_dir($dir .'/'. $file)) {
$file = $dir .'/'. $file;
// Cut of the myAvatarDir from the output
$array_items[] = str_replace($dir2, '', $file);
} else {
// if (is_dir && recusive scan) scan dir
if ($recursive) {
$array_items = array_merge($array_items, gkl_readdir($dir .'/'. $file, $recursive));
}
$file = $dir .'/'. $file;
// Cut of the myAvatarDir from the output
$array_items[] = str_replace($dir2, '', $file);
}
}
}
closedir($handle);
// Limit list to only images
$array_items = preg_grep('/.jpg$|.jpeg$|.gif$|.png$/', $array_items);
asort($array_items);
return $array_items;
}
/**
* Show form selector in Write Page
*
*/
function gkl_avatar_insert() {
global $gkl_myAvatarDir, $gkl_AvatarURL, $gkl_ShowAvatarInPost, $gkl_ScanRecursive;
// Get current post's avatar
$post_id = $_GET['post'];
$CurrAvatar = get_post_meta($post_id, 'postuserpic');
$selected = ltrim( $CurrAvatar[0], '/' );
//! Get AvatarList
if ($gkl_ScanRecursive == 1)
$recursive = true;
else
$recursive = false;
$AvatarList = gkl_readdir($gkl_myAvatarDir, $recursive);
?>
', $after = '') {
global $post, $gkl_AvatarURL, $gkl_myAvatarDir;
$post_id = $post->ID;
$CurrAvatar = get_post_meta($post_id,'postuserpic');
$CheckAvatar = $gkl_myAvatarDir . $CurrAvatar[0];
// Display nothing if value is empty or file does not exist
if ( empty($CurrAvatar) || !file_exists($CheckAvatar) ) {
} else {
// Show post avatar
$post_title = sanitize_title($post->post_title);
$CurrAvatarLoc = $gkl_AvatarURL . $CurrAvatar[0];
if ( $CurrAvatarLoc != $gkl_AvatarURL ) {
$CurrAvatarLoc = str_replace('/', DIRECTORY_SEPARATOR, $gkl_myAvatarDir . ltrim($CurrAvatar[0],'/'));
$dim = getimagesize("$CurrAvatarLoc");
$width = $dim[0];
$height = $dim[1];
$CurrAvatarLoc = $gkl_AvatarURL . ltrim($CurrAvatar[0],'/');
if (empty($before) && empty($after) && !empty($class))
echo ''."\n";
else
echo $before .''. $after ."\n";
}
}
}
/**
* Create Options Page
*
*/
function postavatar_admin() {
if ( function_exists('add_options_page') ) {
add_options_page('Post Avatar Options', 'Post Avatar', 8, basename(__FILE__), 'postavatar_admin_form');
}
}
/**
* Manage Options Form
*
*/
function postavatar_admin_form() {
// Add default Post Avatar settings
add_option('gklpa_mydir', 'wp-content/uploads/icons/', 'Location of images folder', 'yes');
add_option('gklpa_showinwritepage', 1, 'Show image in Write Post Page?', 'yes');
add_option('gklpa_scanrecursive', 1, 'Recursive scan of the images?', 'yes');
add_option('gklpa_showincontent', 1, 'Show avatar with post content', 'yes');
$gklpa_mydir = get_option('gklpa_mydir');
$gklpa_showinwritepage = get_option('gklpa_showinwritepage');
$gklpa_scanrecursive = get_option('gklpa_scanrecursive');
$gklpa_showincontent = get_option('gklpa_showincontent');
// Update Post Avatar settings
if ( isset($_POST['submit']) ) {
$gklpa_mydir = trailingslashit(rtrim($_POST['gklpa_mydir'], '/'));
$gklpa_showinwritepage = $_POST['gklpa_showinwritepage'];
$gklpa_scanrecursive = $_POST['gklpa_scanrecursive'];
$gklpa_showincontent = $_POST['gklpa_showincontent'];
update_option('gklpa_mydir', $gklpa_mydir);
update_option('gklpa_showinwritepage', $gklpa_showinwritepage);
update_option('gklpa_scanrecursive', $gklpa_scanrecursive);
update_option('gklpa_showincontent', $gklpa_showincontent);
}
?>
so it's easier to extend the script, e.g. with slideshow-effects
?>
';
}
/*
* Filter to include post avatar in the_content()
*
* @param text $content
* @return text $content
*/
function gkl_postavatar_filter($content) {
global $post, $gkl_AvatarURL, $gkl_myAvatarDir, $wp_query;
if (!$wp_query->is_feed)
gkl_postavatar();
return $content;
}
// Hook it up
add_action('admin_menu', 'postavatar_admin');
add_action('simple_edit_form', 'gkl_avatar_insert');
add_action('edit_form_advanced','gkl_avatar_insert');
add_action('edit_page_form','gkl_avatar_insert');
add_action('edit_post', 'gkl_avatar_edit');
add_action('save_post', 'gkl_avatar_edit');
add_action('publish_post', 'gkl_avatar_edit');
add_action('admin_head', 'gkl_admin_head');
// Display avatar without template tag
if ($gkl_ShowInContent == 1){
add_filter('the_content', 'gkl_postavatar_filter');
add_filter('wp_head', 'gkl_postavatar_showcss');
}
?>