Permalink
Browse files

Importing the Members plugin files and folders.

git-svn-id: http://svn.locallylost.com/plugins/members/trunk@497 dba0f204-706d-4bc1-bc29-8b92e0485636
  • Loading branch information...
0 parents commit 2cf621462ce3c23d219fb95eef59c5bf6d522fc9 greenshady committed Nov 25, 2010
@@ -0,0 +1,250 @@
+<?php
+/**
+ * Members Components API. This file contains everything you need to build custom components.
+ * Rather than limiting the plugin to self-contained settings, the Components API was created so
+ * that it could be extended by other plugins and themes. Registering a component is as simple as
+ * using the registration function and creating a custom callback function for the component.
+ *
+ * To register a component, use the register_component() function. When registering a custom
+ * component, your registration function should be hooked to 'members_register_components'.
+ *
+ * @package Members
+ * @subpackage Components
+ */
+
+/* Register the default components shippedd with the plugin. */
+add_action( 'init', 'members_create_default_components', 0 );
+
+/* Load the callback functions for each of the registered components. Components should be registered on 'init' with a priority less than '10'. */
+add_action( 'init', 'members_load_components', 5 );
+
+/**
+ * Registers the initial components packaged with the plugin.
+ * @uses register_members_component() Registers a component.
+ *
+ * @since 0.1
+ */
+function members_create_default_components() {
+ register_members_component( array( 'name' => 'role_manager', 'label' => __( 'Role Manager', 'members' ), 'callback' => 'members_component_role_manager', 'requires' => false, 'description' => __('The <em>Role Manager</em> component allows you to manage roles on your site by giving you the ability to create, edit, and delete any role.', 'members' ) ) );
+ //register_members_component( array( 'name' => 'edit_roles', 'label' => __('Edit Roles', 'members'), 'callback' => 'members_component_edit_roles', 'requires' => false, 'description' => __('The <em>Edit Roles</em> component allows you to manage all roles on your site. You can change which capabilities individual roles have. Once you\'ve selected this component, you should immediately give at least one role the <code>edit_roles</code> capability. This makes sure only the roles you select can edit roles.', 'members') ) );
+ //register_members_component( array( 'name' => 'new_roles', 'label' => __('New Roles', 'members'), 'callback' => 'members_component_new_roles', 'requires' => false, 'description' => __('The <em>New Roles</em> component allows you to create new roles on your site. To use this component, you must have the <code>create_roles</code> capability. This makes sure only the roles you select can create new roles.', 'members') ) );
+ register_members_component( array( 'name' => 'content_permissions', 'label' => __('Content Permissions', 'members'), 'callback' => 'members_component_content_permissions', 'requires' => false, 'description' => __('Adds an additional meta box for the post/page editor that allows you to grant permissions for who can read the content based on the the user\'s capabilities or role. Only roles with the <code>restrict_content</code> capability will be able to use this component.', 'members') ) );
+ register_members_component( array( 'name' => 'shortcodes', 'label' => __('Shortcodes', 'members'), 'callback' => 'members_component_shortcodes', 'requires' => false, 'description' => __('Provides a set of shortcodes that may be used to restrict or provide access to certain areas of your site from within the post editor (or other areas where shortcodes are allowed).', 'members') ) );
+ register_members_component( array( 'name' => 'template_tags', 'label' => __('Template Tags', 'members'), 'callback' => 'members_component_template_tags', 'requires' => false, 'description' => __('Provides additional template tags for use within your WordPress theme for restricting or providing access to certain content.', 'members') ) );
+ register_members_component( array( 'hook' => 'widgets_init', 'name' => 'widgets', 'label' => __('Widgets', 'members'), 'callback' => 'members_component_widgets', 'requires' => false, 'description' => __('Creates additional widgets for use in any widget area on your site. The current widgets are Login Form and Users.', 'members') ) );
+ register_members_component( array( 'name' => 'private_blog', 'label' => __('Private Blog', 'members'), 'callback' => 'members_component_private_blog', 'requires' => false, 'description' => __('Forces all users to log into the site before viewing it. It will always redirect users to the login page. Note that this component does not block public access to your feeds.', 'members') ) );
+ register_members_component( array( 'name' => 'stats', 'label' => __( 'Statistics', 'members' ), 'callback' => 'members_component_stats', 'requires' => false, 'description' => __( 'Adds statistics for user signups based on role.', 'members' ) ) );
+ //register_members_component( array( 'name' => 'user_fields', 'label' => __('User Fields', 'members'), 'callback' => 'members_component_user_fields', 'requires' => false, 'description' => __('Provides an interface for building additional user profile fields. Users will then be able provide this additional information when editing their profile', 'members') ) );
+
+ do_action( 'members_register_components' ); // Available hook to register components.
+}
+
+/**
+ * Function for registering an additional component for the plugin, which the user
+ * may choose to use. Note that you should add in all the arguments to properly register
+ * your component. The exception is $requires.
+ *
+ * Your callback function should make use of the is_active_members_component() function
+ * as soon as it fires. This allows you to check if the user has activated the component before
+ * loading all of your code. Please use this system as it keeps the plugin as light as possible.
+ *
+ * @since 0.1
+ * @global $members object The global members object.
+ *
+ * @param $args array Arguments for registering a custom component.
+ * @param $args[$name] string Name of the component (only letters, numbers, and spaces).
+ * @param $args[$label] string Display name of the component.
+ * @param $args[$description] string Description of the component.
+ * @param $args[$callback] string Function that will be called when the components system is loaded.
+ * @param $args[$requires] array|bool Names of other components that are required to run this component.
+ */
+function register_members_component( $args = array() ) {
+ global $members;
+
+ $name = $args['name'];
+
+ $members->registered_components[$name] = (object) $args;
+}
+
+/**
+ * Loops through the registered components and load the necessary callback function.
+ * If a $component->hook is added and a $component->callback function, the plugin
+ * will add the function to the action hook automatically. Else if the $component->callback
+ * function exists but not $component->hook, the function will simply be called.
+ *
+ * @since 0.2
+ * @global $members object The global members object.
+ */
+function members_load_components() {
+ global $members;
+
+ /* Check if there are any registered components. If not, return false. */
+ if ( !is_array( $members->registered_components ) )
+ return false;
+
+ /* Loop through each of the registered components and execute the desired action. */
+ foreach ( $members->registered_components as $component ) {
+
+ /* If a callback function has been input, continue the process. */
+ if ( $component->callback && function_exists( $component->callback ) ) {
+
+ /* If a hook has been input, add the component callback function to the action hook. */
+ if ( isset($component->hook) )
+ add_action( $component->hook, $component->callback );
+
+ /* Call the function directly if there is no hook. */
+ else
+ call_user_func( $component->callback, $component );
+ }
+ }
+}
+
+/**
+ * Function for getting a specific component object from the list of registered
+ * plugin components.
+ *
+ * @since 0.1
+ * @global $members object The global members object.
+ * @param $component string Required. The name of the component.
+ * @return $members->registered_components[$component] object
+ */
+function get_members_component( $component ) {
+ global $members;
+ return $members->registered_components[$component_name];
+}
+
+/**
+ * Checks if a component has been activated by the user. If it has, return true. If not,
+ * return false.
+ *
+ * @since 0.1
+ * @global $members object The global members object.
+ * @param $component string Name of the component to check for.
+ * @return true|false bool
+ */
+function is_active_members_component( $component = '' ) {
+ global $members;
+
+ return (bool) isset( $members->active_components[$component] );
+}
+
+/* Default Components */
+
+/**
+ * Mange roles component. This allows you to create, edit, and delete roles
+ * and each role's capabilities.
+ *
+ * @since 0.2
+ */
+function members_component_role_manager() {
+ if ( is_admin() && is_active_members_component( 'role_manager' ) )
+ require_once( MEMBERS_COMPONENTS . '/role-manager/default.php' );
+}
+
+/**
+ * Manage roles component. This allows you to manage each role's set
+ * of capabilities.
+ *
+ * @since 0.1
+ * @deprecated 0.2
+ */
+function members_component_edit_roles() {
+ if ( is_admin() && is_active_members_component( 'edit_roles' ) )
+ require_once( MEMBERS_COMPONENTS . '/edit-roles/default.php' );
+}
+
+/**
+ * New roles component. This allows you to create new roles.
+ *
+ * @since 0.1
+ * @deprecated 0.2
+ */
+function members_component_new_roles() {
+ if ( is_admin() && is_active_members_component( 'new_roles' ) )
+ require_once( MEMBERS_COMPONENTS . '/new-roles/default.php' );
+}
+
+/**
+ * Loads the content permissions component.
+ *
+ * @since 0.1
+ */
+function members_component_content_permissions() {
+ if ( is_active_members_component( 'content_permissions' ) ) {
+ require_once( MEMBERS_COMPONENTS . '/content-permissions/content-permissions.php' );
+ require_once( MEMBERS_COMPONENTS . '/content-permissions/meta-box.php' );
+ }
+}
+
+/**
+ * Loads the shortcodes component.
+ *
+ * @since 0.1
+ */
+function members_component_shortcodes() {
+ if ( !is_admin() && is_active_members_component( 'shortcodes' ) )
+ require_once( MEMBERS_COMPONENTS . '/shortcodes/shortcodes.php' );
+}
+
+/**
+ * Loads the template tags component.
+ *
+ * @since 0.1
+ */
+function members_component_template_tags() {
+ if ( is_active_members_component( 'template_tags' ) )
+ require_once( MEMBERS_COMPONENTS . '/template-tags/template-tags.php' );
+}
+
+/**
+ * Loads the widgets component.
+ *
+ * @since 0.1
+ */
+function members_component_widgets() {
+ if ( is_active_members_component( 'widgets' ) ) {
+
+ /* Load each of the widget files. */
+ require_once( MEMBERS_COMPONENTS . '/widgets/login.php' );
+ require_once( MEMBERS_COMPONENTS . '/widgets/users.php' );
+
+ /* Register each widget. */
+ register_widget( 'Members_Widget_Login' );
+ register_widget( 'Members_Widget_Users' );
+ }
+}
+
+/**
+ * Loads the private blog component.
+ *
+ * @since 0.1
+ */
+function members_component_private_blog() {
+ if ( is_active_members_component( 'private_blog' ) )
+ require_once( MEMBERS_COMPONENTS . '/private-blog/default.php' );
+}
+
+/**
+ * Loads the stats component.
+ *
+ * @since 0.2
+ */
+function members_component_stats() {
+ if ( is_admin() && is_active_members_component( 'stats' ) )
+ require_once( MEMBERS_COMPONENTS . '/statistics/default.php' );
+}
+
+/**
+ * Loads the user fields component.
+ *
+ * @internal Do not use this function. The component isn't ready.
+ * @todo Pretty much everything.
+ *
+ * @since 0.1
+ */
+function members_component_user_fields() {
+ if ( is_admin() && is_active_members_component( 'user_fields' ) )
+ require_once( MEMBERS_COMPONENTS . '/user-fields/default.php' );
+}
+
+?>
@@ -0,0 +1 @@
+<?php /* Life is sweet. */ ?>
@@ -0,0 +1,112 @@
+<?php
+/**
+ * The Content Permissions component was created so that access to specific parts of a site
+ * can be granted or denied. This is the component that gives truly fine-grained control over
+ * who can see what content on the front end of the site.
+ *
+ * Current features of the Content Permissions component:
+ * - Block content on a post-by-post (or page) basis according to user role.
+ *
+ * This feature set should eventually include the ability to block access to taxonomies and
+ * attachments to be truly useful.
+ *
+ * @todo Check and test feeds and filter if necessary.
+ * @todo Make sure comments aren't shown anywhere.
+ * @todo Remove pages from wp_list_pages() and wp_page_menu().
+ * @todo Cover ALL the bases. If something's restricted, make sure it stays that way.
+ *
+ * @package Members
+ * @subpackage Components
+ */
+
+/**
+ * Adds the content permissions meta box to the post/page edit screen
+ * if the current user has the 'restrict_content' capability.
+ */
+add_action( 'admin_menu', 'members_content_permissions_create_meta_box' );
+
+/* Saves the content permissions metabox data to a custom field. */
+add_action( 'save_post', 'members_content_permissions_save_meta', 1, 2 );
+
+/* Add messages to the components form. */
+add_action( 'members_pre_components_form', 'members_message_no_restrict_content' );
+
+/* Filter the content and exerpts. */
+add_filter( 'the_content', 'members_content_permissions_protect' );
+add_filter( 'get_the_excerpt', 'members_content_permissions_protect' );
+add_filter( 'the_excerpt', 'members_content_permissions_protect' );
+
+/* Filter the comments template to make sure comments aren't shown to users without access. */
+add_filter( 'comments_template', 'members_content_permissions_comments' );
+
+/**
+ * Disables the comments template if the current post has been restricted, unless
+ * the user has the role needed to view the content of the post.
+ *
+ * @todo Allow users to override the "no comments" template if in their theme.
+ *
+ * @since 0.1
+ * @param $template string File URL of the template to display.
+ * @return $template string File URL of the template to display.
+ */
+function members_content_permissions_comments( $template ) {
+ global $wp_query;
+
+ $roles = get_post_meta( $wp_query->post->ID, '_role', false );
+
+ if ( !empty( $roles ) && is_array( $roles ) ) {
+ foreach( $roles as $role ) {
+ if ( !is_feed() && ( current_user_can( $role ) || current_user_can( 'restrict_content' ) ) )
+ return $template;
+ }
+ $template = MEMBERS_COMPONENTS . '/content-permissions/comments.php';
+ }
+ return $template;
+}
+
+/**
+ * Displays a message if the Content Permissions component is active but no role
+ * has been given the capability of 'restrict_content', which is a required capability to
+ * use the component.
+ *
+ * @since 0.1
+ * @uses is_active_members_component() Checks if the content_permissions component is active.
+ * @uses members_check_form_cap() Checks if the restrict_content capability has been given to a role.
+ */
+function members_message_no_restrict_content() {
+ if ( is_active_members_component( 'content_permissions' ) && !members_check_for_cap( 'restrict_content' ) ) {
+ $message = __('No role currently has the <code>restrict_content</code> capability. To use the <em>Content Permissions</em> component, at least one role must have this capability.', 'members');
+ members_admin_message( '', $message );
+ }
+}
+
+/**
+ * Disables content passed through the $content variable given the current user's role. The
+ * function checks for a custom field key of "Role" and loops through its values, checking
+ * if the current user has that particular role.
+ *
+ * Users with the rescrict_content capability should also be able to see the content.
+ *
+ * @since 0.1
+ * @uses get_post_meta() Gets the meta values of the "_role" custom field key.
+ * @uses current_user_can() Checks if the current user has a particular role (capability).
+ * @param $content string The current post's content/excerpt.
+ * @return $content string Either the current post's content/excerpt or a content inaccessible message.
+ */
+function members_content_permissions_protect( $content ) {
+ global $post;
+
+ $roles = get_post_meta( $post->ID, '_role', false );
+
+ if ( !empty( $roles ) && is_array( $roles ) ) {
+ foreach( $roles as $role ) {
+ if ( !is_feed() && ( current_user_can( $role ) || current_user_can( 'restrict_content' ) ) )
+ return $content;
+ }
+ $content = '<p class="restricted alert warning">' . __('Sorry, but you do not have permission to view this content.', 'members') . '</p>';
+ }
+
+ return $content;
+}
+
+?>
Oops, something went wrong.

0 comments on commit 2cf6214

Please sign in to comment.