-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-downloads.php
More file actions
108 lines (83 loc) · 2.38 KB
/
plugin-downloads.php
File metadata and controls
108 lines (83 loc) · 2.38 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
<?php
/*
Plugin Name: Plugin Downloads
Description: Shortcode to list plugin download links
Version: 1.0.1
Author: Wayne Allen
Author URI: http://postieplugin.com
Requires at least: 4.0
Tested up to: 4.7.3
*/
/*
* Change Log
* 1.0.1 - 2017-03-31
* Initial release
*/
if (!defined('WPINC')) {
die; // Exit if accessed directly
}
// Add Shortcode
function plugin_downloads_shortcode($atts) {
$atts = shortcode_atts(array('slug' => '', 'limit' => 20), $atts);
$plugin = get_my_plugin_downloads($atts['slug']);
$html = '<h2>Current Version</h2>';
$html .= "<div>{$plugin['version']} - <a href='{$plugin['url']}'>download</a></div>";
$html .= '<h2>Other Versions</h2>';
$html .= '<ul>';
$i = 0;
foreach ($plugin['tags'] as $tag) {
$html .= "<li>{$tag['version']} - <a href='{$tag['url']}'>download</a></li>";
if ($i++ > $atts['limit']) {
break;
}
}
$html .= '</ul>';
return $html;
}
add_shortcode('plugin-downloads', 'plugin_downloads_shortcode');
/*
* get_my_plugin_downloads
*
* This function will return an array of plugin download information
*
* @type function
* @date 31/3/17
* @since 5.5.10
*
* @param $slug (string)
* @return (array)
*/
function get_my_plugin_downloads($slug = '') {
// vars
$plugin = array(
'version' => 0,
'download' => '',
'tags' => array()
);
$url = 'http://api.wordpress.org/plugins/info/1.0/' . $slug;
// connect
$request = wp_remote_post($url);
// success
if (!is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) {
// unserialize
$obj = @unserialize($request['body']);
// version
$plugin['version'] = $obj->version;
$plugin['url'] = 'https://downloads.wordpress.org/plugin/' . $slug . '.zip';
// tags
preg_match_all('/<h4>(.+?)<\/h4>/', $obj->sections['changelog'], $matches);
// add tags
if (isset($matches[1])) {
foreach ($matches[1] as $tag) {
$versions = explode(' ', $tag);
$version = $versions[0];
$plugin['tags'][] = array(
'version' => $version,
'url' => str_replace('.zip', '.' . $version . '.zip', $plugin['url'])
);
}
}
}
// return
return $plugin;
}