_hook = 'attachment_manager'; $this->_file = plugin_basename( __FILE__ ); $this->_pageTitle = __( 'Attachment Manager Options', $this->_slug ); $this->_menuTitle = __( 'Attachment Manager', $this->_slug ); $this->_accessLevel = 'manage_options'; $this->_optionGroup = 'wam-options'; $this->_optionNames = array( 'wam', 'wam_icon_dir', 'icon_file_types', 'wam_list_on_posts', 'wam_dont_show_files_already_linked', 'wam_dont_show_on_excerpts', 'wam_dont_show_on_cat_page', 'wam_show_file_icons', 'wam_default_icon', 'icons' ); $this->_optionCallbacks = array(); $this->_slug = 'attachment-manager'; $this->_paypalButtonId = '11106634'; /** * 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($this,'check_excerpt'), 1); add_filter('the_content', array($this,'attach_to_post')); add_action('init', array($this,'handle_actions')); add_action('save_post', array($this,'handle_save_post')); register_activation_hook(__FILE__, array($this,'on_activate')); } private function _get_icon_url() { $siteurl = rtrim( get_option( 'siteurl' ), '/' ); return str_replace(rtrim(ABSPATH, '/'), $siteurl, $this->_settings['wam_icon_dir']); } public function _postSettingsInit() { if ( empty($this->_settings['icons']) || !is_array( $this->_settings['icons'] )) { $this->_settings['icons'] = unserialize('a:33:{s:6:"csspng";a:2:{s:4:"icon";s:7:"css.png";s:4:"exts";s:3:"css";}s:8:"emailpng";a:2:{s:4:"icon";s:9:"email.png";s:4:"exts";s:3:"eml";}s:7:"feedpng";a:2:{s:4:"icon";s:8:"feed.png";s:4:"exts";s:3:"rss";}s:15:"page_white_hpng";a:2:{s:4:"icon";s:16:"page_white_h.png";s:4:"exts";s:1:"h";}s:7:"filmpng";a:2:{s:4:"icon";s:8:"film.png";s:4:"exts";s:25:"avi,mov,mp4,mpg,qt,rm,wmv";}s:7:"helppng";a:2:{s:4:"icon";s:8:"help.png";s:4:"exts";s:3:"chm";}s:17:"page_white_keypng";a:2:{s:4:"icon";s:18:"page_white_key.png";s:4:"exts";s:3:"mdb";}s:7:"htmlpng";a:2:{s:4:"icon";s:8:"html.png";s:4:"exts";s:23:"htm,html,sht,shtm,shtml";}s:8:"musicpng";a:2:{s:4:"icon";s:9:"music.png";s:4:"exts";s:48:"aac,aif,mid,midi,mp3,mpa,ra,ram,wav,wma,flac,ogg";}s:21:"page_white_acrobatpng";a:2:{s:4:"icon";s:22:"page_white_acrobat.png";s:4:"exts";s:3:"pdf";}s:26:"page_white_actionscriptpng";a:2:{s:4:"icon";s:27:"page_white_actionscript.png";s:4:"exts";s:2:"as";}s:15:"page_white_cpng";a:2:{s:4:"icon";s:16:"page_white_c.png";s:4:"exts";s:1:"c";}s:20:"page_white_camerapng";a:2:{s:4:"icon";s:21:"page_white_camera.png";s:4:"exts";s:3:"raw";}s:17:"page_white_phppng";a:2:{s:4:"icon";s:18:"page_white_php.png";s:4:"exts";s:32:"inc,php,php4,php5,phps,phtml,tpl";}s:21:"page_white_picturepng";a:2:{s:4:"icon";s:22:"page_white_picture.png";s:4:"exts";s:24:"bmp,gif,jpeg,jpg,png,psd";}s:22:"page_white_code_redpng";a:2:{s:4:"icon";s:23:"page_white_code_red.png";s:4:"exts";s:2:"js";}s:24:"page_white_powerpointpng";a:2:{s:4:"icon";s:25:"page_white_powerpoint.png";s:4:"exts";s:3:"ppt";}s:24:"page_white_coldfusionpng";a:2:{s:4:"icon";s:25:"page_white_coldfusion.png";s:4:"exts";s:8:"cfm,cfml";}s:24:"page_white_compressedpng";a:2:{s:4:"icon";s:25:"page_white_compressed.png";s:4:"exts";s:50:"bz,bz2,cab,gtar,gz,gzip,rar,tar,tar-gz,tgz,war,zip";}s:18:"page_white_rubypng";a:2:{s:4:"icon";s:19:"page_white_ruby.png";s:4:"exts";s:12:"rb,rbs,rhtml";}s:23:"page_white_cpluspluspng";a:2:{s:4:"icon";s:24:"page_white_cplusplus.png";s:4:"exts";s:3:"cpp";}s:20:"page_white_csharppng";a:2:{s:4:"icon";s:21:"page_white_csharp.png";s:4:"exts";s:2:"cs";}s:17:"page_white_cuppng";a:2:{s:4:"icon";s:18:"page_white_cup.png";s:4:"exts";s:22:"class,jad,jar,jav,java";}s:18:"page_white_textpng";a:2:{s:4:"icon";s:19:"page_white_text.png";s:4:"exts";s:7:"rdf,txt";}s:22:"page_white_databasepng";a:2:{s:4:"icon";s:23:"page_white_database.png";s:4:"exts";s:3:"sql";}s:17:"page_white_tuxpng";a:2:{s:4:"icon";s:18:"page_white_tux.png";s:4:"exts";s:4:"conf";}s:20:"page_white_vectorpng";a:2:{s:4:"icon";s:21:"page_white_vector.png";s:4:"exts";s:6:"ai,svg";}s:19:"page_white_excelpng";a:2:{s:4:"icon";s:20:"page_white_excel.png";s:4:"exts";s:3:"xls";}s:18:"page_white_wordpng";a:2:{s:4:"icon";s:19:"page_white_word.png";s:4:"exts";s:3:"doc";}s:19:"page_white_flashpng";a:2:{s:4:"icon";s:20:"page_white_flash.png";s:4:"exts";s:7:"fla,swf";}s:22:"page_white_freehandpng";a:2:{s:4:"icon";s:23:"page_white_freehand.png";s:4:"exts";s:35:"fh,fh10,fh3,fh4,fh5,fh6,fh7,fh8,fh9";}s:18:"page_white_gearpng";a:2:{s:4:"icon";s:19:"page_white_gear.png";s:4:"exts";s:24:"dtd,tld,wsdl,xml,xsd,xsl";}s:8:"xhtmlpng";a:2:{s:4:"icon";s:9:"xhtml.png";s:4:"exts";s:5:"xhtml";}}'); update_option('icons', $this->_settings['icons']); } if ( empty($this->_settings['wam_icon_dir']) ) { $this->_settings['wam_icon_dir'] = $this->_icon_dir = plugin_dir_path(__FILE__) . 'icons'; } else { $this->_icon_dir = $this->_settings['wam_icon_dir']; } /** * Default settings for the wam array */ $default_settings = array( 'home_page' => 'hide', 'show_on_rss' => '' ); $this->_settings['wam'] = wp_parse_args( $this->_settings['wam'], $default_settings ); $this->_icon_dir_is_writable = is_writable($this->_icon_dir); if (isset($_GET['remove'])) { switch ($_GET['remove']) { case 'true': $message = __('Icon removed.', $this->_slug); $class = 'updated'; break; default: $message = __('Problem removing icon.', $this->_slug); $class = 'error'; } $notice = str_replace( "'", "\'", "

{$message}

" ); add_action('admin_notices', create_function( '', "echo '$notice';" ) ); } if (isset($_GET['upload'])) { switch ($_GET['upload']) { case 'true': $message = __('Icon uploaded.', $this->_slug); $class = 'updated'; break; default: $message = __('Problem uploading icon.', $this->_slug); $class = 'error'; } $notice = str_replace( "'", "\'", "

{$message}

" ); add_action('admin_notices', create_function( '', "echo '$notice';" ) ); } if ($this->_settings['wam_list_on_posts'] == 'some') { add_action('admin_menu', array($this, 'adminMenu')); } } /** * Function to instantiate our class and make it a singleton */ public static function getInstance() { if ( !self::$instance ) { self::$instance = new self; } return self::$instance; } public function addOptionsMetaBoxes() { add_meta_box( $this->_slug . '-general-settings', __('General Settings', $this->_slug), array($this, 'generalSettingsMetaBox'), 'xavisys-' . $this->_slug, 'main'); add_meta_box( $this->_slug . '-icons', __('Icons', $this->_slug), array($this, 'iconsMetaBox'), 'xavisys-' . $this->_slug, 'main'); if ($this->_icon_dir_is_writable) { add_meta_box( $this->_slug . '-upload-icons', __('Upload New Icon', $this->_slug), array($this, 'uploadIconPostsMetaBox'), 'xavisys-' . $this->_slug, 'main-2'); } } public function generalSettingsMetaBox() { ?>
_slug); ?> _settings['wam_list_on_posts']); ?> />
_settings['wam_list_on_posts']); ?> />
_settings['wam_list_on_posts']); ?> />

_settings['wam']['home_page'] ); ?> />
_settings['wam']['home_page'] ); ?> />

_settings['wam']['show_on_rss']); ?> />

_settings['wam_dont_show_files_already_linked']); ?> />
_settings['wam_dont_show_on_excerpts']); ?> />
_settings['wam_dont_show_on_cat_page']); ?> />
_settings['wam_show_file_icons']); ?> onclick="check_show_icons();" /> "Silk" by famfamfam', $this->_slug), 'http://famfamfam.com/lab/icons/silk/'); ?>
_get_icon_filetypes(); $icon_files = array(); $icon_file_types_arr = preg_split('/\s*,\s*/', $icon_file_types); $h = opendir($this->_icon_dir); while (($filename = readdir($h)) !== false) { if (in_array($this->_get_extension($filename), $icon_file_types_arr)) { $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)); ?> "; $this->_get_icon_cells($icons[0][$i], true); if (isset($icons[1][$i])) { $this->_get_icon_cells($icons[1][$i]); } echo ""; } $default = (get_option('wam_default_icon') == '')?' checked="checked"':''; ?>
_slug); ?> _slug); ?> _slug); ?>   _slug); ?> _slug); ?> _slug); ?>
/>
_get_icon_url() . '/' . $icon; $img_size = getimagesize($this->_icon_dir.'/'.$icon); $img_alt = htmlentities($icon); $img_alt = htmlentities($icon); $extensions = preg_split( '/\s*,\s*/', $this->_settings['icons'][$ta_name]['exts'] ); sort($extensions); $ta_content = implode(',', $extensions); $default = (get_option('wam_default_icon') == $icon)?' checked="checked"':''; echo "{$img_alt}"; echo ""; echo ""; echo "
'; if ($this->_icon_dir_is_writable) { echo "_slug)."' class='delete'>".__('Remove', $this->_slug).""; } echo ""; echo ""; if ( $add_blank_cell ) { echo " "; } } else { $num_cells = ($add_blank_cell)? 4:3; echo str_repeat(" ", $num_cells); } } public function uploadIconPostsMetaBox() { ?>

_is_static to * true, so we can test for it later. */ public function check_excerpt($theExcerpt) { $this->_is_excerpt = true; return $theExcerpt; } /** * This function is set as a filter on post content, and adds the attachment * list if needed. * * @param string $post_content - Content of the current post * @return string - Modified post content */ public function attach_to_post($post_content) { global $post; $dont_show_on_cats = ($this->_settings['wam_dont_show_on_cat_page'] == 'true'); $this_post = (get_post_meta($post->ID, '_wam_show_attachments', true) == 'true'); $dont_show_on_excerpts = (($this->_settings['wam_dont_show_on_excerpts'] == 'true') && !is_feed()); $show_on_home_page = (($this->_settings['wam']['home_page'] == 'show') || is_feed()); /** * If we show on all posts or this post AND if we show on categories or * this isn't a category AND if we show on excerpt or this isn't an * excerpt */ if ( ($this->_settings['wam_list_on_posts'] == 'all' || ($this->_settings['wam_list_on_posts'] == 'some' && $this_post)) && (!$dont_show_on_cats || !is_category()) && ( !$dont_show_on_excerpts || !$this->_is_excerpt ) && ( 'true' == $this->_settings['wam']['show_on_rss'] || !is_feed() ) && ( $show_on_home_page || !is_home() ) ) { $post_content .= $this->_get_attachments(); } return $post_content; } public function attach_to_rss($post_content) { $this->_is_excerpt = false; return $this->attach_to_post($post_content); } /** * Finds the attachments that belong to the current post, and creates an * unordered list of them * * @return string - unordered list of attachments */ private function _get_attachments() { global $wpdb, $post; $dont_show_linked = (get_option('wam_dont_show_files_already_linked') == 'true'); $attachment_str = ''; $query = "SELECT `id`,`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 || (!$this->_isFileLinked($post->post_content, $attachment) )) { $link = $this->_get_attachment_link($attachment); $post_content = empty($attachment->post_content)? '':"

{$attachment->post_content}

"; $attachment_list[] = $link.$post_content; } } if (count($attachment_list) > 0) { $attachment_str = '

'.__('Attached Files:', $this->_slug).'

'; } } return $attachment_str; } private function _isFileLinked($content, $file) { $pathinfo = pathinfo($file->guid); $filename = preg_quote($pathinfo['dirname'].'/'.basename($pathinfo['filename'], '.'.$pathinfo['basename']), '/'); $ext = preg_quote($pathinfo['extension']); return ( preg_match("/src=['\"]{$filename}(-\d*x\d*)?\.{$ext}['\"]/", $content) || preg_match("/class=['\"][^'\"]*wp-image-{$file->id}[^'\"]*['\"]/", $content) ); } /** * Returns a link to a file, including an icon if needed. * * @param object $attachment - wp-attachment object * @return string - link to attachment */ private function _get_attachment_link($attachment) { $icon = ''; if ($this->_settings['wam_show_file_icons'] == 'true') { $ext = $this->_get_extension($attachment->guid); $img = $this->_settings['wam_default_icon']; foreach( $this->_settings['icons'] as $cur_icon ) { $cur_icon['exts'] = preg_split('/\s*,\s*/', $cur_icon['exts']); if ( in_array($ext, $cur_icon['exts']) ) { $img = $cur_icon['icon']; break; } } if (!empty($img)) { $icon_url = path_join($this->_get_icon_url(),$img); $img_size = getimagesize(path_join($this->_icon_dir,$img)); $img_alt = esc_html($ext); $icon = "{$img_alt} "; } } return sprintf("%s{$attachment->post_title}", $icon); } /** * 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 */ public function handle_actions() { if (isset($_GET['page']) && $_GET['page'] == 'attachment_manager') { if ( isset($_GET['action']) ) { if ('remove' == $_GET['action']) { unset( $this->_settings['icons'][ preg_replace( '/[^\w-]/', '', $_GET['icon'] ) ] ); update_option('icons', $this->_settings['icons']); if (is_writable($this->_icon_dir.'/'.$_GET['icon']) && @unlink($this->_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'])) { check_admin_referer( 'wam_add_icon' ); /** WordPress Administration File API */ require_once( ABSPATH . 'wp-admin/includes/file.php' ); add_filter( 'upload_dir', array( $this, 'upload_dir' ) ); $overrides = array( 'test_form' => false ); $uploaded = wp_handle_upload( $_FILES['wam_add_icon'], $overrides ); remove_filter( 'upload_dir', array( $this, 'upload_dir' ) ); if ( empty( $uploaded['error'] ) ) { wp_redirect('options-general.php?page=attachment_manager&upload=true'); } else { wp_redirect('options-general.php?page=attachment_manager&upload=false'); } } } } public function upload_dir( $upload_dir ) { $upload_dir['path'] = $this->_icon_dir; return $upload_dir; } /** * Used to find the file extension of any file (given a filename.ext, * path/to/filename.ext, or even http://example.com/path/to/filename.ext * * @param string $file_name * @return string - lowercase file extension */ private function _get_extension($file_name) { $file = pathinfo($file_name); return strtolower($file['extension']); } /** * Either returns the icon file types from the plugin options, or returns the * default (jpg, jpeg, gif, and png) * * @return array - icon file types */ private function _get_icon_filetypes() { $icon_file_types = get_option('icon_file_types'); if ($icon_file_types == false || empty($icon_file_types)) { $icon_file_types = 'jpg, jpeg, gif, png'; update_option('icon_file_types', $icon_file_types); } elseif (is_array($icon_file_types)) { $icon_file_types = implode(',', $icon_file_types); update_option('icon_file_types', $icon_file_types); } return $icon_file_types; } /** * Add boxes to the "edit post" and "edit page" pages */ public function adminMenu() { add_meta_box($this->_slug, __('Attachment Manager'), array($this, 'post_form'), 'post', 'normal', 'high'); add_meta_box($this->_slug, __('Attachment Manager'), array($this, 'post_form'), 'page', 'normal', 'high'); } /** * Displays the "show attachments" checkbox if wam_list_on_posts is set to some */ public function post_form() { global $post; ?>
ID, '_wam_show_attachments', true)) ?> />