class-ad-groups-list-table.php
11 years ago
class-display-condition-callbacks.php
11 years ago
class-list-table.php
12 years ago
index.php
12 years ago
class-ad-groups-list-table.php
291 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Groups List Table class. |
| 5 | * |
| 6 | * derrived from WP_Terms_List_Table |
| 7 | * |
| 8 | * @package WordPress |
| 9 | * @subpackage List_Table |
| 10 | * @since 3.1.0 |
| 11 | * @access private |
| 12 | */ |
| 13 | class AdvAds_Groups_List_Table extends AdvAds_List_Table { |
| 14 | |
| 15 | /** |
| 16 | * constructor |
| 17 | * |
| 18 | * @since 1.0.0 |
| 19 | * @global type $status |
| 20 | * @global type $page |
| 21 | */ |
| 22 | function __construct() { |
| 23 | global $status, $page; |
| 24 | |
| 25 | parent::__construct(array( |
| 26 | 'plural' => 'adgroups', |
| 27 | 'singular' => 'adgroup', |
| 28 | )); |
| 29 | |
| 30 | $this->post_type = Advanced_Ads::POST_TYPE_SLUG; |
| 31 | $this->taxonomy = Advanced_Ads::AD_GROUP_TAXONOMY; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * default column handling |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | * @param type $item |
| 39 | * @param type $column_name |
| 40 | * @return output for the column |
| 41 | */ |
| 42 | function column_default($item, $column_name) { |
| 43 | return apply_filters("manage_{$this->taxonomy}_{$column_name}_column", '', $column_name, $item->term_id); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * handle checkbox column |
| 48 | * |
| 49 | * @since 1.0.ß |
| 50 | * @see WP_List_Table::::single_row_columns() |
| 51 | * @param array $item A singular item (one full row's worth of data) |
| 52 | * @return string Text to be placed inside the column <td> (movie title only) |
| 53 | */ |
| 54 | function column_cb($tag) { |
| 55 | $default_term = get_option('default_' . $this->screen->taxonomy); |
| 56 | |
| 57 | return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf(__('Select %s', ADVADS_SLUG), $tag->name) . '</label>' |
| 58 | . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />'; |
| 59 | |
| 60 | return ' '; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * column with the ad group id |
| 65 | * @since 1.0.0 |
| 66 | * @param type $tag |
| 67 | * @return string |
| 68 | */ |
| 69 | function column_id($tag) { |
| 70 | return $tag->term_id; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * render the basic ad group information |
| 75 | * |
| 76 | * @since 1.0.0 |
| 77 | * @param obj $tag |
| 78 | * @return string |
| 79 | */ |
| 80 | function column_name($tag) { |
| 81 | $tax = get_taxonomy($this->taxonomy); |
| 82 | |
| 83 | $default_term = get_option('default_' . $this->taxonomy); |
| 84 | |
| 85 | $name = apply_filters('term_name', $tag->name, $tag); |
| 86 | $qe_data = get_term($tag->term_id, $this->taxonomy, OBJECT, 'edit'); |
| 87 | // $edit_link = esc_url(get_edit_term_link($tag->term_id, $this->taxonomy, $this->screen->post_type)); |
| 88 | $args = array( |
| 89 | 'action' => 'edit', |
| 90 | 'group_id' => $tag->term_id |
| 91 | ); |
| 92 | $edit_link = Advanced_Ads_Admin::group_page_url($args); |
| 93 | |
| 94 | $out = '<strong><a class="row-title" href="' . $edit_link . '" title="' . esc_attr(sprintf(__('Edit “%s”', ADVADS_SLUG), $name)) . '">' . $name . '</a></strong><br />'; |
| 95 | $out .= '<p class="description">' . $tag->description . '</p>'; |
| 96 | |
| 97 | $actions = array(); |
| 98 | if (current_user_can($tax->cap->edit_terms)) { |
| 99 | $actions['edit'] = '<a href="' . $edit_link . '">' . __('Edit', ADVADS_SLUG) . '</a>'; |
| 100 | // $actions['inline hide-if-no-js'] = '<a href="#" class="editinline">' . __('Quick Edit') . '</a>'; |
| 101 | } |
| 102 | if (current_user_can($tax->cap->delete_terms) && $tag->term_id != $default_term){ |
| 103 | $args = array( |
| 104 | 'action' => 'delete', |
| 105 | 'group_id' => $tag->term_id |
| 106 | ); |
| 107 | $delete_link = Advanced_Ads_Admin::group_page_url($args); |
| 108 | $actions['delete'] = "<a class='delete-tag' href='" . wp_nonce_url($delete_link, 'delete-tag_' . $tag->term_id) . "'>" . __('Delete') . "</a>"; |
| 109 | } |
| 110 | |
| 111 | $actions = apply_filters("{$this->taxonomy}_row_actions", $actions, $tag); |
| 112 | |
| 113 | $out .= $this->row_actions($actions); |
| 114 | $out .= '</div>'; |
| 115 | |
| 116 | return $out; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * render the slug column |
| 121 | * |
| 122 | * @since 1.0.0 |
| 123 | * @param obj $tag |
| 124 | * @return string |
| 125 | */ |
| 126 | function column_slug($tag) { |
| 127 | return apply_filters('editable_slug', $tag->slug); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * render the ads column (number of ads belonging to this group) |
| 132 | * |
| 133 | * @since 1.0.0 |
| 134 | * @updated 1.1.0 only display published ads |
| 135 | * @param obj $tag |
| 136 | * @return string |
| 137 | */ |
| 138 | function column_ads($tag) { |
| 139 | $count = number_format_i18n($tag->count); |
| 140 | |
| 141 | $tax = get_taxonomy($this->taxonomy); |
| 142 | $args = array( |
| 143 | 'post_type' => $this->post_type, |
| 144 | 'post_status' => 'publish', |
| 145 | 'taxonomy' => $this->taxonomy, |
| 146 | 'term' => $tag->slug |
| 147 | ); |
| 148 | $ads = new WP_Query($args); |
| 149 | |
| 150 | $group = new Advads_Ad_Group($tag->term_id); |
| 151 | $weights = $group->get_ad_weights(); |
| 152 | |
| 153 | $out = ''; |
| 154 | $actions = array(); |
| 155 | // The Loop |
| 156 | if ( $ads->have_posts() ) { |
| 157 | $out .= '<table class="advads-groups-ads-list">'; |
| 158 | while ( $ads->have_posts() ) { |
| 159 | $ads->the_post(); |
| 160 | $out .= '<tr><td><a href="' . get_edit_post_link(get_the_ID()) . '">' . get_the_title() . '</a>'; |
| 161 | $_weight = (isset($weights[get_the_ID()])) ? $weights[get_the_ID()] : Advads_Ad_Group::MAX_AD_GROUP_WEIGHT; |
| 162 | $out .= '<td class="ad-weight ad-weight-' . get_the_ID() . '" title="'.__('Ad weight', ADVADS_SLUG).'">' . $_weight . '</td></tr>'; |
| 163 | $out .= '</tr>'; |
| 164 | } |
| 165 | $out .= '</table>'; |
| 166 | // include actions |
| 167 | $actions['edit'] = '<a href="#" class="edit-ad-group-ads">' . __('Edit', ADVADS_SLUG) . '</a>'; |
| 168 | $out .= $this->row_actions($actions); |
| 169 | // row with the group id |
| 170 | $out .= '<input type="hidden" class="ad-group-id" value="'. $tag->term_id .'"/>'; |
| 171 | } |
| 172 | // Restore original Post Data |
| 173 | wp_reset_postdata(); |
| 174 | |
| 175 | return $out; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * load the column names |
| 180 | * |
| 181 | * @since 1.0.0 |
| 182 | * @see WP_List_Table::::single_row_columns() |
| 183 | * @return array An associative array containing column information: 'slugs'=>'Visible Titles' |
| 184 | * ************************************************************************ */ |
| 185 | function get_columns() { |
| 186 | $columns = array( |
| 187 | // 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text |
| 188 | 'id' => __('ID', $this->textdomain), |
| 189 | 'name' => __('Ad Group', $this->textdomain), |
| 190 | 'slug' => __('Slug', $this->textdomain), |
| 191 | 'ads' => __('Ads', $this->textdomain), |
| 192 | ); |
| 193 | return $columns; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * contains sortable columns |
| 198 | * |
| 199 | * @since 1.0.0 |
| 200 | * @return array An associative array containing all the columns that should be sortable: 'slugs'=>array('data_values',bool) |
| 201 | * ************************************************************************ */ |
| 202 | function get_sortable_columns() { |
| 203 | $sortable_columns = array( |
| 204 | 'id' => array('id', true), //true means it's already sorted |
| 205 | 'name' => array('name', false), |
| 206 | 'description' => array('description', false), |
| 207 | 'slug' => array('slug', false) |
| 208 | ); |
| 209 | return $sortable_columns; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * define bulk actions |
| 214 | * |
| 215 | * @since 1.0.0 |
| 216 | * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles' |
| 217 | * ************************************************************************ */ |
| 218 | function get_bulk_actions() { |
| 219 | $actions = array( |
| 220 | // 'delete' => __('Delete', $this->textdomain) |
| 221 | ); |
| 222 | return $actions; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * handle bulk actions here |
| 227 | * |
| 228 | * @since 1.0.0 |
| 229 | * @see $this->prepare_items() |
| 230 | * ************************************************************************ */ |
| 231 | function process_bulk_action() { |
| 232 | |
| 233 | //Detect when a bulk action is being triggered... |
| 234 | if ('delete' === $this->current_action()) { |
| 235 | |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * load items for output |
| 241 | * |
| 242 | * @since 1.0.0 |
| 243 | */ |
| 244 | function prepare_items() { |
| 245 | // how many items per page |
| 246 | // get_items_per_page basically uses a filter |
| 247 | $per_page = $this->get_items_per_page('edit_' . $this->taxonomy . '_per_page'); |
| 248 | |
| 249 | // set columns |
| 250 | $columns = $this->get_columns(); |
| 251 | $hidden = array(); |
| 252 | $sortable = $this->get_sortable_columns(); |
| 253 | |
| 254 | // combined array with all kinds of columns |
| 255 | $this->_column_headers = array($columns, $hidden, $sortable); |
| 256 | |
| 257 | // process bulk actions |
| 258 | $this->process_bulk_action(); |
| 259 | |
| 260 | // prepare items |
| 261 | $search = !empty($_REQUEST['s']) ? trim(wp_unslash($_REQUEST['s'])) : ''; |
| 262 | |
| 263 | $args = array( |
| 264 | 'taxonomy' => $this->taxonomy, |
| 265 | 'search' => $search, |
| 266 | 'page' => $this->get_pagenum(), |
| 267 | 'number' => $per_page, |
| 268 | 'hide_empty' => 0, |
| 269 | 'orderby' => 'id' |
| 270 | ); |
| 271 | |
| 272 | if (!empty($_REQUEST['orderby'])) |
| 273 | $args['orderby'] = trim(wp_unslash($_REQUEST['orderby'])); |
| 274 | |
| 275 | if (!empty($_REQUEST['order'])) |
| 276 | $args['order'] = trim(wp_unslash($_REQUEST['order'])); |
| 277 | |
| 278 | $this->callback_args = $args; |
| 279 | |
| 280 | // get items |
| 281 | $this->items = get_categories($args); |
| 282 | |
| 283 | $total_items = count($this->items); |
| 284 | $this->set_pagination_args(array( |
| 285 | 'total_items' => $total_items, |
| 286 | 'per_page' => $per_page, |
| 287 | 'total_pages' => ceil($total_items / $per_page) |
| 288 | )); |
| 289 | } |
| 290 | |
| 291 | } |