-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-plugin-banner.php
115 lines (94 loc) · 3.28 KB
/
wp-plugin-banner.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
<?php
/*
Plugin Name: WP Plugin Banner
Plugin URI: https://chrisk.io
Description: Adds a shortcode to display a banner from a plugin in the WordPress repository
Author: cklosows
Version: 1.0.2
Author URI: https://chrisk.io
Text Domain: wp-plugin-banner
Domain Path: languages
*/
if ( ! class_exists( 'WP_Plugin_Banner' ) ) {
class WP_Plugin_Banner {
private static $instance;
private function __construct() {
$this->init();
}
static public function instance() {
if ( !self::$instance ) {
self::$instance = new WP_Plugin_Banner();
}
return self::$instance;
}
private function init() {
add_shortcode( 'plugin_banner', array( $this, 'display_banner' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'load_styles' ) );
}
public function display_banner( $atts ) {
$atts = shortcode_atts(
array(
'slug' => '',
'link' => false,
'title_wrapper' => 'h2',
),
$atts,
'wp_plugin_banners_display_atts'
);
$allowed_wrappers = array( 'h2', 'h3', 'h4', 'h5', 'p', 'strong', 'span', 'em' );
if ( ! in_array( $atts['title_wrapper'], $allowed_wrappers ) ) {
$atts['title_wrapper'] = 'h2';
}
if ( empty( $atts['slug'] ) ) {
return;
}
$image_url = 'https://plugins.svn.wordpress.org/' . $atts['slug'] . '/assets/banner-772x250.png';
$link_url = $atts['link'] ? 'https://wordpress.org/plugins/' . $atts['slug'] : '';
$image_exists = get_transient( 'wppb_has_img_' . $atts['slug'] );
if ( false === $image_exists ) {
$image_test = wp_remote_head( $image_url );
$image_exists = ! is_wp_error( $image_test ) && 200 == $image_test['response']['code'] ? true : '';
set_transient( 'wppb_has_img_' . $atts['slug'], $image_exists, WEEK_IN_SECONDS );
}
$plugin_data = get_transient( 'wppb_data_' . $atts['slug'] );
if ( false === $plugin_data ) {
$request = wp_remote_get( 'https://api.wordpress.org/plugins/info/1.0/' . $atts['slug'] . '.json' );
$plugin_data = ! is_wp_error( $request ) && 200 === $request['response']['code'] ? json_decode( wp_remote_retrieve_body( $request ), true ) : array();
set_transient( 'wppb_data_' . $atts['slug'], $plugin_data, WEEK_IN_SECONDS );
}
ob_start();
?>
<<?php echo $atts['title_wrapper']; ?> itemprop="name"><?php echo $plugin_data['name']; ?></<?php echo $atts['title_wrapper']; ?>>
<?php
if ( empty( $image_exists ) ) {
$output = ob_get_contents();
ob_end_clean();
return $output;
};
if ( ! empty( $link_url ) ) {
?><a class="wp-plugin-banner-link <?php echo $atts['slug']; ?>" target="_blank" rel="noopener" href="<?php echo $link_url; ?>"><?php
}
?>
<div class="plugin-title <?php echo $atts['slug']; ?>" style="background-image: url(<?php echo $image_url; ?>)">
<div class="vignette"></div>
</div>
<?php
if ( ! empty( $link_url ) ) {
?></a><?php
}
$output = ob_get_contents();
ob_end_clean();
return $output;
}
public function load_styles() {
$plugin_url = trailingslashit( plugin_dir_url( __FILE__ ) );
$assets_url = $plugin_url . 'assets/';
wp_register_style( 'wp-plugin-banner', $assets_url . 'style.css', array(), false, 'all' );
wp_enqueue_style( 'wp-plugin-banner' );
}
}
}
function load_wp_plugin_banner() {
return WP_Plugin_Banner::instance();
}
add_action( 'plugins_loaded', 'load_wp_plugin_banner', PHP_INT_MAX );