is->role_names[$role] = $display_name; return $this->role_objects[$role]; } /** * Remove role by name. * * @since 2.0.0 * @access public * * @param string $role Role name. */ function remove_role( $role ) { if ( ! isset( $this->role_objects[$role] ) ) return; unset( $this->role_objects[$role] ); unset( $this->role_names[$role] ); unset( $this->roles[$role] ); if ( $this->use_db ) update_option( $this->role_key, $this->roles ); if ( get_option( 'default_role' ) == $role ) update_option( 'default_role', 'subscriber' ); } /** * Add capability to role. * * @since 2.0.0 * @access public * * @param string $role Role name. * @param string $cap Capability name. * @param bool $grant Optional, default is true. Whether role is capable of performing capability. */ function add_cap( $role, $cap, $grant = true ) { if ( ! isset( $this->roles[$role] ) ) return; $this->roles[$role]['capabilities'][$cap] = $grant; if ( $this->use_db ) update_option( $this->role_key, $this->roles ); } /** * Remove capability from role. * * @since 2.0.0 * @access public * * @param string $role Role name. * @param string $cap Capability name. */ function remove_cap( $role, $cap ) { if ( ! isset( $this->roles[$role] ) ) return; unset( $this->roles[$role]['capabilities'][$cap] ); if ( $this->use_db ) update_option( $this->role_key, $this->roles ); } /** * Retrieve role object by name. * * @since 2.0.0 * @access public * * @param string $role Role name. * @return WP_Role|null WP_Role object if found, null if the role does not exist. */ function get_role( $role ) { if ( isset( $this->role_objects[$role] ) ) return $this->role_objects[$role]; else return null; } /** * Retrieve list of role names. * * @since 2.0.0 * @access public * * @return array List of role names. */ function get_names() { return $this->role_names; } /** * Whether role name is currently in the list of available roles. * * @since 2.0.0 * @access public * * @param string $role Role name to look up. * @return bool */ function is_role( $role ) { return isset( $this->role_names[$role] ); } } /** * WordPress Role class. * * @since 2.0.0 * @package WordPress * @subpackage User */ class WP_Role { /** * Role name. * * @since 2.0.0 * @access public * @var string */ var $name; /** * List of capabilities the role contains. * * @since 2.0.0 * @access public * @var array */ var $capabilities; /** * Constructor - Set up object properties. * * The list of capabilities, must have the key as the name of the capability * and the value a boolean of whether it is granted to the role. * * @since 2.0.0 * @access public * * @param string $role Role name. * @param array $capabilities List of capabilities. */ function __construct( $role, $capabilities ) { $this->name = $role; $this->capabilities = $capabilities; } /** * Assign role a capability. * * @see WP_Roles::add_cap() Method uses implementation for role. * @since 2.0.0 * @access public * * @param string $cap Capability name. * @param bool $grant Whether role has capability privilege. */ function add_cap( $cap, $grant = true ) { global $wp_roles; if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles(); $this->capabilities[$cap] = $grant; $wp_roles->add_cap( $this->name, $cap, $grant ); } /** * Remove capability from role. * * This is a container for {@link WP_Roles::remove_cap()} to remove the * capability from the role. That is to say, that {@link * WP_Roles::remove_cap()} implements the functionality, but it also makes * sense to use this class, because you don't need to enter the role name. * * @since 2.0.0 * @access public * * @param string $cap Capability name. */ function remove_cap( $cap ) { global $wp_roles; if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles(); unset( $this->capabilities[$cap] ); $wp_roles->remove_cap( $this->name, $cap ); } /** * Whether role has capability. * * The capabilities is passed through the 'role_has_cap' filter. The first * parameter for the hook is the list of capabilities the class has * assigned. The second parameter is the capability name to look for. The * third and final parameter for the hook is the role name. * * @since 2.0.0 * @access public * * @param string $cap Capability name. * @return bool True, if user has capability. False, if doesn't have capability. */ function has_cap( $cap ) { /** * Filter which capabilities a role has. * * @since 2.0.0 * * @param array $capabilities Array of role capabilities. * @param string $cap Capability name. * @param string $name Role name. */ $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name ); if ( !empty( $capabilities[$cap] ) ) return $capabilities[$cap]; else return false; } } /** * WordPress User class. * * @since 2.0.0 * @package WordPress * @subpackage User */ class WP_User { /** * User data container. * * @since 2.0.0 * @access private * @var array */ var $data; /** * The user's ID. * * @since 2.1.0 * @access public * @var int */ var $ID = 0; /** * The individual capabilities the user has been given. * * @since 2.0.0 * @access public * @var array */ var $caps = array(); /** * User metadata option name. * * @since 2.0.0 * @access public * @var string */ var $cap_key; /** * The roles the user is part of. * * @since 2.0.0 * @access public * @var array */ var $roles = array(); /** * All capabilities the user has, including individual and role based. * * @since 2.0.0 * @access public * @var array */ var $allcaps = array(); /** * The filter context applied to user data fields. * * @since 2.9.0 * @access private * @var string */ var $filter = null; private static $back_compat_keys; /** * Constructor * * Retrieves the userdata and passes it to {@link WP_User::init()}. * * @since 2.0.0 * @access public * * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB. * @param string $name Optional. User's username * @param int $blog_id Optional Blog ID, defaults to current blog. * @return WP_User */ function __construct( $id = 0, $name = '', $blog_id = '' ) { if ( ! isset( self::$back_compat_keys ) ) { $prefix = $GLOBALS['wpdb']->prefix; self::$back_compat_keys = array( 'user_firstname' => 'first_name', 'user_lastname' => 'last_name', 'user_description' => 'description', 'user_level' => $prefix . 'user_level', $prefix . 'usersettings' => $prefix . 'user-settings', $prefix . 'usersettingstime' => $prefix . 'user-settings-time', ); } if ( is_a( $id, 'WP_User' ) ) { $this->init( $id->data, $blog_id ); return; } elseif ( is_object( $id ) ) { $this->init( $id, $blog_id ); return; } if ( ! empty( $id ) && ! is_numeric( $id ) ) { $name = $id; $id = 0; } if ( $id ) $data = self::get_data_by( 'id', $id ); else $data = self::get_data_by( 'login', $name ); if ( $data ) $this->init( $data, $blog_id ); } /** * Sets up object properties, including capabilities. * * @param object $data User DB row object * @param int $blog_id Optional. The blog id to initialize for */ function init( $data, $blog_id = '' ) { $this->data = $data; $this->ID = (int) $data->ID; $this->for_blog( $blog_id ); } /** * Return only the main user fields * * @since 3.3.0 * * @param string $field The field to query against: 'id', 'slug', 'email' or 'login' * @param string|int $value The field value * @return object Raw user object */ static function get_data_by( $field, $value ) { global $wpdb; if ( 'id' == $field ) { // Make sure the value is numeric to avoid casting objects, for example, // to int 1. if ( ! is_numeric( $value ) ) return false; $value = intval( $value ); if ( $value < 1 ) return false; } else { $value = trim( $value ); } if ( !$value ) return false; switch ( $field ) { case 'id': $user_id = $value; $db_field = 'ID'; break; case 'slug': $user_id = wp_cache_get($value, 'userslugs'); $db_field = 'user_nicename'; break; case 'email': $user_id = wp_cache_get($value, 'useremail'); $db_field = 'user_email'; break; case 'login': $value = sanitize_user( $value ); $user_id = wp_cache_get($value, 'userlogins'); $db_field = 'user_login'; break; default: return false; } if ( false !== $user_id ) { if ( $user = wp_cache_get( $user_id, 'users' ) ) return $user; } if ( !$user = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value ) ) ) return false; update_user_caches( $user ); return $user; } /** * Magic method for checking the existence of a certain custom field * * @since 3.3.0 */ function __isset( $key ) { if ( 'id' == $key ) { _deprecated_argument( 'WP_User->id', '2.1', __( 'Use WP_User->ID instead.' ) ); $key = 'ID'; } if ( isset( $this->data->$key ) ) return true; if ( isset( self::$back_compat_keys[ $key ] ) ) $key = self::$back_compat_keys[ $key ]; return metadata_exists( 'user', $this->ID, $key ); } /** * Magic method for accessing custom fields * * @since 3.3.0 */ function __get( $key ) { if ( 'id' == $key ) { _deprecated_argument( 'WP_User->id', '2.1', __( 'Use WP_User->ID instead.' ) ); return $this->ID; } if ( isset( $this->data->$key ) ) { $value = $this->data->$key; } else { if ( isset( self::$back_compat_keys[ $key ] ) ) $key = self::$back_compat_keys[ $key ]; $value = get_user_meta( $this->ID, $key, true ); } if ( $this->filter ) { $value = sanitize_user_field( $key, $value, $this->ID, $this->filter ); } return $value; } /** * Magic method for setting custom fields * * @since 3.3.0 */ function __set( $key, $value ) { if ( 'id' == $key ) { _deprecated_argument( 'WP_User->id', '2.1', __( 'Use WP_User->ID instead.' ) ); $this->ID = $value; return; } $this->data->$key = $value; } /** * Determine whether the user exists in the database. * * @since 3.4.0 * @access public * * @return bool True if user exists in the database, false if not. */ function exists() { return ! empty( $this->ID ); } /** * Retrieve the value of a property or meta key. * * Retrieves from the users and usermeta table. * * @since 3.3.0 * * @param string $key Property */ function get( $key ) { return $this->__get( $key ); } /** * Determine whether a property or meta key is set * * Consults the users and usermeta tables. * * @since 3.3.0 * * @param string $key Property */ function has_prop( $key ) { return $this->__isset( $key ); } /** * Return an array representation. * * @since 3.5.0 * * @return array Array representation. */ function to_array() { return get_object_vars( $this->data ); } /** * Set up capability object properties. * * Will set the value for the 'cap_key' property to current database table * prefix, followed by 'capabilities'. Will then check to see if the * property matching the 'cap_key' exists and is an array. If so, it will be * used. * * @access protected * @since 2.1.0 * * @param string $cap_key Optional capability key */ function _init_caps( $cap_key = '' ) { global $wpdb; if ( empty($cap_key) ) $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities'; else $this->cap_key = $cap_key; $this->caps = get_user_meta( $this->ID, $this->cap_key, true ); if ( ! is_array( $this->caps ) ) $this->caps = array(); $this->get_role_caps(); } /** * Retrieve all of the role capabilities and merge with individual capabilities. * * All of the capabilities of the roles the user belongs to are merged with * the users individual roles. This also means that the user can be denied * specific roles that their role might have, but the specific user isn't * granted permission to. * * @since 2.0.0 * @uses $wp_roles * @access public * * @return array List of all capabilities for the user. */ function get_role_caps() { global $wp_roles; if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles(); //Filter out caps that are not role names and assign to $this->roles if ( is_array( $this->caps ) ) $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) ); //Build $allcaps from role caps, overlay user's $caps $this->allcaps = array(); foreach ( (array) $this->roles as $role ) { $the_role = $wp_roles->get_role( $role ); $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities ); } $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); return $this->allcaps; } /** * Add role to user. * * Updates the user's meta data option with capabilities and roles. * * @since 2.0.0 * @access public * * @param string $role Role name. */ function add_role( $role ) { $this->caps[$role] = true; update_user_meta( $this->ID, $this->cap_key, $this->caps ); $this->get_role_caps(); $this->update_user_level_from_caps(); } /** * Remove role from user. * * @since 2.0.0 * @access public * * @param string $role Role name. */ function remove_role( $role ) { if ( !in_array($role, $this->roles) ) return; unset( $this->caps[$role] ); update_user_meta( $this->ID, $this->cap_key, $this->caps ); $this->get_role_caps(); $this->update_user_level_from_caps(); } /** * Set the role of the user. * * This will remove the previous roles of the user and assign the user the * new one. You can set the role to an empty string and it will remove all * of the roles from the user. * * @since 2.0.0 * @access public * * @param string $role Role name. */ function set_role( $role ) { if ( 1 == count( $this->roles ) && $role == current( $this->roles ) ) return; foreach ( (array) $this->roles as $oldrole ) unset( $this->caps[$oldrole] ); $old_roles = $this->roles; if ( !empty( $role ) ) { $this->caps[$role] = true; $this->roles = array( $role => true ); } else { $this->roles = false; } update_user_meta( $this->ID, $this->cap_key, $this->caps ); $this->get_role_caps(); $this->update_user_level_from_caps(); /** * Fires after the user's role has changed. * * @since 2.9.0 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles. * * @param int $user_id The user ID. * @param string $role The new role. * @param array $old_roles An array of the user's previous roles. */ do_action( 'set_user_role', $this->ID, $role, $old_roles ); } /** * Choose the maximum level the user has. * * Will compare the level from the $item parameter against the $max * parameter. If the item is incorrect, then just the $max parameter value * will be returned. * * Used to get the max level based on the capabilities the user has. This * is also based on roles, so if the user is assigned the Administrator role * then the capability 'level_10' will exist and the user will get that * value. * * @since 2.0.0 * @access public * * @param int $max Max level of user. * @param string $item Level capability name. * @return int Max Level. */ function level_reduction( $max, $item ) { if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) { $level = intval( $matches[1] ); return max( $max, $level ); } else { return $max; } } /** * Update the maximum user level for the user. * * Updates the 'user_level' user metadata (includes prefix that is the * database table prefix) with the maximum user level. Gets the value from * the all of the capabilities that the user has. * * @since 2.0.0 * @access public */ function update_user_level_from_caps() { global $wpdb; $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 ); update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level ); } /** * Add capability and grant or deny access to capability. * * @since 2.0.0 * @access public * * @param string $cap Capability name. * @param bool $grant Whether to grant capability to user. */ function add_cap( $cap, $grant = true ) { $this->caps[$cap] = $grant; update_user_meta( $this->ID, $this->cap_key, $this->caps ); } /** * Remove capability from user. * * @since 2.0.0 * @access public * * @param string $cap Capability name. */ function remove_cap( $cap ) { if ( ! isset( $this->caps[$cap] ) ) return; unset( $this->caps[$cap] ); update_user_meta( $this->ID, $this->cap_key, $this->caps ); } /** * Remove all of the capabilities of the user. * * @since 2.1.0 * @access public */ function remove_all_caps() { global $wpdb; $this->caps = array(); delete_user_meta( $this->ID, $this->cap_key ); delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' ); $this->get_role_caps(); } /** * Whether user has capability or role name. * * This is useful for looking up whether the user has a specific role * assigned to the user. The second optional parameter can also be used to * check for capabilities against a specific object, such as a post or user. * * @since 2.0.0 * @access public * * @param string|int $cap Capability or role name to search. * @return bool True, if user has capability; false, if user does not have capability. */ function has_cap( $cap ) { if ( is_numeric( $cap ) ) { _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') ); $cap = $this->translate_level_to_cap( $cap ); } $args = array_slice( func_get_args(), 1 ); $args = array_merge( array( $cap, $this->ID ), $args ); $caps = call_user_func_array( 'map_meta_cap', $args ); // Multisite super admin has all caps by definition, Unless specifically denied. if ( is_multisite() && is_super_admin( $this->ID ) ) { if ( in_array('do_not_allow', $caps) ) return false; return true; } /** * Dynamically filter a user's capabilities. * * @since 2.0.0 * @since 3.7.0 Added the user object. * * @param array $allcaps An array of all the role's capabilities. * @param array $caps Actual capabilities for meta capability. * @param array $args Optional parameters passed to has_cap(), typically object ID. * @param WP_User $user The user object. */ // Must have ALL requested caps $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this ); $capabilities['exist'] = true; // Everyone is allowed to exist foreach ( (array) $caps as $cap ) { if ( empty( $capabilities[ $cap ] ) ) return false; } return true; } /** * Convert numeric level to level capability name. * * Prepends 'level_' to level number. * * @since 2.0.0 * @access public * * @param int $level Level number, 1 to 10. * @return string */ function translate_level_to_cap( $level ) { return 'level_' . $level; } /** * Set the blog to operate on. Defaults to the current blog. * * @since 3.0.0 * * @param int $blog_id Optional Blog ID, defaults to current blog. */ function for_blog( $blog_id = '' ) { global $wpdb; if ( ! empty( $blog_id ) ) $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; else $cap_key = ''; $this->_init_caps( $cap_key ); } } /** * Map meta capabilities to primitive capabilities. * * This does not actually compare whether the user ID has the actual capability, * just what the capability or capabilities are. Meta capability list value can * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post', * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'. * * @since 2.0.0 * * @param string $cap Capability name. * @param int $user_id User ID. * @return array Actual capabilities for meta capability. */ function map_meta_cap( $cap, $user_id ) { $args = array_slice( func_get_args(), 2 ); $caps = array(); switch ( $cap ) { case 'remove_user': $caps[] = 'remove_users'; break; case 'promote_user': $caps[] = 'promote_users'; break; case 'edit_user': case 'edit_users': // Allow user to edit itself if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] ) break; // If multisite these caps are allowed only for super admins. if ( is_multisite() && !is_super_admin( $user_id ) ) $caps[] = 'do_not_allow'; else $caps[] = 'edit_users'; // edit_user maps to edit_users. break; case 'delete_post': case 'delete_page': $post = get_post( $args[0] ); if ( 'revision' == $post->post_type ) { $post = get_post( $post->post_parent ); } $post_type = get_post_type_object( $post->post_type ); if ( ! $post_type->map_meta_cap ) { $caps[] = $post_type->cap->$cap; // Prior to 3.1 we would re-call map_meta_cap here. if ( 'delete_post' == $cap ) $cap = $post_type->cap->$cap; break; } $post_author_id = $post->post_author; // If no author set yet, default to current user for cap checks. if ( ! $post_author_id ) $post_author_id = $user_id; // If the user is the author... if ( $user_id == $post_author_id ) { // If the post is published... if ( 'publish' == $post->post_status ) { $caps[] = $post_type->cap->delete_published_posts; } elseif ( 'trash' == $post->post_status ) { if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) ) $caps[] = $post_type->cap->delete_published_posts; } else { // If the post is draft... $caps[] = $post_type->cap->delete_posts; } } else { // The user is trying to edit someone else's post. $caps[] = $post_type->cap->delete_others_posts; // The post is published, extra cap required. if ( 'publish' == $post->post_status ) $caps[] = $post_type->cap->delete_published_posts; elseif ( 'private' == $post->post_status ) $caps[] = $post_type->cap->delete_private_posts; } break; // edit_post breaks down to edit_posts, edit_published_posts, or // edit_others_posts case 'edit_post': case 'edit_page': $post = get_post( $args[0] ); if ( empty( $post ) ) break; if ( 'revision' == $post->post_type ) { $post = get_post( $post->post_parent ); } $post_type = get_post_type_object( $post->post_type ); if ( ! $post_type->map_meta_cap ) { $caps[] = $post_type->cap->$cap; // Prior to 3.1 we would re-call map_meta_cap here. if ( 'edit_post' == $cap ) $cap = $post_type->cap->$cap; break; } $post_author_id = $post->post_author; // If no author set yet, default to current user for cap checks. if ( ! $post_author_id ) $post_author_id = $user_id; // If the user is the author... if ( $user_id == $post_author_id ) { // If the post is published... if ( 'publish' == $post->post_status ) { $caps[] = $post_type->cap->edit_published_posts; } elseif ( 'trash' == $post->post_status ) { if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) ) $caps[] = $post_type->cap->edit_published_posts; } else { // If the post is draft... $caps[] = $post_type->cap->edit_posts; } } else { // The user is trying to edit someone else's post. $caps[] = $post_type->cap->edit_others_posts; // The post is published, extra cap required. if ( 'publish' == $post->post_status ) $caps[] = $post_type->cap->edit_published_posts; elseif ( 'private' == $post->post_status ) $caps[] = $post_type->cap->edit_private_posts; } break; case 'read_post': case 'read_page': $post = get_post( $args[0] ); if ( 'revision' == $post->post_type ) { $post = get_post( $post->post_parent ); } $post_type = get_post_type_object( $post->post_type ); if ( ! $post_type->map_meta_cap ) { $caps[] = $post_type->cap->$cap; // Prior to 3.1 we would re-call map_meta_cap here. if ( 'read_post' == $cap ) $cap = $post_type->cap->$cap; break; } $status_obj = get_post_status_object( $post->post_status ); if ( $status_obj->public ) { $caps[] = $post_type->cap->read; break; } $post_author_id = $post->post_author; // If no author set yet, default to current user for cap checks. if ( ! $post_author_id ) $post_author_id = $user_id; if ( $user_id == $post_author_id ) $caps[] = $post_type->cap->read; elseif ( $status_obj->private ) $caps[] = $post_type->cap->read_private_posts; else $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); break; case 'publish_post': $post = get_post( $args[0] ); $post_type = get_post_type_object( $post->post_type ); $caps[] = $post_type->cap->publish_posts; break; case 'edit_post_meta': case 'delete_post_meta': case 'add_post_meta': $post = get_post( $args[0] ); $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false; if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) { /** * Filter whether the user is allowed to add post meta to a post. * * The dynamic portion of the hook name, $meta_key, refers to the * meta key passed to map_meta_cap(). * * @since 3.3.0 * * @param bool $allowed Whether the user can add the post meta. Default false. * @param string $meta_key The meta key. * @param int $post_id Post ID. * @param int $user_id User ID. * @param string $cap Capability name. * @param array $caps User capabilities. */ $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps ); if ( ! $allowed ) $caps[] = $cap; } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) { $caps[] = $cap; } break; case 'edit_comment': $comment = get_comment( $args[0] ); if ( empty( $comment ) ) break; $post = get_post( $comment->comment_post_ID ); $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); break; case 'unfiltered_upload': if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) ) $caps[] = $cap; else $caps[] = 'do_not_allow'; break; case 'unfiltered_html' : // Disallow unfiltered_html for all users, even admins and super admins. if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) $caps[] = 'do_not_allow'; elseif ( is_multisite() && ! is_super_admin( $user_id ) ) $caps[] = 'do_not_allow'; else $caps[] = $cap; break; case 'edit_files': case 'edit_plugins': case 'edit_themes': // Disallow the file editors. if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT ) $caps[] = 'do_not_allow'; elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) $caps[] = 'do_not_allow'; elseif ( is_multisite() && ! is_super_admin( $user_id ) ) $caps[] = 'do_not_allow'; else $caps[] = $cap; break; case 'update_plugins': case 'delete_plugins': case 'install_plugins': case 'update_themes': case 'delete_themes': case 'install_themes': case 'update_core': // Disallow anything that creates, deletes, or updates core, plugin, or theme files. // Files in uploads are excepted. if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) $caps[] = 'do_not_allow'; elseif ( is_multisite() && ! is_super_admin( $user_id ) ) $caps[] = 'do_not_allow'; else $caps[] = $cap; break; case 'activate_plugins': $caps[] = $cap; if ( is_multisite() ) { // update_, install_, and delete_ are handled above with is_super_admin(). $menu_perms = get_site_option( 'menu_items', array() ); if ( empty( $menu_perms['plugins'] ) ) $caps[] = 'manage_network_plugins'; } break; case 'delete_user': case 'delete_users': // If multisite only super admins can delete users. if ( is_multisite() && ! is_super_admin( $user_id ) ) $caps[] = 'do_not_allow'; else $caps[] = 'delete_users'; // delete_user maps to delete_users. break; case 'create_users': if ( !is_multisite() ) $caps[] = $cap; elseif ( is_super_admin() || get_site_option( 'add_new_users' ) ) $caps[] = $cap; else $caps[] = 'do_not_allow'; break; case 'manage_links' : if ( get_option( 'link_manager_enabled' ) ) $caps[] = $cap; else $caps[] = 'do_not_allow'; break; default: // Handle meta capabilities for custom post types. $post_type_meta_caps = _post_type_meta_capabilities(); if ( isset( $post_type_meta_caps[ $cap ] ) ) { $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args ); return call_user_func_array( 'map_meta_cap', $args ); } // If no meta caps match, return the original cap. $caps[] = $cap; } /** * Filter a user's capabilities depending on specific context and/or privilege. * * @since 2.8.0 * * @param array $caps Returns the user's actual capabilities. * @param string $cap Capability name. * @param int $user_id The user ID. * @param array $args Adds the context to the cap. Typically the object ID. */ return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args ); } /** * Whether current user has capability or role. * * @since 2.0.0 * * @param string $capability Capability or role name. * @return bool */ function current_user_can( $capability ) { $current_user = wp_get_current_user(); if ( empty( $current_user ) ) return false; $args = array_slice( func_get_args(), 1 ); $args = array_merge( array( $capability ), $args ); return call_user_func_array( array( $current_user, 'has_cap' ), $args ); } /** * Whether current user has a capability or role for a given blog. * * @since 3.0.0 * * @param int $blog_id Blog ID * @param string $capability Capability or role name. * @return bool */ function current_user_can_for_blog( $blog_id, $capability ) { if ( is_multisite() ) switch_to_blog( $blog_id ); $current_user = wp_get_current_user(); if ( empty( $current_user ) ) return false; $args = array_slice( func_get_args(), 2 ); $args = array_merge( array( $capability ), $args ); $can = call_user_func_array( array( $current_user, 'has_cap' ), $args ); if ( is_multisite() ) restore_current_blog(); return $can; } /** * Whether author of supplied post has capability or role. * * @since 2.9.0 * * @param int|object $post Post ID or post object. * @param string $capability Capability or role name. * @return bool */ function author_can( $post, $capability ) { if ( !$post = get_post($post) ) return false; $author = get_userdata( $post->post_author ); if ( ! $author ) return false; $args = array_slice( func_get_args(), 2 ); $args = array_merge( array( $capability ), $args ); return call_user_func_array( array( $author, 'has_cap' ), $args ); } /** * Whether a particular user has capability or role. * * @since 3.1.0 * * @param int|object $user User ID or object. * @param string $capability Capability or role name. * @return bool */ function user_can( $user, $capability ) { if ( ! is_object( $user ) ) $user = get_userdata( $user ); if ( ! $user || ! $user->exists() ) return false; $args = array_slice( func_get_args(), 2 ); $args = array_merge( array( $capability ), $args ); return call_user_func_array( array( $user, 'has_cap' ), $args ); } /** * Retrieve role object. * * @see WP_Roles::get_role() Uses method to retrieve role object. * @since 2.0.0 * * @param string $role Role name. * @return WP_Role|null WP_Role object if found, null if the role does not exist. */ function get_role( $role ) { global $wp_roles; if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles(); return $wp_roles->get_role( $role ); } /** * Add role, if it does not exist. * * @see WP_Roles::add_role() Uses method to add role. * @since 2.0.0 * * @param string $role Role name. * @param string $display_name Display name for role. * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false ); * @return WP_Role|null WP_Role object if role is added, null if already exists. */ function add_role( $role, $display_name, $capabilities = array() ) { global $wp_roles; if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles(); return $wp_roles->add_role( $role, $display_name, $capabilities ); } /** * Remove role, if it exists. * * @see WP_Roles::remove_role() Uses method to remove role. * @since 2.0.0 * * @param string $role Role name. */ function remove_role( $role ) { global $wp_roles; if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles(); $wp_roles->remove_role( $role ); } /** * Retrieve a list of super admins. * * @since 3.0.0 * * @uses $super_admins Super admins global variable, if set. * * @return array List of super admin logins */ function get_super_admins() { global $super_admins; if ( isset($super_admins) ) return $super_admins; else return get_site_option( 'site_admins', array('admin') ); } /** * Determine if user is a site admin. * * @since 3.0.0 * * @param int $user_id (Optional) The ID of a user. Defaults to the current user. * @return bool True if the user is a site admin. */ function is_super_admin( $user_id = false ) { if ( ! $user_id || $user_id == get_current_user_id() ) $user = wp_get_current_user(); else $user = get_userdata( $user_id ); if ( ! $user || ! $user->exists() ) return false; if ( is_multisite() ) { $super_admins = get_super_admins(); if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) ) return true; } else { if ( $user->has_cap('delete_users') ) return true; } return false; } et_post_type( $post_id ), 'category' ) ) return apply_filters( 'the_category', '', $separator, $parents ); $categories = get_the_category( $post_id ); if ( empty( $categories ) ) return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents ); $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"'; $thelist = ''; if ( '' == $separator ) { $thelist .= ''; } else { $i = 0; foreach ( $categories as $category ) { if ( 0 < $i ) $thelist .= $separator; switch ( strtolower( $parents ) ) { case 'multiple': if ( $category->parent ) $thelist .= get_category_parents( $category->parent, true, $separator ); $thelist .= 'name ) ) . '" ' . $rel . '>' . $category->name.''; break; case 'single': $thelist .= 'name ) ) . '" ' . $rel . '>'; if ( $category->parent ) $thelist .= get_category_parents( $category->parent, false, $separator ); $thelist .= "$category->name"; break; case '': default: $thelist .= 'name ) ) . '" ' . $rel . '>' . $category->name.''; } ++$i; } } return apply_filters( 'the_category', $thelist, $separator, $parents ); } /** * Check if the current post in within any of the given categories. * * The given categories are checked against the post's categories' term_ids, names and slugs. * Categories given as integers will only be checked against the post's categories' term_ids. * * Prior to v2.5 of WordPress, category names were not supported. * Prior to v2.7, category slugs were not supported. * Prior to v2.7, only one category could be compared: in_category( $single_category ). * Prior to v2.7, this function could only be used in the WordPress Loop. * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. * * @since 1.2.0 * @uses has_category() * * @param int|string|array $category Category ID, name or slug, or array of said. * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0) * @return bool True if the current post is in any of the given categories. */ function in_category( $category, $post = null ) { if ( empty( $category ) ) return false; return has_category( $category, $post ); } /** * Display the category list for the post. * * @since 0.71 * * @param string $separator Optional, default is empty string. Separator for between the categories. * @param string $parents Optional. How to display the parents. * @param int $post_id Optional. Post ID to retrieve categories. */ function the_category( $separator = '', $parents='', $post_id = false ) { echo get_the_category_list( $separator, $parents, $post_id ); } /** * Retrieve category description. * * @since 1.0.0 * * @param int $category Optional. Category ID. Will use global category ID by default. * @return string Category description, available. */ function category_description( $category = 0 ) { return term_description( $category, 'category' ); } /** * Display or retrieve the HTML dropdown list of categories. * * The list of arguments is below: * 'show_option_all' (string) - Text to display for showing all categories. * 'show_option_none' (string) - Text to display for showing no categories. * 'orderby' (string) default is 'ID' - What column to use for ordering the * categories. * 'order' (string) default is 'ASC' - What direction to order categories. * 'show_count' (bool|int) default is 0 - Whether to show how many posts are * in the category. * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that * don't have any posts attached to them. * 'child_of' (int) default is 0 - See {@link get_categories()}. * 'exclude' (string) - See {@link get_categories()}. * 'echo' (bool|int) default is 1 - Whether to display or retrieve content. * 'depth' (int) - The max depth. * 'tab_index' (int) - Tab index for select element. * 'name' (string) - The name attribute value for select element. * 'id' (string) - The ID attribute value for select element. Defaults to name if omitted. * 'class' (string) - The class attribute value for select element. * 'selected' (int) - Which category ID is selected. * 'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category. * * The 'hierarchical' argument, which is disabled by default, will override the * depth argument, unless it is true. When the argument is false, it will * display all of the categories. When it is enabled it will use the value in * the 'depth' argument. * * @since 2.1.0 * * @param string|array $args Optional. Override default arguments. * @return string HTML content only if 'echo' argument is 0. */ function wp_dropdown_categories( $args = '' ) { $defaults = array( 'show_option_all' => '', 'show_option_none' => '', 'orderby' => 'id', 'order' => 'ASC', 'show_count' => 0, 'hide_empty' => 1, 'child_of' => 0, 'exclude' => '', 'echo' => 1, 'selected' => 0, 'hierarchical' => 0, 'name' => 'cat', 'id' => '', 'class' => 'postform', 'depth' => 0, 'tab_index' => 0, 'taxonomy' => 'category', 'hide_if_empty' => false ); $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0; // Back compat. if ( isset( $args['type'] ) && 'link' == $args['type'] ) { _deprecated_argument( __FUNCTION__, '3.0', '' ); $args['taxonomy'] = 'link_category'; } $r = wp_parse_args( $args, $defaults ); if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) { $r['pad_counts'] = true; } extract( $r ); $tab_index_attribute = ''; if ( (int) $tab_index > 0 ) $tab_index_attribute = " tabindex=\"$tab_index\""; $categories = get_terms( $taxonomy, $r ); $name = esc_attr( $name ); $class = esc_attr( $class ); $id = $id ? esc_attr( $id ) : $name; if ( ! $r['hide_if_empty'] || ! empty($categories) ) $output = "\n"; $output = apply_filters( 'wp_dropdown_cats', $output ); if ( $echo ) echo $output; return $output; } /** * Display or retrieve the HTML list of categories. * * The list of arguments is below: * 'show_option_all' (string) - Text to display for showing all categories. * 'orderby' (string) default is 'ID' - What column to use for ordering the * categories. * 'order' (string) default is 'ASC' - What direction to order categories. * 'show_count' (bool|int) default is 0 - Whether to show how many posts are * in the category. * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that * don't have any posts attached to them. * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the * description instead of the category title. * 'feed' - See {@link get_categories()}. * 'feed_type' - See {@link get_categories()}. * 'feed_image' - See {@link get_categories()}. * 'child_of' (int) default is 0 - See {@link get_categories()}. * 'exclude' (string) - See {@link get_categories()}. * 'exclude_tree' (string) - See {@link get_categories()}. * 'echo' (bool|int) default is 1 - Whether to display or retrieve content. * 'current_category' (int) - See {@link get_categories()}. * 'hierarchical' (bool) - See {@link get_categories()}. * 'title_li' (string) - See {@link get_categories()}. * 'depth' (int) - The max depth. * * @since 2.1.0 * * @param string|array $args Optional. Override default arguments. * @return string HTML content only if 'echo' argument is 0. */ function wp_list_categories( $args = '' ) { $defaults = array( 'show_option_all' => '', 'show_option_none' => __('No categories'), 'orderby' => 'name', 'order' => 'ASC', 'style' => 'list', 'show_count' => 0, 'hide_empty' => 1, 'use_desc_for_title' => 1, 'child_of' => 0, 'feed' => '', 'feed_type' => '', 'feed_image' => '', 'exclude' => '', 'exclude_tree' => '', 'current_category' => 0, 'hierarchical' => true, 'title_li' => __( 'Categories' ), 'echo' => 1, 'depth' => 0, 'taxonomy' => 'category' ); $r = wp_parse_args( $args, $defaults ); if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) $r['pad_counts'] = true; if ( true == $r['hierarchical'] ) { $r['exclude_tree'] = $r['exclude']; $r['exclude'] = ''; } if ( !isset( $r['class'] ) ) $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy']; extract( $r ); if ( !taxonomy_exists($taxonomy) ) return false; $categories = get_categories( $r ); $output = ''; if ( $title_li && 'list' == $style ) $output = '
  • ' . $title_li . '
  • '; $output = apply_filters( 'wp_list_categories', $output, $args ); if ( $echo ) echo $output; else return $output; } /** * Display tag cloud. * * The text size is set by the 'smallest' and 'largest' arguments, which will * use the 'unit' argument value for the CSS text size unit. The 'format' * argument can be 'flat' (default), 'list', or 'array'. The flat value for the * 'format' argument will separate tags with spaces. The list value for the * 'format' argument will format the tags in a UL HTML list. The array value for * the 'format' argument will return in PHP array type format. * * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'. * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'. * * The 'number' argument is how many tags to return. By default, the limit will * be to return the top 45 tags in the tag cloud list. * * The 'topic_count_text_callback' argument is a function, which, given the count * of the posts with that tag, returns a text for the tooltip of the tag link. * * The 'exclude' and 'include' arguments are used for the {@link get_tags()} * function. Only one should be used, because only one will be used and the * other ignored, if they are both set. * * @since 2.3.0 * * @param array|string $args Optional. Override default arguments. * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument. */ function wp_tag_cloud( $args = '' ) { $defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true ); $args = wp_parse_args( $args, $defaults ); $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags if ( empty( $tags ) || is_wp_error( $tags ) ) return; foreach ( $tags as $key => $tag ) { if ( 'edit' == $args['link'] ) $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy ); else $link = get_term_link( intval($tag->term_id), $tag->taxonomy ); if ( is_wp_error( $link ) ) return false; $tags[ $key ]->link = $link; $tags[ $key ]->id = $tag->term_id; } $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args $return = apply_filters( 'wp_tag_cloud', $return, $args ); if ( 'array' == $args['format'] || empty($args['echo']) ) return $return; echo $return; } /** * Default text for tooltip for tag links * * @param integer $count number of posts with that tag * @return string text for the tooltip of a tag link. */ function default_topic_count_text( $count ) { return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) ); } /** * Default topic count scaling for tag links * * @param integer $count number of posts with that tag * @return integer scaled count */ function default_topic_count_scale( $count ) { return round(log10($count + 1) * 100); } /** * Generates a tag cloud (heatmap) from provided data. * * The text size is set by the 'smallest' and 'largest' arguments, which will * use the 'unit' argument value for the CSS text size unit. The 'format' * argument can be 'flat' (default), 'list', or 'array'. The flat value for the * 'format' argument will separate tags with spaces. The list value for the * 'format' argument will format the tags in a UL HTML list. The array value for * the 'format' argument will return in PHP array type format. * * The 'tag_cloud_sort' filter allows you to override the sorting. * Passed to the filter: $tags array and $args array, has to return the $tags array * after sorting it. * * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'. * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or * 'RAND'. * * The 'number' argument is how many tags to return. By default, the limit will * be to return the entire tag cloud list. * * The 'topic_count_text_callback' argument is a function, which given the count * of the posts with that tag returns a text for the tooltip of the tag link. * * @todo Complete functionality. * @since 2.3.0 * * @param array $tags List of tags. * @param string|array $args Optional, override default arguments. * @return string */ function wp_generate_tag_cloud( $tags, $args = '' ) { $defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0, 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', 'topic_count_text_callback' => 'default_topic_count_text', 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1, ); if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { $body = 'return sprintf ( _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count), number_format_i18n( $count ));'; $args['topic_count_text_callback'] = create_function('$count', $body); } $args = wp_parse_args( $args, $defaults ); extract( $args ); if ( empty( $tags ) ) return; $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args ); if ( $tags_sorted != $tags ) { // the tags have been sorted by a plugin $tags = $tags_sorted; unset($tags_sorted); } else { if ( 'RAND' == $order ) { shuffle($tags); } else { // SQL cannot save you; this is a second (potentially different) sort on a subset of data. if ( 'name' == $orderby ) uasort( $tags, '_wp_object_name_sort_cb' ); else uasort( $tags, '_wp_object_count_sort_cb' ); if ( 'DESC' == $order ) $tags = array_reverse( $tags, true ); } } if ( $number > 0 ) $tags = array_slice($tags, 0, $number); $counts = array(); $real_counts = array(); // For the alt tag foreach ( (array) $tags as $key => $tag ) { $real_counts[ $key ] = $tag->count; $counts[ $key ] = $topic_count_scale_callback($tag->count); } $min_count = min( $counts ); $spread = max( $counts ) - $min_count; if ( $spread <= 0 ) $spread = 1; $font_spread = $largest - $smallest; if ( $font_spread < 0 ) $font_spread = 1; $font_step = $font_spread / $spread; $a = array(); foreach ( $tags as $key => $tag ) { $count = $counts[ $key ]; $real_count = $real_counts[ $key ]; $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#'; $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key; $tag_name = $tags[ $key ]->name; $a[] = "$tag_name"; } switch ( $format ) : case 'array' : $return =& $a; break; case 'list' : $return = "\n"; break; default : $return = join( $separator, $a ); break; endswitch; if ( $filter ) return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args ); else return $return; } /** * Callback for comparing objects based on name * * @since 3.1.0 * @access private */ function _wp_object_name_sort_cb( $a, $b ) { return strnatcasecmp( $a->name, $b->name ); } /** * Callback for comparing objects based on count * * @since 3.1.0 * @access private */ function _wp_object_count_sort_cb( $a, $b ) { return ( $a->count > $b->count ); } // // Helper functions // /** * Retrieve HTML list content for category list. * * @uses Walker_Category to create HTML list content. * @since 2.1.0 * @see Walker_Category::walk() for parameters and return description. */ function walk_category_tree() { $args = func_get_args(); // the user's options are the third parameter if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') ) $walker = new Walker_Category; else $walker = $args[2]['walker']; return call_user_func_array(array( &$walker, 'walk' ), $args ); } /** * Retrieve HTML dropdown (select) content for category list. * * @uses Walker_CategoryDropdown to create HTML dropdown content. * @since 2.1.0 * @see Walker_CategoryDropdown::walk() for parameters and return description. */ function walk_category_dropdown_tree() { $args = func_get_args(); // the user's options are the third parameter if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') ) $walker = new Walker_CategoryDropdown; else $walker = $args[2]['walker']; return call_user_func_array(array( &$walker, 'walk' ), $args ); } /** * Create HTML list of categories. * * @package WordPress * @since 2.1.0 * @uses Walker */ class Walker_Category extends Walker { /** * What the class handles. * * @see Walker::$tree_type * @since 2.1.0 * @var string */ var $tree_type = 'category'; /** * Database fields to use. * * @see Walker::$db_fields * @since 2.1.0 * @todo Decouple this * @var array */ var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); /** * Starts the list before the elements are added. * * @see Walker::start_lvl() * * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of category. Used for tab indentation. * @param array $args An array of arguments. Will only append content if style argument value is 'list'. * @see wp_list_categories() */ function start_lvl( &$output, $depth = 0, $args = array() ) { if ( 'list' != $args['style'] ) return; $indent = str_repeat("\t", $depth); $output .= "$indent\n"; } /** * Start the element output. * * @see Walker::start_el() * * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $category Category data object. * @param int $depth Depth of category in reference to parents. Default 0. * @param array $args An array of arguments. @see wp_list_categories() * @param int $id ID of the current category. */ function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { extract($args); $cat_name = esc_attr( $category->name ); $cat_name = apply_filters( 'list_cats', $cat_name, $category ); $link = 'description) ) $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"'; else $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"'; $link .= '>'; $link .= $cat_name . ''; if ( !empty($feed_image) || !empty($feed) ) { $link .= ' '; if ( empty($feed_image) ) $link .= '('; $link .= ''; $link .= ''; if ( empty($feed_image) ) $link .= ')'; } if ( !empty($show_count) ) $link .= ' (' . intval($category->count) . ')'; if ( 'list' == $args['style'] ) { $output .= "\tterm_id; if ( !empty($current_category) ) { $_current_category = get_term( $current_category, $category->taxonomy ); if ( $category->term_id == $current_category ) $class .= ' current-cat'; elseif ( $category->term_id == $_current_category->parent ) $class .= ' current-cat-parent'; } $output .= ' class="' . $class . '"'; $output .= ">$link\n"; } else { $output .= "\t$link
    \n"; } } /** * Ends the element output, if needed. * * @see Walker::end_el() * * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $page Not used. * @param int $depth Depth of category. Not used. * @param array $args An array of arguments. Only uses 'list' for whether should append to output. @see wp_list_categories() */ function end_el( &$output, $page, $depth = 0, $args = array() ) { if ( 'list' != $args['style'] ) return; $output .= "\n"; } } /** * Create HTML dropdown list of Categories. * * @package WordPress * @since 2.1.0 * @uses Walker */ class Walker_CategoryDropdown extends Walker { /** * @see Walker::$tree_type * @since 2.1.0 * @var string */ var $tree_type = 'category'; /** * @see Walker::$db_fields * @since 2.1.0 * @todo Decouple this * @var array */ var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); /** * Start the element output. * * @see Walker::start_el() * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $category Category data object. * @param int $depth Depth of category. Used for padding. * @param array $args Uses 'selected' and 'show_count' keys, if they exist. @see wp_dropdown_categories() */ function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { $pad = str_repeat(' ', $depth * 3); $cat_name = apply_filters('list_cats', $category->name, $category); $output .= "\t\n"; } } // // Tags // /** * Retrieve the link to the tag. * * @since 2.3.0 * @see get_term_link() * * @param int|object $tag Tag ID or object. * @return string Link on success, empty string if tag does not exist. */ function get_tag_link( $tag ) { if ( ! is_object( $tag ) ) $tag = (int) $tag; $tag = get_term_link( $tag, 'post_tag' ); if ( is_wp_error( $tag ) ) return ''; return $tag; } /** * Retrieve the tags for a post. * * @since 2.3.0 * @uses apply_filters() Calls 'get_the_tags' filter on the list of post tags. * * @param int $id Post ID. * @return array|bool Array of tag objects on success, false on failure. */ function get_the_tags( $id = 0 ) { return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) ); } /** * Retrieve the tags for a post formatted as a string. * * @since 2.3.0 * @uses apply_filters() Calls 'the_tags' filter on string list of tags. * * @param string $before Optional. Before tags. * @param string $sep Optional. Between tags. * @param string $after Optional. After tags. * @param int $id Optional. Post ID. Defaults to the current post. * @return string|bool|WP_Error A list of tags on success, false or WP_Error on failure. */ function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) { return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id ); } /** * Retrieve the tags for a post. * * @since 2.3.0 * * @param string $before Optional. Before list. * @param string $sep Optional. Separate items using this. * @param string $after Optional. After list. */ function the_tags( $before = null, $sep = ', ', $after = '' ) { if ( null === $before ) $before = __('Tags: '); echo get_the_tag_list($before, $sep, $after); } /** * Retrieve tag description. * * @since 2.8 * * @param int $tag Optional. Tag ID. Will use global tag ID by default. * @return string Tag description, available. */ function tag_description( $tag = 0 ) { return term_description( $tag ); } /** * Retrieve term description. * * @since 2.8 * * @param int $term Optional. Term ID. Will use global term ID by default. * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'. * @return string Term description, available. */ function term_description( $term = 0, $taxonomy = 'post_tag' ) { if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) { $term = get_queried_object(); if ( $term ) { $taxonomy = $term->taxonomy; $term = $term->term_id; } } $description = get_term_field( 'description', $term, $taxonomy ); return is_wp_error( $description ) ? '' : $description; } /** * Retrieve the terms of the taxonomy that are attached to the post. * * @since 2.5.0 * * @param int|object $post Post ID or object. * @param string $taxonomy Taxonomy name. * @return array|bool|WP_Error Array of term objects on success, false or WP_Error on failure. */ function get_the_terms( $post, $taxonomy ) { if ( ! $post = get_post( $post ) ) return false; $terms = get_object_term_cache( $post->ID, $taxonomy ); if ( false === $terms ) { $terms = wp_get_object_terms( $post->ID, $taxonomy ); wp_cache_add($post->ID, $terms, $taxonomy . '_relationships'); } $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy ); if ( empty( $terms ) ) return false; return $terms; } /** * Retrieve a post's terms as a list with specified format. * * @since 2.5.0 * * @param int $id Post ID. * @param string $taxonomy Taxonomy name. * @param string $before Optional. Before list. * @param string $sep Optional. Separate items using this. * @param string $after Optional. After list. * @return string|bool|WP_Error A list of terms on success, false or WP_Error on failure. */ function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) { $terms = get_the_terms( $id, $taxonomy ); if ( is_wp_error( $terms ) ) return $terms; if ( empty( $terms ) ) return false; foreach ( $terms as $term ) { $link = get_term_link( $term, $taxonomy ); if ( is_wp_error( $link ) ) return $link; $term_links[] = ''; } $term_links = apply_filters( "term_links-$taxonomy", $term_links ); return $before . join( $sep, $term_links ) . $after; } /** * Display the terms in a list. * * @since 2.5.0 * * @param int $id Post ID. * @param string $taxonomy Taxonomy name. * @param string $before Optional. Before list. * @param string $sep Optional. Separate items using this. * @param string $after Optional. After list. * @return null|bool False on WordPress error. Returns null when displaying. */ function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) { $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after ); if ( is_wp_error( $term_list ) ) return false; echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after); } /** * Check if the current post has any of given category. * * @since 3.1.0 * * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for. * @param int|object $post Optional. Post to check instead of the current post. * @return bool True if the current post has any of the given categories (or any category, if no category specified). */ function has_category( $category = '', $post = null ) { return has_term( $category, 'category', $post ); } /** * Check if the current post has any of given tags. * * The given tags are checked against the post's tags' term_ids, names and slugs. * Tags given as integers will only be checked against the post's tags' term_ids. * If no tags are given, determines if post has any tags. * * Prior to v2.7 of WordPress, tags given as integers would also be checked against the post's tags' names and slugs (in addition to term_ids) * Prior to v2.7, this function could only be used in the WordPress Loop. * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. * * @since 2.6.0 * * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0) * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). */ function has_tag( $tag = '', $post = null ) { return has_term( $tag, 'post_tag', $post ); } /** * Check if the current post has any of given terms. * * The given terms are checked against the post's terms' term_ids, names and slugs. * Terms given as integers will only be checked against the post's terms' term_ids. * If no terms are given, determines if post has any terms. * * @since 3.1.0 * * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for. * @param string $taxonomy Taxonomy name * @param int|object $post Optional. Post to check instead of the current post. * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). */ function has_term( $term = '', $taxonomy = '', $post = null ) { $post = get_post($post); if ( !$post ) return false; $r = is_object_in_term( $post->ID, $taxonomy, $term ); if ( is_wp_error( $r ) ) return false; return $r; }