| 1 |
<?php |
| 2 |
/** |
| 3 |
* Main Packeta plugin class. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module; |
| 11 |
|
| 12 |
use Packetery\Module\Hooks\HookRegistrar; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Plugin |
| 16 |
* |
| 17 |
* @package Packetery |
| 18 |
*/ |
| 19 |
class Plugin { |
| 20 |
|
| 21 |
public const VERSION = '2.0.9'; |
| 22 |
public const DOMAIN = 'packeta'; |
| 23 |
public const PARAM_PACKETERY_ACTION = 'packetery_action'; |
| 24 |
public const PARAM_NONCE = '_wpnonce'; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var HookRegistrar |
| 28 |
*/ |
| 29 |
private $hookRegistrar; |
| 30 |
|
| 31 |
public function __construct( HookRegistrar $hookRegistrar ) { |
| 32 |
$this->hookRegistrar = $hookRegistrar; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Gets list of multisite sites. |
| 37 |
* |
| 38 |
* @return array |
| 39 |
*/ |
| 40 |
public static function getSites(): array { |
| 41 |
return get_sites( |
| 42 |
[ |
| 43 |
'fields' => 'ids', |
| 44 |
'number' => 0, |
| 45 |
'update_site_cache' => false, |
| 46 |
] |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Method to register hooks |
| 52 |
*/ |
| 53 |
public function run(): void { |
| 54 |
$this->hookRegistrar->register(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Uninstalls plugin and drops custom database table. |
| 59 |
* Only a static class method or function can be used in an uninstall hook. |
| 60 |
*/ |
| 61 |
public static function uninstall(): void { |
| 62 |
if ( defined( 'PACKETERY_DEBUG' ) && constant( 'PACKETERY_DEBUG' ) === true ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
if ( is_multisite() ) { |
| 67 |
self::cleanUpRepositoriesForMultisite(); |
| 68 |
} else { |
| 69 |
self::cleanUpRepositories(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Drops all plugin tables when Multisite is enabled. |
| 75 |
* |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
private static function cleanUpRepositoriesForMultisite(): void { |
| 79 |
$sites = self::getSites(); |
| 80 |
|
| 81 |
foreach ( $sites as $site ) { |
| 82 |
switch_to_blog( $site ); |
| 83 |
self::cleanUpRepositories(); |
| 84 |
restore_current_blog(); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Drops all plugin tables for a single site. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
private static function cleanUpRepositories(): void { |
| 94 |
$container = require PACKETERY_PLUGIN_DIR . '/bootstrap.php'; |
| 95 |
|
| 96 |
$optionsRepository = $container->getByType( Options\Repository::class ); |
| 97 |
$pluginOptions = $optionsRepository->getPluginOptions(); |
| 98 |
foreach ( $pluginOptions as $option ) { |
| 99 |
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 100 |
delete_option( $option->option_name ); |
| 101 |
} |
| 102 |
|
| 103 |
$logRepository = $container->getByType( Log\Repository::class ); |
| 104 |
$logRepository->drop(); |
| 105 |
|
| 106 |
$carrierRepository = $container->getByType( Carrier\Repository::class ); |
| 107 |
$carrierRepository->drop(); |
| 108 |
|
| 109 |
$orderRepository = $container->getByType( Order\Repository::class ); |
| 110 |
$orderRepository->drop(); |
| 111 |
|
| 112 |
$customsDeclarationItemsRepository = $container->getByType( CustomsDeclaration\Repository::class ); |
| 113 |
$customsDeclarationItemsRepository->dropItems(); |
| 114 |
|
| 115 |
$customsDeclarationRepository = $container->getByType( CustomsDeclaration\Repository::class ); |
| 116 |
$customsDeclarationRepository->drop(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Hides submenu item. Must not be called too early. |
| 121 |
* |
| 122 |
* @param string $itemSlug Item slug. |
| 123 |
*/ |
| 124 |
public static function hideSubmenuItem( string $itemSlug ): void { |
| 125 |
global $submenu; |
| 126 |
if ( isset( $submenu[ Options\Page::SLUG ] ) ) { |
| 127 |
foreach ( $submenu[ Options\Page::SLUG ] as $key => $menu ) { |
| 128 |
if ( $itemSlug === $menu[2] ) { |
| 129 |
unset( $submenu[ Options\Page::SLUG ][ $key ] ); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Gets software identity for Packeta APIs. |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
public static function getAppIdentity(): string { |
| 141 |
return 'WordPress-' . get_bloginfo( 'version' ) . '-Woocommerce-' . WC_VERSION . '-Packeta-' . self::VERSION; |
| 142 |
} |
| 143 |
} |
| 144 |
|