Advertisement
omegastudio

Wordpress Media Library Categories Plugin- revised

Oct 5th, 2011
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 31.87 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Media Categories
  4. Plugin URI: https://sites.google.com/site/medialibarycategories/
  5. 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.
  6. Author: Hart Associates (Rick Mead)
  7. Version: 1.0.8
  8. Author URI: http://www.hartinc.com
  9. */  
  10.    
  11.  
  12.  
  13. /**
  14. *  wp-content and plugin urls/paths
  15. */
  16. // Pre-2.6 compatibility
  17. if ( ! defined( 'WP_CONTENT_URL' ) )
  18.       define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
  19. if ( ! defined( 'WP_CONTENT_DIR' ) )
  20.       define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
  21. if ( ! defined( 'WP_PLUGIN_URL' ) )
  22.       define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
  23. if ( ! defined( 'WP_PLUGIN_DIR' ) )
  24.       define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
  25.  
  26. include_once("variables.php");
  27.        
  28. if (!class_exists('mc')) {
  29.     class mc {
  30.         /**
  31.         * @var string The options string name for this plugin
  32.         */
  33.         var $optionsName = 'mc_options';
  34.        
  35.         /**
  36.         * @var string $localizationDomain Domain used for localization
  37.         */
  38.         var $localizationDomain = "mc";
  39.        
  40.         /**
  41.         * @var string $pluginurl The path to this plugin
  42.         */
  43.         var $thispluginurl = '';
  44.         /**
  45.         * @var string $pluginurlpath The path to this plugin
  46.         */
  47.         var $thispluginpath = '';
  48.            
  49.         /**
  50.         * @var array $options Stores the options for this plugin
  51.         */
  52.         var $options = array();
  53.        
  54.        
  55.         var $isAdmin = true;
  56.        
  57.         var $optionsmenuRoleCapabilityLevel=10;
  58.         var $adminmenuRoleCapabilityLevel=10;
  59.        
  60.        
  61.         //Class Functions
  62.         /**
  63.         * PHP 4 Compatible Constructor
  64.         */
  65.         function mc(){$this->__construct();}
  66.        
  67.         /**
  68.         * PHP 5 Constructor
  69.         */        
  70.         function __construct(){
  71.             $admin_init = 0;
  72.             $attachment_fields_to_edit = 22;
  73.             $attachment_fields_to_save = 0;        
  74.            
  75.             //"Constants" setup
  76.             $this->thispluginurl = WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__)).'/';
  77.             $this->thispluginpath = WP_PLUGIN_DIR . '/' . dirname(plugin_basename(__FILE__)).'/';          
  78.            
  79.             //Initialize the options
  80.             $this->getOptions();                       
  81.            
  82.             //Register Media category type and default
  83.             add_action('init', array(&$this,"create_my_taxonomies"), 0);
  84.            
  85.             //Menu for Media Categories Admin section
  86.             add_action('admin_menu',  array(&$this,"mediaCategory_add_admin"), 0);
  87.            
  88.             //Menu for Media Categories Options Admin section
  89.             add_action("admin_menu", array(&$this,"admin_menu_link"), 1);
  90.            
  91.             //Add admin js scripts
  92.             add_action('admin_init', array(&$this, 'add_admin_scripts'), $admin_init++);
  93.        
  94.            
  95.             //Add Filters for editing and saving media records
  96.             add_filter('attachment_fields_to_edit', array(&$this, 'add_media_category_field'), $attachment_fields_to_edit++, 2);
  97.             add_filter('attachment_fields_to_save', array(&$this, 'save_media_category_field'), $attachment_fields_to_save++, 2);
  98.        
  99.             //Add custom column to media library admin page
  100.             add_filter('manage_media_columns',  array(&$this, 'add_media_column'));
  101.             add_action('manage_media_custom_column',  array(&$this, 'manage_media_column'), 10, 2);
  102.  
  103.             //Add custom filter dropdown to media library admin page
  104.             add_action('restrict_manage_posts',array(&$this, 'restrict_media_by_category'));
  105.             add_filter('posts_where', array(&$this, 'convert_attachment_id_to_taxonomy_term_in_query'));
  106.  
  107.             // Add custom filter dropdown to media library popup
  108.             add_filter( 'media_upload_tabs', 'media_category_upload_tab' );
  109.             add_action( 'media_upload_media_category', 'media_upload_media_category' );
  110.            
  111.             add_filter('admin_head',array(&$this, 'show_tinyMCE'));
  112.  
  113.             //for making sure the rewrites show  'www.url.com/media/[media category slug]'
  114.             add_action('admin_init', 'flush_rewrite_rules', $admin_init++);
  115.             add_action('admin_init', array(&$this,"delete_init"), $admin_init++);
  116.         }
  117.         ///
  118.         // Functions Called From Init
  119.         ///
  120.         public function delete_init() {
  121.           if (current_user_can('delete_posts')) add_action('delete_post', array(&$this,'mc_delete_post_relations'), 10);
  122.         }
  123.         public function mc_delete_post_relations($pID) {
  124.             wp_delete_object_term_relationships($pID, 'media_category');
  125.             return true;
  126.         }
  127.        
  128.        
  129.         function getOptions() {
  130.             if (!$theOptions = get_option($this->optionsName)) {
  131.                 $theOptions = array('default'=>'options');
  132.                 update_option($this->optionsName, $theOptions);
  133.             }
  134.             $this->options = $theOptions;
  135.            
  136.             if($this->options['optionsmenuRoleCapabilityLevel'] !=null && $this->options['optionsmenuRoleCapabilityLevel'] !='' && is_numeric($this->options['optionsmenuRoleCapabilityLevel']))
  137.             {
  138.                 $this->optionsmenuRoleCapabilityLevel=$this->options['optionsmenuRoleCapabilityLevel'];
  139.             }
  140.            
  141.             if($this->options['adminmenuRoleCapabilityLevel'] !=null && $this->options['adminmenuRoleCapabilityLevel'] !='' && is_numeric($this->options['adminmenuRoleCapabilityLevel']))
  142.             {
  143.                 $this->adminmenuRoleCapabilityLevel=$this->options['adminmenuRoleCapabilityLevel'];
  144.             }
  145.                
  146.            
  147.         }
  148.         function create_my_taxonomies() {
  149.             register_taxonomy(
  150.                 'media_category',
  151.                 'media',
  152.                 array(
  153.                     'hierarchical' => true,
  154.                     'label' => 'Media Categories',
  155.                     'public' => true,
  156.                     'show_ui' => true,
  157.                     'query_var' => 'media_categories',
  158.                     'rewrite' => array('slug' => 'media')
  159.                 )
  160.             );
  161.            
  162.             $isterm = term_exists( 'Default', 'media_category' ); // array is returned if taxonomy is given
  163.             $parent_term_id = '0'; // get numeric term id
  164.             if(!$isterm)
  165.             {
  166.                 wp_insert_term(
  167.                   'Default', // the term
  168.                   'media_category', // the taxonomy
  169.                   array(
  170.                     'description'=> 'The default media category.',
  171.                     'slug' => 'default',
  172.                     'parent'=> $parent_term_id
  173.                   )
  174.                 );
  175.             }
  176.            
  177.             $term = term_exists( 'Default', 'media_category' ); // array is returned if taxonomy is given
  178.             if($term)
  179.             {
  180.                 if($this->options['mc_default_media_category'] ==null ||$this->options['mc_default_media_category'] =='')
  181.                 {
  182.                     $this->options['mc_default_media_category'] = $term["term_id"];  
  183.                     $this->saveAdminOptions();
  184.                 }
  185.             }
  186.            
  187.            
  188.         }
  189.         function restrict_admin(){
  190.             global $current_user;
  191.             get_currentuserinfo();
  192.  
  193.             //if not admin, die with message
  194.             if ( $current_user->user_level <  8 ) {
  195.                 $this->isAdmin = false;
  196.             }
  197.         }
  198.         public function add_admin_scripts() {
  199.             global $pagenow;
  200.             if ($pagenow=='admin.php' &&
  201.                     isset($_GET['page']) && $_GET['page']=='media-library-categories/sort.php' &&
  202.                     isset($_GET['termid']) && is_numeric($_GET['termid'])) {               
  203.                
  204.                 // Insert jQuery 1.4.2
  205.                 wp_enqueue_script(
  206.                      'jqueryrequired', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', false);
  207.                  
  208.                 wp_enqueue_script(
  209.                     'WPMediaCategory-jquery-sort',
  210.                     $this->thispluginurl. 'js/jquery.tablednd_0_5.js', array( 'jqueryrequired' ));
  211.            
  212.                 wp_enqueue_script(
  213.                 'WPMediaCategory-jquery-init',
  214.                 $this->thispluginurl. 'js/jquery.admin.js', array( 'jqueryrequired', 'WPMediaCategory-jquery-sort' ));
  215.                
  216.             }
  217.                
  218.         }
  219.         function mediaCategory_add_admin() {
  220.             global $rl_dir, $rl_base, $text_domain;
  221.             add_submenu_page("upload.php", "Media Categories", "Media Categories", $this->adminmenuRoleCapabilityLevel, $rl_dir."/view.php");
  222.            
  223.             add_submenu_page($rl_dir."/view.php", "Sort", "Sort", $this->adminmenuRoleCapabilityLevel, $rl_dir."/sort.php");
  224.             add_submenu_page($rl_dir."/view.php", "Add Media Category", "Add Media Category", $this->adminmenuRoleCapabilityLevel, $rl_dir."/add.php");
  225.            
  226.                
  227.         }
  228.         function admin_menu_link() {
  229.             add_options_page('Media Category Options', 'Media Categories Options', $this->optionsmenuRoleCapabilityLevel, basename(__FILE__), array(&$this,'admin_options_page'));
  230.             add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'filter_plugin_actions'), $this->optionsmenuRoleCapabilityLevel, 2 );        
  231.         }
  232.         public function add_media_category_field($fields, $object) {
  233.             if (!isset($fields['media_library_categories'])) {
  234.                
  235.                 $html='<select id="media-category" name="attachments['.$object->ID.'][media-categories][]" multiple="multiple" style="height:100px;">';
  236.                                
  237.                 $categories = $this->get_category_hierarchical_terms();
  238.                 $selected_categories = (array)wp_get_object_terms($object->ID, 'media_category');
  239.                
  240.                 if (!empty($categories) && !empty($selected_categories)) {
  241.                    
  242.                     foreach ($categories AS $category) {
  243.                    
  244.                         $select='';
  245.                         foreach ($selected_categories AS $category_selected) {
  246.                             if ($category["id"]==$category_selected->term_id) {
  247.                                 $select = 'selected="selected"';
  248.                                 break;
  249.                             }
  250.                         }
  251.                         $html.="<option value='".$category["id"]."'  $select>".$category["name"]."</option>";
  252.                        
  253.                     }
  254.  
  255.                 }
  256.                 else
  257.                 {
  258.                     foreach ($categories AS $category) {
  259.                         $html.="<option value='".$category["id"]."'>".$category["name"]."</option>";
  260.                     }
  261.                 }
  262.  
  263.                 $html.='</select>';
  264.  
  265.                 $label = 'Media Categories';
  266.                 $fields['media_library_categories'] = array(
  267.                     'label' => $label,
  268.                     'input' => 'html',
  269.                     'html' =>  $html,
  270.                     'value' => (!empty($selected_categories)) ? $selected_categories->term_id : '',
  271.                     'helps' => ''
  272.                 );
  273.                
  274.             }
  275.             return $fields;
  276.         }
  277.         public function save_media_category_field($post, $attachment) {
  278.             $terms = array();
  279.             if ( $attachment && (count($attachment['media-categories'])>0)) {
  280.                 foreach ($attachment['media-categories'] as $termID)
  281.                 {
  282.                     $term = get_term( $termID, 'media_category' );
  283.                     array_push($terms, $term->name);
  284.                 }
  285.             }
  286.             //push the new values for this attachment
  287.             wp_set_object_terms($post['ID'], $terms, 'media_category', false);
  288.            
  289.             return $post;
  290.         }
  291.         function add_media_column($posts_columns) {
  292.             // Add a new column
  293.             $posts_columns['att_cats'] = _x('Categories', 'column name');
  294.          
  295.             return $posts_columns;
  296.         }
  297.         function manage_media_column($column_name, $id) {
  298.            
  299.             switch($column_name) {
  300.             case 'att_cats':
  301.                 $tagparent = "upload.php?";
  302.                
  303.                 $categories = (array)wp_get_object_terms($id, 'media_category');
  304.                
  305.                 if (!empty($categories)) {
  306.                
  307.                     $currentLabels = '';
  308.                     foreach ($categories AS $category) {
  309.                         $currentLabels .= (($currentLabels != "")?", ":"").$category->name;
  310.                     }
  311.                    
  312.                     echo $currentLabels;
  313.                 }else {
  314.                     _e('No Categories');
  315.                 }
  316.                 break;
  317.             default:
  318.                 break;
  319.             }
  320.          
  321.         }
  322.  
  323.         function restrict_media_by_category() {
  324.             global $pagenow;
  325.             global $typenow;
  326.             global $wp_query;
  327.             //Create array of pages where we need category filter
  328.             $media_category_pages = array('upload.php','media-upload.php');
  329.             if (in_array($pagenow, $media_category_pages)) {
  330.                 $taxonomy = 'media_category';
  331.                 $media_taxonomy = get_taxonomy($taxonomy);
  332.                 wp_dropdown_categories(array(
  333.                     'show_option_all' =>  __("Show All {$media_taxonomy->label}"),
  334.                     'taxonomy'        =>  $taxonomy,
  335.                     'name'            =>  'media_category',
  336.                     'orderby'         =>  'name',
  337.                     'selected'        =>  $_GET['media_category'], // Current category selected in dropdown
  338.                     'hierarchical'    =>  true,
  339.                     'depth'           =>  3,
  340.                     'show_count'      =>  true, // Show # listings in parens
  341.                     'hide_empty'      =>  true, // Don't show businesses w/o listings
  342.                 ));
  343.             }
  344.         }
  345.         function convert_attachment_id_to_taxonomy_term_in_query($where) {
  346.             global $pagenow;
  347.             global $wpdb;
  348.             //Create array of pages where we need category filter
  349.             $media_category_pages = array('upload.php','media-upload.php');    
  350.             if (in_array($pagenow, $media_category_pages) &&
  351.                     isset($_GET['media_category']) && is_numeric($_GET['media_category'])&& $_GET['media_category']>0 ) {
  352.                    
  353.                      $subquery = "  SELECT r.object_id FROM $wpdb->term_relationships r
  354.                                     INNER JOIN $wpdb->term_taxonomy tax on tax.term_taxonomy_id = r.term_taxonomy_id
  355.                                     WHERE tax.term_id = ".$_GET['media_category'];
  356.                    
  357.                    
  358.                 $where .= " AND ID IN ($subquery)";
  359.                
  360.             }
  361.            
  362.             return $where;
  363.         }
  364.         function show_tinyMCE() {
  365.             global $pagenow;
  366.             if ($pagenow=='admin.php' &&
  367.                     isset($_GET['page']) && $_GET['page']=='media-library-categories/add.php') {
  368.                 wp_enqueue_script( 'common' );
  369.                 wp_enqueue_script( 'jquery-color' );
  370.                 wp_print_scripts('editor');
  371.                 if (function_exists('add_thickbox')) add_thickbox();
  372.                 wp_print_scripts('media-upload');
  373.                 if (function_exists('wp_tiny_mce')) wp_tiny_mce();
  374.                 wp_admin_css();
  375.                 wp_enqueue_script('utils');
  376.                 do_action("admin_print_styles-post-php");
  377.                 do_action('admin_print_styles');
  378.                 remove_all_filters('mce_external_plugins');
  379.                 }
  380.         }    
  381.        
  382.        
  383.        
  384.        
  385.         ///
  386.         // END Functions Called From Init
  387.         ///
  388.        
  389.        
  390.        
  391.        
  392.        
  393.        
  394.  
  395.        
  396.        
  397.        
  398.        
  399.        
  400.            
  401.         public function get_category_hierarchical_list($parentID = 0, $num_per_page=0, $start=0) {
  402.             $return = array();
  403.             if($num_per_page==0)
  404.             {
  405.                 $args = array(
  406.                     'hide_empty' => false,
  407.                     'parent' => (int)$parentID,
  408.                     'hierarchical' => false,               
  409.                     'taxonomy' => 'media_category',
  410.                     'offset'=>$start
  411.                 );
  412.             }
  413.             else
  414.             {
  415.                 $args = array(
  416.                     'hide_empty' => false,
  417.                     'parent' => (int)$parentID,
  418.                     'hierarchical' => false,               
  419.                     'taxonomy' => 'media_category',
  420.                     'number'=>$num_per_page,
  421.                     'offset'=>$start
  422.                 );
  423.             }
  424.            
  425.             $categorias = get_categories($args);
  426.            
  427.             if (empty($categorias)) return $return;
  428.  
  429.             foreach ($categorias AS $categoria) {
  430.                 $array = array();
  431.                 $array['id'] = $categoria->term_id;
  432.                 $array['name'] = $categoria->name;
  433.                 $array['slug'] = $categoria->category_nicename;
  434.                 $array['children'] = $this->get_category_hierarchical_list($categoria->term_id);
  435.                 $return[] = $array;
  436.             }          
  437.            
  438.             return $return;
  439.         }
  440.        
  441.        
  442.         public function get_category_hierarchical_terms($parentID = 0, $return = array(), $dashes ='') {
  443.            
  444.             $args = array(
  445.                     'hide_empty' => false,
  446.                     'parent' => (int)$parentID,
  447.                     'hierarchical' => false,               
  448.                     'taxonomy' => 'media_category'
  449.                 );
  450.            
  451.            
  452.             $categorias = get_categories($args);
  453.            
  454.             if (empty($categorias)) return $return;
  455.  
  456.             foreach ($categorias AS $categoria) {
  457.                
  458.                 if($parentID>0)$dashes.='&mdash;';
  459.                
  460.                     $array = array();
  461.                     $array['id'] = $categoria->term_id;
  462.                     $array['name'] = $dashes.' '.$categoria->name;
  463.                     $array['slug'] = $categoria->category_nicename;
  464.                     $array['description'] = $categoria->description;
  465.                     $array['count'] = $categoria->count;
  466.                    
  467.                     $_attachments = array();
  468.                    
  469.                    
  470.                         $attachmentIds = get_objects_in_term( $term->term_id, 'media_category', $args );
  471.            
  472.                         $args = array(
  473.                                 'orderby'         => 'post_date',
  474.                                 'order'           => 'DESC',
  475.                                 'include'         => $attachmentIds,
  476.                                 'post_type'       => 'attachment',
  477.                                 );
  478.                         $attachments = get_posts($args);
  479.                         if(count($attachments>0)){
  480.                             foreach ( $attachments as $attachment ) {
  481.                                
  482.                                 $mime = strtolower($attachment->post_mime_type);
  483.                                
  484.                                 $_array = array();
  485.                                 $_array['id'] = $attachment->ID;
  486.                                 $_array['title'] = $row['post_title'];
  487.                                                
  488.                                 if($mime=='image/jpeg'
  489.                                     || $mime=='image/jpg'
  490.                                     || $mime=='image/gif'
  491.                                     || $mime=='image/png'
  492.                                     || $mime=='image/bmp'
  493.                                     || $mime=='image/tiff'
  494.                                     )
  495.                                 {
  496.                                     $thumb = wp_get_attachment_thumb_url( $attachment->ID );
  497.                                     $fullsize = $attachment->guid;
  498.                                    
  499.                                     $_array['thumb'] = $thumb;                             
  500.                                     $_array['fileurl'] = $attachment->guid;
  501.                                 }
  502.                                 else
  503.                                 {
  504.                                     $_array['thumb'] = '';                             
  505.                                     $_array['fileurl'] = $attachment->guid;
  506.                                 }
  507.                                 $_attachments[]=$_array;
  508.                             }
  509.                         }
  510.                        
  511.                     $array['attachments'] =$_attachments;
  512.                     $return[] = $array;
  513.                
  514.                 $return = $this->get_category_hierarchical_terms($categoria->term_id, $return, $dashes);
  515.             }          
  516.            
  517.             return $return;
  518.         }
  519.        
  520.        
  521.         public function get_category_hierarchical_selectoptions($selected=0, $parentID = 0, $return ='', $dashes ='') {
  522.                        
  523.             $args = array(
  524.                 'hide_empty' => false,
  525.                 'parent' => (int)$parentID,
  526.                 'hierarchical' => false,               
  527.                 'taxonomy' => 'media_category'
  528.             );
  529.             $categorias = get_categories($args);
  530.            
  531.             foreach ($categorias AS $categoria) {
  532.                 if($parentID>0)$dashes.='-';
  533.                
  534.                 $selectedhtml='';
  535.                
  536.                
  537.                 if($selected==$categoria->term_id)
  538.                 {
  539.                     $selectedhtml = " selected='selected' ";
  540.                 }
  541.                
  542.                 $return .="<option $selectedhtml value='".$categoria->term_id."'>$dashes".$categoria->name."</option>";
  543.                
  544.                 $return =$this->get_category_hierarchical_selectoptions($selected, $categoria->term_id, $return, $dashes);
  545.                 $dashes='';
  546.             }          
  547.            
  548.             return $return;
  549.         }
  550.        
  551.        
  552.         public function get_category_select($selected=0, $parentID = 0, $return ='') {
  553.            
  554.             $selectInput = $this->get_category_hierarchical_selectoptions($selected, $parentID, $return);
  555.            
  556.             return "<select>".$selectInput."</select>";
  557.         }
  558.        
  559.        
  560.         public function get_category_archive($term_id=0, $parentID = 0, $return = '') {
  561.            
  562.             $args = null;
  563.             if($term_id>0)
  564.             {
  565.                 $args = array(
  566.                         'hide_empty' => false,
  567.                         'include' => (int)$term_id,
  568.                         'hierarchical' => false,               
  569.                         'taxonomy' => 'media_category'
  570.                     );
  571.             }
  572.             if($parentID>0)
  573.             {
  574.                 $args = array(
  575.                         'hide_empty' => false,
  576.                         'parent' => (int)$parentID,
  577.                         'hierarchical' => false,               
  578.                         'taxonomy' => 'media_category'
  579.                     );
  580.             }
  581.            
  582.             $categorias = get_categories($args);
  583.            
  584.             if (empty($categorias)) return $return;
  585.  
  586.             foreach ($categorias AS $categoria) {
  587.                
  588.                
  589.                
  590.                     /* $array['id'] = $categoria->term_id;
  591.                     $array['name'] = $dashes.' '.$categoria->name;
  592.                     $array['slug'] = $categoria->category_nicename;
  593.                     $array['description'] = $categoria->description;
  594.                     $array['count'] = $categoria->count; */
  595.                 $return .="<div id='category_".$categoria->term_id."'>";
  596.                 $return .="<h2>". $categoria->name ."</h2>";
  597.                 $return .=(($categoria->description!='')?'<p>'.$categoria->description.'</p>':'');
  598.                
  599.                 $return .="<ul>";
  600.                    
  601.                             $attachmentIds = get_objects_in_term( $categoria->term_id, 'media_category', $args );
  602.                
  603.                             $args = array(
  604.                                     'orderby'         => 'post_date',
  605.                                     'order'           => 'DESC',
  606.                                     'include'         => $attachmentIds,
  607.                                     'post_type'       => 'attachment',
  608.                                     );
  609.                             $attachments = get_posts($args);
  610.                             if(count($attachments>0)):
  611.                                 foreach ( $attachments as $attachment ) {
  612.                                    
  613.                                     $mime = strtolower($attachment->post_mime_type);
  614.                                    
  615.                                     /*$_array['id'] = $attachment->ID;
  616.                                     $_array['title'] = $attachment->post_title;*/
  617.                                                    
  618.                                     if($mime=='image/jpeg'
  619.                                         || $mime=='image/jpg'
  620.                                         || $mime=='image/gif'
  621.                                         || $mime=='image/png'
  622.                                         || $mime=='image/bmp'
  623.                                         || $mime=='image/tiff'
  624.                                         )
  625.                                     {
  626.                                         $thumb = wp_get_attachment_thumb_url( $attachment->ID );
  627.                                         $fullsize = $attachment->guid;
  628.                                         $return .="<li>";
  629.                                                 $return .="<a href='". $fullsize ."' target='_blank'>";
  630.                                                     $return .="<img class='thumb' src='". $thumb ."' alt='". $attachment->post_title ."' /> ";
  631.                                                 $return .="</a>";
  632.                                         $return .="</li>";
  633.                                     }
  634.                                     else
  635.                                     {
  636.                                         $return .="<li>";
  637.                                                 $return .="<a href='". $attachment->guid ."' target='_blank'>". $attachment->post_title ."</a>";
  638.                                         $return .="</li>";
  639.                                     }
  640.                                    
  641.                                 }
  642.                             endif;
  643.                 $return .="</ul>"; 
  644.                
  645.                 $return = $this->get_category_archive(0, $categoria->term_id, $return);
  646.                 $return .="</div>";
  647.                
  648.                
  649.             }          
  650.            
  651.             return $return;
  652.         }
  653.        
  654.        
  655.        
  656.         function saveAdminOptions(){
  657.             return update_option($this->optionsName, $this->options);
  658.         }
  659.         function filter_plugin_actions($links, $file) {
  660.            //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)
  661.            //Then you're going to want to change options-general.php below to the name of your top-level page
  662.            $settings_link = '<a href="options-general.php?page=' . basename(__FILE__) . '">' . __('Settings') . '</a>';
  663.            array_unshift( $links, $settings_link ); // before other links
  664.  
  665.            return $links;
  666.         }
  667.         function admin_options_page() {
  668.             global $rl_dir, $rl_base, $text_domain;
  669.            
  670.        
  671.             if($_POST['mc_save']){
  672.                 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.');
  673.                 $this->options['mc_default_media_category'] = $_POST['mc_default_media_category'];  
  674.                 $this->options['adminmenuRoleCapabilityLevel'] = $_POST['adminmenuRoleCapabilityLevel'];  
  675.                 $this->options['optionsmenuRoleCapabilityLevel'] = $_POST['optionsmenuRoleCapabilityLevel'];  
  676.                                        
  677.                 $this->saveAdminOptions();
  678.                
  679.                 echo '<div class="updated"><p>Success! Your changes were sucessfully saved!</p></div>';
  680.             }
  681.             //echo $this->thispluginpath.'taxonomy-media_category.php';
  682.            
  683. ?>                                
  684.                 <div class="wrap">
  685.                 <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>
  686.                 <form method="post" id="mc_options">
  687.                 <?php wp_nonce_field('mc-update-options'); ?>
  688.                     <table width="100%" cellspacing="2" cellpadding="5" class="form-table">
  689.                         <tr valign="top">
  690.                             <th width="33%" scope="row"><?php _e('Default Category ID:', $this->localizationDomain); ?></th>
  691.                             <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'] ;?>"/>
  692.                         </td>
  693.                         </tr>
  694.                         <tr valign="top">
  695.                             <th width="33%" scope="row"><?php _e('Admin Page Role Access:', $this->localizationDomain); ?></th>
  696.                             <td>
  697.                             <select name="adminmenuRoleCapabilityLevel" id="adminmenuRoleCapabilityLevel">
  698.                                 <option value="10" <?php echo $this->options['adminmenuRoleCapabilityLevel']=="10"? ' selected="selected"':''; ?>>Administrator</option>
  699.                                 <option value="7" <?php echo $this->options['adminmenuRoleCapabilityLevel']=="7"? ' selected="selected"':''; ?>>Editor</option>
  700.                                 <option value="4" <?php echo $this->options['adminmenuRoleCapabilityLevel']=="4"? ' selected="selected"':''; ?>>Author</option>
  701.                                 <option value="1" <?php echo $this->options['adminmenuRoleCapabilityLevel']=="1"? ' selected="selected"':''; ?>>Contributor</option>
  702.                                 <option value="0" <?php echo $this->options['adminmenuRoleCapabilityLevel']=="0"? ' selected="selected"':''; ?>>Subscriber</option>
  703.                             </select>
  704.                         </td>
  705.                         </tr>
  706.                         <tr valign="top">
  707.                             <th width="33%" scope="row"><?php _e('Options Page Role Access:', $this->localizationDomain); ?></th>
  708.                             <td>
  709.                             <select name="optionsmenuRoleCapabilityLevel" id="optionsmenuRoleCapabilityLevel">
  710.                                 <option value="10" <?php echo $this->options['optionsmenuRoleCapabilityLevel']=="10"? ' selected="selected"':''; ?>>Administrator</option>
  711.                                 <option value="7" <?php echo $this->options['optionsmenuRoleCapabilityLevel']=="7"? ' selected="selected"':''; ?>>Editor</option>
  712.                                 <option value="4" <?php echo $this->options['optionsmenuRoleCapabilityLevel']=="4"? ' selected="selected"':''; ?>>Author</option>
  713.                                 <option value="1" <?php echo $this->options['optionsmenuRoleCapabilityLevel']=="1"? ' selected="selected"':''; ?>>Contributor</option>
  714.                                 <option value="0" <?php echo $this->options['optionsmenuRoleCapabilityLevel']=="0"? ' selected="selected"':''; ?>>Subscriber</option>
  715.                             </select>
  716.                         </td>
  717.                         </tr>
  718.                        
  719.                         <tr>
  720.                             <th colspan=2><input type="submit" name="mc_save" value="Save" /></th>
  721.                         </tr>
  722.                     </table>
  723.                 </form>
  724.                 <?php
  725.         }
  726.        
  727.            
  728.          
  729.   } //End Class
  730. } //End if class exists statement
  731.  
  732. //instantiate the class
  733. if (class_exists('mc')) {
  734.  
  735.     $mc_var = new mc();
  736.  
  737. }
  738.  
  739. /* ============================
  740. * Plugin Shortcodes
  741. * ============================
  742. */
  743. include_once("includes/shortcodes.php");
  744.  
  745.  
  746.  
  747. function cleanQuery($string)
  748. {
  749.   if(get_magic_quotes_gpc())  // prevents duplicate backslashes
  750.   {
  751.     $string = stripslashes($string);
  752.   }
  753.   if (phpversion() >= '4.3.0')
  754.   {
  755.     $string = mysql_real_escape_string($string);
  756.   }
  757.   else
  758.   {
  759.     $string = mysql_escape_string($string);
  760.   }
  761.   return $string;
  762. }
  763.  
  764. //Remove default media library tab in popup since there is no filter we can hook into
  765. function remove_medialibrary_tab($tabs) {
  766. unset($tabs['library']);
  767. return $tabs;
  768. }
  769. add_filter('media_upload_tabs','remove_medialibrary_tab');
  770.  
  771. //Add custom media library tab in popup
  772. function media_category_upload_tab($tabs='')
  773. {
  774.     $tabs['media_category'] = _x('Media Library', 'tab label', MEDIA_CATEGORY_TAB);
  775.     return $tabs;
  776. }
  777.  
  778. //Add media library frame
  779. function media_upload_media_category()
  780. {
  781. $errors = array();
  782.     if ( !empty($_POST) ) {
  783.         $return = media_upload_form_handler();
  784.  
  785.         if ( is_string($return) )
  786.             return $return;
  787.         if ( is_array($return) )
  788.             $errors = $return;
  789.     }
  790.  
  791.     return wp_iframe( 'media_category_upload_library_form', $errors );
  792. }
  793.  
  794. //Add changed media library upload function - filter added
  795. function media_category_upload_library_form($errors) {
  796.     global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
  797.  
  798.     media_upload_header();
  799.  
  800.     $post_id = intval($_REQUEST['post_id']);
  801.  
  802.     $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
  803.     $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
  804.  
  805.     $_GET['paged'] = isset( $_GET['paged'] ) ? intval($_GET['paged']) : 0;
  806.     if ( $_GET['paged'] < 1 )
  807.         $_GET['paged'] = 1;
  808.     $start = ( $_GET['paged'] - 1 ) * 10;
  809.     if ( $start < 1 )
  810.         $start = 0;
  811.     add_filter( 'post_limits', create_function( '$a', "return 'LIMIT $start, 10';" ) );
  812.  
  813.     list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  814.  
  815. ?>
  816.  
  817. <form id="filter" action="" method="get">
  818. <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
  819. <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
  820. <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
  821. <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
  822.  
  823. <p id="media-search" class="search-box">
  824.     <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
  825.     <input type="text" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
  826.     <?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
  827. </p>
  828.  
  829. <ul class="subsubsub">
  830. <?php
  831. $type_links = array();
  832. $_num_posts = (array) wp_count_attachments();
  833. $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  834. foreach ( $matches as $_type => $reals )
  835.     foreach ( $reals as $real )
  836.         if ( isset($num_posts[$_type]) )
  837.             $num_posts[$_type] += $_num_posts[$real];
  838.         else
  839.             $num_posts[$_type] = $_num_posts[$real];
  840. // If available type specified by media button clicked, filter by that type
  841. if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
  842.     $_GET['post_mime_type'] = $type;
  843.     list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  844. }
  845. if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
  846.     $class = ' class="current"';
  847. else
  848.     $class = '';
  849. $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
  850. foreach ( $post_mime_types as $mime_type => $label ) {
  851.     $class = '';
  852.  
  853.     if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  854.         continue;
  855.  
  856.     if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  857.         $class = ' class="current"';
  858.  
  859.     $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>';
  860. }
  861. echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
  862. unset($type_links);
  863. ?>
  864. </ul>
  865.  
  866. <div class="tablenav">
  867.  
  868. <?php
  869. $page_links = paginate_links( array(
  870.     'base' => add_query_arg( 'paged', '%#%' ),
  871.     'format' => '',
  872.     'prev_text' => __('&laquo;'),
  873.     'next_text' => __('&raquo;'),
  874.     'total' => ceil($wp_query->found_posts / 10),
  875.     'current' => $_GET['paged']
  876. ));
  877.  
  878. if ( $page_links )
  879.     echo "<div class='tablenav-pages'>$page_links</div>";
  880. ?>
  881.  
  882. <div class="alignleft actions">
  883. <?php
  884.  
  885. $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";
  886.  
  887. $arc_result = $wpdb->get_results( $arc_query );
  888.  
  889. $month_count = count($arc_result);
  890.  
  891. if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
  892. <select name='m'>
  893. <option<?php selected( @$_GET['m'], 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
  894. <?php
  895. foreach ($arc_result as $arc_row) {
  896.     if ( $arc_row->yyear == 0 )
  897.         continue;
  898.     $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
  899.  
  900.     if ( isset($_GET['m']) && ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] ) )
  901.         $default = ' selected="selected"';
  902.     else
  903.         $default = '';
  904.  
  905.     echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
  906.     echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
  907.     echo "</option>\n";
  908. }
  909. ?>
  910. </select>
  911. <?php }
  912.  
  913. //Add filter so we can hook on
  914. do_action( 'restrict_manage_posts' ); ?>
  915.  
  916. <?php submit_button( __( 'Filter &#187;' ), 'secondary', 'post-query-submit', false ); ?>
  917.  
  918. </div>
  919.  
  920. <br class="clear" />
  921. </div>
  922. </form>
  923.  
  924. <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form validate" id="library-form">
  925.  
  926. <?php wp_nonce_field('media-form'); ?>
  927. <?php //media_upload_form( $errors ); ?>
  928.  
  929. <script type="text/javascript">
  930. <!--
  931. jQuery(function($){
  932.     var preloaded = $(".media-item.preloaded");
  933.     if ( preloaded.length > 0 ) {
  934.         preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  935.         updateMediaForm();
  936.     }
  937. });
  938. -->
  939. </script>
  940.  
  941. <div id="media-items">
  942. <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
  943. <?php echo get_media_items(null, $errors); ?>
  944. </div>
  945. <p class="ml-submit">
  946. <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
  947. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  948. </p>
  949. </form>
  950. <?php
  951. }
  952.  
  953. ?>
  954.  
  955.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement