forked from sewpafly/post-thumbnail-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-thumbnail-editor.php
executable file
·292 lines (254 loc) · 9.19 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
<?php
/* Plugin name: Post Thumbnail Editor
Plugin URI: http://wordpress.org/extend/plugins/post-thumbnail-editor/
Author: sewpafly
Author URI: http://sewpafly.github.com/post-thumbnail-editor
Version: 2.0.0
Description: Individually manage your post thumbnails
LICENSE
=======
Copyright 2013 (email : [email protected])
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
CREDITS
=======
*/
/*
* 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.0.0");
/*
* 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_tb_width' => 750
, 'pte_tb_height' => 550
, 'pte_debug' => 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() );
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;
}
/*
* Put Hooks and immediate hook functions in this file
*/
/* Hook into the Edit Image page */
function pte_admin_media_scripts($post_type){
//print("yessir:$post_type:\n");
$options = pte_get_options();
wp_enqueue_script( 'pte'
, PTE_PLUGINURL . 'apps/coffee-script.js'
, array()
, PTE_VERSION
);
wp_localize_script('pte'
, 'objectL10n'
, array('PTE' => __('Post Thumbnail Editor', PTE_DOMAIN))
);
if ($post_type == "attachment") {
add_action("admin_print_footer_scripts","pte_enable_admin_js",100);
}
else {
add_action("admin_print_footer_scripts","pte_enable_media_js",100);
}
}
function pte_enable_admin_js(){
injectCoffeeScript( PTE_PLUGINPATH . "js/snippets/admin.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;
}
// Base url/function. All pte interactions go through here
function pte_ajax(){
// Move all adjuntant functions to a separate file and include that here
require_once(PTE_PLUGINPATH . 'php/functions.php');
$logger = PteLogger::singleton();
$logger->debug( "PARAMETERS: " . print_r( $_REQUEST, true ) );
switch ($_GET['pte-action'])
{
case "test":
pte_test();
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 = pte_check_id((int) $_GET['id']);
print( json_encode( pte_get_all_alternate_size_information( $id ) ) );
break;
}
die();
}
function pte_media_row_actions($actions, $post, $detached){
// Add capability check
if ( !current_user_can( 'edit_post', $post->ID ) ){
return $actions;
}
$options = pte_get_options();
$pte_url = admin_url('upload.php')
. "?page=pte-edit&post="
. $post->ID;
//. "&TB_iframe=true&height={$options['pte_tb_height']}&width={$options['pte_tb_width']}";
$actions['pte'] = "<a href='${pte_url}' title='"
. __( 'Edit Thumbnails', PTE_DOMAIN )
. "'>" . __( 'Thumbnails', PTE_DOMAIN ) . "</a>";
return $actions;
}
// Anonymous function (which apparently some versions of PHP will whine about)
function pte_launch_options_page(){
require_once( PTE_PLUGINPATH . 'php/options.php' ); pte_options_page();
}
function pte_edit_page(){
global $post;
include_once( PTE_PLUGINPATH . "php/functions.php" );
pte_launch( PTE_PLUGINPATH . "html/pte.php", $post->ID );
}
/**
* 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) . "-title",
__('Post Thumbnail Editor', PTE_DOMAIN),
'edit_posts',
'pte',
'pte_launch_options_page'
);
/**
* This hook depends on which page you use in the admin section
*/
$hook = add_submenu_page( NULL, __('Post Thumbnail Editor', PTE_DOMAIN) . "-edit",
__('Post Thumbnail Editor', PTE_DOMAIN),
'edit_posts',
'pte-edit',
'pte_edit_page'
);
//print( "HOOK: $hook" );
}
function pte_options(){
require_once( PTE_PLUGINPATH . 'php/options.php' );
pte_options_init();
}
/* This is the main admin media page */
/** For the "Edit Image" stuff **/
//add_action('edit_form_advanced', 'pte_admin_media_scripts');
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');
}
/* Adds the Thumbnail option to the media library list */
add_filter('media_row_actions', 'pte_media_row_actions', 10, 3); // priority: 10, args: 3
/* For all purpose needs */
add_action('wp_ajax_pte_ajax', 'pte_ajax');
/* Add SubMenus/Pages */
add_action( 'admin_menu', 'pte_admin_menu' );
/* Add Settings Page */
add_action( 'load-settings_page_pte', 'pte_options' );
/* Add Settings Page -> Submit/Update options */
add_action( 'load-options.php', 'pte_options' );
//add_action( 'admin_init', 'pte_options' );
/* 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;
$post_id = (int) $_GET['post'];
if ( !isset( $post_id ) || !is_int( $post_id ) || !wp_attachment_is_image( $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 );
// Enqueue Scripts
// Would do jquery here, but looks to be default enqueued out of the box
wp_localize_script( 'jquery', // Called on every admin page :)
'pteI18n',
array( 'no_t_selected' => __( 'No thumbnails selected', PTE_DOMAIN )
, 'no_c_selected' => __( 'No crop selected', PTE_DOMAIN )
, 'crop_problems' => __( 'Cropping will likely result in skewed imagery', PTE_DOMAIN )
, 'save_crop_problem' => __( 'There was a problem saving the crop...', PTE_DOMAIN )
)
);
}
/** End Settings Hooks **/
load_plugin_textdomain( PTE_DOMAIN
, false
, basename( PTE_PLUGINPATH ) . DIRECTORY_SEPARATOR . "i18n" );
/** Test Settings **/
//add_image_size( 'pte test 1', 100, 0 );
?>