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