PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/role-scoper/analyst_rs.php

https://bitbucket.org/broderboy/nycendurance-wordpress
PHP | 231 lines | 165 code | 51 blank | 15 comment | 45 complexity | 0a860f5c50e71f044aee991e72075775 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0, Apache-2.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * ScoperAnalyst PHP class for the WordPress plugin Role Scoper
  4. * analyst_rs.php
  5. *
  6. * @author Kevin Behrens
  7. * @copyright Copyright 2011
  8. *
  9. */
  10. class ScoperAnalyst {
  11. function identify_protected_attachments( $attachment_id = 0, $guid = '', $cols = '', $args = array() ) {
  12. if ( $guid && empty( $args['guid'] ) )
  13. $args = array_merge( $args, array( 'guid' => $guid ) );
  14. return ScoperAnalyst::identify_protected_posts( $attachment_id, true, $cols, $args );
  15. }
  16. function identify_protected_posts( $attachment_id = 0, $attachments = false, $cols = '', $args = array() ) {
  17. $defaults = array( 'use_object_restrictions' => true, 'use_term_restrictions' => true, 'use_private_status' => true, 'guid' => '' );
  18. $args = array_merge( $defaults, (array) $args );
  19. extract($args);
  20. global $wpdb, $scoper;
  21. if ( ! isset($scoper) || is_null($scoper) ) {
  22. scoper_get_init_options();
  23. scoper_init();
  24. }
  25. if ( empty($scoper->taxonomies) )
  26. $scoper->load_config();
  27. $restricted_roles = array();
  28. $unrestricted_roles = array(); // TODO: also protect uploads based on restriction of other taxonomies
  29. $restricted_terms = array();
  30. $restricted_objects = array();
  31. $term_restriction_clause = '';
  32. $object_restriction_clause = '';
  33. $limit_clause = '';
  34. $unattached_clause = '';
  35. global $scoper;
  36. $reader_roles = array();
  37. foreach( $scoper->role_defs->role_caps as $role_handle => $role_caps ) {
  38. $caps_by_op = $scoper->cap_defs->organize_caps_by_op( array_keys($role_caps) );
  39. if ( ( count( $caps_by_op ) == 1 ) &&( 'read' == key($caps_by_op ) ) )
  40. $reader_roles[]= $role_handle;
  41. }
  42. $role_clause = "AND rs.role_name IN ('" . implode( "','", scoper_role_handles_to_names($reader_roles) ) . "')";
  43. //if ( $use_private_status )
  44. // $role_clause = ( 'rs' == SCOPER_ROLE_TYPE ) ? "AND rs.role_name IN ('post_reader', 'page_reader')" : ''; // if also checking for private status, don't need to check for restriction of private_reader roles
  45. //else
  46. // $role_clause = ( 'rs' == SCOPER_ROLE_TYPE ) ? "AND rs.role_name IN ('post_reader', 'page_reader', 'private_post_reader', 'private_page_reader')" : '';
  47. if ( $use_term_restrictions ) {
  48. $term_restriction_query = "SELECT rs.obj_or_term_id AS term_id, rs.role_name, rs.max_scope FROM $wpdb->role_scope_rs AS rs "
  49. . "INNER JOIN $wpdb->term_taxonomy AS tt ON tt.taxonomy = rs.src_or_tx_name AND tt.taxonomy = 'category' AND tt.term_taxonomy_id = rs.obj_or_term_id "
  50. . "WHERE rs.role_type = 'rs' AND rs.require_for IN ('entity', 'both') AND rs.topic = 'term' $role_clause";
  51. $term_default_restriction_query = "SELECT rs.role_name FROM $wpdb->role_scope_rs AS rs "
  52. . "WHERE rs.role_type = 'rs' AND rs.require_for IN ('children', 'both') AND rs.topic = 'term' AND rs.max_scope = 'term' AND rs.src_or_tx_name = 'category' AND rs.obj_or_term_id = '0' $role_clause";
  53. $all_terms = array();
  54. $all_terms['category'] = $scoper->get_terms( 'category', false, COL_ID_RS );
  55. if ( $results = scoper_get_results( $term_restriction_query ) ) {
  56. foreach ( $results as $row ) {
  57. if ( 'blog' == $row->max_scope )
  58. $unrestricted_roles['category'][$row->role_name] []= $row->term_id;
  59. else
  60. $restricted_roles['category'][$row->role_name] []= $row->term_id;
  61. }
  62. }
  63. // if there a role is default-restricted, mark all terms as restricted (may be unrestricted later)
  64. if ( $results = scoper_get_col( $term_default_restriction_query ) ) {
  65. foreach ( $results as $role_name ) {
  66. if ( isset( $unrestricted_roles['category'][$role_name] ) )
  67. $default_restricted = array_diff( $all_terms['category'], $unrestricted_roles['category'][$role_name] );
  68. else
  69. $default_restricted = $all_terms['category'];
  70. if ( isset( $restricted_roles['category'][$role_name] ) )
  71. $restricted_roles['category'][$role_name] = array_unique( array_merge( $restricted_roles['category'][$role_name], $default_restricted ) );
  72. else
  73. $restricted_roles['category'][$role_name] = $default_restricted;
  74. }
  75. }
  76. $restricted_terms['category'] = isset($restricted_roles['category']) ? agp_array_flatten( $restricted_roles['category'] ) : array();
  77. if ( $restricted_terms['category'] ) {
  78. $term_restriction_clause = "OR post_parent IN ( SELECT $wpdb->posts.ID FROM $wpdb->posts "
  79. . "INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = $wpdb->posts.ID "
  80. . "WHERE tr.term_taxonomy_id IN ('" . implode( "','", $restricted_terms['category'] ) . "') )";
  81. }
  82. }
  83. if ( $attachment_id ) {
  84. if ( is_array($attachment_id) )
  85. $id_clause = "AND ID IN ('" . implode( "','", $attachment_id ) . "')";
  86. else {
  87. $id_clause = "AND ID = '$attachment_id'";
  88. $limit_clause = 'LIMIT 1';
  89. }
  90. } elseif ( $guid )
  91. $id_clause = "AND guid = '$file_path'";
  92. else
  93. $id_clause = '';
  94. if ( defined( 'SCOPER_NO_THUMBNAIL_FILTER' ) ) {
  95. if ( $thumbnail_ids = scoper_get_col( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id'" ) ) {
  96. $id_clause .= " AND ID NOT IN ('" . implode( "','", $thumbnail_ids ) . "')";
  97. }
  98. }
  99. if ( $attachments ) {
  100. // to reduce pool of objects, we only care about those that have an attachment
  101. $attachment_query = "SELECT $wpdb->posts.ID FROM $wpdb->posts WHERE $wpdb->posts.ID IN ( SELECT post_parent FROM $wpdb->posts WHERE post_type = 'attachment' $id_clause ) ";
  102. }
  103. if ( $use_object_restrictions ) {
  104. $object_restriction_query = "SELECT rs.obj_or_term_id AS obj_id, rs.role_name, rs.max_scope FROM $wpdb->role_scope_rs AS rs "
  105. . "WHERE rs.role_type = 'rs' AND rs.require_for IN ('entity', 'both') AND rs.topic = 'object' AND rs.src_or_tx_name = 'post' $role_clause AND rs.obj_or_term_id IN ( $attachment_query )";
  106. $object_default_restriction_query = "SELECT rs.role_name FROM $wpdb->role_scope_rs AS rs "
  107. . "WHERE rs.require_for IN ('children', 'both') AND rs.topic = 'object' AND rs.max_scope = 'object' AND rs.src_or_tx_name = 'post' AND rs.obj_or_term_id = '0' $role_clause";
  108. $all_objects = array();
  109. $all_objects['post'] = scoper_get_col( $attachment_query );
  110. $restricted_roles = array();
  111. $unrestricted_roles = array();
  112. if ( $results = scoper_get_results( $object_restriction_query ) ) {
  113. foreach ( $results as $row ) {
  114. if ( 'blog' == $row->max_scope )
  115. $unrestricted_roles['post'][$row->role_name] []= $row->obj_id;
  116. else
  117. $restricted_roles['post'][$row->role_name] []= $row->obj_id;
  118. }
  119. }
  120. // if there a role is default-restricted, mark all terms as restricted (may be unrestricted later)
  121. if ( $results = scoper_get_col( $object_default_restriction_query ) ) {
  122. foreach ( $results as $role_name ) {
  123. if ( isset( $unrestricted_roles['category'][$role_name] ) )
  124. $default_restricted = array_diff( $all_terms['post'], $unrestricted_roles['post'][$role_name] );
  125. else
  126. $default_restricted = $all_objects['post'];
  127. if ( isset( $restricted_roles['post'][$role_name] ) )
  128. $restricted_roles['post'][$role_name] = array_unique( array_merge( $restricted_roles['post'][$role_name], $default_restricted ) );
  129. else
  130. $restricted_roles['post'][$role_name] = $default_restricted;
  131. }
  132. }
  133. if ( ! empty( $restricted_roles ) ) {
  134. $restricted_objects['post'] = array_unique( agp_array_flatten( $restricted_roles['post'] ) );
  135. if ( $restricted_objects['post'] )
  136. $object_restriction_clause = "OR post_parent IN ( SELECT ID FROM $wpdb->posts WHERE ID IN ('" . implode( "','", $restricted_objects['post'] ) . "') )";
  137. }
  138. }
  139. if ( $use_private_status ) {
  140. $status_query = "AND post_parent IN ( SELECT $wpdb->posts.ID FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'private' )";
  141. }
  142. if ( $attachments ) {
  143. $attachment_type_clause = "post_type = 'attachment' AND";
  144. $unattached_clause = ( defined('SCOPER_BLOCK_UNATTACHED_UPLOADS') ) ? " OR post_parent < 1" : '';
  145. }
  146. $single_col = false;
  147. if ( COLS_ALL_RS === $cols )
  148. $query_cols = '*';
  149. elseif ( COL_ID_RS == $cols ) {
  150. $query_cols = 'ID';
  151. $single_col = true;
  152. } elseif ( COLS_ID_DISPLAYNAME_RS == $cols ) {
  153. if ( $attachment )
  154. $query_cols = 'ID, post_title, guid';
  155. else
  156. $query_cols = 'ID, post_title';
  157. } else {
  158. if ( $attachments )
  159. $query_cols = 'ID, guid';
  160. else {
  161. $query_cols = 'ID';
  162. $single_col = true;
  163. }
  164. }
  165. $query = "SELECT $query_cols FROM $wpdb->posts WHERE $attachment_type_clause ( 1=1 $status_query $term_restriction_clause $object_restriction_clause $unattached_clause ) $id_clause ORDER BY ID DESC $limit_clause";
  166. if ( $attachment_id && ! is_array( $attachment_id ) ) {
  167. if ( $single_col )
  168. $results = scoper_get_var( $query );
  169. else
  170. $results = scoper_get_row( $query );
  171. } else {
  172. if ( $single_col )
  173. $results = scoper_get_col( $query );
  174. else
  175. $results = scoper_get_results( $query );
  176. }
  177. return $results;
  178. }
  179. }
  180. ?>