Package.php
130 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Domain; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Options; |
| 5 | use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating; |
| 6 | |
| 7 | |
| 8 | /** |
| 9 | * Main package class. |
| 10 | * |
| 11 | * Returns information about the package and handles init. |
| 12 | * |
| 13 | * @since 2.5.0 |
| 14 | */ |
| 15 | class Package { |
| 16 | |
| 17 | /** |
| 18 | * Holds the current version of the blocks plugin. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | private $version; |
| 23 | |
| 24 | /** |
| 25 | * Holds the main path to the blocks plugin directory. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | private $path; |
| 30 | |
| 31 | /** |
| 32 | * Holds locally the plugin_dir_url to avoid recomputing it. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | private $plugin_dir_url; |
| 37 | |
| 38 | /** |
| 39 | * Holds the feature gating class instance. |
| 40 | * |
| 41 | * @var FeatureGating |
| 42 | */ |
| 43 | private $feature_gating; |
| 44 | |
| 45 | /** |
| 46 | * Constructor |
| 47 | * |
| 48 | * @param string $version Version of the plugin. |
| 49 | * @param string $plugin_path Path to the main plugin file. |
| 50 | * @param FeatureGating $deprecated Deprecated Feature gating class. |
| 51 | */ |
| 52 | public function __construct( $version, $plugin_path, $deprecated = null ) { |
| 53 | if ( null !== $deprecated ) { |
| 54 | wc_deprecated_argument( 'FeatureGating', '9.6', 'FeatureGating class is deprecated, please use wp_get_environment_type() instead.' ); |
| 55 | $this->feature_gating = new FeatureGating(); |
| 56 | } |
| 57 | $this->version = $version; |
| 58 | $this->path = $plugin_path; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the version of WooCommerce Blocks. |
| 63 | * |
| 64 | * Note: since Blocks was merged into WooCommerce Core, the version of |
| 65 | * WC Blocks doesn't update anymore. Use |
| 66 | * `Constants::get_constant( 'WC_VERSION' )` when possible to get the |
| 67 | * WooCommerce Core version. |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public function get_version() { |
| 72 | return $this->version; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the version of WooCommerce Blocks stored in the database. |
| 77 | * |
| 78 | * @return string |
| 79 | */ |
| 80 | public function get_version_stored_on_db() { |
| 81 | return get_option( Options::WC_BLOCK_VERSION, '' ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Sets the version of WooCommerce Blocks in the database. |
| 86 | * This is useful during the first installation or after the upgrade process. |
| 87 | */ |
| 88 | public function set_version_stored_on_db() { |
| 89 | update_option( Options::WC_BLOCK_VERSION, $this->get_version() ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the path to the plugin directory. |
| 94 | * |
| 95 | * @param string $relative_path If provided, the relative path will be |
| 96 | * appended to the plugin path. |
| 97 | * |
| 98 | * @return string |
| 99 | */ |
| 100 | public function get_path( $relative_path = '' ) { |
| 101 | return trailingslashit( $this->path ) . $relative_path; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the url to the blocks plugin directory. |
| 106 | * |
| 107 | * @param string $relative_url If provided, the relative url will be |
| 108 | * appended to the plugin url. |
| 109 | * |
| 110 | * @return string |
| 111 | */ |
| 112 | public function get_url( $relative_url = '' ) { |
| 113 | if ( ! $this->plugin_dir_url ) { |
| 114 | // Append index.php so WP does not return the parent directory. |
| 115 | $this->plugin_dir_url = plugin_dir_url( $this->path . '/index.php' ); |
| 116 | } |
| 117 | |
| 118 | return $this->plugin_dir_url . $relative_url; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns an instance of the FeatureGating class. |
| 123 | * |
| 124 | * @return FeatureGating |
| 125 | */ |
| 126 | public function feature() { |
| 127 | return $this->feature_gating; |
| 128 | } |
| 129 | } |
| 130 |