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

namespace AutomateWoo\Fields;

defined'ABSPATH' ) || exit;

/**
 * Class Before_After_Day
 *
 * @since 4.5
 *
 * @package AutomateWoo\Fields
 */
class Before_After_Day extends Field {

    
/**
     * The field's default name.
     *
     * @var string
     */
    
protected $name 'before_after_day';

    
/**
     * The field type.
     *
     * @var string
     */
    
protected $type 'field-group';

    
/**
     * Before_After_Day constructor.
     */
    
public function __construct() {
        
parent::__construct();
        
$this->title __'When to run''automatewoo' );
    }

    
/**
     * Renders field.
     *
     * @param array $value
     */
    
public function render$value ) {
        
$value = (int) $value;

        
// convert the integer value for the inputs
        
if ( $value ) {
            
$day_val  $value;
            
$type_val 'days_after';
        } elseif ( 
$value ) {
            
$day_val  $value * - 1;
            
$type_val 'days_before';
        } else {
            
$day_val  '';
            
$type_val 'on_the_day';
        }

        
$field_names $this->get_name_base() . '[' $this->get_name() . '][]';

        
$types = [
            
'on_the_day'  => __'On the day''automatewoo' ),
            
'days_after'  => __'day(s) after''automatewoo' ),
            
'days_before' => __'day(s) before''automatewoo' ),
        ];

        
?>
        <div class="automatewoo-before-after-day-field-group">
            <div class="automatewoo-before-after-day-field-group__fields">

                <input type="number" name="<?php echo esc_attr$field_names ); ?>"
                    value="<?php echo esc_attr$day_val ); ?>"
                    class="automatewoo-before-after-day-field-group__field automatewoo-before-after-day-field-group__field--days"
                    min="1"
                    placeholder="1"
                >

                <select name="<?php echo esc_attr$field_names ); ?>"
                        class="automatewoo-before-after-day-field-group__field automatewoo-before-after-day-field-group__field--type"
                >
                    <?php foreach ( $types as $opt_name => $opt_value ) : ?>
                        <option value="<?php echo esc_attr$opt_name ); ?><?php selected$type_val$opt_name ); ?>><?php echo esc_html$opt_value ); ?></option>
                    <?php endforeach; ?>
                </select>

            </div>
        </div>
        <?php
    
}

    
/**
     * Sanitizes the value of the field.
     *
     * Converts the field value to an integer.
     *
     * @param array $value
     *
     * @return int|false
     */
    
public function sanitize_value$value ) {
        if ( 
is_int$value ) ) {
            return 
$value;
        }

        
// convert array format to a simple integer
        
if ( is_array$value ) && count$value ) === ) {
            
$days $value[0] ? $value[0] : 1;
            
$type $value[1];

            switch ( 
$type ) {
                case 
'on_the_day':
                    return 
0;
                case 
'days_after':
                    return (int) 
$days;
                case 
'days_before':
                    return 
intval$days ) * -1;
            }
        }

        return 
false;
    }
}