/** * Add restriction options to the edit category page * * @access public * @since 2.0 * @return void */ function rcp_category_edit_meta_fields($term) { // retrieve the existing value(s) for this meta field. This returns an array $term_meta = rcp_get_term_restrictions($term->term_id); $access_level = isset($term_meta['access_level']) ? absint($term_meta['access_level']) : 0; $subscription_levels = isset($term_meta['subscriptions']) ? array_map('absint', $term_meta['subscriptions']) : array(); ?> <tr> <th scope="row"><?php _e('Paid Only?', 'rcp'); ?> </th> <td> <label for="rcp_category_meta[paid_only]"> <input type="checkbox" name="rcp_category_meta[paid_only]" id="rcp_category_meta[paid_only]" value="1"<?php checked(true, isset($term_meta['paid_only'])); ?> > <span class="description"><?php _e('Restrict items in this category to paid subscribers only?', 'rcp'); ?> </span> </label> </td> </tr> <tr> <th scope="row"><?php _e('Access Level', 'rcp'); ?> </th> <td> <label for="rcp_category_meta[access_level]"> <select name="rcp_category_meta[access_level]" id="rcp_category_meta[access_level]"> <?php foreach (rcp_get_access_levels() as $level) { ?> <option value="<?php echo esc_attr($level); ?> "<?php selected($level, $access_level); ?> ><?php echo $level; ?> </option> <?php } ?> </select> <span class="description"><?php _e('Access level required to view content in this category.', 'rcp'); ?> </span> </label> </td> </tr> <tr> <th scope="row"><?php _e('Subscription Levels', 'rcp'); ?> </th> <td> <?php foreach (rcp_get_subscription_levels() as $level) { ?> <label for="rcp_category_meta[subscriptions][<?php echo $level->id; ?> ]"> <input type="checkbox" name="rcp_category_meta[subscriptions][<?php echo $level->id; ?> ]" id="rcp_category_meta[subscriptions][<?php echo $level->id; ?> ]" value="1"<?php checked(true, in_array($level->id, $subscription_levels)); ?> > <?php echo $level->name; ?> </label><br/> <?php } ?> <span class="description"><?php _e('Subscription levels allowed to view content in this category. Leave unchecked for all.', 'rcp'); ?> </span> <?php wp_nonce_field('rcp_edit_category', 'rcp_edit_category'); ?> </td> </tr> <?php }
/** * Check the provided taxonomy along with the given post id to see if any restrictions are found * * @since 2.5 * @param $post_id * @param $taxonomy * @param null $user_id * * @return int|bool true if tax is restricted, false if user can access, -1 if unrestricted or invalid */ function rcp_is_post_taxonomy_restricted($post_id, $taxonomy, $user_id = null) { $restricted = -1; if (current_user_can('edit_post', $post_id)) { return $restricted; } // make sure this post supports the supplied taxonomy $post_taxonomies = get_post_taxonomies($post_id); if (!in_array($taxonomy, (array) $post_taxonomies)) { return $restricted; } $terms = get_the_terms($post_id, $taxonomy); if (empty($terms) || is_wp_error($terms)) { return $restricted; } if (!$user_id) { $user_id = get_current_user_id(); } // Loop through the categories and determine if one has restriction options foreach ($terms as $term) { $term_meta = rcp_get_term_restrictions($term->term_id); if (empty($term_meta['paid_only']) && empty($term_meta['subscriptions']) && (empty($term_meta['access_level']) || 'None' == $term_meta['access_level'])) { continue; } $restricted = false; /** Check that the user has a paid subscription ****************************************************************/ $paid_only = !empty($term_meta['paid_only']); if ($paid_only && !rcp_is_paid_user($user_id)) { $restricted = true; } /** If restricted to one or more subscription levels, make sure that the user is a member of one of the levels */ $subscriptions = !empty($term_meta['subscriptions']) ? array_map('absint', $term_meta['subscriptions']) : false; if ($subscriptions && !in_array(rcp_get_subscription_id($user_id), $subscriptions)) { $restricted = true; } /** If restricted to one or more access levels, make sure that the user is a member of one of the levls ********/ $access_level = !empty($term_meta['access_level']) ? absint($term_meta['access_level']) : 0; if ($access_level > 0 && !rcp_user_has_access($user_id, $access_level)) { $restricted = true; } $match_all = apply_filters('rcp_restricted_taxonomy_term_match_all', false, $post_id, $taxonomy, $user_id); // if we are matching all terms then it only takes one restricted term to restrict the taxonomy if ($restricted && $match_all) { break; } // if we are matching any term, then we only need the user to have access to one if (!$match_all && !$restricted) { break; } } return apply_filters('rcp_is_post_taxonomy_restricted', $restricted, $taxonomy, $post_id, $user_id); }