~canonical-sysadmins/wordpress/4.8.1

« back to all changes in this revision

Viewing changes to wp-includes/ms-blogs.php

  • Committer: Barry Price
  • Date: 2016-08-17 04:50:12 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: barry.price@canonical.com-20160817045012-qfui81zhqnqv2ba9
Merge WP4.6 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
235
235
        restore_current_blog();
236
236
 
237
237
        /**
238
 
         * Filter a blog's details.
 
238
         * Filters a blog's details.
239
239
         *
240
240
         * @since MU
241
241
         *
447
447
        $domain_path_key = md5( $blog->domain . $blog->path );
448
448
 
449
449
        wp_cache_delete( $blog_id, 'sites' );
 
450
        wp_cache_delete( $blog_id, 'site-details' );
450
451
        wp_cache_delete( $blog_id , 'blog-details' );
451
452
        wp_cache_delete( $blog_id . 'short' , 'blog-details' );
452
453
        wp_cache_delete(  $domain_path_key, 'blog-lookup' );
454
455
        wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
455
456
        wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
456
457
        wp_cache_delete( $domain_path_key, 'blog-id-cache' );
 
458
 
 
459
        /**
 
460
         * Fires immediately after a site has been removed from the object cache.
 
461
         *
 
462
         * @since 4.6.0
 
463
         *
 
464
         * @param int     $id              Blog ID.
 
465
         * @param WP_Site $blog            Site object.
 
466
         * @param string  $domain_path_key md5 hash of domain and path.
 
467
         */
 
468
        do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
 
469
 
 
470
        wp_cache_set( 'last_changed', microtime(), 'sites' );
 
471
}
 
472
 
 
473
/**
 
474
 * Retrieves site data given a site ID or site object.
 
475
 *
 
476
 * Site data will be cached and returned after being passed through a filter.
 
477
 * If the provided site is empty, the current site global will be used.
 
478
 *
 
479
 * @since 4.6.0
 
480
 *
 
481
 * @param WP_Site|int|null $site Optional. Site to retrieve. Default is the current site.
 
482
 * @return WP_Site|null The site object or null if not found.
 
483
 */
 
484
function get_site( $site = null ) {
 
485
        if ( empty( $site ) ) {
 
486
                $site = get_current_blog_id();
 
487
        }
 
488
 
 
489
        if ( $site instanceof WP_Site ) {
 
490
                $_site = $site;
 
491
        } elseif ( is_object( $site ) ) {
 
492
                $_site = new WP_Site( $site );
 
493
        } else {
 
494
                $_site = WP_Site::get_instance( $site );
 
495
        }
 
496
 
 
497
        if ( ! $_site ) {
 
498
                return null;
 
499
        }
 
500
 
 
501
        /**
 
502
         * Fires after a site is retrieved.
 
503
         *
 
504
         * @since 4.6.0
 
505
         *
 
506
         * @param WP_Site $_site Site data.
 
507
         */
 
508
        $_site = apply_filters( 'get_site', $_site );
 
509
 
 
510
        return $_site;
 
511
}
 
512
 
 
513
/**
 
514
 * Adds any sites from the given ids to the cache that do not already exist in cache.
 
515
 *
 
516
 * @since 4.6.0
 
517
 * @access private
 
518
 *
 
519
 * @see update_site_cache()
 
520
 * @global wpdb $wpdb WordPress database abstraction object.
 
521
 *
 
522
 * @param array $ids ID list.
 
523
 */
 
524
function _prime_site_caches( $ids ) {
 
525
        global $wpdb;
 
526
 
 
527
        $non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
 
528
        if ( ! empty( $non_cached_ids ) ) {
 
529
                $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
 
530
 
 
531
                update_site_cache( $fresh_sites );
 
532
        }
 
533
}
 
534
 
 
535
/**
 
536
 * Updates sites in cache.
 
537
 *
 
538
 * @since 4.6.0
 
539
 *
 
540
 * @param array $sites Array of site objects.
 
541
 */
 
542
function update_site_cache( $sites ) {
 
543
        if ( ! $sites ) {
 
544
                return;
 
545
        }
 
546
 
 
547
        foreach ( $sites as $site ) {
 
548
                wp_cache_add( $site->blog_id, $site, 'sites' );
 
549
                wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' );
 
550
        }
 
551
}
 
552
 
 
553
/**
 
554
 * Retrieves a list of sites matching requested arguments.
 
555
 *
 
556
 * @since 4.6.0
 
557
 *
 
558
 * @see WP_Site_Query::parse_query()
 
559
 *
 
560
 * @param string|array $args {
 
561
 *     Optional. Array or query string of site query parameters. Default empty.
 
562
 *
 
563
 *     @type array        $site__in          Array of site IDs to include. Default empty.
 
564
 *     @type array        $site__not_in      Array of site IDs to exclude. Default empty.
 
565
 *     @type bool         $count             Whether to return a site count (true) or array of site objects.
 
566
 *                                           Default false.
 
567
 *     @type array        $date_query        Date query clauses to limit sites by. See WP_Date_Query.
 
568
 *                                           Default null.
 
569
 *     @type string       $fields            Site fields to return. Accepts 'ids' for site IDs only or empty
 
570
 *                                           for all fields. Default empty.
 
571
 *     @type int          $ID                A site ID to only return that site. Default empty.
 
572
 *     @type int          $number            Maximum number of sites to retrieve. Default null (no limit).
 
573
 *     @type int          $offset            Number of sites to offset the query. Used to build LIMIT clause.
 
574
 *                                           Default 0.
 
575
 *     @type bool         $no_found_rows     Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
 
576
 *     @type string|array $orderby           Site status or array of statuses. Accepts 'id', 'domain', 'path',
 
577
 *                                           'network_id', 'last_updated', 'registered', 'domain_length',
 
578
 *                                           'path_length', 'site__in' and 'network__in'. Also accepts false,
 
579
 *                                           an empty array, or 'none' to disable `ORDER BY` clause.
 
580
 *                                           Default 'id'.
 
581
 *     @type string       $order             How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'.
 
582
 *     @type int          $network_id        Limit results to those affiliated with a given network ID.
 
583
 *                                           Default current network ID.
 
584
 *     @type array        $network__in       Array of network IDs to include affiliated sites for. Default empty.
 
585
 *     @type array        $network__not_in   Array of network IDs to exclude affiliated sites for. Default empty.
 
586
 *     @type string       $domain            Limit results to those affiliated with a given domain.
 
587
 *                                           Default empty.
 
588
 *     @type array        $domain__in        Array of domains to include affiliated sites for. Default empty.
 
589
 *     @type array        $domain__not_in    Array of domains to exclude affiliated sites for. Default empty.
 
590
 *     @type string       $path              Limit results to those affiliated with a given path.
 
591
 *                                           Default empty.
 
592
 *     @type array        $path__in          Array of paths to include affiliated sites for. Default empty.
 
593
 *     @type array        $path__not_in      Array of paths to exclude affiliated sites for. Default empty.
 
594
 *     @type int          $public            Limit results to public sites. Accepts '1' or '0'. Default empty.
 
595
 *     @type int          $archived          Limit results to archived sites. Accepts '1' or '0'. Default empty.
 
596
 *     @type int          $mature            Limit results to mature sites. Accepts '1' or '0'. Default empty.
 
597
 *     @type int          $spam              Limit results to spam sites. Accepts '1' or '0'. Default empty.
 
598
 *     @type int          $deleted           Limit results to deleted sites. Accepts '1' or '0'. Default empty.
 
599
 *     @type string       $search            Search term(s) to retrieve matching sites for. Default empty.
 
600
 *     @type bool         $update_site_cache Whether to prime the cache for found sites. Default false.
 
601
 * }
 
602
 * @return array List of sites.
 
603
 */
 
604
function get_sites( $args = array() ) {
 
605
        $query = new WP_Site_Query();
 
606
 
 
607
        return $query->query( $args );
457
608
}
458
609
 
459
610
/**
487
638
        restore_current_blog();
488
639
 
489
640
        /**
490
 
         * Filter a blog option value.
 
641
         * Filters a blog option value.
491
642
         *
492
643
         * The dynamic portion of the hook name, `$option`, refers to the blog option name.
493
644
         *
574
725
        $id = (int) $id;
575
726
 
576
727
        if ( null !== $deprecated  )
577
 
                _deprecated_argument( __FUNCTION__, '3.1' );
 
728
                _deprecated_argument( __FUNCTION__, '3.1.0' );
578
729
 
579
730
        if ( get_current_blog_id() == $id )
580
731
                return update_option( $option, $value );
660
811
                        if ( is_array( $global_groups ) ) {
661
812
                                wp_cache_add_global_groups( $global_groups );
662
813
                        } else {
663
 
                                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites' ) );
 
814
                                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
664
815
                        }
665
 
                        wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
 
816
                        wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
666
817
                }
667
818
        }
668
819
 
731
882
                        if ( is_array( $global_groups ) ) {
732
883
                                wp_cache_add_global_groups( $global_groups );
733
884
                        } else {
734
 
                                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites' ) );
 
885
                                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
735
886
                        }
736
 
                        wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
 
887
                        wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
737
888
                }
738
889
        }
739
890
 
808
959
        global $wpdb;
809
960
 
810
961
        if ( null !== $deprecated  )
811
 
                _deprecated_argument( __FUNCTION__, '3.1' );
 
962
                _deprecated_argument( __FUNCTION__, '3.1.0' );
812
963
 
813
964
        if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
814
965
                return $value;
910
1061
}
911
1062
 
912
1063
/**
 
1064
 * Retrieves a list of networks.
 
1065
 *
 
1066
 * @since 4.6.0
 
1067
 *
 
1068
 * @param string|array $args Optional. Array or string of arguments. See WP_Network_Query::parse_query()
 
1069
 *                           for information on accepted arguments. Default empty array.
 
1070
 * @return int|array List of networks or number of found networks if `$count` argument is true.
 
1071
 */
 
1072
function get_networks( $args = array() ) {
 
1073
        $query = new WP_Network_Query();
 
1074
 
 
1075
        return $query->query( $args );
 
1076
}
 
1077
 
 
1078
/**
 
1079
 * Retrieves network data given a network ID or network object.
 
1080
 *
 
1081
 * Network data will be cached and returned after being passed through a filter.
 
1082
 * If the provided network is empty, the current network global will be used.
 
1083
 *
 
1084
 * @since 4.6.0
 
1085
 *
 
1086
 * @global WP_Network $current_site
 
1087
 *
 
1088
 * @param WP_Network|int|null $network Optional. Network to retrieve. Default is the current network.
 
1089
 * @return WP_Network|null The network object or null if not found.
 
1090
 */
 
1091
function get_network( $network = null ) {
 
1092
        global $current_site;
 
1093
        if ( empty( $network ) && isset( $current_site ) ) {
 
1094
                $network = $current_site;
 
1095
        }
 
1096
 
 
1097
        if ( $network instanceof WP_Network ) {
 
1098
                $_network = $network;
 
1099
        } elseif ( is_object( $network ) ) {
 
1100
                $_network = new WP_Network( $network );
 
1101
        } else {
 
1102
                $_network = WP_Network::get_instance( $network );
 
1103
        }
 
1104
 
 
1105
        if ( ! $_network ) {
 
1106
                return null;
 
1107
        }
 
1108
 
 
1109
        /**
 
1110
         * Fires after a network is retrieved.
 
1111
         *
 
1112
         * @since 4.6.0
 
1113
         *
 
1114
         * @param WP_Network $_network Network data.
 
1115
         */
 
1116
        $_network = apply_filters( 'get_network', $_network );
 
1117
 
 
1118
        return $_network;
 
1119
}
 
1120
 
 
1121
/**
 
1122
 * Removes a network from the object cache.
 
1123
 *
 
1124
 * @since 4.6.0
 
1125
 *
 
1126
 * @param int|array $ids Network ID or an array of network IDs to remove from cache.
 
1127
 */
 
1128
function clean_network_cache( $ids ) {
 
1129
        foreach ( (array) $ids as $id ) {
 
1130
                wp_cache_delete( $id, 'networks' );
 
1131
 
 
1132
                /**
 
1133
                 * Fires immediately after a network has been removed from the object cache.
 
1134
                 *
 
1135
                 * @since 4.6.0
 
1136
                 *
 
1137
                 * @param int $id Network ID.
 
1138
                 */
 
1139
                do_action( 'clean_network_cache', $id );
 
1140
        }
 
1141
 
 
1142
        wp_cache_set( 'last_changed', microtime(), 'networks' );
 
1143
}
 
1144
 
 
1145
/**
 
1146
 * Updates the network cache of given networks.
 
1147
 *
 
1148
 * Will add the networks in $networks to the cache. If network ID already exists
 
1149
 * in the network cache then it will not be updated. The network is added to the
 
1150
 * cache using the network group with the key using the ID of the networks.
 
1151
 *
 
1152
 * @since 4.6.0
 
1153
 *
 
1154
 * @param array $networks Array of network row objects.
 
1155
 */
 
1156
function update_network_cache( $networks ) {
 
1157
        foreach ( (array) $networks as $network ) {
 
1158
                wp_cache_add( $network->id, $network, 'networks' );
 
1159
        }
 
1160
}
 
1161
 
 
1162
/**
 
1163
 * Adds any networks from the given IDs to the cache that do not already exist in cache.
 
1164
 *
 
1165
 * @since 4.6.0
 
1166
 * @access private
 
1167
 *
 
1168
 * @see update_network_cache()
 
1169
 * @global wpdb $wpdb WordPress database abstraction object.
 
1170
 *
 
1171
 * @param array $network_ids Array of network IDs.
 
1172
 */
 
1173
function _prime_network_caches( $network_ids ) {
 
1174
        global $wpdb;
 
1175
 
 
1176
        $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
 
1177
        if ( !empty( $non_cached_ids ) ) {
 
1178
                $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
 
1179
 
 
1180
                update_network_cache( $fresh_networks );
 
1181
        }
 
1182
}
 
1183
 
 
1184
/**
913
1185
 * Handler for updating the blog date when a post is published or an already published post is changed.
914
1186
 *
915
1187
 * @since 3.3.0