Plugin Directory

Changeset 2979112

Timestamp:
10/15/2023 04:36:27 AM (10 months ago)
Author:
nielslange
Message:

Update to version 1.5 from GitHub

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  
    22
    33![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)
    55![GPLv2 License](https://img.shields.io/github/license/nielslange/smntcs-image-dimensions.svg)
    66![Compatible to WordPress version](https://plugintests.com/plugins/smntcs-image-dimensions/wp-badge.svg)
     
    2323## Changelog
    2424
    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)
    2630
    2731-   Test up to WP 6.1
    2832
    29 = 1.3 (2022.08.15) =
     33### 1.3 (2022.08.15)
    3034
    3135-   Add sortable colums height and width
  • smntcs-image-dimensions/tags/1.5/README.txt

    r2828033 r2979112  
    33Contributors:       nielslange
    44Tags:               Media Dimensions, Media File Size, Media Library
    5 Stable tag:         1.4
    6 Tested up to:       6.1
     5Stable tag:         1.
     6Tested up to:       6.
    77Requires PHP:       5.6
    88Requires at least:  5.2
     
    2727== Changelog ==
    2828
     29
     30
     31
     32
    2933= 1.4 (2022.12.03) =
    3034
  • smntcs-image-dimensions/tags/1.5/phpcs.dist.xml

    r2712401 r2979112  
    11<?xml version="1.0"?>
    22<ruleset name="smntcs-image-dimensions">
    3     <description>Custom coding standards for SMNTCS Image Dimensions</description>
     3<description>Custom coding standards for SMNTCS Image Dimensions</description>
    44
    5   <!-- Check the entire theme. -->
    6   <file>.</file>
     5<!-- Check the entire theme. -->
     6<file>.</file>
    77
    8   <!-- Exclude patterns. -->
    9   <exclude-pattern>/vendor/*</exclude-pattern>
    10   <exclude-pattern>/node_modules/*</exclude-pattern>
     8<!-- Exclude patterns. -->
     9<exclude-pattern>/vendor/*</exclude-pattern>
     10<exclude-pattern>/node_modules/*</exclude-pattern>
    1111
    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"/>
     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"/>
    1818
    19   <!-- Include the WordPress rulesets. -->
    20   <rule ref="WordPress-Extra"/>
    21   <rule ref="WordPress-Docs"/>
     19<!-- Include the WordPress rulesets. -->
     20<rule ref="WordPress-Extra"/>
     21<rule ref="WordPress-Docs"/>
    2222
    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>
    3031
    31   <!-- Allow multiple parameters on one line for multi-line function calls. -->
    32   <rule ref="PEAR.Functions.FunctionCallSignature">
    33     <properties>
    34        <property name="allowMultipleArguments" value="true" />
    35     </properties>
    36   </rule>
     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>
    3738
    38   <!-- Improve code readablilty by allowing the artguments after function call. -->
    39   <rule ref="PEAR.Functions.FunctionCallSignature">
    40     <exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket"/>
    41     <exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine"/>
    42   </rule>
     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>
    4344
    44   <!-- Check valid textdomain. -->
    45   <rule ref="WordPress.WP.I18n">
    46     <properties>
    47       <property name="text_domain" type="array">
    48         <element value="smntcs-image-dimensions"/>
    49       </property>
    50     </properties>
    51   </rule>
     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>
    5253
    53   <rule ref="WordPress-Extra">
    54     <!-- _e() and _ex() are considered safe for core translations. -->
    55     <exclude name="WordPress.Security.EscapeOutput.UnsafePrintingFunction"/>
    56   </rule>
     54<rule ref="WordPress-Extra">
     55<!-- _e() and _ex() are considered safe for core translations. -->
     56<exclude name="WordPress.Security.EscapeOutput.UnsafePrintingFunction"/>
     57</rule>
    5758
    58   <rule ref="WordPress.Security.EscapeOutput">
    59     <properties>
    60       <property name="customAutoEscapedFunctions" type="array">
    61         <!-- i18n functions are considered safe for core translations. -->
    62         <element value="__"/>
    63         <element value="_x"/>
    64         <element value="_n"/>
    65         <element value="_nx"/>
    66         <element value="number_format_i18n"/>
    67         <element value="date_i18n"/>
    68       </property>
    69     </properties>
    70   </rule>
     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>
    7172
    7273</ruleset>
  • smntcs-image-dimensions/tags/1.5/smntcs-image-dimensions.php

    r2828033 r2979112  
    77 * Author URI:            https://nielslange.de
    88 * Text Domain:           smntcs-image-dimensions
    9  * Version:               1.4
     9 * Version:               1.
    1010 * Requires PHP:          5.6
    1111 * Requires at least:     5.2
     
    1919
    2020/**
    21  * Add image file size to post meta on plugin activation.
    22  *
    23  * @return void
     21 * Class SMNTCS_Image_Dimensions
    2422 */
    25 function smntcs_activate_plugin() {
    26     global $wpdb;
     23class SMNTCS_Image_Dimensions {
    2724
    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' ) );
    3231
    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 &times; %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>';
    36174    }
    37175}
    38 register_activation_hook( __FILE__, 'smntcs_activate_plugin' );
    39176
    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 &times; %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' );
     177new SMNTCS_Image_Dimensions();
  • smntcs-image-dimensions/trunk/README.md

    r2828033 r2979112  
    22
    33![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)
    55![GPLv2 License](https://img.shields.io/github/license/nielslange/smntcs-image-dimensions.svg)
    66![Compatible to WordPress version](https://plugintests.com/plugins/smntcs-image-dimensions/wp-badge.svg)
     
    2323## Changelog
    2424
    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)
    2630
    2731-   Test up to WP 6.1
    2832
    29 = 1.3 (2022.08.15) =
     33### 1.3 (2022.08.15)
    3034
    3135-   Add sortable colums height and width
  • smntcs-image-dimensions/trunk/README.txt

    r2828033 r2979112  
    33Contributors:       nielslange
    44Tags:               Media Dimensions, Media File Size, Media Library
    5 Stable tag:         1.4
    6 Tested up to:       6.1
     5Stable tag:         1.
     6Tested up to:       6.
    77Requires PHP:       5.6
    88Requires at least:  5.2
     
    2727== Changelog ==
    2828
     29
     30
     31
     32
    2933= 1.4 (2022.12.03) =
    3034
  • smntcs-image-dimensions/trunk/phpcs.dist.xml

    r2712401 r2979112  
    11<?xml version="1.0"?>
    22<ruleset name="smntcs-image-dimensions">
    3     <description>Custom coding standards for SMNTCS Image Dimensions</description>
     3<description>Custom coding standards for SMNTCS Image Dimensions</description>
    44
    5   <!-- Check the entire theme. -->
    6   <file>.</file>
     5<!-- Check the entire theme. -->
     6<file>.</file>
    77
    8   <!-- Exclude patterns. -->
    9   <exclude-pattern>/vendor/*</exclude-pattern>
    10   <exclude-pattern>/node_modules/*</exclude-pattern>
     8<!-- Exclude patterns. -->
     9<exclude-pattern>/vendor/*</exclude-pattern>
     10<exclude-pattern>/node_modules/*</exclude-pattern>
    1111
    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"/>
     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"/>
    1818
    19   <!-- Include the WordPress rulesets. -->
    20   <rule ref="WordPress-Extra"/>
    21   <rule ref="WordPress-Docs"/>
     19<!-- Include the WordPress rulesets. -->
     20<rule ref="WordPress-Extra"/>
     21<rule ref="WordPress-Docs"/>
    2222
    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>
    3031
    31   <!-- Allow multiple parameters on one line for multi-line function calls. -->
    32   <rule ref="PEAR.Functions.FunctionCallSignature">
    33     <properties>
    34        <property name="allowMultipleArguments" value="true" />
    35     </properties>
    36   </rule>
     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>
    3738
    38   <!-- Improve code readablilty by allowing the artguments after function call. -->
    39   <rule ref="PEAR.Functions.FunctionCallSignature">
    40     <exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket"/>
    41     <exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine"/>
    42   </rule>
     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>
    4344
    44   <!-- Check valid textdomain. -->
    45   <rule ref="WordPress.WP.I18n">
    46     <properties>
    47       <property name="text_domain" type="array">
    48         <element value="smntcs-image-dimensions"/>
    49       </property>
    50     </properties>
    51   </rule>
     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>
    5253
    53   <rule ref="WordPress-Extra">
    54     <!-- _e() and _ex() are considered safe for core translations. -->
    55     <exclude name="WordPress.Security.EscapeOutput.UnsafePrintingFunction"/>
    56   </rule>
     54<rule ref="WordPress-Extra">
     55<!-- _e() and _ex() are considered safe for core translations. -->
     56<exclude name="WordPress.Security.EscapeOutput.UnsafePrintingFunction"/>
     57</rule>
    5758
    58   <rule ref="WordPress.Security.EscapeOutput">
    59     <properties>
    60       <property name="customAutoEscapedFunctions" type="array">
    61         <!-- i18n functions are considered safe for core translations. -->
    62         <element value="__"/>
    63         <element value="_x"/>
    64         <element value="_n"/>
    65         <element value="_nx"/>
    66         <element value="number_format_i18n"/>
    67         <element value="date_i18n"/>
    68       </property>
    69     </properties>
    70   </rule>
     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>
    7172
    7273</ruleset>
  • smntcs-image-dimensions/trunk/smntcs-image-dimensions.php

    r2828033 r2979112  
    77 * Author URI:            https://nielslange.de
    88 * Text Domain:           smntcs-image-dimensions
    9  * Version:               1.4
     9 * Version:               1.
    1010 * Requires PHP:          5.6
    1111 * Requires at least:     5.2
     
    1919
    2020/**
    21  * Add image file size to post meta on plugin activation.
    22  *
    23  * @return void
     21 * Class SMNTCS_Image_Dimensions
    2422 */
    25 function smntcs_activate_plugin() {
    26     global $wpdb;
     23class SMNTCS_Image_Dimensions {
    2724
    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' ) );
    3231
    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 &times; %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>';
    36174    }
    37175}
    38 register_activation_hook( __FILE__, 'smntcs_activate_plugin' );
    39176
    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 &times; %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' );
     177new SMNTCS_Image_Dimensions();
Note: See TracChangeset for help on using the changeset viewer.