| 1 |
<?php declare( strict_types=1 ); |
| 2 |
|
| 3 |
namespace lloc\Msls\Blog; |
| 4 |
|
| 5 |
use lloc\Msls\Admin\Icon; |
| 6 |
use lloc\Msls\Options\OptionsInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* Internal representation of a blog |
| 10 |
* |
| 11 |
* @property int $userblog_id |
| 12 |
* @package Msls |
| 13 |
*/ |
| 14 |
class Blog { |
| 15 |
|
| 16 |
const MSLS_GET_PERMALINK_HOOK = 'msls_blog_get_permalink'; |
| 17 |
|
| 18 |
const WP_ADMIN_BAR_SHOW_SITE_ICONS_HOOK = 'wp_admin_bar_show_site_icons'; |
| 19 |
|
| 20 |
/** |
| 21 |
* WordPress generates such an object |
| 22 |
* |
| 23 |
* @var \StdClass |
| 24 |
*/ |
| 25 |
private $obj; |
| 26 |
|
| 27 |
/** |
| 28 |
* Language-code e.g. "de_DE", or "en_US", or "it_IT" |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
private string $language = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* Description e.g. "Deutsch", or "English", or "Italiano" |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
private string $description; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor |
| 43 |
* |
| 44 |
* @param ?\StdClass $obj |
| 45 |
* @param string $description |
| 46 |
*/ |
| 47 |
public function __construct( $obj, $description ) { |
| 48 |
if ( is_object( $obj ) ) { |
| 49 |
$this->obj = $obj; |
| 50 |
$this->language = Collection::get_blog_language( $this->obj->userblog_id ); |
| 51 |
} |
| 52 |
|
| 53 |
$this->description = (string) $description; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Gets a member of the \StdClass-object by name |
| 58 |
* |
| 59 |
* The method return <em>null</em> if the requested member does not exists. |
| 60 |
* |
| 61 |
* @param string $key |
| 62 |
* |
| 63 |
* @return mixed|null |
| 64 |
*/ |
| 65 |
final public function __get( $key ) { |
| 66 |
return $this->obj->$key ?? null; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Gets the description stored in this object |
| 71 |
* |
| 72 |
* The method returns the stored language if the description is empty. |
| 73 |
* |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
public function get_description(): string { |
| 77 |
return empty( $this->description ) ? $this->get_language() : $this->description; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Gets a customized title for the blog |
| 82 |
* |
| 83 |
* @param string $icon_type |
| 84 |
* |
| 85 |
* @return string |
| 86 |
*/ |
| 87 |
public function get_title( string $icon_type = 'flag' ): string { |
| 88 |
$icon = ( new Icon( null ) )->set_language( $this->language )->set_icon_type( $icon_type ); |
| 89 |
|
| 90 |
return sprintf( |
| 91 |
'%1$s %2$s', |
| 92 |
$this->obj->blogname, |
| 93 |
'<span class="msls-icon-wrapper flag">' . $icon->get_icon() . '</span>' |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Gets the language stored in this object |
| 99 |
* |
| 100 |
* @param string $preset |
| 101 |
* |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
public function get_language( $preset = 'en_US' ) { |
| 105 |
return empty( $this->language ) ? $preset : $this->language; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Gets the alpha2-part of the language-code |
| 110 |
* |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public function get_alpha2() { |
| 114 |
$language = $this->get_language(); |
| 115 |
|
| 116 |
return substr( $language, 0, 2 ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @param OptionsInterface $options |
| 121 |
* |
| 122 |
* @return string|null |
| 123 |
*/ |
| 124 |
public function get_url( $options ) { |
| 125 |
if ( msls_blog_collection()->get_current_blog_id() === $this->obj->userblog_id ) { |
| 126 |
return $options->get_current_link(); |
| 127 |
} |
| 128 |
|
| 129 |
return $this->get_permalink( $options ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @param OptionsInterface $options |
| 134 |
* |
| 135 |
* @return ?string |
| 136 |
*/ |
| 137 |
protected function get_permalink( OptionsInterface $options ) { |
| 138 |
$url = null; |
| 139 |
|
| 140 |
$is_front_page = is_front_page(); |
| 141 |
$is_posts_page = ! $is_front_page && is_home(); |
| 142 |
|
| 143 |
switch_to_blog( $this->obj->userblog_id ); |
| 144 |
|
| 145 |
if ( $is_front_page || $options->has_value( $this->get_language() ) ) { |
| 146 |
$url = apply_filters( self::MSLS_GET_PERMALINK_HOOK, $options->get_permalink( $this->get_language() ), $this ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- constant value is already prefixed with "msls_". |
| 147 |
} elseif ( $is_posts_page ) { |
| 148 |
$page_for_posts = (int) get_option( 'page_for_posts' ); |
| 149 |
if ( $page_for_posts > 0 ) { |
| 150 |
$url = apply_filters( self::MSLS_GET_PERMALINK_HOOK, (string) get_permalink( $page_for_posts ), $this ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- constant value is already prefixed with "msls_". |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
restore_current_blog(); |
| 155 |
|
| 156 |
return $url; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Sort objects helper |
| 161 |
* |
| 162 |
* @param string $a |
| 163 |
* @param string $b |
| 164 |
* |
| 165 |
* @return int |
| 166 |
*/ |
| 167 |
public static function internal_cmp( $a, $b ) { |
| 168 |
if ( $a === $b ) { |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
return ( $a < $b ? ( - 1 ) : 1 ); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Sort objects by language |
| 177 |
* |
| 178 |
* @param Blog $a |
| 179 |
* @param Blog $b |
| 180 |
* |
| 181 |
* @return int |
| 182 |
*/ |
| 183 |
public static function language( Blog $a, Blog $b ) { |
| 184 |
return self::internal_cmp( $a->get_language(), $b->get_language() ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Sort objects by description |
| 189 |
* |
| 190 |
* @param Blog $a |
| 191 |
* @param Blog $b |
| 192 |
* |
| 193 |
* @return int |
| 194 |
*/ |
| 195 |
public static function description( Blog $a, Blog $b ) { |
| 196 |
return self::internal_cmp( $a->get_description(), $b->get_description() ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* @return string |
| 201 |
*/ |
| 202 |
public function get_blavatar(): string { |
| 203 |
$blavatar_html = '<div class="blavatar"></div>'; |
| 204 |
$show_site_icons = apply_filters( self::WP_ADMIN_BAR_SHOW_SITE_ICONS_HOOK, true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- intentionally reuses WordPress core's own "wp_admin_bar_show_site_icons" hook name, not a plugin-specific hook. |
| 205 |
|
| 206 |
switch_to_blog( $this->obj->userblog_id ); |
| 207 |
|
| 208 |
if ( true === $show_site_icons && has_site_icon( $this->obj->userblog_id ) ) { |
| 209 |
$blavatar_html = sprintf( |
| 210 |
'<img class="blavatar" src="%s" srcset="%s 2x" alt="" width="16" height="16"%s />', |
| 211 |
esc_url( get_site_icon_url( 16 ) ), |
| 212 |
esc_url( get_site_icon_url( 32 ) ), |
| 213 |
( wp_lazy_loading_enabled( 'img', 'site_icon_in_toolbar' ) ? ' loading="lazy"' : '' ) |
| 214 |
); |
| 215 |
} |
| 216 |
|
| 217 |
restore_current_blog(); |
| 218 |
|
| 219 |
return $blavatar_html; |
| 220 |
} |
| 221 |
} |
| 222 |
|