-
Notifications
You must be signed in to change notification settings - Fork 18
/
post-thumbnail-editor.php
executable file
·478 lines (411 loc) · 13.2 KB
/
post-thumbnail-editor.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/*
Plugin name: Post Thumbnail Editor
Plugin URI: http://sewpafly.github.io/post-thumbnail-editor/
Author: sewpafly
Author URI: http://sewpafly.github.io/post-thumbnail-editor/
Version: 2.4.8
Description: Individually manage your post thumbnails
LICENSE
=======
Copyright 2013 (email : sewpafly@gmail.com)
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Useful constants
*/
define( 'PTE_PLUGINURL', plugins_url(basename( dirname(__FILE__))) . "/");
define( 'PTE_PLUGINPATH', dirname(__FILE__) . "/");
define( 'PTE_DOMAIN', "post-thumbnail-editor");
define( 'PTE_VERSION', "2.4.8");
// TODO:
// * Find the best place for the require log (only when it's really needed, create an init function?)
// * Change all the log calls?
// * Rip out everything that's not a CONSTANT or a hook in here
// * Make this an object
// * Add a tour for new users
require_once( PTE_PLUGINPATH . 'php/log.php' );
/*
* Option Functionality
*/
function pte_get_option_name(){
global $current_user;
if ( ! isset( $current_user ) ){
get_currentuserinfo();
}
return "pte-option-{$current_user->ID}";
}
function pte_get_user_options(){
$pte_options = get_option( pte_get_option_name() );
if ( !is_array( $pte_options ) ){
$pte_options = array();
}
$defaults = array( 'pte_debug' => false
, 'pte_crop_save' => false
, 'pte_thumbnail_bar' => 'horizontal'
, 'pte_imgedit_disk' => false
, 'pte_imgedit_max_size' => 600
, 'pte_debug_out_chrome' => false
, 'pte_debug_out_file' => false
);
// WORDPRESS DEBUG overrides user setting...
return array_merge( $defaults, $pte_options );
}
function pte_get_site_options(){
$pte_site_options = get_option( 'pte-site-options' );
if ( !is_array( $pte_site_options ) ){
$pte_site_options = array();
}
$defaults = array( 'pte_hidden_sizes' => array()
, 'cache_buster' => true
);
return array_merge( $defaults, $pte_site_options );
}
function pte_get_options(){
global $pte_options, $current_user;
if ( isset( $pte_options ) ){
return $pte_options;
}
$pte_options = array_merge( pte_get_user_options(), pte_get_site_options() );
if ( WP_DEBUG )
$pte_options['pte_debug'] = true;
if ( !isset( $pte_options['pte_jpeg_compression'] ) ){
$pte_options['pte_jpeg_compression'] = apply_filters( 'jpeg_quality', 90, 'pte_options' );
}
return $pte_options;
}
function pte_update_user_options(){
require_once( PTE_PLUGINPATH . 'php/options.php' );
$options = pte_get_user_options();
// Check nonce
if ( !check_ajax_referer( "pte-options", 'pte-nonce', false ) ){
return pte_json_error( "CSRF Check failed" );
}
if ( isset( $_REQUEST['pte_crop_save'] ) ) {
if ( strtolower( $_REQUEST['pte_crop_save'] ) === "true" )
$options['pte_crop_save'] = true;
else
$options['pte_crop_save'] = false;
}
if ( isset( $_REQUEST['pte_thumbnail_bar'] ) ) {
if ( strtolower( $_REQUEST['pte_thumbnail_bar'] ) == 'vertical' )
$options['pte_thumbnail_bar'] = 'vertical';
else
$options['pte_thumbnail_bar'] = 'horizontal';
}
update_option( pte_get_option_name(), $options );
}
/**
* Get the URL for the PTE interface
*
* @param $id the post id of the attachment to modify
*/
function pte_url( $id, $iframe=false ){
if ($iframe) {
$pte_url = admin_url( 'admin-ajax.php' )
. "?action=pte_ajax&pte-action=iframe&pte-id={$id}"
. "&TB_iframe=true";
}
else {
$pte_url = admin_url('upload.php')
. "?page=pte-edit&pte-id={$id}";
}
return $pte_url;
}
/**
* Used in functions.php, log.php & options.php to get pseudo-TMP file paths
*/
function pte_tmp_dir()
{
$uploads = wp_upload_dir();
$PTE_TMP_DIR = $uploads['basedir'] . DIRECTORY_SEPARATOR . "ptetmp" . DIRECTORY_SEPARATOR;
$PTE_TMP_URL = $uploads['baseurl'] . "/ptetmp/";
return compact( 'PTE_TMP_DIR', 'PTE_TMP_URL' );
}
/*
* Put Hooks and immediate hook functions in this file
*/
/** For the "Edit Image" stuff **/
/* Hook into the Edit Image page */
add_action('dbx_post_advanced', 'pte_edit_form_hook_redirect');
/* Slight redirect so this isn't called on all versions of the media upload page */
function pte_edit_form_hook_redirect(){
add_action('add_meta_boxes', 'pte_admin_media_scripts');
}
add_action( 'media_upload_library', 'pte_admin_media_scripts_editor' );
add_action( 'media_upload_gallery', 'pte_admin_media_scripts_editor' );
add_action( 'media_upload_image', 'pte_admin_media_scripts_editor' );
function pte_admin_media_scripts_editor(){
pte_admin_media_scripts('attachment');
}
function pte_admin_media_scripts($post_type){
$options = pte_get_options();
pte_add_thickbox();
if ($post_type == "attachment") {
wp_enqueue_script( 'pte'
, PTE_PLUGINURL . 'apps/coffee-script.js'
, array('underscore')
, PTE_VERSION
);
add_action( 'admin_print_footer_scripts', 'pte_enable_editor_js', 100);
}
else {
//add_action( 'admin_print_footer_scripts', 'pte_enable_media_js', 100);
wp_enqueue_script( 'pte'
, PTE_PLUGINURL . 'js/snippets/pte_enable_media.js'
, array('media-views')
, PTE_VERSION
, true
);
wp_enqueue_style( 'pte'
, PTE_PLUGINURL . 'css/pte-media.css'
, NULL
, PTE_VERSION
);
}
wp_localize_script('pte'
, 'pteL10n'
, array('PTE' => __('Post Thumbnail Editor', PTE_DOMAIN)
, 'url' => pte_url( "<%= id %>", true )
, 'fallbackUrl' => pte_url( "<%= id %>" )
)
);
}
function pte_enable_editor_js(){
injectCoffeeScript( PTE_PLUGINPATH . "js/snippets/editor.coffee" );
}
function pte_enable_media_js(){
injectCoffeeScript( PTE_PLUGINPATH . "js/snippets/media.coffee" );
}
function injectCoffeeScript($coffeeFile){
$coffee = @file_get_contents( $coffeeFile );
//$options = json_encode( pte_get_options() );
echo <<<EOT
<script type="text/coffeescript">
$coffee
</script>
EOT;
}
// Add the PTE link to the featured image in the post screen
// Called in wp-admin/includes/post.php
add_filter( 'admin_post_thumbnail_html', 'pte_admin_post_thumbnail_html', 10, 2 );
function pte_admin_post_thumbnail_html( $content, $post_id ){
pte_add_thickbox();
$thumbnail_id = get_post_thumbnail_id( $post_id );
if ( $thumbnail_id == null )
return $content;
return $content .= '<p id="pte-link" class="hide-if-no-js"><a class="thickbox" href="'
. pte_url( $thumbnail_id, true )
. '">'
. esc_html__( 'Post Thumbnail Editor', PTE_DOMAIN )
. '</a></p>';
}
/* Fix wordpress ridiculousness about making a thickbox max width=720 */
function pte_add_thickbox() {
add_thickbox();
wp_enqueue_script('pte-fix-thickbox',
PTE_PLUGINURL . "js/snippets/pte-fix-thickbox.js",
array( 'media-upload' ),
PTE_VERSION
);
}
/* For all purpose needs */
add_action('wp_ajax_pte_ajax', 'pte_ajax');
function pte_ajax(){
// Move all adjuntant functions to a separate file and include that here
require_once(PTE_PLUGINPATH . 'php/functions.php');
PteLogger::debug( "PARAMETERS: " . print_r( $_REQUEST, true ) );
//header('Content-type: application/json');
switch ($_GET['pte-action'])
{
case "iframe":
pte_init_iframe();
break;
case "resize-images":
pte_resize_images();
break;
case "confirm-images":
pte_confirm_images();
break;
case "delete-images":
pte_delete_images();
break;
case "get-thumbnail-info":
$id = (int) $_GET['id'];
if ( pte_check_id( $id ) )
print( json_encode( pte_get_all_alternate_size_information( $id ) ) );
break;
case "change-options":
pte_update_user_options();
break;
}
die();
}
/**
* Perform the capability check
*
* @param $id References the post that the user needs to have permission to edit
* @returns boolean true if the current user has permission else false
*/
function pte_check_id( $id ) {
if ( !$post = get_post( $id ) ) {
return false;
}
if ( current_user_can( 'edit_post', $id )
|| current_user_can( 'pte_edit', $id ) )
{
return apply_filters( 'pte-capability-check', true, $id );
}
return apply_filters( 'pte-capability-check', false, $id );
}
/**
* Upload.php (the media library page) fires:
* - 'load-upload.php' (wp-admin/admin.php)
* - GRID VIEW:
* + 'wp_enqueue_media' (upload.php:wp-includes/media.php:wp_enqueue_media)
* - LIST VIEW:
* + 'media_row_actions' (filter)(class-wp-media-list-table.php)
*/
add_action('load-upload.php', 'pte_media_library_boot');
function pte_media_library_boot() {
add_action('wp_enqueue_media', 'pte_load_media_library');
}
function pte_load_media_library() {
global $mode;
if ('grid' !== $mode) return;
wp_enqueue_script( 'pte'
, PTE_PLUGINURL . 'js/snippets/pte_enable_media.js'
, null
, PTE_VERSION
, true
);
wp_localize_script('pte'
, 'pteL10n'
, array('PTE' => __('Post Thumbnail Editor', PTE_DOMAIN)
, 'url' => pte_url( "<%= id %>", true )
, 'fallbackUrl' => pte_url( "<%= id %>" )
)
);
}
/* Adds the Thumbnail option to the media library list */
add_filter('media_row_actions', 'pte_media_row_actions', 10, 3); // priority: 10, args: 3
function pte_media_row_actions($actions, $post, $detached){
// Add capability check
if ( !pte_check_id( $post->ID ) ) {
return $actions;
}
$options = pte_get_options();
$pte_url = pte_url( $post->ID );
$actions['pte'] = "<a href='${pte_url}' title='"
. __( 'Edit Thumbnails', PTE_DOMAIN )
. "'>" . __( 'Thumbnails', PTE_DOMAIN ) . "</a>";
return $actions;
}
/* Add Settings Page */
add_action( 'load-settings_page_pte', 'pte_options' );
/* Add Settings Page -> Submit/Update options */
add_action( 'load-options.php', 'pte_options' );
function pte_options(){
require_once( PTE_PLUGINPATH . 'php/options.php' );
pte_options_init();
}
/* Add SubMenus/Pages */
add_action( 'admin_menu', 'pte_admin_menu' );
/**
* These pages are linked into the hook system of wordpress, this means
* that almost any wp_admin page will work as long as you append "?page=pte"
* or "?page=pte-edit". Try the function `'admin_url("index.php") . '?page=pte';`
*
* The function referred to here will output the HTML for the page that you want
* to display. However if you want to hook into enqueue_scripts or styles you
* should use the page-suffix that is returned from the function. (e.g.
* `add_action("load-".$hook, hook_func);`)
*
* There is also another hook with the same name as the hook that's returned.
* I don't remember in which order it is launched, but I believe the pertinent
* code is in admin-header.php.
*/
function pte_admin_menu(){
add_options_page( __('Post Thumbnail Editor', PTE_DOMAIN),
__('Post Thumbnail Editor', PTE_DOMAIN),
'edit_posts',
'pte',
'pte_launch_options_page'
);
// The submenu page function does not put a menu item in the wordpress sidebar.
add_submenu_page(NULL, __('Post Thumbnail Editor', PTE_DOMAIN),
__('Post Thumbnail Editor', PTE_DOMAIN),
'edit_posts',
'pte-edit',
'pte_edit_page'
);
}
function pte_launch_options_page(){
require_once( PTE_PLUGINPATH . 'php/options.php' );
pte_options_page();
}
/**
* This runs after headers have been sent, see the pte_edit_setup for the
* function that runs before anything is sent to the browser
*/
function pte_edit_page(){
// This is set via the pte_edit_setup function
global $pte_body;
echo( $pte_body );
}
/* Admin Edit Page: setup*/
/**
* This hook (load-media_page_pte-edit)
* depends on which page you use in the admin section
* (load-media_page_pte-edit) : wp-admin/upload.php?page=pte-edit
* (dashboard_page_pte-edit) : wp-admin/?page=pte-edit
* (posts_page_pte-edit) : wp-admin/edit.php?page=pte-edit
*/
add_action( 'load-media_page_pte-edit', 'pte_edit_setup' );
function pte_edit_setup() {
global $post, $title, $pte_body;
$post_id = (int) $_GET['pte-id'];
if ( !isset( $post_id )
|| !is_int( $post_id )
|| !wp_attachment_is_image( $post_id )
|| !pte_check_id( $post_id ) ) {
//die("POST: $post_id IS_INT:" . is_int( $post_id ) . " ATTACHMENT: " . wp_attachment_is_image( $post_id ));
wp_redirect( admin_url( "upload.php" ) );
exit();
}
$post = get_post( $post_id );
$title = __( "Post Thumbnail Editor", PTE_DOMAIN );
include_once( PTE_PLUGINPATH . "php/functions.php" );
// Add the scripts and styles
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-ui-dialog' );
wp_enqueue_script( 'iris' );
wp_enqueue_style( 'colors' );
wp_enqueue_style( 'wp-jquery-ui-dialog' );
$pte_body = pte_body( $post->ID );
}
/**
* This code creates the image used for the crop
*
* By overwriting the wordpress code (same functions), we can change the default size
* to our own option.
*/
add_action('wp_ajax_pte_imgedit_preview','pte_wp_ajax_imgedit_preview_wrapper');
function pte_wp_ajax_imgedit_preview_wrapper(){
require_once( PTE_PLUGINPATH . "php/overwrite_imgedit_preview.php" );
pte_wp_ajax_imgedit_preview();
}
/** End Settings Hooks **/
load_plugin_textdomain( PTE_DOMAIN
, false
, basename( PTE_PLUGINPATH ) . DIRECTORY_SEPARATOR . "i18n" );