class.swpm-protection-base.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. abstract class SwpmProtectionBase {
  3. protected $bitmap;
  4. protected $posts;
  5. protected $pages;
  6. protected $comments;
  7. protected $categories;
  8. protected $attachments;
  9. protected $custom_posts;
  10. protected $details;
  11. protected $options;
  12. private function __construct() {
  13. }
  14. protected function init($level_id) {
  15. global $wpdb;
  16. $this->owning_level_id = $level_id;
  17. $query = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}swpm_membership_tbl WHERE "
  18. . (is_numeric($level_id) ? 'id = %d' : 'md5(id) = %s' ), $level_id);
  19. $result = $wpdb->get_row($query);
  20. $this->bitmap = isset($result->permissions) ? $result->permissions : 0;
  21. $this->posts = isset($result->post_list) ? (array) unserialize($result->post_list) : array();
  22. $this->pages = isset($result->page_list) ? (array) unserialize($result->page_list) : array();
  23. $this->comments = isset($result->comment_list) ? (array) unserialize($result->comment_list) : array();
  24. $this->categories = isset($result->category_list) ? (array) unserialize($result->category_list) : array();
  25. $this->attachments = isset($result->attachment_list) ? (array) unserialize($result->attachment_list) : array();
  26. $this->custom_posts = isset($result->custom_post_list) ? (array) unserialize($result->custom_post_list) : array();
  27. $this->options = isset($result->options) ? (array) unserialize($result->options) : array();
  28. $this->disable_bookmark = isset($result->disable_bookmark_list) ? (array) unserialize($result->disable_bookmark_list) : array();
  29. $this->details = (array) $result;
  30. }
  31. public function apply($ids, $type) {
  32. $post_types = get_post_types(array('public' => true, '_builtin' => false));
  33. if (in_array($type, $post_types)) {
  34. $type = 'custom_post';
  35. }
  36. return $this->update_perms($ids, true, $type);
  37. }
  38. public function remove($ids, $type) {
  39. $post_types = get_post_types(array('public' => true, '_builtin' => false));
  40. if (in_array($type, $post_types)) {
  41. $type = 'custom_post';
  42. }
  43. return $this->update_perms($ids, false, $type);
  44. }
  45. public function get_options() {
  46. return $this->options;
  47. }
  48. public function get_posts() {
  49. return $this->posts;
  50. }
  51. public function get_pages() {
  52. return $this->pages;
  53. }
  54. public function get_comments() {
  55. return $this->comments;
  56. }
  57. public function get_categories() {
  58. return $this->categories;
  59. }
  60. public function get_attachments() {
  61. return $this->attachments;
  62. }
  63. public function get_custom_posts() {
  64. return $this->custom_posts;
  65. }
  66. public function is_bookmark_disabled($id) {
  67. $posts = isset($this->disable_bookmark['posts']) ?
  68. (array) $this->disable_bookmark['posts'] : array();
  69. $pages = isset($this->disable_bookmark['pages']) ?
  70. (array) $this->disable_bookmark['pages'] : array();
  71. return in_array($id, $pages) || in_array($id, $posts);
  72. }
  73. public function in_posts($id) {
  74. return (/* ($this->bitmap&4)===4) && */in_array($id, (array) $this->posts));
  75. }
  76. public function in_pages($id) {
  77. return (/* ($this->bitmap&8)===8) && */ in_array($id, (array) $this->pages));
  78. }
  79. public function in_attachments($id) {
  80. return (/* ($this->bitmap&16)===16) && */in_array($id, (array) $this->attachments));
  81. }
  82. public function in_custom_posts($id) {
  83. return (/* ($this->bitmap&32)===32) && */ in_array($id, (array) $this->custom_posts));
  84. }
  85. public function in_comments($id) {
  86. return (/* ($this->bitmap&2)===2) && */ in_array($id, (array) $this->comments));
  87. }
  88. public function in_categories($id) {
  89. if (empty($this->categories))
  90. return false;
  91. return (/* ($this->bitmap&1)===1) && */ in_array($id, (array) $this->categories));
  92. }
  93. public function post_in_categories($post_id) {
  94. if (empty($this->categories)){
  95. return false;
  96. }
  97. $taxonomies = get_taxonomies(array('public' => true,'_builtin'=>false));
  98. if (!is_array($taxonomies) || empty($taxonomies)) {
  99. $taxonomies = 'category';
  100. } else {
  101. $taxonomies['category'] = 'category';
  102. }
  103. $terms = wp_get_post_terms( $post_id, $taxonomies, array('fields'=>'ids'));
  104. if(!is_array($terms)){
  105. return false;
  106. }
  107. foreach ($terms as $key=>$value){
  108. if (in_array($value, $this->categories)) {return true;}
  109. }
  110. return false;
  111. }
  112. public function in_parent_categories($id) {
  113. if (empty($this->categories)){
  114. return false;
  115. }
  116. $taxonomies = get_taxonomies(array('public' => true,'_builtin'=>false));
  117. if (!is_array($taxonomies) || empty($taxonomies)) {
  118. $taxonomies = 'category';
  119. } else {
  120. $taxonomies['category'] = 'category';
  121. }
  122. $terms = get_term($id, $taxonomies);
  123. if(!is_array($terms)){
  124. return false;
  125. }
  126. foreach ($terms as $term){
  127. if ($term->parent == 0) {continue;}
  128. if (in_array($term->parent, $this->categories)) {return true;}
  129. }
  130. return false;
  131. }
  132. public function post_in_parent_categories($post_id) {
  133. if (empty($this->categories)){
  134. return false;
  135. }
  136. $taxonomies = get_taxonomies(array('public' => true,'_builtin'=>false));
  137. if (!is_array($taxonomies) || empty($taxonomies)) {
  138. $taxonomies = 'category';
  139. } else {
  140. $taxonomies['category'] = 'category';
  141. }
  142. $terms = wp_get_post_terms( $post_id, $taxonomies, array('fields'=>'all'));
  143. if(!is_array($terms)){
  144. return false;
  145. }
  146. foreach ($terms as $term){
  147. if ($term->parent != 0 &&in_array($term->parent, $this->categories)) {
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. public function add_posts($ids) {
  154. return $this->update_perms($ids, true, 'post');
  155. }
  156. public function add_pages($ids) {
  157. return $this->update_perms($ids, true, 'page');
  158. }
  159. public function add_attachments($ids) {
  160. return $this->update_perms($ids, true, 'attachment');
  161. }
  162. public function add_comments($ids) {
  163. return $this->update_perms($ids, true, 'comment');
  164. }
  165. public function add_categories($ids) {
  166. return $this->update_perms($ids, true, 'category');
  167. }
  168. public function add_custom_posts($ids) {
  169. return $this->update_perms($ids, true, 'custom_post');
  170. }
  171. public function remove_posts($ids) {
  172. return $this->update_perms($ids, false, 'post');
  173. }
  174. public function remove_pages($ids) {
  175. return $this->update_perms($ids, false, 'page');
  176. }
  177. public function remove_attachments($ids) {
  178. return $this->update_perms($ids, false, 'attachment');
  179. }
  180. public function remove_comments($ids) {
  181. return $this->update_perms($ids, false, 'comment');
  182. }
  183. public function remove_categories($ids) {
  184. return $this->update_perms($ids, false, 'category');
  185. }
  186. public function remove_custom_posts($ids) {
  187. return $this->update_perms($ids, false, 'custom_post');
  188. }
  189. private function update_perms($ids, $set, $type) {
  190. $list = null;
  191. $index = '';
  192. if (empty($ids)) {
  193. return $this;
  194. }
  195. $ids = (array) $ids;
  196. switch ($type) {
  197. case 'page':
  198. $list = $this->pages;
  199. $index = 'page_list';
  200. break;
  201. case 'post':
  202. $list = $this->posts;
  203. $index = 'post_list';
  204. break;
  205. case 'attachment':
  206. $list = $this->attachments;
  207. $index = 'attachment_list';
  208. break;
  209. case 'comment':
  210. $list = $this->comments;
  211. $index = 'comment_list';
  212. break;
  213. case 'category':
  214. $list = $this->categories;
  215. $index = 'category_list';
  216. break;
  217. case 'custom_post':
  218. $list = $this->custom_posts;
  219. $index = 'custom_post_list';
  220. break;
  221. default:
  222. break;
  223. }
  224. if (!empty($index)) {
  225. if ($set) {
  226. $list = array_merge($list, $ids);
  227. $list = array_unique($list);
  228. } else {
  229. $list = array_diff($list, $ids);
  230. }
  231. switch ($type) {
  232. case 'page':
  233. $this->pages = $list;
  234. break;
  235. case 'post':
  236. $this->posts = $list;
  237. break;
  238. case 'attachment':
  239. $this->attachments = $list;
  240. break;
  241. case 'comment':
  242. $this->comments = $list;
  243. break;
  244. case 'category':
  245. $this->categories = $list;
  246. break;
  247. case 'custom_post':
  248. $this->custom_posts = $list;
  249. break;
  250. default:
  251. break;
  252. }
  253. $this->details[$index] = $list;
  254. }
  255. return $this;
  256. }
  257. public function save() {
  258. global $wpdb;
  259. $data = array();
  260. $list_type = array('page_list', 'post_list', 'attachment_list',
  261. 'custom_post_list', 'comment_list', 'category_list');
  262. foreach ($this->details as $key => $value) {
  263. if ($key == 'id')
  264. continue;
  265. if (is_serialized($value) || !in_array($key, $list_type))
  266. $data[$key] = $value;
  267. else
  268. $data[$key] = serialize($value);
  269. }
  270. $wpdb->update($wpdb->prefix . "swpm_membership_tbl", $data, array('id' => $this->owning_level_id));
  271. }
  272. public function get($key, $default = '') {
  273. if (isset($this->details[$key])) {
  274. return $this->details[$key];
  275. }
  276. return $default;
  277. }
  278. }