/
oik-activation.php
266 lines (241 loc) · 8.62 KB
/
oik-activation.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
<?php // (C) Copyright Bobbing Wide 2012-2017
if ( !defined( "OIK_ACTIVATION_INCLUDED" ) ) {
define( "OIK_ACTIVATION_INCLUDED", "3.2.0" );
if ( function_exists( "oik_plugin_lazy_activation" ) ) {
// It's already defined so we don't need this lot
} else {
/**
* Produce an install plugin link
*
* @param string $plugin plugin slug
* @return string link for the plugin install
*/
function oik_plugin_install_plugin( $plugin ) {
$path = "update.php?action=install-plugin&plugin=$plugin";
$url = admin_url( $path );
$url = wp_nonce_url( $url, "install-plugin_$plugin" );
$link = '<a href="';
$link .= $url;
$link .= '">';
$link .= sprintf( __( 'Install %1$s', null ), $plugin ) ;
$link .= "</a>";
return $link ;
}
/**
* Produces an "activate" plugin link
*
* We probably don't need plugin_status, s(earch) or paged parameters
*
* `
http://example.com/wp-admin/plugins.php?
action=activate
&plugin=oik%2Foik.php
&plugin_status=all
&paged=1
&s
&_wpnonce=a53a158be5
* `
*
* @param string $plugin - relative plugin file e.g. oik/oik-header.php
* We may not be activating the main plugin, so we need the relative path filename of the plugin to activate
* @param string $plugin_name - e.g. oik-header
* @return string link to enable activation - which user must choose
*/
function oik_plugin_activate_plugin( $plugin, $plugin_name) {
$path = "plugins.php?action=activate&plugin_status=all&paged=1&s&plugin=$plugin";
$url = admin_url( $path );
$url = wp_nonce_url( $url, "activate-plugin_$plugin" );
$link = '<a href="';
$link .= $url;
$link .= '">';
$link .= sprintf( __( 'Activate %1$s', null ), $plugin_name );
$link .= "</a>";
return $link;
}
/**
* Create an Update plugin link
*
* @param string $plugin the plugin slug
* @return string the update link
*/
function oik_plugin_update_plugin( $plugin ) {
$path = "update.php?action=upgrade-plugin&plugin=$plugin";
$url = admin_url( $path );
$url = wp_nonce_url( $url, "upgrade-plugin_$plugin" );
$link = '<a href="';
$link .= $url;
$link .= '">';
$link .= sprintf( __( 'Upgrade %1$s', null ), $plugin );
$link .= "</a>";
return $link;
}
/**
* Find out if we think the plugin is installed but not activated or not even installed
*
* @param string $plugin - the plugin file name ( without plugin path info? )
* @return string - null if it's not installed or plugin to be activated
C:\apache\htdocs\wordpress\wp-content\plugins\oik\shortcodes\oik-bob-bing-wide.php(289:0) 2012-05-23T07:52:15+00:00 696 cf=the_content bw_get_plugins(4) Array
(
[oik/oik.php] => Array
(
[Name] => oik base plugin
[PluginURI] => http://www.oik-plugins.com/oik
[Version] => 1.13
[Description] => Lazy smart shortcodes for displaying often included key-information and other WordPress content
[Author] => bobbingwide
[AuthorURI] => http://www.bobbingwide.com
[TextDomain] =>
[DomainPath] =>
[Network] =>
[Title] => oik base plugin
[AuthorName] => bobbingwide
)
)
*/
function oik_plugin_check_installed_plugin( $plugin ) {
$plugin_to_activate = null;
$needle = "/$plugin.php";
$plugins = get_plugins();
//bw_trace2( $plugins );
if ( count( $plugins )) {
foreach ( $plugins as $plugin_name => $plugin_details ) {
if ( strpos( $plugin_name, $needle ) ) {
//bw_trace2( $plugin_name, $needle );
$plugin_to_activate['Name'] = $plugin_details['Name'];
$plugin_to_activate['file'] = $plugin_name;
break;
}
}
}
return( $plugin_to_activate );
}
/**
* Test if the plugin is activated
*
* This won't work for Multisite since it doesn't find the network activated plugins
* Even if it did, the admin may not be able to do anything.
*/
function oik_plugin_is_plugin_activated( $plugin ) {
$active_plugins = get_option('active_plugins');
$activated = false;
foreach ( $active_plugins as $key => $active_plugin ) {
$bn = basename( $active_plugin, '.php' );
if ( $plugin == $bn ) {
$activated = true;
}
}
return( $activated );
}
/**
* oik_plugin_oik_install_link
http://example.com/wp-admin/update.php?action=install-plugin&plugin=oik&_wpnonce=eb1c632af5
http://example.com/wp-admin/plugin-install.php?tab=search&s=oik&plugin-search-input=Search+Plugins
* For versions of oik before 1.14 the $problem is not passed so we have to find out ourselves
* if oik happens to be installed but is the wrong version. Tricky isn't it?
*/
function oik_plugin_oik_install_link( $plugin, $problem="missing" ) {
if ( $problem == "missing" ) {
/* Is it missing or just inactive ? */
$plugin_to_activate = oik_plugin_check_installed_plugin( $plugin );
if ( $plugin_to_activate ) {
$link = oik_plugin_activate_plugin( $plugin_to_activate['file'], $plugin_to_activate['Name'] );
} else {
$link = oik_plugin_install_plugin( $plugin );
}
} elseif ( $problem == null ) {
$plugin_to_activate = oik_plugin_check_installed_plugin( $plugin );
if ( $plugin_to_activate ) {
$activated = oik_plugin_is_plugin_activated( $plugin );
if ( $activated ) {
$link = oik_plugin_update_plugin( $plugin );
} else {
$link = oik_plugin_activate_plugin( $plugin_to_activate['file'], $plugin_to_activate['Name'] );
}
} else {
$link = oik_plugin_install_plugin( $plugin );
}
} else {
$link = oik_plugin_update_plugin( $plugin );
}
return( $link );
}
/**
*
* Display a message when setup is not fully functional due to the dependencies not being activated or installed
* Note: We can't use oik APIs here as we don't know if it's activated.
* If the message is issued due to a version mismatch then there is a chance that one plugin attempts to use
* functions that are not available in the dependent plugin. How do we manage this?
*/
if ( !function_exists( "oik_plugin_plugin_inactive" ) ) {
function oik_plugin_plugin_inactive( $plugin=null, $dependencies=null, $problem=null ) {
$plugin_name = basename( $plugin, ".php" );
$dependencies = str_replace( ":", __(" version ", null) , $dependencies );
$text = "<p><b>";
$text .= sprintf( __( '%1$s may not be fully functional.', null), $plugin_name );
$text .= "</b> ";
$text .= sprintf( __( 'Please install and activate the required minimum version of this plugin: %1$s', null ), $dependencies );
$text .= "</p>";
if ( current_filter() == "admin_notices" ) {
$message = '<div class=" updated fade">';
$message .= $text;
$depends = strtok( $dependencies, " " );
$message .= oik_plugin_oik_install_link( $depends, $problem );
$message .= '</div>';
} else {
$message = '<tr class="plugin-update-tr">';
$message .= '<td colspan="3" class="plugin-update colspanchange">';
$message .= '<div class="update-message">';
$message .= $text;
$message .= "</div>";
$message .= "</td>";
$message .= "</tr>";
}
echo $message;
}
}
/**
* Test if this plugin is functional
*
* Unless oik is installed and activated this plugin won't do anything
* Note: If oik is installed and activated then we shouldn't have any problem
* unless there's a version number mismatch.
*/
function oik_plugin_lazy_activation( $plugin=null, $dependencies=null, $callback=null ) {
if ( function_exists( "oik_depends" ) ) {
/* Good - oik appears to be activated and loaded */
oik_depends( $plugin, $dependencies, $callback );
} else {
call_user_func( $callback, $plugin, $dependencies, "missing" );
}
}
/**
* Simple implementation of plugin dependency logic
*
* @param string $plugin - the plugin file name
* @param string $dependencies - the list of plugins upon which this plugin is dependent
* @param string $callback - the callback function to invoke when the dependencies aren't satisfied
*
* Instead of calling this module during activation we invoke it in response to the
* after_plugin_row_$plugin_basename action.
*
* This gives us a bit more control over the information we provide.
* IF the oik plugin is not activated then oik_lazy_depends() will not be defined
*
*/
if ( !function_exists( "oik_depends" ) ) {
function oik_depends( $plugin=null, $dependencies="oik", $callback=null ) {
//bw_trace2();
//if ( function_exists( "oik_load_plugins" )) {
// oik_load_plugins();
//}
if ( function_exists( "oik_lazy_depends" ) ) {
oik_lazy_depends( $plugin, $dependencies, $callback );
} else {
if ( is_callable( $callback ) ) {
call_user_func( $callback, $plugin, $dependencies, "missing" );
}
}
}
}
} // end else
} // end if !defined()