PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/pica-design/nextstepmaine
PHP | 697 lines | 505 code | 72 blank | 120 comment | 79 complexity | d11fa2a4f9b8b057be4eb3e3a7416943 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, GPL-3.0
  1. <?php
  2. /*
  3. * TODO: add helper functions to get various links to different functions
  4. */
  5. require_once(PTE_PLUGINPATH . 'php/log.php');
  6. function pte_require_json() {
  7. if ( function_exists( 'ob_start' ) ){
  8. ob_start();
  9. }
  10. }
  11. /*
  12. * This is used to output JSON
  13. * - Calling this should return all the way up the chain...
  14. */
  15. function pte_json_encode($mixed = null){
  16. $logger = PteLogger::singleton();
  17. $options = pte_get_options();
  18. $logs['error'] = array();
  19. $logs['log'] = array();
  20. // If a buffer was started this will check for any residual output
  21. // and add to the existing errors.
  22. if ( function_exists( 'ob_get_flush' ) ){
  23. $buffer = ob_get_clean();
  24. if ( isset( $buffer ) && strlen( $buffer ) > 0 ){
  25. $logger->warn( "Buffered output: {$buffer}" );
  26. }
  27. }
  28. if ( $logger->get_log_count( PteLogMessage::$ERROR ) > 0
  29. || $logger->get_log_count( PteLogMessage::$WARN ) > 0 )
  30. {
  31. $logs['error'] = $logger->get_logs( PteLogMessage::$ERROR | PteLogMessage::$WARN );
  32. }
  33. //if ( $logger->get_log_count( PteLogMessage::$WARN ) > 0 ){
  34. // $logs['warn'] = $logger->get_logs( PteLogMessage::$WARN );
  35. //}
  36. if ( $options['pte_debug'] ){
  37. $logs['log'] = $logger->get_logs();
  38. }
  39. if ( ! function_exists('json_encode') ){
  40. $logs['error'][] = "json_encode not available, upgrade your php";
  41. $messages = implode( "\r\n", $logs['error'] );
  42. die("{\"error\":\"{$messages}\"}");
  43. }
  44. if ( ! isset($mixed) ){
  45. $mixed = array();
  46. }
  47. else if ( ! is_array( $mixed ) ){
  48. $mixed = array('noarray' => $mixed);
  49. }
  50. if ( count( $logs['error'] )+count( $logs['log'] ) > 0 ){
  51. $mixed = array_merge_recursive( $mixed, $logs );
  52. }
  53. print( json_encode($mixed) );
  54. return true;
  55. }
  56. /*
  57. * pte_json_error - Calling this should return all the way up the chain...
  58. */
  59. function pte_json_error( $error ){
  60. $logger = PteLogger::singleton();
  61. $logger->error( $error );
  62. return pte_json_encode();
  63. }
  64. /*
  65. * pte_filter_sizes
  66. *
  67. * This is used by the get_sizes functions to determine which sizes to
  68. * reduce to
  69. */
  70. function pte_filter_sizes( $element ){
  71. global $pte_sizes;
  72. $options = pte_get_options();
  73. // Check if the element is in the pte_sizes array
  74. if ( ( is_array( $pte_sizes ) && !in_array( $element, $pte_sizes ) )
  75. or ( in_array( $element, $options['pte_hidden_sizes'] ) )
  76. ){
  77. return false;
  78. }
  79. return true;
  80. }
  81. /*
  82. * pte_get_alternate_sizes
  83. *
  84. * Creates an array of each thumbnail size and the corresponding data:
  85. * * height
  86. * * width
  87. * * crop boolean
  88. *
  89. * Thanks to the ajax_thumbnail_rebuild plugin
  90. */
  91. function pte_get_alternate_sizes($filter=true){
  92. //Put in some code to check if it's already been called...
  93. global $_wp_additional_image_sizes, $pte_gas;
  94. if ( !isset($pte_gas) ){
  95. $pte_gas = array();
  96. $sizes = array();
  97. // Some times we don't want the filter to run (in admin for example)
  98. if ($filter){
  99. $sizes = array_filter( get_intermediate_image_sizes(), 'pte_filter_sizes' );
  100. }
  101. else{
  102. $sizes = get_intermediate_image_sizes();
  103. }
  104. foreach ($sizes as $s){
  105. if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) // For theme-added sizes
  106. $width = intval( $_wp_additional_image_sizes[$s]['width'] );
  107. else // For default sizes set in options
  108. $width = get_option( "{$s}_size_w" );
  109. if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) // For theme-added sizes
  110. $height = intval( $_wp_additional_image_sizes[$s]['height'] );
  111. else // For default sizes set in options
  112. $height = get_option( "{$s}_size_h" );
  113. if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) // For theme-added sizes
  114. $crop = intval( $_wp_additional_image_sizes[$s]['crop'] );
  115. else // For default sizes set in options
  116. $crop = get_option( "{$s}_crop" );
  117. $pte_gas[$s] = array(
  118. 'width' => $width,
  119. 'height' => $height,
  120. 'crop' => $crop
  121. );
  122. }
  123. }
  124. return $pte_gas;
  125. }
  126. /*
  127. * pte_get_image_data
  128. *
  129. * Gets specific data for a given image (id) at a given size (size)
  130. * Optionally can return the JSON value or PHP array
  131. */
  132. function pte_get_image_data( $id, $size, $size_data ){
  133. $logger = PteLogger::singleton();
  134. $fullsizepath = get_attached_file( $id );
  135. $path_information = image_get_intermediate_size($id, $size);
  136. if ( $path_information &&
  137. @file_exists( dirname( $fullsizepath ) . DIRECTORY_SEPARATOR . $path_information['file'] )
  138. ){
  139. return $path_information;
  140. }
  141. // We don't really care how it gets generated, just that it is...
  142. // see ajax-thumbnail-rebuild plugin for inspiration
  143. if ( FALSE !== $fullsizepath && @file_exists($fullsizepath) ) {
  144. // Create the image and update the wordpress metadata
  145. $resized = image_make_intermediate_size( $fullsizepath,
  146. $size_data['width'],
  147. $size_data['height'],
  148. $size_data['crop']
  149. );
  150. if ($resized){
  151. $metadata = wp_get_attachment_metadata($id, true);
  152. $metadata['sizes'][$size] = $resized;
  153. wp_update_attachment_metadata( $id, $metadata);
  154. }
  155. }
  156. // Finish how we started
  157. $path_information = image_get_intermediate_size($id, $size);
  158. if ($path_information){
  159. return $path_information;
  160. }
  161. else {
  162. $logger->warn( "Couldn't find or generate metadata for image: {$id}-{$size}" );
  163. }
  164. return false;
  165. }
  166. /*
  167. * pte_get_all_alternate_size_information
  168. *
  169. * Gets all pertinent data describing the alternate sizes
  170. */
  171. function pte_get_all_alternate_size_information( $id ){
  172. $sizes = pte_get_alternate_sizes();
  173. foreach ( $sizes as $size => &$info ){
  174. if ( $info['crop'] )
  175. $info['crop'] = true;
  176. else
  177. $info['crop'] = false;
  178. $info['current'] = pte_get_image_data( $id, $size, $info );
  179. }
  180. return $sizes;
  181. }
  182. /*
  183. * pte_body
  184. *
  185. * Returns the base HTML needed to display and transform the inages
  186. *
  187. * Requires post id as $_GET['id']
  188. */
  189. function pte_body( $id ){
  190. ob_start();
  191. $logger = PteLogger::singleton();
  192. $options = pte_get_options();
  193. // Get the information needed for image preview
  194. // (See wp-admin/includes/image-edit.php)
  195. $nonce = wp_create_nonce("image_editor-$id");
  196. $meta = wp_get_attachment_metadata($id, true);
  197. if ( is_array($meta) && isset( $meta['width'] ) ){
  198. $big = max( $meta['width'], $meta['height'] );
  199. }
  200. else {
  201. $logger->error(
  202. sprintf( __( "Invalid meta data for POST #%d: %s" )
  203. , $id
  204. , print_r( $meta, true )
  205. )
  206. );
  207. $logger->error( __( "Please contact support", PTE_DOMAIN ) );
  208. }
  209. PteLogger::debug( "PTE-VERSION: " . PTE_VERSION .
  210. "\nUSER-AGENT: " . $_SERVER['HTTP_USER_AGENT'] .
  211. "\nWORDPRESS: " . $GLOBALS['wp_version'] );
  212. $script_url = PTE_PLUGINURL . 'php/load-scripts.php?load=jquery,imgareaselect,jquery-json,pte';
  213. $style_url = PTE_PLUGINURL . 'php/load-styles.php?load=imgareaselect,pte';
  214. if ( $options['pte_debug'] ){
  215. $style_url .= "&d=1";
  216. $script_url .= "&d=1";
  217. }
  218. // Generate an image and put into the ptetmp directory
  219. if (false === $editor_image = pte_generate_working_image($id)) {
  220. $editor_image = sprintf("%s?action=pte_imgedit_preview&amp;_ajax_nonce=%s&amp;postid=%d&amp;rand=%d",
  221. admin_url('admin-ajax.php'),
  222. $nonce,
  223. $id,
  224. rand(1,99999)
  225. );
  226. }
  227. require( PTE_PLUGINPATH . "html/pte.php" );
  228. return ob_get_clean();
  229. }
  230. function pte_generate_working_image($id)
  231. {
  232. $options = pte_get_options();
  233. if (false == $options['pte_imgedit_disk'])
  234. return false;
  235. // SETS PTE_TMP_DIR and PTE_TMP_URL
  236. extract( pte_tmp_dir() );
  237. $original_file = _load_image_to_edit_path( $id );
  238. $size = $options['pte_imgedit_max_size'];
  239. $editor = wp_get_image_editor( $original_file );
  240. $finfo = pathinfo( $original_file );
  241. $basename = sprintf("%s-%s.%s", $id, $size, $finfo['extension']);
  242. $file = sprintf("%s%s", $PTE_TMP_DIR, $basename );
  243. $url = sprintf("%s%s", $PTE_TMP_URL, $basename );
  244. if ( file_exists( $file ) )
  245. return $url;
  246. PteLogger::debug("\nGENERATING WORKING IMAGE:\n [{$file}]\n [{$url}]");
  247. // Resize the image and check the results
  248. $results = $editor->resize($size,$size);
  249. if ( is_wp_error( $results ) ) {
  250. PteLogger::error( $results );
  251. return false;
  252. }
  253. // Save the image
  254. if ( is_wp_error( $editor->save( $file ) ) ) {
  255. PteLogger::error( "Unable to save the generated image falling back to ajax-ed image" );
  256. return false;
  257. }
  258. return $url;
  259. }
  260. function pte_check_int( $int ){
  261. $logger = PteLogger::singleton();
  262. if (! is_numeric( $int ) ){
  263. $logger->warn( "PARAM not numeric: '{$int}'" );
  264. return false;
  265. }
  266. return $int;
  267. }
  268. /*
  269. * Get Destination width & height
  270. * ==============================
  271. * When the crop isn't set:
  272. * the size information for the biggest dimension is accurate,
  273. * but the other dimension is wrong
  274. */
  275. function pte_get_width_height( $size_information, $w, $h ){
  276. $logger = PteLogger::singleton();
  277. if ( $size_information['crop'] == 1 ){
  278. $logger->debug("GETwidthheightCROPPED");
  279. $dst_w = $size_information['width'];
  280. $dst_h = $size_information['height'];
  281. }
  282. // Crop isn't set so the height / width should be based on the smallest side
  283. // or check if the post_thumbnail has a 0 for a side.
  284. else {
  285. $use_width = false;
  286. if ( $w > $h ) $use_width = true;
  287. if ( $size_information['height'] == 0 ) $use_width = true;
  288. // This case appeared because theme twentytwelve made a thumbnail 'post-thumbnail'
  289. // with 624x9999, no crop... The images it created were huge...
  290. if ( $size_information['width'] < $size_information['height'] ) $use_width = true;
  291. if ( $size_information['width'] == 0 ) $use_width = false;
  292. $logger->debug("GETwidthheightPARAMS\nWIDTH: $w\nHEIGHT: $h\nUSE_WIDTH: " . print_r($use_width, true));
  293. if ( $use_width ){
  294. $dst_w = $size_information['width'];
  295. $dst_h = round( ($dst_w/$w) * $h, 0);
  296. }
  297. else {
  298. $dst_h = $size_information['height'];
  299. $dst_w = round( ($dst_h/$h) * $w, 0);
  300. }
  301. }
  302. // Sanity Check
  303. if ( $dst_h == 0 || $dst_w == 0 ){
  304. $logger->error( "Invalid derived dimensions: ${dst_w} x ${dst_h}" );
  305. }
  306. return compact( "dst_w", "dst_h" );
  307. }
  308. /*
  309. * ================
  310. * Get the filename
  311. * ================
  312. * See image_resize function in wp-includes/media.php to follow the same conventions
  313. * - Check if the file exists
  314. *
  315. * Using the cache buster is a good idea because:
  316. * * we shouldn't overwrite old images that have been placed into posts
  317. * * keeps problems from occuring when I try to debug and people think picture
  318. * didn't save, when it's just a caching issue
  319. */
  320. function pte_generate_filename( $file, $w, $h ){
  321. $options = pte_get_options();
  322. $info = pathinfo( $file );
  323. $ext = $info['extension'];
  324. $name = wp_basename( $file, ".$ext" );
  325. $suffix = "{$w}x{$h}";
  326. if ( $options['cache_buster'] ){
  327. $cache_buster = time();
  328. return sprintf( "%s-%s-%s.%s",
  329. $name,
  330. $suffix,
  331. $cache_buster,
  332. $ext );
  333. }
  334. //print_r( compact( "file", "info", "ext", "name", "suffix" ) );
  335. return "{$name}-{$suffix}.{$ext}";
  336. }
  337. /*
  338. * resize_images
  339. *
  340. * Take an array of sizes along with the associated resize data (w/h/x/y)
  341. * and save the images to a temp directory
  342. *
  343. * OUTPUT: JSON object 'size: url'
  344. */
  345. function pte_resize_images(){
  346. $logger = PteLogger::singleton();
  347. global $pte_sizes;
  348. // Require JSON output
  349. pte_require_json();
  350. $id = intval( $_GET['id'] );
  351. $w = pte_check_int( $_GET['w'] );
  352. $h = pte_check_int( $_GET['h'] );
  353. $x = pte_check_int( $_GET['x'] );
  354. $y = pte_check_int( $_GET['y'] );
  355. $save = isset( $_GET['save'] ) && ( strtolower( $_GET['save'] ) === "true" );
  356. if ( pte_check_id( $id ) === false
  357. || $w === false
  358. || $h === false
  359. || $x === false
  360. || $y === false
  361. ) {
  362. return pte_json_error( "ResizeImages initialization failed: '{$id}-{$w}-{$h}-{$x}-{$y}'" );
  363. }
  364. // Get the sizes to process
  365. $pte_sizes = $_GET['pte-sizes'];
  366. if ( !is_array( $pte_sizes ) ){
  367. $logger->debug( "Converting pte_sizes to array" );
  368. $pte_sizes = explode( ",", $pte_sizes );
  369. }
  370. $sizes = pte_get_all_alternate_size_information( $id );
  371. // The following information is common to all sizes
  372. // *** common-info
  373. $original_file = _load_image_to_edit_path( $id );
  374. $original_size = @getimagesize( $original_file );
  375. // SETS PTE_TMP_DIR and PTE_TMP_URL
  376. extract( pte_tmp_dir() );
  377. $thumbnails = array();
  378. if ( !$original_size ){
  379. return pte_json_error("Could not read image size");
  380. }
  381. $logger->debug( "BASE FILE DIMENSIONS/INFO: " . print_r( $original_size, true ) );
  382. list( $orig_w, $orig_h, $orig_type ) = $original_size;
  383. // *** End common-info
  384. // So this never interrupts the jpeg_quality anywhere else
  385. add_filter('jpeg_quality', 'pte_get_jpeg_quality');
  386. add_filter('wp_editor_set_quality', 'pte_get_jpeg_quality');
  387. foreach ( $sizes as $size => $data ){
  388. // Get all the data needed to run image_create
  389. //
  390. // $dst_w, $dst_h
  391. extract( pte_get_width_height( $data, $w, $h ) );
  392. $logger->debug( "WIDTHxHEIGHT: $dst_w x $dst_h" );
  393. // Set the directory
  394. $basename = pte_generate_filename( $original_file, $dst_w, $dst_h );
  395. // Set the file and URL's - defines set in pte_ajax
  396. $tmpfile = "{$PTE_TMP_DIR}{$id}" . DIRECTORY_SEPARATOR . "{$basename}";
  397. // === CREATE IMAGE ===================
  398. // This function is in wp-includes/media.php
  399. // We've added a filter to return our own editor which extends the wordpress one.
  400. add_filter( 'wp_image_editors', 'pte_image_editors' );
  401. $editor = wp_get_image_editor( $original_file );
  402. if ( is_a( $editor, "WP_Image_Editor_Imagick" ) ) $logger->debug( "EDITOR: ImageMagick" );
  403. if ( is_a( $editor, "WP_Image_Editor_GD" ) ) $logger->debug( "EDITOR: GD" );
  404. $crop_results = $editor->crop($x, $y, $w, $h, $dst_w, $dst_h);
  405. if ( is_wp_error( $crop_results ) ){
  406. $logger->error( "Error creating image: {$size}" );
  407. continue;
  408. }
  409. // The directory containing the original file may no longer exist when
  410. // using a replication plugin.
  411. wp_mkdir_p( dirname( $tmpfile ) );
  412. $tmpfile = dirname( $tmpfile ) . '/' . wp_unique_filename( dirname( $tmpfile ), basename( $tmpfile ) );
  413. $tmpurl = "{$PTE_TMP_URL}{$id}/" . basename( $tmpfile );
  414. if ( is_wp_error( $editor->save( $tmpfile ) ) ){
  415. $logger->error( "Error writing image: {$size} to '{$tmpfile}'" );
  416. continue;
  417. }
  418. // === END CREATE IMAGE ===============
  419. // URL: wp_upload_dir => base_url/subdir + /basename of $tmpfile
  420. // This is for the output
  421. $thumbnails[$size]['url'] = $tmpurl;
  422. $thumbnails[$size]['file'] = basename( $tmpfile );
  423. }
  424. // Did you process anything?
  425. if ( count( $thumbnails ) < 1 ){
  426. return pte_json_error("No images processed");
  427. }
  428. $ptenonce = wp_create_nonce( "pte-{$id}" );
  429. // If save -- return pte_confirm_images
  430. if ( $save ){
  431. function create_pte_confirm($thumbnail){
  432. return $thumbnail['file'];
  433. }
  434. $_REQUEST['pte-nonce'] = $ptenonce;
  435. $_GET['pte-confirm'] = array_map('create_pte_confirm', $thumbnails);
  436. $logger->debug( "CONFIRM:" );
  437. $logger->debug( print_r( $_GET, true ) );
  438. return pte_confirm_images(true);
  439. }
  440. return pte_json_encode( array(
  441. 'thumbnails' => $thumbnails,
  442. 'pte-nonce' => $ptenonce,
  443. 'pte-delete-nonce' => wp_create_nonce( "pte-delete-{$id}" )
  444. ) );
  445. }
  446. function pte_image_editors( $editor_array ){
  447. require_once( PTE_PLUGINPATH . 'php/class-pte-image-editor-gd.php' );
  448. require_once( PTE_PLUGINPATH . 'php/class-pte-image-editor-imagick.php' );
  449. array_unshift( $editor_array, 'PTE_Image_Editor_Imagick', 'PTE_Image_Editor_GD' );
  450. return $editor_array;
  451. }
  452. /*
  453. * pte_confirm_images
  454. *
  455. * Take an array of image sizes, an ID and a nonce and then move the confirmed images
  456. * to the official position and update metadata...
  457. *
  458. * Clean up and return error/success information...
  459. */
  460. function pte_confirm_images($immediate = false){
  461. global $pte_sizes;
  462. $logger = PteLogger::singleton();
  463. // Require JSON output
  464. pte_require_json();
  465. $id = (int) $_GET['id'];
  466. if ( pte_check_id( $id ) === false ){
  467. return pte_json_error( "ID invalid: {$id}" );
  468. }
  469. // Check nonce
  470. if ( !check_ajax_referer( "pte-{$id}", 'pte-nonce', false ) ){
  471. return pte_json_error( "CSRF Check failed" );
  472. }
  473. // Get the available sizes
  474. if ( is_array( $_GET['pte-confirm'] ) ){
  475. $pte_sizes = array_keys( $_GET['pte-confirm'] );
  476. $sizes = pte_get_all_alternate_size_information( $id );
  477. $logger->debug( "pte_get_all_alternate_size_information returned: "
  478. . print_r( $sizes, true ) );
  479. }
  480. else {
  481. return pte_json_error( "Invalid Parameters: can't find sizes" );
  482. }
  483. // === END INITIALIZATION ================================
  484. // Foreach size:
  485. // Move good image
  486. // Update metadata
  487. // Delete old image
  488. // Remove PTE/$id directory
  489. // SETS PTE_TMP_DIR and PTE_TMP_URL
  490. extract( pte_tmp_dir() );
  491. foreach ( $sizes as $size => $data ){
  492. // Make sure we're only moving our files
  493. $good_file = $PTE_TMP_DIR
  494. . $id . DIRECTORY_SEPARATOR
  495. . basename( $_GET['pte-confirm'][$size] );
  496. if ( ! ( isset( $good_file ) && file_exists( $good_file ) ) ){
  497. return pte_json_error("FILE is invalid: {$good_file}");
  498. }
  499. $dir = dirname( get_attached_file( $id ) );
  500. $new_file = $dir
  501. . DIRECTORY_SEPARATOR
  502. . basename( $good_file );
  503. if ( isset( $data['current']['file'] ) ){
  504. $old_file = $dir
  505. . DIRECTORY_SEPARATOR
  506. . $data['current']['file'];
  507. }
  508. // Delete/unlink old file
  509. if ( isset( $old_file ) )
  510. {
  511. $logger->debug( "Deleting old thumbnail: {$old_file}" );
  512. @unlink( apply_filters( 'wp_delete_file', $old_file ) );
  513. }
  514. // Move good image
  515. $logger->debug( "Moving '{$good_file}' to '{$new_file}'" );
  516. wp_mkdir_p( dirname( $new_file ) );
  517. rename( $good_file, $new_file );
  518. // Update metadata
  519. $image_dimensions = @getimagesize( $new_file );
  520. list( $w, $h, $type ) = $image_dimensions;
  521. //print("IMAGE DIMENSIONS...");
  522. //print_r( $image_dimensions );
  523. $metadata = wp_get_attachment_metadata( $id, true );
  524. $metadata['sizes'][$size] = array(
  525. 'file' => basename( $new_file ),
  526. 'width' => $w,
  527. 'height' => $h
  528. );
  529. $logger->debug( "Updating '{$size}' metadata: " . print_r( $metadata['sizes'][$size], true ) );
  530. wp_update_attachment_metadata( $id, $metadata);
  531. }
  532. // Delete tmpdir
  533. //pte_rmdir( $PTE_TMP_DIR );
  534. return pte_json_encode( array(
  535. 'thumbnails' => pte_get_all_alternate_size_information( $id ),
  536. 'immediate' => $immediate
  537. ) );
  538. }
  539. function pte_rmdir( $dir ){
  540. $logger = PteLogger::singleton();
  541. if ( !is_dir( $dir ) || !preg_match( "/ptetmp/", $dir ) ){
  542. $logger->warn("Tried to delete invalid directory: {$dir}");
  543. return;
  544. }
  545. foreach ( scandir( $dir ) as $file ){
  546. if ( "." == $file || ".." == $file ) continue;
  547. $full_path_to_file = $dir . DIRECTORY_SEPARATOR . $file;
  548. $logger->debug("DELETING: {$full_path_to_file}");
  549. unlink( $full_path_to_file );
  550. }
  551. rmdir( $dir );
  552. }
  553. function pte_delete_images()
  554. {
  555. // Require JSON output
  556. pte_require_json();
  557. $id = (int) $_GET['id'];
  558. if ( pte_check_id( $id ) === false ){
  559. return pte_json_error( "ID invalid: {$id}" );
  560. }
  561. // Check nonce
  562. if ( !check_ajax_referer( "pte-delete-{$id}", 'pte-nonce', false ) ){
  563. return pte_json_error( "CSRF Check failed" );
  564. }
  565. // SETS PTE_TMP_DIR and PTE_TMP_URL
  566. extract( pte_tmp_dir() );
  567. $PTE_TMP_DIR = $PTE_TMP_DIR . $id . DIRECTORY_SEPARATOR;
  568. // Delete tmpdir
  569. PteLogger::debug( "Deleting [{$PTE_TMP_DIR}]" );
  570. pte_rmdir( $PTE_TMP_DIR );
  571. return pte_json_encode( array( "success" => "Yay!" ) );
  572. }
  573. function pte_get_jpeg_quality($quality){
  574. $options = pte_get_options();
  575. $jpeg_compression = $options['pte_jpeg_compression'];
  576. if ( isset( $_GET['pte-jpeg-compression'] ) ) {
  577. $tmp_jpeg = intval( $_GET['pte-jpeg-compression'] );
  578. if ( 0 <= $tmp_jpeg && $tmp_jpeg <= 100 ){
  579. $jpeg_compression = $tmp_jpeg;
  580. }
  581. }
  582. PteLogger::debug( "COMPRESSION: " . $jpeg_compression );
  583. return $jpeg_compression;
  584. }
  585. /**
  586. * Sending output to an iframe
  587. */
  588. function pte_init_iframe() {
  589. global $title, $pte_iframe;
  590. $pte_iframe = true;
  591. // Provide the base framework/HTML for the editor.
  592. require_once( ABSPATH . WPINC . '/script-loader.php' );
  593. // Check the input parameters and create the HTML
  594. pte_edit_setup();
  595. print( "<!DOCTYPE html>\n<html><head><title>${title}</title>\n" );
  596. print_head_scripts();
  597. print_admin_styles();
  598. print( '</head><body class="wp-core-ui pte-iframe">' );
  599. // Simply echo the created HTML
  600. pte_edit_page();
  601. print_footer_scripts();
  602. print( '</body></html>' );
  603. }