forked from Automattic/media-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.mexp.php
291 lines (224 loc) · 6.79 KB
/
class.mexp.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
286
287
288
289
290
291
<?php
/*
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.
*/
class Media_Explorer extends MEXP_Plugin {
/**
* Array of Service objects.
*/
public $services = array();
/**
* Class constructor. Set up some actions and filters.
*
* @return null
*/
protected function __construct( $file ) {
# Filters:
# (none)
# Actions:
add_action( 'plugins_loaded', array( $this, 'action_plugins_loaded' ) );
add_action( 'init', array( $this, 'action_init' ) );
add_action( 'wp_enqueue_media', array( $this, 'action_enqueue_media' ) );
add_action( 'print_media_templates', array( $this, 'action_print_media_templates' ) );
# AJAX actions:
add_action( 'wp_ajax_mexp_request', array( $this, 'ajax_request' ) );
# Parent setup:
parent::__construct( $file );
}
/**
* Retrieve a registered Service object by its ID.
*
* @param string $service_id A service ID.
* @return Service|WP_Error A Service object on success, a WP_Error object on failure.
*/
public function get_service( $service_id ) {
if ( isset( $this->services[$service_id] ) )
return $this->services[$service_id];
return new WP_Error(
'invalid_service',
sprintf( __( 'Media service "%s" was not found', 'mexp' ), esc_html( $service_id ) )
);
}
/**
* Retrieve all the registered services.
*
* @return array An array of registered Service objects.
*/
public function get_services() {
return $this->services;
}
/**
* Load the Backbone templates for each of our registered services.
*
* @action print_media_templates
* @return null
*/
public function action_print_media_templates() {
foreach ( $this->get_services() as $service_id => $service ) {
if ( ! $template = $service->get_template() )
continue;
// apply filters for tabs
$tabs = apply_filters( 'mexp_tabs', array() );
# @TODO this list of templates should be somewhere else. where?
foreach ( array( 'search', 'item' ) as $t ) {
foreach ( $tabs[$service_id] as $tab_id => $tab ) {
$id = sprintf( 'mexp-%s-%s-%s',
esc_attr( $service_id ),
esc_attr( $t ),
esc_attr( $tab_id )
);
$template->before_template( $id, $tab_id );
call_user_func( array( $template, $t ), $id, $tab_id );
$template->after_template( $id, $tab_id );
}
}
foreach ( array( 'thumbnail' ) as $t ) {
$id = sprintf( 'mexp-%s-%s',
esc_attr( $service_id ),
esc_attr( $t )
);
$template->before_template( $id );
call_user_func( array( $template, $t ), $id );
$template->after_template( $id );
}
}
}
/**
* Process an AJAX request and output the resulting JSON.
*
* @action wp_ajax_mexp_request
* @return null
*/
public function ajax_request() {
if ( !isset( $_POST['_nonce'] ) or !wp_verify_nonce( $_POST['_nonce'], 'mexp_request' ) )
die( '-1' );
$service = $this->get_service( stripslashes( $_POST['service'] ) );
if ( is_wp_error( $service ) ) {
do_action( 'mexp_ajax_request_error', $service );
wp_send_json_error( array(
'error_code' => $service->get_error_code(),
'error_message' => $service->get_error_message()
) );
}
foreach ( $service->requires() as $file => $class ) {
if ( class_exists( $class ) )
continue;
require_once sprintf( '%s/class.%s.php',
dirname( __FILE__ ),
$file
);
}
$request = wp_parse_args( stripslashes_deep( $_POST ), array(
'params' => array(),
'tab' => null,
'min_id' => null,
'max_id' => null,
'page' => 1,
) );
$request['page'] = absint( $request['page'] );
$request['user_id'] = absint( get_current_user_id() );
$request = apply_filters( 'mexp_ajax_request_args', $request, $service );
$response = $service->request( $request );
if ( is_wp_error( $response ) ) {
do_action( 'mexp_ajax_request_error', $response );
wp_send_json_error( array(
'error_code' => $response->get_error_code(),
'error_message' => $response->get_error_message()
) );
} else if ( is_a( $response, 'MEXP_Response' ) ) {
do_action( 'mexp_ajax_request_success', $response );
wp_send_json_success( $response->output() );
} else {
do_action( 'mexp_ajax_request_success', false );
wp_send_json_success( false );
}
}
/**
* Enqueue and localise the JS and CSS we need for the media manager.
*
* @action enqueue_media
* @return null
*/
public function action_enqueue_media() {
$mexp = array(
'_nonce' => wp_create_nonce( 'mexp_request' ),
'labels' => array(
'insert' => __( 'Insert into post', 'mexp' ),
'loadmore' => __( 'Load more', 'mexp' ),
),
'base_url' => untrailingslashit( $this->plugin_url() ),
'admin_url' => untrailingslashit( admin_url() ),
);
foreach ( $this->get_services() as $service_id => $service ) {
$service->load();
$tabs = apply_filters( 'mexp_tabs', array() );
$labels = apply_filters( 'mexp_labels', array() );
$mexp['services'][$service_id] = array(
'id' => $service_id,
'labels' => $labels[$service_id],
'tabs' => $tabs[$service_id],
);
}
// this action enqueues all the statics for each service
do_action( 'mexp_enqueue' );
wp_enqueue_script(
'mexp',
$this->plugin_url( 'js/mexp.js' ),
array( 'jquery', 'media-views' ),
$this->plugin_ver( 'js/mexp.js' )
);
wp_localize_script(
'mexp',
'mexp',
$mexp
);
wp_enqueue_style(
'mexp',
$this->plugin_url( 'css/mexp.css' ),
array( /*'wp-admin'*/ ),
$this->plugin_ver( 'css/mexp.css' )
);
}
/**
* Fire the `mexp_init` action for plugins to hook into.
*
* @action plugins_loaded
* @return null
*/
public function action_plugins_loaded() {
do_action( 'mexp_init' );
}
/**
* Load text domain and localisation files.
* Populate the array of Service objects.
*
* @action init
* @return null
*/
public function action_init() {
load_plugin_textdomain( 'mexp', false, dirname( $this->plugin_base() ) . '/languages/' );
foreach ( apply_filters( 'mexp_services', array() ) as $service_id => $service ) {
if ( is_a( $service, 'MEXP_Service' ) )
$this->services[$service_id] = $service;
}
}
/**
* Singleton instantiator.
*
* @param string $file The plugin file (usually __FILE__) (optional)
* @return Media_Explorer
*/
public static function init( $file = null ) {
static $instance = null;
if ( !$instance )
$instance = new Media_Explorer( $file );
return $instance;
}
}