plugin_url = WP_CONTENT_URL.'/plugins/'.plugin_basename(dirname(__FILE__)); $this->plugin_path = WP_CONTENT_DIR . '/plugins/' . dirname( plugin_basename(__FILE__)) ; $this->afi_table = ''; //adds admin menu options to manage add_action('admin_menu', array(&$this, 'admin_menu')); //adds option to pages/posts content add_filter('the_content', array(&$this, 'Attachment_File_Parse')); //adds option to widget content add_filter('widget_text', array(&$this, 'Attachment_File_Parse')); //adds scripts and css stylesheets add_action('wp_print_scripts', array(&$this, 'Attachment_Header_Code')); //adds contextual help add_action('admin_init', array(&$this, 'add_gallery_contextual_help')); } /** * Function to add main menu and submenus to admin panel * @return adds menu * @author Praveen Rajan */ function admin_menu() { add_menu_page('Attachment File Icons', 'AF Icons', 'manage_options', 'afi-overview' , array(&$this, 'afi_overview'), $this->plugin_url .'/afi_small.png'); add_submenu_page( 'afi-overview', 'Attachment File Icons Overview', 'Manage', 'manage_options', 'afi-overview',array(&$this, 'afi_overview')); add_submenu_page( 'afi-overview', 'Add Attachment Icons', 'Add Icons', 'manage_options', 'afi-add',array(&$this, 'afi_add')); } /** * Function to add contextual help for plugin pages. * @author Praveen Rajan */ function add_gallery_contextual_help() { $help_content = '
Instructions to use Attachment File Icons(AF Icons):
'; $help_content .= 'Alternatively:
Sample Preview:
AF Icon File
Icon ' . $icon_result . ' deleted successfully.
File extension left blank.
'; $status = false; } $icon_result = $wpdb->get_var("SELECT icon_extension FROM " . $this->afi_table . " WHERE icon_extension = '". trim($_POST['extension_name']) . "' "); if($icon_result){ $error_message .= 'Extension type already exist.
'; $status = false; } if($_FILES['afifiles']['error'][0] == 4) { $error_message .= 'No icon file uploaded.
'; $status = false; } if($status){ $message = AttachmentFileIcons::upload_icons(); if($message == 'success'){ echo 'Successfully added icon for file type.
Icon file does not meet the specified dimension.
'; $status = false; break; } //clean filename and extract extension $filepart = AttachmentFileIcons::fileinfo( $afifiles['name'][$key] ); $filename = trim($_POST['extension_name']) . '-icon.' .$filepart['extension']; $dest_file = $this->plugin_path . '/mime/' . $filename; //check for folder permission if ( !is_writeable($this->plugin_path . '/mime/') ) { $message .= 'Unable to write to directory ' . $this->plugin_path . '/mime/ Is this directory writable by the server?
'; $status = false; break; } // save temp file to gallery if ( !@move_uploaded_file($temp_file, $dest_file) ){ $message .= 'Error, the file could not moved to : '.$dest_file.'
'; $status = false; break; } if ( !AttachmentFileIcons::chmod($dest_file) ) { $message .= 'Error, the file permissions could not set
'; $status = false; break; } }else { $message .= 'Error uploading file(Missing \'temp\' folder).
'; $status = false; break; } } if($status){ global $wpdb; $sub_name_type = 'afi_types'; $this->afi_table = $wpdb->prefix . $sub_name_type; $icon_data = array( 'icon_extension' => trim($_POST['extension_name']), 'icon_name' => $filename ); if (function_exists('is_multisite') && is_multisite()) { $old_blog = $wpdb->blogid; // Get all blog ids $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs")); foreach ($blogids as $blog_id) { switch_to_blog($blog_id); AttachmentFileIcons::_afi_add($icon_data); } switch_to_blog($old_blog); $status = true; }else { $wpdb->insert( $this->afi_table, $icon_data ); $status = true; } if($status) return 'success'; } } return $message; } /** * Function to add data to database in multisite. * @param $icon_data - icon data * @author Praveen Rajan */ function _afi_add($icon_data) { global $wpdb; $sub_name_type = 'afi_types'; $this->afi_table = $wpdb->prefix . $sub_name_type; $wpdb->insert( $this->afi_table, $icon_data ); } /** * Function to get fileinfo * * @param string $name The name being checked. * @return array containing information about file * author Praveen Rajan */ function fileinfo( $name ) { //Sanitizes a filename replacing whitespace with dashes $name = sanitize_file_name($name); //get the parts of the name $filepart = pathinfo ( strtolower($name) ); if ( empty($filepart) ) return false; // required until PHP 5.2.0 if ( empty($filepart['filename']) ) $filepart['filename'] = substr($filepart['basename'],0 ,strlen($filepart['basename']) - (strlen($filepart['extension']) + 1) ); $filepart['filename'] = sanitize_title_with_dashes( $filepart['filename'] ); $filepart['extension'] = $filepart['extension']; //combine the new file name $filepart['basename'] = $filepart['filename'] . '.' . $filepart['extension']; return $filepart; } /** * Set correct file permissions (taken from wp core) * * @param string $filename * @return bool $result * @author Praveen Rajan */ function chmod($filename = '') { $stat = @ stat(dirname($filename)); $perms = $stat['mode'] & 0007777; $perms = $perms & 0000666; if ( @chmod($filename, $perms) ) return true; return false; } /** * Function to install afi plugin * @author Praveen Rajan */ function afi_install(){ global $wpdb; if (function_exists('is_multisite') && is_multisite()) { // check if it is a network activation - if so, run the activation function for each blog id if (isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) { $old_blog = $wpdb->blogid; // Get all blog ids $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs")); foreach ($blogids as $blog_id) { switch_to_blog($blog_id); $this->_afi_activate(); } switch_to_blog($old_blog); return; } } $this->_afi_activate(); } /** * Function to create database for plugin. * @author Praveen Rajan */ function _afi_activate() { global $wpdb; $sub_name_type = 'afi_types'; $this->afi_table = $wpdb->prefix . $sub_name_type; if($wpdb->get_var("SHOW TABLES LIKE '$this->afi_table'") != $this->afi_table) { $sql = "CREATE TABLE " . $this->afi_table . " ( `id` bigint(20) NOT NULL auto_increment, `icon_extension` varchar(255) NOT NULL, `icon_name` mediumtext, PRIMARY KEY (`id`), UNIQUE KEY `icon_extension` (`icon_extension`) );"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } $icons_exist = AttachmentFileIcons::scandir_mime($this->plugin_path . '/mime/'); $initial_icons = array(); foreach($icons_exist as $icons) { $temp_value = explode('-', $icons); $initial_icons[] = array( 'icon_extension' => $temp_value[0], 'icon_name' => $icons ); } foreach($initial_icons as $icon) { $wpdb->insert( $this->afi_table, $icon ); } } /** * Function to uninstall plugin * @author Praveen Rajan */ function afi_uninstall(){ global $wpdb; if (function_exists('is_multisite') && is_multisite()) { // check if it is a network activation - if so, run the activation function for each blog id if (isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) { $old_blog = $wpdb->blogid; // Get all blog ids $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs")); foreach ($blogids as $blog_id) { switch_to_blog($blog_id); $this->_afi_deactivate(); } switch_to_blog($old_blog); return; } } $this->_afi_deactivate(); } /** * Function to delete tables of plugins * @author Praveen Rajan */ function _afi_deactivate() { global $wpdb; $sub_name_type = 'afi_types'; $this->afi_table = $wpdb->prefix . $sub_name_type; $wpdb->query("DROP TABLE IF EXISTS $this->afi_table"); } /** * Function to add document icon to each attachment parsed. * @param $matches - input content * @return string - replaced output string * @author Praveen Rajan */ function Attachment_File_Render($matches){ global $wpdb; $sub_name_type = 'afi_types'; $this->afi_table = $wpdb->prefix . $sub_name_type; $icon_types = $wpdb->get_results("SELECT * FROM $this->afi_table", ARRAY_A); $arguments = array(); $arguments = $this->Attachment_File_Split($matches[0]); $file_link = $arguments['href']; $file_name_array = explode("/", $file_link); $file_name = array_reverse($file_name_array); //creates an array out of the url and grabs the filename $file_title = explode('.', $file_name[0]); $file_title = array_reverse($file_title); $ext_array = array(); foreach($icon_types as $icon) { $ext_array[] = $icon['icon_extension']; } if(in_array($file_title[0], $ext_array)) { foreach($icon_types as $icon) { if($icon['icon_extension'] == $file_title[0]){ $image_name = $icon['icon_name']; break; } } $sAttachmentString .= "