class-arr.php
2 years ago
class-attachment.php
2 years ago
class-conditional.php
2 years ago
class-db.php
2 years ago
class-html.php
2 years ago
class-param.php
2 years ago
class-str.php
2 years ago
class-url.php
2 years ago
class-wordpress.php
2 years ago
index.php
2 years ago
class-conditional.php
131 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The Conditional helpers. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package MyThemeShop |
| 7 | * @subpackage MyThemeShop\Helpers |
| 8 | * @author MyThemeShop <admin@mythemeshop.com> |
| 9 | */ |
| 10 | |
| 11 | namespace MyThemeShop\Helpers; |
| 12 | |
| 13 | use MyThemeShop\Helpers\Param; |
| 14 | |
| 15 | /** |
| 16 | * Conditional class. |
| 17 | */ |
| 18 | class Conditional { |
| 19 | |
| 20 | /** |
| 21 | * Is AJAX request |
| 22 | * |
| 23 | * @return bool Returns true when the page is loaded via ajax. |
| 24 | */ |
| 25 | public static function is_ajax() { |
| 26 | return function_exists( 'wp_doing_ajax' ) ? wp_doing_ajax() : defined( 'DOING_AJAX' ) && DOING_AJAX; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Is CRON request |
| 31 | * |
| 32 | * @return bool Returns true when the page is loaded via cron. |
| 33 | */ |
| 34 | public static function is_cron() { |
| 35 | return function_exists( 'wp_doing_cron' ) ? wp_doing_cron() : defined( 'DOING_CRON' ) && DOING_CRON; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Is auto-saving |
| 40 | * |
| 41 | * @return bool Returns true when the page is loaded for auto-saving. |
| 42 | */ |
| 43 | public static function is_autosave() { |
| 44 | return defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Is REST request |
| 49 | * |
| 50 | * @link https://wordpress.stackexchange.com/questions/221202/does-something-like-is-rest-exist/221289 |
| 51 | * |
| 52 | * Case #1: After WP_REST_Request initialisation |
| 53 | * Case #2: Support "plain" permalink settings |
| 54 | * Case #3: It can happen that WP_Rewrite is not yet initialized, |
| 55 | * so do this (wp-settings.php) |
| 56 | * Case #4: URL Path begins with wp-json/ (your REST prefix) |
| 57 | * Also supports WP installations in subfolders |
| 58 | * |
| 59 | * @return bool |
| 60 | */ |
| 61 | public static function is_rest() { |
| 62 | global $wp_rewrite; |
| 63 | |
| 64 | $prefix = rest_get_url_prefix(); |
| 65 | if ( |
| 66 | defined( 'REST_REQUEST' ) && REST_REQUEST || // (#1) |
| 67 | isset( $_GET['rest_route'] ) && // (#2) |
| 68 | 0 === strpos( trim( $_GET['rest_route'], '\\/' ), $prefix, 0 ) |
| 69 | ) { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | // (#3) |
| 74 | if ( null === $wp_rewrite ) { |
| 75 | $wp_rewrite = new \WP_Rewrite; |
| 76 | } |
| 77 | |
| 78 | // (#4) |
| 79 | $rest_url = wp_parse_url( trailingslashit( rest_url() ) ); |
| 80 | $current_url = wp_parse_url( add_query_arg( [] ) ); |
| 81 | |
| 82 | if ( ! isset( $current_url['path'] ) || ! isset( $rest_url['path'] ) ) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | return 0 === strpos( $current_url['path'], $rest_url['path'], 0 ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Check if the request is heartbeat. |
| 91 | * |
| 92 | * @return bool |
| 93 | */ |
| 94 | public static function is_heartbeat() { |
| 95 | return 'heartbeat' === Param::post( 'action' ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Check if the request is from frontend. |
| 100 | * |
| 101 | * @codeCoverageIgnore |
| 102 | * |
| 103 | * @return bool |
| 104 | */ |
| 105 | public function is_frontend() { |
| 106 | return ! is_admin(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Is WooCommerce Installed |
| 111 | * |
| 112 | * @return bool |
| 113 | */ |
| 114 | public static function is_woocommerce_active() { |
| 115 | // @codeCoverageIgnoreStart |
| 116 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 117 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 118 | } |
| 119 | // @codeCoverageIgnoreEnd |
| 120 | return is_plugin_active( 'woocommerce/woocommerce.php' ) && function_exists( 'is_woocommerce' ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Is EDD Installed |
| 125 | * |
| 126 | * @return bool |
| 127 | */ |
| 128 | public static function is_edd_active() { |
| 129 | return class_exists( 'Easy_Digital_Downloads' ); |
| 130 | } |
| 131 | } |