~canonical-sysadmins/wordpress/4.8.3

« back to all changes in this revision

Viewing changes to wp-includes/capabilities.php

  • Committer: Ryan Finnie
  • Date: 2015-08-31 16:09:47 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: ryan.finnie@canonical.com-20150831160947-1h6rfxby9z1ec62u
Merge WP4.3 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
87
87
         *
88
88
         * @param callable $name      Method to call.
89
89
         * @param array    $arguments Arguments to pass when calling.
90
 
         * @return mixed|bool Return value of the callback, false otherwise.
 
90
         * @return mixed|false Return value of the callback, false otherwise.
91
91
         */
92
92
        public function __call( $name, $arguments ) {
93
93
                if ( '_init' === $name ) {
138
138
         *
139
139
         * @since 3.5.0
140
140
         * @access public
 
141
         *
 
142
         * @global wpdb $wpdb
141
143
         */
142
144
        public function reinit() {
143
145
                // There is no need to reinit if using the wp_user_roles global.
174
176
         * @param string $role Role name.
175
177
         * @param string $display_name Role display name.
176
178
         * @param array $capabilities List of role capabilities in the above format.
177
 
         * @return WP_Role|null WP_Role object if role is added, null if already exists.
 
179
         * @return WP_Role|void WP_Role object, if role is added.
178
180
         */
179
181
        public function add_role( $role, $display_name, $capabilities = array() ) {
180
182
                if ( isset( $this->roles[$role] ) )
339
341
        /**
340
342
         * Assign role a capability.
341
343
         *
342
 
         * @see WP_Roles::add_cap() Method uses implementation for role.
343
344
         * @since 2.0.0
344
345
         * @access public
345
346
         *
347
348
         * @param bool $grant Whether role has capability privilege.
348
349
         */
349
350
        public function add_cap( $cap, $grant = true ) {
350
 
                global $wp_roles;
351
 
 
352
 
                if ( ! isset( $wp_roles ) )
353
 
                        $wp_roles = new WP_Roles();
354
 
 
355
351
                $this->capabilities[$cap] = $grant;
356
 
                $wp_roles->add_cap( $this->name, $cap, $grant );
 
352
                wp_roles()->add_cap( $this->name, $cap, $grant );
357
353
        }
358
354
 
359
355
        /**
370
366
         * @param string $cap Capability name.
371
367
         */
372
368
        public function remove_cap( $cap ) {
373
 
                global $wp_roles;
374
 
 
375
 
                if ( ! isset( $wp_roles ) )
376
 
                        $wp_roles = new WP_Roles();
377
 
 
378
369
                unset( $this->capabilities[$cap] );
379
 
                $wp_roles->remove_cap( $this->name, $cap );
 
370
                wp_roles()->remove_cap( $this->name, $cap );
380
371
        }
381
372
 
382
373
        /**
498
489
         */
499
490
        var $filter = null;
500
491
 
 
492
        /**
 
493
         * @static
 
494
         * @access private
 
495
         * @var array
 
496
         */
501
497
        private static $back_compat_keys;
502
498
 
503
499
        /**
508
504
         * @since 2.0.0
509
505
         * @access public
510
506
         *
 
507
         * @global wpdb $wpdb
 
508
         *
511
509
         * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
512
510
         * @param string $name Optional. User's username
513
511
         * @param int $blog_id Optional Blog ID, defaults to current blog.
569
567
         *
570
568
         * @since 3.3.0
571
569
         *
 
570
         * @static
 
571
         *
 
572
         * @global wpdb $wpdb
 
573
         *
572
574
         * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
573
575
         * @param string|int $value The field value
574
576
         * @return object|false Raw user object
629
631
        }
630
632
 
631
633
        /**
 
634
         * Makes private/protected methods readable for backwards compatibility.
 
635
         *
 
636
         * @since 4.3.0
 
637
         * @access public
 
638
         *
 
639
         * @param callable $name      Method to call.
 
640
         * @param array    $arguments Arguments to pass when calling.
 
641
         * @return mixed|false Return value of the callback, false otherwise.
 
642
         */
 
643
        public function __call( $name, $arguments ) {
 
644
                if ( '_init_caps' === $name ) {
 
645
                        return call_user_func_array( array( $this, $name ), $arguments );
 
646
                }
 
647
                return false;
 
648
        }
 
649
 
 
650
        /**
632
651
         * Magic method for checking the existence of a certain custom field
633
652
         *
634
653
         * @since 3.3.0
713
732
         * @since 3.3.0
714
733
         *
715
734
         * @param string $key Property
 
735
         * @return mixed
716
736
         */
717
737
        public function get( $key ) {
718
738
                return $this->__get( $key );
726
746
         * @since 3.3.0
727
747
         *
728
748
         * @param string $key Property
 
749
         * @return bool
729
750
         */
730
751
        public function has_prop( $key ) {
731
752
                return $this->__isset( $key );
753
774
         * @access protected
754
775
         * @since 2.1.0
755
776
         *
 
777
         * @global wpdb $wpdb
 
778
         *
756
779
         * @param string $cap_key Optional capability key
757
780
         */
758
 
        function _init_caps( $cap_key = '' ) {
 
781
        protected function _init_caps( $cap_key = '' ) {
759
782
                global $wpdb;
760
783
 
761
784
                if ( empty($cap_key) )
780
803
         * granted permission to.
781
804
         *
782
805
         * @since 2.0.0
783
 
         * @uses $wp_roles
784
806
         * @access public
785
807
         *
786
808
         * @return array List of all capabilities for the user.
787
809
         */
788
810
        public function get_role_caps() {
789
 
                global $wp_roles;
790
 
 
791
 
                if ( ! isset( $wp_roles ) )
792
 
                        $wp_roles = new WP_Roles();
 
811
                $wp_roles = wp_roles();
793
812
 
794
813
                //Filter out caps that are not role names and assign to $this->roles
795
814
                if ( is_array( $this->caps ) )
821
840
                update_user_meta( $this->ID, $this->cap_key, $this->caps );
822
841
                $this->get_role_caps();
823
842
                $this->update_user_level_from_caps();
 
843
 
 
844
                /**
 
845
                 * Fires immediately after the user has been given a new role.
 
846
                 *
 
847
                 * @since 4.3.0
 
848
                 *
 
849
                 * @param int    $user_id The user ID.
 
850
                 * @param string $role    The new role.
 
851
                 */
 
852
                do_action( 'add_user_role', $this->ID, $role );
824
853
        }
825
854
 
826
855
        /**
838
867
                update_user_meta( $this->ID, $this->cap_key, $this->caps );
839
868
                $this->get_role_caps();
840
869
                $this->update_user_level_from_caps();
 
870
 
 
871
                /**
 
872
                 * Fires immediately after a role as been removed from a user.
 
873
                 *
 
874
                 * @since 4.3.0
 
875
                 *
 
876
                 * @param int    $user_id The user ID.
 
877
                 * @param string $role    The removed role.
 
878
                 */
 
879
                do_action( 'remove_user_role', $this->ID, $role );
841
880
        }
842
881
 
843
882
        /**
920
959
         *
921
960
         * @since 2.0.0
922
961
         * @access public
 
962
         *
 
963
         * @global wpdb $wpdb
923
964
         */
924
965
        public function update_user_level_from_caps() {
925
966
                global $wpdb;
966
1007
         *
967
1008
         * @since 2.1.0
968
1009
         * @access public
 
1010
         *
 
1011
         * @global wpdb $wpdb
969
1012
         */
970
1013
        public function remove_all_caps() {
971
1014
                global $wpdb;
1047
1090
         *
1048
1091
         * @since 3.0.0
1049
1092
         *
 
1093
         * @global wpdb $wpdb
 
1094
         *
1050
1095
         * @param int $blog_id Optional Blog ID, defaults to current blog.
1051
1096
         */
1052
1097
        public function for_blog( $blog_id = '' ) {
1260
1305
                if ( empty( $comment ) )
1261
1306
                        break;
1262
1307
                $post = get_post( $comment->comment_post_ID );
1263
 
                $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
 
1308
 
 
1309
                /*
 
1310
                 * If the post doesn't exist, we have an orphaned comment.
 
1311
                 * Fall back to the edit_posts capability, instead.
 
1312
                 */
 
1313
                if ( $post ) {
 
1314
                        $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
 
1315
                } else {
 
1316
                        $caps = map_meta_cap( 'edit_posts', $user_id );
 
1317
                }
1264
1318
                break;
1265
1319
        case 'unfiltered_upload':
1266
1320
                if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) )  )
1333
1387
        case 'create_users':
1334
1388
                if ( !is_multisite() )
1335
1389
                        $caps[] = $cap;
1336
 
                elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
 
1390
                elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) )
1337
1391
                        $caps[] = $cap;
1338
1392
                else
1339
1393
                        $caps[] = 'do_not_allow';
1475
1529
}
1476
1530
 
1477
1531
/**
 
1532
 * Retrieves the global WP_Roles instance and instantiates it if necessary.
 
1533
 *
 
1534
 * @since 4.3.0
 
1535
 *
 
1536
 * @global WP_Roles $wp_roles WP_Roles global instance.
 
1537
 *
 
1538
 * @return WP_Roles WP_Roles global instance if not already instantiated.
 
1539
 */
 
1540
function wp_roles() {
 
1541
        global $wp_roles;
 
1542
 
 
1543
        if ( ! isset( $wp_roles ) ) {
 
1544
                $wp_roles = new WP_Roles();
 
1545
        }
 
1546
        return $wp_roles;
 
1547
}
 
1548
 
 
1549
/**
1478
1550
 * Retrieve role object.
1479
1551
 *
1480
 
 * @see WP_Roles::get_role() Uses method to retrieve role object.
1481
1552
 * @since 2.0.0
1482
1553
 *
1483
1554
 * @param string $role Role name.
1484
1555
 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
1485
1556
 */
1486
1557
function get_role( $role ) {
1487
 
        global $wp_roles;
1488
 
 
1489
 
        if ( ! isset( $wp_roles ) )
1490
 
                $wp_roles = new WP_Roles();
1491
 
 
1492
 
        return $wp_roles->get_role( $role );
 
1558
        return wp_roles()->get_role( $role );
1493
1559
}
1494
1560
 
1495
1561
/**
1496
1562
 * Add role, if it does not exist.
1497
1563
 *
1498
 
 * @see WP_Roles::add_role() Uses method to add role.
1499
1564
 * @since 2.0.0
1500
1565
 *
1501
1566
 * @param string $role Role name.
1504
1569
 * @return WP_Role|null WP_Role object if role is added, null if already exists.
1505
1570
 */
1506
1571
function add_role( $role, $display_name, $capabilities = array() ) {
1507
 
        global $wp_roles;
1508
 
 
1509
 
        if ( ! isset( $wp_roles ) )
1510
 
                $wp_roles = new WP_Roles();
1511
 
 
1512
 
        return $wp_roles->add_role( $role, $display_name, $capabilities );
 
1572
        return wp_roles()->add_role( $role, $display_name, $capabilities );
1513
1573
}
1514
1574
 
1515
1575
/**
1516
1576
 * Remove role, if it exists.
1517
1577
 *
1518
 
 * @see WP_Roles::remove_role() Uses method to remove role.
1519
1578
 * @since 2.0.0
1520
1579
 *
1521
1580
 * @param string $role Role name.
1522
1581
 */
1523
1582
function remove_role( $role ) {
1524
 
        global $wp_roles;
1525
 
 
1526
 
        if ( ! isset( $wp_roles ) )
1527
 
                $wp_roles = new WP_Roles();
1528
 
 
1529
 
        $wp_roles->remove_role( $role );
 
1583
        wp_roles()->remove_role( $role );
1530
1584
}
1531
1585
 
1532
1586
/**
1534
1588
 *
1535
1589
 * @since 3.0.0
1536
1590
 *
1537
 
 * @uses $super_admins Super admins global variable, if set.
 
1591
 * @global array $super_admins
1538
1592
 *
1539
1593
 * @return array List of super admin logins
1540
1594
 */