PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.5
Elementor Website Builder – more than just a page builder v4.1.5
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / utils / version.php

version.php in Elementor Website Builder – more than just a page builder 4.1.5, at core/utils/version.php

185 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Utils;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 class Version {
9 const PART_MAJOR_1 = 'major1';
10 const PART_MAJOR_2 = 'major2';
11 const PART_PATCH = 'patch';
12 const PART_STAGE = 'stage';
13
14 /**
15 * First number of a version 0.x.x
16 *
17 * @var string
18 */
19 public $major1;
20
21 /**
22 * Second number of a version x.0.x
23 *
24 * @var string
25 */
26 public $major2;
27
28 /**
29 * Third number of a version x.x.0
30 *
31 * @var string
32 */
33 public $patch;
34
35 /**
36 * The stage of a version x.x.x-stage.
37 * e.g: x.x.x-dev1, x.x.x-beta3, x.x.x-rc
38 *
39 * @var string|null
40 */
41 public $stage;
42
43 /**
44 * Version constructor.
45 *
46 * @param $major1
47 * @param $major2
48 * @param $patch
49 * @param $stage
50 */
51 public function __construct( $major1, $major2, $patch, $stage = null ) {
52 $this->major1 = $major1;
53 $this->major2 = $major2;
54 $this->patch = $patch;
55 $this->stage = $stage;
56 }
57
58 /**
59 * Create Version instance.
60 *
61 * @param string $major1
62 * @param string $major2
63 * @param string $patch
64 * @param null $stage
65 *
66 * @return static
67 */
68 public static function create( $major1 = '0', $major2 = '0', $patch = '0', $stage = null ) {
69 return new static( $major1, $major2, $patch, $stage );
70 }
71
72 /**
73 * Checks if the current version string is valid.
74 *
75 * @param $version
76 *
77 * @return bool
78 */
79 public static function is_valid_version( $version ) {
80 return (bool) preg_match( '/^(\d+\.)?(\d+\.)?(\*|\d+)(-.+)?$/', $version );
81 }
82
83 /**
84 * Creates a Version instance from a string.
85 *
86 * @param $version
87 * @param bool $should_validate
88 *
89 * @return static
90 * @throws \Exception If version comparison fails or invalid version format is provided.
91 */
92 public static function create_from_string( $version, $should_validate = true ) {
93 if ( $should_validate && ! static::is_valid_version( $version ) ) {
94 throw new \Exception( sprintf( '%s is an invalid version.', esc_html( $version ) ) );
95 }
96
97 $parts = explode( '.', $version );
98 $patch_parts = [];
99
100 $major1 = '0';
101 $major2 = '0';
102 $patch = '0';
103 $stage = null;
104
105 if ( isset( $parts[0] ) ) {
106 $major1 = $parts[0];
107 }
108
109 if ( isset( $parts[1] ) ) {
110 $major2 = $parts[1];
111 }
112
113 if ( isset( $parts[2] ) ) {
114 $patch_parts = explode( '-', $parts[2] );
115
116 $patch = $patch_parts[0];
117 }
118
119 if ( isset( $patch_parts[1] ) ) {
120 $stage = $patch_parts[1];
121 }
122
123 return static::create( $major1, $major2, $patch, $stage );
124 }
125
126 /**
127 * Compare the current version instance with another version.
128 *
129 * @param $operator
130 * @param $version
131 * @param string $part
132 *
133 * @return bool
134 * @throws \Exception If version validation fails or parsing errors occur.
135 */
136 public function compare( $operator, $version, $part = self::PART_STAGE ) {
137 if ( ! ( $version instanceof Version ) ) {
138 if ( ! static::is_valid_version( $version ) ) {
139 $version = '0.0.0';
140 }
141
142 $version = static::create_from_string( $version, false );
143 }
144
145 $current_version = clone $this;
146 $compare_version = clone $version;
147
148 if ( in_array( $part, [ self::PART_PATCH, self::PART_MAJOR_2, self::PART_MAJOR_1 ], true ) ) {
149 $current_version->stage = null;
150 $compare_version->stage = null;
151 }
152
153 if ( in_array( $part, [ self::PART_MAJOR_2, self::PART_MAJOR_1 ], true ) ) {
154 $current_version->patch = '0';
155 $compare_version->patch = '0';
156 }
157
158 if ( self::PART_MAJOR_1 === $part ) {
159 $current_version->major2 = '0';
160 $compare_version->major2 = '0';
161 }
162
163 return version_compare(
164 $current_version,
165 $compare_version,
166 $operator
167 );
168 }
169
170 /**
171 * Implode the version and return it as string.
172 *
173 * @return string
174 */
175 public function __toString() {
176 $version = implode( '.', [ $this->major1, $this->major2, $this->patch ] );
177
178 if ( $this->stage ) {
179 $version .= '-' . $this->stage;
180 }
181
182 return $version;
183 }
184 }
185