folders.class.php
6 years ago
form.class.php
7 years ago
plugin.updates.php
7 years ago
tree.class.php
6 years ago
tree.class.php
110 lines
| 1 | <?php |
| 2 | defined('ABSPATH') or die('Nope, not accessing this'); |
| 3 | class WCP_Tree { |
| 4 | public function __construct() { |
| 5 | parent::__construct(); |
| 6 | } |
| 7 | |
| 8 | public static function get_full_tree_data($post_type) { |
| 9 | $isAjax = (!empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest')?1:0; |
| 10 | if(isset($_GET[$post_type]) || ! $isAjax) { |
| 11 | update_option("selected_" . $post_type . "_folder", ""); |
| 12 | } |
| 13 | $string = self::get_folder_category_data($post_type, 0, 0); |
| 14 | return $string['string']; |
| 15 | } |
| 16 | |
| 17 | public static function get_folder_category_data($post_type, $parent = 0, $parentStatus = 0) { |
| 18 | // echo "<pre>"; print_r($post_type); die; |
| 19 | $terms = get_terms( $post_type, array( |
| 20 | 'hide_empty' => false, |
| 21 | 'parent' => $parent, |
| 22 | 'orderby' => 'meta_value_num', |
| 23 | 'order' => 'ASC', |
| 24 | 'hierarchical' => false, |
| 25 | 'update_count_callback' => '_update_generic_term_count', |
| 26 | 'meta_query' => [[ |
| 27 | 'key' => 'wcp_custom_order', |
| 28 | 'type' => 'NUMERIC', |
| 29 | ]] |
| 30 | )); |
| 31 | $string = ""; |
| 32 | $child = 0; |
| 33 | $isAjax = (!empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest')?1:0; |
| 34 | if(!empty($terms)) { |
| 35 | $child = count($terms); |
| 36 | foreach($terms as $term) { |
| 37 | $status = get_term_meta($term->term_id, "is_active", true); |
| 38 | $return = self::get_folder_category_data($post_type, $term->term_id, $status); |
| 39 | $class = ($status == 1 && $return['child']>0)?"active":""; |
| 40 | $class .= ($return['child'])>0?" has-sub-tree":""; |
| 41 | if($post_type == "attachment") { |
| 42 | $class .= (isset($_GET['term']) && $_GET['term'] == $term->slug)?" active-item active-term":""; |
| 43 | if(isset($_GET[$post_type]) && $_GET[$post_type] == $term->slug) { |
| 44 | update_option("selected_".$post_type."_folder", $term->term_id); |
| 45 | } |
| 46 | if(!isset($_GET[$post_type]) && $isAjax) { |
| 47 | $termId = get_option("selected_".$post_type."_folder"); |
| 48 | $class .= ($termId == $term->term_id)?" active-item active-term":""; |
| 49 | } |
| 50 | } else { |
| 51 | $class .= (isset($_GET[$post_type]) && $_GET[$post_type] == $term->slug)?" active-item active-term":""; |
| 52 | if(isset($_GET[$post_type]) && $_GET[$post_type] == $term->slug) { |
| 53 | update_option("selected_" . $post_type . "_folder", $term->term_id); |
| 54 | } |
| 55 | if(!isset($_GET[$post_type]) && $isAjax) { |
| 56 | $termId = get_option("selected_".$post_type."_folder"); |
| 57 | $class .= ($termId == $term->term_id)?" active-item active-term":""; |
| 58 | } |
| 59 | } |
| 60 | $status = get_term_meta($term->term_id, "is_highlighted", true); |
| 61 | $class .= ($status == 1)?" is-high":""; |
| 62 | $count = ($term->count != 0)?"<span class='total-count'>{$term->count}</span>":""; |
| 63 | $delete_nonce = wp_create_nonce('wcp_folder_delete_term_'.$term->term_id); |
| 64 | $rename_nonce = wp_create_nonce('wcp_folder_rename_term_'.$term->term_id); |
| 65 | $highlight_nonce = wp_create_nonce('wcp_folder_highlight_term_'.$term->term_id); |
| 66 | $term_nonce = wp_create_nonce('wcp_folder_term_'.$term->term_id); |
| 67 | $string .= "<li data-nonce='{$term_nonce}' data-star='{$highlight_nonce}' data-rename='{$rename_nonce}' data-delete='{$delete_nonce}' data-slug='{$term->slug}' class='ui-state-default route {$class}' id='wcp_folder_{$term->term_id}' data-folder-id='{$term->term_id}'><h3 class='title' title='{$term->name}' id='title_{$term->term_id}'><span class='title-text'>{$term->name}</span> <span class='update-inline-record'></span> {$count} <span class='star-icon'></span></h3><span class='nav-icon'><i class='wcp-icon folder-icon-arrow_right'></i></span><span class='ui-icon'><i class='wcp-icon folder-icon-folder'></i></span> <ul class='space' id='space_{$term->term_id}'>"; |
| 68 | $string .= $return['string']; |
| 69 | $string .= "</ul></li>"; |
| 70 | } |
| 71 | } |
| 72 | return array( |
| 73 | 'string' =>$string, |
| 74 | 'child' => $child |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | public static function get_option_data_for_select($post_type) { |
| 79 | $string = "<option value='0'>Parent Folder</option>"; |
| 80 | $string .= self::get_folder_option_data($post_type, 0, ' '); |
| 81 | return $string; |
| 82 | } |
| 83 | |
| 84 | public static function get_folder_option_data($post_type, $parent = 0, $space = "") { |
| 85 | $terms = get_terms( $post_type, array( |
| 86 | 'hide_empty' => false, |
| 87 | 'parent' => $parent, |
| 88 | 'orderby' => 'meta_value_num', |
| 89 | 'order' => 'ASC', |
| 90 | 'hierarchical' => false, |
| 91 | 'meta_query' => [[ |
| 92 | 'key' => 'wcp_custom_order', |
| 93 | 'type' => 'NUMERIC', |
| 94 | ]] |
| 95 | ) ); |
| 96 | |
| 97 | $selected_term = get_option("selected_" . $post_type . "_folder"); |
| 98 | |
| 99 | |
| 100 | $string = ""; |
| 101 | if(!empty($terms)) { |
| 102 | foreach($terms as $term) { |
| 103 | $selected = ($selected_term == $term->term_id)?"selected":""; |
| 104 | $string .= "<option {$selected} value='{$term->term_id}'>{$space}{$term->name}</option>"; |
| 105 | $string .= self::get_folder_option_data($post_type, $term->term_id, $space."  "); |
| 106 | } |
| 107 | } |
| 108 | return $string; |
| 109 | } |
| 110 | } |