PageRenderTime 54ms CodeModel.GetById 15ms app.highlight 30ms RepoModel.GetById 3ms app.codeStats 0ms

/wp-content/plugins/post-thumbnail-editor/post-thumbnail-editor.php

https://github.com/pica-design/nextstepmaine
PHP | 439 lines | 272 code | 57 blank | 110 comment | 28 complexity | eb5f1e7688e69d9181f155575c3f9707 MD5 | raw file
  1<?php
  2/*
  3Plugin name: Post Thumbnail Editor
  4Plugin URI: http://sewpafly.github.io/post-thumbnail-editor/
  5Author: sewpafly
  6Author URI: http://sewpafly.github.io/post-thumbnail-editor/
  7Version: 2.4.0
  8Description: Individually manage your post thumbnails
  9
 10LICENSE
 11=======
 12
 13Copyright 2013  (email : sewpafly@gmail.com)
 14
 15This program is free software; you can redistribute it and/or modify
 16it under the terms of the GNU General Public License as published by
 17the Free Software Foundation; either version 2 of the License, or
 18(at your option) any later version.
 19
 20This program is distributed in the hope that it will be useful,
 21but WITHOUT ANY WARRANTY; without even the implied warranty of
 22MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 23GNU General Public License for more details.
 24
 25You should have received a copy of the GNU General Public License
 26along with this program; if not, write to the Free Software
 27Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 28
 29*/
 30
 31/* 
 32 * Useful constants  
 33 */
 34define( 'PTE_PLUGINURL', plugins_url(basename( dirname(__FILE__))) . "/");
 35define( 'PTE_PLUGINPATH', dirname(__FILE__) . "/");
 36define( 'PTE_DOMAIN', "post-thumbnail-editor");
 37define( 'PTE_VERSION', "2.4.0");
 38
 39// TODO:
 40// * Find the best place for the require log (only when it's really needed, create an init function?)
 41// * Change all the log calls?
 42// * Rip out everything that's not a CONSTANT or a hook in here
 43// * Make this an object
 44// * Add a tour for new users
 45require_once( PTE_PLUGINPATH . 'php/log.php' );
 46
 47/*
 48 * Option Functionality
 49 */
 50function pte_get_option_name(){
 51	global $current_user;
 52	if ( ! isset( $current_user ) ){
 53		get_currentuserinfo();
 54	}
 55	return "pte-option-{$current_user->ID}";
 56}
 57
 58function pte_get_user_options(){
 59	$pte_options = get_option( pte_get_option_name() );
 60	if ( !is_array( $pte_options ) ){
 61		$pte_options = array();
 62	}
 63	$defaults = array( 'pte_debug' => false
 64		, 'pte_crop_save' => false
 65		, 'pte_thumbnail_bar' => 'horizontal'
 66		, 'pte_imgedit_disk' => false
 67		, 'pte_imgedit_max_size' => 600
 68		, 'pte_debug_out_chrome' => false
 69		, 'pte_debug_out_file' => false
 70	);
 71
 72	// WORDPRESS DEBUG overrides user setting...
 73	return array_merge( $defaults, $pte_options );
 74}
 75
 76function pte_get_site_options(){
 77	$pte_site_options = get_option( 'pte-site-options' );
 78	if ( !is_array( $pte_site_options ) ){
 79		$pte_site_options = array();
 80	}
 81	$defaults = array( 'pte_hidden_sizes' => array()
 82		, 'cache_buster' => true
 83	);
 84	return array_merge( $defaults, $pte_site_options );
 85}
 86
 87function pte_get_options(){
 88	global $pte_options, $current_user;
 89	if ( isset( $pte_options ) ){
 90		return $pte_options;
 91	}
 92
 93	$pte_options = array_merge( pte_get_user_options(), pte_get_site_options() );
 94
 95	if ( WP_DEBUG )
 96		$pte_options['pte_debug'] = true;
 97
 98	if ( !isset( $pte_options['pte_jpeg_compression'] ) ){
 99		$pte_options['pte_jpeg_compression'] = apply_filters( 'jpeg_quality', 90, 'pte_options' );
100	}
101
102	return $pte_options;
103}
104
105function pte_update_user_options(){
106	require_once( PTE_PLUGINPATH . 'php/options.php' );
107	$options = pte_get_user_options();
108
109	if ( isset( $_REQUEST['pte_crop_save'] ) ) {
110		if ( strtolower( $_REQUEST['pte_crop_save'] ) === "true" )
111			$options['pte_crop_save'] = true;
112		else
113			$options['pte_crop_save'] = false;
114	}
115
116	if ( isset( $_REQUEST['pte_thumbnail_bar'] ) ) {
117		if ( strtolower( $_REQUEST['pte_thumbnail_bar'] ) == 'vertical' )
118			$options['pte_thumbnail_bar'] = 'vertical';
119		else
120			$options['pte_thumbnail_bar'] = 'horizontal';
121	}
122
123	update_option( pte_get_option_name(), $options );
124}
125
126
127
128/**
129 * Get the URL for the PTE interface
130 *
131 * @param $id the post id of the attachment to modify
132 */
133function pte_url( $id, $iframe=false ){
134	if ($iframe) {
135		$pte_url = admin_url( 'admin-ajax.php' )
136			. "?action=pte_ajax&pte-action=iframe&pte-id={$id}"
137			. "&TB_iframe=true";
138	}
139	else {
140		$pte_url = admin_url('upload.php') 
141			. "?page=pte-edit&pte-id={$id}";
142	}
143
144	return $pte_url;
145}
146
147
148/**
149 * Used in functions.php, log.php & options.php to get pseudo-TMP file paths
150 */
151function pte_tmp_dir()
152{
153	$uploads 	    = wp_upload_dir();
154	$PTE_TMP_DIR    = $uploads['basedir'] . DIRECTORY_SEPARATOR . "ptetmp" . DIRECTORY_SEPARATOR;
155	$PTE_TMP_URL    = $uploads['baseurl'] . "/ptetmp/";
156	return compact( 'PTE_TMP_DIR', 'PTE_TMP_URL' );
157}
158
159
160/*
161 * Put Hooks and immediate hook functions in this file
162 */
163
164/** For the "Edit Image" stuff **/
165/* Hook into the Edit Image page */
166add_action('dbx_post_advanced', 'pte_edit_form_hook_redirect');
167/* Slight redirect so this isn't called on all versions of the media upload page */
168function pte_edit_form_hook_redirect(){
169	add_action('add_meta_boxes', 'pte_admin_media_scripts');
170}
171
172add_action( 'media_upload_library', 'pte_admin_media_scripts_editor' );
173add_action( 'media_upload_gallery', 'pte_admin_media_scripts_editor' );
174add_action( 'media_upload_image', 'pte_admin_media_scripts_editor' );
175function pte_admin_media_scripts_editor(){
176	pte_admin_media_scripts('attachment');
177}
178
179function pte_admin_media_scripts($post_type){
180	$options = pte_get_options();
181	pte_add_thickbox();
182	if ($post_type == "attachment") {
183		wp_enqueue_script( 'pte'
184			, PTE_PLUGINURL . 'apps/coffee-script.js'
185			, array('underscore')
186			, PTE_VERSION
187		);
188		add_action( 'admin_print_footer_scripts', 'pte_enable_editor_js', 100);
189	}
190	else {
191		//add_action( 'admin_print_footer_scripts', 'pte_enable_media_js', 100);
192		wp_enqueue_script( 'pte'
193			, PTE_PLUGINURL . 'js/snippets/pte_enable_media.js'
194			, array('media-views')
195			, PTE_VERSION
196			, true
197		);
198		wp_enqueue_style( 'pte'
199			, PTE_PLUGINURL . 'css/pte-media.css'
200			, NULL
201			, PTE_VERSION
202		);
203	}
204	wp_localize_script('pte'
205		, 'pteL10n'
206		, array('PTE' => __('Post Thumbnail Editor', PTE_DOMAIN)
207			, 'url' => pte_url( "<%= id %>", true )
208		)
209	);
210}
211
212function pte_enable_editor_js(){
213	injectCoffeeScript( PTE_PLUGINPATH . "js/snippets/editor.coffee" );
214}
215
216function pte_enable_media_js(){
217	injectCoffeeScript( PTE_PLUGINPATH . "js/snippets/media.coffee" );
218}
219
220function injectCoffeeScript($coffeeFile){
221	$coffee = @file_get_contents( $coffeeFile );
222	//$options = json_encode( pte_get_options() );
223	echo <<<EOT
224<script type="text/coffeescript">
225$coffee
226</script>
227EOT;
228}
229
230
231
232// Add the PTE link to the featured image in the post screen
233// Called in wp-admin/includes/post.php
234add_filter( 'admin_post_thumbnail_html', 'pte_admin_post_thumbnail_html', 10, 2 );
235
236function pte_admin_post_thumbnail_html( $content, $post_id ){
237	pte_add_thickbox();
238	$thumbnail_id = get_post_thumbnail_id( $post_id );
239	if ( $thumbnail_id == null )
240		return $content;
241
242	return $content .= '<p id="pte-link" class="hide-if-no-js"><a class="thickbox" href="' 
243		. pte_url( $thumbnail_id, true )
244		. '">' 
245		. esc_html__( 'Post Thumbnail Editor', PTE_DOMAIN ) 
246		. '</a></p>';
247}
248
249/* Fix wordpress ridiculousness about making a thickbox max width=720 */
250function pte_add_thickbox() {
251	add_thickbox();
252	wp_enqueue_script('pte-fix-thickbox',
253		PTE_PLUGINURL . "js/snippets/pte-fix-thickbox.js",
254		array( 'media-upload' ),
255		PTE_VERSION
256	);
257}
258
259
260/* For all purpose needs */
261add_action('wp_ajax_pte_ajax', 'pte_ajax');
262function pte_ajax(){
263	// Move all adjuntant functions to a separate file and include that here
264	require_once(PTE_PLUGINPATH . 'php/functions.php');
265	PteLogger::debug( "PARAMETERS: " . print_r( $_REQUEST, true ) );
266
267	switch ($_GET['pte-action'])
268	{
269	case "iframe":
270		pte_init_iframe();
271		break;
272	case "resize-images":
273		pte_resize_images();
274		break;
275	case "confirm-images":
276		pte_confirm_images();
277		break;
278	case "delete-images":
279		pte_delete_images();
280		break;
281	case "get-thumbnail-info":
282		$id = (int) $_GET['id'];
283		if ( pte_check_id( $id ) )
284			print( json_encode( pte_get_all_alternate_size_information( $id ) ) );
285		break;
286	case "change-options":
287		pte_update_user_options();
288		break;
289	}
290	die();
291}
292
293/**
294 * Perform the capability check
295 *
296 * @param $id References the post that the user needs to have permission to edit
297 * @returns boolean true if the current user has permission else false
298 */
299function pte_check_id( $id ) {
300	if ( !$post = get_post( $id ) ) {
301		return false;
302	}
303	if ( current_user_can( 'edit_post', $id )
304		|| current_user_can( 'pte-edit', $id ) )
305   	{
306		return apply_filters( 'pte-capability-check', true, $id );
307	}
308	return apply_filters( 'pte-capability-check', false, $id );
309}
310
311/* Adds the Thumbnail option to the media library list */
312add_filter('media_row_actions', 'pte_media_row_actions', 10, 3); // priority: 10, args: 3
313
314function pte_media_row_actions($actions, $post, $detached){
315	// Add capability check
316	if ( !pte_check_id( $post->ID ) ) {
317		return $actions;
318	}
319	$options = pte_get_options();
320
321	$pte_url = pte_url( $post->ID );
322
323	$actions['pte'] = "<a href='${pte_url}' title='"
324		. __( 'Edit Thumbnails', PTE_DOMAIN )
325		. "'>" . __( 'Thumbnails', PTE_DOMAIN ) . "</a>";
326	return $actions;
327}
328
329
330/* Add Settings Page */
331add_action( 'load-settings_page_pte', 'pte_options' );
332/* Add Settings Page -> Submit/Update options */
333add_action( 'load-options.php', 'pte_options' );
334function pte_options(){
335	require_once( PTE_PLUGINPATH . 'php/options.php' );
336	pte_options_init();
337}
338
339/* Add SubMenus/Pages */
340add_action( 'admin_menu', 'pte_admin_menu' );
341
342/**
343 * These pages are linked into the hook system of wordpress, this means
344 * that almost any wp_admin page will work as long as you append "?page=pte"
345 * or "?page=pte-edit".  Try the function `'admin_url("index.php") . '?page=pte';`
346 *
347 * The function referred to here will output the HTML for the page that you want
348 * to display. However if you want to hook into enqueue_scripts or styles you
349 * should use the page-suffix that is returned from the function. (e.g.
350 * `add_action("load-".$hook, hook_func);`)
351 *
352 * There is also another hook with the same name as the hook that's returned.
353 * I don't remember in which order it is launched, but I believe the pertinent
354 * code is in admin-header.php.
355 */
356function pte_admin_menu(){
357	add_options_page( __('Post Thumbnail Editor', PTE_DOMAIN),
358		__('Post Thumbnail Editor', PTE_DOMAIN),
359		'edit_posts',
360		'pte',
361		'pte_launch_options_page'
362	);
363	// The submenu page function does not put a menu item in the wordpress sidebar.
364	add_submenu_page(NULL, __('Post Thumbnail Editor', PTE_DOMAIN),
365		__('Post Thumbnail Editor', PTE_DOMAIN),
366		'edit_posts',
367		'pte-edit',
368		'pte_edit_page'
369	);
370}
371
372function pte_launch_options_page(){
373	require_once( PTE_PLUGINPATH . 'php/options.php' );
374	pte_options_page();
375}
376
377/**
378 * This runs after headers have been sent, see the pte_edit_setup for the
379 * function that runs before anything is sent to the browser
380 */
381function pte_edit_page(){
382	// This is set via the pte_edit_setup function
383	global $pte_body;
384	echo( $pte_body );
385}
386
387/* Admin Edit Page: setup*/
388/**
389 * This hook (load-media_page_pte-edit)
390 *    depends on which page you use in the admin section
391 * (load-media_page_pte-edit) : wp-admin/upload.php?page=pte-edit
392 * (dashboard_page_pte-edit)  : wp-admin/?page=pte-edit
393 * (posts_page_pte-edit)      : wp-admin/edit.php?page=pte-edit
394 */
395add_action( 'load-media_page_pte-edit', 'pte_edit_setup' );
396function pte_edit_setup() {
397	global $post, $title, $pte_body;
398	$post_id = (int) $_GET['pte-id'];
399	if ( !isset( $post_id ) 
400			|| !is_int( $post_id )
401			|| !wp_attachment_is_image( $post_id )
402			|| !pte_check_id( $post_id ) ) {
403		//die("POST: $post_id IS_INT:" . is_int( $post_id ) . " ATTACHMENT: " . wp_attachment_is_image( $post_id ));
404		wp_redirect( admin_url( "upload.php" ) );
405		exit();
406	}
407	$post = get_post( $post_id );
408	$title = __( "Post Thumbnail Editor", PTE_DOMAIN );
409
410	include_once( PTE_PLUGINPATH . "php/functions.php" );
411	$pte_body = pte_body( $post->ID );
412
413	// Add the scripts and styles
414	wp_enqueue_script( 'jquery' );
415	wp_enqueue_script( 'jquery-ui-dialog' );
416	wp_enqueue_script( 'iris' );
417	wp_enqueue_style( 'colors' );
418	wp_enqueue_style( 'wp-jquery-ui-dialog' );
419}
420
421/**
422 * This code creates the image used for the crop
423 *
424 * By overwriting the wordpress code (same functions), we can change the default size
425 * to our own option.
426 */
427add_action('wp_ajax_pte_imgedit_preview','pte_wp_ajax_imgedit_preview_wrapper');
428function pte_wp_ajax_imgedit_preview_wrapper(){
429	require_once( PTE_PLUGINPATH . "php/overwrite_imgedit_preview.php" );
430	pte_wp_ajax_imgedit_preview();
431}
432
433
434/** End Settings Hooks **/
435
436load_plugin_textdomain( PTE_DOMAIN
437	, false
438	, basename( PTE_PLUGINPATH ) . DIRECTORY_SEPARATOR . "i18n" );
439