Advertisement
terorama

DK_Tickets

Dec 9th, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.13 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: DK Tickets
  4. Plugin URI: http://dmkim1.bget.ru/
  5. Description: Ticket request system
  6. Version: 4.44
  7. Author: Dmitriy Kim
  8. Author URI: http://dmkim1.bget.ru/
  9.  
  10. License: GPL2
  11.  
  12. Copyright 2013.  Dmitriy Kim  (email : dmkim1979@gmail.com)
  13.  
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License, version 2, as
  16. published by the Free Software Foundation.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  26. */
  27.  
  28.  
  29. add_action('init', 'myinit');
  30.  
  31. //-----------------------------------------------------------------------
  32. //                 startup initialization
  33. //-----------------------------------------------------------------------
  34. function myinit() {
  35.  
  36.    register_ticket_taxonomy();
  37.    register_ticket_post_type();
  38.    
  39.    //register_taxonomy_for_object_type('category', 'tickets');
  40.    //flush_rewrite_rules( false );
  41.    
  42.    add_ticket_role();
  43.    add_filter( 'map_meta_cap', 'map_ticket_capabilities', 10, 4 );
  44.    
  45.    //--------------- rewrite rules
  46.    
  47.    add_filter('rewrite_rules_array', 'add_ticket_rewrite_rules');
  48.    add_action('wp_loaded','flush_ticket_rewrite_rules');
  49.    
  50.    //--------------- adjust query
  51.    
  52.    add_filter('parse_query', 'restricted_access_to_tickets_list' );
  53.    
  54.    //--------------- view actions
  55.    
  56.    add_action('template_redirect', 'send_users_to_login');
  57.    add_filter('template_include','select_ticket_template');
  58.    
  59.    add_action('login_head', 'replace_login_logo');
  60.    
  61.    //--------------- admin actions
  62.    
  63.    add_action('admin_head', 'custom_admin_head');
  64.    
  65.    add_action('admin_menu', 'remove_menus');  
  66.    add_action('admin_menu', 'add_ticket_metaboxes');
  67.    add_action('admin_menu','remove_standart_metaboxes');
  68.    
  69.    add_action('admin_init','myadmininit');
  70.      
  71.    
  72.    
  73. }
  74.  
  75. //-----------------------------------------------------------------------
  76. //                     admin initialization
  77. //-----------------------------------------------------------------------
  78. function myadmininit() {
  79.  
  80.    //--------------------------------------------
  81.    //               admin area
  82.    //--------------------------------------------
  83.    if (is_admin()) {
  84.      
  85.       //------------- process data from category and status metaboxes
  86.      
  87.       add_action('save_post','save_ticket_category_and_metadata');
  88.      
  89.       //------------- set ticket columns
  90.      
  91.       add_filter('manage_edit-ticket_columns','set_ticket_columns');
  92.       add_filter('manage_edit-ticket_sortable_columns','set_ticket_sortable_columns');
  93.      
  94.       add_action('manage_ticket_posts_custom_column','fill_ticket_columns');
  95.      
  96.       add_action('restrict_manage_posts','set_ticket_filters');
  97.      
  98.       add_filter('pre_get_posts', 'handle_ticket_sorts');
  99.       add_filter('posts_where', 'handle_ticket_filters');
  100.    }
  101. }
  102.  
  103. //-----------------------------------------------------------------------
  104. //                     register ticket taxonomy
  105. //-----------------------------------------------------------------------
  106. function register_ticket_taxonomy() {
  107.  
  108.    register_taxonomy('ticket_category',array (
  109.        0 => 'ticket'),
  110.  
  111.         array(
  112.  
  113.        'hierarchical' => true,
  114.        'label' => 'Ticats',
  115.        'show_ui' => true,
  116.        'query_var' =>'ticket_category',
  117.        'rewrite' => array('slug' => 'ticket_category'),
  118.        'singular_label' => 'Ticat')
  119.  
  120.      );
  121. }
  122.  
  123.  
  124. //-----------------------------------------------------------------------
  125. //                    register ticket post type
  126. //-----------------------------------------------------------------------
  127.  
  128. function register_ticket_post_type() {
  129.  
  130.  
  131.    register_post_type('ticket',
  132.  
  133.       array(
  134.      
  135.          'label' => 'Tickets',
  136.          'description' => '',
  137.          'public' => true,
  138.          'show_ui' => true,
  139.          'show_in_menu' => true,
  140.          'map_meta_cap' => false,
  141.          'capability_type' => 'ticket',
  142.  
  143.          'capabilities' => array(
  144.                 'publish_posts' => 'publish_tickets',
  145.                 'edit_posts' => 'edit_tickets',
  146.                 'edit_others_posts' => 'edit_others_tickets',
  147.                 'delete_posts' => 'delete_tickets',
  148.                 'delete_others_posts' => 'delete_others_tickets',
  149.                 'read_private_posts' => 'read_private_tickets',
  150.                 'edit_post' => 'edit_ticket',
  151.                 'delete_post' => 'delete_ticket',
  152.                 'read_post' => 'read_ticket',
  153.             ),
  154.  
  155.          'hierarchical' => true,
  156.          'rewrite' => array('slug' => 'ticket'),
  157.          'query_var' => true,
  158.          'has_archive' => false,
  159.          'exclude_from_search' => false,
  160.          'supports' => array('title','editor','comments'/*,'author'*/),
  161.  
  162.           'taxonomies' => array('ticket_category'),
  163.  
  164.          'labels' => array (
  165.             'name' => 'Tickets',
  166.             'singular_name' => 'Ticket',
  167.             'menu_name' => 'Tickets',
  168.             'add_new' => 'Add Ticket',
  169.             'add_new_item' => 'Add New Ticket',
  170.             'edit' => 'Edit',
  171.             'edit_item' => 'Edit Ticket',
  172.             'new_item' => 'New Ticket',
  173.             'view' => 'View Ticket',
  174.             'view_item' => 'View Ticket',
  175.             'search_items' => 'Search Tickets',
  176.             'not_found' => 'No Tickets Found',
  177.             'not_found_in_trash' => 'No Tickets Found in Trash',
  178.             'parent' => 'Parent Ticket'
  179.              )
  180.         )
  181.     );
  182.  
  183. }
  184.  
  185. //-----------------------------------------------------------------------
  186. //                     add ticket role
  187. //-----------------------------------------------------------------------
  188. function add_ticket_role() {
  189.  
  190.    $role = get_role( 'administrator' );
  191.    
  192.      $role->add_cap('publish_tickets');
  193.      $role->add_cap( 'edit_tickets');
  194.      $role->add_cap('edit_others_tickets');
  195.      $role->add_cap('delete_tickets');
  196.      $role->add_cap('delete_others_tickets');
  197.      $role->add_cap('read_private_tickets');
  198.      $role->add_cap('edit_ticket');
  199.      $role->add_cap('delete_ticket');
  200.      $role->add_cap('read_ticket');
  201.  
  202.      $result = add_role(
  203.        'ticket_author',
  204.        'Ticket Author',
  205.        
  206.        array(
  207.            'read' => true,
  208.            'publish_tickets'=>true,
  209.            'edit_tickets'=> true,
  210.            'edit_ticket' => true,
  211.            'read_ticket'=>true
  212.     )
  213.    );
  214. }
  215.  
  216.  
  217. //-----------------------------------------------------------------------
  218. //                    map ticket capabilities
  219. //-----------------------------------------------------------------------
  220. function map_ticket_capabilities( $caps, $cap, $user_id, $args ) {
  221.  
  222.     /* If editing, deleting, or reading a ticket, get the post and post type object. */
  223.  
  224.     if ( 'edit_ticket' == $cap || 'delete_ticket' == $cap || 'read_ticket' == $cap ) {
  225.    
  226.         $post = get_post( $args[0] );
  227.         $post_type = get_post_type_object( $post->post_type );
  228.        
  229.         $caps = array();
  230.     }
  231.  
  232.     /* If editing a ticket, assign the required capability. */
  233.    
  234.     if ( 'edit_ticket' == $cap ) {
  235.    
  236.         if ( $user_id == $post->post_author )
  237.        
  238.             $caps[] = $post_type->cap->edit_posts;
  239.         else
  240.             $caps[] = $post_type->cap->edit_others_posts;
  241.     }
  242.  
  243.     /* If deleting a ticket, assign the required capability. */
  244.    
  245.     elseif ( 'delete_ticket' == $cap ) {
  246.    
  247.         if ( $user_id == $post->post_author )
  248.        
  249.             $caps[] = $post_type->cap->delete_posts;
  250.         else
  251.             $caps[] = $post_type->cap->delete_others_posts;
  252.     }
  253.  
  254.     /* If reading a private ticket, assign the required capability. */
  255.    
  256.     elseif ( 'read_ticket' == $cap ) {
  257.  
  258.         if ( 'private' != $post->post_status )
  259.             $caps[] = 'read';
  260.         elseif ( $user_id == $post->post_author )
  261.             $caps[] = 'read';
  262.         else
  263.             $caps[] = $post_type->cap->read_private_posts;
  264.     }
  265.  
  266.     /* Return the capabilities required by the user. */
  267.    
  268.     return $caps;
  269. }
  270.  
  271. //-----------------------------------------------------------------------
  272. //             add rewrite rule for ticket archive page
  273. //-----------------------------------------------------------------------
  274. function add_ticket_rewrite_rules($rules) {
  275.    
  276.     $newrules = array();
  277.     $newrules['(client)/(\w*)$'] = 'index.php?post_type=$matches[2]';
  278.     return array_merge($newrules,$rules);
  279. }
  280.  
  281. //-----------------------------------------------------------------------
  282. //            flush rewrite rules if needed
  283. //-----------------------------------------------------------------------
  284. function flush_ticket_rewrite_rules() {
  285.  
  286.     $rules = get_option( 'rewrite_rules' );
  287.  
  288.     if ( ! isset( $rules['(client)/(\w*)$'] ) ) {
  289.         global $wp_rewrite;
  290.         $wp_rewrite->flush_rules();
  291.     }
  292. }
  293.  
  294. //-----------------------------------------------------------------------
  295. //          restrict access to tickets list so  
  296. //          the authors could see only their own tickets
  297. //          admins could see all,
  298. //          unauthorized could see none
  299. //-----------------------------------------------------------------------
  300. function set_filter($wp_query) {
  301.  
  302.  if ( !current_user_can( 'update_core' ) ) {
  303.             global $current_user;
  304.             $wp_query->set( 'author', $current_user->id!=0?$current_user->id:999999 );
  305.            
  306.  
  307.         }
  308. }
  309. //-----------------------------------------------------------------------
  310. function restricted_access_to_tickets_list( $wp_query ) {
  311. //-----------------------------------------------------------------------
  312.  
  313.  
  314.     $post_type = $wp_query->get('post_type');
  315.  
  316.     if ((is_string($post_type)) && ($post_type=='ticket'))
  317.        set_filter($wp_query);
  318.  
  319.     if ((is_array($post_type)) && (in_array('ticket', $post_type))) {
  320.        set_filter($wp_query);
  321.     }
  322. }
  323.  
  324. //-----------------------------------------------------------------------
  325. //        if not logged in users tries to access tickets page
  326. //        he is redirected to login page
  327. //-----------------------------------------------------------------------
  328. function send_users_to_login() {
  329.  
  330.    global $wp;
  331.    global $wp_query;
  332.    
  333.    if ( (!is_user_logged_in()) && ($wp_query->get('post_type')=='ticket')) {
  334.    
  335.       wp_redirect(wp_login_url($_SERVER['REQUEST_URI']));
  336.       exit();
  337.    }
  338. }
  339.  
  340. //-----------------------------------------------------------------------
  341. //            select template for ticket post type
  342. //-----------------------------------------------------------------------
  343. function select_ticket_template($template) {
  344.  
  345.    if ((get_query_var('post_type')=='ticket') && (!is_single())) {
  346.       return TEMPLATEPATH.'/archive-ticket.php';
  347.    }
  348.  
  349.    if ((get_query_var('post_type')=='ticket') && (is_single())) {
  350.       return TEMPLATEPATH.'/single-ticket.php';
  351.    }
  352.  
  353.    return $template;  
  354.    
  355. }
  356.  
  357. //-----------------------------------------------------------------------
  358. //                set site logo on login page
  359. //-----------------------------------------------------------------------
  360. function replace_login_logo(){  
  361.  
  362.     echo '<style type="text/css">  
  363.        h1 a { background-image:url('.get_bloginfo('template_directory').
  364.          '/images/login-logo.jpg) !important; }  
  365.   </style>';  
  366. }  
  367.  
  368.  
  369. //-----------------------------------------------------------------------
  370. //-----------------------------------------------------------------------
  371. //                            admin area
  372. //-----------------------------------------------------------------------
  373. //              customize styles of admin area
  374. //-----------------------------------------------------------------------
  375. function custom_admin_head() {
  376.  
  377.   echo '<style type="text/css">  
  378.   #wpadminbar{background:#910!important}  
  379.   </style>';  
  380. }
  381. //-----------------------------------------------------------------------
  382. //              remove menus for non-admins
  383. //-----------------------------------------------------------------------
  384. function remove_menus() {
  385.  
  386.    
  387.  
  388.     //if (current_user_can('patch_core'))
  389.     if ( $GLOBALS['user_level'] == 10 )  
  390.        return;  
  391.        
  392.  
  393.     global $menu;  
  394.     $restricted = array(__('Dashboard'), __('Posts'),
  395.                         __('Media'), __('Links'), __('Pages'),
  396.                         __('Appearance'),
  397.                         __('Tools'), __('Users'),
  398.                         __('Settings'), __('Comments'), __('Plugins'));
  399.  
  400.     end ($menu);  
  401.     while (prev($menu)){  
  402.         $value = explode(' ', $menu[key($menu)][0]);  
  403.         if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){
  404.             unset($menu[key($menu)]);}  
  405.     }  
  406. }
  407. //-----------------------------------------------------------------------
  408. //       remove standart metaboxes on tickets edit screen
  409. //-----------------------------------------------------------------------
  410. function remove_standart_metaboxes() {
  411.  
  412.    remove_meta_box('ticket_categorydiv','ticket','side');
  413. }
  414.  
  415. //-----------------------------------------------------------------------
  416. //              display metaboxes
  417. //              for assigning ticket category
  418. //                            and open/close status
  419. //                   on ticket edit screen
  420. //-----------------------------------------------------------------------
  421. function add_ticket_metaboxes() {
  422.  
  423.    add_meta_box('ticket_category_id', 'Категория тикета',
  424.                    'ticket_category_meta_show', 'ticket','side','core');
  425.                    
  426.                  
  427.                    
  428.    add_meta_box('ticket_status_id', 'Статус тикета',
  429.                    'ticket_status_meta_show', 'ticket', 'side', 'core');  
  430.                    
  431. }
  432. //-----------------------------------------------------------------------
  433. //               show ticket category meta box
  434. //-----------------------------------------------------------------------
  435. function ticket_category_meta_show($post) {
  436.  
  437.    echo '<input type="hidden" name="ticket_category_nonce"
  438.          id="ticket_category_nonce" value="'.
  439.           wp_create_nonce('ticket_category').'" />';
  440.        
  441.    $ticket_cats = get_terms('ticket_category','hide_empty=0'); 
  442.    
  443.    $post_ticket_cats = wp_get_object_terms($post->ID, 'ticket_category');
  444.    
  445.    foreach ($ticket_cats as $ticket_cat) {
  446.  
  447.       echo '<p><input type="RADIO" name="ticket_category"
  448.               id="ticket_category" '.checked($ticket_cat->slug, $post_ticket_cats[0]->slug, false ).
  449.               ' value="'.$ticket_cat->slug.'" /> ';
  450.              
  451.       echo  $ticket_cat->name.'</p>';        
  452.    }      
  453. }
  454.  
  455. //-----------------------------------------------------------------------
  456. //             show ticket status meta box
  457. //-----------------------------------------------------------------------
  458. function ticket_status_meta_show($post) {
  459.  
  460.    echo '<input type="hidden" name="ticket_status_nonce"
  461.         id="ticket_status_nonce" value="'.
  462.          wp_create_nonce('ticket_status').'" />';
  463.    
  464.    $status = get_post_meta ($post->ID, 'ticket_status', true);
  465.    
  466.    if (!$status)
  467.       $status=0;
  468.      
  469.    echo '<p><input type="RADIO" name="ticket_status"
  470.          id="ticket_status" '.checked($status,0, false).
  471.           ' value=0 /> Тикет в обработке</p>';
  472.          
  473.    echo '<p><input type="RADIO" name="ticket_status"
  474.          id="ticket_status" '.checked($status,1, false).
  475.           ' value=1 /> Тикет закрыт</p>';
  476.    
  477.    
  478. }
  479.  
  480. //-----------------------------------------------------------------------
  481. //                      ticket save handler
  482. //-----------------------------------------------------------------------
  483. function save_ticket_category_and_metadata($post_id) {
  484.  
  485.    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  486.       return $post_id;
  487.  
  488.    if ($_POST['post_type']!='ticket') {
  489.       return $post_id;
  490.    }
  491.    
  492.    if ( !current_user_can( 'edit_ticket', $post_id ) )
  493.         return $post_id;
  494.        
  495.    //------------- async ticket category metabox submit
  496.    
  497.    if (wp_verify_nonce($_POST['ticket_category_nonce'], 'ticket_category')) {
  498.      
  499.       $ticket_category = $_POST['ticket_category'];
  500.       wp_set_object_terms($post_id, $ticket_category, 'ticket_category');
  501.    }
  502.    
  503.    //------------- async ticket status metabox submit
  504.    if (wp_verify_nonce($_POST['ticket_status_nonce'], 'ticket_status')) {
  505.    
  506.       update_post_meta($post_id, 'ticket_status',esc_attr($_POST['ticket_status']) );
  507.    }
  508. }
  509.  
  510. //-----------------------------------------------------------------------
  511. //           define set of ticket post type columns
  512. //-----------------------------------------------------------------------
  513. function set_ticket_columns($columns) {
  514.  
  515.  
  516.    $next_arr = array('ticket_category'=>'Категория',
  517.                      'ticket_author'=>'Автор', 'ticket_date'=>'Дата обращения',
  518.                      'ticket_status'=>'Статус', 'id'=>'№');
  519.                                  
  520.    $columns['title']='Тикет';
  521.    unset ($columns['date']);
  522.                                  
  523.    return $columns+$next_arr;
  524.  
  525. }
  526.  
  527. //-----------------------------------------------------------------------
  528. //       define set of ticket post type sortable columns
  529. //-----------------------------------------------------------------------
  530. function set_ticket_sortable_columns($sortable_columns) {
  531.  
  532.    $new_columns = array (
  533.       'ticket_category'=> 'category_sort',
  534.       'ticket_author'=>'author_sort',
  535.       'ticket_date'=>'date_sort',
  536.       'ticket_status'=>'status_sort'
  537.    );
  538.    
  539.    return $sortable_columns + $new_columns;
  540. }
  541.  
  542. //-----------------------------------------------------------------------
  543. //      define ticket post type custom filters
  544. //-----------------------------------------------------------------------
  545. function set_ticket_filters() {
  546.  
  547.    $filter = 0;
  548.    if (isset($_GET['category_filter']))
  549.       $filter = $_GET['category_filter'];
  550.  
  551.    echo '<select name="category_filter">';
  552.    
  553.       $ticket_cats = get_terms('ticket_category','hide_empty=0');
  554.       echo '<option value="0">Все категории</option>';
  555.       foreach ($ticket_cats as $ticket_cat) {
  556.          echo '<option value="'.$ticket_cat->term_id.'" '.
  557.          selected($ticket_cat->term_id, $filter).' >'.
  558.          $ticket_cat->name.'</option>';
  559.       }
  560.    echo '</select>';
  561.    
  562.   //----------------------------------------
  563.   global $wpdb;
  564.  
  565.  
  566.   $filter = 0;
  567.   if (isset($_GET['author_filter']))
  568.      $filter = $_GET['author_filter'];
  569.  
  570.   $authors = $wpdb->get_results(
  571.      "select post_author, display_name from {$wpdb->posts} a ".
  572.      "inner join {$wpdb->users} b on a.post_author=b.id  ".
  573.      "group by post_author, display_name order by display_name", ARRAY_A);
  574.      
  575.   if ($authors===false)
  576.          throw new Exception ('db error <br/>sql: '.$sql, 30044);
  577.      
  578.   echo '<select name="author_filter">';
  579.  
  580.       echo '<option value="0">Все авторы</option>';
  581.       foreach ($authors as $author) {
  582.          echo '<option value="'.$author['post_author'].'" '.
  583.          selected($author['post_author'], $filter).' >'.
  584.          $author['display_name'].'</option>';
  585.       }
  586.    echo '</select>';
  587. }
  588.  
  589. //-----------------------------------------------------------------------
  590. //       handle ticket sorts
  591. //-----------------------------------------------------------------------
  592. function handle_ticket_sorts($wp_query) {
  593.  
  594.    if ($wp_query->get('orderby')=='date_sort') {
  595.       $wp_query->set('orderby', 'date');
  596.    }
  597.    
  598.    if ($wp_query->get('orderby')=='status_sort') {
  599.    
  600.       $wp_query->set('meta_key','ticket_status');
  601.       $wp_query->set('orderby','meta_value_num');
  602.    }
  603. }
  604.  
  605. //-----------------------------------------------------------------------
  606. //          handle ticket filters
  607. //-----------------------------------------------------------------------
  608. function handle_ticket_filters($where) {
  609.  
  610.    if( is_admin() ) {
  611.    
  612.       global $wpdb;
  613.      
  614.       //-----------------------------------------
  615.       if (isset($_GET['category_filter']) && ($_GET['category_filter']!=0)) {
  616.  
  617.             $where .= " and ID in (select object_id from {$wpdb->term_relationships} a ".
  618.                       " inner join {$wpdb->term_taxonomy} b ".
  619.                       " on a.term_taxonomy_id=b.term_taxonomy_id  ".
  620.                       " inner join {$wpdb->terms} c on b.term_id=c.term_id ".
  621.                       " where b.taxonomy='ticket_category' and c.term_id={$_GET['category_filter']}) ";
  622.                
  623.        
  624.        }
  625.        //-----------------------------------------
  626.        if (isset($_GET['author_filter']) && ($_GET['author_filter']!=0)) {
  627.           $where .= ' and post_author='.$_GET['author_filter'];
  628.        }
  629.        
  630.     }
  631.     return $where;
  632. }
  633.  
  634. //-----------------------------------------------------------------------
  635. //           fill ticket columns with data
  636. //-----------------------------------------------------------------------
  637. function fill_ticket_columns($column_name, $post_id=0) {
  638.    
  639.    global $post;
  640.    
  641.    switch ($column_name) {
  642.    
  643.       case 'ticket_date':
  644.          echo get_the_time('d M ( H:i )',$post->ID);
  645.          break;
  646.          
  647.       case 'ticket_category':
  648.          $cat = wp_get_object_terms($post->ID, 'ticket_category');
  649.          echo $cat[0]->name;
  650.          break;
  651.          
  652.       case 'ticket_author':
  653.          echo get_the_author($post->ID);
  654.          break;
  655.          
  656.       case 'ticket_status':
  657.          $s = get_post_meta($post->ID, 'ticket_status', true);
  658.          echo  $s ? 'Тикет закрыт' : '';
  659.          break;
  660.          
  661.       case 'id':
  662.          echo $post->ID;
  663.          break;
  664.    }
  665. }
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement