| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Metabox |
| 4 |
* |
| 5 |
* @package Packetery\Order |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core; |
| 13 |
use Packetery\Core\Helper; |
| 14 |
use Packetery\Module\Checkout; |
| 15 |
use Packetery\Module\FormFactory; |
| 16 |
use Packetery\Module\MessageManager; |
| 17 |
use Packetery\Module\Options; |
| 18 |
use Packetery\Module\Plugin; |
| 19 |
use PacketeryLatte\Engine; |
| 20 |
use PacketeryNette\Forms\Form; |
| 21 |
use PacketeryNette\Http\Request; |
| 22 |
|
| 23 |
/** |
| 24 |
* Class Metabox |
| 25 |
* |
| 26 |
* @package Packetery\Order |
| 27 |
*/ |
| 28 |
class Metabox { |
| 29 |
|
| 30 |
const FIELD_WEIGHT = 'packetery_weight'; |
| 31 |
const FIELD_WIDTH = 'packetery_width'; |
| 32 |
const FIELD_LENGTH = 'packetery_length'; |
| 33 |
const FIELD_HEIGHT = 'packetery_height'; |
| 34 |
|
| 35 |
/** |
| 36 |
* PacketeryLatte engine. |
| 37 |
* |
| 38 |
* @var Engine |
| 39 |
*/ |
| 40 |
private $latte_engine; |
| 41 |
|
| 42 |
/** |
| 43 |
* Message manager. |
| 44 |
* |
| 45 |
* @var MessageManager |
| 46 |
*/ |
| 47 |
private $message_manager; |
| 48 |
|
| 49 |
/** |
| 50 |
* Helper. |
| 51 |
* |
| 52 |
* @var Helper |
| 53 |
*/ |
| 54 |
private $helper; |
| 55 |
|
| 56 |
/** |
| 57 |
* Order form. |
| 58 |
* |
| 59 |
* @var Form |
| 60 |
*/ |
| 61 |
private $order_form; |
| 62 |
|
| 63 |
/** |
| 64 |
* HTTP request. |
| 65 |
* |
| 66 |
* @var Request |
| 67 |
*/ |
| 68 |
private $request; |
| 69 |
|
| 70 |
/** |
| 71 |
* Options provider. |
| 72 |
* |
| 73 |
* @var Options\Provider |
| 74 |
*/ |
| 75 |
private $optionsProvider; |
| 76 |
|
| 77 |
/** |
| 78 |
* Form factory. |
| 79 |
* |
| 80 |
* @var FormFactory |
| 81 |
*/ |
| 82 |
private $formFactory; |
| 83 |
|
| 84 |
/** |
| 85 |
* Order repository. |
| 86 |
* |
| 87 |
* @var Repository |
| 88 |
*/ |
| 89 |
private $orderRepository; |
| 90 |
|
| 91 |
/** |
| 92 |
* Metabox constructor. |
| 93 |
* |
| 94 |
* @param Engine $latte_engine PacketeryLatte engine. |
| 95 |
* @param MessageManager $message_manager Message manager. |
| 96 |
* @param Helper $helper Helper. |
| 97 |
* @param Request $request Http request. |
| 98 |
* @param Options\Provider $optionsProvider Options provider. |
| 99 |
* @param FormFactory $formFactory Form factory. |
| 100 |
* @param Repository $orderRepository Order repository. |
| 101 |
*/ |
| 102 |
public function __construct( |
| 103 |
Engine $latte_engine, |
| 104 |
MessageManager $message_manager, |
| 105 |
Helper $helper, |
| 106 |
Request $request, |
| 107 |
Options\Provider $optionsProvider, |
| 108 |
FormFactory $formFactory, |
| 109 |
Repository $orderRepository |
| 110 |
) { |
| 111 |
$this->latte_engine = $latte_engine; |
| 112 |
$this->message_manager = $message_manager; |
| 113 |
$this->helper = $helper; |
| 114 |
$this->request = $request; |
| 115 |
$this->optionsProvider = $optionsProvider; |
| 116 |
$this->formFactory = $formFactory; |
| 117 |
$this->orderRepository = $orderRepository; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Registers related hooks. |
| 122 |
*/ |
| 123 |
public function register(): void { |
| 124 |
add_action( |
| 125 |
'admin_init', |
| 126 |
function () { |
| 127 |
$this->order_form = $this->formFactory->create(); |
| 128 |
$this->add_fields(); |
| 129 |
} |
| 130 |
); |
| 131 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); |
| 132 |
add_action( 'save_post', array( $this, 'save_fields' ) ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Add metaboxes |
| 137 |
*/ |
| 138 |
public function add_meta_boxes(): void { |
| 139 |
global $post; |
| 140 |
|
| 141 |
$order = $this->orderRepository->getById( (int) $post->ID ); |
| 142 |
if ( null === $order ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
add_meta_box( |
| 147 |
'packetery_metabox', |
| 148 |
__( 'Packeta', 'packetery' ), |
| 149 |
array( |
| 150 |
$this, |
| 151 |
'render_metabox', |
| 152 |
), |
| 153 |
'shop_order', |
| 154 |
'side', |
| 155 |
'high' |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Adds packetery fields to order form. |
| 161 |
*/ |
| 162 |
public function add_fields(): void { |
| 163 |
$this->order_form->addHidden( 'packetery_order_metabox_nonce' ); |
| 164 |
$this->order_form->addText( self::FIELD_WEIGHT, __( 'Weight (kg)', 'packetery' ) ) |
| 165 |
->setRequired( false ) |
| 166 |
->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packetery' ) ); |
| 167 |
$this->order_form->addText( self::FIELD_WIDTH, __( 'Width (mm)', 'packetery' ) ) |
| 168 |
->setRequired( false ) |
| 169 |
->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packetery' ) ); |
| 170 |
$this->order_form->addText( self::FIELD_LENGTH, __( 'Length (mm)', 'packetery' ) ) |
| 171 |
->setRequired( false ) |
| 172 |
->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packetery' ) ); |
| 173 |
$this->order_form->addText( self::FIELD_HEIGHT, __( 'Height (mm)', 'packetery' ) ) |
| 174 |
->setRequired( false ) |
| 175 |
->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packetery' ) ); |
| 176 |
|
| 177 |
foreach ( Checkout::$pickupPointAttrs as $attrs ) { |
| 178 |
$this->order_form->addHidden( $attrs['name'] ); |
| 179 |
} |
| 180 |
$this->order_form->addButton( 'packetery_pick_pickup_point', __( 'choosePickupPoint', 'packetery' ) ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Renders metabox |
| 185 |
*/ |
| 186 |
public function render_metabox(): void { |
| 187 |
global $post; |
| 188 |
|
| 189 |
$order = $this->orderRepository->getById( (int) $post->ID ); |
| 190 |
if ( null === $order ) { |
| 191 |
return; |
| 192 |
} |
| 193 |
$packetId = $order->getPacketId(); |
| 194 |
|
| 195 |
if ( $packetId ) { |
| 196 |
$this->latte_engine->render( |
| 197 |
PACKETERY_PLUGIN_DIR . '/template/order/metabox-overview.latte', |
| 198 |
[ |
| 199 |
'packet_id' => $packetId, |
| 200 |
'packet_tracking_url' => $this->helper->get_tracking_url( $packetId ), |
| 201 |
] |
| 202 |
); |
| 203 |
|
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
$this->order_form->setDefaults( |
| 208 |
[ |
| 209 |
'packetery_order_metabox_nonce' => wp_create_nonce(), |
| 210 |
self::FIELD_WEIGHT => $order->getWeight(), |
| 211 |
self::FIELD_WIDTH => $order->getWidth(), |
| 212 |
self::FIELD_LENGTH => $order->getLength(), |
| 213 |
self::FIELD_HEIGHT => $order->getHeight(), |
| 214 |
] |
| 215 |
); |
| 216 |
|
| 217 |
$prev_invalid_values = get_transient( 'packetery_metabox_nette_form_prev_invalid_values' ); |
| 218 |
if ( $prev_invalid_values ) { |
| 219 |
$this->order_form->setValues( $prev_invalid_values ); |
| 220 |
$this->order_form->validate(); |
| 221 |
} |
| 222 |
delete_transient( 'packetery_metabox_nette_form_prev_invalid_values' ); |
| 223 |
|
| 224 |
$wpOrder = wc_get_order( $order->getNumber() ); |
| 225 |
$widgetSettings = [ |
| 226 |
'packeteryApiKey' => $this->optionsProvider->get_api_key(), |
| 227 |
'country' => mb_strtolower( $wpOrder->get_shipping_country() ), |
| 228 |
'language' => substr( get_locale(), 0, 2 ), |
| 229 |
'appIdentity' => Plugin::getAppIdentity(), |
| 230 |
'weight' => $order->getWeight(), |
| 231 |
'carriers' => Checkout::getWidgetCarriersParam( $order->isPickupPointDelivery(), $order->getCarrierId() ), |
| 232 |
'pickupPointAttrs' => Checkout::$pickupPointAttrs, |
| 233 |
]; |
| 234 |
|
| 235 |
$this->latte_engine->render( |
| 236 |
PACKETERY_PLUGIN_DIR . '/template/order/metabox-form.latte', |
| 237 |
[ |
| 238 |
'form' => $this->order_form, |
| 239 |
'order' => $order, |
| 240 |
'widgetSettings' => $widgetSettings, |
| 241 |
'logo' => plugin_dir_url( PACKETERY_PLUGIN_DIR . '/packeta.php' ) . 'public/packeta-symbol.png', |
| 242 |
] |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Saves added packetery form fields to order metas. |
| 248 |
* |
| 249 |
* @param mixed $orderId Order id. |
| 250 |
* |
| 251 |
* @return mixed Order id. |
| 252 |
*/ |
| 253 |
public function save_fields( $orderId ) { |
| 254 |
$order = $this->orderRepository->getById( $orderId ); |
| 255 |
if ( |
| 256 |
( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || |
| 257 |
null === $this->request->getPost( 'packetery_order_metabox_nonce' ) || |
| 258 |
null === $order |
| 259 |
) { |
| 260 |
return $orderId; |
| 261 |
} |
| 262 |
|
| 263 |
if ( false === $this->order_form->isValid() ) { |
| 264 |
set_transient( 'packetery_metabox_nette_form_prev_invalid_values', $this->order_form->getValues( true ) ); |
| 265 |
$this->message_manager->flash_message( __( 'Error happened in Packeta fields!', 'packetery' ), MessageManager::TYPE_ERROR ); |
| 266 |
|
| 267 |
return $orderId; |
| 268 |
} |
| 269 |
|
| 270 |
$values = $this->order_form->getValues( 'array' ); |
| 271 |
|
| 272 |
if ( ! wp_verify_nonce( $values['packetery_order_metabox_nonce'] ) ) { |
| 273 |
$this->message_manager->flash_message( __( 'Session has expired! Please try again.', 'packetery' ), MessageManager::TYPE_ERROR ); |
| 274 |
|
| 275 |
return $orderId; |
| 276 |
} |
| 277 |
|
| 278 |
if ( ! current_user_can( 'edit_post', $orderId ) ) { |
| 279 |
$this->message_manager->flash_message( __( 'You are not allowed to edit posts!', 'packetery' ), MessageManager::TYPE_ERROR ); |
| 280 |
|
| 281 |
return $orderId; |
| 282 |
} |
| 283 |
|
| 284 |
$propsToSave = [ |
| 285 |
self::FIELD_WEIGHT => ( is_numeric( $values[ self::FIELD_WEIGHT ] ) ? Helper::simplifyWeight( $values[ self::FIELD_WEIGHT ] ) : null ), |
| 286 |
self::FIELD_WIDTH => ( is_numeric( $values[ self::FIELD_WIDTH ] ) ? (float) number_format( $values[ self::FIELD_WIDTH ], 0, '.', '' ) : null ), |
| 287 |
self::FIELD_LENGTH => ( is_numeric( $values[ self::FIELD_LENGTH ] ) ? (float) number_format( $values[ self::FIELD_LENGTH ], 0, '.', '' ) : null ), |
| 288 |
self::FIELD_HEIGHT => ( is_numeric( $values[ self::FIELD_HEIGHT ] ) ? (float) number_format( $values[ self::FIELD_HEIGHT ], 0, '.', '' ) : null ), |
| 289 |
]; |
| 290 |
|
| 291 |
if ( $values[ Checkout::ATTR_POINT_ID ] && $order->isPickupPointDelivery() ) { |
| 292 |
foreach ( Checkout::$pickupPointAttrs as $pickupPointAttr ) { |
| 293 |
$value = $values[ $pickupPointAttr['name'] ]; |
| 294 |
|
| 295 |
if ( Checkout::ATTR_CARRIER_ID === $pickupPointAttr['name'] ) { |
| 296 |
$value = ( ! empty( $values[ Checkout::ATTR_CARRIER_ID ] ) ? $values[ Checkout::ATTR_CARRIER_ID ] : \Packetery\Module\Carrier\Repository::INTERNAL_PICKUP_POINTS_ID ); |
| 297 |
} |
| 298 |
|
| 299 |
$propsToSave[ $pickupPointAttr['name'] ] = $value; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
$orderSize = $order->getSize(); |
| 304 |
if ( null === $orderSize ) { |
| 305 |
$orderSize = new Core\Entity\Size(); |
| 306 |
} |
| 307 |
|
| 308 |
foreach ( $propsToSave as $attrName => $attrValue ) { |
| 309 |
switch ( $attrName ) { |
| 310 |
case self::FIELD_WEIGHT: |
| 311 |
$order->setWeight( $attrValue ); |
| 312 |
break; |
| 313 |
case self::FIELD_WIDTH: |
| 314 |
$orderSize->setWidth( $attrValue ); |
| 315 |
break; |
| 316 |
case self::FIELD_LENGTH: |
| 317 |
$orderSize->setLength( $attrValue ); |
| 318 |
break; |
| 319 |
case self::FIELD_HEIGHT: |
| 320 |
$orderSize->setHeight( $attrValue ); |
| 321 |
break; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
$order->setSize( $orderSize ); |
| 326 |
Checkout::updateOrderEntityFromPropsToSave( $order, $propsToSave ); |
| 327 |
$this->orderRepository->save( $order ); |
| 328 |
|
| 329 |
return $orderId; |
| 330 |
} |
| 331 |
} |
| 332 |
|