/var/www/html_us/wp-content/plugins/woocommerce/src/Internal/Admin/ProductForm/Component.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
<?php
/**
 * Abstract class for product form components.
 */

namespace Automattic\WooCommerce\Internal\Admin\ProductForm;

/**
 * Component class.
 */
abstract class Component {
    
/**
     * Product Component traits.
     */
    
use ComponentTrait;

    
/**
     * Component additional arguments.
     *
     * @var array
     */
    
protected $additional_args;

    
/**
     * Constructor
     *
     * @param string $id Component id.
     * @param string $plugin_id Plugin id.
     * @param array  $additional_args Array containing additional arguments.
     */
    
public function __construct$id$plugin_id$additional_args ) {
        
$this->id              $id;
        
$this->plugin_id       $plugin_id;
        
$this->additional_args $additional_args;
    }

    
/**
     * Component arguments.
     *
     * @return array
     */
    
public function get_additional_args() {
        return 
$this->additional_args;
    }

    
/**
     * Component arguments.
     *
     * @param string $key key of argument.
     * @return mixed
     */
    
public function get_additional_argument$key ) {
        return 
self::get_argument_from_path$this->additional_args$key );
    }

    
/**
     * Get the component as JSON.
     *
     * @return array
     */
    
public function get_json() {
        return 
array_merge(
            array(
                
'id'        => $this->get_id(),
                
'plugin_id' => $this->get_plugin_id(),
            ),
            
$this->get_additional_args()
        );
    }

    
/**
     * Sorting function for product form component.
     *
     * @param Component $a Component a.
     * @param Component $b Component b.
     * @param array     $sort_by key and order to sort by.
     * @return int
     */
    
public static function sort$a$b$sort_by = array() ) {
        
$key   $sort_by['key'];
        
$a_val $a->get_additional_argument$key );
        
$b_val $b->get_additional_argument$key );
        if ( 
'asc' === $sort_by['order'] ) {
            return 
$a_val <=> $b_val;
        } else {
            return 
$b_val <=> $a_val;
        }
    }

    
/**
     * Gets argument by dot notation path.
     *
     * @param array  $arguments Arguments array.
     * @param string $path Path for argument key.
     * @param string $delimiter Path delimiter, default: '.'.
     * @return mixed|null
     */
    
public static function get_argument_from_path$arguments$path$delimiter '.' ) {
        
$path_keys explode$delimiter$path );
        
$num_keys  false !== $path_keys count$path_keys ) : 0;

        
$val $arguments;
        for ( 
$i 0$i $num_keys$i++ ) {
            
$key $path_keys$i ];
            if ( 
array_key_exists$key$val ) ) {
                
$val $val$key ];
            } else {
                
$val null;
                break;
            }
        }
        return 
$val;
    }

    
/**
     * Array of required arguments.
     *
     * @var array
     */
    
protected $required_arguments = array();

    
/**
     * Get missing arguments of args array.
     *
     * @param array $args field arguments.
     * @return array
     */
    
public function get_missing_arguments$args ) {
        return 
array_values(
            
array_filter(
                
$this->required_arguments,
                function( 
$arg_key ) use ( $args ) {
                    return 
null === self::get_argument_from_path$args$arg_key );
                }
            )
        );
    }
}