PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/simple-image-sizes/inc/class.admin.php

https://bitbucket.org/rossberenson/michael-karas
PHP | 811 lines | 450 code | 120 blank | 241 comment | 96 complexity | 80588e70a2cd8663faed2a9abbd77eb5 MD5 | raw file
  1. <?php
  2. Class SISAdmin {
  3. // Original sizes
  4. public static $original = array( 'thumbnail', 'medium', 'large' );
  5. public function __construct(){
  6. // Init
  7. add_action ( 'admin_menu', array( &$this, 'init' ) );
  8. add_action ( 'admin_enqueue_scripts', array( __CLASS__, 'registerScripts' ), 11 );
  9. // Add underscore template
  10. add_action( 'admin_footer', array( __CLASS__, 'addTemplate' ) );
  11. // Add ajax action
  12. // Option page
  13. add_action( 'wp_ajax_'.'sis_get_list', array( __CLASS__, 'a_GetList' ) );
  14. add_action( 'wp_ajax_'.'sis_rebuild_image', array( __CLASS__, 'a_ThumbnailRebuild' ) );
  15. add_action( 'wp_ajax_'.'sis_get_sizes', array( __CLASS__, 'a_GetSizes' ) );
  16. add_action( 'wp_ajax_'.'sis_add_size', array( __CLASS__, 'a_AddSize' ) );
  17. add_action( 'wp_ajax_'.'sis_remove_size', array( __CLASS__, 'a_RemoveSize' ) );
  18. // Add image sizes in the form, check if 3.3 is installed or not
  19. if( !function_exists( 'is_main_query' ) ) {
  20. add_filter( 'attachment_fields_to_edit', array( __CLASS__, 'sizesInForm' ), 11, 2 ); // Add our sizes to media forms
  21. } else {
  22. add_filter( 'image_size_names_choose', array( __CLASS__, 'AddThumbnailName' ) );
  23. }
  24. // Add link in plugins list
  25. add_filter( 'plugin_action_links', array( __CLASS__,'addSettingsLink' ), 10, 2 );
  26. // Add action in media row quick actions
  27. add_filter( 'media_row_actions', array( __CLASS__, 'addActionsList' ), 10, 2 );
  28. // Add filter for the Media single
  29. add_filter( 'attachment_fields_to_edit', array( __CLASS__, 'addFieldRegenerate' ), 9, 2 );
  30. }
  31. /**
  32. * Register javascripts and css.
  33. *
  34. * @access public
  35. * @return void
  36. * @author Nicolas Juen
  37. */
  38. public static function registerScripts( $hook_suffix = '' ) {
  39. if( !isset( $hook_suffix ) || empty( $hook_suffix ) ) {
  40. return false;
  41. }
  42. if( $hook_suffix == 'options-media.php' ) {
  43. // Add javascript
  44. wp_enqueue_script( 'underscore', 'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.3/underscore-min.js' , array(), '1.4.3' );
  45. wp_enqueue_script( 'sis_js', SIS_URL.'/js/sis.min.js', array( 'jquery', 'jquery-ui-button', 'jquery-ui-progressbar', 'underscore' ), SIS_VERSION );
  46. // Add CSS
  47. wp_enqueue_style( 'jquery-ui-sis', SIS_URL.'/css/Aristo/jquery-ui-1.8.7.custom.css', array(), '1.8.7' );
  48. wp_enqueue_style( 'sis_css', SIS_URL.'/css/sis-style.css', array(), SIS_VERSION );
  49. } elseif( $hook_suffix == 'upload.php' || ( $hook_suffix == 'post.php' && isset( $_GET['post'] ) && isset( $_GET['action'] ) && $_GET['action'] == 'edit' ) ) {
  50. // Add javascript
  51. wp_enqueue_script( 'sis_js', SIS_URL.'/js/sis-attachments.min.js', array( 'jquery' ), SIS_VERSION );
  52. }
  53. // Add javascript translation
  54. wp_localize_script( 'sis_js', 'sis', self::localizeVars() );
  55. }
  56. /**
  57. * Localize the var for javascript
  58. *
  59. * @access public
  60. * @return void
  61. * @author Nicolas Juen
  62. */
  63. public static function localizeVars() {
  64. return array(
  65. 'ajaxUrl' => admin_url( '/admin-ajax.php' ),
  66. 'reading' => __( 'Reading attachments...', 'sis' ),
  67. 'maximumWidth' => __( 'Maximum width', 'sis' ),
  68. 'maximumHeight' => __( 'Maximum height', 'sis' ),
  69. 'crop' => __( 'Crop ?', 'sis' ),
  70. 'tr' => __( 'yes', 'sis' ),
  71. 'fl' => __( 'no', 'sis' ),
  72. 'show' => __( 'Show in post insertion ?', 'sis' ),
  73. 'of' => __( ' of ', 'sis' ),
  74. 'or' => __( ' or ', 'sis' ),
  75. 'beforeEnd' => __( ' before the end.', 'sis' ),
  76. 'deleteImage' => __( 'Delete', 'sis' ),
  77. 'noMedia' => __( 'No media in your site to regenerate !', 'sis' ),
  78. 'regenerating' => __( 'Regenerating ', 'sis'),
  79. 'regenerate' => __( 'Regenerate ', 'sis'),
  80. 'validate' => __( 'Validate image size name', 'sis' ),
  81. 'done' => __( 'Done.', 'sis' ),
  82. 'size' => __( 'Size', 'sis' ),
  83. 'notOriginal' => __( 'Don\'t use the basic Wordpress thumbnail size name, use the form above to edit them', 'sis' ),
  84. 'alreadyPresent' => __( 'This size is already registered, edit it instead of recreating it.', 'sis' ),
  85. 'confirmDelete' => __( 'Do you really want to delete these size ?', 'sis' ),
  86. 'update' => __( 'Update', 'sis' ),
  87. 'ajaxErrorHandler' => __( 'Error requesting page', 'sis' ),
  88. 'messageRegenerated' => __( 'images have been regenerated !', 'sis' ),
  89. 'validateButton' => __( 'Validate', 'sis' ),
  90. 'startedAt' => __( ' started at', 'sis' ),
  91. 'customName' => __( 'Public name', 'sis' ),
  92. 'finishedAt' => __( ' finished at :', 'sis' ),
  93. 'phpError' => __( 'Error during the php treatment, be sure to not have php errors in your page', 'sis' ),
  94. 'notSaved' => __( 'All the sizes you have modifed are not saved, continue anyway ?', 'sis' ),
  95. 'soloRegenerated' => __( 'This image has been regenerated in %s seconds', 'sis' ),
  96. 'regen_one' => wp_create_nonce( 'regen' )
  97. );
  98. }
  99. public static function addTemplate() {
  100. global $pagenow;
  101. if( $pagenow != 'options-media.php' ) {
  102. return false;
  103. }
  104. if( is_file( SIS_DIR.'/templates/admin-js.html' ) ) {
  105. include( SIS_DIR.'/templates/admin-js.html' );
  106. }
  107. return true;
  108. }
  109. /**
  110. * Add action in media row
  111. *
  112. * @since 2.2
  113. * @access public
  114. * @return $actions : array of actions and content to display
  115. * @author Nicolas Juen
  116. */
  117. public static function addActionsList( $actions, $object ) {
  118. // Add action for regeneration
  119. $actions['sis-regenerate'] = "<a href='#' data-id='".$object->ID."' class='sis-regenerate-one'>".__( 'Regenerate thumbnails', 'sis' )."</a>";
  120. // Return actions
  121. return $actions;
  122. }
  123. /**
  124. * Add a link to the setting option page
  125. *
  126. * @access public
  127. * @param array $links
  128. * @param string $file
  129. * @return void
  130. * @author Nicolas Juen
  131. */
  132. public static function addSettingsLink( $links, $file ) {
  133. if( $file != 'simple-image-sizes/simple_image_sizes.php' ) {
  134. return $links;
  135. }
  136. $settings_link = '<a href="'.admin_url('options-media.php').'"> '.__( 'Settings', 'sis' ).' </a>';
  137. array_unshift( $links, $settings_link );
  138. return $links;
  139. }
  140. /**
  141. * Init for the option page
  142. *
  143. * @access public
  144. * @return void
  145. * @author Nicolas Juen
  146. */
  147. function init() {
  148. // Check if admin
  149. if( !is_admin() ) {
  150. return false;
  151. }
  152. // Get the image sizes
  153. global $_wp_additional_image_sizes;
  154. $options = get_option( SIS_OPTION );
  155. // Get the sizes and add the settings
  156. foreach ( get_intermediate_image_sizes() as $s ) {
  157. // Don't make the original sizes or numeric sizes that appear
  158. if( in_array( $s, self::$original ) || is_integer( $s ) ) {
  159. continue;
  160. }
  161. // Set width
  162. $width = isset( $_wp_additional_image_sizes[$s]['width'] ) ? intval( $_wp_additional_image_sizes[$s]['width'] ) : get_option( "{$s}_size_w" ) ;
  163. // Set height
  164. $height = isset( $_wp_additional_image_sizes[$s]['height'] ) ? intval( $_wp_additional_image_sizes[$s]['height'] ) : get_option( "{$s}_size_h" ) ;
  165. //Set crop
  166. $crop = isset( $_wp_additional_image_sizes[$s]['crop'] ) ? intval( $_wp_additional_image_sizes[$s]['crop'] ) : get_option( "{$s}_crop" ) ;
  167. // Add the setting field for this size
  168. add_settings_field( 'image_size_'.$s, sprintf( __( '%s size', 'sis' ), $s ), array( &$this, 'imageSizes' ), 'media' , 'default', array( 'name' => $s , 'width' => $width , 'height' => $height, 'c' => $crop ) );
  169. }
  170. // Register the setting for media option page
  171. register_setting( 'media', SIS_OPTION );
  172. // Add the button
  173. add_settings_field( 'add_size_button', __( 'Add a new size', 'sis' ), array( &$this, 'addSizeButton' ), 'media' );
  174. // Add php button
  175. add_settings_field( 'get_php_button', __( 'Get php for theme', 'sis' ), array( &$this, 'getPhpButton' ), 'media' );
  176. // Add section for the thumbnail regeneration
  177. add_settings_section( 'thumbnail_regenerate', __( 'Thumbnail regeneration', 'sis' ), array( &$this, 'thumbnailRegenerate' ), 'media' );
  178. }
  179. /**
  180. * Display the row of the image size
  181. *
  182. * @access public
  183. * @param mixed $args
  184. * @return void
  185. * @author Nicolas Juen
  186. */
  187. public function imageSizes( $args ) {
  188. if( is_integer( $args['name'] ) )
  189. return false;
  190. // Get the options
  191. $sizes = (array)get_option( SIS_OPTION, array() );
  192. // Get the vars
  193. $height = isset( $sizes[$args['name']]['h'] )? $sizes[$args['name']]['h'] : $args['height'] ;
  194. $width = isset( $sizes[$args['name']]['w'] )? $sizes[$args['name']]['w'] : $args['width'] ;
  195. $crop = isset( $sizes[$args['name']]['c'] ) && !empty( $sizes[$args['name']]['c'] )? $sizes[$args['name']]['c'] : $args['c'] ;
  196. $show = isset( $sizes[$args['name']]['s'] ) && !empty( $sizes[$args['name']]['s'] )? '1' : '0' ;
  197. $custom = isset( $sizes[$args['name']]['custom'] ) && !empty( $sizes[$args['name']]['custom'] )? '1' : '0' ;
  198. $name = isset( $sizes[$args['name']]['n'] ) && !empty( $sizes[$args['name']]['n'] )? esc_html( $sizes[$args['name']]['n'] ) : esc_html( $args['name'] ) ;
  199. ?>
  200. <input type="hidden" value="<?php echo esc_attr( $args['name'] ); ?>" name="image_name" />
  201. <?php if( $custom ): ?>
  202. <input name="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][custom]' ); ?>" type="hidden" id="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][custom]' ); ?>" value="1" />
  203. <?php else: ?>
  204. <input name="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][theme]' ); ?>" type="hidden" id="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][theme]' ); ?>" value="1" />
  205. <?php endif; ?>
  206. <label class="sis-label" for="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][w]' ); ?>">
  207. <?php _e( 'Maximum width', 'sis'); ?>
  208. <input name="<?php esc_attr_e( 'custom_image_sizes['.$args['name'].'][w]' ); ?>" class='w small-text' type="number" step='1' min='0' id="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][w]' ); ?>" base_w='<?php echo esc_attr( $width ); ?>' value="<?php echo esc_attr( $width ); ?>" />
  209. </label>
  210. <label class="sis-label" for="<?php esc_attr_e( 'custom_image_sizes['.$args['name'].'][h]' ); ?>">
  211. <?php _e( 'Maximum height', 'sis'); ?>
  212. <input name="<?php esc_attr_e( 'custom_image_sizes['.$args['name'].'][h]' ); ?>" class='h small-text' type="number" step='1' min='0' id="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][h]' ); ?>" base_h='<?php echo esc_attr( $height ); ?>' value="<?php echo esc_attr( $height ); ?>" />
  213. </label>
  214. <label class="sis-label" for="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][n]' ); ?>">
  215. <?php _e( 'Public name', 'sis'); ?>
  216. <input name="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][n]' ); ?>" class='n' type="text" id="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][n]' ); ?>" base_n='<?php echo $name; ?>' value="<?php echo $name ?>" />
  217. </label>
  218. <span class="size_options">
  219. <input type='checkbox' id="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][c]' ); ?>" <?php checked( $crop, 1 ) ?> class="c crop" base_c='<?php echo esc_attr( $crop ); ?>' name="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][c]' ); ?>" value="1" />
  220. <label class="c" for="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][c]' ); ?>"><?php _e( 'Crop ?', 'sis'); ?></label>
  221. <input type='checkbox' id="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][s]'); ?>" <?php checked( $show, 1 ) ?> class="s show" base_s='<?php echo esc_attr( $show ); ?>' name="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][s]'); ?>" value="1" />
  222. <label class="s" for="<?php echo esc_attr( 'custom_image_sizes['.$args['name'].'][s]'); ?>"><?php _e( 'Show in post insertion ?', 'sis'); ?></label>
  223. </span>
  224. <span class="delete_size"><?php _e( 'Delete', 'sis'); ?></span>
  225. <span class="add_size validate_size"><?php _e( 'Update', 'sis'); ?></span>
  226. <input type="hidden" class="deleteSize" value='<?php echo wp_create_nonce( 'delete_'.$args['name'] ); ?>' />
  227. <?php }
  228. /**
  229. * Add the button to add a size
  230. *
  231. * @access public
  232. * @return void
  233. * @author Nicolas Juen
  234. */
  235. public function addSizeButton() { ?>
  236. <input type="button" class="button-secondary action" id="add_size" value="<?php esc_attr_e( 'Add a new size of thumbnail', 'sis'); ?>" />
  237. <?php
  238. }
  239. /**
  240. * Add the button to get the php for th sizes
  241. *
  242. * @access public
  243. * @return void
  244. * @author Nicolas Juen
  245. */
  246. public function getPhpButton() { ?>
  247. <input type="button" class="button-secondary action" id="get_php" value="<?php esc_attr_e( 'Get the PHP for the theme', 'sis'); ?>" />
  248. <p> <?php _e( 'Copy and paste the code below into your Wordpress theme function file if you wanted to save them and deactivate the plugin.', 'sis'); ?> </p>
  249. <code></code>
  250. <?php
  251. }
  252. /**
  253. * Display the Table of sizes and post types for regenerating
  254. *
  255. * @access public
  256. * @return void
  257. * @author Nicolas Juen
  258. */
  259. public function thumbnailRegenerate() {
  260. if( is_file( SIS_DIR.'/templates/options-media.php' ) ) {
  261. include( SIS_DIR.'/templates/options-media.php' );
  262. } else {
  263. esc_html_e( 'Admin option-media template missing' );
  264. }
  265. }
  266. /**
  267. * Add a size by Ajax
  268. *
  269. * @access public
  270. * @return void
  271. * @author Nicolas Juen
  272. */
  273. public static function a_AddSize() {
  274. // Get the nonce
  275. $nonce = isset( $_POST['nonce'] ) ? $_POST['nonce']: '' ;
  276. // Get old options
  277. $sizes = (array)get_option( SIS_OPTION, array() );
  278. // Check entries
  279. $name = isset( $_POST['name'] ) ? sanitize_title( $_POST['name'] ): '' ;
  280. $height = !isset( $_POST['height'] )? 0 : absint( $_POST['height'] );
  281. $width = !isset( $_POST['width'] )? 0 : absint( $_POST['width'] );
  282. $crop = isset( $_POST['crop'] ) && $_POST['crop'] == 'false' ? false : true;
  283. $show = isset( $_POST['show'] ) && $_POST['show'] == 'false' ? false : true;
  284. $cn = isset( $_POST['customName'] ) && !empty( $_POST['customName'] ) ? sanitize_text_field( $_POST['customName'] ): $name ;
  285. // Check the nonce
  286. if( !wp_verify_nonce( $nonce , 'add_size' ) ) {
  287. die(0);
  288. }
  289. // If no name given do not save
  290. if( empty( $name ) ) {
  291. die(0);
  292. }
  293. // Make values
  294. $values = array( 'custom' => 1, 'w' => $width , 'h' => $height, 'c' => $crop, 's' => $show, 'n' => $cn );
  295. // If the size have not changed return 2
  296. if( isset( $sizes[$name] ) && $sizes[$name] === $values ) {
  297. die(2);
  298. }
  299. // Put the new values
  300. $sizes[$name] = $values;
  301. // display update result
  302. echo (int)update_option( 'custom_image_sizes', $sizes );
  303. die();
  304. }
  305. /**
  306. * Remove a size by Ajax
  307. *
  308. * @access public
  309. * @return void
  310. * @author Nicolas Juen
  311. */
  312. public static function a_RemoveSize() {
  313. // Get old options
  314. $sizes = (array)get_option( SIS_OPTION, array() );
  315. // Get the nonce and name
  316. $nonce = isset( $_POST['nonce'] ) ? $_POST['nonce']: '' ;
  317. $name = isset( $_POST['name'] ) ? sanitize_title( $_POST['name'] ): '' ;
  318. // Check the nonce
  319. if( !wp_verify_nonce( $nonce , 'delete_'.$name ) ) {
  320. die(0);
  321. }
  322. // Remove the size
  323. unset( $sizes[sanitize_title( $name )] );
  324. unset( $sizes[0] );
  325. // Display the results
  326. echo (int)update_option( SIS_OPTION, $sizes );
  327. die();
  328. }
  329. /**
  330. * Display the add_image_size for the registered sizes
  331. *
  332. * @access public
  333. * @return void
  334. */
  335. public static function a_GetSizes() {
  336. global $_wp_additional_image_sizes;
  337. foreach ( get_intermediate_image_sizes() as $s ) {
  338. // Don't make the original sizes
  339. if( in_array( $s, self::$original ) ) {
  340. continue;
  341. }
  342. // Set width
  343. $width = isset( $_wp_additional_image_sizes[$s]['width'] ) ? intval( $_wp_additional_image_sizes[$s]['width'] ) : get_option( "{$s}_size_w" ) ;
  344. // Set height
  345. $height = isset( $_wp_additional_image_sizes[$s]['height'] ) ? intval( $_wp_additional_image_sizes[$s]['height'] ) : get_option( "{$s}_size_h" ) ;
  346. //Set crop
  347. $crop = isset( $_wp_additional_image_sizes[$s]['crop'] ) ? intval( $_wp_additional_image_sizes[$s]['crop'] ) : get_option( "{$s}_crop" ) ;
  348. $crop = ( $crop == 0 )? 'false' : 'true' ;
  349. ?>
  350. add_image_size( '<?php echo $s; ?>', '<?php echo $width; ?>', '<?php echo $height; ?>', <?php echo $crop ?> );<br />
  351. <?php
  352. }
  353. die();
  354. }
  355. /**
  356. *
  357. * Get the media list to regenerate
  358. *
  359. * @param : void
  360. * @return oid
  361. */
  362. public static function a_GetList() {
  363. global $wpdb;
  364. // Basic vars
  365. $res = array();
  366. $nonce = isset( $_POST['nonce'] ) ? $_POST['nonce']: '' ;
  367. // Check the nonce
  368. if( !wp_verify_nonce( $nonce , 'getList' ) ) {
  369. self::displayJson();
  370. }
  371. if ( isset( $_POST['post_types'] ) && !empty( $_POST['post_types'] ) ) {
  372. foreach( $_POST['post_types'] as $key => $type ) {
  373. if( !post_type_exists( $type ) ) {
  374. unset( $_POST['post_types'][$key] );
  375. }
  376. }
  377. if( empty( $_POST['post_types'][$key]) ) {
  378. self::displayJson();
  379. }
  380. // Get image medias
  381. $whichmimetype = wp_post_mime_type_where( 'image', $wpdb->posts );
  382. // Get all parent from post type
  383. $attachments = $wpdb->get_results( "SELECT *
  384. FROM $wpdb->posts
  385. WHERE 1 = 1
  386. AND post_type = 'attachment'
  387. $whichmimetype
  388. AND post_parent IN (
  389. SELECT DISTINCT ID
  390. FROM $wpdb->posts
  391. WHERE post_type IN ('".implode( "', '", $_POST['post_types'] )."')
  392. )" );
  393. } else {
  394. $attachments =& get_children( array(
  395. 'post_type' => 'attachment',
  396. 'post_mime_type' => 'image',
  397. 'numberposts' => -1,
  398. 'post_status' => null,
  399. 'post_parent' => null, // any parent
  400. 'output' => 'object',
  401. ) );
  402. }
  403. // Get the attachments
  404. foreach ( $attachments as $attachment ) {
  405. $res[] = array( 'id' => $attachment->ID, 'title' => $attachment->post_title );
  406. }
  407. // Return the Id's and Title of medias
  408. self::displayJson( $res );
  409. }
  410. /**
  411. * Rebuild the image
  412. *
  413. * @access public
  414. * @return void
  415. * @author Nicolas Juen
  416. */
  417. public static function a_ThumbnailRebuild() {
  418. global $wpdb;
  419. // Get the nonce
  420. $nonce = isset( $_POST['nonce'] ) ? $_POST['nonce']: '' ;
  421. // Time a the begining
  422. $start_time = microtime( true );
  423. // Get the thumbnails
  424. $thumbnails = isset( $_POST['thumbnails'] )? $_POST['thumbnails'] : NULL;
  425. // Check the nonce
  426. if( !wp_verify_nonce( $nonce , 'regen' ) ) {
  427. self::displayJson( array( 'error' => _e( 'Trying to cheat ?', 'sis' ) ) );
  428. }
  429. // Get the id
  430. $id = isset( $_POST["id"] ) ? $_POST["id"] : 0 ;
  431. // Check Id
  432. if( (int)$id <= 0 ) {
  433. self::displayJson(
  434. array(
  435. 'time' => round( microtime( true ) - $start_time, 4 ),
  436. 'error' => __( 'No id given in POST datas.', 'sis' )
  437. )
  438. );
  439. }
  440. // Get the path
  441. $fullsizepath = get_attached_file( $id );
  442. // Regen the attachment
  443. if ( false !== $fullsizepath && @file_exists( $fullsizepath ) ) {
  444. set_time_limit( 60 );
  445. if( wp_update_attachment_metadata( $id, self::wp_generate_attachment_metadata_custom( $id, $fullsizepath, $thumbnails ) ) == false ) {
  446. self::displayJson(
  447. array(
  448. 'src' => wp_get_attachment_thumb_url( $id ),
  449. 'time' => round( microtime( true ) - $start_time, 4 ),
  450. 'message' => sprintf( __( 'This file already exists in this size and have not been regenerated :<br/><a target="_blank" href="%1$s" >%2$s</a>', 'sis'), get_edit_post_link( $id ), get_the_title( $id ) )
  451. )
  452. );
  453. }
  454. } else {
  455. self::displayJson(
  456. array(
  457. 'src' => wp_get_attachment_thumb_url( $id ),
  458. 'time' => round( microtime( true ) - $start_time, 4 ),
  459. 'error' => sprintf( __( 'This file does not exists and have not been regenerated :<br/><a target="_blank" href="%1$s" >%2$s</a>', 'sis'), get_edit_post_link( $id ), get_the_title( $id ) )
  460. )
  461. );
  462. }
  463. // Display the attachment url for feedback
  464. self::displayJson(
  465. array(
  466. 'time' => round( microtime( true ) - $start_time, 4 ) ,
  467. 'src' => wp_get_attachment_thumb_url( $id ),
  468. 'title' => get_the_title( $id )
  469. )
  470. );
  471. }
  472. /**
  473. * Generate post thumbnail attachment meta data.
  474. *
  475. * @since 2.1.0
  476. *
  477. * @param int $attachment_id Attachment Id to process.
  478. * @param string $file Filepath of the Attached image.
  479. * @return mixed Metadata for attachment.
  480. */
  481. public static function wp_generate_attachment_metadata_custom( $attachment_id, $file, $thumbnails = NULL ) {
  482. $attachment = get_post( $attachment_id );
  483. $meta_datas = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
  484. $metadata = array();
  485. if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
  486. $imagesize = getimagesize( $file );
  487. $metadata['width'] = $imagesize[0];
  488. $metadata['height'] = $imagesize[1];
  489. list($uwidth, $uheight) = wp_constrain_dimensions($metadata['width'], $metadata['height'], 128, 96);
  490. $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
  491. // Make the file path relative to the upload dir
  492. $metadata['file'] = _wp_relative_upload_path($file);
  493. // make thumbnails and other intermediate sizes
  494. global $_wp_additional_image_sizes;
  495. foreach ( get_intermediate_image_sizes() as $s ) {
  496. $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE );
  497. if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
  498. $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
  499. else
  500. $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
  501. if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
  502. $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
  503. else
  504. $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
  505. if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
  506. $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
  507. else
  508. $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
  509. }
  510. $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
  511. // Only if not all sizes
  512. if( isset( $thumbnails ) && is_array( $thumbnails ) && isset( $meta_datas['sizes'] ) && !empty( $meta_datas['sizes'] ) ) {
  513. // Fill the array with the other sizes not have to be done
  514. foreach( $meta_datas['sizes'] as $name => $fsize ) {
  515. $metadata['sizes'][$name] = $fsize;
  516. }
  517. }
  518. foreach ( $sizes as $size => $size_data ) {
  519. if( isset( $thumbnails ) )
  520. if( !in_array( $size, $thumbnails ) ) {
  521. continue;
  522. }
  523. $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] );
  524. if( isset( $meta_datas['size'][$size] ) ) {
  525. // Remove the size from the orignal sizes for after work
  526. unset( $meta_datas['size'][$size] );
  527. }
  528. if ( $resized ) {
  529. $metadata['sizes'][$size] = $resized;
  530. }
  531. }
  532. // fetch additional metadata from exif/iptc
  533. $image_meta = wp_read_image_metadata( $file );
  534. if ( $image_meta ) {
  535. $metadata['image_meta'] = $image_meta;
  536. }
  537. }
  538. return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
  539. }
  540. /**
  541. * Add the custom sizes to the image sizes in article edition
  542. *
  543. * @access public
  544. * @param array $form_fields
  545. * @param object $post
  546. * @return void
  547. * @author Nicolas Juen
  548. * @author Additional Image Sizes (zui)
  549. */
  550. public static function sizesInForm( $form_fields, $post ) {
  551. // Protect from being view in Media editor where there are no sizes
  552. if ( isset( $form_fields['image-size'] ) ) {
  553. $out = NULL;
  554. $size_names = array();
  555. $sizes_custom = get_option( SIS_OPTION, array() );
  556. if ( is_array( $sizes_custom ) ) {
  557. foreach( $sizes_custom as $key => $value ) {
  558. if( isset( $value['s'] ) && $value['s'] == 1 ) {
  559. $size_names[$key] = self::_getThumbnailName( $key );;
  560. }
  561. }
  562. }
  563. foreach ( $size_names as $size => $label ) {
  564. $downsize = image_downsize( $post->ID, $size );
  565. // is this size selectable?
  566. $enabled = ( $downsize[3] || 'full' == $size );
  567. $css_id = "image-size-{$size}-{$post->ID}";
  568. // We must do a clumsy search of the existing html to determine is something has been checked yet
  569. if ( FALSE === strpos( 'checked="checked"', $form_fields['image-size']['html'] ) ) {
  570. if ( empty($check) )
  571. $check = get_user_setting( 'imgsize' ); // See if they checked a custom size last time
  572. $checked = '';
  573. // if this size is the default but that's not available, don't select it
  574. if ( $size == $check || str_replace( " ", "", $size ) == $check ) {
  575. if ( $enabled )
  576. $checked = " checked='checked'";
  577. else
  578. $check = '';
  579. } elseif ( !$check && $enabled && 'thumbnail' != $size ) {
  580. // if $check is not enabled, default to the first available size that's bigger than a thumbnail
  581. $check = $size;
  582. $checked = " checked='checked'";
  583. }
  584. }
  585. $html = "<div class='image-size-item' style='min-height: 50px; margin-top: 18px;'><input type='radio' " . disabled( $enabled, false, false ) . "name='attachments[$post->ID][image-size]' id='{$css_id}' value='{$size}'$checked />";
  586. $html .= "<label for='{$css_id}'>$label</label>";
  587. // only show the dimensions if that choice is available
  588. if ( $enabled )
  589. $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
  590. $html .= '</div>';
  591. $out .= $html;
  592. }
  593. $form_fields['image-size']['html'] .= $out;
  594. } // End protect from Media editor
  595. return $form_fields;
  596. }
  597. /**
  598. * Add the thumbnail name in the post insertion, based on new WP filter
  599. *
  600. * @access public
  601. * @param array $sizes
  602. * @return array
  603. * @since 2.3
  604. * @author Nicolas Juen
  605. * @author radeno based on this post : http://www.wpmayor.com/wordpress-hacks/how-to-add-custom-image-sizes-to-wordpress-uploader/
  606. */
  607. public static function AddThumbnailName($sizes) {
  608. // Get options
  609. $sizes_custom = get_option( SIS_OPTION, array() );
  610. // init size array
  611. $addsizes = array();
  612. // check there is custom sizes
  613. if ( is_array( $sizes_custom ) && !empty( $sizes_custom ) ) {
  614. foreach( $sizes_custom as $key => $value ) {
  615. // If we show this size in the admin
  616. if( isset( $value['s'] ) && $value['s'] == 1 ) {
  617. $addsizes[$key] = self::_getThumbnailName( $key );
  618. }
  619. }
  620. }
  621. // Merge the two array
  622. $newsizes = array_merge($sizes, $addsizes);
  623. // Add new size
  624. return $newsizes;
  625. }
  626. /**
  627. * Get a thumbnail name from its slug
  628. *
  629. * @access private
  630. * @param string $thumbnailSlug : the slug of the thumbnail
  631. * @return array
  632. * @since 2.3
  633. * @author Nicolas Juen
  634. */
  635. private static function _getThumbnailName( $thumbnailSlug = '' ) {
  636. // get the options
  637. $sizes_custom = get_option( SIS_OPTION );
  638. // If the size exists
  639. if( isset( $sizes_custom[$thumbnailSlug] ) ) {
  640. // If the name exists return it, slug by default
  641. if( isset( $sizes_custom[$thumbnailSlug]['n'] ) && !empty( $sizes_custom[$thumbnailSlug]['n'] ) ) {
  642. return $sizes_custom[$thumbnailSlug]['n'];
  643. } else {
  644. return $thumbnailSlug;
  645. }
  646. }
  647. // return slug if not found
  648. return $thumbnailSlug;
  649. }
  650. /**
  651. * Get a thumbnail name from its slug
  652. *
  653. * @access public
  654. * @param array $fields : the fields of the media
  655. * @param object $post : the post object
  656. * @return array
  657. * @since 2.3.1
  658. * @author Nicolas Juen
  659. */
  660. public static function addFieldRegenerate( $fields, $post ) {
  661. // Check this is an image
  662. if( strpos( $post->post_mime_type, 'image' ) === false ) {
  663. return $fields;
  664. }
  665. $fields['sis-regenerate'] = array(
  666. 'label' => __( 'Regenerate Thumbnails', 'sis' ),
  667. 'input' => 'html',
  668. 'html' => '
  669. <input type="button" data-id="'.$post->ID.'" class="button title sis-regenerate-one" value="'.__( 'Regenerate Thumbnails', 'sis' ).'" />
  670. <span class="title"><em></em></span>
  671. <input type="hidden" class="regen" value="'.wp_create_nonce( 'regen' ).'" />',
  672. 'show_in_edit' => true,
  673. 'show_in_modal' => false,
  674. );
  675. return $fields;
  676. }
  677. /**
  678. * Display a json encoded element with right headers
  679. *
  680. * @param $data(optional) : the element to display ( if needed )
  681. * @return void
  682. * @author Nicolas Juen
  683. */
  684. private static function displayJson( $data = array() ) {
  685. header('Cache-Control: no-cache, must-revalidate');
  686. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  687. header('Content-type: application/json');
  688. echo json_encode( $data );
  689. die();
  690. }
  691. }