ID, '_wam_show_attachments', true) == 'true'); $dont_show_on_excerpts = (get_option('wam_dont_show_on_excerpts') == 'true'); $is_excerpt = wpAttachmentManager::excerpt_static(); //check if we exclude excerpts if ( ($list_on_option == 'all' || ($list_on_option == 'some' && $this_post)) && (!$dont_show_on_cats || !is_category()) && (!$dont_show_on_excerpts || !$is_excerpt) ) { $post_content .= wpAttachmentManager::get_attachments(); } if (WAM_DEBUG) { var_dump(wpAttachmentManager::excerpt_static()); } return $post_content; } /** * Finds the attachments that belong to the current post, and creates an * unordered list of them * * @return string - unordered list of attachments */ function get_attachments() { global $wpdb, $post; $dont_show_linked = (get_option('wam_dont_show_files_already_linked') == 'true'); $attachment_str = ''; $debug = ''; $query = "SELECT `guid`, `post_content`, `post_title` FROM {$wpdb->posts} WHERE (post_status = 'attachment' || post_type = 'attachment') AND post_parent = '{$post->ID}'"; $attachments = $wpdb->get_results($query); if (count($attachments) > 0) { $attachment_list = array(); foreach ( $attachments as $attachment ) { if (!$dont_show_linked || (strpos($post->post_content, $attachment->guid)===false)) { $link = wpAttachmentManager::get_attachment_link($attachment); $attachment_list[] = "{$link}

{$attachment->post_content}

"; if (WAM_DEBUG) { $debug .= '
'.print_r($attachment, true).'
'; } } } if (count($attachment_list) > 0) { $attachment_str = '

'.__('Attached Files:').'

'; } } return $attachment_str.$debug; } /** * Returns a link to a file, including an icon if needed. * * @param object $attachment - wp-attachment object * @return string - link to attachment */ function get_attachment_link($attachment) { $link = "guid); $exts = wpAttachmentManager::get_exts(); $img = (isset($exts[$ext]))? $exts[$ext]:get_option('wam_default_icon'); if (!empty($img)) { $icon_dir = wpAttachmentManager::get_icon_dir().'/'; $image_size = getimagesize("{$icon_dir}/{$img}"); $lp = $image_size[0]+4; $height = $image_size[1]; $siteurl = get_settings('siteurl').'/'; $link .= ' class="wam_icon_link"'; $link .= " style='padding-left:{$lp}px; line-height:{$height}px; background-image:url(\"{$siteurl}{$icon_dir}{$img}\");'"; } } $link .= ">{$attachment->post_title}"; return $link; } /** * This attaches to init, and was needed so we could use wp_redirect. It * checks to see if an icon is has been requested to be removed. Then it * removes it, and redirects back to the options page, and gives a succes or * error message */ function handle_actions() { if (isset($_GET['page']) && $_GET['page'] == 'attachment_manager') { if ( isset($_GET['action']) ) { if ('remove' == $_GET['action']) { $exts = wpAttachmentManager::get_exts(); $exts = array_diff($exts, array($_GET['icon'])); update_option('wam_exts', $exts); $icon_dir = wpAttachmentManager::get_icon_dir().'/'; if (is_writable(ABSPATH.$icon_dir.$_GET['icon']) && @unlink(ABSPATH.$icon_dir.$_GET['icon'])) { wp_redirect('options-general.php?page=attachment_manager&remove=true'); } else { wp_redirect('options-general.php?page=attachment_manager&remove=false'); } } exit; } elseif (isset($_FILES['wam_add_icon'])) { $file = ABSPATH.wpAttachmentManager::get_icon_dir().'/'.basename($_FILES['wam_add_icon']['name']); if (move_uploaded_file($_FILES['wam_add_icon']['tmp_name'], $file)) { wp_redirect('options-general.php?page=attachment_manager&upload=true'); } else { wp_redirect('options-general.php?page=attachment_manager&upload=false'); } } } } /** * This is used to attach the JS and CSS needed for the options page */ function config_head() { if (isset($_GET['page']) && $_GET['page'] == 'attachment_manager') { wpAttachmentManager::css(); ?> $ext) { $_POST['icon_file_types'][$key] = trim(preg_replace('/[^\w-]/', '', $ext)); } update_option('icon_file_types', $_POST['icon_file_types']); $_POST['wam_list_on_posts'] = strtolower($_POST['wam_list_on_posts']); update_option('wam_list_on_posts', $_POST['wam_list_on_posts']); $_POST['wam_dont_show_files_already_linked'] = (isset($_POST['wam_dont_show_files_already_linked']) && strtolower($_POST['wam_dont_show_files_already_linked']) == 'true')? 'true':'false'; update_option('wam_dont_show_files_already_linked', $_POST['wam_dont_show_files_already_linked']); $_POST['wam_dont_show_on_excerpts'] = (isset($_POST['wam_dont_show_on_excerpts']) && strtolower($_POST['wam_dont_show_on_excerpts']) == 'true')? 'true':'false'; update_option('wam_dont_show_on_excerpts', $_POST['wam_dont_show_on_excerpts']); $_POST['wam_dont_show_on_cat_page'] = (isset($_POST['wam_dont_show_on_cat_page']) && strtolower($_POST['wam_dont_show_on_cat_page']) == 'true')? 'true':'false'; update_option('wam_dont_show_on_cat_page', $_POST['wam_dont_show_on_cat_page']); $_POST['wam_show_file_icons'] = (isset($_POST['wam_show_file_icons']) && strtolower($_POST['wam_show_file_icons']) == 'true')? 'true':'false'; update_option('wam_show_file_icons', $_POST['wam_show_file_icons']); $_POST['wam_default_icon'] = (!isset($_POST['wam_default_icon']))? '':$_POST['wam_default_icon']; update_option('wam_default_icon', $_POST['wam_default_icon']); $ext_setting = array(); foreach ($_POST['icons'] as $icon_info) { if (!empty($icon_info['exts'])) { $ext_array = explode(',', $icon_info['exts']); foreach ($ext_array as $ext) { $ext = trim(strtolower(preg_replace('/[^\w-]/', '', $ext))); $ext_setting[$ext] = $icon_info['icon']; } } } update_option('wam_exts', $ext_setting); } $siteurl = get_settings('siteurl').'/'; $plugin_dir = wpAttachmentManager::get_plugin_dir(); $icon_dir = wpAttachmentManager::get_icon_dir(); $icon_file_types = wpAttachmentManager::get_icon_filetypes(); $debug .= "
icon_dir: ".print_r(ABSPATH.$icon_dir, true)."\r\nIs icon_dir writable: ".print_r(is_writable(ABSPATH.$icon_dir), true).'
'; $debug .= "
\$_POST:\r\n".print_r($_POST, true).'
'; $debug .= "
\$_FILES:\r\n".print_r($_FILES, true).'
'; if ( is_writable(ABSPATH.$icon_dir) ) { $writable = true; } else { $writable = false; } if (isset($_GET['remove'])) { switch ($_GET['remove']) { case 'true': $message = __('Icon removed.'); $class = 'updated'; break; default: $message = __('Problem removing icon.'); $class = 'error'; } echo "

{$message}

"; } if (isset($_GET['upload'])) { switch ($_GET['upload']) { case 'true': $message = __('Icon uploaded.'); $class = 'updated'; break; default: $message = __('Problem uploading icon.'); $class = 'error'; } echo "

{$message}

"; } $exts = wpAttachmentManager::get_exts(); $wam_list_on_posts = get_option('wam_list_on_posts'); $wam_show_file_icons = get_option('wam_show_file_icons'); $wam_dont_show_files_already_linked = get_option('wam_dont_show_files_already_linked'); $wam_dont_show_on_excerpts = get_option('wam_dont_show_on_excerpts'); $wam_dont_show_on_cat_page = get_option('wam_dont_show_on_cat_page'); $icon_files = array(); $h = opendir(ABSPATH.$icon_dir); while (($filename = readdir($h)) !== false) { if (in_array(wpAttachmentManager::get_extension($filename), $icon_file_types)) { $icon_files[] = $filename; } } sort($icon_files); $icons[0] = array_slice($icon_files, 0, ceil(count($icon_files)/2)); $icons[1] = array_slice($icon_files, ceil(count($icon_files)/2)); ?>

writable, you would be able to add and remove icons!') ?>

(Relative to the Wordpress install directory):'); ?>
(Relative to the Wordpress install directory):'); ?>
(Comma Seperated):'); ?>

Options

Show file lists on:










Icons \r\n"; $ta_name = preg_replace('/[^\w-]/', '', $icons[0][$i]); $img_src = "{$siteurl}{$icon_dir}/{$icons[0][$i]}"; $img_size = getimagesize(ABSPATH.$icon_dir.'/'.$icons[0][$i]); $img_alt = htmlentities($icons[0][$i]); $extensions = array_keys($exts, $icons[0][$i]); sort($extensions); $ta_content = implode(',', $extensions); $default = (get_option('wam_default_icon') == $icons[0][$i])?' checked="checked"':''; echo " \r\n"; echo " \r\n"; echo " \r\n"; echo " \r\n"; if (isset($icons[1][$i])) { $ta_name = preg_replace('/[^\w-]/', '', $icons[1][$i]); $img_src = "{$siteurl}{$icon_dir}/{$icons[1][$i]}"; $img_size = getimagesize(ABSPATH.$icon_dir.'/'.$icons[1][$i]); $img_alt = htmlentities($icons[1][$i]); $extensions = array_keys($exts, $icons[1][$i]); sort($extensions); $ta_content = implode(',', $extensions); $default = (get_option('wam_default_icon') == $icons[1][$i])?' checked="checked"':''; echo " \r\n"; echo " \r\n"; echo " \r\n"; } else { echo " \r\n"; echo " \r\n"; echo " \r\n"; } echo " \r\n"; } $default = (get_option('wam_default_icon') == '')?' checked="checked"':''; ?>
Icon Options Associated File Types (comma seperated)   Icon Options Associated File Types (comma seperated)
{$img_alt}"; echo "
"; if ($writable) { echo "".__('Remove').""; } echo "
 {$img_alt}"; echo "
"; if ($writable) { echo "".__('Remove').""; } echo "
   

Add Icon

$plugin) { if ($plugin['Name'] == 'Attachment Manager' && strip_tags($plugin['Author']) == 'Aaron D. Campbell') { $plugin_loc .= dirname($plugin_file); break; } } } update_option('wam_plugin_dir', $plugin_loc); } return $plugin_loc; } /** * Either returns the icon directory from the plugin options, or gets the * plugin directory and adds /icons (the default) * * @return string - icon directory */ function get_icon_dir() { $icon_loc = get_option('wam_icon_dir'); if ($icon_loc == false) { $icon_loc = wpAttachmentManager::get_plugin_dir().'/icons'; update_option('wam_icon_dir', $icon_loc); } return $icon_loc; } /** * Either returns the icon file types from the plugin options, or returns the * default (jpg, jpeg, gif, and png) * * @return array - icon file types */ function get_icon_filetypes() { $icon_file_types = get_option('icon_file_types'); if ($icon_file_types == false) { $icon_file_types = array('jpg', 'jpeg', 'gif', 'png'); update_option('icon_file_types', $icon_file_types); } return $icon_file_types; } /** * Returns the extentions array from the plugin options. Formatted like: * $arr[ext] = 'icon_file_name.png'; * * @return array - extensions array */ function get_exts() { $exts = get_option('wam_exts'); if (!is_array($exts)) { $exts = wpAttachmentManager::get_default_ext_array(); update_option('wam_exts', $exts); } if (WAM_DEBUG) { echo '
',var_dump($exts),'
'; } return $exts; } /** * Echos the link to the plugin stylesheet */ function css() { $siteurl = get_option('siteurl'); $css_file = "{$siteurl}/".wpAttachmentManager::get_plugin_dir().'/css/wp-attachment-manager.css'; echo ""; } /** * Used to get the default extensions array */ function get_default_ext_array() { return unserialize('a:99:{s:3:"css";s:7:"css.png";s:3:"eml";s:9:"email.png";s:3:"rss";s:8:"feed.png";s:1:"h";s:16:"page_white_h.png";s:3:"avi";s:8:"film.png";s:3:"mov";s:8:"film.png";s:3:"mp4";s:8:"film.png";s:3:"mpg";s:8:"film.png";s:2:"qt";s:8:"film.png";s:2:"rm";s:8:"film.png";s:3:"wmv";s:8:"film.png";s:3:"chm";s:8:"help.png";s:3:"mdb";s:18:"page_white_key.png";s:3:"htm";s:8:"html.png";s:4:"html";s:8:"html.png";s:3:"sht";s:8:"html.png";s:4:"shtm";s:8:"html.png";s:5:"shtml";s:8:"html.png";s:3:"aac";s:9:"music.png";s:3:"aif";s:9:"music.png";s:3:"mid";s:9:"music.png";s:4:"midi";s:9:"music.png";s:3:"mp3";s:9:"music.png";s:3:"mpa";s:9:"music.png";s:2:"ra";s:9:"music.png";s:3:"ram";s:9:"music.png";s:3:"wav";s:9:"music.png";s:3:"wma";s:9:"music.png";s:4:"flac";s:9:"music.png";s:3:"ogg";s:9:"music.png";s:3:"pdf";s:22:"page_white_acrobat.png";s:2:"as";s:27:"page_white_actionscript.png";s:1:"c";s:16:"page_white_c.png";s:3:"raw";s:21:"page_white_camera.png";s:3:"inc";s:18:"page_white_php.png";s:3:"php";s:18:"page_white_php.png";s:4:"php4";s:18:"page_white_php.png";s:4:"php5";s:18:"page_white_php.png";s:4:"phps";s:18:"page_white_php.png";s:5:"phtml";s:18:"page_white_php.png";s:3:"tpl";s:18:"page_white_php.png";s:3:"bmp";s:22:"page_white_picture.png";s:3:"gif";s:22:"page_white_picture.png";s:4:"jpeg";s:22:"page_white_picture.png";s:3:"jpg";s:22:"page_white_picture.png";s:3:"png";s:22:"page_white_picture.png";s:3:"psd";s:22:"page_white_picture.png";s:2:"js";s:23:"page_white_code_red.png";s:3:"ppt";s:25:"page_white_powerpoint.png";s:3:"cfm";s:25:"page_white_coldfusion.png";s:4:"cfml";s:25:"page_white_coldfusion.png";s:2:"bz";s:25:"page_white_compressed.png";s:3:"bz2";s:25:"page_white_compressed.png";s:3:"cab";s:25:"page_white_compressed.png";s:4:"gtar";s:25:"page_white_compressed.png";s:2:"gz";s:25:"page_white_compressed.png";s:4:"gzip";s:25:"page_white_compressed.png";s:3:"rar";s:25:"page_white_compressed.png";s:3:"tar";s:25:"page_white_compressed.png";s:6:"tar-gz";s:25:"page_white_compressed.png";s:3:"tgz";s:25:"page_white_compressed.png";s:3:"war";s:25:"page_white_compressed.png";s:3:"zip";s:25:"page_white_compressed.png";s:2:"rb";s:19:"page_white_ruby.png";s:3:"rbs";s:19:"page_white_ruby.png";s:5:"rhtml";s:19:"page_white_ruby.png";s:3:"cpp";s:24:"page_white_cplusplus.png";s:2:"cs";s:21:"page_white_csharp.png";s:5:"class";s:18:"page_white_cup.png";s:3:"jad";s:18:"page_white_cup.png";s:3:"jar";s:18:"page_white_cup.png";s:3:"jav";s:18:"page_white_cup.png";s:4:"java";s:18:"page_white_cup.png";s:3:"rdf";s:19:"page_white_text.png";s:3:"txt";s:19:"page_white_text.png";s:3:"sql";s:23:"page_white_database.png";s:4:"conf";s:18:"page_white_tux.png";s:2:"ai";s:21:"page_white_vector.png";s:3:"svg";s:21:"page_white_vector.png";s:3:"xls";s:20:"page_white_excel.png";s:3:"doc";s:19:"page_white_word.png";s:3:"fla";s:20:"page_white_flash.png";s:3:"swf";s:20:"page_white_flash.png";s:2:"fh";s:23:"page_white_freehand.png";s:4:"fh10";s:23:"page_white_freehand.png";s:3:"fh3";s:23:"page_white_freehand.png";s:3:"fh4";s:23:"page_white_freehand.png";s:3:"fh5";s:23:"page_white_freehand.png";s:3:"fh6";s:23:"page_white_freehand.png";s:3:"fh7";s:23:"page_white_freehand.png";s:3:"fh8";s:23:"page_white_freehand.png";s:3:"fh9";s:23:"page_white_freehand.png";s:3:"dtd";s:19:"page_white_gear.png";s:3:"tld";s:19:"page_white_gear.png";s:4:"wsdl";s:19:"page_white_gear.png";s:3:"xml";s:19:"page_white_gear.png";s:3:"xsd";s:19:"page_white_gear.png";s:3:"xsl";s:19:"page_white_gear.png";s:5:"xhtml";s:9:"xhtml.png";}'); } /** * Displays the "show attachments" checkbox if wam_list_on_posts is set to some */ function post_form() { if (get_option('wam_list_on_posts') == 'some') { global $post; $checked = (get_post_meta($post->ID, '_wam_show_attachments', true) == 'true')? ' checked="checked"':''; echo "

"; } } /** * Adds or removes the show_attachments meta from the post * * @param int $pid - Post ID */ function handle_save_post($pid) { if (isset($_POST['wam_show_attachments']) && strtolower($_POST['wam_show_attachments']) == 'true') { add_post_meta($pid, '_wam_show_attachments', 'true', true); } else { delete_post_meta($pid, '_wam_show_attachments'); } } /** * This is used to set some default values when the plugin is activated. */ function on_activate() { update_option('wam_list_on_posts', 'all'); update_option('wam_show_file_icons', 'true'); update_option('wam_dont_show_on_excerpts', 'true'); wpAttachmentManager::get_exts(); } /** * This is called if a post is in excerpt. It uses self::excerpt_static to * set a static variable to true, so we can test for it later. */ function check_excerpt($theExcerpt) { if (WAM_DEBUG) { echo "excerpt = true\r\n"; } wpAttachmentManager::excerpt_static(true); return $theExcerpt; } /** * This is a lame PHP4 hack since it doesn't support static class variables. * One of these days I'm just going to say "screw people that won't upgrade" * and do ONLY PHP5 * * @param bool $set[optional] - What to set the static var to * @return bool - value of static var */ function excerpt_static($set = null) { static $excerpt = false; if (!is_null($set)) { $excerpt = $set; } return $excerpt; } } /** * Add filters and actions */ //We have to set the priority of the excerpt check to one, or it will run AFTER the post handler add_filter('get_the_excerpt', array('wpAttachmentManager','check_excerpt'), 1); add_filter('the_content', array('wpAttachmentManager','attach_to_post')); add_action('admin_menu', array('wpAttachmentManager','admin_menu')); add_action('admin_head', array('wpAttachmentManager','config_head')); add_action('init', array('wpAttachmentManager','handle_actions')); add_action('wp_head', array('wpAttachmentManager','css')); add_action('edit_form_advanced', array('wpAttachmentManager','post_form')); add_action('save_post', array('wpAttachmentManager','handle_save_post')); register_activation_hook(__FILE__, array('wpAttachmentManager','on_activate'));