home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / class-wp-site.php < prev    next >
Encoding:
PHP Script  |  2017-10-16  |  7.1 KB  |  348 lines

  1. <?php
  2. /**
  3.  * Site API: WP_Site class
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Multisite
  7.  * @since 4.5.0
  8.  */
  9.  
  10. /**
  11.  * Core class used for interacting with a multisite site.
  12.  *
  13.  * This class is used during load to populate the `$current_blog` global and
  14.  * setup the current site.
  15.  *
  16.  * @since 4.5.0
  17.  *
  18.  * @property int    $id
  19.  * @property int    $network_id
  20.  * @property string $blogname
  21.  * @property string $siteurl
  22.  * @property int    $post_count
  23.  * @property string $home
  24.  */
  25. final class WP_Site {
  26.  
  27.     /**
  28.      * Site ID.
  29.      *
  30.      * A numeric string, for compatibility reasons.
  31.      *
  32.      * @since 4.5.0
  33.      * @var string
  34.      */
  35.     public $blog_id;
  36.  
  37.     /**
  38.      * Domain of the site.
  39.      *
  40.      * @since 4.5.0
  41.      * @var string
  42.      */
  43.     public $domain = '';
  44.  
  45.     /**
  46.      * Path of the site.
  47.      *
  48.      * @since 4.5.0
  49.      * @var string
  50.      */
  51.     public $path = '';
  52.  
  53.     /**
  54.      * The ID of the site's parent network.
  55.      *
  56.      * Named "site" vs. "network" for legacy reasons. An individual site's "site" is
  57.      * its network.
  58.      *
  59.      * A numeric string, for compatibility reasons.
  60.      *
  61.      * @since 4.5.0
  62.      * @var string
  63.      */
  64.     public $site_id = '0';
  65.  
  66.     /**
  67.      * The date on which the site was created or registered.
  68.      *
  69.      * @since 4.5.0
  70.      * @var string Date in MySQL's datetime format.
  71.      */
  72.     public $registered = '0000-00-00 00:00:00';
  73.  
  74.     /**
  75.      * The date and time on which site settings were last updated.
  76.      *
  77.      * @since 4.5.0
  78.      * @var string Date in MySQL's datetime format.
  79.      */
  80.     public $last_updated = '0000-00-00 00:00:00';
  81.  
  82.     /**
  83.      * Whether the site should be treated as public.
  84.      *
  85.      * A numeric string, for compatibility reasons.
  86.      *
  87.      * @since 4.5.0
  88.      * @var string
  89.      */
  90.     public $public = '1';
  91.  
  92.     /**
  93.      * Whether the site should be treated as archived.
  94.      *
  95.      * A numeric string, for compatibility reasons.
  96.      *
  97.      * @since 4.5.0
  98.      * @var string
  99.      */
  100.     public $archived = '0';
  101.  
  102.     /**
  103.      * Whether the site should be treated as mature.
  104.      *
  105.      * Handling for this does not exist throughout WordPress core, but custom
  106.      * implementations exist that require the property to be present.
  107.      *
  108.      * A numeric string, for compatibility reasons.
  109.      *
  110.      * @since 4.5.0
  111.      * @var string
  112.      */
  113.     public $mature = '0';
  114.  
  115.     /**
  116.      * Whether the site should be treated as spam.
  117.      *
  118.      * A numeric string, for compatibility reasons.
  119.      *
  120.      * @since 4.5.0
  121.      * @var string
  122.      */
  123.     public $spam = '0';
  124.  
  125.     /**
  126.      * Whether the site should be treated as deleted.
  127.      *
  128.      * A numeric string, for compatibility reasons.
  129.      *
  130.      * @since 4.5.0
  131.      * @var string
  132.      */
  133.     public $deleted = '0';
  134.  
  135.     /**
  136.      * The language pack associated with this site.
  137.      *
  138.      * A numeric string, for compatibility reasons.
  139.      *
  140.      * @since 4.5.0
  141.      * @var string
  142.      */
  143.     public $lang_id = '0';
  144.  
  145.     /**
  146.      * Retrieves a site from the database by its ID.
  147.      *
  148.      * @static
  149.      * @since 4.5.0
  150.      *
  151.      * @global wpdb $wpdb WordPress database abstraction object.
  152.      *
  153.      * @param int $site_id The ID of the site to retrieve.
  154.      * @return WP_Site|false The site's object if found. False if not.
  155.      */
  156.     public static function get_instance( $site_id ) {
  157.         global $wpdb;
  158.  
  159.         $site_id = (int) $site_id;
  160.         if ( ! $site_id ) {
  161.             return false;
  162.         }
  163.  
  164.         $_site = wp_cache_get( $site_id, 'sites' );
  165.  
  166.         if ( ! $_site ) {
  167.             $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );
  168.  
  169.             if ( empty( $_site ) || is_wp_error( $_site ) ) {
  170.                 return false;
  171.             }
  172.  
  173.             wp_cache_add( $site_id, $_site, 'sites' );
  174.         }
  175.  
  176.         return new WP_Site( $_site );
  177.     }
  178.  
  179.     /**
  180.      * Creates a new WP_Site object.
  181.      *
  182.      * Will populate object properties from the object provided and assign other
  183.      * default properties based on that information.
  184.      *
  185.      * @since 4.5.0
  186.      *
  187.      * @param WP_Site|object $site A site object.
  188.      */
  189.     public function __construct( $site ) {
  190.         foreach( get_object_vars( $site ) as $key => $value ) {
  191.             $this->$key = $value;
  192.         }
  193.     }
  194.  
  195.     /**
  196.      * Converts an object to array.
  197.      *
  198.      * @since 4.6.0
  199.      *
  200.      * @return array Object as array.
  201.      */
  202.     public function to_array() {
  203.         return get_object_vars( $this );
  204.     }
  205.  
  206.     /**
  207.      * Getter.
  208.      *
  209.      * Allows current multisite naming conventions when getting properties.
  210.      * Allows access to extended site properties.
  211.      *
  212.      * @since 4.6.0
  213.      *
  214.      * @param string $key Property to get.
  215.      * @return mixed Value of the property. Null if not available.
  216.      */
  217.     public function __get( $key ) {
  218.         switch ( $key ) {
  219.             case 'id':
  220.                 return (int) $this->blog_id;
  221.             case 'network_id':
  222.                 return (int) $this->site_id;
  223.             case 'blogname':
  224.             case 'siteurl':
  225.             case 'post_count':
  226.             case 'home':
  227.             default: // Custom properties added by 'site_details' filter.
  228.                 if ( ! did_action( 'ms_loaded' ) ) {
  229.                     return null;
  230.                 }
  231.  
  232.                 $details = $this->get_details();
  233.                 if ( isset( $details->$key ) ) {
  234.                     return $details->$key;
  235.                 }
  236.         }
  237.  
  238.         return null;
  239.     }
  240.  
  241.     /**
  242.      * Isset-er.
  243.      *
  244.      * Allows current multisite naming conventions when checking for properties.
  245.      * Checks for extended site properties.
  246.      *
  247.      * @since 4.6.0
  248.      *
  249.      * @param string $key Property to check if set.
  250.      * @return bool Whether the property is set.
  251.      */
  252.     public function __isset( $key ) {
  253.         switch ( $key ) {
  254.             case 'id':
  255.             case 'network_id':
  256.                 return true;
  257.             case 'blogname':
  258.             case 'siteurl':
  259.             case 'post_count':
  260.             case 'home':
  261.                 if ( ! did_action( 'ms_loaded' ) ) {
  262.                     return false;
  263.                 }
  264.                 return true;
  265.             default: // Custom properties added by 'site_details' filter.
  266.                 if ( ! did_action( 'ms_loaded' ) ) {
  267.                     return false;
  268.                 }
  269.  
  270.                 $details = $this->get_details();
  271.                 if ( isset( $details->$key ) ) {
  272.                     return true;
  273.                 }
  274.         }
  275.  
  276.         return false;
  277.     }
  278.  
  279.     /**
  280.      * Setter.
  281.      *
  282.      * Allows current multisite naming conventions while setting properties.
  283.      *
  284.      * @since 4.6.0
  285.      *
  286.      * @param string $key   Property to set.
  287.      * @param mixed  $value Value to assign to the property.
  288.      */
  289.     public function __set( $key, $value ) {
  290.         switch ( $key ) {
  291.             case 'id':
  292.                 $this->blog_id = (string) $value;
  293.                 break;
  294.             case 'network_id':
  295.                 $this->site_id = (string) $value;
  296.                 break;
  297.             default:
  298.                 $this->$key = $value;
  299.         }
  300.     }
  301.  
  302.     /**
  303.      * Retrieves the details for this site.
  304.      *
  305.      * This method is used internally to lazy-load the extended properties of a site.
  306.      *
  307.      * @since 4.6.0
  308.      *
  309.      * @see WP_Site::__get()
  310.      *
  311.      * @return stdClass A raw site object with all details included.
  312.      */
  313.     private function get_details() {
  314.         $details = wp_cache_get( $this->blog_id, 'site-details' );
  315.  
  316.         if ( false === $details ) {
  317.  
  318.             switch_to_blog( $this->blog_id );
  319.             // Create a raw copy of the object for backwards compatibility with the filter below.
  320.             $details = new stdClass();
  321.             foreach ( get_object_vars( $this ) as $key => $value ) {
  322.                 $details->$key = $value;
  323.             }
  324.             $details->blogname   = get_option( 'blogname' );
  325.             $details->siteurl    = get_option( 'siteurl' );
  326.             $details->post_count = get_option( 'post_count' );
  327.             $details->home       = get_option( 'home' );
  328.             restore_current_blog();
  329.  
  330.             wp_cache_set( $this->blog_id, $details, 'site-details' );
  331.         }
  332.  
  333.         /** This filter is documented in wp-includes/ms-blogs.php */
  334.         $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
  335.  
  336.         /**
  337.          * Filters a site's extended properties.
  338.          *
  339.          * @since 4.6.0
  340.          *
  341.          * @param stdClass $details The site details.
  342.          */
  343.         $details = apply_filters( 'site_details', $details );
  344.  
  345.         return $details;
  346.     }
  347. }
  348.