/var/www/html_uk/wp-content/plugins/automatewoo/includes/Model.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php

namespace AutomateWoo;

/**
 * @class Model
 *
 * @property $id
 */
abstract class Model {

    
/** @var string : required for every model and must have a corresponding Database_Table */
    
public $table_id;

    
/** @var bool */
    
public $exists false;

    
/** @var array */
    
public $data = [];

    
/** @var array - data as it last existed in the database */
    
public $original_data = [];

    
/** @var array */
    
public $changed_fields = [];

    
/** @var string */
    
public $object_type;


    
/**
     * @return int
     */
    
public function get_id() {
        return 
$this->id ? (int) $this->id 0;
    }


    
/**
     * @param int $id
     */
    
public function set_id$id ) {
        
$this->id $id;
    }


    
/**
     * Fill model with data
     *
     * @param array $row
     */
    
public function fill$row ) {
        if ( ! 
is_array$row ) ) {
            return;
        }

        
$this->data          $row;
        
$this->original_data $row;
        
$this->exists        true;

        
do_action'automatewoo/object/load'$this );
    }


    
/**
     * @param string     $field
     * @param string|int $value
     */
    
public function get_by$field$value ) {

        global 
$wpdb;

        
$row $wpdb->get_row(
            
$wpdb->prepare(
                
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
                
"SELECT * FROM {$this->get_table_name()} WHERE $field = %s",
                
$value
            
),
            
ARRAY_A
        
);

        if ( ! 
$row ) {
            return;
        }

        
$this->fill$row );
    }


    
/**
     * Magic method for accessing db fields
     *
     * @param string $key
     * @return mixed
     */
    
public function __get$key ) {
        return 
$this->get_prop$key );
    }


    
/**
     * Magic method for setting db fields
     *
     * @param string $key
     * @param mixed  $value
     */
    
public function __set$key$value ) {
        
$this->set_prop$key$value );
    }


    
/**
     * @param string $key
     * @param mixed  $value
     */
    
public function set_prop$key$value ) {

        if ( 
is_array$value ) && ! $value ) {
            
$value ''// convert empty arrays to blank
        
}

        
$this->data$key ]     = $value;
        
$this->changed_fields[] = $key;
    }


    
/**
     * @param string $key
     * @return bool
     */
    
public function has_prop$key ) {
        return isset( 
$this->data$key ] );
    }


    
/**
     * @param string $key
     * @return mixed
     */
    
public function get_prop$key ) {
        if ( ! isset( 
$this->data$key ] ) ) {
            return 
false;
        }

        
$value $this->data$key ];
        
$value maybe_unserialize$value );

        return 
$value;
    }


    
/**
     * @return Database_Table
     */
    
public function get_table() {

        if ( ! isset( 
$this->table_id ) ) {
            
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
            
wp_diesprintf'AutomateWoo - %s is an incompatible subclass of %s. You may need need to update your AutomateWoo add-ons.'get_called_class(), __CLASS__ ) );
        }

        return 
Database_Tables::get$this->table_id );
    }


    
/**
     * @return string
     */
    
public function get_table_name() {
        return 
$this->get_table()->get_name();
    }


    
/**
     * Inserts or updates the model
     * Only updates modified fields
     *
     * @return bool True on success, false on error.
     */
    
public function save() {

        global 
$wpdb;

        if ( 
$this->exists ) {
            
// update changed fields
            
$changed_data array_intersect_key$this->dataarray_flip$this->changed_fields ) );

            
// serialize
            
$changed_data array_map'maybe_serialize'$changed_data );

            if ( empty( 
$changed_data ) ) {
                return 
true;
            }

            
$updated $wpdb->update(
                
$this->get_table_name(),
                
$changed_data,
                [ 
'id' => $this->get_id() ],
                
null,
                [ 
'%d' ]
            );

            if ( 
false === $updated ) {
                
// Return here to prevent cache updates on error
                
return false;
            }

            
do_action'automatewoo/object/update'$this ); // cleans object cache
        
} else {
            
$this->data array_map'maybe_serialize'$this->data );

            
// insert row
            
$wpdb->insert(
                
$this->get_table_name(),
                
$this->data
            
);

            if ( 
$wpdb->insert_id ) {
                
$this->exists true;
                
$this->id     $wpdb->insert_id;
            } else {
                
$aw_catch_db_error 'Error: ' $wpdb->last_error ' (Query: ' $wpdb->last_query ')';
                
/* translators: %1$s object type, %2$s DB error message and query */
                
Logger::critical'errors'sprintf__'Could not insert \'%1$s\' item to database. AutomateWoo tables may not be installed. (%2$s)''automatewoo' ), $this->object_type$aw_catch_db_error ) );

                
// Return here to prevent cache updates on error
                
return false;
            }

            
do_action'automatewoo/object/create'$this ); // cleans object cache
        
}

        
$this->clear_cached_data();

        
// reset changed data
        // important to reset after cache hooks
        
$this->changed_fields = [];
        
$this->original_data  $this->data;

        return 
true;
    }


    
/**
     * @return void
     */
    
public function delete() {
        global 
$wpdb;

        
do_action'automatewoo/object/delete'$this ); // cleans object cache

        
if ( ! $this->exists ) {
            return;
        }

        
$wpdb->query(
            
$wpdb->prepare(
                
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
                
"DELETE FROM {$this->get_table_name()} WHERE id = %d",
                
$this->get_id()
            )
        );

        
$this->exists false;
        
$this->clear_cached_data();
    }

    
/**
     * Clear cached data.
     *
     * @since 6.1.8
     */
    
public function clear_cached_data() {}

    
/**
     * @param string $column
     * @return bool|DateTime
     */
    
protected function get_date_column$column ) {
        if ( ! 
$column ) {
            return 
false;
        }

        
$prop $this->get_prop$column );
        if ( ! 
$prop ) {
            return 
false;
        }

        return new 
DateTime$prop );
    }


    
/**
     * Sets the value of a date column from  a mixed input.
     *
     * $value can be an instance of WC_DateTime the timezone will be ignored.
     * If $value is a string it must be MYSQL formatted.
     *
     * @param string                                      $column
     * @param \WC_DateTime|DateTime|\DateTime|string|null $value
     */
    
protected function set_date_column$column$value ) {
        if ( 
is_a$value'DateTime' ) ) {
            
/** @var \DateTime $value Accepts AutomateWoo\DateTime, DateTime or WC_DateTime */
            // convert to UTC time
            
$utc_date = new DateTime();
            
$utc_date->setTimestamp$value->getTimestamp() );
            
$this->set_prop$column$utc_date->to_mysql_string() );
        } elseif ( 
$value ) {
            
$this->set_prop$columnClean::string$value ) );
        } elseif ( 
null === $value ) {
            
$this->set_prop$columnnull );
        }
    }
}