/var/www/html_us/wp-content/plugins/woocommerce/src/Internal/Admin/Notes/MarketingJetpack.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
<?php
/**
 * WooCommerce Admin Jetpack Marketing Note Provider.
 *
 * Adds notes to the merchant's inbox concerning Jetpack Backup.
 */

namespace Automattic\WooCommerce\Internal\Admin\Notes;

defined'ABSPATH' ) || exit;

use 
Automattic\Jetpack\Constants;
use 
Automattic\WooCommerce\Admin\Notes\Note;
use 
Automattic\WooCommerce\Admin\Notes\Notes;
use 
Automattic\WooCommerce\Admin\Notes\NoteTraits;
use 
Automattic\WooCommerce\Admin\PluginsHelper;

/**
 * Suggest Jetpack Backup to Woo users.
 *
 * Note: This should probably live in the Jetpack plugin in the future.
 *
 * @see  https://developer.woocommerce.com/2020/10/16/using-the-admin-notes-inbox-in-woocommerce/
 */
class MarketingJetpack {
    
// Shared Note Traits.
    
use NoteTraits;

    
/**
     * Name of the note for use in the database.
     */
    
const NOTE_NAME 'wc-admin-marketing-jetpack-backup';

    
/**
     * Product IDs that include Backup.
     */
    
const BACKUP_IDS = [
        
2010,
        
2011,
        
2012,
        
2013,
        
2014,
        
2015,
        
2100,
        
2101,
        
2102,
        
2103,
        
2005,
        
2006,
        
2000,
        
2003,
        
2001,
        
2004,
    ];

    
/**
     * Maybe add a note on Jetpack Backups for Jetpack sites older than a week without Backups.
     */
    
public static function possibly_add_note() {
        
/**
         * Check if Jetpack is installed.
         */
        
$installed_plugins PluginsHelper::get_installed_plugin_slugs();
        if ( ! 
in_array'jetpack'$installed_pluginstrue ) ) {
            return;
        }

        
$data_store \WC_Data_Store::load'admin-note' );

        
// Do we already have this note?
        
$note_ids $data_store->get_notes_with_nameself::NOTE_NAME );
        if ( ! empty( 
$note_ids ) ) {

            
$note_id array_pop$note_ids );
            
$note    Notes::get_note$note_id );
            if ( 
false === $note ) {
                return;
            }

            
// If Jetpack Backups was purchased after the note was created, mark this note as actioned.
            
if ( self::has_backups() && Note::E_WC_ADMIN_NOTE_ACTIONED !== $note->get_status() ) {
                
$note->set_statusNote::E_WC_ADMIN_NOTE_ACTIONED );
                
$note->save();
            }

            return;
        }

        
// Check requirements.
        
if ( ! self::is_wc_admin_active_in_date_range'week-1-4'DAY_IN_SECONDS ) || ! self::can_be_added() || self::has_backups() ) {
            return;
        }

        
// Add note.
        
$note self::get_note();
        
$note->save();
    }

    
/**
     * Get the note.
     */
    
public static function get_note() {
        
$note = new Note();
        
$note->set_title__'Protect your WooCommerce Store with Jetpack Backup.''woocommerce' ) );
        
$note->set_content__'Store downtime means lost sales. One-click restores get you back online quickly if something goes wrong.''woocommerce' ) );
        
$note->set_typeNote::E_WC_ADMIN_NOTE_MARKETING );
        
$note->set_nameself::NOTE_NAME );
        
$note->set_layout'thumbnail' );
        
$note->set_image(
            
WC_ADMIN_IMAGES_FOLDER_URL '/admin_notes/marketing-jetpack-2x.png'
        
);
        
$note->set_content_data( (object) array() );
        
$note->set_source'woocommerce-admin-notes' );
        
$note->add_action(
            
'jetpack-backup-woocommerce',
            
__'Get backups''woocommerce' ),
            
esc_url'https://jetpack.com/upgrade/backup-woocommerce/?utm_source=inbox&utm_medium=automattic_referred&utm_campaign=jp_backup_to_woo' ),
            
Note::E_WC_ADMIN_NOTE_ACTIONED
        
);
        return 
$note;
    }

    
/**
     * Check if this blog already has a Jetpack Backups product.
     *
     * @return boolean  Whether or not this blog has backups.
     */
    
protected static function has_backups() {
        
$product_ids = [];

        
$plan get_option'jetpack_active_plan' );
        if ( ! empty( 
$plan ) ) {
            
$product_ids[] = $plan['product_id'];
        }

        
$products get_option'jetpack_site_products' );
        if ( ! empty( 
$products ) ) {
            foreach ( 
$products as $product ) {
                
$product_ids[] = $product['product_id'];
            }
        }

        return (bool) 
array_intersectself::BACKUP_IDS$product_ids );
    }

}