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
|
<?php /** * Update to 5.1.0 * * - use `date-desc` sorting for existing `shop.products` variables */
namespace AutomateWoo\DatabaseUpdates;
use AutomateWoo\Replace_Helper; use AutomateWoo\Variables_Processor; use AutomateWoo\Workflow; use AutomateWoo\Workflows\Factory; use AutomateWoo\Workflows\VariableParsing\ParsedVariable;
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * Class Database_Update_5_1_0 * * @package AutomateWoo\DatabaseUpdates */ class Database_Update_5_1_0 extends AbstractDatabaseUpdate {
const UPDATE_ITEMS_OPTIONS_KEY = 'automatewoo_update_items';
/** @var string */ protected $version = '5.1.0';
/** * Runs immediately before a database update begins. */ protected function start() { parent::start();
// get list of workflows to update $workflows = $this->get_all_workflow_ids();
update_option( self::UPDATE_ITEMS_OPTIONS_KEY, $workflows ); }
/** * Called immediately after database update is completed. */ protected function finish() { parent::finish();
delete_option( self::UPDATE_ITEMS_OPTIONS_KEY ); }
/** * @return bool */ protected function process() { $items = get_option( self::UPDATE_ITEMS_OPTIONS_KEY );
if ( empty( $items ) ) { return true; // no more items to process, return complete }
$batch = array_splice( $items, 0, 5 );
foreach ( $batch as $item ) { $workflow = Factory::get( $item ); $this->update_shop_products_variables( $workflow );
++$this->items_processed; }
update_option( self::UPDATE_ITEMS_OPTIONS_KEY, $items ); return false; }
/** * @return bool|int */ public function get_items_to_process_count() { if ( ! get_option( self::UPDATE_ITEMS_OPTIONS_KEY ) ) { $workflows_to_update = $this->get_all_workflow_ids(); } else { $workflows_to_update = get_option( self::UPDATE_ITEMS_OPTIONS_KEY ); }
return count( $workflows_to_update ); }
/** * @param Workflow $workflow */ private function update_shop_products_variables( $workflow ) { $actions = $workflow->get_meta( 'actions' );
if ( ! $actions ) { return; }
foreach ( $actions as &$action ) { foreach ( $action as $field_name => &$field_value ) { if ( 'action_name' === $field_name ) { continue; }
$replacer = new Replace_Helper( $field_value, function ( $value ) { $variable = Variables_Processor::parse_variable( $value ); if ( ! $variable instanceof ParsedVariable ) { return false; }
if ( 'shop' === $variable->type && 'products' === $variable->field && ! array_key_exists( 'sort', $variable->parameters ) ) { $value .= ", sort: 'date-desc'"; }
return "{{ $value }}"; }, 'variables' );
$field_value = $replacer->process(); } }
$workflow->update_meta( 'actions', $actions ); }
/** * Return the list of IDs of all the workflows in the database * * @return int[] */ private function get_all_workflow_ids() { return get_posts( [ 'post_type' => 'aw_workflow', 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids', ] ); } }
return new Database_Update_5_1_0();
|