-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin-last-updated-warning.php
executable file
·124 lines (99 loc) · 4.37 KB
/
plugin-last-updated-warning.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
<?php
/**
* Plugin Name: Plugin Last Updated Warning
* Plugin URI:
* Description: This plugin will display a warning for plugins that haven't received updates on the WP.org plugin repo the past year.
* Author: Vincent S Hasselgård
* Author URI: https://vincenthasselgard.no
* Text Domain: plugin-last-updated-warning
* Domain Path: /languages
* Version: 0.9.1
*
* @package Plugin_Last_Updated_Warning
*/
namespace MilesToGo;
if( !class_exists('Plugin_Last_Updated_Warning') ) {
class Plugin_Last_Updated_Warning{
public static function init(){
/**
* Retrieves numbers of years since last plugin update on WP.org
*
* @since 0.1.0
*
* @param string $plugin_slug The slug of the plugin that is being checked. Must match plugin slug on WP.org.
*
*/
function get_last_updated_date($plugin_slug){
$action = 'plugin_information';
$args = array(
'slug' => $plugin_slug
);
$url = 'http://api.wordpress.org/plugins/info/1.2/';
$ssl = wp_http_supports( array( 'ssl' ) );
if ( $ssl ) {
$url = set_url_scheme( $url, 'https' );
}
$url = add_query_arg(
array(
'action' => $action,
'request' => $args,
),
$url
);
$http_args = array(
'timeout' => 15,
'user-agent' => 'WordPress;' . home_url( '/' ),
);
$request = wp_remote_get( $url, $http_args );
$plugin_info = json_decode($request['body']);
$plugin_last_update = new \DateTime($plugin_info->last_updated);
$today = new \DateTime( date('Y-m-d', time() ) ); // use to get a return of number of years since update
$diff = $today->diff($plugin_last_update);
return $diff->y ;
}
/**
* Adds a warning for every plugin which has not received an update in the past year.
*
* @since 0.1.0
*
* @param string $plugin_file String with directory and filename for plugin. IE 'akismet/akismet.php'
* @param string $plugin_data Array that contains plugin data.
*
*/
function add_plugin_warning($plugin_file, $plugin_data){
if( isset($plugin_data['slug'] ) ){
$last_updated = get_last_updated_date($plugin_data['slug']);
if( $last_updated > 0 ) {
printf (
'<tr class="plugin-old-tr">
<td colspan="3" class="plugin-old colspanchange">
<div class="notice inline notice-warning notice-alt">
<p>'
);
echo sprintf( __( 'It looks like %1$s hasn\'t received updates in the last %2$d year(s). It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.', 'plugin-last-updated-warning' ), $plugin_data['Name'], $last_updated );
printf(
'</p>
</div>
</td>
</tr>'
);
}
}
}
/**
* Does a foreach loop to check all installed plugins.
*
* @since 0.1.0
*
*/
function get_all_plugins(){
$plugins = \get_plugins();
foreach ($plugins as $plugin_file=>$plugin_data){
add_action("after_plugin_row_" . $plugin_file . "", 'MilesToGo\\add_plugin_warning', 10, 3);
}
}
add_action('admin_init', 'MilesToGo\\get_all_plugins' );
}
}
Plugin_Last_Updated_Warning::init();
}