/var/www/html/wp-content/plugins/woocommerce/includes/admin/helper/class-wc-helper-compat.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
<?php
/**
 * WooCommerce Admin Helper Compat
 *
 * @package WooCommerce\Admin\Helper
 */

if ( ! defined'ABSPATH' ) ) {
    exit;
}

/**
 * WC_Helper_Compat Class
 *
 * Some level of compatibility with the legacy WooCommerce Helper plugin.
 */
class WC_Helper_Compat {

    
/**
     * Loads the class, runs on init.
     */
    
public static function load() {
        
add_action'woocommerce_helper_loaded', array( __CLASS__'helper_loaded' ) );
    }

    
/**
     * Runs during woocommerce_helper_loaded
     */
    
public static function helper_loaded() {
        
// Stop the nagging about WooThemes Updater
        
remove_action'admin_notices''woothemes_updater_notice' );

        
// A placeholder dashboard menu for legacy helper users.
        
add_action'admin_menu', array( __CLASS__'admin_menu' ) );

        if ( empty( 
$GLOBALS['woothemes_updater'] ) ) {
            return;
        }

        
self::remove_actions();
        
self::migrate_connection();
        
self::deactivate_plugin();
    }

    
/**
     * Remove legacy helper actions (notices, menus, etc.)
     */
    
public static function remove_actions() {
        
// Remove WooThemes Updater notices
        
remove_action'network_admin_notices', array( $GLOBALS['woothemes_updater']->admin'maybe_display_activation_notice' ) );
        
remove_action'admin_notices', array( $GLOBALS['woothemes_updater']->admin'maybe_display_activation_notice' ) );
        
remove_action'network_admin_menu', array( $GLOBALS['woothemes_updater']->admin'register_settings_screen' ) );
        
remove_action'admin_menu', array( $GLOBALS['woothemes_updater']->admin'register_settings_screen' ) );
    }

    
/**
     * Attempt to migrate a legacy connection to a new one.
     */
    
public static function migrate_connection() {
        
// Don't attempt to migrate if attempted before.
        
if ( WC_Helper_Options::get'did-migrate' ) ) {
            return;
        }

        
$auth WC_Helper_Options::get'auth' );
        if ( ! empty( 
$auth ) ) {
            return;
        }

        
WC_Helper::log'Attempting oauth/migrate' );
        
WC_Helper_Options::update'did-migrate'true );

        
$master_key get_option'woothemes_helper_master_key' );
        if ( empty( 
$master_key ) ) {
            
WC_Helper::log'Master key not found, aborting' );
            return;
        }

        
$request WC_Helper_API::post(
            
'oauth/migrate',
            array(
                
'body' => array(
                    
'home_url'   => home_url(),
                    
'master_key' => $master_key,
                ),
            )
        );

        if ( 
is_wp_error$request ) || wp_remote_retrieve_response_code$request ) !== 200 ) {
            
WC_Helper::log'Call to oauth/migrate returned a non-200 response code' );
            return;
        }

        
$request_token json_decodewp_remote_retrieve_body$request ) );
        if ( empty( 
$request_token ) ) {
            
WC_Helper::log'Call to oauth/migrate returned an empty token' );
            return;
        }

        
// Obtain an access token.
        
$request WC_Helper_API::post(
            
'oauth/access_token',
            array(
                
'body' => array(
                    
'request_token' => $request_token,
                    
'home_url'      => home_url(),
                    
'migrate'       => true,
                ),
            )
        );

        if ( 
is_wp_error$request ) || wp_remote_retrieve_response_code$request ) !== 200 ) {
            
WC_Helper::log'Call to oauth/access_token returned a non-200 response code' );
            return;
        }

        
$access_token json_decodewp_remote_retrieve_body$request ), true );
        if ( empty( 
$access_token ) ) {
            
WC_Helper::log'Call to oauth/access_token returned an invalid token' );
            return;
        }

        
WC_Helper_Options::update(
            
'auth',
            array(
                
'access_token'        => $access_token['access_token'],
                
'access_token_secret' => $access_token['access_token_secret'],
                
'site_id'             => $access_token['site_id'],
                
'user_id'             => null// Set this later
                
'updated'             => time(),
            )
        );

        
// Obtain the connected user info.
        
if ( ! WC_Helper::_flush_authentication_cache() ) {
            
WC_Helper::log'Could not obtain connected user info in migrate_connection' );
            
WC_Helper_Options::update'auth', array() );
            return;
        }
    }

    
/**
     * Attempt to deactivate the legacy helper plugin.
     */
    
public static function deactivate_plugin() {
        include_once 
ABSPATH 'wp-admin/includes/plugin.php';
        if ( ! 
function_exists'deactivate_plugins' ) ) {
            return;
        }

        if ( 
is_plugin_active'woothemes-updater/woothemes-updater.php' ) ) {
            
deactivate_plugins'woothemes-updater/woothemes-updater.php' );

            
// Notify the user when the plugin is deactivated.
            
add_action'pre_current_active_plugins', array( __CLASS__'plugin_deactivation_notice' ) );
        }
    }

    
/**
     * Display admin notice directing the user where to go.
     */
    
public static function plugin_deactivation_notice() {
        
?>
        <div id="message" class="error is-dismissible">
            <p><?php printf__'The WooCommerce Helper plugin is no longer needed. <a href="%s">Manage subscriptions</a> from the extensions tab instead.''woocommerce' ), esc_urladmin_url'admin.php?page=wc-addons&section=helper' ) ) ); ?></p>
        </div>
        <?php
    
}

    
/**
     * Register menu item.
     */
    
public static function admin_menu() {
        
// No additional menu items for users who did not have a connected helper before.
        
$master_key get_option'woothemes_helper_master_key' );
        if ( empty( 
$master_key ) ) {
            return;
        }

        
// Do not show the menu item if user has already seen the new screen.
        
$auth WC_Helper_Options::get'auth' );
        if ( ! empty( 
$auth['user_id'] ) ) {
            return;
        }

        
add_dashboard_page__'WooCommerce Helper''woocommerce' ), __'WooCommerce Helper''woocommerce' ), 'manage_options''woothemes-helper', array( __CLASS__'render_compat_menu' ) );
    }

    
/**
     * Render the legacy helper compat view.
     */
    
public static function render_compat_menu() {
        
$helper_url add_query_arg(
            array(
                
'page'    => 'wc-addons',
                
'section' => 'helper',
            ),
            
admin_url'admin.php' )
        );
        include 
WC_Helper::get_view_filename'html-helper-compat.php' );
    }
}

WC_Helper_Compat::load();