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
|
<?php /** * Update to 5.0.0 * * - set existing workflow types to "automatic" */
namespace AutomateWoo\DatabaseUpdates;
use AutomateWoo\Workflow_Query;
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * Class Database_Update_5_0_0 * * @package AutomateWoo\DatabaseUpdates */ class Database_Update_5_0_0 extends AbstractDatabaseUpdate {
/** @var string */ protected $version = '5.0.0';
/** * @return bool */ protected function process() { $limit = 5; $query = $this->get_workflows_query( $limit );
$results = $query->get_results();
if ( empty( $results ) ) { return true; // no more items to process, return complete }
foreach ( $results as $workflow ) { $workflow->update_meta( 'type', 'automatic' );
++$this->items_processed; }
return false; }
/** * @return bool|int */ public function get_items_to_process_count() { // Set limit to minimum since it's irrelevant to total count $query = $this->get_workflows_query( 1 );
// We need a call to this function to be able to count the results $query->get_results();
return $query->get_found_rows(); }
/** * @param int $limit * @return Workflow_Query */ private function get_workflows_query( $limit ) { $query = new Workflow_Query();
$query->set_no_found_rows( false ); $query->set_limit( $limit );
$query->args['meta_query'][] = [ 'key' => 'type', 'compare' => 'NOT EXISTS', ];
return $query; }
/** * Called immediately after database update is completed. */ protected function finish() { global $wpdb; $table = $wpdb->prefix . 'automatewoo_unsubscribes';
// Avoid DB errors if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ) === null ) { return; }
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.SchemaChange
// Only delete the table if it's empty $row_count = $wpdb->get_var( "SELECT COUNT(*) FROM `{$table}`" ); if ( is_numeric( $row_count ) && (int) $row_count === 0 ) { $wpdb->query( "DROP TABLE `{$table}`" ); }
// phpcs:enable
parent::finish(); } }
return new Database_Update_5_0_0();
|