426 * @since 2.0.0 |
408 * @since 2.0.0 |
427 * @access public |
409 * @access public |
428 * @var array |
410 * @var array |
429 */ |
411 */ |
430 var $allcaps = array(); |
412 var $allcaps = array(); |
431 |
|
432 /** |
|
433 * First name of the user. |
|
434 * |
|
435 * Created to prevent notices. |
|
436 * |
|
437 * @since 2.7.0 |
|
438 * @access public |
|
439 * @var string |
|
440 */ |
|
441 var $first_name = ''; |
|
442 |
|
443 /** |
|
444 * Last name of the user. |
|
445 * |
|
446 * Created to prevent notices. |
|
447 * |
|
448 * @since 2.7.0 |
|
449 * @access public |
|
450 * @var string |
|
451 */ |
|
452 var $last_name = ''; |
|
453 |
413 |
454 /** |
414 /** |
455 * The filter context applied to user data fields. |
415 * The filter context applied to user data fields. |
456 * |
416 * |
457 * @since 2.9.0 |
417 * @since 2.9.0 |
458 * @access private |
418 * @access private |
459 * @var string |
419 * @var string |
460 */ |
420 */ |
461 var $filter = null; |
421 var $filter = null; |
462 |
422 |
463 /** |
423 private static $back_compat_keys; |
464 * PHP4 Constructor - Sets up the object properties. |
424 |
465 * |
425 /** |
466 * Retrieves the userdata and then assigns all of the data keys to direct |
426 * Constructor |
467 * properties of the object. Calls {@link WP_User::_init_caps()} after |
427 * |
468 * setting up the object's user data properties. |
428 * Retrieves the userdata and passes it to {@link WP_User::init()}. |
469 * |
429 * |
470 * @since 2.0.0 |
430 * @since 2.0.0 |
471 * @access public |
431 * @access public |
472 * |
432 * |
473 * @param int|string $id User's ID or username |
433 * @param int|string $id User's ID |
474 * @param int $name Optional. User's username |
434 * @param string $name Optional. User's username |
|
435 * @param int $blog_id Optional Blog ID, defaults to current blog. |
475 * @return WP_User |
436 * @return WP_User |
476 */ |
437 */ |
477 function WP_User( $id, $name = '' ) { |
438 function __construct( $id = 0, $name = '', $blog_id = '' ) { |
478 |
439 if ( ! isset( self::$back_compat_keys ) ) { |
479 if ( empty( $id ) && empty( $name ) ) |
440 $prefix = $GLOBALS['wpdb']->prefix; |
480 return; |
441 self::$back_compat_keys = array( |
481 |
442 'user_firstname' => 'first_name', |
482 if ( ! is_numeric( $id ) ) { |
443 'user_lastname' => 'last_name', |
|
444 'user_description' => 'description', |
|
445 'user_level' => $prefix . 'user_level', |
|
446 $prefix . 'usersettings' => $prefix . 'user-settings', |
|
447 $prefix . 'usersettingstime' => $prefix . 'user-settings-time', |
|
448 ); |
|
449 } |
|
450 |
|
451 if ( ! empty( $id ) && ! is_numeric( $id ) ) { |
483 $name = $id; |
452 $name = $id; |
484 $id = 0; |
453 $id = 0; |
485 } |
454 } |
486 |
455 |
487 if ( ! empty( $id ) ) |
456 if ( $id ) |
488 $this->data = get_userdata( $id ); |
457 $data = self::get_data_by( 'id', $id ); |
489 else |
458 else |
490 $this->data = get_userdatabylogin( $name ); |
459 $data = self::get_data_by( 'login', $name ); |
491 |
460 |
492 if ( empty( $this->data->ID ) ) |
461 if ( $data ) |
|
462 $this->init( $data, $blog_id ); |
|
463 } |
|
464 |
|
465 /** |
|
466 * Sets up object properties, including capabilities. |
|
467 * |
|
468 * @param object $data User DB row object |
|
469 * @param int $blog_id Optional. The blog id to initialize for |
|
470 */ |
|
471 function init( $data, $blog_id = '' ) { |
|
472 $this->data = $data; |
|
473 $this->ID = (int) $data->ID; |
|
474 |
|
475 $this->for_blog( $blog_id ); |
|
476 } |
|
477 |
|
478 /** |
|
479 * Return only the main user fields |
|
480 * |
|
481 * @since 3.3.0 |
|
482 * |
|
483 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login' |
|
484 * @param string|int $value The field value |
|
485 * @return object Raw user object |
|
486 */ |
|
487 static function get_data_by( $field, $value ) { |
|
488 global $wpdb; |
|
489 |
|
490 if ( 'id' == $field ) { |
|
491 // Make sure the value is numeric to avoid casting objects, for example, |
|
492 // to int 1. |
|
493 if ( ! is_numeric( $value ) ) |
|
494 return false; |
|
495 $value = absint( $value ); |
|
496 } else { |
|
497 $value = trim( $value ); |
|
498 } |
|
499 |
|
500 if ( !$value ) |
|
501 return false; |
|
502 |
|
503 switch ( $field ) { |
|
504 case 'id': |
|
505 $user_id = $value; |
|
506 $db_field = 'ID'; |
|
507 break; |
|
508 case 'slug': |
|
509 $user_id = wp_cache_get($value, 'userslugs'); |
|
510 $db_field = 'user_nicename'; |
|
511 break; |
|
512 case 'email': |
|
513 $user_id = wp_cache_get($value, 'useremail'); |
|
514 $db_field = 'user_email'; |
|
515 break; |
|
516 case 'login': |
|
517 $value = sanitize_user( $value ); |
|
518 $user_id = wp_cache_get($value, 'userlogins'); |
|
519 $db_field = 'user_login'; |
|
520 break; |
|
521 default: |
|
522 return false; |
|
523 } |
|
524 |
|
525 if ( false !== $user_id ) { |
|
526 if ( $user = wp_cache_get( $user_id, 'users' ) ) |
|
527 return $user; |
|
528 } |
|
529 |
|
530 if ( !$user = $wpdb->get_row( $wpdb->prepare( |
|
531 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value |
|
532 ) ) ) |
|
533 return false; |
|
534 |
|
535 update_user_caches( $user ); |
|
536 |
|
537 return $user; |
|
538 } |
|
539 |
|
540 /** |
|
541 * Magic method for checking the existence of a certain custom field |
|
542 * |
|
543 * @since 3.3.0 |
|
544 */ |
|
545 function __isset( $key ) { |
|
546 if ( 'id' == $key ) { |
|
547 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) ); |
|
548 $key = 'ID'; |
|
549 } |
|
550 |
|
551 if ( isset( $this->data->$key ) ) |
|
552 return true; |
|
553 |
|
554 if ( isset( self::$back_compat_keys[ $key ] ) ) |
|
555 $key = self::$back_compat_keys[ $key ]; |
|
556 |
|
557 return metadata_exists( 'user', $this->ID, $key ); |
|
558 } |
|
559 |
|
560 /** |
|
561 * Magic method for accessing custom fields |
|
562 * |
|
563 * @since 3.3.0 |
|
564 */ |
|
565 function __get( $key ) { |
|
566 if ( 'id' == $key ) { |
|
567 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) ); |
|
568 return $this->ID; |
|
569 } |
|
570 |
|
571 if ( isset( $this->data->$key ) ) { |
|
572 $value = $this->data->$key; |
|
573 } else { |
|
574 if ( isset( self::$back_compat_keys[ $key ] ) ) |
|
575 $key = self::$back_compat_keys[ $key ]; |
|
576 $value = get_user_meta( $this->ID, $key, true ); |
|
577 } |
|
578 |
|
579 if ( $this->filter ) { |
|
580 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter ); |
|
581 } |
|
582 |
|
583 return $value; |
|
584 } |
|
585 |
|
586 /** |
|
587 * Magic method for setting custom fields |
|
588 * |
|
589 * @since 3.3.0 |
|
590 */ |
|
591 function __set( $key, $value ) { |
|
592 if ( 'id' == $key ) { |
|
593 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) ); |
|
594 $this->ID = $value; |
493 return; |
595 return; |
494 |
596 } |
495 foreach ( get_object_vars( $this->data ) as $key => $value ) { |
597 |
496 $this->{$key} = $value; |
598 $this->data->$key = $value; |
497 } |
599 } |
498 |
600 |
499 $this->id = $this->ID; |
601 /** |
500 $this->_init_caps(); |
602 * Determine whether the user exists in the database. |
501 } |
603 * |
502 |
604 * @since 3.4.0 |
503 /** |
605 * @access public |
504 * Setup capability object properties. |
606 * |
|
607 * @return bool True if user exists in the database, false if not. |
|
608 */ |
|
609 function exists() { |
|
610 return ! empty( $this->ID ); |
|
611 } |
|
612 |
|
613 /** |
|
614 * Retrieve the value of a property or meta key. |
|
615 * |
|
616 * Retrieves from the users and usermeta table. |
|
617 * |
|
618 * @since 3.3.0 |
|
619 * |
|
620 * @param string $key Property |
|
621 */ |
|
622 function get( $key ) { |
|
623 return $this->__get( $key ); |
|
624 } |
|
625 |
|
626 /** |
|
627 * Determine whether a property or meta key is set |
|
628 * |
|
629 * Consults the users and usermeta tables. |
|
630 * |
|
631 * @since 3.3.0 |
|
632 * |
|
633 * @param string $key Property |
|
634 */ |
|
635 function has_prop( $key ) { |
|
636 return $this->__isset( $key ); |
|
637 } |
|
638 |
|
639 /** |
|
640 * Set up capability object properties. |
505 * |
641 * |
506 * Will set the value for the 'cap_key' property to current database table |
642 * Will set the value for the 'cap_key' property to current database table |
507 * prefix, followed by 'capabilities'. Will then check to see if the |
643 * prefix, followed by 'capabilities'. Will then check to see if the |
508 * property matching the 'cap_key' exists and is an array. If so, it will be |
644 * property matching the 'cap_key' exists and is an array. If so, it will be |
509 * used. |
645 * used. |
510 * |
646 * |
|
647 * @access protected |
511 * @since 2.1.0 |
648 * @since 2.1.0 |
512 * @access protected |
649 * |
513 */ |
650 * @param string $cap_key Optional capability key |
514 function _init_caps() { |
651 */ |
|
652 function _init_caps( $cap_key = '' ) { |
515 global $wpdb; |
653 global $wpdb; |
516 $this->cap_key = $wpdb->prefix . 'capabilities'; |
654 |
517 $this->caps = &$this->{$this->cap_key}; |
655 if ( empty($cap_key) ) |
|
656 $this->cap_key = $wpdb->prefix . 'capabilities'; |
|
657 else |
|
658 $this->cap_key = $cap_key; |
|
659 |
|
660 $this->caps = get_user_meta( $this->ID, $this->cap_key, true ); |
|
661 |
518 if ( ! is_array( $this->caps ) ) |
662 if ( ! is_array( $this->caps ) ) |
519 $this->caps = array(); |
663 $this->caps = array(); |
|
664 |
520 $this->get_role_caps(); |
665 $this->get_role_caps(); |
521 } |
666 } |
522 |
667 |
523 /** |
668 /** |
524 * Retrieve all of the role capabilities and merge with individual capabilities. |
669 * Retrieve all of the role capabilities and merge with individual capabilities. |
690 * @access public |
841 * @access public |
691 */ |
842 */ |
692 function remove_all_caps() { |
843 function remove_all_caps() { |
693 global $wpdb; |
844 global $wpdb; |
694 $this->caps = array(); |
845 $this->caps = array(); |
695 update_usermeta( $this->ID, $this->cap_key, '' ); |
846 delete_user_meta( $this->ID, $this->cap_key ); |
696 update_usermeta( $this->ID, $wpdb->prefix.'user_level', '' ); |
847 delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' ); |
697 $this->get_role_caps(); |
848 $this->get_role_caps(); |
698 } |
849 } |
699 |
850 |
700 /** |
851 /** |
701 * Whether user has capability or role name. |
852 * Whether user has capability or role name. |
702 * |
853 * |
703 * This is useful for looking up whether the user has a specific role |
854 * This is useful for looking up whether the user has a specific role |
704 * assigned to the user. The second optional parameter can also be used to |
855 * assigned to the user. The second optional parameter can also be used to |
705 * check for capabilities against a specfic post. |
856 * check for capabilities against a specific object, such as a post or user. |
706 * |
857 * |
707 * @since 2.0.0 |
858 * @since 2.0.0 |
708 * @access public |
859 * @access public |
709 * |
860 * |
710 * @param string|int $cap Capability or role name to search. |
861 * @param string|int $cap Capability or role name to search. |
711 * @param int $post_id Optional. Post ID to check capability against specific post. |
|
712 * @return bool True, if user has capability; false, if user does not have capability. |
862 * @return bool True, if user has capability; false, if user does not have capability. |
713 */ |
863 */ |
714 function has_cap( $cap ) { |
864 function has_cap( $cap ) { |
715 if ( is_numeric( $cap ) ) |
865 if ( is_numeric( $cap ) ) { |
|
866 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') ); |
716 $cap = $this->translate_level_to_cap( $cap ); |
867 $cap = $this->translate_level_to_cap( $cap ); |
|
868 } |
717 |
869 |
718 $args = array_slice( func_get_args(), 1 ); |
870 $args = array_slice( func_get_args(), 1 ); |
719 $args = array_merge( array( $cap, $this->ID ), $args ); |
871 $args = array_merge( array( $cap, $this->ID ), $args ); |
720 $caps = call_user_func_array( 'map_meta_cap', $args ); |
872 $caps = call_user_func_array( 'map_meta_cap', $args ); |
|
873 |
|
874 // Multisite super admin has all caps by definition, Unless specifically denied. |
|
875 if ( is_multisite() && is_super_admin( $this->ID ) ) { |
|
876 if ( in_array('do_not_allow', $caps) ) |
|
877 return false; |
|
878 return true; |
|
879 } |
|
880 |
721 // Must have ALL requested caps |
881 // Must have ALL requested caps |
722 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args ); |
882 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args ); |
|
883 $capabilities['exist'] = true; // Everyone is allowed to exist |
723 foreach ( (array) $caps as $cap ) { |
884 foreach ( (array) $caps as $cap ) { |
724 //echo "Checking cap $cap<br />"; |
885 if ( empty( $capabilities[ $cap ] ) ) |
725 if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] ) |
|
726 return false; |
886 return false; |
727 } |
887 } |
728 |
888 |
729 return true; |
889 return true; |
730 } |
890 } |
763 function map_meta_cap( $cap, $user_id ) { |
938 function map_meta_cap( $cap, $user_id ) { |
764 $args = array_slice( func_get_args(), 2 ); |
939 $args = array_slice( func_get_args(), 2 ); |
765 $caps = array(); |
940 $caps = array(); |
766 |
941 |
767 switch ( $cap ) { |
942 switch ( $cap ) { |
768 case 'delete_user': |
943 case 'remove_user': |
769 $caps[] = 'delete_users'; |
944 $caps[] = 'remove_users'; |
|
945 break; |
|
946 case 'promote_user': |
|
947 $caps[] = 'promote_users'; |
770 break; |
948 break; |
771 case 'edit_user': |
949 case 'edit_user': |
772 if ( !isset( $args[0] ) || $user_id != $args[0] ) { |
950 // Allow user to edit itself |
773 $caps[] = 'edit_users'; |
951 if ( isset( $args[0] ) && $user_id == $args[0] ) |
774 } |
952 break; |
|
953 // Fall through |
|
954 case 'edit_users': |
|
955 // If multisite these caps are allowed only for super admins. |
|
956 if ( is_multisite() && !is_super_admin( $user_id ) ) |
|
957 $caps[] = 'do_not_allow'; |
|
958 else |
|
959 $caps[] = 'edit_users'; // Explicit due to primitive fall through |
775 break; |
960 break; |
776 case 'delete_post': |
961 case 'delete_post': |
|
962 case 'delete_page': |
777 $author_data = get_userdata( $user_id ); |
963 $author_data = get_userdata( $user_id ); |
778 //echo "post ID: {$args[0]}<br />"; |
|
779 $post = get_post( $args[0] ); |
964 $post = get_post( $args[0] ); |
780 if ( 'page' == $post->post_type ) { |
965 |
781 $args = array_merge( array( 'delete_page', $user_id ), $args ); |
966 if ( 'revision' == $post->post_type ) { |
782 return call_user_func_array( 'map_meta_cap', $args ); |
967 $post = get_post( $post->post_parent ); |
783 } |
968 } |
784 |
969 |
785 if ('' != $post->post_author) { |
970 $post_type = get_post_type_object( $post->post_type ); |
|
971 |
|
972 if ( ! $post_type->map_meta_cap ) { |
|
973 $caps[] = $post_type->cap->$cap; |
|
974 // Prior to 3.1 we would re-call map_meta_cap here. |
|
975 if ( 'delete_post' == $cap ) |
|
976 $cap = $post_type->cap->$cap; |
|
977 break; |
|
978 } |
|
979 |
|
980 if ( '' != $post->post_author ) { |
786 $post_author_data = get_userdata( $post->post_author ); |
981 $post_author_data = get_userdata( $post->post_author ); |
787 } else { |
982 } else { |
788 //No author set yet so default to current user for cap checks |
983 // No author set yet, so default to current user for cap checks. |
789 $post_author_data = $author_data; |
984 $post_author_data = $author_data; |
790 } |
985 } |
791 |
986 |
792 // If the user is the author... |
987 // If the user is the author... |
793 if ( $user_id == $post_author_data->ID ) { |
988 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) { |
794 // If the post is published... |
989 // If the post is published... |
795 if ( 'publish' == $post->post_status ) { |
990 if ( 'publish' == $post->post_status ) { |
796 $caps[] = 'delete_published_posts'; |
991 $caps[] = $post_type->cap->delete_published_posts; |
797 } elseif ( 'trash' == $post->post_status ) { |
992 } elseif ( 'trash' == $post->post_status ) { |
798 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) ) |
993 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) ) |
799 $caps[] = 'delete_published_posts'; |
994 $caps[] = $post_type->cap->delete_published_posts; |
800 } else { |
995 } else { |
801 // If the post is draft... |
996 // If the post is draft... |
802 $caps[] = 'delete_posts'; |
997 $caps[] = $post_type->cap->delete_posts; |
803 } |
998 } |
804 } else { |
999 } else { |
805 // The user is trying to edit someone else's post. |
1000 // The user is trying to edit someone else's post. |
806 $caps[] = 'delete_others_posts'; |
1001 $caps[] = $post_type->cap->delete_others_posts; |
807 // The post is published, extra cap required. |
1002 // The post is published, extra cap required. |
808 if ( 'publish' == $post->post_status ) |
1003 if ( 'publish' == $post->post_status ) |
809 $caps[] = 'delete_published_posts'; |
1004 $caps[] = $post_type->cap->delete_published_posts; |
810 elseif ( 'private' == $post->post_status ) |
1005 elseif ( 'private' == $post->post_status ) |
811 $caps[] = 'delete_private_posts'; |
1006 $caps[] = $post_type->cap->delete_private_posts; |
812 } |
|
813 break; |
|
814 case 'delete_page': |
|
815 $author_data = get_userdata( $user_id ); |
|
816 //echo "post ID: {$args[0]}<br />"; |
|
817 $page = get_page( $args[0] ); |
|
818 $page_author_data = get_userdata( $page->post_author ); |
|
819 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />"; |
|
820 // If the user is the author... |
|
821 |
|
822 if ('' != $page->post_author) { |
|
823 $page_author_data = get_userdata( $page->post_author ); |
|
824 } else { |
|
825 //No author set yet so default to current user for cap checks |
|
826 $page_author_data = $author_data; |
|
827 } |
|
828 |
|
829 if ( $user_id == $page_author_data->ID ) { |
|
830 // If the page is published... |
|
831 if ( $page->post_status == 'publish' ) { |
|
832 $caps[] = 'delete_published_pages'; |
|
833 } elseif ( 'trash' == $page->post_status ) { |
|
834 if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) ) |
|
835 $caps[] = 'delete_published_pages'; |
|
836 } else { |
|
837 // If the page is draft... |
|
838 $caps[] = 'delete_pages'; |
|
839 } |
|
840 } else { |
|
841 // The user is trying to edit someone else's page. |
|
842 $caps[] = 'delete_others_pages'; |
|
843 // The page is published, extra cap required. |
|
844 if ( $page->post_status == 'publish' ) |
|
845 $caps[] = 'delete_published_pages'; |
|
846 elseif ( $page->post_status == 'private' ) |
|
847 $caps[] = 'delete_private_pages'; |
|
848 } |
1007 } |
849 break; |
1008 break; |
850 // edit_post breaks down to edit_posts, edit_published_posts, or |
1009 // edit_post breaks down to edit_posts, edit_published_posts, or |
851 // edit_others_posts |
1010 // edit_others_posts |
852 case 'edit_post': |
1011 case 'edit_post': |
|
1012 case 'edit_page': |
853 $author_data = get_userdata( $user_id ); |
1013 $author_data = get_userdata( $user_id ); |
854 //echo "post ID: {$args[0]}<br />"; |
|
855 $post = get_post( $args[0] ); |
1014 $post = get_post( $args[0] ); |
856 if ( 'page' == $post->post_type ) { |
1015 |
857 $args = array_merge( array( 'edit_page', $user_id ), $args ); |
1016 if ( 'revision' == $post->post_type ) { |
858 return call_user_func_array( 'map_meta_cap', $args ); |
1017 $post = get_post( $post->post_parent ); |
859 } |
1018 } |
860 $post_author_data = get_userdata( $post->post_author ); |
1019 |
|
1020 $post_type = get_post_type_object( $post->post_type ); |
|
1021 |
|
1022 if ( ! $post_type->map_meta_cap ) { |
|
1023 $caps[] = $post_type->cap->$cap; |
|
1024 // Prior to 3.1 we would re-call map_meta_cap here. |
|
1025 if ( 'edit_post' == $cap ) |
|
1026 $cap = $post_type->cap->$cap; |
|
1027 break; |
|
1028 } |
|
1029 |
|
1030 if ( '' != $post->post_author ) { |
|
1031 $post_author_data = get_userdata( $post->post_author ); |
|
1032 } else { |
|
1033 // No author set yet, so default to current user for cap checks. |
|
1034 $post_author_data = $author_data; |
|
1035 } |
|
1036 |
861 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />"; |
1037 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />"; |
862 // If the user is the author... |
1038 // If the user is the author... |
863 if ( $user_id == $post_author_data->ID ) { |
1039 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) { |
864 // If the post is published... |
1040 // If the post is published... |
865 if ( 'publish' == $post->post_status ) { |
1041 if ( 'publish' == $post->post_status ) { |
866 $caps[] = 'edit_published_posts'; |
1042 $caps[] = $post_type->cap->edit_published_posts; |
867 } elseif ( 'trash' == $post->post_status ) { |
1043 } elseif ( 'trash' == $post->post_status ) { |
868 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) ) |
1044 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) ) |
869 $caps[] = 'edit_published_posts'; |
1045 $caps[] = $post_type->cap->edit_published_posts; |
870 } else { |
1046 } else { |
871 // If the post is draft... |
1047 // If the post is draft... |
872 $caps[] = 'edit_posts'; |
1048 $caps[] = $post_type->cap->edit_posts; |
873 } |
1049 } |
874 } else { |
1050 } else { |
875 // The user is trying to edit someone else's post. |
1051 // The user is trying to edit someone else's post. |
876 $caps[] = 'edit_others_posts'; |
1052 $caps[] = $post_type->cap->edit_others_posts; |
877 // The post is published, extra cap required. |
1053 // The post is published, extra cap required. |
878 if ( 'publish' == $post->post_status ) |
1054 if ( 'publish' == $post->post_status ) |
879 $caps[] = 'edit_published_posts'; |
1055 $caps[] = $post_type->cap->edit_published_posts; |
880 elseif ( 'private' == $post->post_status ) |
1056 elseif ( 'private' == $post->post_status ) |
881 $caps[] = 'edit_private_posts'; |
1057 $caps[] = $post_type->cap->edit_private_posts; |
882 } |
|
883 break; |
|
884 case 'edit_page': |
|
885 $author_data = get_userdata( $user_id ); |
|
886 //echo "post ID: {$args[0]}<br />"; |
|
887 $page = get_page( $args[0] ); |
|
888 $page_author_data = get_userdata( $page->post_author ); |
|
889 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />"; |
|
890 // If the user is the author... |
|
891 if ( $user_id == $page_author_data->ID ) { |
|
892 // If the page is published... |
|
893 if ( 'publish' == $page->post_status ) { |
|
894 $caps[] = 'edit_published_pages'; |
|
895 } elseif ( 'trash' == $page->post_status ) { |
|
896 if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) ) |
|
897 $caps[] = 'edit_published_pages'; |
|
898 } else { |
|
899 // If the page is draft... |
|
900 $caps[] = 'edit_pages'; |
|
901 } |
|
902 } else { |
|
903 // The user is trying to edit someone else's page. |
|
904 $caps[] = 'edit_others_pages'; |
|
905 // The page is published, extra cap required. |
|
906 if ( 'publish' == $page->post_status ) |
|
907 $caps[] = 'edit_published_pages'; |
|
908 elseif ( 'private' == $page->post_status ) |
|
909 $caps[] = 'edit_private_pages'; |
|
910 } |
1058 } |
911 break; |
1059 break; |
912 case 'read_post': |
1060 case 'read_post': |
|
1061 case 'read_page': |
|
1062 $author_data = get_userdata( $user_id ); |
913 $post = get_post( $args[0] ); |
1063 $post = get_post( $args[0] ); |
914 if ( 'page' == $post->post_type ) { |
1064 |
915 $args = array_merge( array( 'read_page', $user_id ), $args ); |
1065 if ( 'revision' == $post->post_type ) { |
916 return call_user_func_array( 'map_meta_cap', $args ); |
1066 $post = get_post( $post->post_parent ); |
917 } |
1067 } |
918 |
1068 |
919 if ( 'private' != $post->post_status ) { |
1069 $post_type = get_post_type_object( $post->post_type ); |
920 $caps[] = 'read'; |
1070 |
|
1071 if ( ! $post_type->map_meta_cap ) { |
|
1072 $caps[] = $post_type->cap->$cap; |
|
1073 // Prior to 3.1 we would re-call map_meta_cap here. |
|
1074 if ( 'read_post' == $cap ) |
|
1075 $cap = $post_type->cap->$cap; |
921 break; |
1076 break; |
922 } |
1077 } |
923 |
1078 |
924 $author_data = get_userdata( $user_id ); |
1079 $status_obj = get_post_status_object( $post->post_status ); |
925 $post_author_data = get_userdata( $post->post_author ); |
1080 if ( $status_obj->public ) { |
926 if ( $user_id == $post_author_data->ID ) |
1081 $caps[] = $post_type->cap->read; |
927 $caps[] = 'read'; |
1082 break; |
|
1083 } |
|
1084 |
|
1085 if ( '' != $post->post_author ) { |
|
1086 $post_author_data = get_userdata( $post->post_author ); |
|
1087 } else { |
|
1088 // No author set yet, so default to current user for cap checks. |
|
1089 $post_author_data = $author_data; |
|
1090 } |
|
1091 |
|
1092 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) |
|
1093 $caps[] = $post_type->cap->read; |
|
1094 elseif ( $status_obj->private ) |
|
1095 $caps[] = $post_type->cap->read_private_posts; |
928 else |
1096 else |
929 $caps[] = 'read_private_posts'; |
1097 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); |
930 break; |
1098 break; |
931 case 'read_page': |
1099 case 'edit_post_meta': |
932 $page = get_page( $args[0] ); |
1100 case 'delete_post_meta': |
933 |
1101 case 'add_post_meta': |
934 if ( 'private' != $page->post_status ) { |
1102 $post = get_post( $args[0] ); |
935 $caps[] = 'read'; |
1103 $post_type_object = get_post_type_object( $post->post_type ); |
936 break; |
1104 $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID ); |
937 } |
1105 |
938 |
1106 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false; |
939 $author_data = get_userdata( $user_id ); |
1107 |
940 $page_author_data = get_userdata( $page->post_author ); |
1108 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) { |
941 if ( $user_id == $page_author_data->ID ) |
1109 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps ); |
942 $caps[] = 'read'; |
1110 if ( ! $allowed ) |
943 else |
1111 $caps[] = $cap; |
944 $caps[] = 'read_private_pages'; |
1112 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) { |
|
1113 $caps[] = $cap; |
|
1114 } |
|
1115 break; |
|
1116 case 'edit_comment': |
|
1117 $comment = get_comment( $args[0] ); |
|
1118 $post = get_post( $comment->comment_post_ID ); |
|
1119 $post_type_object = get_post_type_object( $post->post_type ); |
|
1120 |
|
1121 $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID ); |
945 break; |
1122 break; |
946 case 'unfiltered_upload': |
1123 case 'unfiltered_upload': |
947 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS == true ) |
1124 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) ) |
|
1125 $caps[] = $cap; |
|
1126 else |
|
1127 $caps[] = 'do_not_allow'; |
|
1128 break; |
|
1129 case 'unfiltered_html' : |
|
1130 // Disallow unfiltered_html for all users, even admins and super admins. |
|
1131 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) |
|
1132 $caps[] = 'do_not_allow'; |
|
1133 elseif ( is_multisite() && ! is_super_admin( $user_id ) ) |
|
1134 $caps[] = 'do_not_allow'; |
|
1135 else |
|
1136 $caps[] = $cap; |
|
1137 break; |
|
1138 case 'edit_files': |
|
1139 case 'edit_plugins': |
|
1140 case 'edit_themes': |
|
1141 if ( defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ) { |
|
1142 $caps[] = 'do_not_allow'; |
|
1143 break; |
|
1144 } |
|
1145 // Fall through if not DISALLOW_FILE_EDIT. |
|
1146 case 'update_plugins': |
|
1147 case 'delete_plugins': |
|
1148 case 'install_plugins': |
|
1149 case 'update_themes': |
|
1150 case 'delete_themes': |
|
1151 case 'install_themes': |
|
1152 case 'update_core': |
|
1153 // Disallow anything that creates, deletes, or edits core, plugin, or theme files. |
|
1154 // Files in uploads are excepted. |
|
1155 if ( defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ) { |
|
1156 $caps[] = 'do_not_allow'; |
|
1157 break; |
|
1158 } |
|
1159 // Fall through if not DISALLOW_FILE_MODS. |
|
1160 case 'delete_user': |
|
1161 case 'delete_users': |
|
1162 // If multisite these caps are allowed only for super admins. |
|
1163 if ( is_multisite() && !is_super_admin( $user_id ) ) { |
|
1164 $caps[] = 'do_not_allow'; |
|
1165 } else { |
|
1166 if ( 'delete_user' == $cap ) |
|
1167 $cap = 'delete_users'; |
|
1168 $caps[] = $cap; |
|
1169 } |
|
1170 break; |
|
1171 case 'create_users': |
|
1172 if ( !is_multisite() ) |
|
1173 $caps[] = $cap; |
|
1174 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) ) |
948 $caps[] = $cap; |
1175 $caps[] = $cap; |
949 else |
1176 else |
950 $caps[] = 'do_not_allow'; |
1177 $caps[] = 'do_not_allow'; |
951 break; |
1178 break; |
952 default: |
1179 default: |
|
1180 // Handle meta capabilities for custom post types. |
|
1181 $post_type_meta_caps = _post_type_meta_capabilities(); |
|
1182 if ( isset( $post_type_meta_caps[ $cap ] ) ) { |
|
1183 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args ); |
|
1184 return call_user_func_array( 'map_meta_cap', $args ); |
|
1185 } |
|
1186 |
953 // If no meta caps match, return the original cap. |
1187 // If no meta caps match, return the original cap. |
954 $caps[] = $cap; |
1188 $caps[] = $cap; |
955 } |
1189 } |
956 |
1190 |
957 return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args); |
1191 return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args); |