PluginProbe
Packeta / 2.3.1
Packeta v2.3.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 / Dashboard / DashboardItemBuilder.php

DashboardItemBuilder.php in Packeta 2.3.1, at src/Packetery/Module/Dashboard/DashboardItemBuilder.php

206 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Packetery\Module\Dashboard;
6
7 use Packetery\Module\Carrier;
8 use Packetery\Module\Carrier\CarrierUpdater;
9 use Packetery\Module\Framework\WpAdapter;
10 use Packetery\Module\Options;
11 use Packetery\Module\Options\OptionsProvider;
12 use Packetery\Module\Order\PacketSynchronizer;
13 use Packetery\Module\Product;
14 use WP_Query;
15
16 class DashboardItemBuilder {
17
18 /**
19 * @var WpAdapter
20 */
21 private $wpAdapter;
22
23 /**
24 * @var DashboardHelper
25 */
26 private $dashboardHelper;
27
28 /**
29 * @var OptionsProvider
30 */
31 private $optionsProvider;
32
33 /**
34 * @var Carrier\EntityRepository
35 */
36 private $carrierEntityRepository;
37
38 /**
39 * @var CarrierUpdater
40 */
41 private $carrierUpdater;
42
43 /**
44 * @var PacketSynchronizer
45 */
46 private $packetSynchronizer;
47
48 public function __construct(
49 WpAdapter $wpAdapter,
50 DashboardHelper $dashboardHelper,
51 OptionsProvider $optionsProvider,
52 Carrier\EntityRepository $carrierEntityRepository,
53 CarrierUpdater $carrierUpdater,
54 PacketSynchronizer $packetSynchronizer
55 ) {
56 $this->wpAdapter = $wpAdapter;
57 $this->dashboardHelper = $dashboardHelper;
58 $this->optionsProvider = $optionsProvider;
59 $this->carrierEntityRepository = $carrierEntityRepository;
60 $this->carrierUpdater = $carrierUpdater;
61 $this->packetSynchronizer = $packetSynchronizer;
62 }
63
64 /**
65 * @return DashboardItem[]
66 */
67 public function buildItems(): array {
68 return [
69 new DashboardItem(
70 $this->wpAdapter->__( 'Basic settings of the Packeta plugin', 'packeta' ),
71 $this->wpAdapter->adminUrl( 'admin.php?page=' . Options\Page::SLUG . '&wizard-enabled=true&wizard-general-settings-tour-enabled=true' ),
72 $this->wpAdapter->__( 'Start with this to start using the plugin.', 'packeta' ),
73 1,
74 $this->optionsProvider->get_api_password() !== null && $this->optionsProvider->get_sender() !== null
75 ),
76 new DashboardItem(
77 $this->wpAdapter->__( 'Carrier type setting', 'packeta' ),
78 $this->wpAdapter->adminUrl( 'admin.php?page=' . Options\Page::SLUG . '&tab=' . Options\Page::TAB_ADVANCED . '&wizard-enabled=true&wizard-advanced-tour-enabled=true' ),
79 $this->wpAdapter->__( 'Here you can choose whether to use one shipping method for all carriers or separate methods for each carrier (recommended).', 'packeta' ),
80 2,
81 $this->optionsProvider->isWcCarrierConfigEnabledNullable() !== null
82 ),
83 new DashboardItem(
84 $this->wpAdapter->__( 'Product settings', 'packeta' ),
85 $this->getNewestProductUrl(),
86 $this->getProductSettingsDescription(),
87 3,
88 $this->hasProductsWithPacketaSettings()
89 ),
90 new DashboardItem(
91 $this->wpAdapter->__( 'Carriers update', 'packeta' ),
92 $this->getCarrierUpdateUrl(),
93 $this->wpAdapter->__( 'Load the current list of external carriers so that you can use them.', 'packeta' ),
94 4,
95 $this->carrierUpdater->getLastUpdate() !== null
96 ),
97 new DashboardItem(
98 $this->wpAdapter->__( 'Carrier settings', 'packeta' ),
99 $this->wpAdapter->adminUrl( 'admin.php?page=' . Carrier\OptionsPage::SLUG ),
100 $this->wpAdapter->__( 'Set prices, weight limits and other settings for the carriers you want to use.', 'packeta' ),
101 5,
102 count( $this->carrierEntityRepository->getAllActiveCarriersList() ) > 0
103 ),
104 new DashboardItem(
105 $this->wpAdapter->__( 'Shipping zone settings', 'packeta' ),
106 $this->wpAdapter->adminUrl( 'admin.php?page=wc-settings&tab=shipping' ),
107 $this->wpAdapter->__( 'Assign shipping methods to WooCommerce zones so that they are offered at checkout.', 'packeta' ),
108 6,
109 $this->dashboardHelper->isPacketaShippingMethodActive()
110 ),
111 new DashboardItem(
112 $this->wpAdapter->__( 'Set up shipment status tracking', 'packeta' ),
113 $this->wpAdapter->adminUrl( 'admin.php?page=' . Options\Page::SLUG . '&tab=' . Options\Page::TAB_PACKET_STATUS_SYNC . '&wizard-enabled=true&wizard-packet-status-tracking-tour-enabled=true' ),
114 $this->wpAdapter->__( 'Always keep an eye on the current status of your shipment. This status will be displayed in the order overview.', 'packeta' ),
115 7,
116 count( $this->optionsProvider->getStatusSyncingPacketStatuses( $this->packetSynchronizer->getDefaultPacketStatuses() ) ) > 0 &&
117 count( $this->optionsProvider->getExistingStatusSyncingOrderStatuses() ) > 0
118 ),
119 new DashboardItem(
120 $this->wpAdapter->__( 'Set up automatic shipment submission', 'packeta' ),
121 $this->wpAdapter->adminUrl( 'admin.php?page=' . Options\Page::SLUG . '&tab=' . Options\Page::TAB_AUTO_SUBMISSION . '&wizard-enabled=true&wizard-auto-submission-tour-enabled=true' ),
122 $this->wpAdapter->__( 'Use this if you want to automatically submit shipments based on the order status.', 'packeta' ),
123 8,
124 $this->optionsProvider->isPacketAutoSubmissionEnabled() &&
125 count( $this->optionsProvider->getPacketAutoSubmissionMappedUniqueEvents() ) > 0
126 ),
127 ];
128 }
129
130 private function getNewestProductUrl(): ?string {
131 $args = array(
132 'post_type' => 'product',
133 'posts_per_page' => 1,
134 'orderby' => 'date',
135 'order' => 'DESC',
136 );
137
138 $query = new WP_Query( $args );
139
140 if ( $query->have_posts() ) {
141 $query->the_post();
142 $editUrl = $this->wpAdapter->getEditPostLink( $this->wpAdapter->getTheId(), '' );
143 $this->wpAdapter->resetPostdata();
144
145 return $editUrl;
146 }
147
148 return null;
149 }
150
151 private function getProductSettingsDescription(): string {
152 $suffix = '';
153 if ( $this->getNewestProductUrl() === null ) {
154 $suffix = ' ' . $this->wpAdapter->__( 'First create at least one product.', 'packeta' );
155 }
156
157 return $this->wpAdapter->__( 'For each product, you can set it to be intended for adults only, or disable specific Packeta carriers for this product.', 'packeta' ) . $suffix;
158 }
159
160 private function getCarrierUpdateUrl(): ?string {
161 if ( $this->optionsProvider->get_api_password() === null ) {
162 return null;
163 }
164 if ( $this->carrierUpdater->getLastUpdate() !== null ) {
165 return $this->wpAdapter->adminUrl( 'admin.php?page=' . Carrier\OptionsPage::SLUG . '&update_carriers=1' );
166 }
167
168 return $this->wpAdapter->adminUrl( 'admin.php?page=' . DashboardPage::SLUG . '&update_carriers=1' );
169 }
170
171 private function hasProductsWithPacketaSettings(): bool {
172 $args = [
173 'post_type' => 'product',
174 'posts_per_page' => 1,
175 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
176 'meta_query' => [
177 'relation' => 'OR',
178 [
179 'key' => Product\Entity::META_AGE_VERIFICATION_18_PLUS,
180 'value' => '1',
181 'compare' => '=',
182 ],
183 [
184 'relation' => 'AND',
185 [
186 'key' => Product\Entity::META_DISALLOWED_SHIPPING_RATES,
187 'compare' => 'EXISTS',
188 ],
189 [
190 'key' => Product\Entity::META_DISALLOWED_SHIPPING_RATES,
191 'value' => 'a:0:{}',
192 'compare' => '!=',
193 ],
194 ],
195 ],
196 ];
197
198 $query = new WP_Query( $args );
199 if ( $query->have_posts() ) {
200 return true;
201 }
202
203 return false;
204 }
205 }
206