Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: DK Tickets
- Plugin URI: http://dmkim1.bget.ru/
- Description: Ticket request system
- Version: 4.44
- Author: Dmitriy Kim
- Author URI: http://dmkim1.bget.ru/
- License: GPL2
- Copyright 2013. Dmitriy Kim (email : dmkim1979@gmail.com)
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License, version 2, as
- published by the Free Software Foundation.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
- add_action('init', 'myinit');
- //-----------------------------------------------------------------------
- // startup initialization
- //-----------------------------------------------------------------------
- function myinit() {
- register_ticket_taxonomy();
- register_ticket_post_type();
- //register_taxonomy_for_object_type('category', 'tickets');
- //flush_rewrite_rules( false );
- add_ticket_role();
- add_filter( 'map_meta_cap', 'map_ticket_capabilities', 10, 4 );
- //--------------- rewrite rules
- add_filter('rewrite_rules_array', 'add_ticket_rewrite_rules');
- add_action('wp_loaded','flush_ticket_rewrite_rules');
- //--------------- adjust query
- add_filter('parse_query', 'restricted_access_to_tickets_list' );
- //--------------- view actions
- add_action('template_redirect', 'send_users_to_login');
- add_filter('template_include','select_ticket_template');
- add_action('login_head', 'replace_login_logo');
- //--------------- admin actions
- add_action('admin_head', 'custom_admin_head');
- add_action('admin_menu', 'remove_menus');
- add_action('admin_menu', 'add_ticket_metaboxes');
- add_action('admin_menu','remove_standart_metaboxes');
- add_action('admin_init','myadmininit');
- }
- //-----------------------------------------------------------------------
- // admin initialization
- //-----------------------------------------------------------------------
- function myadmininit() {
- //--------------------------------------------
- // admin area
- //--------------------------------------------
- if (is_admin()) {
- //------------- process data from category and status metaboxes
- add_action('save_post','save_ticket_category_and_metadata');
- //------------- set ticket columns
- add_filter('manage_edit-ticket_columns','set_ticket_columns');
- add_filter('manage_edit-ticket_sortable_columns','set_ticket_sortable_columns');
- add_action('manage_ticket_posts_custom_column','fill_ticket_columns');
- add_action('restrict_manage_posts','set_ticket_filters');
- add_filter('pre_get_posts', 'handle_ticket_sorts');
- add_filter('posts_where', 'handle_ticket_filters');
- }
- }
- //-----------------------------------------------------------------------
- // register ticket taxonomy
- //-----------------------------------------------------------------------
- function register_ticket_taxonomy() {
- register_taxonomy('ticket_category',array (
- 0 => 'ticket'),
- array(
- 'hierarchical' => true,
- 'label' => 'Ticats',
- 'show_ui' => true,
- 'query_var' =>'ticket_category',
- 'rewrite' => array('slug' => 'ticket_category'),
- 'singular_label' => 'Ticat')
- );
- }
- //-----------------------------------------------------------------------
- // register ticket post type
- //-----------------------------------------------------------------------
- function register_ticket_post_type() {
- register_post_type('ticket',
- array(
- 'label' => 'Tickets',
- 'description' => '',
- 'public' => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'map_meta_cap' => false,
- 'capability_type' => 'ticket',
- 'capabilities' => array(
- 'publish_posts' => 'publish_tickets',
- 'edit_posts' => 'edit_tickets',
- 'edit_others_posts' => 'edit_others_tickets',
- 'delete_posts' => 'delete_tickets',
- 'delete_others_posts' => 'delete_others_tickets',
- 'read_private_posts' => 'read_private_tickets',
- 'edit_post' => 'edit_ticket',
- 'delete_post' => 'delete_ticket',
- 'read_post' => 'read_ticket',
- ),
- 'hierarchical' => true,
- 'rewrite' => array('slug' => 'ticket'),
- 'query_var' => true,
- 'has_archive' => false,
- 'exclude_from_search' => false,
- 'supports' => array('title','editor','comments'/*,'author'*/),
- 'taxonomies' => array('ticket_category'),
- 'labels' => array (
- 'name' => 'Tickets',
- 'singular_name' => 'Ticket',
- 'menu_name' => 'Tickets',
- 'add_new' => 'Add Ticket',
- 'add_new_item' => 'Add New Ticket',
- 'edit' => 'Edit',
- 'edit_item' => 'Edit Ticket',
- 'new_item' => 'New Ticket',
- 'view' => 'View Ticket',
- 'view_item' => 'View Ticket',
- 'search_items' => 'Search Tickets',
- 'not_found' => 'No Tickets Found',
- 'not_found_in_trash' => 'No Tickets Found in Trash',
- 'parent' => 'Parent Ticket'
- )
- )
- );
- }
- //-----------------------------------------------------------------------
- // add ticket role
- //-----------------------------------------------------------------------
- function add_ticket_role() {
- $role = get_role( 'administrator' );
- $role->add_cap('publish_tickets');
- $role->add_cap( 'edit_tickets');
- $role->add_cap('edit_others_tickets');
- $role->add_cap('delete_tickets');
- $role->add_cap('delete_others_tickets');
- $role->add_cap('read_private_tickets');
- $role->add_cap('edit_ticket');
- $role->add_cap('delete_ticket');
- $role->add_cap('read_ticket');
- $result = add_role(
- 'ticket_author',
- 'Ticket Author',
- array(
- 'read' => true,
- 'publish_tickets'=>true,
- 'edit_tickets'=> true,
- 'edit_ticket' => true,
- 'read_ticket'=>true
- )
- );
- }
- //-----------------------------------------------------------------------
- // map ticket capabilities
- //-----------------------------------------------------------------------
- function map_ticket_capabilities( $caps, $cap, $user_id, $args ) {
- /* If editing, deleting, or reading a ticket, get the post and post type object. */
- if ( 'edit_ticket' == $cap || 'delete_ticket' == $cap || 'read_ticket' == $cap ) {
- $post = get_post( $args[0] );
- $post_type = get_post_type_object( $post->post_type );
- $caps = array();
- }
- /* If editing a ticket, assign the required capability. */
- if ( 'edit_ticket' == $cap ) {
- if ( $user_id == $post->post_author )
- $caps[] = $post_type->cap->edit_posts;
- else
- $caps[] = $post_type->cap->edit_others_posts;
- }
- /* If deleting a ticket, assign the required capability. */
- elseif ( 'delete_ticket' == $cap ) {
- if ( $user_id == $post->post_author )
- $caps[] = $post_type->cap->delete_posts;
- else
- $caps[] = $post_type->cap->delete_others_posts;
- }
- /* If reading a private ticket, assign the required capability. */
- elseif ( 'read_ticket' == $cap ) {
- if ( 'private' != $post->post_status )
- $caps[] = 'read';
- elseif ( $user_id == $post->post_author )
- $caps[] = 'read';
- else
- $caps[] = $post_type->cap->read_private_posts;
- }
- /* Return the capabilities required by the user. */
- return $caps;
- }
- //-----------------------------------------------------------------------
- // add rewrite rule for ticket archive page
- //-----------------------------------------------------------------------
- function add_ticket_rewrite_rules($rules) {
- $newrules = array();
- $newrules['(client)/(\w*)$'] = 'index.php?post_type=$matches[2]';
- return array_merge($newrules,$rules);
- }
- //-----------------------------------------------------------------------
- // flush rewrite rules if needed
- //-----------------------------------------------------------------------
- function flush_ticket_rewrite_rules() {
- $rules = get_option( 'rewrite_rules' );
- if ( ! isset( $rules['(client)/(\w*)$'] ) ) {
- global $wp_rewrite;
- $wp_rewrite->flush_rules();
- }
- }
- //-----------------------------------------------------------------------
- // restrict access to tickets list so
- // the authors could see only their own tickets
- // admins could see all,
- // unauthorized could see none
- //-----------------------------------------------------------------------
- function set_filter($wp_query) {
- if ( !current_user_can( 'update_core' ) ) {
- global $current_user;
- $wp_query->set( 'author', $current_user->id!=0?$current_user->id:999999 );
- }
- }
- //-----------------------------------------------------------------------
- function restricted_access_to_tickets_list( $wp_query ) {
- //-----------------------------------------------------------------------
- $post_type = $wp_query->get('post_type');
- if ((is_string($post_type)) && ($post_type=='ticket'))
- set_filter($wp_query);
- if ((is_array($post_type)) && (in_array('ticket', $post_type))) {
- set_filter($wp_query);
- }
- }
- //-----------------------------------------------------------------------
- // if not logged in users tries to access tickets page
- // he is redirected to login page
- //-----------------------------------------------------------------------
- function send_users_to_login() {
- global $wp;
- global $wp_query;
- if ( (!is_user_logged_in()) && ($wp_query->get('post_type')=='ticket')) {
- wp_redirect(wp_login_url($_SERVER['REQUEST_URI']));
- exit();
- }
- }
- //-----------------------------------------------------------------------
- // select template for ticket post type
- //-----------------------------------------------------------------------
- function select_ticket_template($template) {
- if ((get_query_var('post_type')=='ticket') && (!is_single())) {
- return TEMPLATEPATH.'/archive-ticket.php';
- }
- if ((get_query_var('post_type')=='ticket') && (is_single())) {
- return TEMPLATEPATH.'/single-ticket.php';
- }
- return $template;
- }
- //-----------------------------------------------------------------------
- // set site logo on login page
- //-----------------------------------------------------------------------
- function replace_login_logo(){
- echo '<style type="text/css">
- h1 a { background-image:url('.get_bloginfo('template_directory').
- '/images/login-logo.jpg) !important; }
- </style>';
- }
- //-----------------------------------------------------------------------
- //-----------------------------------------------------------------------
- // admin area
- //-----------------------------------------------------------------------
- // customize styles of admin area
- //-----------------------------------------------------------------------
- function custom_admin_head() {
- echo '<style type="text/css">
- #wpadminbar{background:#910!important}
- </style>';
- }
- //-----------------------------------------------------------------------
- // remove menus for non-admins
- //-----------------------------------------------------------------------
- function remove_menus() {
- //if (current_user_can('patch_core'))
- if ( $GLOBALS['user_level'] == 10 )
- return;
- global $menu;
- $restricted = array(__('Dashboard'), __('Posts'),
- __('Media'), __('Links'), __('Pages'),
- __('Appearance'),
- __('Tools'), __('Users'),
- __('Settings'), __('Comments'), __('Plugins'));
- end ($menu);
- while (prev($menu)){
- $value = explode(' ', $menu[key($menu)][0]);
- if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){
- unset($menu[key($menu)]);}
- }
- }
- //-----------------------------------------------------------------------
- // remove standart metaboxes on tickets edit screen
- //-----------------------------------------------------------------------
- function remove_standart_metaboxes() {
- remove_meta_box('ticket_categorydiv','ticket','side');
- }
- //-----------------------------------------------------------------------
- // display metaboxes
- // for assigning ticket category
- // and open/close status
- // on ticket edit screen
- //-----------------------------------------------------------------------
- function add_ticket_metaboxes() {
- add_meta_box('ticket_category_id', 'Категория тикета',
- 'ticket_category_meta_show', 'ticket','side','core');
- add_meta_box('ticket_status_id', 'Статус тикета',
- 'ticket_status_meta_show', 'ticket', 'side', 'core');
- }
- //-----------------------------------------------------------------------
- // show ticket category meta box
- //-----------------------------------------------------------------------
- function ticket_category_meta_show($post) {
- echo '<input type="hidden" name="ticket_category_nonce"
- id="ticket_category_nonce" value="'.
- wp_create_nonce('ticket_category').'" />';
- $ticket_cats = get_terms('ticket_category','hide_empty=0');
- $post_ticket_cats = wp_get_object_terms($post->ID, 'ticket_category');
- foreach ($ticket_cats as $ticket_cat) {
- echo '<p><input type="RADIO" name="ticket_category"
- id="ticket_category" '.checked($ticket_cat->slug, $post_ticket_cats[0]->slug, false ).
- ' value="'.$ticket_cat->slug.'" /> ';
- echo $ticket_cat->name.'</p>';
- }
- }
- //-----------------------------------------------------------------------
- // show ticket status meta box
- //-----------------------------------------------------------------------
- function ticket_status_meta_show($post) {
- echo '<input type="hidden" name="ticket_status_nonce"
- id="ticket_status_nonce" value="'.
- wp_create_nonce('ticket_status').'" />';
- $status = get_post_meta ($post->ID, 'ticket_status', true);
- if (!$status)
- $status=0;
- echo '<p><input type="RADIO" name="ticket_status"
- id="ticket_status" '.checked($status,0, false).
- ' value=0 /> Тикет в обработке</p>';
- echo '<p><input type="RADIO" name="ticket_status"
- id="ticket_status" '.checked($status,1, false).
- ' value=1 /> Тикет закрыт</p>';
- }
- //-----------------------------------------------------------------------
- // ticket save handler
- //-----------------------------------------------------------------------
- function save_ticket_category_and_metadata($post_id) {
- if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
- return $post_id;
- if ($_POST['post_type']!='ticket') {
- return $post_id;
- }
- if ( !current_user_can( 'edit_ticket', $post_id ) )
- return $post_id;
- //------------- async ticket category metabox submit
- if (wp_verify_nonce($_POST['ticket_category_nonce'], 'ticket_category')) {
- $ticket_category = $_POST['ticket_category'];
- wp_set_object_terms($post_id, $ticket_category, 'ticket_category');
- }
- //------------- async ticket status metabox submit
- if (wp_verify_nonce($_POST['ticket_status_nonce'], 'ticket_status')) {
- update_post_meta($post_id, 'ticket_status',esc_attr($_POST['ticket_status']) );
- }
- }
- //-----------------------------------------------------------------------
- // define set of ticket post type columns
- //-----------------------------------------------------------------------
- function set_ticket_columns($columns) {
- $next_arr = array('ticket_category'=>'Категория',
- 'ticket_author'=>'Автор', 'ticket_date'=>'Дата обращения',
- 'ticket_status'=>'Статус', 'id'=>'№');
- $columns['title']='Тикет';
- unset ($columns['date']);
- return $columns+$next_arr;
- }
- //-----------------------------------------------------------------------
- // define set of ticket post type sortable columns
- //-----------------------------------------------------------------------
- function set_ticket_sortable_columns($sortable_columns) {
- $new_columns = array (
- 'ticket_category'=> 'category_sort',
- 'ticket_author'=>'author_sort',
- 'ticket_date'=>'date_sort',
- 'ticket_status'=>'status_sort'
- );
- return $sortable_columns + $new_columns;
- }
- //-----------------------------------------------------------------------
- // define ticket post type custom filters
- //-----------------------------------------------------------------------
- function set_ticket_filters() {
- $filter = 0;
- if (isset($_GET['category_filter']))
- $filter = $_GET['category_filter'];
- echo '<select name="category_filter">';
- $ticket_cats = get_terms('ticket_category','hide_empty=0');
- echo '<option value="0">Все категории</option>';
- foreach ($ticket_cats as $ticket_cat) {
- echo '<option value="'.$ticket_cat->term_id.'" '.
- selected($ticket_cat->term_id, $filter).' >'.
- $ticket_cat->name.'</option>';
- }
- echo '</select>';
- //----------------------------------------
- global $wpdb;
- $filter = 0;
- if (isset($_GET['author_filter']))
- $filter = $_GET['author_filter'];
- $authors = $wpdb->get_results(
- "select post_author, display_name from {$wpdb->posts} a ".
- "inner join {$wpdb->users} b on a.post_author=b.id ".
- "group by post_author, display_name order by display_name", ARRAY_A);
- if ($authors===false)
- throw new Exception ('db error <br/>sql: '.$sql, 30044);
- echo '<select name="author_filter">';
- echo '<option value="0">Все авторы</option>';
- foreach ($authors as $author) {
- echo '<option value="'.$author['post_author'].'" '.
- selected($author['post_author'], $filter).' >'.
- $author['display_name'].'</option>';
- }
- echo '</select>';
- }
- //-----------------------------------------------------------------------
- // handle ticket sorts
- //-----------------------------------------------------------------------
- function handle_ticket_sorts($wp_query) {
- if ($wp_query->get('orderby')=='date_sort') {
- $wp_query->set('orderby', 'date');
- }
- if ($wp_query->get('orderby')=='status_sort') {
- $wp_query->set('meta_key','ticket_status');
- $wp_query->set('orderby','meta_value_num');
- }
- }
- //-----------------------------------------------------------------------
- // handle ticket filters
- //-----------------------------------------------------------------------
- function handle_ticket_filters($where) {
- if( is_admin() ) {
- global $wpdb;
- //-----------------------------------------
- if (isset($_GET['category_filter']) && ($_GET['category_filter']!=0)) {
- $where .= " and ID in (select object_id from {$wpdb->term_relationships} a ".
- " inner join {$wpdb->term_taxonomy} b ".
- " on a.term_taxonomy_id=b.term_taxonomy_id ".
- " inner join {$wpdb->terms} c on b.term_id=c.term_id ".
- " where b.taxonomy='ticket_category' and c.term_id={$_GET['category_filter']}) ";
- }
- //-----------------------------------------
- if (isset($_GET['author_filter']) && ($_GET['author_filter']!=0)) {
- $where .= ' and post_author='.$_GET['author_filter'];
- }
- }
- return $where;
- }
- //-----------------------------------------------------------------------
- // fill ticket columns with data
- //-----------------------------------------------------------------------
- function fill_ticket_columns($column_name, $post_id=0) {
- global $post;
- switch ($column_name) {
- case 'ticket_date':
- echo get_the_time('d M ( H:i )',$post->ID);
- break;
- case 'ticket_category':
- $cat = wp_get_object_terms($post->ID, 'ticket_category');
- echo $cat[0]->name;
- break;
- case 'ticket_author':
- echo get_the_author($post->ID);
- break;
- case 'ticket_status':
- $s = get_post_meta($post->ID, 'ticket_status', true);
- echo $s ? 'Тикет закрыт' : '';
- break;
- case 'id':
- echo $post->ID;
- break;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement