edition = $edition; $this->channel = $channel; $this->access_mode = $access_mode; $this->package_slug = $package_slug; $this->package_name = $package_name; $this->plugin_file = $plugin_file; } public static function from_runtime(): self { $has_release_markers = defined( 'IPSCMI_ORDER_CART_RELEASE_PACKAGE_SLUG' ) || defined( 'IPSCMI_ORDER_CART_RELEASE_PACKAGE_NAME' ) || defined( 'IPSCMI_ORDER_CART_RELEASE_PLUGIN_FILE' ) || defined( 'IPSCMI_ORDER_CART_RELEASE_ACCESS_MODE' ) || defined( 'IPSCMI_ORDER_CART_RELEASE_CHANNEL' ); $edition = defined( 'IPSCMI_ORDER_CART_EDITION' ) ? (string) IPSCMI_ORDER_CART_EDITION : self::environment_value( 'IPSCMI_ORDER_CART_LOCAL_EDITION' ); if ( '' === $edition ) { $edition = $has_release_markers ? self::EDITION_CORE : self::EDITION_FULL; } $channel = defined( 'IPSCMI_ORDER_CART_RELEASE_CHANNEL' ) ? (string) IPSCMI_ORDER_CART_RELEASE_CHANNEL : self::environment_value( 'IPSCMI_ORDER_CART_LOCAL_CHANNEL' ); if ( '' === $channel ) { $channel = $has_release_markers ? self::CHANNEL_IPS : self::CHANNEL_DEVELOPMENT; } $access_mode = defined( 'IPSCMI_ORDER_CART_RELEASE_ACCESS_MODE' ) ? (string) IPSCMI_ORDER_CART_RELEASE_ACCESS_MODE : self::environment_value( 'IPSCMI_ORDER_CART_LOCAL_ACCESS_MODE' ); if ( '' === $access_mode ) { $access_mode = $has_release_markers ? self::ACCESS_PUBLIC : self::ACCESS_INTERNAL; } $edition = self::normalize( $edition, array( self::EDITION_CORE, self::EDITION_PRO, self::EDITION_FULL ), self::EDITION_CORE ); $channel = self::normalize( $channel, array( self::CHANNEL_DEVELOPMENT, self::CHANNEL_IPS, self::CHANNEL_WORDPRESS_ORG ), self::CHANNEL_IPS ); $access_mode = self::normalize( $access_mode, array( self::ACCESS_INTERNAL, self::ACCESS_PUBLIC, self::ACCESS_LICENSED ), self::ACCESS_PUBLIC ); // WordPress.org packages are always free Core packages. Fail closed when // inconsistent constants are supplied by a hand-built artifact. if ( self::CHANNEL_WORDPRESS_ORG === $channel ) { $edition = self::EDITION_CORE; $access_mode = self::ACCESS_PUBLIC; } $package_slug = defined( 'IPSCMI_ORDER_CART_RELEASE_PACKAGE_SLUG' ) ? self::sanitize_slug( (string) IPSCMI_ORDER_CART_RELEASE_PACKAGE_SLUG ) : 'ips-order-cart'; $package_name = defined( 'IPSCMI_ORDER_CART_RELEASE_PACKAGE_NAME' ) ? sanitize_text_field( (string) IPSCMI_ORDER_CART_RELEASE_PACKAGE_NAME ) : 'IPS Cart&Checkout for WooCommerce'; $plugin_file = defined( 'IPSCMI_ORDER_CART_RELEASE_PLUGIN_FILE' ) ? self::sanitize_plugin_file( (string) IPSCMI_ORDER_CART_RELEASE_PLUGIN_FILE ) : 'ips-order-cart/ips-order-cart.php'; return new self( $edition, $channel, $access_mode, $package_slug, $package_name, $plugin_file ); } public function edition(): string { return $this->edition; } public function channel(): string { return $this->channel; } public function access_mode(): string { return $this->access_mode; } public function package_slug(): string { return $this->package_slug; } public function package_name(): string { return $this->package_name; } public function plugin_file(): string { return $this->plugin_file; } public function includes_pro(): bool { return in_array( $this->edition, array( self::EDITION_PRO, self::EDITION_FULL ), true ); } public function is_wordpress_org(): bool { return self::CHANNEL_WORDPRESS_ORG === $this->channel; } public function uses_ips_distribution(): bool { return self::CHANNEL_IPS === $this->channel; } public function requires_license(): bool { return $this->uses_ips_distribution() && self::ACCESS_LICENSED === $this->access_mode && $this->includes_pro(); } /** * @return array */ public function to_array(): array { return array( 'edition' => $this->edition, 'channel' => $this->channel, 'accessMode' => $this->access_mode, 'packageSlug' => $this->package_slug, 'packageName' => $this->package_name, 'pluginFile' => $this->plugin_file, ); } private static function environment_value( string $name ): string { if ( defined( $name ) ) { return (string) constant( $name ); } $value = getenv( $name ); return is_string( $value ) ? $value : ''; } /** * @param string[] $allowed */ private static function normalize( string $value, array $allowed, string $fallback ): string { $value = function_exists( 'sanitize_key' ) ? sanitize_key( $value ) : (string) preg_replace( '/[^a-z0-9_-]/', '', strtolower( $value ) ); return in_array( $value, $allowed, true ) ? $value : $fallback; } private static function sanitize_slug( string $value ): string { $value = function_exists( 'sanitize_key' ) ? sanitize_key( $value ) : (string) preg_replace( '/[^a-z0-9_-]/', '', strtolower( $value ) ); return '' !== $value ? $value : 'ips-order-cart'; } private static function sanitize_plugin_file( string $value ): string { $value = wp_normalize_path( trim( $value ) ); $value = ltrim( $value, '/' ); if ( false !== strpos( $value, '..' ) || ! preg_match( '#^[a-z0-9._-]+/[a-z0-9._-]+\.php$#i', $value ) ) { return 'ips-order-cart/ips-order-cart.php'; } return $value; } }