class-itthinx-updates.php
4.9 KB
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
<?php
class Itthinx_Updates {
/**
* Plugin initialization.
*/
public static function init() {
$proceed = true;
if ( is_multisite() && !is_network_admin() ) {
remove_action( 'admin_notices', 'itthinx_updates_install' );
if ( !function_exists( 'is_plugin_active_for_network' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
if ( !is_plugin_active_for_network( plugin_basename( ITTHINX_UPDATES_FILE ) ) ) {
add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
$proceed = false;
}
}
load_plugin_textdomain( ITTHINX_UPDATES_PLUGIN_DOMAIN, null, 'itthinx-updates/languages' );
if ( $proceed ) {
if ( is_admin() ) {
require_once 'class-itthinx-updates-update.php';
require_once 'class-itthinx-updates-admin.php';
add_action( 'init', array( __CLASS__, 'wp_init' ) );
$service_key = Itthinx_Updates::get_service_key( '' );
if ( empty( $service_key ) ) {
add_action( 'admin_notices', array( __CLASS__, 'admin_notices_service_key' ) );
if ( is_multisite() ) {
add_action( 'network_admin_notices', array( __CLASS__, 'admin_notices_service_key' ) );
}
}
}
// @see Itthinx_Updates_Bridge
// add_filter( 'pre_set_site_transient_update_plugins', array( __CLASS__, 'pre_set_site_transient_update_plugins' ), 20, 1 );
// add_filter( 'site_transient_update_plugins', array( __CLASS__, 'site_transient_update_plugins' ), 20, 1 );
}
}
/**
* Show the network activation required error message.
*/
public static function admin_notices() {
echo '<div class="error">';
echo __( 'The <strong>Itthinx Updates</strong> plugin must be network activated for this multisite.', ITTHINX_UPDATES_PLUGIN_DOMAIN );
echo '</div>';
}
/**
* Show the service key required error message.
*/
public static function admin_notices_service_key() {
if ( is_multisite() && ( !is_super_admin() || !is_network_admin() ) ) {
return;
}
if ( $screen = get_current_screen() ) {
if ( ( $screen->id == 'plugins_page_itthinx-updates' ) || ( $screen->id == 'plugins_page_itthinx-updates-network' ) ) {
return;
}
}
if ( current_user_can( 'manage_options' ) ) {
echo '<div class="error">';
echo sprintf(
__( 'Please enter a valid <a href="%s">service key</a> to enable automatic updates with the <strong>Itthinx Updates</strong> plugin.', ITTHINX_UPDATES_PLUGIN_DOMAIN ),
is_multisite() ? esc_attr( admin_url( 'network/plugins.php?page=itthinx-updates' ) ) : esc_attr( admin_url( 'plugins.php?page=itthinx-updates' ) )
);
echo '</div>';
}
}
/**
* Adds plugin update bridges for registered plugins.
*/
public static function wp_init() {
global $itthinx_plugins;
self::network_add_plugins();
if ( !empty( $itthinx_plugins ) && is_array( $itthinx_plugins ) ) {
require_once 'class-itthinx-updates-bridge.php';
foreach( $itthinx_plugins as $itthinx_plugin ) {
$u = new Itthinx_Updates_Bridge( $itthinx_plugin );
}
}
}
/**
* Check all plugins in multisite so that even inactive plugins produce
* an update notification. In case they are not network activated but
* active on some sites, the network admin should be aware of updates.
*/
public static function network_add_plugins() {
global $itthinx_plugins;
if ( is_multisite() && is_network_admin() ) {
if ( !function_exists( 'get_plugins' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
$plugins = get_plugins();
if ( is_array( $plugins ) ) {
foreach( $plugins as $plugin_basename => $plugin ) {
$plugin_file = WP_PLUGIN_DIR . '/' . $plugin_basename;
if ( isset( $plugin['Author'] ) && $plugin['Author'] == 'itthinx' ) {
if ( !isset( $itthinx_plugins ) ) {
$itthinx_plugins = array();
}
if ( !in_array( $plugin_file, $itthinx_plugins ) ) {
$itthinx_plugins[] = $plugin_file;
}
}
}
}
}
}
/**
* Not used.
* @param array $transient
* @return array
*/
public static function pre_set_site_transient_update_plugins( $transient ) {
return $transient;
}
/**
* Not used.
* @param array $transient update_plugins
* @return array update_plugins transient
*/
public static function site_transient_update_plugins( $transient ) {
return $transient;
}
public static function get_service_key( $default = null ) {
$service_key = null;
if ( is_multisite() ) {
$service_key = get_site_option( 'itthinx_service_key', $default );
} else {
$service_key = get_option( 'itthinx_service_key', $default );
}
return $service_key;
}
public static function set_service_key( $service_key ) {
if ( is_multisite() ) {
delete_site_option( 'itthinx_service_key' );
if ( !empty( $service_key ) ) {
add_site_option( 'itthinx_service_key', $service_key );
}
} else {
delete_option( 'itthinx_service_key' );
if ( !empty( $service_key ) ) {
add_option( 'itthinx_service_key', $service_key, '', 'no' );
}
}
}
}
Itthinx_Updates::init();