| 1 |
<?php |
| 2 |
/** |
| 3 |
* Blog (site) handling |
| 4 |
* |
| 5 |
* Handles all Blog (site) operations and detection. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace OPcacheManager\System; |
| 13 |
|
| 14 |
/** |
| 15 |
* Define the Blog (site) functionality. |
| 16 |
* |
| 17 |
* Handles all Blog (site) operations and detection. |
| 18 |
* |
| 19 |
* @package System |
| 20 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class Blog { |
| 24 |
|
| 25 |
/** |
| 26 |
* Initializes the class and set its properties. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get a blog name. |
| 35 |
* |
| 36 |
* @param integer $id Optional. The blog id. |
| 37 |
* @param string $default Optional. Default value to return if blog is not detected. |
| 38 |
* @return string The blog name if detected, $default otherwise. |
| 39 |
* @since 1.0.0 |
| 40 |
*/ |
| 41 |
public static function get_blog_name( $id = null, $default = 'unknown' ) { |
| 42 |
if ( $id && is_numeric( $id ) && $id > 0 && Environment::is_wordpress_multisite() ) { |
| 43 |
$blog_info = get_blog_details( $id ); |
| 44 |
if ( is_object( $blog_info ) ) { |
| 45 |
return $blog_info->blogname; |
| 46 |
} |
| 47 |
return '- deleted site -'; |
| 48 |
|
| 49 |
} elseif ( $id && is_numeric( $id ) && $id > 0 ) { |
| 50 |
return get_bloginfo( 'name' ); |
| 51 |
|
| 52 |
} else { |
| 53 |
return $default; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get a blog url. |
| 59 |
* |
| 60 |
* @param integer $id Optional. The blog id. |
| 61 |
* @param string $default Optional. Default value to return if blog is not detected. |
| 62 |
* @return string The blog url (without scheme) if detected, $default otherwise. |
| 63 |
* @since 1.0.0 |
| 64 |
*/ |
| 65 |
public static function get_blog_url( $id = null, $default = 'wordpress.org' ) { |
| 66 |
if ( $id && is_numeric( $id ) && $id > 0 && Environment::is_wordpress_multisite() ) { |
| 67 |
$url_parts = wp_parse_url( get_blog_option( $id, 'siteurl', 'https://wordpress.org' ) ); |
| 68 |
|
| 69 |
} elseif ( $id && is_numeric( $id ) && $id > 0 ) { |
| 70 |
$url_parts = wp_parse_url( get_option( 'siteurl', 'https://wordpress.org' ) ); |
| 71 |
$url_parts['path'] = ''; |
| 72 |
|
| 73 |
} else { |
| 74 |
return $default; |
| 75 |
} |
| 76 |
$site = ''; |
| 77 |
if ( array_key_exists( 'host', $url_parts ) && isset( $url_parts['host'] ) ) { |
| 78 |
$site .= $url_parts['host']; |
| 79 |
} |
| 80 |
if ( array_key_exists( 'path', $url_parts ) && isset( $url_parts['path'] ) ) { |
| 81 |
$site .= $url_parts['path']; |
| 82 |
} |
| 83 |
return $site; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Verify if a blog exist. |
| 88 |
* |
| 89 |
* @param integer $id The blog id. |
| 90 |
* @return boolean True if the blog exists, false otherwise. |
| 91 |
* @since 1.0.0 |
| 92 |
*/ |
| 93 |
public static function is_blog_exists( $id ) { |
| 94 |
$result = false; |
| 95 |
if ( $id && is_numeric( $id ) && $id > 0 && Environment::is_wordpress_multisite() ) { |
| 96 |
$blog_info = get_blog_details( $id ); |
| 97 |
if ( is_object( $blog_info ) ) { |
| 98 |
$result = $id === $blog_info->id; |
| 99 |
} |
| 100 |
} |
| 101 |
return $result; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get a fully qualified blog name. |
| 106 |
* |
| 107 |
* @param mixed $id Optional. The blog id. |
| 108 |
* @return string The blog name if detected, $default otherwise. |
| 109 |
* @since 1.3.0 |
| 110 |
*/ |
| 111 |
public static function get_full_blog_name( $id = 0 ) { |
| 112 |
if ( is_numeric( $id ) ) { |
| 113 |
return sprintf( '"%s" (site ID %s)', self::get_blog_name( $id ), $id ); |
| 114 |
} |
| 115 |
if ( $id instanceof \WP_Site ) { |
| 116 |
return sprintf( '"%s" (site ID %s)', (string) $id->blogname, $id->id ); |
| 117 |
} |
| 118 |
return 'unknow site'; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get the current blog id. |
| 123 |
* |
| 124 |
* @param mixed $default Optional. Default value to return if blog is not detected. |
| 125 |
* @return mixed|integer The blog id if detected, null otherwise. |
| 126 |
* @since 1.0.0 |
| 127 |
*/ |
| 128 |
public static function get_current_blog_id( $default = null ) { |
| 129 |
$blog_id = $default; |
| 130 |
$id = get_current_blog_id(); |
| 131 |
if ( $id && is_numeric( $id ) && $id > 0 ) { |
| 132 |
$blog_id = $id; |
| 133 |
} |
| 134 |
return $blog_id; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get the current blog name. |
| 139 |
* |
| 140 |
* @param string $default Optional. Default value to return if blog is not detected. |
| 141 |
* @return string The current blog name if detected, "anonymous" otherwise. |
| 142 |
* @since 1.0.0 |
| 143 |
*/ |
| 144 |
public static function get_current_blog_name( $default = 'unknown' ) { |
| 145 |
return self::get_blog_name( self::get_current_blog_id(), $default ); |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|