-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-external-media.php
285 lines (237 loc) · 8.1 KB
/
add-external-media.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
277
278
279
280
281
282
283
284
285
<?php
/*
Plugin Name: Add External Media
Plugin URI: http://wordpress.org/plugins/add-external-media/
Description: Add external media to the media library
Version: 1.0.5
Author: leemon
Text Domain: add-external-media
License: GPLv2 or later
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class AddExternalMedia {
/**
* Plugin instance.
*
* @since 1.0
*
*/
protected static $instance = null;
/**
* Access this plugin’s working instance
*
* @since 1.0
*
*/
public static function get_instance() {
if ( !self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Used for regular plugin work.
*
* @since 1.0
*
*/
public function plugin_setup() {
$this->includes();
add_action( 'init', array( $this, 'load_language' ) );
add_action( 'wp_enqueue_media', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'wp_ajax_add-oembed', array( $this, 'add_oembed' ) );
add_action( 'print_media_templates', array( $this, 'print_media_templates' ) );
add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), null, 2 );
add_filter( 'attachment_fields_to_save', array( $this, 'attachment_fields_to_save' ), null, 2 );
add_filter( 'media_view_strings', array( $this, 'media_view_strings' ), 10, 2 );
add_filter( 'wp_prepare_attachment_for_js', array( $this, 'wp_prepare_attachment_for_js' ), 10, 3 );
}
/**
* Constructor. Intentionally left empty and public.
*
* @since 1.0
*
*/
public function __construct() {}
/**
* Includes required core files used in admin and on the frontend.
*
* @since 1.0
*
*/
protected function includes() {
if ( version_compare( $GLOBALS['wp_version'], '5.3-alpha', '<' ) ) {
require_once ABSPATH . WPINC . '/class-oembed.php';
} else {
require_once ABSPATH . WPINC . '/class-wp-oembed.php';
}
}
/**
* Loads language
*
* @since 1.0
*
*/
function load_language() {
load_plugin_textdomain( 'add-external-media' );
}
/**
* Enqueues scripts in the backend.
*
* @since 1.0
*
*/
function admin_enqueue_scripts() {
wp_enqueue_script( 'add-external-media', plugins_url( '/add-external-media.js', __FILE__), array( 'media-models', 'media-views' ), false, true );
}
/**
* Adds an oEmbed attachment to the database.
*
* @since 1.0
*
*/
function add_oembed() {
$url = trim( $_POST['url'] );
$width = absint( $_POST['width'] );
$height = absint( $_POST['height'] );
$post_ID = intval( $_POST['post_id'] );
$nonce = $_POST['nonce'];
if ( !wp_verify_nonce( $nonce, 'update-post_' . $post_ID ) ) {
wp_send_json_error();
}
if ( !current_user_can( 'edit_post', $post_ID ) ) {
wp_send_json_error();
}
$oembed = new WP_oEmbed();
$provider = $oembed->get_provider( $url );
if ( $provider === false && substr( $url, 0, 5 ) === 'https' ) {
$url = str_replace( 'https', 'http', $url );
$provider = $oembed->get_provider( $url );
}
if ( $provider === false ) {
wp_send_json_error();
}
$response = $oembed->fetch( $provider, $url );
if ( $response === false ) {
wp_send_json_error();
}
$attachment = array(
'post_parent' => $post_ID,
'post_title' => $response->title,
'post_content' => '',
'post_status' => 'inherit',
'post_author' => get_current_user_id(),
'post_type' => 'attachment',
'guid' => $url,
'post_mime_type' => 'oembed/' . strtolower( $response->provider_name )
);
$attachment_id = wp_insert_post( $attachment );
if ( ! is_int( $attachment_id ) ) {
wp_send_json_error();
}
$width = ( !empty( $width ) ? $width : 400 );
$height = ( !empty( $height ) ? $height : 400 );
update_post_meta( $attachment_id, '_oembed_width', $width );
update_post_meta( $attachment_id, '_oembed_height', $height );
if ( ! $attachment_js = wp_prepare_attachment_for_js( $attachment_id ) ) {
wp_send_json_error();
}
wp_send_json_success( $attachment_js );
}
/**
* Adds the width and height fields to the attachment details panel.
*
* @since 1.0
*
*/
function attachment_fields_to_edit( $form_fields, $post ) {
$mime = get_post_mime_type( $post->ID );
$type = strtok( $mime, '/' );
if ( $type == 'oembed' ) {
$form_fields['width'] = array(
'label' => __( 'Width' ),
'input' => 'text',
'value' => get_post_meta( $post->ID, '_oembed_width', true )
);
$form_fields['height'] = array(
'label' => __( 'Height' ),
'input' => 'text',
'value' => get_post_meta( $post->ID, '_oembed_height', true )
);
}
return $form_fields;
}
/**
* Save the width and height field values to the database.
*
* @since 1.0
*
*/
function attachment_fields_to_save( $post, $attachment ) {
if ( isset( $attachment['width'] ) ) {
update_post_meta( $post['ID'], '_oembed_width', $attachment['width'] );
}
if ( isset( $attachment['height'] ) ) {
update_post_meta( $post['ID'], '_oembed_height', $attachment['height'] );
}
return $post;
}
/**
* Prints the attachment details media view template.
*
* @since 1.0
*
*/
function print_media_templates() {
?>
<script type="text/html" id="tmpl-add-external-settings">
<div class="embed-container" style="display: none;">
<div class="embed-preview"></div>
</div>
<label class="setting width">
<span><?php _e( 'Width' ); ?></span>
<input type="text" class="alignment" data-setting="width" />
</label>
<label class="setting height">
<span><?php _e( 'Height' ); ?></span>
<input type="text" class="alignment" data-setting="height" />
</label>
</script>
<?php
}
/**
* Defines the menu item and button strings.
*
* @since 1.0
*
*/
function media_view_strings( $strings, $post ) {
$strings['AddExternalMediaMenuTitle'] = _x( 'Add External Media', 'menu item', 'add-external-media' );
$strings['AddExternalMediaButton'] = __( 'Add to library', 'add-external-media' );
return $strings;
}
/**
* Sets the filename of an oembed library item to its title.
*
* @since 1.0
*
*/
function wp_prepare_attachment_for_js( $response, $attachment, $meta ) {
if ( $response['type'] == 'oembed' ) {
if ( $response['title'] != '' ) {
$response['filename'] = mb_strimwidth( $response['title'], 0, 25 );
}
}
return $response;
}
}
add_action( 'plugins_loaded', array ( AddExternalMedia::get_instance(), 'plugin_setup' ) );