PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/broderboy/nycendurance-wordpress
PHP | 1101 lines | 734 code | 258 blank | 109 comment | 252 complexity | 0d0e775a77942124a4499293da41e154 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0, Apache-2.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. if( basename(__FILE__) == basename($_SERVER['SCRIPT_FILENAME']) )
  3. die( 'This page cannot be called directly.' );
  4. /**
  5. * Scoper PHP class for the WordPress plugin Role Scoper
  6. * role-scoper_main.php
  7. *
  8. * @author Kevin Behrens
  9. * @copyright Copyright 2012
  10. *
  11. */
  12. class Scoper
  13. {
  14. var $definitions;
  15. var $access_types;
  16. var $data_sources;
  17. var $taxonomies;
  18. var $cap_defs;
  19. var $role_defs;
  20. var $cap_interceptor; // legacy API
  21. // === Temporary status variables ===
  22. var $direct_file_access;
  23. var $listed_ids = array(); // $listed_ids[src_name][object_id] = true : general purpose memory cache for non-post data sources; primary use is with has_cap filter to avoid a separate db query for each listed item
  24. var $default_restrictions = array();
  25. // minimal config retrieval to support pre-init usage by WP_Scoped_User before text domain is loaded
  26. function Scoper() {
  27. $this->definitions = array( 'data_sources' => 'Data_Sources', 'taxonomies' => 'Taxonomies', 'cap_defs' => 'Capabilities', 'role_defs' => 'Roles' );
  28. require_once( dirname(__FILE__).'/definitions_cr.php' );
  29. if ( defined( 'RVY_VERSION' ) )
  30. $this->cap_interceptor = (object) array(); // legacy support for Revisionary < 1.1 which set flags on this object property
  31. }
  32. function load_config() {
  33. require_once( dirname(__FILE__).'/lib/agapetry_config_items.php');
  34. $this->access_types = new AGP_Config_Items();
  35. $this->access_types->init( cr_access_types() ); // 'front' and 'admin' are the hardcoded access types
  36. // establish access type for this http request
  37. $access_name = ( is_admin() || defined('XMLRPC_REQUEST') ) ? 'admin' : 'front';
  38. $access_name = apply_filters( 'scoper_access_name', $access_name ); // others plugins can apply additional criteria for treating a particular URL with wp-admin or front-end filtering
  39. if ( ! defined('CURRENT_ACCESS_NAME_RS') )
  40. define('CURRENT_ACCESS_NAME_RS', $access_name);
  41. // disable RS filtering of access type(s) if specified in realm options
  42. if ( ! is_admin() || ! defined('SCOPER_REALM_ADMIN_RS') ) { // don't remove items if the option is being editied
  43. if ( $disabled_access_types = scoper_get_option('disabled_access_types') )
  44. $this->access_types->remove_members_by_key($disabled_access_types, true);
  45. // If the detected access type (admin, front or custom) was "disabled", it is still detected, but we note that query filters should not be applied
  46. if ( ! $this->access_types->is_member($access_name) )
  47. define('DISABLE_QUERYFILTERS_RS', true);
  48. }
  49. // populate data_sources, taxonomies, cap_defs, role_defs arrays
  50. foreach( array_keys($this->definitions) as $topic )
  51. $this->load_definition( $topic );
  52. foreach( array_keys($this->definitions) as $topic )
  53. $this->$topic->lock();
  54. // clean up after 3rd party plugins (such as Role Scoping for NGG) which don't set object type and src_name properties for roles
  55. if ( has_filter( 'define_roles_rs' ) ) {
  56. require_once( dirname(__FILE__).'/extension-helper_rs.php' );
  57. scoper_adjust_legacy_extension_cfg( $this->role_defs, $this->cap_defs );
  58. }
  59. add_action( 'set_current_user', array( &$this, 'credit_blogroles' ) );
  60. $this->credit_blogroles();
  61. do_action('config_loaded_rs');
  62. }
  63. function credit_blogroles() {
  64. // credit non-logged and "no role" users for any anonymous roles
  65. global $current_rs_user;
  66. if ( $current_rs_user ) {
  67. if ( empty($current_rs_user->assigned_blog_roles) ) {
  68. foreach ( $this->role_defs->filter_keys( -1, array( 'anon_user_blogrole' => true ) ) as $role_handle) {
  69. $current_rs_user->assigned_blog_roles[ANY_CONTENT_DATE_RS][$role_handle] = true;
  70. $current_rs_user->blog_roles[ANY_CONTENT_DATE_RS][$role_handle] = true;
  71. }
  72. }
  73. if ( isset($current_rs_user->assigned_blog_roles) )
  74. $this->refresh_blogroles();
  75. }
  76. }
  77. function refresh_blogroles() {
  78. global $current_rs_user;
  79. if ( empty($current_rs_user) )
  80. return;
  81. $current_rs_user->merge_scoped_blogcaps();
  82. $GLOBALS['current_user']->allcaps = $current_rs_user->allcaps;
  83. foreach( array( 'groups', 'blog_roles', 'assigned_blog_roles' ) as $var ) {
  84. $GLOBALS['current_user']->$var = $current_rs_user->$var;
  85. }
  86. if ( $current_rs_user->ID ) {
  87. foreach ( array_keys($current_rs_user->assigned_blog_roles) as $date_key )
  88. $current_rs_user->blog_roles[$date_key] = $this->role_defs->add_contained_roles( $current_rs_user->assigned_blog_roles[$date_key] );
  89. }
  90. }
  91. function load_definition( $topic ) {
  92. $class_name = "CR_" . $this->definitions[$topic];
  93. require_once( strtolower($this->definitions[$topic]) . '_rs.php' );
  94. $filter_name = "define_" . strtolower($this->definitions[$topic]) . "_rs";
  95. $this->$topic = apply_filters( $filter_name, new $class_name( call_user_func("cr_{$topic}") ) );
  96. if ( 'role_defs' == $topic ) {
  97. $this->role_defs->role_caps = apply_filters('define_role_caps_rs', cr_role_caps() );
  98. if ( $user_role_caps = scoper_get_option( 'user_role_caps' ) )
  99. $this->role_defs->add_role_caps( $user_role_caps );
  100. $this->log_cap_usage( $this->role_defs, $this->cap_defs ); // add any otype associations from new user_role_caps, but don't remove an otype association due to disabled_role_caps
  101. if ( $disabled_role_caps = scoper_get_option( 'disabled_role_caps' ) )
  102. $this->role_defs->remove_role_caps( $disabled_role_caps );
  103. $this->role_defs->remove_invalid(); // currently don't allow additional custom-defined post, page or link roles
  104. $this->customize_role_objscope();
  105. // To support merging in of WP role assignments, always note actual WP-defined roles
  106. // regardless of which role type we are scoping with.
  107. $this->log_wp_roles( $this->role_defs );
  108. }
  109. }
  110. function log_cap_usage( &$role_defs, &$cap_defs ) {
  111. foreach( $role_defs->members as $role_handle => $role_def ) {
  112. foreach( array_keys( $role_defs->role_caps[$role_handle] ) as $cap_name ) {
  113. if ( empty( $cap_defs->members[$cap_name]->object_types ) || ! in_array( $role_def->object_type, $cap_defs->members[$cap_name]->object_types ) ) {
  114. if ( 'post' == $role_def->src_name )
  115. $cap_defs->members[$cap_name]->object_types[] = $role_def->object_type;
  116. elseif ( in_array( $role_def->src_name, array( 'link', 'group' ) ) ) // TODO: other data sources?
  117. $cap_defs->members[$cap_name]->object_types[] = $role_def->src_name;
  118. }
  119. }
  120. }
  121. }
  122. function customize_role_objscope() {
  123. foreach ( $this->role_defs->get_all_keys() as $role_handle ) {
  124. if ( ! empty($this->role_defs->members[$role_handle]->objscope_equivalents) ) {
  125. foreach( $this->role_defs->members[$role_handle]->objscope_equivalents as $equiv_key => $equiv_role_handle ) {
  126. if ( scoper_get_option( "{$equiv_role_handle}_role_objscope" ) ) { // If "Additional Object Role" option is set for this role, treat it as a regular direct-assigned Object Role
  127. if ( isset($this->role_defs->members[$equiv_role_handle]->valid_scopes) )
  128. $this->role_defs->members[$equiv_role_handle]->valid_scopes = array('blog' => 1, 'term' => 1, 'object' => 1);
  129. unset( $this->role_defs->members[$role_handle]->objscope_equivalents[$equiv_key] );
  130. if ( ! defined( 'DISABLE_OBJSCOPE_EQUIV_' . $role_handle ) )
  131. define( 'DISABLE_OBJSCOPE_EQUIV_' . $role_handle, true ); // prevent Role Caption / Abbrev from being substituted from equivalent role
  132. }
  133. }
  134. }
  135. }
  136. }
  137. function log_wp_roles( &$role_defs ) {
  138. global $wp_roles;
  139. if ( ! isset($wp_roles) )
  140. $wp_roles = new WP_Roles();
  141. // populate WP roles least-role-first to match RS roles
  142. $keys = array_keys($wp_roles->role_objects);
  143. $keys = array_reverse($keys);
  144. $cr_cap_names = $this->cap_defs->get_all_keys();
  145. $last_lock = $role_defs->locked;
  146. $role_defs->locked = false;
  147. foreach ( $keys as $role_name ) {
  148. if ( ! empty( $wp_roles->role_objects[$role_name]->capabilities ) ) {
  149. // remove any WP caps which are in array, but have value = false
  150. if ( $caps = array_intersect( $wp_roles->role_objects[$role_name]->capabilities, array(true) ) )
  151. $caps = array_intersect_key( $caps, array_flip($cr_cap_names) ); // we only care about WP caps that are RS-defined
  152. } else
  153. $caps = array();
  154. $role_defs->add( $role_name, 'wordpress', '', '', 'wp' );
  155. // temp hardcode for site-wide Nav Menu cap
  156. if ( ! empty( $caps['edit_theme_options'] ) )
  157. $caps['manage_nav_menus'] = true;
  158. $role_defs->role_caps['wp_' . $role_name] = $caps;
  159. }
  160. $role_defs->locked = $last_lock;
  161. }
  162. function init() {
  163. scoper_version_check();
  164. if ( ! isset($this->data_sources) )
  165. $this->load_config();
  166. $is_administrator = is_content_administrator_rs();
  167. if ( $doing_cron = defined('DOING_CRON') )
  168. if ( ! defined('DISABLE_QUERYFILTERS_RS') )
  169. define('DISABLE_QUERYFILTERS_RS', true);
  170. if ( ! $this->direct_file_access = strpos($_SERVER['QUERY_STRING'], 'rs_rewrite') )
  171. $this->add_main_filters();
  172. // ===== Special early exit if this is a plugin install script
  173. if ( is_admin() ) {
  174. if ( in_array( $GLOBALS['pagenow'], array( 'plugin-install.php', 'plugin-editor.php' ) ) ) {
  175. // flush RS cache on activation of any plugin, in case we cached results based on its presence / absence
  176. if ( ( ! empty($_POST) ) || ( ! empty($_REQUEST['action']) ) ) {
  177. if ( ! empty($_POST['networkwide']) || ( 'plugin-editor.php' == $GLOBALS['pagenow'] ) )
  178. wpp_cache_flush_all_sites();
  179. else
  180. wpp_cache_flush();
  181. }
  182. do_action( 'scoper_init' );
  183. return; // no further filtering on WP plugin maintenance scripts
  184. }
  185. }
  186. // =====
  187. require_once( dirname(__FILE__).'/attachment-interceptor_rs.php');
  188. $GLOBALS['attachment_interceptor'] = new AttachmentInterceptor_RS(); // .htaccess file is always there, so we always need to handle its rewrites
  189. // ===== Content Filters to limit/enable the current user
  190. $disable_queryfilters = defined('DISABLE_QUERYFILTERS_RS');
  191. if ( $disable_queryfilters ) {
  192. // Some wp-admin pages need to list pages or categories based on front-end access. Classic example is Subscribe2 categories checklist, included in Subscriber profile
  193. // In that case, filtering will be applied even if wp-admin filtering is disabled. API hook enables other plugins to defined their own "always filter" URIs.
  194. $always_filter_uris = apply_filters( 'scoper_always_filter_uris', array( 'p-admin/profile.php' ) );
  195. if ( in_array( $GLOBALS['pagenow'], $always_filter_uris ) || in_array( $GLOBALS['plugin_page_cr'], $always_filter_uris ) ) {
  196. $disable_queryfilters = false;
  197. break;
  198. }
  199. }
  200. // register a map_meta_cap filter to handle the type-specific meta caps we are forcing
  201. require_once( dirname(__FILE__).'/meta_caps_rs.php' );
  202. if ( ! $disable_queryfilters ) {
  203. if ( ! $is_administrator ) {
  204. if ( $this->direct_file_access ) {
  205. require_once( dirname(__FILE__).'/cap-interceptor-basic_rs.php'); // only need to support basic read_post / read_page check for direct file access
  206. $GLOBALS['cap_interceptor_basic'] = new CapInterceptorBasic_RS();
  207. } else {
  208. require_once( dirname(__FILE__).'/cap-interceptor_rs.php');
  209. $GLOBALS['cap_interceptor'] = new CapInterceptor_RS();
  210. }
  211. }
  212. // (also use content filters on front end to FILTER IN private content which WP inappropriately hides from administrators)
  213. if ( ( ! $is_administrator ) || $this->is_front() ) {
  214. require_once( dirname(__FILE__).'/query-interceptor_rs.php');
  215. $GLOBALS['query_interceptor'] = new QueryInterceptor_RS();
  216. }
  217. if ( ( ! $this->direct_file_access ) && ( ! $is_administrator || ! defined('XMLRPC_REQUEST') ) ) { // don't tempt trouble by adding hardway filters on XMLRPC for logged administrator
  218. $this->add_hardway_filters();
  219. if ( $this->is_front() || ! $is_administrator ) {
  220. require_once( dirname(__FILE__).'/terms-query-lib_rs.php');
  221. if ( awp_ver( '3.1' ) && ! defined( 'SCOPER_LEGACY_TERMS_FILTER' ) ) {
  222. require_once( dirname(__FILE__).'/terms-interceptor_rs.php');
  223. $GLOBALS['terms_interceptor'] = new TermsInterceptor_RS();
  224. } else
  225. require_once( dirname(__FILE__).'/hardway/hardway-taxonomy-legacy_rs.php');
  226. }
  227. }
  228. } // endif query filtering not disabled for this access type
  229. if ( $is_administrator ) {
  230. if ( $this->is_front() )
  231. require_once( 'comments-int-administrator_rs.php' );
  232. } else
  233. require_once( 'comments-interceptor_rs.php' );
  234. if ( is_admin() )
  235. $this->add_admin_ui_filters( $is_administrator );
  236. do_action( 'scoper_init' );
  237. // ===== end Content Filters
  238. } // end function init
  239. // filters which are only needed for the wp-admin UI
  240. function add_admin_ui_filters( $is_administrator ) {
  241. global $pagenow;
  242. // ===== Admin filters (menu and other basics) which are (almost) always loaded
  243. require_once( dirname(__FILE__).'/admin/admin_rs.php');
  244. $GLOBALS['scoper_admin'] = new ScoperAdmin();
  245. if ( 'async-upload.php' != $pagenow ) {
  246. if ( ! defined('DISABLE_QUERYFILTERS_RS') || $is_administrator ) {
  247. require_once( dirname(__FILE__).'/admin/filters-admin-ui_rs.php' );
  248. $GLOBALS['scoper_admin_filters_ui'] = new ScoperAdminFiltersUI();
  249. }
  250. }
  251. // =====
  252. // ===== Script-specific Admin filters
  253. if ( 'users.php' == $pagenow ) {
  254. require_once( dirname(__FILE__).'/admin/filters-admin-users_rs.php' );
  255. } elseif ( 'edit.php' == $pagenow ) {
  256. if ( ! defined('DISABLE_QUERYFILTERS_RS') || $is_administrator )
  257. require_once( dirname(__FILE__).'/admin/filters-admin-ui-listing_rs.php' );
  258. } elseif ( in_array( $pagenow, array( 'edit-tags.php', 'edit-link-categories.php' ) ) ) {
  259. if ( ! defined('DISABLE_QUERYFILTERS_RS') )
  260. require_once( dirname(__FILE__).'/admin/filters-admin-terms_rs.php' );
  261. }
  262. // =====
  263. if ( scoper_get_option( 'group_ajax' ) && ( isset( $_GET['rs_user_search'] ) || isset( $_GET['rs_group_search'] ) ) ) {
  264. require_once( dirname(__FILE__).'/admin/user_query_rs.php' );
  265. exit;
  266. }
  267. }
  268. function add_hardway_filters() {
  269. // port or low-level query filters to work around limitations in WP core API
  270. require_once( dirname(__FILE__).'/hardway/hardway_rs.php'); // need get_pages() filtering to include private pages for some 3rd party plugin config UI (Simple Section Nav)
  271. // buffering of taxonomy children is disabled with non-admin user logged in
  272. // But that non-admin user may add cats. Don't allow unfiltered admin to rely on an old copy of children
  273. global $wp_taxonomies;
  274. if ( ! empty($wp_taxonomies) ) {
  275. foreach ( array_keys($wp_taxonomies) as $taxonomy )
  276. add_filter ( "option_{$taxonomy}_children", create_function( '$option_value', "return rs_get_terms_children('$taxonomy', " . '$option_value );') );
  277. //add_filter("option_{$taxonomy}_children", create_function( '', "return rs_get_terms_children('$taxonomy');") );
  278. }
  279. if ( is_admin() || defined('XMLRPC_REQUEST') ) {
  280. global $pagenow;
  281. if ( ! in_array( $pagenow, array( 'plugin-editor.php', 'plugins.php' ) ) ) {
  282. global $plugin_page_cr;
  283. // low-level filtering for miscellaneous admin operations which are not well supported by the WP API
  284. $hardway_uris = array(
  285. 'index.php', 'revision.php', 'admin.php?page=rvy-revisions',
  286. 'post.php', 'post-new.php', 'edit.php',
  287. 'upload.php', 'edit-comments.php', 'edit-tags.php',
  288. 'profile.php', 'admin-ajax.php',
  289. 'link-manager.php', 'link-add.php', 'link.php',
  290. 'edit-link-category.php', 'edit-link-categories.php',
  291. 'media-upload.php', 'nav-menus.php'
  292. );
  293. $hardway_uris = apply_filters( 'scoper_admin_hardway_uris', $hardway_uris );
  294. // support for rs-config-ngg <= 1.0
  295. if ( defined('XMLRPC_REQUEST') || in_array( $pagenow, $hardway_uris ) || in_array( $plugin_page_cr, $hardway_uris ) || in_array( "p-admin/admin.php?page=$plugin_page_cr", $hardway_uris ) )
  296. require_once( dirname(__FILE__).'/hardway/hardway-admin_rs.php' );
  297. }
  298. } // endif is_admin or xmlrpc
  299. }
  300. // add filters which were skipped due to direct file access, but are now needed for the error page display
  301. function add_main_filters() {
  302. $is_admin = is_admin();
  303. $is_administrator = is_content_administrator_rs();
  304. $disable_queryfilters = defined('DISABLE_QUERYFILTERS_RS');
  305. $frontend_admin = false;
  306. if ( ! defined('DOING_CRON') ) {
  307. if ( $this->is_front() ) {
  308. if ( ! $disable_queryfilters )
  309. require_once( dirname(__FILE__).'/query-interceptor-front_rs.php');
  310. if ( ! $is_administrator ) {
  311. require_once( dirname(__FILE__).'/qry-front_non-administrator_rs.php');
  312. $GLOBALS['feed_interceptor'] = new FeedInterceptor_RS(); // file already required in role-scoper.php
  313. }
  314. require_once( dirname(__FILE__).'/template-interceptor_rs.php');
  315. $GLOBALS['template_interceptor'] = new TemplateInterceptor_RS();
  316. $frontend_admin = ! scoper_get_option('no_frontend_admin'); // potential performance enhancement
  317. if ( ! empty($_REQUEST['s']) && function_exists('relevanssi_query') ) {
  318. require_once( dirname(__FILE__).'/relevanssi-helper-front_rs.php' );
  319. $rel_helper_rs = new Relevanssi_Search_Filter_RS();
  320. }
  321. }
  322. // ===== Filters which are always loaded (except on plugin scripts), for any access type
  323. include_once( dirname(__FILE__).'/hardway/wp-patches_agp.php' ); // simple patches for WP
  324. if ( $this->is_front() || ( 'edit.php' == $GLOBALS['pagenow'] ) ) {
  325. require_once( dirname(__FILE__).'/query-interceptor-base_rs.php');
  326. $GLOBALS['query_interceptor_base'] = new QueryInterceptorBase_RS(); // listing filter used for role status indication in edit posts/pages and on front end by template functions
  327. }
  328. }
  329. // ===== Filters which support automated role maintenance following content creation/update
  330. // Require an explicitly set option to skip these for front end access, just in case other plugins modify content from the front end.
  331. if ( ( $is_admin || defined('XMLRPC_REQUEST') || $frontend_admin || defined('DOING_CRON') ) ) {
  332. require_once( dirname(__FILE__).'/admin/cache_flush_rs.php' );
  333. require_once( dirname(__FILE__).'/admin/filters-admin_rs.php' );
  334. $GLOBALS['scoper_admin_filters'] = new ScoperAdminFilters();
  335. if ( defined( 'RVY_VERSION' ) ) // Support Revisionary references to $scoper->filters_admin (TODO: eventually phase this out)
  336. $this->filters_admin =& $GLOBALS['scoper_admin_filters'];
  337. }
  338. // =====
  339. }
  340. function init_users_interceptor() {
  341. if ( ! isset($GLOBALS['users_interceptor']) ) {
  342. require_once( dirname(__FILE__).'/users-interceptor_rs.php');
  343. $GLOBALS['users_interceptor'] = new UsersInterceptor_RS();
  344. //log_mem_usage_rs( 'init Users Interceptor' );
  345. }
  346. return $GLOBALS['users_interceptor'];
  347. }
  348. // Primarily for internal use. Drops some features of WP core get_terms while adding the following versatility:
  349. // - supports any RS-defined taxonomy, with or without WP taxonomy schema
  350. // - optionally return term_id OR term_taxonomy_id as single column
  351. // - specify filtered or unfiltered via argument
  352. // - optionally get terms for a specific object
  353. // - option to order by term hierarchy (but structure as flat array)
  354. function get_terms($taxonomy, $filtering = true, $cols = COLS_ALL_RS, $object_id = 0, $args = array()) {
  355. if ( ! $tx = $this->taxonomies->get($taxonomy) )
  356. return array();
  357. global $wpdb;
  358. $defaults = array( 'order_by' => '', 'use_object_roles' => false, 'operation' => '' ); // IMPORTANT to default operation to nullstring
  359. $args = array_merge( $defaults, (array) $args );
  360. extract($args);
  361. if ( is_administrator_rs( $this->taxonomies->member_property( $taxonomy, 'object_source' ) ) )
  362. $filtering = false;
  363. // try to pull it out of wpcache
  364. $ckey = md5( $taxonomy . $cols . $object_id . serialize($args) . $order_by );
  365. if ( $filtering ) {
  366. $src_name = $this->taxonomies->member_property($taxonomy, 'object_source', 'name');
  367. $args['reqd_caps_by_otype'] = $this->get_terms_reqd_caps( $taxonomy, $operation, ADMIN_TERMS_FILTER_RS === $filtering );
  368. $ckey = md5( $ckey . serialize($args['reqd_caps_by_otype']) ); ; // can vary based on request URI
  369. global $current_rs_user;
  370. $cache_flag = 'rs_scoper_get_terms';
  371. $cache = $current_rs_user->cache_get($cache_flag);
  372. } else {
  373. $cache_flag = "all_terms";
  374. $cache_id = 'all';
  375. $cache = wpp_cache_get( $cache_id, $cache_flag );
  376. }
  377. if ( isset( $cache[ $ckey ] ) ) {
  378. return $cache[ $ckey ];
  379. }
  380. // call base class method to build query
  381. $terms_only = ( ! $filtering || empty($use_object_roles) );
  382. $query_base = $this->taxonomies->get_terms_query($taxonomy, $cols, $object_id, $terms_only );
  383. if ( ! $query_base )
  384. return array();
  385. $query = ( $filtering ) ? apply_filters('terms_request_rs', $query_base, $taxonomy, $args) : $query_base;
  386. // avoid sending alarms to SQL purists if this query was not modified by RS filter
  387. if ( $query_base == $query )
  388. $query = str_replace( 'WHERE 1=1 AND', 'WHERE', $query );
  389. if ( COL_ID_RS == $cols )
  390. $results = scoper_get_col($query);
  391. elseif ( COL_COUNT_RS == $cols )
  392. $results = intval( scoper_get_var($query) );
  393. else {
  394. // TODO: why is this still causing an extra (and costly) scoped query?
  395. /*
  396. // for COLS_ALL query, need to call core get_terms call in case another plugin is translating term names
  397. if ( has_filter( 'get_terms', array('ScoperHardwayTaxonomy', 'flt_get_terms') ) ) {
  398. remove_filter( 'get_terms', array('ScoperHardwayTaxonomy', 'flt_get_terms'), 1, 3 );
  399. $all_terms = get_terms($taxonomy);
  400. add_filter( 'get_terms', array('ScoperHardwayTaxonomy', 'flt_get_terms'), 1, 3 );
  401. $term_names = scoper_get_property_array( $all_terms, 'term_id', 'name' );
  402. }
  403. */
  404. $results = scoper_get_results($query);
  405. //scoper_restore_property_array( $results, $term_names, 'term_id', 'name' );
  406. if ( ORDERBY_HIERARCHY_RS == $order_by ) {
  407. require_once( dirname(__FILE__).'/admin/admin_lib_rs.php');
  408. if ( $src = $this->data_sources->get( $tx->source ) ) {
  409. if ( ! empty($src->cols->id) && ! empty($src->cols->parent) ) {
  410. require_once( dirname(__FILE__).'/admin/admin_lib-bulk-parent_rs.php');
  411. $results = ScoperAdminBulkParent::order_by_hierarchy($results, $src->cols->id, $src->cols->parent);
  412. }
  413. }
  414. }
  415. }
  416. $cache[ $ckey ] = $results;
  417. if ( $results || empty( $_POST ) ) { // todo: why do we get an empty array for unfiltered request for object terms early in POST processing? (on submission of a new post by a contributor)
  418. if ( $filtering )
  419. $current_rs_user->cache_force_set( $cache, $cache_flag );
  420. else
  421. wpp_cache_force_set( $cache_id, $cache, $cache_flag );
  422. }
  423. return $results;
  424. }
  425. function get_default_restrictions($scope, $args = array()) {
  426. $defaults = array( 'force_refresh' => false );
  427. $args = array_merge( $defaults, (array) $args );
  428. extract($args);
  429. if ( isset($this->default_restrictions[$scope]) && ! $force_refresh )
  430. return $this->default_restrictions[$scope];
  431. if ( empty($force_refresh) ) {
  432. $cache_flag = "rs_{$scope}_def_restrictions";
  433. $cache_id = md5(''); // maintain default id generation from previous versions
  434. $default_strict = wpp_cache_get($cache_id, $cache_flag);
  435. }
  436. if ( $force_refresh || ! is_array($default_strict) ) {
  437. global $wpdb;
  438. $qry = "SELECT src_or_tx_name, role_name FROM $wpdb->role_scope_rs WHERE role_type = 'rs' AND topic = '$scope' AND max_scope = '$scope' AND obj_or_term_id = '0'";
  439. $default_strict = array();
  440. if ( $results = scoper_get_results($qry) ) {
  441. foreach ( $results as $row ) {
  442. $role_handle = scoper_get_role_handle($row->role_name, 'rs');
  443. $default_strict[$row->src_or_tx_name][$role_handle] = true;
  444. if (OBJECT_SCOPE_RS == $scope) {
  445. if ( $objscope_equivalents = $this->role_defs->member_property($role_handle, 'objscope_equivalents') )
  446. foreach ( $objscope_equivalents as $equiv_role_handle )
  447. $default_strict[$row->src_or_tx_name][$equiv_role_handle] = true;
  448. }
  449. }
  450. }
  451. }
  452. $this->default_restrictions[$scope] = $default_strict;
  453. wpp_cache_set($cache_id, $default_strict, $cache_flag);
  454. return $default_strict;
  455. }
  456. // for any given role requirement, a strict term is one which won't blend in blog role assignments
  457. // (i.e. a term which requires the specified role to be assigned as a term role or object role)
  458. //
  459. // returns $arr['restrictions'][role_handle][obj_or_term_id] = array( 'assign_for' => $row->assign_for, 'inherited_from' => $row->inherited_from ),
  460. // ['unrestrictions'][role_handle][obj_or_term_id] = array( 'assign_for' => $row->assign_for, 'inherited_from' => $row->inherited_from )
  461. function get_restrictions($scope, $src_or_tx_name, $args = array()) {
  462. $def_cols = COL_ID_RS;
  463. // Note: propogating child restrictions are always directly assigned to the child term(s).
  464. // Use include_child_restrictions to force inclusion of restrictions that are set for child items only,
  465. // for direct admin of these restrictions and for propagation on term/object creation.
  466. $defaults = array( 'id' => 0, 'include_child_restrictions' => false,
  467. 'force_refresh' => false,
  468. 'cols' => $def_cols, 'return_array' => false );
  469. $args = array_merge( $defaults, (array) $args );
  470. extract($args);
  471. $cache_flag = "rs_{$scope}_restrictions_{$src_or_tx_name}";
  472. $cache_id = md5($src_or_tx_name . $cols . strval($return_array) . strval($include_child_restrictions) );
  473. if ( ! $force_refresh ) {
  474. $items = wpp_cache_get($cache_id, $cache_flag);
  475. if ( is_array($items) ) {
  476. if ( $id ) {
  477. foreach ( $items as $setting_type => $roles )
  478. foreach ( array_keys($roles) as $role_handle )
  479. $items[$setting_type][$role_handle] = array_intersect_key( $items[$setting_type][$role_handle], array( $id => true ) );
  480. }
  481. return $items;
  482. }
  483. }
  484. if ( ! isset($this->default_restrictions[$scope]) )
  485. $this->default_restrictions[$scope] = $this->get_default_restrictions($scope);
  486. global $wpdb;
  487. if ( ! empty($this->default_restrictions[$scope][$src_or_tx_name]) ) {
  488. if ( $strict_roles = array_keys($this->default_restrictions[$scope][$src_or_tx_name]) ) {
  489. if ( OBJECT_SCOPE_RS == $scope ) {
  490. // apply default_strict handling to objscope equivalents of each strict role
  491. foreach ( $strict_roles as $role_handle )
  492. if ( $objscope_equivalents = $this->role_defs->member_property($role_handle, 'objscope_equivalents') )
  493. $strict_roles = array_merge($strict_roles, $objscope_equivalents);
  494. $strict_roles = array_unique($strict_roles);
  495. }
  496. }
  497. $strict_role_in = "'" . implode("', '", scoper_role_handles_to_names($strict_roles) ) . "'";
  498. } else
  499. $strict_role_in = '';
  500. $items = array();
  501. if ( ! empty($strict_roles) ) {
  502. foreach ( $strict_roles as $role_handle )
  503. $items['unrestrictions'][$role_handle] = array(); // calling code will use this as an indication that the role is default strict
  504. }
  505. $default_strict_modes = array( false );
  506. if ( $strict_role_in )
  507. $default_strict_modes []= true;
  508. foreach ( $default_strict_modes as $default_strict ) {
  509. $setting_type = ( $default_strict ) ? 'unrestrictions' : 'restrictions';
  510. if ( TERM_SCOPE_RS == $scope )
  511. $max_scope = ( $default_strict ) ? 'blog' : 'term'; // note: max_scope='object' entries are treated as separate, overriding requirements
  512. else
  513. $max_scope = ( $default_strict ) ? 'blog' : 'object'; // Storage of 'blog' max_scope as object restriction does not eliminate any term restrictions. It merely indicates, for data sources that are default strict, that this object does not restrict roles
  514. if ( $default_strict )
  515. $role_clause = "AND role_name IN ($strict_role_in)";
  516. elseif ($strict_role_in)
  517. $role_clause = "AND role_name NOT IN ($strict_role_in)";
  518. else
  519. $role_clause = '';
  520. $for_clause = ( $include_child_restrictions ) ? '' : "AND require_for IN ('entity', 'both')";
  521. $qry_base = "FROM $wpdb->role_scope_rs WHERE role_type = 'rs' AND topic = '$scope' AND max_scope = '$max_scope' AND src_or_tx_name = '$src_or_tx_name' $for_clause $role_clause";
  522. if ( COL_COUNT_RS == $cols )
  523. $qry = "SELECT role_name, count(obj_or_term_id) AS item_count, require_for $qry_base GROUP BY role_name";
  524. else
  525. $qry = "SELECT role_name, obj_or_term_id, require_for AS assign_for, inherited_from $qry_base";
  526. if ( $results = scoper_get_results($qry) ) {
  527. foreach( $results as $row) {
  528. $role_handle = scoper_get_role_handle($row->role_name, 'rs');
  529. if ( COL_COUNT_RS == $cols )
  530. $items[$setting_type][$role_handle] = $row->item_count;
  531. elseif ( $return_array )
  532. $items[$setting_type][$role_handle][$row->obj_or_term_id] = array( 'assign_for' => $row->assign_for, 'inherited_from' => $row->inherited_from );
  533. else
  534. $items[$setting_type][$role_handle][$row->obj_or_term_id] = $row->assign_for;
  535. }
  536. }
  537. } // end foreach default_strict_mode
  538. wpp_cache_force_set($cache_id, $items, $cache_flag);
  539. if ( $id ) {
  540. foreach ( $items as $setting_type => $roles )
  541. foreach ( array_keys($roles) as $role_handle )
  542. $items[$setting_type][$role_handle] = array_intersect_key( $items[$setting_type][$role_handle], array( $id => true ) );
  543. }
  544. return $items;
  545. }
  546. // wrapper for back-compat with calling code expecting array without date limit dimension
  547. function qualify_terms($reqd_caps, $taxonomy = 'category', $qualifying_roles = '', $args = array()) {
  548. $terms = $this->qualify_terms_daterange( $reqd_caps, $taxonomy, $qualifying_roles, $args );
  549. if ( isset($terms['']) && is_array($terms['']) )
  550. return $terms[''];
  551. else
  552. return array();
  553. }
  554. // $qualifying_roles = array[role_handle] = 1 : qualifying roles
  555. // returns array of term_ids (terms which have at least one of the qualifying roles assigned)
  556. function qualify_terms_daterange($reqd_caps, $taxonomy = 'category', $qualifying_roles = '', $args = array()) {
  557. $defaults = array( 'user' => '', 'return_id_type' => COL_ID_RS, 'use_blog_roles' => true, 'ignore_restrictions' => false );
  558. if ( isset($args['qualifying_roles']) )
  559. unset($args['qualifying_roles']);
  560. if ( isset($args['reqd_caps']) )
  561. unset($args['reqd_caps']);
  562. $args = array_merge( $defaults, (array) $args );
  563. extract($args);
  564. if ( ! $qualifying_roles ) // calling function might save a little work or limit to a subset of qualifying roles
  565. $qualifying_roles = $this->role_defs->qualify_roles( $reqd_caps );
  566. if ( ! $this->taxonomies->is_member($taxonomy) )
  567. return array( '' => array() );
  568. if ( ! is_object($user) ) {
  569. $user = $GLOBALS['current_rs_user'];
  570. }
  571. // If the taxonomy does not require objects to have at least one term, there are no strict terms.
  572. if ( ! $this->taxonomies->member_property($taxonomy, 'requires_term') )
  573. $ignore_restrictions = true;
  574. if ( ! is_array($qualifying_roles) )
  575. $qualifying_roles = array($qualifying_roles => 1);
  576. // no need to serialize and md5 the whole user object
  577. if ( ! empty($user) )
  578. $args['user'] = $user->ID;
  579. // try to pull previous result out of memcache
  580. ksort($qualifying_roles);
  581. $rolereq_key = md5( serialize($reqd_caps) . serialize( array_keys($qualifying_roles) ) . serialize($args) );
  582. if ( isset($user->qualified_terms[$taxonomy][$rolereq_key]) )
  583. return $user->qualified_terms[$taxonomy][$rolereq_key];
  584. if ( ! $qualifying_roles )
  585. return array( '' => array() );
  586. $all_terms = $this->get_terms($taxonomy, UNFILTERED_RS, COL_ID_RS); // returns term_id, even for WP > 2.3
  587. if ( ! isset($user->term_roles[$taxonomy]) )
  588. $user->get_term_roles_daterange($taxonomy); // returns term_id for categories
  589. $good_terms = array( '' => array() );
  590. if ( $user->term_roles[$taxonomy] ) {
  591. foreach ( array_keys($user->term_roles[$taxonomy]) as $date_key ) {
  592. //narrow down to roles which satisfy this call AND are owned by current user
  593. if ( $good_terms[$date_key] = array_intersect_key( $user->term_roles[$taxonomy][$date_key], $qualifying_roles ) )
  594. // flatten from term_roles_terms[role_handle] = array of term_ids
  595. // to term_roles_terms = array of term_ids
  596. $good_terms[$date_key] = agp_array_flatten( $good_terms[$date_key] );
  597. }
  598. }
  599. if ( $use_blog_roles ) {
  600. foreach ( array_keys($user->blog_roles) as $date_key ) {
  601. $user_blog_roles = array_intersect_key( $user->blog_roles[$date_key], $qualifying_roles );
  602. // Also include user's WP blogrole(s) which correspond to the qualifying RS role(s)
  603. if ( $wp_qualifying_roles = $this->role_defs->qualify_roles($reqd_caps, 'wp') ) {
  604. if ( $user_blog_roles_wp = array_intersect_key( $user->blog_roles[$date_key], $wp_qualifying_roles ) ) {
  605. // Credit user's qualifying WP blogrole via equivalent RS role(s)
  606. // so we can also enforce "term restrictions", which are based on RS roles
  607. $user_blog_roles_via_wp = $this->role_defs->get_contained_roles( array_keys($user_blog_roles_wp), false, 'rs' );
  608. $user_blog_roles_via_wp = array_intersect_key( $user_blog_roles_via_wp, $qualifying_roles );
  609. $user_blog_roles = array_merge( $user_blog_roles, $user_blog_roles_via_wp );
  610. }
  611. }
  612. if ( $user_blog_roles ) {
  613. if ( empty($ignore_restrictions) ) {
  614. // array of term_ids that require the specified role to be assigned via taxonomy or object role (user blog caps ignored)
  615. $strict_terms = $this->get_restrictions(TERM_SCOPE_RS, $taxonomy);
  616. } else
  617. $strict_terms = array();
  618. foreach ( array_keys($user_blog_roles) as $role_handle ) {
  619. if ( isset($strict_terms['restrictions'][$role_handle]) && is_array($strict_terms['restrictions'][$role_handle]) )
  620. $terms_via_this_role = array_diff( $all_terms, array_keys($strict_terms['restrictions'][$role_handle]) );
  621. elseif ( isset($strict_terms['unrestrictions'][$role_handle]) && is_array($strict_terms['unrestrictions'][$role_handle]) )
  622. $terms_via_this_role = array_intersect( $all_terms, array_keys( $strict_terms['unrestrictions'][$role_handle] ) );
  623. else
  624. $terms_via_this_role = $all_terms;
  625. if( $good_terms[$date_key] )
  626. $good_terms[$date_key] = array_merge( $good_terms[$date_key], $terms_via_this_role );
  627. else
  628. $good_terms[$date_key] = $terms_via_this_role;
  629. }
  630. }
  631. }
  632. }
  633. foreach ( array_keys($good_terms) as $date_key ) {
  634. if ( $good_terms[$date_key] = array_intersect( $good_terms[$date_key], $all_terms ) ) // prevent orphaned category roles from skewing access
  635. $good_terms[$date_key] = array_unique( $good_terms[$date_key] );
  636. // if COL_TAXONOMY_ID_RS, return a term_taxonomy_id instead of term_id
  637. if ( $good_terms[$date_key] && (COL_TAXONOMY_ID_RS == $return_id_type) && taxonomy_exists($taxonomy) ) {
  638. $all_terms_cols = $this->get_terms( $taxonomy, UNFILTERED_RS );
  639. $good_tt_ids = array();
  640. foreach ( $good_terms[$date_key] as $term_id )
  641. foreach ( array_keys($all_terms_cols) as $termkey )
  642. if ( $all_terms_cols[$termkey]->term_id == $term_id ) {
  643. $good_tt_ids []= $all_terms_cols[$termkey]->term_taxonomy_id;
  644. break;
  645. }
  646. $good_terms[$date_key] = $good_tt_ids;
  647. }
  648. }
  649. $user->qualified_terms[$taxonomy][$rolereq_key] = $good_terms;
  650. return $good_terms;
  651. }
  652. // account for different contexts of get_terms calls
  653. // (Scoped roles can dictate different results for front end, edit page/post, manage categories)
  654. function get_terms_reqd_caps( $taxonomy, $operation = '', $is_term_admin = false ) {
  655. global $pagenow;
  656. if ( ! $src_name = $this->taxonomies->member_property( $taxonomy, 'object_source' ) ) {
  657. if ( taxonomy_exists( $taxonomy ) )
  658. $src_name = 'post';
  659. }
  660. $return_caps = array();
  661. $is_term_admin = $is_term_admin
  662. || in_array( $pagenow, array( 'edit-tags.php' ) )
  663. || ( 'nav_menu' == $taxonomy && ( 'nav-menus.php' == $pagenow )
  664. || ( ( 'admin-ajax.php' == $pagenow ) && ( ! empty($_REQUEST['action']) && in_array( $_REQUEST['action'], array( 'add-menu-item', 'menu-locations-save' ) ) ) )
  665. ); // possible TODO: abstract for non-WP taxonomies
  666. if ( $is_term_admin ) {
  667. // query pertains to the management of terms
  668. if ( 'post' == $src_name ) {
  669. $taxonomy_obj = get_taxonomy( $taxonomy );
  670. $return_caps[$taxonomy] = array( $taxonomy_obj->cap->manage_terms );
  671. } elseif ( 'link_category' == $taxonomy ) {
  672. $return_caps[$taxonomy] = array( 'manage_categories' );
  673. } else {
  674. global $scoper;
  675. $cap_defs = $scoper->cap_defs->get_matching( $src_name, $taxonomy, OP_ADMIN_RS );
  676. $return_caps[$taxonomy] = $cap_defs ? array_keys( $cap_defs ) : array();
  677. }
  678. } else {
  679. // query pertains to reading or editing content within certain terms, or adding terms to content
  680. $base_caps_only = true;
  681. if ( 'post' == $src_name ) {
  682. if ( ! $operation )
  683. $operation = ( $this->is_front() || ( 'profile.php' == $pagenow ) || ( is_admin() && ( 's2' == $GLOBALS['plugin_page'] ) ) ) ? 'read' : 'edit'; // hack to support subscribe2 categories checklist
  684. $status = ( 'read' == $operation ) ? 'publish' : 'draft';
  685. // terms query should be limited to a single object type for post.php, post-new.php, so only return caps for that object type (TODO: do this in wp-admin regardless of URI ?)
  686. if ( in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) )
  687. $object_type = cr_find_post_type();
  688. } else {
  689. if ( ! $operation )
  690. $operation = ( $this->is_front() ) ? 'read' : 'edit';
  691. $status = '';
  692. }
  693. // The return array will indicate term role enable / disable, as well as associated capabilities
  694. if ( ! empty($object_type) )
  695. $check_object_types = array( $object_type );
  696. else {
  697. if ( $check_object_types = (array) $this->data_sources->member_property( $src_name, 'object_types' ) )
  698. $check_object_types = array_keys( $check_object_types );
  699. }
  700. if ( 'post' == $src_name )
  701. $use_post_types = scoper_get_option( 'use_post_types' );
  702. $enabled_object_types = array();
  703. foreach ( $check_object_types as $_object_type ) {
  704. if ( $use_term_roles = scoper_get_otype_option( 'use_term_roles', $src_name, $_object_type ) )
  705. if ( ! empty( $use_term_roles[$taxonomy] ) ) {
  706. if ( ( 'post' != $src_name ) || ! empty( $use_post_types[$_object_type] ) )
  707. $enabled_object_types []= $_object_type;
  708. }
  709. }
  710. foreach( $enabled_object_types as $object_type )
  711. $return_caps[$object_type] = cr_get_reqd_caps( $src_name, $operation, $object_type, $status, $base_caps_only );
  712. }
  713. return $return_caps;
  714. }
  715. function users_who_can($reqd_caps, $cols = COLS_ALL_RS, $object_src_name = '', $object_id = 0, $args = array() ) {
  716. // if there are not capability requirements, no need to load Users_Interceptor filtering class
  717. if ( ! $reqd_caps ) {
  718. if ( COL_ID_RS == $cols )
  719. $qcols = 'ID';
  720. elseif ( COLS_ID_NAME_RS == $cols )
  721. $qcols = "ID, user_login AS display_name"; // calling code assumes display_name property for user or group object
  722. elseif ( COLS_ID_DISPLAYNAME_RS == $cols )
  723. $qcols = "ID, display_name";
  724. elseif ( COLS_ALL_RS == $cols )
  725. $qcols = "*";
  726. else
  727. $qcols = $cols;
  728. global $wpdb;
  729. $orderby = ( $cols == COL_ID_RS ) ? '' : 'ORDER BY display_name';
  730. if ( IS_MU_RS && ! scoper_get_option( 'mu_sitewide_groups' ) && ! defined( 'FORCE_ALL_SITE_USERS_RS' ) )
  731. $qry = "SELECT $qcols FROM $wpdb->users INNER JOIN $wpdb->usermeta AS um ON $wpdb->users.ID = um.user_id AND um.meta_key = '{$wpdb->prefix}capabilities' $orderby";
  732. else
  733. $qry = "SELECT $qcols FROM $wpdb->users $orderby";
  734. if ( COL_ID_RS == $cols )
  735. return scoper_get_col( $qry );
  736. else
  737. return scoper_get_results( $qry );
  738. } else {
  739. $defaults = array( 'where' => '', 'orderby' => '', 'disable_memcache' => false, 'group_ids' => '', 'force_refresh' => false, 'force_all_users' => false );
  740. $args = array_merge( $defaults, (array) $args );
  741. extract($args);
  742. $cache_flag = "rs_users_who_can";
  743. $cache_id = md5(serialize($reqd_caps) . $cols . 'src' . $object_src_name . 'id' . $object_id . serialize($args) );
  744. if ( ! $force_refresh ) {
  745. // if we already have the results cached, no need to load Users_Interceptor filtering class
  746. $users = wpp_cache_get($cache_id, $cache_flag);
  747. if ( is_array($users) )
  748. return $users;
  749. }
  750. $this->init_users_interceptor();
  751. $users = $GLOBALS['users_interceptor']->users_who_can($reqd_caps, $cols, $object_src_name, $object_id, $args );
  752. wpp_cache_set($cache_id, $users, $cache_flag);
  753. return $users;
  754. }
  755. }
  756. function groups_who_can($reqd_caps, $cols = COLS_ALL_RS, $object_src_name = '', $object_id = 0, $args = array() ) {
  757. $this->init_users_interceptor();
  758. return $GLOBALS['users_interceptor']->groups_who_can($reqd_caps, $cols, $object_src_name, $object_id, $args );
  759. }
  760. function is_front() {
  761. return ( defined('CURRENT_ACCESS_NAME_RS') && ( 'front' == CURRENT_ACCESS_NAME_RS ) );
  762. }
  763. // returns array of role names which have the required caps (or their basecap equivalent)
  764. // AND have been applied to at least one object, for any user or group
  765. function qualify_object_roles( $reqd_caps, $object_type = '', $user = '', $base_caps_only = false ) {
  766. $roles = array();
  767. if ( $base_caps_only )
  768. $reqd_caps = $this->cap_defs->get_base_caps($reqd_caps);
  769. $roles = $this->role_defs->qualify_roles($reqd_caps, 'rs', $object_type);
  770. return $this->confirm_object_scope( $roles, $user );
  771. }
  772. // $roles[$role_handle] = array
  773. // returns arr[$role_handle]
  774. function confirm_object_scope( $roles, $user = '' ) {
  775. foreach ( array_keys($roles) as $role_handle ) {
  776. if ( empty( $this->role_defs->members[$role_handle]->valid_scopes['object'] ) )
  777. unset( $roles[$role_handle] );
  778. }
  779. if ( ! $roles )
  780. return array();
  781. if ( is_object($user) )
  782. $applied_obj_roles = $this->get_applied_object_roles( $user );
  783. elseif ( empty($user) ) {
  784. $applied_obj_roles = $this->get_applied_object_roles( $GLOBALS['current_rs_user'] );
  785. } else // -1 value passed to indicate check for all users
  786. $applied_obj_roles = $this->get_applied_object_roles();
  787. return array_intersect_key( $roles, $applied_obj_roles );
  788. }
  789. // returns array of role_handles which have been applied to any object
  790. // if $user arg is supplied, returns only roles applied for that user (or that user's groups)
  791. function get_applied_object_roles( $user = '' ) {
  792. if ( is_object( $user ) ) {
  793. $cache_flag = 'rs_object-roles'; // v 1.1: changed cache key from "object_roles" to "object-roles" to match new key format for blog, term roles
  794. $cache = $user->cache_get($cache_flag);
  795. $limit = '';
  796. $u_g_clause = $user->get_user_clause('');
  797. } else {
  798. $cache_flag = 'rs_applied_object-roles'; // v 1.1: changed cache key from "object_roles" to "object-roles" to match new key format for blog, term roles
  799. $cache_id = 'all';
  800. $cache = wpp_cache_get($cache_id, $cache_flag);
  801. $u_g_clause = '';
  802. }
  803. if ( is_array($cache) )
  804. return $cache;
  805. $role_handles = array();
  806. global $wpdb;
  807. // object roles support date limits, but content date limits (would be redundant and a needless performance hit)
  808. $duration_clause = scoper_get_duration_clause( '', $wpdb->user2role2object_rs );
  809. if ( $role_names = scoper_get_col("SELECT DISTINCT role_name FROM $wpdb->user2role2object_rs WHERE role_type='rs' AND scope='object' $duration_clause $u_g_clause") )
  810. $role_handles = scoper_role_names_to_handles($role_names, 'rs', true); //arg: return role keys as array key
  811. if ( is_object($user) ) {
  812. $user->cache_force_set($role_handles, $cache_flag);
  813. } else
  814. wpp_cache_force_set($cache_id, $role_handles, $cache_flag);
  815. return $role_handles;
  816. }
  817. function user_can_edit_blogwide( $src_name = '', $object_type = '', $args = '' ) {
  818. if ( is_administrator_rs($src_name) )
  819. return true;
  820. require_once( dirname(__FILE__).'/admin/permission_lib_rs.php' );
  821. return user_can_edit_blogwide_rs($src_name, $object_type, $args);
  822. }
  823. } // end Scoper class
  824. // (needed to stop using shared core library function with Revisionary due to changes in meta_flag handling)
  825. if ( ! function_exists('awp_user_can') ) {
  826. function awp_user_can( $reqd_caps, $object_id = 0, $user_id = 0, $meta_flags = array() ) {
  827. return cr_user_can( $reqd_caps, $object_id, $user_id, $meta_flags );
  828. }
  829. }
  830. // equivalent to current_user_can,
  831. // except it supports array of reqd_caps, supports non-current user, and does not support numeric reqd_caps
  832. function cr_user_can( $reqd_caps, $object_id = 0, $user_id = 0, $meta_flags = array() ) {
  833. if ( ! $user_id ) {
  834. if ( function_exists('is_super_admin') && is_super_admin() )
  835. return true;
  836. if ( is_content_administrator_rs() || ! function_exists( '_cr_user_can' ) )
  837. return current_user_can( $reqd_caps );
  838. }
  839. if ( function_exists( '_cr_user_can' ) )
  840. return _cr_user_can( $reqd_caps, $object_id, $user_id, $meta_flags );
  841. }
  842. ?>