| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Helper |
| 4 |
* |
| 5 |
* @package Packetery\Module |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module; |
| 11 |
|
| 12 |
use Automattic\WooCommerce\Utilities\OrderUtil; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Helper |
| 16 |
* |
| 17 |
* @package Packetery\Module |
| 18 |
*/ |
| 19 |
class Helper { |
| 20 |
|
| 21 |
/** |
| 22 |
* Gets order detail url. |
| 23 |
* |
| 24 |
* @param int $orderId Order ID. |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public static function getOrderDetailUrl( int $orderId ): string { |
| 29 |
$queryVars = []; |
| 30 |
|
| 31 |
if ( self::isHposEnabled() ) { |
| 32 |
$queryVars['page'] = 'wc-orders'; |
| 33 |
$queryVars['id'] = $orderId; |
| 34 |
$path = 'admin.php'; |
| 35 |
} else { |
| 36 |
$queryVars['post_type'] = 'shop_order'; |
| 37 |
$queryVars['post'] = $orderId; |
| 38 |
$path = 'post.php'; |
| 39 |
} |
| 40 |
|
| 41 |
$queryVars['action'] = 'edit'; |
| 42 |
|
| 43 |
return add_query_arg( $queryVars, admin_url( $path ) ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Gets order grid url. |
| 48 |
* |
| 49 |
* @param array $queryVars Query vars. |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
public static function getOrderGridUrl( array $queryVars = [] ): string { |
| 54 |
if ( self::isHposEnabled() ) { |
| 55 |
$queryVars['page'] = 'wc-orders'; |
| 56 |
$path = 'admin.php'; |
| 57 |
} else { |
| 58 |
$queryVars['post_type'] = 'shop_order'; |
| 59 |
$path = 'edit.php'; |
| 60 |
} |
| 61 |
|
| 62 |
return add_query_arg( $queryVars, admin_url( $path ) ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Tells if plugin is active. |
| 67 |
* |
| 68 |
* @param string $pluginRelativePath Relative path of plugin bootstrap file. |
| 69 |
* |
| 70 |
* @return bool |
| 71 |
*/ |
| 72 |
public static function isPluginActive( string $pluginRelativePath ): bool { |
| 73 |
if ( is_multisite() ) { |
| 74 |
$plugins = get_site_option( 'active_sitewide_plugins' ); |
| 75 |
if ( isset( $plugins[ $pluginRelativePath ] ) ) { |
| 76 |
return true; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if ( ! function_exists( 'get_mu_plugins' ) ) { |
| 81 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 82 |
} |
| 83 |
|
| 84 |
$muPlugins = get_mu_plugins(); |
| 85 |
if ( isset( $muPlugins[ $pluginRelativePath ] ) ) { |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
return in_array( $pluginRelativePath, (array) get_option( 'active_plugins', [] ), true ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Introduced as ManageWP Worker plugin hack fix. |
| 94 |
* |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public static function transformGlobalCookies(): void { |
| 98 |
if ( empty( $_COOKIE ) ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
foreach ( $_COOKIE as $key => $value ) { |
| 102 |
// @codingStandardsIgnoreStart |
| 103 |
if ( is_int( $value ) ) { |
| 104 |
$_COOKIE[ $key ] = (string) $value; |
| 105 |
} |
| 106 |
// @codingStandardsIgnoreEnd |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Gets WooCommerce version. |
| 112 |
* |
| 113 |
* @return string|null |
| 114 |
*/ |
| 115 |
public static function getWooCommerceVersion(): ?string { |
| 116 |
if ( false === file_exists( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php' ) ) { |
| 117 |
return null; |
| 118 |
} |
| 119 |
|
| 120 |
$version = get_file_data( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php', [ 'Version' => 'Version' ], 'plugin' )['Version']; |
| 121 |
if ( ! $version ) { |
| 122 |
return null; |
| 123 |
} |
| 124 |
|
| 125 |
return $version; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Renders string. |
| 130 |
* |
| 131 |
* @param string $string String to render. |
| 132 |
* |
| 133 |
* @return void |
| 134 |
*/ |
| 135 |
public static function renderString( string $string ): void { |
| 136 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 137 |
echo $string; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Gets country from WC order. |
| 142 |
* |
| 143 |
* @param \WC_Order $wcOrder WC order. |
| 144 |
* |
| 145 |
* @return string May be empty. |
| 146 |
*/ |
| 147 |
public static function getWcOrderCountry( \WC_Order $wcOrder ): string { |
| 148 |
$country = $wcOrder->get_shipping_country(); |
| 149 |
if ( empty( $country ) ) { |
| 150 |
$country = $wcOrder->get_billing_country(); |
| 151 |
} |
| 152 |
|
| 153 |
return strtolower( $country ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Tells if High Performance Order Storage is enabled. |
| 158 |
* |
| 159 |
* @return bool |
| 160 |
*/ |
| 161 |
public static function isHposEnabled(): bool { |
| 162 |
if ( false === class_exists( 'Automattic\\WooCommerce\\Utilities\\OrderUtil' ) ) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
if ( false === method_exists( OrderUtil::class, 'custom_orders_table_usage_is_enabled' ) ) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
return OrderUtil::custom_orders_table_usage_is_enabled(); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
/** |
| 175 |
* Converts all float values within an array to strings. |
| 176 |
* |
| 177 |
* @param array $array Array with parameters. |
| 178 |
* |
| 179 |
* @return array |
| 180 |
*/ |
| 181 |
public static function convertArrayFloatsToStrings( array $array ): array { |
| 182 |
array_walk_recursive( |
| 183 |
$array, |
| 184 |
static function ( &$item ) { |
| 185 |
if ( is_float( $item ) ) { |
| 186 |
$item = (string) $item; |
| 187 |
} |
| 188 |
} |
| 189 |
); |
| 190 |
|
| 191 |
return $array; |
| 192 |
|
| 193 |
} |
| 194 |
} |
| 195 |
|