class.swpm-post-list.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 SwpmPostList extends WP_List_Table {
  11. public $selected_level_id = 1;
  12. public $post;
  13. public $type;
  14. function __construct() {
  15. parent::__construct(array(
  16. 'singular' => SwpmUtils::_('Membership Level'),
  17. 'plural' => SwpmUtils::_('Membership Levels'),
  18. 'ajax' => false
  19. ));
  20. $selected = filter_input(INPUT_POST, 'membership_level_id');
  21. $this->selected_level_id = empty($selected) ? 1 : $selected;
  22. $this->post = ($this->selected_level_id == 1) ?
  23. SwpmProtection::get_instance() :
  24. SwpmPermission::get_instance($this->selected_level_id);
  25. $this->type = filter_input(INPUT_GET, 'list_type');
  26. if (is_null($this->type)) {
  27. $this->type = filter_input(INPUT_POST, 'list_type');
  28. }
  29. if (is_null($this->type)) {
  30. $this->type = 'post';
  31. }
  32. }
  33. function get_columns() {
  34. switch ($this->type) {
  35. case 'page':
  36. return array(
  37. 'cb' => '<input type="checkbox" />'
  38. , 'date' => SwpmUtils::_('Date')
  39. , 'title' => SwpmUtils::_('Title')
  40. , 'author' => SwpmUtils::_('Author')
  41. , 'status' => SwpmUtils::_('Status')
  42. );
  43. break;
  44. case 'post':
  45. return array(
  46. 'cb' => '<input type="checkbox" />'
  47. , 'date' => SwpmUtils::_('Date')
  48. , 'title' => SwpmUtils::_('Title')
  49. , 'author' => SwpmUtils::_('Author')
  50. , 'categories' => SwpmUtils::_('Categories')
  51. , 'status' => SwpmUtils::_('Status')
  52. );
  53. break;
  54. case 'custom_post':
  55. return array(
  56. 'cb' => '<input type="checkbox" />'
  57. , 'date' => SwpmUtils::_('Date')
  58. , 'title' => SwpmUtils::_('Title')
  59. , 'author' => SwpmUtils::_('Author')
  60. , 'type' => SwpmUtils::_('Type')
  61. , 'status' => SwpmUtils::_('Status')
  62. );
  63. break;
  64. }
  65. }
  66. function get_sortable_columns() {
  67. return array();
  68. }
  69. function column_default($item, $column_name) {
  70. return stripslashes($item[$column_name]);
  71. }
  72. function column_term_id($item) {
  73. return $item->term_id;
  74. }
  75. function column_taxonomy($item) {
  76. $taxonomy = $item->taxonomy;
  77. if ($taxonomy == 'category') {
  78. $taxonomy = 'Post Category';
  79. } else {
  80. $taxonomy = 'Custom Post Type (' . $taxonomy . ')';
  81. }
  82. return $taxonomy;
  83. }
  84. function column_cb($item) {
  85. return sprintf(
  86. '<input type="hidden" name="ids_in_page[]" value="%s">
  87. <input type="checkbox" %s name="ids[]" value="%s" />', $item['ID'], $item['protected'], $item['ID']
  88. );
  89. }
  90. public static function update_post_list() {
  91. //Check we are on the admin end and user has management permission
  92. SwpmMiscUtils::check_user_permission_and_is_admin('post protection update');
  93. //Check nonce
  94. $swpm_post_prot_update_nonce = filter_input(INPUT_POST, 'swpm_post_prot_update_nonce');
  95. if (!wp_verify_nonce($swpm_post_prot_update_nonce, 'swpm_post_prot_update_nonce_action')) {
  96. //Nonce check failed.
  97. wp_die(SwpmUtils::_("Error! Nonce security verification failed for Post Protection Update action. Clear cache and try again."));
  98. }
  99. $type = filter_input(INPUT_POST, 'list_type');
  100. $selected = filter_input(INPUT_POST, 'membership_level_id');
  101. $selected_level_id = empty($selected) ? 1 : $selected;
  102. $post = ($selected_level_id == 1) ?
  103. SwpmProtection::get_instance() :
  104. SwpmPermission::get_instance($selected_level_id);
  105. $args = array('ids' => array(
  106. 'filter' => FILTER_VALIDATE_INT,
  107. 'flags' => FILTER_REQUIRE_ARRAY,
  108. ));
  109. $filtered = filter_input_array(INPUT_POST, $args);
  110. $ids = $filtered['ids'];
  111. $args = array('ids_in_page' => array(
  112. 'filter' => FILTER_VALIDATE_INT,
  113. 'flags' => FILTER_REQUIRE_ARRAY,
  114. ));
  115. $filtered = filter_input_array(INPUT_POST, $args);
  116. $ids_in_page = $filtered['ids_in_page'];
  117. $post->remove($ids_in_page, $type)->apply($ids, $type)->save();
  118. $message = array('succeeded' => true, 'message' => '<p class="swpm-green-box">' . SwpmUtils::_('Protection settings updated!') . '</p>');
  119. SwpmTransfer::get_instance()->set('status', $message);
  120. }
  121. function prepare_items() {
  122. global $wpdb;
  123. switch ($this->type) {
  124. case 'page':
  125. $args = array(
  126. 'child_of' => 0,
  127. 'sort_order' => 'ASC',
  128. 'sort_column' => 'post_title',
  129. 'hierarchical' => 0,
  130. 'parent' => -1,
  131. );
  132. $all_pages = get_pages($args);
  133. $filtered_items = array();
  134. foreach ($all_pages as $page) {
  135. $page_summary = array();
  136. $user_info = get_userdata($page->post_author);
  137. $page_summary['protected'] = $this->post->in_pages($page->ID) ? " checked='checked'" : "";
  138. $page_summary['ID'] = $page->ID;
  139. $page_summary['date'] = $page->post_date;
  140. $page_summary['title'] = '<a href="' . get_permalink($page->ID) . '" target="_blank">' . $page->post_title . '</a>';
  141. $page_summary['author'] = $user_info->user_login;
  142. $page_summary['status'] = $page->post_status;
  143. $filtered_items[] = $page_summary;
  144. }
  145. break;
  146. case 'post':
  147. $sql = "SELECT ID,post_date,post_title,post_author, post_type, post_status FROM $wpdb->posts ";
  148. $sql .= " WHERE post_type = 'post' AND post_status = 'publish'";
  149. $all_posts = $wpdb->get_results($sql);
  150. $filtered_items = array();
  151. foreach ($all_posts as $post) {
  152. //if($post->post_type=='page')continue;
  153. $post_summary = array();
  154. $user_info = get_userdata($post->post_author);
  155. $categories = get_the_category($post->ID);
  156. $cat = array();
  157. foreach ($categories as $category)
  158. $cat[] = $category->category_nicename;
  159. $post_summary['protected'] = $this->post->in_posts($post->ID) ? " checked='checked'" : "";
  160. $post_summary['ID'] = $post->ID;
  161. $post_summary['date'] = $post->post_date;
  162. $post_summary['title'] = '<a href="' . get_permalink($post->ID) . '" target="_blank">' . $post->post_title . '</a>';
  163. $post_summary['author'] = $user_info->user_login;
  164. $post_summary['categories'] = rawurldecode(implode(' ', $cat));
  165. $post_summary['status'] = $post->post_status;
  166. $filtered_items[] = $post_summary;
  167. }
  168. break;
  169. case 'custom_post':
  170. $filtered_items = array();
  171. $args = array('public' => true, '_builtin' => false);
  172. $post_types = get_post_types($args);
  173. $arg = "'" . implode('\',\'', $post_types) . "'";
  174. if (!empty($arg)) {
  175. $sql = "SELECT ID,post_date,post_title,post_author, post_type, post_status FROM $wpdb->posts ";
  176. $sql .= " WHERE post_type IN (" . $arg . ") AND (post_status='inherit' OR post_status='publish')";
  177. $all_posts = $wpdb->get_results($sql);
  178. foreach ($all_posts as $post) {
  179. $post_summary = array();
  180. $user_info = get_userdata($post->post_author);
  181. $post_summary['protected'] = $this->post->in_custom_posts($post->ID) ? "checked='checked'" : "";
  182. $post_summary['ID'] = $post->ID;
  183. $post_summary['date'] = $post->post_date;
  184. $post_summary['title'] = '<a href="' . get_permalink($post->ID) . '" target="_blank">' . $post->post_title . '</a>';
  185. $post_summary['author'] = $user_info->user_login;
  186. $post_summary['type'] = $post->post_type;
  187. $post_summary['status'] = $post->post_status;
  188. $filtered_items[] = $post_summary;
  189. }
  190. }
  191. break;
  192. }
  193. $totalitems = count($filtered_items);
  194. $perpage = 100;
  195. $paged = !empty($_GET["paged"]) ? sanitize_text_field($_GET["paged"]) : '';
  196. if (empty($paged) || !is_numeric($paged) || $paged <= 0) {
  197. $paged = 1;
  198. }
  199. $totalpages = ceil($totalitems / $perpage);
  200. $offset = 0;
  201. if (!empty($paged) && !empty($perpage)) {
  202. $offset = ($paged - 1) * $perpage;
  203. }
  204. for ($i = $offset; $i < ((int) $offset + (int) $perpage) && !empty($filtered_items[$i]); $i++) {
  205. $all_items[] = $filtered_items[$i];
  206. }
  207. $this->set_pagination_args(array(
  208. "total_items" => $totalitems,
  209. "total_pages" => $totalpages,
  210. "per_page" => $perpage,
  211. ));
  212. $columns = $this->get_columns();
  213. $hidden = array();
  214. $sortable = $this->get_sortable_columns();
  215. $this->_column_headers = array($columns, $hidden, $sortable);
  216. $this->items = $all_items;
  217. }
  218. function no_items() {
  219. SwpmUtils::e('No items found.');
  220. }
  221. }