-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbrowser-shots.php
276 lines (204 loc) · 6.71 KB
/
browser-shots.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
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Plugin Name: Browser Shots
* Plugin URI: https://wordpress.org/plugins/browser-shots/
* Description: Easily take dynamic screenshots of a website inside of WordPress
* Author: Ben Gillbanks
* Version: 1.7.7
* Author URI: https://prothemedesign.com
* Text Domain: browser-shots
*
* @package browser-shots
*/
// Define variable for JS and CSS versioning.
define( 'BROWSER_SHOTS_VERSION', '1.7.4' );
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
if ( ! class_exists( 'BrowserShots' ) ) {
/**
* Browsershots is a class for making use of the wordpress.com 'mshots' functionality to automatically take screenshots of websites.
*/
class BrowserShots {
/**
* Setup the object
*
* @param array $options An array of options for this object.
*/
public function __construct( $options = array() ) {
add_shortcode( 'browser-shot', array( $this, 'shortcode' ) );
add_action( 'init', array( $this, 'tinymce_button' ) );
add_action( 'plugins_loaded', array( $this, 'load_plugin_text_domain' ) );
}
/**
* Load plugin text domain
*/
public function load_plugin_text_domain() {
load_plugin_textdomain( 'browser-shots', '', basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* [browser-shot] Shortcode
*
* Create a shortcode: [browser-shot url="http://link-to-website" width="600"]
*
* @param array $attributes Shortcode properties.
* @param string $content Content of shortcode.
* @param string $code Code.
* @return string
*/
public function shortcode( $attributes, $content = '', $code = '' ) {
// Get attributes as parameters.
extract(
shortcode_atts(
array(
'url' => '',
'width' => 600,
'height' => 450,
'alt' => '',
'link' => '',
'target' => '',
'class' => '',
'image_class' => 'alignnone',
'rel' => '',
'display_link' => true,
'post_links' => false,
),
$attributes,
'browser-shots'
)
);
// Filter booleans.
$display_link = filter_var( $display_link, FILTER_VALIDATE_BOOLEAN );
$post_links = filter_var( $post_links, FILTER_VALIDATE_BOOLEAN );
if ( empty( $alt ) ) {
$parse = wp_parse_url( esc_url( $url ) );
if ( ! empty( $parse['host'] ) ) {
// translators: %s = domain name for site that is having a screenshot taken.
$alt = sprintf( __( 'Screenshot of %s', 'browser-shots' ), $parse['host'] );
} else {
// Fallback in case of relative path or other problem.
$alt = esc_url( $url );
}
}
if ( $post_links ) {
$link = esc_url( get_permalink( get_the_ID() ) );
}
// Use the permalink for the current page.
if ( 'PERMALINK' === $link || 'http://PERMALINK' === $link ) {
$link = esc_url( get_permalink( get_the_ID() ) );
}
if ( empty( $link ) ) {
$link = esc_url( $url );
}
if ( $rel ) {
$rel = ' rel="' . esc_attr( $rel ) . '"';
}
if ( $target ) {
$target = ' target="' . esc_attr( $target ) . '"';
}
// Get screenshot.
$image_uri = $this->get_shot( $url, $width, $height );
if ( ! empty( $image_uri ) ) {
ob_start();
if ( ! empty( $content ) ) {
echo '<div class="wp-caption ' . esc_attr( $image_class ) . '" style="width:' . ( intval( $width ) + 10 ) . 'px;">';
// Reset image_class so it's not used again.
$image_class = '';
}
echo '<div class="browser-shot ' . esc_attr( $image_class ) . '">';
if ( $display_link ) {
echo '<a href="' . esc_url( $link ) . '" ' . $target . $rel . '>';
}
echo '<img src="' . esc_url( $image_uri ) . '" alt="' . esc_attr( $alt ) . '" width="' . intval( $width ) . '" height="' . intval( $height ) . '" class="' . esc_attr( $image_class ) . '" />';
if ( $display_link ) {
echo '</a>';
}
echo '</div>';
if ( ! empty( $content ) ) {
echo '<p class="wp-caption-text">' . wp_kses_post( $content ) . '</p></div>';
}
return ob_get_clean();
}
return '';
}
/**
* Get Browser Screenshot
*
* Get a screenshot of a website using WordPress
*
* @param string $url Url of screenshot.
* @param int $width Width of screenshot.
* @param int $height Height of screenshot.
* @return string
*/
public static function get_shot( $url = '', $width = 600, $height = 450 ) {
// Image found.
if ( '' !== $url ) {
$query_args = array(
'w' => intval( $width ),
'h' => intval( $height ),
);
return add_query_arg( $query_args, 'https://s0.wp.com/mshots/v1/' . rawurlencode( esc_url( $url ) ) );
}
return '';
}
/**
* Register TinyMCE Button
*
* @param array $buttons List of TinyMCE buttons.
* @return array Modified list of TinyMCE buttons.
*/
public function register_button( $buttons ) {
array_push( $buttons, '|', 'browsershots' );
return $buttons;
}
/**
* Register TinyMCE Plugin
*
* @param array $plugins List of tinyMCE plugins already available.
* @return array Modified list of TinyMCE plugins
*/
public function add_plugin( $plugins ) {
$plugins['browsershots'] = plugins_url( 'js/browser-shots.js', __FILE__ );
return $plugins;
}
/**
* Add translations to TinyMCE button
*
* @param array $locales List of TinyMCE plugin locales.
* @return array Modified list of TinyMCE plugin locales.
*/
public function button_locale( $locales ) {
$locales['browsershots'] = plugin_dir_path( __FILE__ ) . 'locale.php';
return $locales;
}
/**
* Create TinyMCE Button
*/
public function tinymce_button() {
// Capabilities check.
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
return;
}
if ( 'true' === get_user_option( 'rich_editing' ) ) {
add_filter( 'mce_external_plugins', array( $this, 'add_plugin' ) );
add_filter( 'mce_buttons', array( $this, 'register_button' ) );
add_filter( 'mce_external_languages', array( $this, 'button_locale' ) );
}
}
}
new BrowserShots();
include 'src/class-browser-shots-gutenberg.php';
}