Assets.php
3 weeks ago
Cache.php
3 weeks ago
Date.php
3 weeks ago
Debug.php
3 weeks ago
FeedReader.php
3 weeks ago
HTML.php
3 weeks ago
Helper.php
3 weeks ago
JSON.php
3 weeks ago
Nonce.php
3 weeks ago
Notice.php
3 weeks ago
Number.php
3 weeks ago
NumberConverter.php
3 weeks ago
Param.php
3 weeks ago
Sanitizing.php
3 weeks ago
Strip.php
3 weeks ago
Templates.php
3 weeks ago
User.php
3 weeks ago
Validating.php
3 weeks ago
WooCommerce.php
3 weeks ago
WordPress.php
3 weeks ago
WordPress.php
290 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPParsidate\Helper; |
| 4 | |
| 5 | class WordPress { |
| 6 | public static function debug(): bool { |
| 7 | return WP_DEBUG || wp_is_development_mode( 'plugin' ); |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * Get current page name |
| 12 | * |
| 13 | * @return string Page name, Return empty string if isn't standard WP pages. |
| 14 | */ |
| 15 | public static function getPageName(): string { |
| 16 | if ( self::isHome() ) { |
| 17 | return 'home'; |
| 18 | } elseif ( self::isBlog() ) { |
| 19 | return 'blog'; |
| 20 | } elseif ( self::isCategory() ) { |
| 21 | return 'category'; |
| 22 | } elseif ( self::isTag() ) { |
| 23 | return 'tag'; |
| 24 | } elseif ( self::isSinglePost() ) { |
| 25 | return 'post'; |
| 26 | } elseif ( self::isSingle() ) { |
| 27 | return 'single'; |
| 28 | } elseif ( self::isPage() ) { |
| 29 | return 'page'; |
| 30 | } elseif ( self::isSingular() ) { |
| 31 | return 'singular'; |
| 32 | } elseif ( self::is404() ) { |
| 33 | return '404'; |
| 34 | } |
| 35 | |
| 36 | return ''; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Retrieves the name of the current action hook. |
| 42 | * |
| 43 | * @return string|false Hook name of the current action, false if no action is running. |
| 44 | * @since 6.0 |
| 45 | * |
| 46 | */ |
| 47 | public static function getCurrentAction(): string { |
| 48 | return current_action(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Check action |
| 53 | * |
| 54 | * @param $actions |
| 55 | * |
| 56 | * @return bool |
| 57 | */ |
| 58 | public static function isAction( $actions ): bool { |
| 59 | $currentAction = self::getCurrentAction(); |
| 60 | |
| 61 | if ( is_array( $actions ) ) { |
| 62 | return in_array( $currentAction, $actions, true ); |
| 63 | } |
| 64 | |
| 65 | return $currentAction === $actions; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Determines whether the current locale is right-to-left (RTL). |
| 70 | * @return bool Whether locale is RTL. |
| 71 | * |
| 72 | * @since 6.0 |
| 73 | */ |
| 74 | public static function isRTL(): bool { |
| 75 | return is_rtl(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Determines whether the current visitor is a logged in user. |
| 80 | * |
| 81 | * @return bool True if user is logged in, false if not logged in. |
| 82 | */ |
| 83 | public static function isUserLoggedIn(): bool { |
| 84 | return is_user_logged_in(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Gets the current user's ID. |
| 89 | * |
| 90 | * @return int The current user's ID, or 0 if no user is logged in. |
| 91 | */ |
| 92 | public static function getCurrentUserID(): int { |
| 93 | return get_current_user_id(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Retrieves information about the current site. |
| 98 | * |
| 99 | * @param string $show Optional. Site info to retrieve. Default empty (site name). |
| 100 | * @param string $filter Optional. How to filter what is retrieved. Default 'raw'. |
| 101 | * |
| 102 | * @return string Mostly string values, might be empty. |
| 103 | */ |
| 104 | public static function blogInfo( $show = '', $filter = 'raw' ) { |
| 105 | return get_bloginfo( $show, $filter ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Determines whether the current request is a WordPress Ajax request. |
| 110 | * |
| 111 | * @return bool True if it's a WordPress Ajax request, false otherwise. |
| 112 | * @since 6.0 |
| 113 | * |
| 114 | */ |
| 115 | public static function isAjax(): bool { |
| 116 | return wp_doing_ajax(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Check current page is Home page |
| 121 | * |
| 122 | * @return bool |
| 123 | */ |
| 124 | public static function isHome(): bool { |
| 125 | return ( is_front_page() && is_home() ) || is_front_page(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Check current page is Blog page |
| 130 | * |
| 131 | * @return bool |
| 132 | */ |
| 133 | public static function isBlog(): bool { |
| 134 | return ! is_front_page() && is_home(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Check current page is Single Post page |
| 139 | * |
| 140 | * @return bool True, if is current page is Single post page |
| 141 | */ |
| 142 | public static function isSinglePost(): bool { |
| 143 | return is_singular( 'post' ); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * Determines whether the query is for an existing single post. |
| 149 | * |
| 150 | * @param int|string|int[]|string[] $post Optional. Post ID, title, slug, or array of such |
| 151 | * * to check against. Default empty. |
| 152 | * * @return bool Whether the query is for an existing single post. |
| 153 | */ |
| 154 | public static function isSingle( $post = '' ): bool { |
| 155 | return is_single( $post ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Determines whether the query is for an existing single post of any post type |
| 160 | * * (post, attachment, page, custom post types). |
| 161 | * |
| 162 | * @param string|string[] $postTypes Optional. Post type or array of post types |
| 163 | * * to check against. Default empty. |
| 164 | * * @return bool Whether the query is for an existing single post |
| 165 | * * or any of the given post types. |
| 166 | */ |
| 167 | public static function isSingular( $postTypes = '' ): bool { |
| 168 | return is_singular( $postTypes ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Determines whether the query has resulted in a 404 (returns no results). |
| 173 | * |
| 174 | * @return bool Whether the query is a 404 error. |
| 175 | */ |
| 176 | public static function is404(): bool { |
| 177 | return is_404(); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Determines whether the query is for an existing single page. |
| 182 | * |
| 183 | * @param int|string|int[]|string[] $page Optional. Page ID, title, slug, or array of such |
| 184 | * to check against. Default empty. |
| 185 | * |
| 186 | * @return bool Whether the query is for an existing single page. |
| 187 | */ |
| 188 | public static function isPage( $page = '' ): bool { |
| 189 | return is_page( $page ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Determines whether the query is for an existing category archive page. |
| 194 | * |
| 195 | * @param int|string|int[]|string[] $category Optional. Category ID, name, slug, or array of such |
| 196 | * * to check against. Default empty. |
| 197 | * * @return bool Whether the query is for an existing category archive page. |
| 198 | */ |
| 199 | public static function isCategory( $category = '' ): bool { |
| 200 | return is_category( $category ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Determines whether the query is for an existing tag archive page. |
| 205 | * |
| 206 | * @param int|string|int[]|string[] $tag Optional. Tag ID, name, slug, or array of such |
| 207 | * * to check against. Default empty. |
| 208 | * * @return bool Whether the query is for an existing tag archive page. |
| 209 | */ |
| 210 | public static function isTag( $tag = '' ): bool { |
| 211 | return is_tag( $tag ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Detects current page is feed or not |
| 216 | * |
| 217 | * @return bool True when page is feed, false when page isn't feed |
| 218 | * @since 1.0 |
| 219 | */ |
| 220 | public static function isFeed(): bool { |
| 221 | global $wp_query; |
| 222 | |
| 223 | if ( ! isset( $wp_query ) ) { |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | if ( $wp_query->is_feed() ) { |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | $path = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) ); |
| 232 | $ext = pathinfo( $path, PATHINFO_EXTENSION ); |
| 233 | |
| 234 | return in_array( $ext, array( 'xml', 'gz', 'xsl' ) ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Checks is WordPress sitemap |
| 239 | * |
| 240 | * @return bool |
| 241 | */ |
| 242 | public static function isSitemap(): bool { |
| 243 | $path = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) ); |
| 244 | |
| 245 | return ! empty( $path ) and strpos( $path, 'wp-sitemap' ) !== false; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Converts a plugin filepath to a slug. |
| 250 | * |
| 251 | * @param string $pluginFile The plugin's filepath, relative to the plugins directory. |
| 252 | * |
| 253 | * @return string The plugin's slug. |
| 254 | */ |
| 255 | public static function pluginPathToSlug( string $pluginFile ): string { |
| 256 | if ( 'hello.php' === $pluginFile ) { |
| 257 | return 'hello-dolly'; |
| 258 | } |
| 259 | |
| 260 | return str_contains( $pluginFile, '/' ) ? dirname( $pluginFile ) : str_replace( '.php', '', $pluginFile ); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Determines whether a plugin is active. |
| 265 | * |
| 266 | * Since 5.0.1 |
| 267 | * |
| 268 | * @param string $plugin Path to the plugin file relative to the plugins' directory. |
| 269 | * |
| 270 | * @return bool True, if in the active plugins list. False, not in the list. |
| 271 | */ |
| 272 | public static function isPluginActivated( string $plugin ): bool { |
| 273 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 274 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 275 | } |
| 276 | |
| 277 | return is_plugin_active( $plugin ); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Checks WPML or PolyLang plugins is active |
| 282 | * |
| 283 | * Since 4.0.1 |
| 284 | * @return bool True, if Multilingual plugins is active. |
| 285 | */ |
| 286 | public static function isMultilingualActive(): bool { |
| 287 | return self::isPluginActivated( 'polylang/polylang.php' ) || self::isPluginActivated( 'sitepress-multilingual-cms/sitepress.php' ); |
| 288 | } |
| 289 | } |
| 290 |