/var/www/html_us/wp-content/plugins/elementor/core/utils/version.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace Elementor\Core\Utils;

if ( ! 
defined'ABSPATH' ) ) {
    exit; 
// Exit if accessed directly.
}

class 
Version {
    const 
PART_MAJOR_1 'major1';
    const 
PART_MAJOR_2 'major2';
    const 
PART_PATCH 'patch';
    const 
PART_STAGE 'stage';

    
/**
     * First number of a version 0.x.x
     *
     * @var string
     */
    
public $major1;

    
/**
     * Second number of a version x.0.x
     *
     * @var string
     */
    
public $major2;

    
/**
     * Third number of a version x.x.0
     *
     * @var string
     */
    
public $patch;

    
/**
     * The stage of a version x.x.x-stage.
     * e.g: x.x.x-dev1, x.x.x-beta3, x.x.x-rc
     *
     * @var string|null
     */
    
public $stage;

    
/**
     * Version constructor.
     *
     * @param $major1
     * @param $major2
     * @param $patch
     * @param $stage
     */
    
public function __construct$major1$major2$patch$stage null ) {
        
$this->major1 $major1;
        
$this->major2 $major2;
        
$this->patch  $patch;
        
$this->stage  $stage;
    }

    
/**
     * Create Version instance.
     *
     * @param string $major1
     * @param string $major2
     * @param string $patch
     * @param null   $stage
     *
     * @return static
     */
    
public static function create$major1 '0'$major2 '0'$patch '0'$stage null ) {
        return new static( 
$major1$major2$patch$stage );
    }

    
/**
     * Checks if the current version string is valid.
     *
     * @param $version
     *
     * @return bool
     */
    
public static function is_valid_version$version ) {
        return ! ! 
preg_match'/^(\d+\.)?(\d+\.)?(\*|\d+)(-.+)?$/'$version );
    }

    
/**
     * Creates a Version instance from a string.
     *
     * @param      $version
     * @param bool $should_validate
     *
     * @return static
     * @throws \Exception
     */
    
public static function create_from_string$version$should_validate true ) {
        if ( 
$should_validate && ! static::is_valid_version$version ) ) {
            throw new 
\Exception"{$version} is an invalid version." );
        }

        
$parts explode'.'$version );
        
$patch_parts = [];

        
$major1 '0';
        
$major2 '0';
        
$patch '0';
        
$stage null;

        if ( isset( 
$parts[0] ) ) {
            
$major1 $parts[0];
        }

        if ( isset( 
$parts[1] ) ) {
            
$major2 $parts[1];
        }

        if ( isset( 
$parts[2] ) ) {
            
$patch_parts explode'-'$parts[2] );

            
$patch $patch_parts[0];
        }

        if ( isset( 
$patch_parts[1] ) ) {
            
$stage $patch_parts[1];
        }

        return static::
create$major1$major2$patch$stage );
    }

    
/**
     * Compare the current version instance with another version.
     *
     * @param        $operator
     * @param        $version
     * @param string $part
     *
     * @return bool
     * @throws \Exception
     */
    
public function compare$operator$version$part self::PART_STAGE ) {
        if ( ! ( 
$version instanceof Version ) ) {
            if ( ! static::
is_valid_version$version ) ) {
                
$version '0.0.0';
            }

            
$version = static::create_from_string$versionfalse );
        }

        
$current_version = clone $this;
        
$compare_version = clone $version;

        if ( 
in_array$part, [ self::PART_PATCHself::PART_MAJOR_2self::PART_MAJOR_1 ], true ) ) {
            
$current_version->stage null;
            
$compare_version->stage null;
        }

        if ( 
in_array$part, [ self::PART_MAJOR_2self::PART_MAJOR_1 ], true ) ) {
            
$current_version->patch '0';
            
$compare_version->patch '0';
        }

        if ( 
self::PART_MAJOR_1 === $part ) {
            
$current_version->major2 '0';
            
$compare_version->major2 '0';
        }

        return 
version_compare(
            
$current_version,
            
$compare_version,
            
$operator
        
);
    }

    
/**
     * Implode the version and return it as string.
     *
     * @return string
     */
    
public function __toString() {
        
$version implode'.', [ $this->major1$this->major2$this->patch ] );

        if ( 
$this->stage ) {
            
$version .= '-' $this->stage;
        }

        return 
$version;
    }
}