| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class ContextResolver. |
| 4 |
* |
| 5 |
* @package Packetery\Module |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module; |
| 11 |
|
| 12 |
use Packetery\Nette\Http\Request; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class ContextResolver. |
| 16 |
* |
| 17 |
* @package Packetery\Module |
| 18 |
*/ |
| 19 |
class ContextResolver { |
| 20 |
|
| 21 |
/** |
| 22 |
* Request. |
| 23 |
* |
| 24 |
* @var Request |
| 25 |
*/ |
| 26 |
private $request; |
| 27 |
|
| 28 |
/** |
| 29 |
* ContextResolver constructor. |
| 30 |
* |
| 31 |
* @param Request $request Packetery request. |
| 32 |
*/ |
| 33 |
public function __construct( Request $request ) { |
| 34 |
$this->request = $request; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Tells if requesting user is at order grid page. |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public function isOrderGridPage(): bool { |
| 43 |
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps |
| 44 |
global $pagenow, $typenow, $plugin_page; |
| 45 |
|
| 46 |
if ( ModuleHelper::isHposEnabled() ) { |
| 47 |
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps |
| 48 |
return $pagenow === 'admin.php' && $plugin_page === 'wc-orders' && in_array( $this->request->getQuery( 'action' ), [ 'edit', 'new' ], true ) === false; |
| 49 |
} |
| 50 |
|
| 51 |
return $pagenow === 'edit.php' && $typenow === 'shop_order'; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Tells if requesting user is at order detail page. |
| 56 |
* |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
public function isOrderDetailPage(): bool { |
| 60 |
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps |
| 61 |
global $pagenow, $typenow, $plugin_page; |
| 62 |
|
| 63 |
if ( ModuleHelper::isHposEnabled() ) { |
| 64 |
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps |
| 65 |
return $pagenow === 'admin.php' && $plugin_page === 'wc-orders' && $this->request->getQuery( 'action' ) === 'edit'; |
| 66 |
} |
| 67 |
|
| 68 |
return $pagenow === 'post.php' && $typenow === 'shop_order'; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Tells if requesting user is at product detail page. |
| 73 |
* |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
private function isProductDetailPage(): bool { |
| 77 |
global $pagenow, $typenow; |
| 78 |
|
| 79 |
return $pagenow === 'post.php' && $typenow === 'product'; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Tells if requesting user is at product create page. |
| 84 |
* |
| 85 |
* @return bool |
| 86 |
*/ |
| 87 |
private function isProductCreatePage(): bool { |
| 88 |
global $pagenow, $typenow; |
| 89 |
|
| 90 |
return $pagenow === 'post-new.php' && $typenow === 'product'; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Tells if requesting user is at product page. |
| 95 |
* |
| 96 |
* @return bool |
| 97 |
*/ |
| 98 |
public function isProductPage(): bool { |
| 99 |
return $this->isProductCreatePage() || $this->isProductDetailPage(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Tells if requesting user is at page using packetery confirm. |
| 104 |
* |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
public function isConfirmModalPage(): bool { |
| 108 |
return $this->isOrderDetailPage() || $this->isOrderGridPage(); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Tells if requesting user is at product category grid page. |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
public function isProductCategoryGridPage(): bool { |
| 117 |
global $pagenow; |
| 118 |
|
| 119 |
return $pagenow === 'edit-tags.php' && $this->request->getQuery( 'taxonomy' ) === ProductCategory\Entity::TAXONOMY_NAME; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Tells if requesting user is at product category grid page. |
| 124 |
* |
| 125 |
* @return bool |
| 126 |
*/ |
| 127 |
public function isProductCategoryDetailPage(): bool { |
| 128 |
global $pagenow; |
| 129 |
|
| 130 |
return $pagenow === 'term.php' && $this->request->getQuery( 'taxonomy' ) === ProductCategory\Entity::TAXONOMY_NAME; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Tells if user is at page detail. |
| 135 |
* |
| 136 |
* @return bool |
| 137 |
*/ |
| 138 |
public function isPageDetail(): bool { |
| 139 |
global $pagenow, $typenow; |
| 140 |
|
| 141 |
return $pagenow === 'post.php' && $typenow === 'page'; |
| 142 |
} |
| 143 |
|
| 144 |
private function isShippingZoneDetailPage(): bool { |
| 145 |
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps |
| 146 |
global $pagenow, $plugin_page; |
| 147 |
|
| 148 |
return ( |
| 149 |
$pagenow === 'admin.php' && |
| 150 |
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps |
| 151 |
$plugin_page === 'wc-settings' && |
| 152 |
$this->request->getQuery( 'tab' ) === 'shipping' && |
| 153 |
$this->request->getQuery( 'zone_id' ) > 0 |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
public function getShippingZoneId(): ?int { |
| 158 |
if ( $this->isShippingZoneDetailPage() ) { |
| 159 |
return (int) $this->request->getQuery( 'zone_id' ); |
| 160 |
} |
| 161 |
|
| 162 |
return null; |
| 163 |
} |
| 164 |
|
| 165 |
public function isPluginsOverviewPage(): bool { |
| 166 |
global $pagenow; |
| 167 |
|
| 168 |
return $pagenow === 'plugins.php'; |
| 169 |
} |
| 170 |
} |
| 171 |
|