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

namespace AutomateWoo\ActivityPanelInbox;

use 
AutomateWoo\Admin;
use 
Automattic\WooCommerce\Admin\Notes\DataStore;
use 
Automattic\WooCommerce\Admin\Notes\NoteTraits;
use 
Automattic\WooCommerce\Admin\Notes\Note;
use 
Automattic\WooCommerce\Admin\Notes\Notes;
use 
WC_Data_Store;

/**
 * Add the Update note when updating from < $version to >= $version and remove
 * the note if the plugin is deactivated.
 * Also remove the Welcome note and any prior Update notes if present, to avoid
 * double notes.
 *
 * @package AutomateWoo\ActivityPanelInbox
 * @since 5.1.0
 */
class UpdateNote {

    use 
NoteTraits;

    const 
NOTE_NAME 'automatewoo-update';

    
/** @var string The version this notice relates to. */
    
protected static $version '5.1';

    
/**
     * Init the hooks for the note.
     */
    
public static function init() {
        
add_action'automatewoo_version_changed', [ __CLASS__'maybe_add_activity_panel_inbox_note' ], 10);
        
register_deactivation_hookAUTOMATEWOO_FILE, [ __CLASS__'possibly_delete_note' ] );
    }

    
/**
     * Add the Update note if notes are enabled and it's the first update to $version or higher.
     *
     * @param string $old_version previously installed version.
     * @param string $new_version newly updated version.
     *
     * @throws \Exception If the data store fails to load.
     */
    
public static function maybe_add_activity_panel_inbox_note$old_version$new_version ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
        
if ( ! class_existsNotes::class ) || ! class_existsWC_Data_Store::class ) ) {
            return;
        }

        
// First install or already above $version.
        
if ( '' === $old_version || version_compare$old_versionself::$version'>=' ) ) {
            return;
        }

        
// Remove older Update notes if present.
        
self::possibly_delete_older_update_notes();

        
self::possibly_add_note();
    }

    
/**
     * Get the note.
     */
    
public static function get_note() {
        
$note = new Note();
        
$note->set_typeNote::E_WC_ADMIN_NOTE_INFORMATIONAL );
        
$note->set_nameself::NOTE_NAME );
        
$note->set_source'automatewoo' );
        
$note->set_title__'Check out these new AutomateWoo presets''automatewoo' ) );
        
$note->set_content(
            
__(
                
'Reach out to your customers and automate your work easily with these new preset workflows.',
                
'automatewoo'
            
)
        );
        
$note->add_action(
            
'presets',
            
__'Browse presets''automatewoo' ),
            
Admin::page_url'workflow-presets' )
        );
        
$note->set_content_data(
            (object) [
                
'version' => self::$version,
            ]
        );

        return 
$note;
    }

    
/**
     * Find any older Update notes and remove them.
     */
    
public static function possibly_delete_older_update_notes() {
        
/** @var DataStore $data_store */
        
$data_store WC_Data_Store::load'admin-note' );
        
$note_ids   $data_store->get_notes_with_nameself::NOTE_NAME );

        foreach ( (array) 
$note_ids as $note_id ) {
            
$note Notes::get_note$note_id );
            if ( ! 
$note ) {
                continue;
            }
            
$content_data $note->get_content_data();
            
$note_version $content_data->version ?? 0;
            if ( 
version_compare$note_versionself::$version'<' ) ) {
                
$note->delete();
            }
        }
    }
}