/var/www/html_uk/wp-content/plugins/automatewoo/includes/Workflows/Status.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
<?php

namespace AutomateWoo\Workflows;

use 
AutomateWoo\Exceptions\InvalidStatus;

/**
 * Class Status
 *
 * @since 5.1.0
 */
final class Status {

    const 
ACTIVE   'active';
    const 
DISABLED 'disabled';

    const 
POST_ACTIVE   'publish';
    const 
POST_DISABLED 'aw-disabled';

    
/**
     * The equivalent post status.
     *
     * @var string
     */
    
private $post_status;

    
/**
     * A Workflow status.
     *
     * @var string
     */
    
private $status;

    
/**
     * Status constructor.
     *
     * @param string $status The workflow status.
     *
     * @throws InvalidStatus When the status is not a known workflow status.
     */
    
public function __constructstring $status ) {
        
$this->validate_status$status );
        
$this->status      $status;
        
$this->post_status $this->get_available_statuses()[ $status ];
    }

    
/**
     * Get the post status.
     *
     * @return string
     */
    
public function get_post_status(): string {
        return 
$this->post_status;
    }

    
/**
     * Get the workflow status.
     *
     * @return string
     */
    
public function get_status(): string {
        return 
$this->status;
    }

    
/**
     * Change the workflow status.
     *
     * Will return a new object.
     *
     * @param string $new_status The new workflow status.
     *
     * @return Status
     */
    
public function change_status_tostring $new_status ): Status {
        return new 
self$new_status );
    }

    
/**
     * Validate a workflow status.
     *
     * @param string $status The status to validate.
     *
     * @throws InvalidStatus When the status is not a known workflow status.
     */
    
private function validate_statusstring $status ) {
        
$available_statuses $this->get_available_statuses();
        if ( ! 
array_key_exists$status$available_statuses ) ) {
            throw 
InvalidStatus::unknown_statusesc_html$status ) );
        }

        if ( ! 
is_string$available_statuses$status ] ) || empty( $available_statuses$status ] ) ) {
            throw 
InvalidStatus::no_post_stausesc_html$status ) );
        }
    }

    
/**
     * Get the available workflow statuses and their post status mapping.
     *
     * @return array
     */
    
private function get_available_statuses(): array {
        
$additional_statuses apply_filters'automatewoo/workflow/statuses', [] );

        return 
array_merge(
            
$additional_statuses,
            [
                
self::ACTIVE   => self::POST_ACTIVE,
                
self::DISABLED => self::POST_DISABLED,
            ]
        );
    }
}