PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / ModuleHelper.php

ModuleHelper.php in Packeta 2.1, at src/Packetery/Module/ModuleHelper.php

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