class.swpm-category-list.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * BCategoryList
  4. *
  5. * @author nur
  6. */
  7. if (!class_exists('WP_List_Table')) {
  8. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  9. }
  10. class SwpmCategoryList extends WP_List_Table {
  11. public $selected_level_id = 1;
  12. public $category;
  13. function __construct() {
  14. parent::__construct(array(
  15. 'singular' => SwpmUtils::_('Membership Level'),
  16. 'plural' => SwpmUtils::_('Membership Levels'),
  17. 'ajax' => false
  18. ));
  19. $selected = filter_input(INPUT_POST, 'membership_level_id');
  20. $this->selected_level_id = empty($selected) ? 1 : $selected;
  21. $this->category = ($this->selected_level_id == 1) ?
  22. SwpmProtection::get_instance() :
  23. SwpmPermission::get_instance($this->selected_level_id);
  24. }
  25. function get_columns() {
  26. return array(
  27. 'cb' => '<input type="checkbox" />'
  28. , 'term_id' => SwpmUtils::_('Category ID')
  29. , 'name' => SwpmUtils::_('Category Name')
  30. , 'taxonomy' => SwpmUtils::_('Category Type (Taxonomy)')
  31. , 'description' => SwpmUtils::_('Description')
  32. , 'count' => SwpmUtils::_('Count')
  33. );
  34. }
  35. function get_sortable_columns() {
  36. return array();
  37. }
  38. function column_default($item, $column_name) {
  39. return stripslashes($item->$column_name);
  40. }
  41. function column_term_id($item) {
  42. return $item->term_id;
  43. }
  44. function column_taxonomy($item) {
  45. $taxonomy = $item->taxonomy;
  46. if ($taxonomy == 'category'){
  47. $taxonomy = 'Post Category';
  48. } else {
  49. $taxonomy = 'Custom Post Type ('.$taxonomy.')';
  50. }
  51. return $taxonomy;
  52. }
  53. function column_cb($item) {
  54. return sprintf(
  55. '<input type="hidden" name="ids_in_page[]" value="%s">
  56. <input type="checkbox" %s name="ids[]" value="%s" />', $item->term_id, $this->category->in_categories($item->term_id) ? "checked" : "", $item->term_id
  57. );
  58. }
  59. public static function update_category_list() {
  60. //Check we are on the admin end and user has management permission
  61. SwpmMiscUtils::check_user_permission_and_is_admin('category protection update');
  62. //Check nonce
  63. $swpm_category_prot_update_nonce = filter_input(INPUT_POST, 'swpm_category_prot_update_nonce');
  64. if (!wp_verify_nonce($swpm_category_prot_update_nonce, 'swpm_category_prot_update_nonce_action')) {
  65. //Nonce check failed.
  66. wp_die(SwpmUtils::_("Error! Nonce security verification failed for Category Protection Update action. Clear cache and try again."));
  67. }
  68. $selected = filter_input(INPUT_POST, 'membership_level_id');
  69. $selected_level_id = empty($selected) ? 1 : $selected;
  70. $category = ($selected_level_id == 1) ?
  71. SwpmProtection::get_instance() :
  72. SwpmPermission::get_instance($selected_level_id);
  73. $args = array('ids' => array(
  74. 'filter' => FILTER_VALIDATE_INT,
  75. 'flags' => FILTER_REQUIRE_ARRAY,
  76. ));
  77. $filtered = filter_input_array(INPUT_POST, $args);
  78. $ids = $filtered['ids'];
  79. $args = array('ids_in_page' => array(
  80. 'filter' => FILTER_VALIDATE_INT,
  81. 'flags' => FILTER_REQUIRE_ARRAY,
  82. ));
  83. $filtered = filter_input_array(INPUT_POST, $args);
  84. $ids_in_page = $filtered['ids_in_page'];
  85. $category->remove($ids_in_page, 'category')->apply($ids, 'category')->save();
  86. $message = array('succeeded' => true, 'message' => '<p class="swpm-green-box">' . SwpmUtils::_('Category protection updated!') . '</p>');
  87. SwpmTransfer::get_instance()->set('status', $message);
  88. }
  89. function prepare_items() {
  90. $all_categories = array();
  91. $taxonomies = get_taxonomies($args = array('public' => true,'_builtin'=>false));
  92. $taxonomies['category'] = 'category';
  93. $all_terms = get_terms( $taxonomies, 'orderby=count&hide_empty=0&order=DESC');
  94. $totalitems = count($all_terms);
  95. $perpage = 100;
  96. $paged = !empty($_GET["paged"]) ? sanitize_text_field($_GET["paged"]) : '';
  97. if (empty($paged) || !is_numeric($paged) || $paged <= 0) {
  98. $paged = 1;
  99. }
  100. $totalpages = ceil($totalitems / $perpage);
  101. $offset = 0;
  102. if (!empty($paged) && !empty($perpage)) {
  103. $offset = ($paged - 1) * $perpage;
  104. }
  105. for ($i = $offset; $i < ((int) $offset + (int) $perpage) && !empty($all_terms[$i]); $i++) {
  106. $all_categories[] = $all_terms[$i];
  107. }
  108. $this->set_pagination_args(array(
  109. "total_items" => $totalitems,
  110. "total_pages" => $totalpages,
  111. "per_page" => $perpage,
  112. ));
  113. $columns = $this->get_columns();
  114. $hidden = array();
  115. $sortable = $this->get_sortable_columns();
  116. $this->_column_headers = array($columns, $hidden, $sortable);
  117. $this->items = $all_categories;
  118. }
  119. function no_items() {
  120. SwpmUtils::e('No category found.');
  121. }
  122. }