class-conditional.php
2 years ago
class-data.php
2 years ago
class-groups.php
2 years ago
class-str.php
2 years ago
class-wordpress.php
2 years ago
index.php
2 years ago
class-wordpress.php
138 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The class provides utility functions related to WordPress. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.47.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Utilities; |
| 11 | |
| 12 | use AdvancedAds\Framework\Utilities\Params; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Utilities WordPress. |
| 18 | */ |
| 19 | class WordPress { |
| 20 | |
| 21 | /** |
| 22 | * Get the current action selected from the bulk actions dropdown. |
| 23 | * |
| 24 | * @return string|false The action name or False if no action was selected |
| 25 | */ |
| 26 | public static function current_action() { |
| 27 | $action = Params::request( 'action' ); |
| 28 | if ( '-1' !== $action ) { |
| 29 | return sanitize_key( $action ); |
| 30 | } |
| 31 | |
| 32 | $action = Params::request( 'action2' ); |
| 33 | if ( '-1' !== $action ) { |
| 34 | return sanitize_key( $action ); |
| 35 | } |
| 36 | |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns whether the current user has the specified capability. |
| 42 | * |
| 43 | * @param string $capability Capability name. |
| 44 | * |
| 45 | * @return bool |
| 46 | */ |
| 47 | public static function user_can( $capability = 'manage_options' ): bool { |
| 48 | // Admins can do everything. |
| 49 | if ( current_user_can( 'manage_options' ) ) { |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | return current_user_can( |
| 54 | apply_filters( 'advanced-ads-capability', $capability ) |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns the capability needed to perform an action |
| 60 | * |
| 61 | * @param string $capability A capability to check, can be internal to Advanced Ads. |
| 62 | * |
| 63 | * @return string |
| 64 | */ |
| 65 | public static function user_cap( $capability = 'manage_options' ) { |
| 66 | // Admins can do everything. |
| 67 | if ( current_user_can( 'manage_options' ) ) { |
| 68 | return 'manage_options'; |
| 69 | } |
| 70 | |
| 71 | return apply_filters( 'advanced-ads-capability', $capability ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get site domain |
| 76 | * |
| 77 | * @param string $part Part of domain. |
| 78 | * |
| 79 | * @return string |
| 80 | */ |
| 81 | public static function get_site_domain( $part = 'host' ): string { |
| 82 | $domain = wp_parse_url( home_url( '/' ), PHP_URL_HOST ); |
| 83 | |
| 84 | if ( 'name' === $part ) { |
| 85 | $domain = explode( '.', $domain ); |
| 86 | $domain = count( $domain ) > 2 ? $domain[1] : $domain[0]; |
| 87 | } |
| 88 | |
| 89 | return $domain; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns true if the current request is a REST request. |
| 94 | * |
| 95 | * @return bool |
| 96 | */ |
| 97 | public static function is_rest_request(): bool { |
| 98 | $request = Params::server( 'REQUEST_URI' ); |
| 99 | if ( empty( $request ) ) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | return false !== strpos( $request, trailingslashit( rest_get_url_prefix() ) ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns true if a REST request has an Advanced Ads endpoint. |
| 108 | * |
| 109 | * @return bool |
| 110 | */ |
| 111 | public static function is_gutenberg_writing_request(): bool { |
| 112 | global $wp; |
| 113 | $rest_route = $wp->query_vars['rest_route'] ?? ''; |
| 114 | |
| 115 | $is_writing = in_array( Params::server( 'REQUEST_METHOD' ), [ 'POST', 'PUT' ], true ); |
| 116 | $is_gutenberg = strpos( $rest_route, '/wp/v2/posts' ) !== false || strpos( $rest_route, '/wp/v2/pages' ) !== false; |
| 117 | |
| 118 | return $is_gutenberg && $is_writing; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Unserializes data only if it was serialized. |
| 123 | * |
| 124 | * @link https://patchstack.com/articles/unauthenticated-php-object-injection-in-flatsome-theme-3-17-5/ |
| 125 | * |
| 126 | * @param string $data Data that might be unserialized. |
| 127 | * |
| 128 | * @return mixed Unserialized data can be any type. |
| 129 | */ |
| 130 | public static function maybe_unserialize( $data ) { |
| 131 | if ( is_serialized( $data ) ) { // Don't attempt to unserialize data that wasn't serialized going in. |
| 132 | return @unserialize( trim( $data ), [ 'allowed_classes' => false ] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
| 133 | } |
| 134 | |
| 135 | return $data; |
| 136 | } |
| 137 | } |
| 138 |