| 1 |
<?php |
| 2 |
/** |
| 3 |
* WidgetIntegration. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Packetery\Module\Blocks; |
| 9 |
|
| 10 |
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface; |
| 11 |
use Packetery\Module\Plugin; |
| 12 |
|
| 13 |
if ( file_exists( WP_PLUGIN_DIR . '/woocommerce/src/Blocks/Integrations/IntegrationInterface.php' ) ) { |
| 14 |
require_once WP_PLUGIN_DIR . '/woocommerce/src/Blocks/Integrations/IntegrationInterface.php'; |
| 15 |
} |
| 16 |
|
| 17 |
if ( file_exists( WP_PLUGIN_DIR . '/woocommerce/packages/woocommerce-blocks/src/Integrations/IntegrationInterface.php' ) ) { |
| 18 |
require_once WP_PLUGIN_DIR . '/woocommerce/packages/woocommerce-blocks/src/Integrations/IntegrationInterface.php'; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* WidgetIntegration. |
| 23 |
* |
| 24 |
* @package Packetery |
| 25 |
*/ |
| 26 |
class WidgetIntegration implements IntegrationInterface { |
| 27 |
const INTEGRATION_NAME = 'packeta-widget'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Settings. |
| 31 |
* |
| 32 |
* @var array<string, mixed> |
| 33 |
*/ |
| 34 |
public $settings; |
| 35 |
|
| 36 |
/** |
| 37 |
* Constructor. |
| 38 |
* |
| 39 |
* @param array $settings Settings. |
| 40 |
*/ |
| 41 |
public function __construct( array $settings ) { |
| 42 |
$this->settings = $settings; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Gets name. |
| 47 |
* |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
public function get_name(): string { |
| 51 |
return self::INTEGRATION_NAME; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Initializes. |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function initialize(): void { |
| 60 |
$scriptPath = '/packeta/public/block/index.js'; |
| 61 |
$scriptAssetPath = PACKETERY_PLUGIN_DIR . '/public/block/index.asset.php'; |
| 62 |
$scriptAsset = file_exists( $scriptAssetPath ) |
| 63 |
? require $scriptAssetPath |
| 64 |
: [ |
| 65 |
'dependencies' => [], |
| 66 |
'version' => $this->get_file_version( $scriptPath ), |
| 67 |
]; |
| 68 |
|
| 69 |
wp_register_script( |
| 70 |
self::INTEGRATION_NAME, |
| 71 |
plugins_url( $scriptPath ), |
| 72 |
$scriptAsset['dependencies'], |
| 73 |
$scriptAsset['version'], |
| 74 |
true |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Gets script handles. |
| 80 |
* |
| 81 |
* @return string[] |
| 82 |
*/ |
| 83 |
public function get_script_handles() { |
| 84 |
return [ self::INTEGRATION_NAME ]; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Gets editor script handles. |
| 89 |
* |
| 90 |
* @return string[] |
| 91 |
*/ |
| 92 |
public function get_editor_script_handles(): array { |
| 93 |
return [ self::INTEGRATION_NAME ]; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Gets script data. |
| 98 |
* |
| 99 |
* @return array<string, mixed> |
| 100 |
*/ |
| 101 |
public function get_script_data(): array { |
| 102 |
return $this->settings; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Gets file version. |
| 107 |
* |
| 108 |
* @param string $filePath File path. |
| 109 |
* |
| 110 |
* @return bool|int|string |
| 111 |
*/ |
| 112 |
protected function get_file_version( string $filePath ) { |
| 113 |
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $filePath ) ) { |
| 114 |
return filemtime( $filePath ); |
| 115 |
} |
| 116 |
|
| 117 |
return Plugin::VERSION; |
| 118 |
} |
| 119 |
} |
| 120 |
|