Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Media Categories
- Plugin URI: https://sites.google.com/site/medialibarycategories/
- Description: Media Library Categories is a WordPress plugin that lets you add custom categories for use in the media library. Media items can then be sorted per category.
- Author: Hart Associates (Rick Mead)
- Version: 1.0.8
- Author URI: http://www.hartinc.com
- */
- /**
- * wp-content and plugin urls/paths
- */
- // Pre-2.6 compatibility
- if ( ! defined( 'WP_CONTENT_URL' ) )
- define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
- if ( ! defined( 'WP_CONTENT_DIR' ) )
- define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
- if ( ! defined( 'WP_PLUGIN_URL' ) )
- define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
- if ( ! defined( 'WP_PLUGIN_DIR' ) )
- define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
- include_once("variables.php");
- if (!class_exists('mc')) {
- class mc {
- /**
- * @var string The options string name for this plugin
- */
- var $optionsName = 'mc_options';
- /**
- * @var string $localizationDomain Domain used for localization
- */
- var $localizationDomain = "mc";
- /**
- * @var string $pluginurl The path to this plugin
- */
- var $thispluginurl = '';
- /**
- * @var string $pluginurlpath The path to this plugin
- */
- var $thispluginpath = '';
- /**
- * @var array $options Stores the options for this plugin
- */
- var $options = array();
- var $isAdmin = true;
- var $optionsmenuRoleCapabilityLevel=10;
- var $adminmenuRoleCapabilityLevel=10;
- //Class Functions
- /**
- * PHP 4 Compatible Constructor
- */
- function mc(){$this->__construct();}
- /**
- * PHP 5 Constructor
- */
- function __construct(){
- $admin_init = 0;
- $attachment_fields_to_edit = 22;
- $attachment_fields_to_save = 0;
- //"Constants" setup
- $this->thispluginurl = WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__)).'/';
- $this->thispluginpath = WP_PLUGIN_DIR . '/' . dirname(plugin_basename(__FILE__)).'/';
- //Initialize the options
- $this->getOptions();
- //Register Media category type and default
- add_action('init', array(&$this,"create_my_taxonomies"), 0);
- //Menu for Media Categories Admin section
- add_action('admin_menu', array(&$this,"mediaCategory_add_admin"), 0);
- //Menu for Media Categories Options Admin section
- add_action("admin_menu", array(&$this,"admin_menu_link"), 1);
- //Add admin js scripts
- add_action('admin_init', array(&$this, 'add_admin_scripts'), $admin_init++);
- //Add Filters for editing and saving media records
- add_filter('attachment_fields_to_edit', array(&$this, 'add_media_category_field'), $attachment_fields_to_edit++, 2);
- add_filter('attachment_fields_to_save', array(&$this, 'save_media_category_field'), $attachment_fields_to_save++, 2);
- //Add custom column to media library admin page
- add_filter('manage_media_columns', array(&$this, 'add_media_column'));
- add_action('manage_media_custom_column', array(&$this, 'manage_media_column'), 10, 2);
- //Add custom filter dropdown to media library admin page
- add_action('restrict_manage_posts',array(&$this, 'restrict_media_by_category'));
- add_filter('posts_where', array(&$this, 'convert_attachment_id_to_taxonomy_term_in_query'));
- // Add custom filter dropdown to media library popup
- add_filter( 'media_upload_tabs', 'media_category_upload_tab' );
- add_action( 'media_upload_media_category', 'media_upload_media_category' );
- add_filter('admin_head',array(&$this, 'show_tinyMCE'));
- //for making sure the rewrites show 'www.url.com/media/[media category slug]'
- add_action('admin_init', 'flush_rewrite_rules', $admin_init++);
- add_action('admin_init', array(&$this,"delete_init"), $admin_init++);
- }
- ///
- // Functions Called From Init
- ///
- public function delete_init() {
- if (current_user_can('delete_posts')) add_action('delete_post', array(&$this,'mc_delete_post_relations'), 10);
- }
- public function mc_delete_post_relations($pID) {
- wp_delete_object_term_relationships($pID, 'media_category');
- return true;
- }
- function getOptions() {
- if (!$theOptions = get_option($this->optionsName)) {
- $theOptions = array('default'=>'options');
- update_option($this->optionsName, $theOptions);
- }
- $this->options = $theOptions;
- if($this->options['optionsmenuRoleCapabilityLevel'] !=null && $this->options['optionsmenuRoleCapabilityLevel'] !='' && is_numeric($this->options['optionsmenuRoleCapabilityLevel']))
- {
- $this->optionsmenuRoleCapabilityLevel=$this->options['optionsmenuRoleCapabilityLevel'];
- }
- if($this->options['adminmenuRoleCapabilityLevel'] !=null && $this->options['adminmenuRoleCapabilityLevel'] !='' && is_numeric($this->options['adminmenuRoleCapabilityLevel']))
- {
- $this->adminmenuRoleCapabilityLevel=$this->options['adminmenuRoleCapabilityLevel'];
- }
- }
- function create_my_taxonomies() {
- register_taxonomy(
- 'media_category',
- 'media',
- array(
- 'hierarchical' => true,
- 'label' => 'Media Categories',
- 'public' => true,
- 'show_ui' => true,
- 'query_var' => 'media_categories',
- 'rewrite' => array('slug' => 'media')
- )
- );
- $isterm = term_exists( 'Default', 'media_category' ); // array is returned if taxonomy is given
- $parent_term_id = '0'; // get numeric term id
- if(!$isterm)
- {
- wp_insert_term(
- 'Default', // the term
- 'media_category', // the taxonomy
- array(
- 'description'=> 'The default media category.',
- 'slug' => 'default',
- 'parent'=> $parent_term_id
- )
- );
- }
- $term = term_exists( 'Default', 'media_category' ); // array is returned if taxonomy is given
- if($term)
- {
- if($this->options['mc_default_media_category'] ==null ||$this->options['mc_default_media_category'] =='')
- {
- $this->options['mc_default_media_category'] = $term["term_id"];
- $this->saveAdminOptions();
- }
- }
- }
- function restrict_admin(){
- global $current_user;
- get_currentuserinfo();
- //if not admin, die with message
- if ( $current_user->user_level < 8 ) {
- $this->isAdmin = false;
- }
- }
- public function add_admin_scripts() {
- global $pagenow;
- if ($pagenow=='admin.php' &&
- isset($_GET['page']) && $_GET['page']=='media-library-categories/sort.php' &&
- isset($_GET['termid']) && is_numeric($_GET['termid'])) {
- // Insert jQuery 1.4.2
- wp_enqueue_script(
- 'jqueryrequired', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', false);
- wp_enqueue_script(
- 'WPMediaCategory-jquery-sort',
- $this->thispluginurl. 'js/jquery.tablednd_0_5.js', array( 'jqueryrequired' ));
- wp_enqueue_script(
- 'WPMediaCategory-jquery-init',
- $this->thispluginurl. 'js/jquery.admin.js', array( 'jqueryrequired', 'WPMediaCategory-jquery-sort' ));
- }
- }
- function mediaCategory_add_admin() {
- global $rl_dir, $rl_base, $text_domain;
- add_submenu_page("upload.php", "Media Categories", "Media Categories", $this->adminmenuRoleCapabilityLevel, $rl_dir."/view.php");
- add_submenu_page($rl_dir."/view.php", "Sort", "Sort", $this->adminmenuRoleCapabilityLevel, $rl_dir."/sort.php");
- add_submenu_page($rl_dir."/view.php", "Add Media Category", "Add Media Category", $this->adminmenuRoleCapabilityLevel, $rl_dir."/add.php");
- }
- function admin_menu_link() {
- add_options_page('Media Category Options', 'Media Categories Options', $this->optionsmenuRoleCapabilityLevel, basename(__FILE__), array(&$this,'admin_options_page'));
- add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'filter_plugin_actions'), $this->optionsmenuRoleCapabilityLevel, 2 );
- }
- public function add_media_category_field($fields, $object) {
- if (!isset($fields['media_library_categories'])) {
- $html='<select id="media-category" name="attachments['.$object->ID.'][media-categories][]" multiple="multiple" style="height:100px;">';
- $categories = $this->get_category_hierarchical_terms();
- $selected_categories = (array)wp_get_object_terms($object->ID, 'media_category');
- if (!empty($categories) && !empty($selected_categories)) {
- foreach ($categories AS $category) {
- $select='';
- foreach ($selected_categories AS $category_selected) {
- if ($category["id"]==$category_selected->term_id) {
- $select = 'selected="selected"';
- break;
- }
- }
- $html.="<option value='".$category["id"]."' $select>".$category["name"]."</option>";
- }
- }
- else
- {
- foreach ($categories AS $category) {
- $html.="<option value='".$category["id"]."'>".$category["name"]."</option>";
- }
- }
- $html.='</select>';
- $label = 'Media Categories';
- $fields['media_library_categories'] = array(
- 'label' => $label,
- 'input' => 'html',
- 'html' => $html,
- 'value' => (!empty($selected_categories)) ? $selected_categories->term_id : '',
- 'helps' => ''
- );
- }
- return $fields;
- }
- public function save_media_category_field($post, $attachment) {
- $terms = array();
- if ( $attachment && (count($attachment['media-categories'])>0)) {
- foreach ($attachment['media-categories'] as $termID)
- {
- $term = get_term( $termID, 'media_category' );
- array_push($terms, $term->name);
- }
- }
- //push the new values for this attachment
- wp_set_object_terms($post['ID'], $terms, 'media_category', false);
- return $post;
- }
- function add_media_column($posts_columns) {
- // Add a new column
- $posts_columns['att_cats'] = _x('Categories', 'column name');
- return $posts_columns;
- }
- function manage_media_column($column_name, $id) {
- switch($column_name) {
- case 'att_cats':
- $tagparent = "upload.php?";
- $categories = (array)wp_get_object_terms($id, 'media_category');
- if (!empty($categories)) {
- $currentLabels = '';
- foreach ($categories AS $category) {
- $currentLabels .= (($currentLabels != "")?", ":"").$category->name;
- }
- echo $currentLabels;
- }else {
- _e('No Categories');
- }
- break;
- default:
- break;
- }
- }
- function restrict_media_by_category() {
- global $pagenow;
- global $typenow;
- global $wp_query;
- //Create array of pages where we need category filter
- $media_category_pages = array('upload.php','media-upload.php');
- if (in_array($pagenow, $media_category_pages)) {
- $taxonomy = 'media_category';
- $media_taxonomy = get_taxonomy($taxonomy);
- wp_dropdown_categories(array(
- 'show_option_all' => __("Show All {$media_taxonomy->label}"),
- 'taxonomy' => $taxonomy,
- 'name' => 'media_category',
- 'orderby' => 'name',
- 'selected' => $_GET['media_category'], // Current category selected in dropdown
- 'hierarchical' => true,
- 'depth' => 3,
- 'show_count' => true, // Show # listings in parens
- 'hide_empty' => true, // Don't show businesses w/o listings
- ));
- }
- }
- function convert_attachment_id_to_taxonomy_term_in_query($where) {
- global $pagenow;
- global $wpdb;
- //Create array of pages where we need category filter
- $media_category_pages = array('upload.php','media-upload.php');
- if (in_array($pagenow, $media_category_pages) &&
- isset($_GET['media_category']) && is_numeric($_GET['media_category'])&& $_GET['media_category']>0 ) {
- $subquery = " SELECT r.object_id FROM $wpdb->term_relationships r
- INNER JOIN $wpdb->term_taxonomy tax on tax.term_taxonomy_id = r.term_taxonomy_id
- WHERE tax.term_id = ".$_GET['media_category'];
- $where .= " AND ID IN ($subquery)";
- }
- return $where;
- }
- function show_tinyMCE() {
- global $pagenow;
- if ($pagenow=='admin.php' &&
- isset($_GET['page']) && $_GET['page']=='media-library-categories/add.php') {
- wp_enqueue_script( 'common' );
- wp_enqueue_script( 'jquery-color' );
- wp_print_scripts('editor');
- if (function_exists('add_thickbox')) add_thickbox();
- wp_print_scripts('media-upload');
- if (function_exists('wp_tiny_mce')) wp_tiny_mce();
- wp_admin_css();
- wp_enqueue_script('utils');
- do_action("admin_print_styles-post-php");
- do_action('admin_print_styles');
- remove_all_filters('mce_external_plugins');
- }
- }
- ///
- // END Functions Called From Init
- ///
- public function get_category_hierarchical_list($parentID = 0, $num_per_page=0, $start=0) {
- $return = array();
- if($num_per_page==0)
- {
- $args = array(
- 'hide_empty' => false,
- 'parent' => (int)$parentID,
- 'hierarchical' => false,
- 'taxonomy' => 'media_category',
- 'offset'=>$start
- );
- }
- else
- {
- $args = array(
- 'hide_empty' => false,
- 'parent' => (int)$parentID,
- 'hierarchical' => false,
- 'taxonomy' => 'media_category',
- 'number'=>$num_per_page,
- 'offset'=>$start
- );
- }
- $categorias = get_categories($args);
- if (empty($categorias)) return $return;
- foreach ($categorias AS $categoria) {
- $array = array();
- $array['id'] = $categoria->term_id;
- $array['name'] = $categoria->name;
- $array['slug'] = $categoria->category_nicename;
- $array['children'] = $this->get_category_hierarchical_list($categoria->term_id);
- $return[] = $array;
- }
- return $return;
- }
- public function get_category_hierarchical_terms($parentID = 0, $return = array(), $dashes ='') {
- $args = array(
- 'hide_empty' => false,
- 'parent' => (int)$parentID,
- 'hierarchical' => false,
- 'taxonomy' => 'media_category'
- );
- $categorias = get_categories($args);
- if (empty($categorias)) return $return;
- foreach ($categorias AS $categoria) {
- if($parentID>0)$dashes.='—';
- $array = array();
- $array['id'] = $categoria->term_id;
- $array['name'] = $dashes.' '.$categoria->name;
- $array['slug'] = $categoria->category_nicename;
- $array['description'] = $categoria->description;
- $array['count'] = $categoria->count;
- $_attachments = array();
- $attachmentIds = get_objects_in_term( $term->term_id, 'media_category', $args );
- $args = array(
- 'orderby' => 'post_date',
- 'order' => 'DESC',
- 'include' => $attachmentIds,
- 'post_type' => 'attachment',
- );
- $attachments = get_posts($args);
- if(count($attachments>0)){
- foreach ( $attachments as $attachment ) {
- $mime = strtolower($attachment->post_mime_type);
- $_array = array();
- $_array['id'] = $attachment->ID;
- $_array['title'] = $row['post_title'];
- if($mime=='image/jpeg'
- || $mime=='image/jpg'
- || $mime=='image/gif'
- || $mime=='image/png'
- || $mime=='image/bmp'
- || $mime=='image/tiff'
- )
- {
- $thumb = wp_get_attachment_thumb_url( $attachment->ID );
- $fullsize = $attachment->guid;
- $_array['thumb'] = $thumb;
- $_array['fileurl'] = $attachment->guid;
- }
- else
- {
- $_array['thumb'] = '';
- $_array['fileurl'] = $attachment->guid;
- }
- $_attachments[]=$_array;
- }
- }
- $array['attachments'] =$_attachments;
- $return[] = $array;
- $return = $this->get_category_hierarchical_terms($categoria->term_id, $return, $dashes);
- }
- return $return;
- }
- public function get_category_hierarchical_selectoptions($selected=0, $parentID = 0, $return ='', $dashes ='') {
- $args = array(
- 'hide_empty' => false,
- 'parent' => (int)$parentID,
- 'hierarchical' => false,
- 'taxonomy' => 'media_category'
- );
- $categorias = get_categories($args);
- foreach ($categorias AS $categoria) {
- if($parentID>0)$dashes.='-';
- $selectedhtml='';
- if($selected==$categoria->term_id)
- {
- $selectedhtml = " selected='selected' ";
- }
- $return .="<option $selectedhtml value='".$categoria->term_id."'>$dashes".$categoria->name."</option>";
- $return =$this->get_category_hierarchical_selectoptions($selected, $categoria->term_id, $return, $dashes);
- $dashes='';
- }
- return $return;
- }
- public function get_category_select($selected=0, $parentID = 0, $return ='') {
- $selectInput = $this->get_category_hierarchical_selectoptions($selected, $parentID, $return);
- return "<select>".$selectInput."</select>";
- }
- public function get_category_archive($term_id=0, $parentID = 0, $return = '') {
- $args = null;
- if($term_id>0)
- {
- $args = array(
- 'hide_empty' => false,
- 'include' => (int)$term_id,
- 'hierarchical' => false,
- 'taxonomy' => 'media_category'
- );
- }
- if($parentID>0)
- {
- $args = array(
- 'hide_empty' => false,
- 'parent' => (int)$parentID,
- 'hierarchical' => false,
- 'taxonomy' => 'media_category'
- );
- }
- $categorias = get_categories($args);
- if (empty($categorias)) return $return;
- foreach ($categorias AS $categoria) {
- /* $array['id'] = $categoria->term_id;
- $array['name'] = $dashes.' '.$categoria->name;
- $array['slug'] = $categoria->category_nicename;
- $array['description'] = $categoria->description;
- $array['count'] = $categoria->count; */
- $return .="<div id='category_".$categoria->term_id."'>";
- $return .="<h2>". $categoria->name ."</h2>";
- $return .=(($categoria->description!='')?'<p>'.$categoria->description.'</p>':'');
- $return .="<ul>";
- $attachmentIds = get_objects_in_term( $categoria->term_id, 'media_category', $args );
- $args = array(
- 'orderby' => 'post_date',
- 'order' => 'DESC',
- 'include' => $attachmentIds,
- 'post_type' => 'attachment',
- );
- $attachments = get_posts($args);
- if(count($attachments>0)):
- foreach ( $attachments as $attachment ) {
- $mime = strtolower($attachment->post_mime_type);
- /*$_array['id'] = $attachment->ID;
- $_array['title'] = $attachment->post_title;*/
- if($mime=='image/jpeg'
- || $mime=='image/jpg'
- || $mime=='image/gif'
- || $mime=='image/png'
- || $mime=='image/bmp'
- || $mime=='image/tiff'
- )
- {
- $thumb = wp_get_attachment_thumb_url( $attachment->ID );
- $fullsize = $attachment->guid;
- $return .="<li>";
- $return .="<a href='". $fullsize ."' target='_blank'>";
- $return .="<img class='thumb' src='". $thumb ."' alt='". $attachment->post_title ."' /> ";
- $return .="</a>";
- $return .="</li>";
- }
- else
- {
- $return .="<li>";
- $return .="<a href='". $attachment->guid ."' target='_blank'>". $attachment->post_title ."</a>";
- $return .="</li>";
- }
- }
- endif;
- $return .="</ul>";
- $return = $this->get_category_archive(0, $categoria->term_id, $return);
- $return .="</div>";
- }
- return $return;
- }
- function saveAdminOptions(){
- return update_option($this->optionsName, $this->options);
- }
- function filter_plugin_actions($links, $file) {
- //If your plugin is under a different top-level menu than Settiongs (IE - you changed the function above to something other than add_options_page)
- //Then you're going to want to change options-general.php below to the name of your top-level page
- $settings_link = '<a href="options-general.php?page=' . basename(__FILE__) . '">' . __('Settings') . '</a>';
- array_unshift( $links, $settings_link ); // before other links
- return $links;
- }
- function admin_options_page() {
- global $rl_dir, $rl_base, $text_domain;
- if($_POST['mc_save']){
- if (! wp_verify_nonce($_POST['_wpnonce'], 'mc-update-options') ) die('Whoops! There was a problem with the data you posted. Please go back and try again.');
- $this->options['mc_default_media_category'] = $_POST['mc_default_media_category'];
- $this->options['adminmenuRoleCapabilityLevel'] = $_POST['adminmenuRoleCapabilityLevel'];
- $this->options['optionsmenuRoleCapabilityLevel'] = $_POST['optionsmenuRoleCapabilityLevel'];
- $this->saveAdminOptions();
- echo '<div class="updated"><p>Success! Your changes were sucessfully saved!</p></div>';
- }
- //echo $this->thispluginpath.'taxonomy-media_category.php';
- ?>
- <div class="wrap">
- <h2>Media Category Options <a class='button add-new-h2' href='admin.php?page=<?php echo $rl_dir ?>/view.php'>Manage Media Categories</a></h2>
- <form method="post" id="mc_options">
- <?php wp_nonce_field('mc-update-options'); ?>
- <table width="100%" cellspacing="2" cellpadding="5" class="form-table">
- <tr valign="top">
- <th width="33%" scope="row"><?php _e('Default Category ID:', $this->localizationDomain); ?></th>
- <td><input name="mc_default_media_category" type="text" id="mc_default_media_category" size="45" value="<?php echo $this->options['mc_default_media_category'] ;?>"/>
- </td>
- </tr>
- <tr valign="top">
- <th width="33%" scope="row"><?php _e('Admin Page Role Access:', $this->localizationDomain); ?></th>
- <td>
- <select name="adminmenuRoleCapabilityLevel" id="adminmenuRoleCapabilityLevel">
- <option value="10" <?php echo $this->options['adminmenuRoleCapabilityLevel']=="10"? ' selected="selected"':''; ?>>Administrator</option>
- <option value="7" <?php echo $this->options['adminmenuRoleCapabilityLevel']=="7"? ' selected="selected"':''; ?>>Editor</option>
- <option value="4" <?php echo $this->options['adminmenuRoleCapabilityLevel']=="4"? ' selected="selected"':''; ?>>Author</option>
- <option value="1" <?php echo $this->options['adminmenuRoleCapabilityLevel']=="1"? ' selected="selected"':''; ?>>Contributor</option>
- <option value="0" <?php echo $this->options['adminmenuRoleCapabilityLevel']=="0"? ' selected="selected"':''; ?>>Subscriber</option>
- </select>
- </td>
- </tr>
- <tr valign="top">
- <th width="33%" scope="row"><?php _e('Options Page Role Access:', $this->localizationDomain); ?></th>
- <td>
- <select name="optionsmenuRoleCapabilityLevel" id="optionsmenuRoleCapabilityLevel">
- <option value="10" <?php echo $this->options['optionsmenuRoleCapabilityLevel']=="10"? ' selected="selected"':''; ?>>Administrator</option>
- <option value="7" <?php echo $this->options['optionsmenuRoleCapabilityLevel']=="7"? ' selected="selected"':''; ?>>Editor</option>
- <option value="4" <?php echo $this->options['optionsmenuRoleCapabilityLevel']=="4"? ' selected="selected"':''; ?>>Author</option>
- <option value="1" <?php echo $this->options['optionsmenuRoleCapabilityLevel']=="1"? ' selected="selected"':''; ?>>Contributor</option>
- <option value="0" <?php echo $this->options['optionsmenuRoleCapabilityLevel']=="0"? ' selected="selected"':''; ?>>Subscriber</option>
- </select>
- </td>
- </tr>
- <tr>
- <th colspan=2><input type="submit" name="mc_save" value="Save" /></th>
- </tr>
- </table>
- </form>
- <?php
- }
- } //End Class
- } //End if class exists statement
- //instantiate the class
- if (class_exists('mc')) {
- $mc_var = new mc();
- }
- /* ============================
- * Plugin Shortcodes
- * ============================
- */
- include_once("includes/shortcodes.php");
- function cleanQuery($string)
- {
- if(get_magic_quotes_gpc()) // prevents duplicate backslashes
- {
- $string = stripslashes($string);
- }
- if (phpversion() >= '4.3.0')
- {
- $string = mysql_real_escape_string($string);
- }
- else
- {
- $string = mysql_escape_string($string);
- }
- return $string;
- }
- //Remove default media library tab in popup since there is no filter we can hook into
- function remove_medialibrary_tab($tabs) {
- unset($tabs['library']);
- return $tabs;
- }
- add_filter('media_upload_tabs','remove_medialibrary_tab');
- //Add custom media library tab in popup
- function media_category_upload_tab($tabs='')
- {
- $tabs['media_category'] = _x('Media Library', 'tab label', MEDIA_CATEGORY_TAB);
- return $tabs;
- }
- //Add media library frame
- function media_upload_media_category()
- {
- $errors = array();
- if ( !empty($_POST) ) {
- $return = media_upload_form_handler();
- if ( is_string($return) )
- return $return;
- if ( is_array($return) )
- $errors = $return;
- }
- return wp_iframe( 'media_category_upload_library_form', $errors );
- }
- //Add changed media library upload function - filter added
- function media_category_upload_library_form($errors) {
- global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
- media_upload_header();
- $post_id = intval($_REQUEST['post_id']);
- $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
- $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
- $_GET['paged'] = isset( $_GET['paged'] ) ? intval($_GET['paged']) : 0;
- if ( $_GET['paged'] < 1 )
- $_GET['paged'] = 1;
- $start = ( $_GET['paged'] - 1 ) * 10;
- if ( $start < 1 )
- $start = 0;
- add_filter( 'post_limits', create_function( '$a', "return 'LIMIT $start, 10';" ) );
- list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
- ?>
- <form id="filter" action="" method="get">
- <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
- <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
- <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
- <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
- <p id="media-search" class="search-box">
- <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
- <input type="text" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
- <?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
- </p>
- <ul class="subsubsub">
- <?php
- $type_links = array();
- $_num_posts = (array) wp_count_attachments();
- $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
- foreach ( $matches as $_type => $reals )
- foreach ( $reals as $real )
- if ( isset($num_posts[$_type]) )
- $num_posts[$_type] += $_num_posts[$real];
- else
- $num_posts[$_type] = $_num_posts[$real];
- // If available type specified by media button clicked, filter by that type
- if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
- $_GET['post_mime_type'] = $type;
- list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
- }
- if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
- $class = ' class="current"';
- else
- $class = '';
- $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
- foreach ( $post_mime_types as $mime_type => $label ) {
- $class = '';
- if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
- continue;
- if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
- $class = ' class="current"';
- $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false))) . "'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), "<span id='$mime_type-counter'>" . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
- }
- echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
- unset($type_links);
- ?>
- </ul>
- <div class="tablenav">
- <?php
- $page_links = paginate_links( array(
- 'base' => add_query_arg( 'paged', '%#%' ),
- 'format' => '',
- 'prev_text' => __('«'),
- 'next_text' => __('»'),
- 'total' => ceil($wp_query->found_posts / 10),
- 'current' => $_GET['paged']
- ));
- if ( $page_links )
- echo "<div class='tablenav-pages'>$page_links</div>";
- ?>
- <div class="alignleft actions">
- <?php
- $arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC";
- $arc_result = $wpdb->get_results( $arc_query );
- $month_count = count($arc_result);
- if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
- <select name='m'>
- <option<?php selected( @$_GET['m'], 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
- <?php
- foreach ($arc_result as $arc_row) {
- if ( $arc_row->yyear == 0 )
- continue;
- $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
- if ( isset($_GET['m']) && ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] ) )
- $default = ' selected="selected"';
- else
- $default = '';
- echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
- echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
- echo "</option>\n";
- }
- ?>
- </select>
- <?php }
- //Add filter so we can hook on
- do_action( 'restrict_manage_posts' ); ?>
- <?php submit_button( __( 'Filter »' ), 'secondary', 'post-query-submit', false ); ?>
- </div>
- <br class="clear" />
- </div>
- </form>
- <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form validate" id="library-form">
- <?php wp_nonce_field('media-form'); ?>
- <?php //media_upload_form( $errors ); ?>
- <script type="text/javascript">
- <!--
- jQuery(function($){
- var preloaded = $(".media-item.preloaded");
- if ( preloaded.length > 0 ) {
- preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
- updateMediaForm();
- }
- });
- -->
- </script>
- <div id="media-items">
- <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
- <?php echo get_media_items(null, $errors); ?>
- </div>
- <p class="ml-submit">
- <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
- <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
- </p>
- </form>
- <?php
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement