| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class ModuleHelper |
| 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 |
use DateTimeImmutable; |
| 14 |
use Packetery\Module\Framework\WpAdapter; |
| 15 |
use Packetery\Nette\Utils\FileSystem; |
| 16 |
use Packetery\Nette\Utils\Html; |
| 17 |
use WC_DateTime; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class ModuleHelper |
| 21 |
* |
| 22 |
* @package Packetery\Module |
| 23 |
*/ |
| 24 |
class ModuleHelper { |
| 25 |
|
| 26 |
/** |
| 27 |
* @var WpAdapter |
| 28 |
*/ |
| 29 |
private $wpAdapter; |
| 30 |
|
| 31 |
public function __construct( WpAdapter $wpAdapter ) { |
| 32 |
$this->wpAdapter = $wpAdapter; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Gets order detail url. |
| 37 |
* |
| 38 |
* @param int $orderId Order ID. |
| 39 |
* |
| 40 |
* @return string |
| 41 |
*/ |
| 42 |
public static function getOrderDetailUrl( int $orderId ): string { |
| 43 |
$queryVars = []; |
| 44 |
|
| 45 |
if ( self::isHposEnabled() ) { |
| 46 |
$queryVars['page'] = 'wc-orders'; |
| 47 |
$queryVars['id'] = $orderId; |
| 48 |
$path = 'admin.php'; |
| 49 |
} else { |
| 50 |
$queryVars['post_type'] = 'shop_order'; |
| 51 |
$queryVars['post'] = $orderId; |
| 52 |
$path = 'post.php'; |
| 53 |
} |
| 54 |
|
| 55 |
$queryVars['action'] = 'edit'; |
| 56 |
|
| 57 |
return add_query_arg( $queryVars, admin_url( $path ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Gets order grid url. |
| 62 |
* |
| 63 |
* @param array $queryVars Query vars. |
| 64 |
* |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public static function getOrderGridUrl( array $queryVars = [] ): string { |
| 68 |
if ( self::isHposEnabled() ) { |
| 69 |
$queryVars['page'] = 'wc-orders'; |
| 70 |
$path = 'admin.php'; |
| 71 |
} else { |
| 72 |
$queryVars['post_type'] = 'shop_order'; |
| 73 |
$path = 'edit.php'; |
| 74 |
} |
| 75 |
|
| 76 |
return add_query_arg( $queryVars, admin_url( $path ) ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Tells if plugin is active. |
| 81 |
* |
| 82 |
* @param string $pluginRelativePath Relative path of plugin bootstrap file. |
| 83 |
* |
| 84 |
* @return bool |
| 85 |
*/ |
| 86 |
public function isPluginActive( string $pluginRelativePath ): bool { |
| 87 |
if ( $this->wpAdapter->isMultisite() ) { |
| 88 |
$plugins = $this->wpAdapter->getSiteOption( 'active_sitewide_plugins' ); |
| 89 |
if ( isset( $plugins[ $pluginRelativePath ] ) ) { |
| 90 |
return true; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if ( ! function_exists( 'get_mu_plugins' ) ) { |
| 95 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 96 |
} |
| 97 |
|
| 98 |
$muPlugins = $this->wpAdapter->getMuPlugins(); |
| 99 |
if ( isset( $muPlugins[ $pluginRelativePath ] ) ) { |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
return in_array( $pluginRelativePath, (array) $this->wpAdapter->getOption( 'active_plugins', [] ), true ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Gets WooCommerce version. |
| 108 |
* |
| 109 |
* @return string|null |
| 110 |
*/ |
| 111 |
public static function getWooCommerceVersion(): ?string { |
| 112 |
if ( file_exists( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php' ) === false ) { |
| 113 |
return null; |
| 114 |
} |
| 115 |
|
| 116 |
$version = ''; |
| 117 |
$fileData = get_file_data( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php', [ 'Version' => 'Version' ], 'plugin' ); |
| 118 |
|
| 119 |
if ( isset( $fileData['Version'] ) ) { |
| 120 |
$version = $fileData['Version']; |
| 121 |
} |
| 122 |
|
| 123 |
if ( $version === '' ) { |
| 124 |
return null; |
| 125 |
} |
| 126 |
|
| 127 |
return $version; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Renders string. |
| 132 |
* |
| 133 |
* @param string $inputString String to render. |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
public static function renderString( string $inputString ): void { |
| 138 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 139 |
echo $inputString; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Gets country from WC order. |
| 144 |
* |
| 145 |
* @param \WC_Order $wcOrder WC order. |
| 146 |
* |
| 147 |
* @return string May be empty. |
| 148 |
*/ |
| 149 |
public static function getWcOrderCountry( \WC_Order $wcOrder ): string { |
| 150 |
$country = $wcOrder->get_shipping_country(); |
| 151 |
if ( $country === null || $country === '' ) { |
| 152 |
$country = $wcOrder->get_billing_country(); |
| 153 |
} |
| 154 |
|
| 155 |
return strtolower( $country ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Tells if High Performance Order Storage is enabled. |
| 160 |
* |
| 161 |
* @return bool |
| 162 |
*/ |
| 163 |
public static function isHposEnabled(): bool { |
| 164 |
if ( class_exists( 'Automattic\\WooCommerce\\Utilities\\OrderUtil' ) === false ) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
// @phpstan-ignore-next-line (This method was probably added in WC version 6.9.0, backward compatibility) |
| 168 |
if ( method_exists( OrderUtil::class, 'custom_orders_table_usage_is_enabled' ) === false ) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
return OrderUtil::custom_orders_table_usage_is_enabled(); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Converts all float values within an array to strings. |
| 177 |
* |
| 178 |
* @param array $inputArray Array with parameters. |
| 179 |
* |
| 180 |
* @return array |
| 181 |
*/ |
| 182 |
public static function convertArrayFloatsToStrings( array $inputArray ): array { |
| 183 |
array_walk_recursive( |
| 184 |
$inputArray, |
| 185 |
static function ( &$item ) { |
| 186 |
if ( is_float( $item ) ) { |
| 187 |
$item = (string) $item; |
| 188 |
} |
| 189 |
} |
| 190 |
); |
| 191 |
|
| 192 |
return $inputArray; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Returns null for value lesser than 1. |
| 197 |
* |
| 198 |
* @param int $number Value (mm). |
| 199 |
* |
| 200 |
* @return float|null |
| 201 |
*/ |
| 202 |
public static function convertToCentimeters( int $number ): ?float { |
| 203 |
return $number < 1 ? null : ( $number * 0.1 ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Creates a named tracking URL for packet. |
| 208 |
* |
| 209 |
* @param string $trackingUrl Tracking URL. |
| 210 |
* @param string $text Text. |
| 211 |
* @param string $target Target attribute. |
| 212 |
* |
| 213 |
* @return Html |
| 214 |
*/ |
| 215 |
public function createHtmlLink( string $trackingUrl, string $text, string $target = '_blank' ): Html { |
| 216 |
return Html::el( 'a' ) |
| 217 |
->href( $trackingUrl ) |
| 218 |
->setText( $text ) |
| 219 |
->setAttribute( 'target', $target ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Returns null for value lesser than 0.1. |
| 224 |
* |
| 225 |
* @param float $number Value (cm). |
| 226 |
* |
| 227 |
* @return float|null |
| 228 |
*/ |
| 229 |
public static function convertToMillimeters( float $number ): ?float { |
| 230 |
return $number < 0.1 ? null : ( $number * 10 ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Creates HTML link parts in array. |
| 235 |
* |
| 236 |
* @param string $href Href. |
| 237 |
* @param string|null $target Target. |
| 238 |
* @param string|null $className Class. |
| 239 |
* |
| 240 |
* @return string[] |
| 241 |
*/ |
| 242 |
public function createLinkParts( string $href, ?string $target = null, ?string $className = null ): array { |
| 243 |
$link = Html::el( 'a' )->href( $href ); |
| 244 |
|
| 245 |
if ( $target !== null ) { |
| 246 |
$link->target( $target ); |
| 247 |
} |
| 248 |
|
| 249 |
if ( $className !== null ) { |
| 250 |
$link->class( $className ); |
| 251 |
} |
| 252 |
|
| 253 |
return [ $link->startTag(), $link->endTag() ]; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Creates translated Date |
| 258 |
* |
| 259 |
* @param DateTimeImmutable|null $date Datetime. |
| 260 |
* |
| 261 |
* @return string|null |
| 262 |
*/ |
| 263 |
public function getTranslatedStringFromDateTime( ?DateTimeImmutable $date ): ?string { |
| 264 |
if ( $date !== null ) { |
| 265 |
$wcDateTime = new WC_DateTime(); |
| 266 |
$wcDateTime->setTimestamp( $date->getTimestamp() ); |
| 267 |
|
| 268 |
return $wcDateTime->date_i18n( |
| 269 |
/** |
| 270 |
* Applies woocommerce_admin_order_date_format filters. |
| 271 |
* |
| 272 |
* @since 1.8.3 |
| 273 |
*/ |
| 274 |
apply_filters( 'woocommerce_admin_order_date_format', __( 'M j, Y', 'woocommerce' ) ) //phpcs:ignore WordPress.WP.I18n.TextDomainMismatch |
| 275 |
); |
| 276 |
} |
| 277 |
|
| 278 |
return null; |
| 279 |
} |
| 280 |
|
| 281 |
public function isWooCommercePluginActive(): bool { |
| 282 |
return $this->isPluginActive( 'woocommerce/woocommerce.php' ); |
| 283 |
} |
| 284 |
|
| 285 |
public static function getPluginMainFilePath(): string { |
| 286 |
return PACKETERY_PLUGIN_DIR . '/packeta.php'; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Gets current locale. |
| 291 |
*/ |
| 292 |
public function getLocale(): string { |
| 293 |
/** |
| 294 |
* Applies plugin_locale filters. |
| 295 |
* |
| 296 |
* @since 1.0.0 |
| 297 |
*/ |
| 298 |
return (string) $this->wpAdapter->applyFilters( |
| 299 |
'plugin_locale', |
| 300 |
( $this->wpAdapter->isAdmin() ? $this->wpAdapter->getUserLocale() : $this->wpAdapter->getLocale() ), |
| 301 |
Plugin::DOMAIN |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
public function isCzechLocale(): bool { |
| 306 |
return $this->getLocale() === 'cs_CZ'; |
| 307 |
} |
| 308 |
|
| 309 |
public static function instantDelete( string $path ): void { |
| 310 |
$tempOldPath = $path . '-old'; |
| 311 |
FileSystem::rename( $path, $tempOldPath ); |
| 312 |
FileSystem::delete( $tempOldPath ); |
| 313 |
} |
| 314 |
} |
| 315 |
|