| 1 |
<?php |
| 2 |
|
| 3 |
namespace Packetery\Module\Hooks; |
| 4 |
|
| 5 |
use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 6 |
use Packetery\Module\Framework\WcAdapter; |
| 7 |
use Packetery\Module\Framework\WpAdapter; |
| 8 |
use Packetery\Module\ModuleHelper; |
| 9 |
use Packetery\Module\Options; |
| 10 |
use Packetery\Module\Order; |
| 11 |
use Packetery\Module\Plugin; |
| 12 |
use Packetery\Nette\Http\Request; |
| 13 |
|
| 14 |
class PluginHooks { |
| 15 |
|
| 16 |
/** |
| 17 |
* @var Request |
| 18 |
*/ |
| 19 |
private $request; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var Order\PacketSubmitter |
| 23 |
*/ |
| 24 |
private $packetSubmitter; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var Order\PacketCanceller |
| 28 |
*/ |
| 29 |
private $packetCanceller; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var Order\PacketClaimSubmitter |
| 33 |
*/ |
| 34 |
private $packetClaimSubmitter; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var ModuleHelper |
| 38 |
*/ |
| 39 |
private $moduleHelper; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var WpAdapter |
| 43 |
*/ |
| 44 |
private $wpAdapter; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var WcAdapter |
| 48 |
*/ |
| 49 |
private $wcAdapter; |
| 50 |
|
| 51 |
public function __construct( |
| 52 |
Request $request, |
| 53 |
Order\PacketSubmitter $packetSubmitter, |
| 54 |
Order\PacketCanceller $packetCanceller, |
| 55 |
Order\PacketClaimSubmitter $packetClaimSubmitter, |
| 56 |
ModuleHelper $moduleHelper, |
| 57 |
WpAdapter $wpAdapter, |
| 58 |
WcAdapter $wcAdapter |
| 59 |
) { |
| 60 |
$this->request = $request; |
| 61 |
$this->packetSubmitter = $packetSubmitter; |
| 62 |
$this->packetCanceller = $packetCanceller; |
| 63 |
$this->packetClaimSubmitter = $packetClaimSubmitter; |
| 64 |
$this->moduleHelper = $moduleHelper; |
| 65 |
$this->wpAdapter = $wpAdapter; |
| 66 |
$this->wcAdapter = $wcAdapter; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Declares plugin compability with features. |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function declareWooCommerceCompability(): void { |
| 75 |
if ( class_exists( FeaturesUtil::class ) === false ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
// High-Performance Order Storage. |
| 80 |
$this->wcAdapter->featuresUtilDeclareCompatibility( 'custom_order_tables', ModuleHelper::getPluginMainFilePath() ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Loads plugin translation file by user locale. |
| 85 |
*/ |
| 86 |
public function loadTranslation(): void { |
| 87 |
$domain = Plugin::DOMAIN; |
| 88 |
$this->wpAdapter->unloadTextDomain( $domain, true ); |
| 89 |
$locale = $this->moduleHelper->getLocale(); |
| 90 |
$moFile = WP_LANG_DIR . "/plugins/$domain-$locale.mo"; |
| 91 |
|
| 92 |
if ( file_exists( $moFile ) ) { |
| 93 |
$this->wpAdapter->loadPluginTextDomain( $domain ); |
| 94 |
} else { |
| 95 |
$this->wpAdapter->loadDefaultTextDomain(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Adds action links visible at the plugin screen. |
| 101 |
* |
| 102 |
* @link https://developer.wordpress.org/reference/hooks/plugin_action_links_plugin_file/ |
| 103 |
* |
| 104 |
* @param array $links Plugin Action links. |
| 105 |
* |
| 106 |
* @return array |
| 107 |
*/ |
| 108 |
public function addPluginActionLinks( array $links ): array { |
| 109 |
$settingsLink = '<a href="' . $this->wpAdapter->escUrl( $this->wpAdapter->adminUrl( 'admin.php?page=' . Options\Page::SLUG ) ) . '" aria-label="' . |
| 110 |
$this->wpAdapter->escAttr( $this->wpAdapter->__( 'View the plugin documentation', 'packeta' ) ) . '">' . |
| 111 |
$this->wpAdapter->escHtml( $this->wpAdapter->__( 'Settings', 'packeta' ) ) . '</a>'; |
| 112 |
|
| 113 |
array_unshift( $links, $settingsLink ); |
| 114 |
|
| 115 |
if ( isset( $links['deactivate'] ) && is_string( $links['deactivate'] ) === true ) { |
| 116 |
$links['deactivate'] = str_replace( |
| 117 |
'<a ', |
| 118 |
'<a class="js-packetery-deactivate" ', |
| 119 |
$links['deactivate'] |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
return $links; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Adds links to the plugin list screen. |
| 128 |
* |
| 129 |
* @link https://developer.wordpress.org/reference/hooks/plugin_row_meta/ |
| 130 |
* |
| 131 |
* @param array $links Plugin Row Meta. |
| 132 |
* @param string $pluginFileName Plugin Base file. |
| 133 |
* |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
public function addPluginRowMeta( array $links, string $pluginFileName ): array { |
| 137 |
if ( strpos( $pluginFileName, basename( ModuleHelper::getPluginMainFilePath() ) ) === false ) { |
| 138 |
return $links; |
| 139 |
} |
| 140 |
$links[] = '<a href="' . $this->wpAdapter->escUrl( 'https://github.com/Zasilkovna/WooCommerce/wiki' ) . '" aria-label="' . |
| 141 |
$this->wpAdapter->escAttr( $this->wpAdapter->__( 'View Packeta documentation', 'packeta' ) ) . '">' . |
| 142 |
$this->wpAdapter->escHtml( $this->wpAdapter->__( 'Documentation', 'packeta' ) ) . '</a>'; |
| 143 |
|
| 144 |
return $links; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Check for action parameter and process wanted action. |
| 149 |
* |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
public function handleActions(): void { |
| 153 |
$action = $this->request->getQuery( Plugin::PARAM_PACKETERY_ACTION ); |
| 154 |
|
| 155 |
if ( $action === Order\PacketActionsCommonLogic::ACTION_SUBMIT_PACKET ) { |
| 156 |
$this->packetSubmitter->processAction(); |
| 157 |
} |
| 158 |
|
| 159 |
if ( $action === Order\PacketActionsCommonLogic::ACTION_SUBMIT_PACKET_CLAIM ) { |
| 160 |
$this->packetClaimSubmitter->processAction(); |
| 161 |
} |
| 162 |
|
| 163 |
if ( $action === Order\PacketActionsCommonLogic::ACTION_CANCEL_PACKET ) { |
| 164 |
$this->packetCanceller->processAction(); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|