Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Media Categories
- Plugin URI:
- Description:
- Author: Hart Associates (Rick Mead)
- Version: 1.0
- 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;
- //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);
- //Restrict options page to adminsitrators
- add_action( 'admin_init', array(&$this,"restrict_admin"), $admin_init++);
- //Add admin js scripts
- add_action('admin_init', array(&$this, 'add_admin_scripts'), $admin_init++);
- //Menu for Media Categories Admin section
- add_action('admin_menu', array(&$this,"mediaCategory_add_admin"), 0);
- //Menu for Media Categories Options Admin section
- if($this->isAdmin)
- {add_action("admin_menu", array(&$this,"admin_menu_link"), 1);}
- //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('parse_query',array(&$this, 'convert_attachment_id_to_taxonomy_term_in_query'));
- //add_action($rl_dir."/add.php", "js_libs");
- add_filter('admin_head',array(&$this, 'show_tinyMCE'));
- }
- function show_tinyMCE() {
- 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');
- }
- /* function js_libs() {
- wp_enqueue_script('tiny_mce');
- } */
- ///
- // Functions Called From Init
- ///
- function getOptions() {
- if (!$theOptions = get_option($this->optionsName)) {
- $theOptions = array('default'=>'options');
- update_option($this->optionsName, $theOptions);
- }
- $this->options = $theOptions;
- }
- function create_my_taxonomies() {
- register_taxonomy(
- 'media_category',
- 'media',
- array(
- 'hierarchical' => true,
- 'label' => 'Media Categories',
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => true
- )
- );
- $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() {
- // Insert jQuery 1.4.2
- /* mod 260211 - fix breaking admin,menu's page -
- wp_enqueue_script(
- 'jquery142', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', false);
- */
- wp_enqueue_script(
- 'WPMediaCategory-jquery-init',
- $this->thispluginurl. 'js/jquery.admin.js',
- /* mod jrc 260211 - array('jquery142'), */
- array('jquery'),
- '0',
- true);
- wp_enqueue_script(
- 'WPMediaCategory-jquery-sort',
- $this->thispluginurl. 'js/jquery.tablednd_0_5.js',
- /* mod jrc 260211 - array('jquery142'), */
- array('jquery'),
- '0',
- true);
- }
- function mediaCategory_add_admin() {
- global $rl_dir, $rl_base, $text_domain;
- add_submenu_page("upload.php", "Media Categories", "Media Categories", 10, $rl_dir."/view.php");
- add_submenu_page($rl_dir."/view.php", "Sort", "Sort", 10, $rl_dir."/sort.php");
- add_submenu_page($rl_dir."/view.php", "Add Media Category", "Add Media Category", 10, $rl_dir."/add.php");
- }
- function admin_menu_link() {
- add_options_page('Media Category Options', 'Media Categories Options', 10, basename(__FILE__), array(&$this,'admin_options_page'));
- add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'filter_plugin_actions'), 10, 2 );
- }
- public function add_media_category_field($fields, $object) {
- if (!isset($fields['media_library_categories'])) {
- $categories_json = json_encode($this->get_category_hierarchical_list());
- $html = '';
- $html .= '<script type="text/javascript">var categories = jQuery.parseJSON(\''. $categories_json .'\');</script>' . PHP_EOL;
- $currentCats = '';
- $categories = (array)wp_get_object_terms($object->ID, 'media_category');
- if (!empty($categories)) {
- $currentCats = '';
- $currentLabels = '';
- foreach ($categories AS $category) {
- $currentCats .= (($currentCats != "")?",":"").$category->term_id;
- $currentLabels .= (($currentLabels != "")?", ":"").$category->name;
- }
- }
- $html .= '<div><input type="hidden" name="attachments['.$object->ID.'][media-categories][]" id="media-category" value="'. $currentCats .'" /></div>' . PHP_EOL;
- $label = 'Media Categories';
- $fields['media_library_categories'] = array(
- 'label' => $label,
- 'input' => 'html',
- 'html' => $html,
- 'value' => (!empty($categories)) ? $category->term_id : '',
- 'helps' => (!empty($categories) AND !empty($category)) ? '<strong>' . __('Current', __CLASS__) . '</strong>: ' . $currentLabels : ''
- );
- }
- return $fields;
- }
- public function save_media_category_field($post, $attachment) {
- $currentCats = '';
- //get data from hidden field
- foreach ($attachment['media-categories'] AS $category) {
- $currentCats = (string)$category;
- }
- $terms = array();
- //split the value into an array and the add the term name to the array. term id does not work.
- $categories = explode(",", $currentCats);
- foreach ($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) {
- // Delete an existing column
- //unset($posts_columns['comments']);
- // 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;
- if ($pagenow=='upload.php') {
- $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' => $wp_query->query['term'],
- '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($query) {
- global $pagenow;
- $qv = &$query->query_vars;
- if ($pagenow=='upload.php' &&
- isset($qv['taxonomy']) && $qv['taxonomy']=='media_category' &&
- isset($qv['term']) && is_numeric($qv['term'])) {
- $term = get_term_by('id',$qv['term'],'media_category');
- $qv['term'] = $term->slug;
- }
- }
- ///
- // END Functions Called From Init
- ///
- private function get_category_hierarchical_list($parentID = 0) {
- $return = array();
- $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 = 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;
- }
- 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->saveAdminOptions();
- echo '<div class="updated"><p>Success! Your changes were sucessfully saved!</p></div>';
- }
- ?>
- <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>
- <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");
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement