/var/www/html_uk/wp-content/plugins/automatewoo/includes/Installer.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
316
317
318
319
320
321
322
<?php
namespace AutomateWoo;

use 
AutomateWoo\DatabaseUpdates\AbstractDatabaseUpdate;

/**
 * @class Installer
 */
class Installer {

    
/** @var array */
    
public static $db_updates = [
        
'2.6.0',
        
'2.6.1',
        
'2.7.0',
        
'2.9.7',
        
'3.0.0',
        
'3.5.0',
        
'3.6.0',
        
'4.0.0',
        
'5.0.0',
        
'5.1.0',
        
'5.3.0',
        
'6.0.0',
    ];

    
/** @var int  */
    
public static $db_update_items_processed 0;


    
/**
     * Initialize installer.
     */
    
public static function init() {
        
add_action'admin_init', [ __CLASS__'admin_init' ], );
        
add_filter'plugin_action_links_' AW()->plugin_basename, [ __CLASS__'plugin_action_links' ] );
    }


    
/**
     * Admin init
     */
    
public static function admin_init() {

        if ( 
defined'IFRAME_REQUEST' ) || wp_doing_ajax() ) {
            return;
        }

        
self::check_if_plugin_files_updated();

        if ( 
Options::database_version() !== AW()->version ) {
            
self::install();

            if ( ! 
Options::database_version() ) {
                
self::first_install();
            }

            
// check for required database update
            
if ( self::is_database_upgrade_required() ) {
                if ( 
apply_filters'woocommerce_enable_auto_update_db'false ) ) {
                    
self::run_database_updates();
                } else {
                    
add_action'admin_notices', [ __CLASS__'data_upgrade_prompt' ] );
                }
            } else {
                
self::update_database_versionAW()->version );
                
self::do_plugin_updated_actions();
            }
        }

        foreach ( 
Addons::get_all() as $addon ) {
            
$addon->check_version();
        }
    }


    
/**
     * Checks and logs if plugin files have been updated.
     *
     * @since 4.3.0
     */
    
public static function check_if_plugin_files_updated() {
        
$file_version Options::file_version();

        if ( 
$file_version !== AW()->version ) {
            
$old_version $file_version;
            
$new_version AW()->version;

            if ( 
$old_version ) {
                
Logger::info'updates'sprintf'AutomateWoo - Plugin updated from %s to %s'$old_version$new_version ) );
            }

            
update_option'automatewoo_file_version'$new_versiontrue );

            
do_action'automatewoo_version_changed'$old_version$new_version );
        }
    }

    
/**
     * Install
     */
    
public static function install() {
        
Database_Tables::install_tables();
        
self::create_pages();

        
do_action'automatewoo_installed' );
    }

    
/**
     * Runs when AW is installed for the first time.
     *
     * On the other hand `Installer::install()` runs on every plugin update.
     *
     * @since 4.7.0
     */
    
private static function first_install() {
        
do_action'automatewoo_first_installed' );
    }

    
/**
     * @return bool
     */
    
public static function is_database_upgrade_required() {
        if ( 
Options::database_version() === AW()->version ) {
            return 
false;
        }

        return 
Options::database_version() && version_compareOptions::database_version(), endself::$db_updates ), '<' );
    }


    
/**
     * @return array
     */
    
public static function get_required_database_updates() {

        
$required_updates = [];

        foreach ( 
self::$db_updates as $version ) {
            if ( 
version_compareOptions::database_version(), $version'<' ) ) {
                
$required_updates[] = $version;
            }
        }

        return 
$required_updates;
    }


    
/**
     * Handle updates, may be called multiple times to batch complete
     * Returns false if updates are still required
     *
     * @return bool
     */
    
public static function run_database_updates() {
        
wp_raise_memory_limit'admin' );

        
$required_updates                self::get_required_database_updates();
        
self::$db_update_items_processed 0// reset counter

        // update one version at a time
        
$update   current$required_updates );
        
$complete self::run_database_update$update );

        if ( 
count$required_updates ) > ) {
            
$complete false// not complete if there is more than one update
        
}

        if ( 
$complete ) {
            
self::do_plugin_updated_actions();
        }

        return 
$complete;
    }


    
/**
     * Return true if update is complete, return false if another pass is required
     *
     * @param string $version
     * @return bool
     */
    
public static function run_database_update$version ) {

        
$update_file AW()->path"/includes/DatabaseUpdates/$version.php" );

        
// recent updates will return a class
        
$update = include $update_file// nosemgrep No user input here

        
if ( $update instanceof AbstractDatabaseUpdate ) {
            
$update->dispatch_process();
            
self::$db_update_items_processed += $update->get_items_processed_count();

            
$complete $update->is_complete();
        } else {
            
// don't check completion on legacy updates
            
$complete true;
        }

        if ( 
$complete ) {
            
self::update_database_version$version );
        }

        return 
$complete;
    }


    
/**
     * Returns the item to process count for all currently required updates.
     *
     * @return int
     */
    
public static function get_database_update_items_to_process_count() {
        
$required_updates self::get_required_database_updates();
        
$count            0;

        foreach ( 
$required_updates as $version ) {
            if ( 
version_compare$version'2.7.0''<=' ) ) {
                continue; 
// old updates don't extend AbstractDatabaseUpdate class
            
}

            
$update_file AW()->path"/includes/DatabaseUpdates/$version.php" );
            
$update      = include $update_file/** @var $update AbstractDatabaseUpdate */
            
$count      += $update->get_items_to_process_count();
        }

        return 
$count;
    }


    
/**
     * Update version to current
     *
     * @param string $version
     */
    
private static function update_database_version$version ) {
        
update_option'automatewoo_version'$versiontrue );
    }


    
/**
     * Renders prompt notice for user to update
     */
    
public static function data_upgrade_prompt() {
        
Admin::get_view(
            
'data-upgrade-prompt',
            [
                
'plugin_name' => __'AutomateWoo''automatewoo' ),
                
'plugin_slug' => AW()->plugin_slug,
            ]
        );
    }


    
/**
     * @return bool
     */
    
public static function is_data_update_screen() {
        
$screen get_current_screen();
        return 
$screen->id === 'automatewoo_page_automatewoo-data-upgrade';
    }


    
/**
     * Show action links on the plugin screen.
     *
     * @param  mixed $links Plugin Action links
     * @return array
     */
    
public static function plugin_action_links$links ) {
        
$action_links = [
            
'settings'      => '<a href="' esc_urlAdmin::page_url'settings' ) ) . '" title="' esc_attr__'View AutomateWoo Settings''automatewoo' ) ) . '">' esc_html__'Settings''automatewoo' ) . '</a>',
            
'documentation' => '<a href="' esc_urlAdmin::page_url'documentation' ) ) . '" title="' esc_attr__'View AutomateWoo Documentation''automatewoo' ) ) . '">' esc_html__'Documentation''automatewoo' ) . '</a>',
        ];

        return 
array_merge$action_links$links );
    }


    
/**
     * Run plugin updated actions.
     */
    
public static function do_plugin_updated_actions() {
        
do_action'automatewoo_updated' );
        
AW()->action_scheduler()->enqueue_async_action'automatewoo_updated_async' );

        
// Queue the requirements changes notice to show (if necessary).
        
AdminNotices::add_notice'requirements_changes' );
    }


    
/**
     * Creates required pages, run on every update, so it can be repeated without creating duplicates
     */
    
public static function create_pages() {

        
$created_pages get_option'_automatewoo_created_pages', [] );

        
$pages apply_filters(
            
'automatewoo_create_pages',
            [
                
'communication_preferences' => [
                    
'name'    => _x'communication-preferences''Page slug''automatewoo' ),
                    
'title'   => _x'Communication preferences''Page title''automatewoo' ),
                    
'content' => '[automatewoo_communication_preferences]',
                    
'option'  => 'automatewoo_communication_preferences_page_id',
                ],
            ]
        );

        foreach ( 
$pages as $key => $page ) {
            if ( 
in_array$key$created_pagestrue ) ) {
                continue;
            }

            
Admin::create_pageesc_sql$page['name'] ), $page['title'], $page['content'], $page['option'] );
            
$created_pages[] = $key;
        }

        
update_option'_automatewoo_created_pages'$created_pagesfalse );
    }
}