Changeset 2979112
- Timestamp:
- 10/15/2023 04:36:27 AM (10 months ago)
- Location:
- smntcs-image-dimensions
- Files:
-
- 16 added
- 8 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
smntcs-image-dimensions/tags/1.5/README.md
r2828033 r2979112 2 2 3 3 ![Support Level](https://img.shields.io/badge/support-active-green.svg) 4 ![Build Status](https://github.com/nielslange/smntcs- quantity-buttons-for-woocommerce/actions/workflows/test.yml/badge.svg)4 ![Build Status](https://github.com/nielslange/smntcs-/actions/workflows/test.yml/badge.svg) 5 5 ![GPLv2 License](https://img.shields.io/github/license/nielslange/smntcs-image-dimensions.svg) 6 6 ![Compatible to WordPress version](https://plugintests.com/plugins/smntcs-image-dimensions/wp-badge.svg) … … 23 23 ## Changelog 24 24 25 = 1.4 (2022.12.03) = 25 ### 1.5 (2023.10.15) 26 27 - Test up to WP 6.4 28 29 ### 1.4 (2022.12.03) 26 30 27 31 - Test up to WP 6.1 28 32 29 = 1.3 (2022.08.15) = 33 ### 1.3 (2022.08.15) 30 34 31 35 - Add sortable colums height and width -
smntcs-image-dimensions/tags/1.5/README.txt
r2828033 r2979112 3 3 Contributors: nielslange 4 4 Tags: Media Dimensions, Media File Size, Media Library 5 Stable tag: 1. 46 Tested up to: 6. 15 Stable tag: 1. 6 Tested up to: 6. 7 7 Requires PHP: 5.6 8 8 Requires at least: 5.2 … … 27 27 == Changelog == 28 28 29 30 31 32 29 33 = 1.4 (2022.12.03) = 30 34 -
smntcs-image-dimensions/tags/1.5/phpcs.dist.xml
r2712401 r2979112 1 1 <?xml version="1.0"?> 2 2 <ruleset name="smntcs-image-dimensions"> 3 3 <description>Custom coding standards for SMNTCS Image Dimensions</description> 4 4 5 6 5 <!-- Check the entire theme. --> 6 <file>.</file> 7 7 8 9 10 8 <!-- Exclude patterns. --> 9 <exclude-pattern>/vendor/*</exclude-pattern> 10 <exclude-pattern>/node_modules/*</exclude-pattern> 11 11 12 13 14 15 16 17 12 <!-- Additional arguments. --> 13 <arg value="sp"/> 14 <arg name="basepath" value="."/> 15 <arg name="parallel" value="8"/> 16 <arg name="cache"/> 17 <arg name="extensions" value="php"/> 18 18 19 20 21 19 <!-- Include the WordPress rulesets. --> 20 <rule ref="WordPress-Extra"/> 21 <rule ref="WordPress-Docs"/> 22 22 23 <!-- We want to allow exceptions for the filename naming, since we are working with a theme. --> 24 <!-- See https://github.com/WordPress/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#themes-allow-filename-exceptions --> 25 <rule ref="WordPress.Files.FileName"> 26 <properties> 27 <property name="is_theme" value="false"/> 28 </properties> 29 </rule> 23 <!-- We want to allow exceptions for the filename naming, since we are working with a theme. --> 24 <!-- See https://github.com/WordPress/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#themes-allow-filename-exceptions --> 25 <rule ref="WordPress.Files.FileName"> 26 <properties> 27 <property name="is_theme" value="false"/> 28 <property name="strict_class_file_names" value="false"/> 29 </properties> 30 </rule> 30 31 31 32 33 34 35 36 32 <!-- Allow multiple parameters on one line for multi-line function calls. --> 33 <rule ref="PEAR.Functions.FunctionCallSignature"> 34 <properties> 35 <property name="allowMultipleArguments" value="true" /> 36 </properties> 37 </rule> 37 38 38 39 40 41 42 39 <!-- Improve code readablilty by allowing the artguments after function call. --> 40 <rule ref="PEAR.Functions.FunctionCallSignature"> 41 <exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket"/> 42 <exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine"/> 43 </rule> 43 44 44 45 46 47 48 49 50 51 45 <!-- Check valid textdomain. --> 46 <rule ref="WordPress.WP.I18n"> 47 <properties> 48 <property name="text_domain" type="array"> 49 <element value="smntcs-image-dimensions"/> 50 </property> 51 </properties> 52 </rule> 52 53 53 54 55 56 54 <rule ref="WordPress-Extra"> 55 <!-- _e() and _ex() are considered safe for core translations. --> 56 <exclude name="WordPress.Security.EscapeOutput.UnsafePrintingFunction"/> 57 </rule> 57 58 58 59 60 61 62 63 64 65 66 67 68 69 70 59 <rule ref="WordPress.Security.EscapeOutput"> 60 <properties> 61 <property name="customAutoEscapedFunctions" type="array"> 62 <!-- i18n functions are considered safe for core translations. --> 63 <element value="__"/> 64 <element value="_x"/> 65 <element value="_n"/> 66 <element value="_nx"/> 67 <element value="number_format_i18n"/> 68 <element value="date_i18n"/> 69 </property> 70 </properties> 71 </rule> 71 72 72 73 </ruleset> -
smntcs-image-dimensions/tags/1.5/smntcs-image-dimensions.php
r2828033 r2979112 7 7 * Author URI: https://nielslange.de 8 8 * Text Domain: smntcs-image-dimensions 9 * Version: 1. 49 * Version: 1. 10 10 * Requires PHP: 5.6 11 11 * Requires at least: 5.2 … … 19 19 20 20 /** 21 * Add image file size to post meta on plugin activation. 22 * 23 * @return void 21 * Class SMNTCS_Image_Dimensions 24 22 */ 25 function smntcs_activate_plugin() { 26 global $wpdb; 23 class SMNTCS_Image_Dimensions { 27 24 28 $attachments = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_mime_type LIKE '%image%'" ); 29 foreach ( $attachments as $attachment ) { 30 $filesize = filesize( get_attached_file( $attachment->ID ) ); 31 update_post_meta( $attachment->ID, '_filesize', $filesize ); 25 /** 26 * SMNTCS_Image_Dimensions constructor. 27 */ 28 public function __construct() { 29 register_activation_hook( __FILE__, array( $this, 'activate_plugin' ) ); 30 register_deactivation_hook( __FILE__, array( $this, 'deactivate_plugin' ) ); 32 31 33 list(, $width, $height) = wp_get_attachment_image_src( $attachment->ID, 'full' ); 34 update_post_meta( $attachment->ID, '_width', intval( $width ) ); 35 update_post_meta( $attachment->ID, '_height', intval( $height ) ); 32 add_action( 'add_attachment', array( $this, 'attachment_fields_to_save' ), 10, 1 ); 33 add_filter( 'manage_media_columns', array( $this, 'manage_media_columns' ) ); 34 add_action( 'manage_media_custom_column', array( $this, 'manage_media_custom_column' ), 10, 2 ); 35 add_filter( 'manage_upload_sortable_columns', array( $this, 'manage_upload_sortable_columns' ) ); 36 add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); 37 add_action( 'admin_head', array( $this, 'admin_head' ) ); 38 } 39 40 /** 41 * Activate plugin. 42 */ 43 public function activate_plugin() { 44 global $wpdb; 45 46 $attachments = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_mime_type LIKE '%image%'" ); 47 foreach ( $attachments as $attachment ) { 48 $filesize = filesize( get_attached_file( $attachment->ID ) ); 49 update_post_meta( $attachment->ID, '_filesize', $filesize ); 50 51 list(, $width, $height) = wp_get_attachment_image_src( $attachment->ID, 'full' ); 52 update_post_meta( $attachment->ID, '_width', intval( $width ) ); 53 update_post_meta( $attachment->ID, '_height', intval( $height ) ); 54 } 55 } 56 57 /** 58 * Deactivate plugin. 59 */ 60 public function deactivate_plugin() { 61 global $wpdb; 62 63 $attachments = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_mime_type LIKE '%image%'" ); 64 foreach ( $attachments as $attachment ) { 65 delete_post_meta( $attachment->ID, '_filesize' ); 66 } 67 } 68 69 /** 70 * Save attachment fields. 71 * 72 * @param int $post_id Post ID. 73 */ 74 public function attachment_fields_to_save( $post_id ) { 75 $filesize = filesize( get_attached_file( $post_id ) ); 76 update_post_meta( $post_id, '_filesize', $filesize ); 77 78 list(, $width, $height) = wp_get_attachment_image_src( $post_id, 'full' ); 79 update_post_meta( $post_id, '_width', intval( $width ) ); 80 update_post_meta( $post_id, '_height', intval( $height ) ); 81 } 82 83 /** 84 * Manage media columns. 85 * 86 * @param array $posts_columns Posts columns. 87 * @return array 88 */ 89 public function manage_media_columns( $posts_columns ) { 90 $posts_columns['dimensions'] = __( 'Dimensions', 'smntcs-image-dimensions' ); 91 $posts_columns['width'] = __( 'Width', 'smntcs-image-dimensions' ); 92 $posts_columns['height'] = __( 'Height', 'smntcs-image-dimensions' ); 93 $posts_columns['filesize'] = __( 'File Size', 'smntcs-image-dimensions' ); 94 return $posts_columns; 95 } 96 97 /** 98 * Manage media custom column. 99 * 100 * @param string $column_name Column name. 101 * @param int $post_id Post ID. 102 */ 103 public function manage_media_custom_column( $column_name, $post_id ) { 104 if ( 'dimensions' === $column_name ) { 105 list($url, $width, $height) = wp_get_attachment_image_src( $post_id, 'full' ); 106 printf( '%d × %d', (int) $width, (int) $height ); 107 } 108 109 if ( 'width' === $column_name ) { 110 $width = get_post_meta( $post_id, '_width', true ); 111 print( esc_attr( $width ) ); 112 } 113 114 if ( 'height' === $column_name ) { 115 $height = get_post_meta( $post_id, '_height', true ); 116 print( esc_attr( $height ) ); 117 } 118 119 if ( 'filesize' === $column_name ) { 120 $filesize = get_post_meta( $post_id, '_filesize', true ); 121 print( esc_attr( size_format( $filesize, 2 ) ) ); 122 } 123 } 124 125 /** 126 * Manage upload sortable columns. 127 * 128 * @param array $columns Columns. 129 * @return array 130 */ 131 public function manage_upload_sortable_columns( $columns ) { 132 $columns['height'] = 'height'; 133 $columns['width'] = 'width'; 134 $columns['filesize'] = 'filesize'; 135 136 return $columns; 137 } 138 139 /** 140 * Pre get posts. 141 * 142 * @param WP_Query $query Query. 143 */ 144 public function pre_get_posts( $query ) { 145 if ( ! is_admin() ) { 146 return; 147 } 148 149 $orderby = $query->get( 'orderby' ); 150 if ( 'width' === $orderby ) { 151 $query->set( 'meta_key', '_width' ); 152 $query->set( 'orderby', 'meta_value_num' ); 153 } 154 155 if ( 'height' === $orderby ) { 156 $query->set( 'meta_key', '_height' ); 157 $query->set( 'orderby', 'meta_value_num' ); 158 } 159 160 if ( 'filesize' === $orderby ) { 161 $query->set( 'meta_key', '_filesize' ); 162 $query->set( 'orderby', 'meta_value_num' ); 163 } 164 } 165 166 /** 167 * Add admin CSS. 168 */ 169 public function admin_head() { 170 echo '<style>'; 171 echo '.column-width, .column-height, .column-filesize { text-align: right; }'; 172 echo '.column-filesize { width: 10%; }'; 173 echo '</style>'; 36 174 } 37 175 } 38 register_activation_hook( __FILE__, 'smntcs_activate_plugin' );39 176 40 /** 41 * Remove image file size from post meta on plugin activation. 42 * 43 * @return void 44 */ 45 function smntcs_deactivate_plugin() { 46 global $wpdb; 47 48 $attachments = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_mime_type LIKE '%image%'" ); 49 foreach ( $attachments as $attachment ) { 50 delete_post_meta( $attachment->ID, '_filesize' ); 51 } 52 } 53 register_deactivation_hook( __FILE__, 'smntcs_deactivate_plugin' ); 54 55 /** 56 * Add image file size to post meta after media upload. 57 * 58 * @param int $post_id The post ID of the attachment. 59 * @return void 60 */ 61 function smntcs_attachment_fields_to_save( $post_id ) { 62 $filesize = filesize( get_attached_file( $post_id ) ); 63 update_post_meta( $post_id, '_filesize', $filesize ); 64 65 list(, $width, $height) = wp_get_attachment_image_src( $post_id, 'full' ); 66 update_post_meta( $post_id, '_width', intval( $width ) ); 67 update_post_meta( $post_id, '_height', intval( $height ) ); 68 } 69 add_action( 'add_attachment', 'smntcs_attachment_fields_to_save', 10, 1 ); 70 71 /** 72 * Add custom admin column title. 73 * 74 * @param array $posts_columns The initial array of column headings. 75 * @return array The initial array of column headings. 76 */ 77 function smntcs_manage_media_columns( $posts_columns ) { 78 $posts_columns['dimensions'] = __( 'Dimensions', 'smntcs-image-dimensions' ); 79 $posts_columns['width'] = __( 'Width', 'smntcs-image-dimensions' ); 80 $posts_columns['height'] = __( 'Height', 'smntcs-image-dimensions' ); 81 $posts_columns['filesize'] = __( 'File Size', 'smntcs-image-dimensions' ); 82 return $posts_columns; 83 } 84 add_filter( 'manage_media_columns', 'smntcs_manage_media_columns' ); 85 86 /** 87 * Add custom admin column content. 88 * 89 * @param string $column_name The name of the column to display. 90 * @param int $post_id The ID of the post entry. 91 * @return void 92 */ 93 function smntcs_manage_media_custom_column( $column_name, $post_id ) { 94 if ( 'dimensions' === $column_name ) { 95 list($url, $width, $height) = wp_get_attachment_image_src( $post_id, 'full' ); 96 printf( '%d × %d', (int) $width, (int) $height ); 97 } 98 99 if ( 'width' === $column_name ) { 100 $width = get_post_meta( $post_id, '_width', true ); 101 print( esc_attr( $width ) ); 102 } 103 104 if ( 'height' === $column_name ) { 105 $height = get_post_meta( $post_id, '_height', true ); 106 print( esc_attr( $height ) ); 107 } 108 109 if ( 'filesize' === $column_name ) { 110 $filesize = get_post_meta( $post_id, '_filesize', true ); 111 print( esc_attr( size_format( $filesize, 2 ) ) ); 112 } 113 } 114 add_action( 'manage_media_custom_column', 'smntcs_manage_media_custom_column', 10, 2 ); 115 116 /** 117 * Make image filesize column sortable. 118 * 119 * @param array $columns The initial array of sortable columns. 120 * @return array The updated array of sortable columns. 121 */ 122 function smntcs_manage_upload_sortable_columns( $columns ) { 123 $columns['height'] = 'height'; 124 $columns['width'] = 'width'; 125 $columns['filesize'] = 'filesize'; 126 return $columns; 127 } 128 add_filter( 'manage_upload_sortable_columns', 'smntcs_manage_upload_sortable_columns' ); 129 130 /** 131 * Alter posts query when sorting image filesize. 132 * 133 * @param WP_Query $query The WP_Query instance. 134 * @return void 135 */ 136 function smntcs_pre_get_posts( $query ) { 137 if ( ! is_admin() ) { 138 return; 139 } 140 141 $orderby = $query->get( 'orderby' ); 142 if ( 'width' === $orderby ) { 143 $query->set( 'meta_key', '_width' ); 144 $query->set( 'meta_query', array( 'type' => 'NUMERIC' ) ); 145 $query->set( 'orderby', 'meta_value_num' ); 146 } 147 148 if ( 'height' === $orderby ) { 149 $query->set( 'meta_key', '_height' ); 150 $query->set( 'meta_query', array( 'type' => 'NUMERIC' ) ); 151 $query->set( 'orderby', 'meta_value_num' ); 152 } 153 154 if ( 'filesize' === $orderby ) { 155 $query->set( 'meta_key', '_filesize' ); 156 $query->set( 'orderby', 'meta_value_num' ); 157 } 158 } 159 add_action( 'pre_get_posts', 'smntcs_pre_get_posts' ); 160 161 /** 162 * Adjust width of custom admin columns. 163 * 164 * @return void 165 */ 166 function smntcs_admin_head() { 167 print( '<style>.column-dimensions, .column-height, .column-width, .column-filesize { width: 120px; }</style>' ); 168 } 169 add_action( 'admin_head', 'smntcs_admin_head' ); 177 new SMNTCS_Image_Dimensions(); -
smntcs-image-dimensions/trunk/README.md
r2828033 r2979112 2 2 3 3 ![Support Level](https://img.shields.io/badge/support-active-green.svg) 4 ![Build Status](https://github.com/nielslange/smntcs- quantity-buttons-for-woocommerce/actions/workflows/test.yml/badge.svg)4 ![Build Status](https://github.com/nielslange/smntcs-/actions/workflows/test.yml/badge.svg) 5 5 ![GPLv2 License](https://img.shields.io/github/license/nielslange/smntcs-image-dimensions.svg) 6 6 ![Compatible to WordPress version](https://plugintests.com/plugins/smntcs-image-dimensions/wp-badge.svg) … … 23 23 ## Changelog 24 24 25 = 1.4 (2022.12.03) = 25 ### 1.5 (2023.10.15) 26 27 - Test up to WP 6.4 28 29 ### 1.4 (2022.12.03) 26 30 27 31 - Test up to WP 6.1 28 32 29 = 1.3 (2022.08.15) = 33 ### 1.3 (2022.08.15) 30 34 31 35 - Add sortable colums height and width -
smntcs-image-dimensions/trunk/README.txt
r2828033 r2979112 3 3 Contributors: nielslange 4 4 Tags: Media Dimensions, Media File Size, Media Library 5 Stable tag: 1. 46 Tested up to: 6. 15 Stable tag: 1. 6 Tested up to: 6. 7 7 Requires PHP: 5.6 8 8 Requires at least: 5.2 … … 27 27 == Changelog == 28 28 29 30 31 32 29 33 = 1.4 (2022.12.03) = 30 34 -
smntcs-image-dimensions/trunk/phpcs.dist.xml
r2712401 r2979112 1 1 <?xml version="1.0"?> 2 2 <ruleset name="smntcs-image-dimensions"> 3 3 <description>Custom coding standards for SMNTCS Image Dimensions</description> 4 4 5 6 5 <!-- Check the entire theme. --> 6 <file>.</file> 7 7 8 9 10 8 <!-- Exclude patterns. --> 9 <exclude-pattern>/vendor/*</exclude-pattern> 10 <exclude-pattern>/node_modules/*</exclude-pattern> 11 11 12 13 14 15 16 17 12 <!-- Additional arguments. --> 13 <arg value="sp"/> 14 <arg name="basepath" value="."/> 15 <arg name="parallel" value="8"/> 16 <arg name="cache"/> 17 <arg name="extensions" value="php"/> 18 18 19 20 21 19 <!-- Include the WordPress rulesets. --> 20 <rule ref="WordPress-Extra"/> 21 <rule ref="WordPress-Docs"/> 22 22 23 <!-- We want to allow exceptions for the filename naming, since we are working with a theme. --> 24 <!-- See https://github.com/WordPress/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#themes-allow-filename-exceptions --> 25 <rule ref="WordPress.Files.FileName"> 26 <properties> 27 <property name="is_theme" value="false"/> 28 </properties> 29 </rule> 23 <!-- We want to allow exceptions for the filename naming, since we are working with a theme. --> 24 <!-- See https://github.com/WordPress/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#themes-allow-filename-exceptions --> 25 <rule ref="WordPress.Files.FileName"> 26 <properties> 27 <property name="is_theme" value="false"/> 28 <property name="strict_class_file_names" value="false"/> 29 </properties> 30 </rule> 30 31 31 32 33 34 35 36 32 <!-- Allow multiple parameters on one line for multi-line function calls. --> 33 <rule ref="PEAR.Functions.FunctionCallSignature"> 34 <properties> 35 <property name="allowMultipleArguments" value="true" /> 36 </properties> 37 </rule> 37 38 38 39 40 41 42 39 <!-- Improve code readablilty by allowing the artguments after function call. --> 40 <rule ref="PEAR.Functions.FunctionCallSignature"> 41 <exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket"/> 42 <exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine"/> 43 </rule> 43 44 44 45 46 47 48 49 50 51 45 <!-- Check valid textdomain. --> 46 <rule ref="WordPress.WP.I18n"> 47 <properties> 48 <property name="text_domain" type="array"> 49 <element value="smntcs-image-dimensions"/> 50 </property> 51 </properties> 52 </rule> 52 53 53 54 55 56 54 <rule ref="WordPress-Extra"> 55 <!-- _e() and _ex() are considered safe for core translations. --> 56 <exclude name="WordPress.Security.EscapeOutput.UnsafePrintingFunction"/> 57 </rule> 57 58 58 59 60 61 62 63 64 65 66 67 68 69 70 59 <rule ref="WordPress.Security.EscapeOutput"> 60 <properties> 61 <property name="customAutoEscapedFunctions" type="array"> 62 <!-- i18n functions are considered safe for core translations. --> 63 <element value="__"/> 64 <element value="_x"/> 65 <element value="_n"/> 66 <element value="_nx"/> 67 <element value="number_format_i18n"/> 68 <element value="date_i18n"/> 69 </property> 70 </properties> 71 </rule> 71 72 72 73 </ruleset> -
smntcs-image-dimensions/trunk/smntcs-image-dimensions.php
r2828033 r2979112 7 7 * Author URI: https://nielslange.de 8 8 * Text Domain: smntcs-image-dimensions 9 * Version: 1. 49 * Version: 1. 10 10 * Requires PHP: 5.6 11 11 * Requires at least: 5.2 … … 19 19 20 20 /** 21 * Add image file size to post meta on plugin activation. 22 * 23 * @return void 21 * Class SMNTCS_Image_Dimensions 24 22 */ 25 function smntcs_activate_plugin() { 26 global $wpdb; 23 class SMNTCS_Image_Dimensions { 27 24 28 $attachments = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_mime_type LIKE '%image%'" ); 29 foreach ( $attachments as $attachment ) { 30 $filesize = filesize( get_attached_file( $attachment->ID ) ); 31 update_post_meta( $attachment->ID, '_filesize', $filesize ); 25 /** 26 * SMNTCS_Image_Dimensions constructor. 27 */ 28 public function __construct() { 29 register_activation_hook( __FILE__, array( $this, 'activate_plugin' ) ); 30 register_deactivation_hook( __FILE__, array( $this, 'deactivate_plugin' ) ); 32 31 33 list(, $width, $height) = wp_get_attachment_image_src( $attachment->ID, 'full' ); 34 update_post_meta( $attachment->ID, '_width', intval( $width ) ); 35 update_post_meta( $attachment->ID, '_height', intval( $height ) ); 32 add_action( 'add_attachment', array( $this, 'attachment_fields_to_save' ), 10, 1 ); 33 add_filter( 'manage_media_columns', array( $this, 'manage_media_columns' ) ); 34 add_action( 'manage_media_custom_column', array( $this, 'manage_media_custom_column' ), 10, 2 ); 35 add_filter( 'manage_upload_sortable_columns', array( $this, 'manage_upload_sortable_columns' ) ); 36 add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); 37 add_action( 'admin_head', array( $this, 'admin_head' ) ); 38 } 39 40 /** 41 * Activate plugin. 42 */ 43 public function activate_plugin() { 44 global $wpdb; 45 46 $attachments = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_mime_type LIKE '%image%'" ); 47 foreach ( $attachments as $attachment ) { 48 $filesize = filesize( get_attached_file( $attachment->ID ) ); 49 update_post_meta( $attachment->ID, '_filesize', $filesize ); 50 51 list(, $width, $height) = wp_get_attachment_image_src( $attachment->ID, 'full' ); 52 update_post_meta( $attachment->ID, '_width', intval( $width ) ); 53 update_post_meta( $attachment->ID, '_height', intval( $height ) ); 54 } 55 } 56 57 /** 58 * Deactivate plugin. 59 */ 60 public function deactivate_plugin() { 61 global $wpdb; 62 63 $attachments = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_mime_type LIKE '%image%'" ); 64 foreach ( $attachments as $attachment ) { 65 delete_post_meta( $attachment->ID, '_filesize' ); 66 } 67 } 68 69 /** 70 * Save attachment fields. 71 * 72 * @param int $post_id Post ID. 73 */ 74 public function attachment_fields_to_save( $post_id ) { 75 $filesize = filesize( get_attached_file( $post_id ) ); 76 update_post_meta( $post_id, '_filesize', $filesize ); 77 78 list(, $width, $height) = wp_get_attachment_image_src( $post_id, 'full' ); 79 update_post_meta( $post_id, '_width', intval( $width ) ); 80 update_post_meta( $post_id, '_height', intval( $height ) ); 81 } 82 83 /** 84 * Manage media columns. 85 * 86 * @param array $posts_columns Posts columns. 87 * @return array 88 */ 89 public function manage_media_columns( $posts_columns ) { 90 $posts_columns['dimensions'] = __( 'Dimensions', 'smntcs-image-dimensions' ); 91 $posts_columns['width'] = __( 'Width', 'smntcs-image-dimensions' ); 92 $posts_columns['height'] = __( 'Height', 'smntcs-image-dimensions' ); 93 $posts_columns['filesize'] = __( 'File Size', 'smntcs-image-dimensions' ); 94 return $posts_columns; 95 } 96 97 /** 98 * Manage media custom column. 99 * 100 * @param string $column_name Column name. 101 * @param int $post_id Post ID. 102 */ 103 public function manage_media_custom_column( $column_name, $post_id ) { 104 if ( 'dimensions' === $column_name ) { 105 list($url, $width, $height) = wp_get_attachment_image_src( $post_id, 'full' ); 106 printf( '%d × %d', (int) $width, (int) $height ); 107 } 108 109 if ( 'width' === $column_name ) { 110 $width = get_post_meta( $post_id, '_width', true ); 111 print( esc_attr( $width ) ); 112 } 113 114 if ( 'height' === $column_name ) { 115 $height = get_post_meta( $post_id, '_height', true ); 116 print( esc_attr( $height ) ); 117 } 118 119 if ( 'filesize' === $column_name ) { 120 $filesize = get_post_meta( $post_id, '_filesize', true ); 121 print( esc_attr( size_format( $filesize, 2 ) ) ); 122 } 123 } 124 125 /** 126 * Manage upload sortable columns. 127 * 128 * @param array $columns Columns. 129 * @return array 130 */ 131 public function manage_upload_sortable_columns( $columns ) { 132 $columns['height'] = 'height'; 133 $columns['width'] = 'width'; 134 $columns['filesize'] = 'filesize'; 135 136 return $columns; 137 } 138 139 /** 140 * Pre get posts. 141 * 142 * @param WP_Query $query Query. 143 */ 144 public function pre_get_posts( $query ) { 145 if ( ! is_admin() ) { 146 return; 147 } 148 149 $orderby = $query->get( 'orderby' ); 150 if ( 'width' === $orderby ) { 151 $query->set( 'meta_key', '_width' ); 152 $query->set( 'orderby', 'meta_value_num' ); 153 } 154 155 if ( 'height' === $orderby ) { 156 $query->set( 'meta_key', '_height' ); 157 $query->set( 'orderby', 'meta_value_num' ); 158 } 159 160 if ( 'filesize' === $orderby ) { 161 $query->set( 'meta_key', '_filesize' ); 162 $query->set( 'orderby', 'meta_value_num' ); 163 } 164 } 165 166 /** 167 * Add admin CSS. 168 */ 169 public function admin_head() { 170 echo '<style>'; 171 echo '.column-width, .column-height, .column-filesize { text-align: right; }'; 172 echo '.column-filesize { width: 10%; }'; 173 echo '</style>'; 36 174 } 37 175 } 38 register_activation_hook( __FILE__, 'smntcs_activate_plugin' );39 176 40 /** 41 * Remove image file size from post meta on plugin activation. 42 * 43 * @return void 44 */ 45 function smntcs_deactivate_plugin() { 46 global $wpdb; 47 48 $attachments = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_mime_type LIKE '%image%'" ); 49 foreach ( $attachments as $attachment ) { 50 delete_post_meta( $attachment->ID, '_filesize' ); 51 } 52 } 53 register_deactivation_hook( __FILE__, 'smntcs_deactivate_plugin' ); 54 55 /** 56 * Add image file size to post meta after media upload. 57 * 58 * @param int $post_id The post ID of the attachment. 59 * @return void 60 */ 61 function smntcs_attachment_fields_to_save( $post_id ) { 62 $filesize = filesize( get_attached_file( $post_id ) ); 63 update_post_meta( $post_id, '_filesize', $filesize ); 64 65 list(, $width, $height) = wp_get_attachment_image_src( $post_id, 'full' ); 66 update_post_meta( $post_id, '_width', intval( $width ) ); 67 update_post_meta( $post_id, '_height', intval( $height ) ); 68 } 69 add_action( 'add_attachment', 'smntcs_attachment_fields_to_save', 10, 1 ); 70 71 /** 72 * Add custom admin column title. 73 * 74 * @param array $posts_columns The initial array of column headings. 75 * @return array The initial array of column headings. 76 */ 77 function smntcs_manage_media_columns( $posts_columns ) { 78 $posts_columns['dimensions'] = __( 'Dimensions', 'smntcs-image-dimensions' ); 79 $posts_columns['width'] = __( 'Width', 'smntcs-image-dimensions' ); 80 $posts_columns['height'] = __( 'Height', 'smntcs-image-dimensions' ); 81 $posts_columns['filesize'] = __( 'File Size', 'smntcs-image-dimensions' ); 82 return $posts_columns; 83 } 84 add_filter( 'manage_media_columns', 'smntcs_manage_media_columns' ); 85 86 /** 87 * Add custom admin column content. 88 * 89 * @param string $column_name The name of the column to display. 90 * @param int $post_id The ID of the post entry. 91 * @return void 92 */ 93 function smntcs_manage_media_custom_column( $column_name, $post_id ) { 94 if ( 'dimensions' === $column_name ) { 95 list($url, $width, $height) = wp_get_attachment_image_src( $post_id, 'full' ); 96 printf( '%d × %d', (int) $width, (int) $height ); 97 } 98 99 if ( 'width' === $column_name ) { 100 $width = get_post_meta( $post_id, '_width', true ); 101 print( esc_attr( $width ) ); 102 } 103 104 if ( 'height' === $column_name ) { 105 $height = get_post_meta( $post_id, '_height', true ); 106 print( esc_attr( $height ) ); 107 } 108 109 if ( 'filesize' === $column_name ) { 110 $filesize = get_post_meta( $post_id, '_filesize', true ); 111 print( esc_attr( size_format( $filesize, 2 ) ) ); 112 } 113 } 114 add_action( 'manage_media_custom_column', 'smntcs_manage_media_custom_column', 10, 2 ); 115 116 /** 117 * Make image filesize column sortable. 118 * 119 * @param array $columns The initial array of sortable columns. 120 * @return array The updated array of sortable columns. 121 */ 122 function smntcs_manage_upload_sortable_columns( $columns ) { 123 $columns['height'] = 'height'; 124 $columns['width'] = 'width'; 125 $columns['filesize'] = 'filesize'; 126 return $columns; 127 } 128 add_filter( 'manage_upload_sortable_columns', 'smntcs_manage_upload_sortable_columns' ); 129 130 /** 131 * Alter posts query when sorting image filesize. 132 * 133 * @param WP_Query $query The WP_Query instance. 134 * @return void 135 */ 136 function smntcs_pre_get_posts( $query ) { 137 if ( ! is_admin() ) { 138 return; 139 } 140 141 $orderby = $query->get( 'orderby' ); 142 if ( 'width' === $orderby ) { 143 $query->set( 'meta_key', '_width' ); 144 $query->set( 'meta_query', array( 'type' => 'NUMERIC' ) ); 145 $query->set( 'orderby', 'meta_value_num' ); 146 } 147 148 if ( 'height' === $orderby ) { 149 $query->set( 'meta_key', '_height' ); 150 $query->set( 'meta_query', array( 'type' => 'NUMERIC' ) ); 151 $query->set( 'orderby', 'meta_value_num' ); 152 } 153 154 if ( 'filesize' === $orderby ) { 155 $query->set( 'meta_key', '_filesize' ); 156 $query->set( 'orderby', 'meta_value_num' ); 157 } 158 } 159 add_action( 'pre_get_posts', 'smntcs_pre_get_posts' ); 160 161 /** 162 * Adjust width of custom admin columns. 163 * 164 * @return void 165 */ 166 function smntcs_admin_head() { 167 print( '<style>.column-dimensions, .column-height, .column-width, .column-filesize { width: 120px; }</style>' ); 168 } 169 add_action( 'admin_head', 'smntcs_admin_head' ); 177 new SMNTCS_Image_Dimensions();
Note: See TracChangeset
for help on using the changeset viewer.