├── .github └── workflows │ ├── deploytowp.yml │ ├── performance-check.yml │ ├── plugin-check.yml │ └── upateassets.yml ├── .wordpress-org ├── banner-1544x500.png ├── banner-772x250.png ├── icon-128x128.png ├── icon-256x256.png ├── screenshot-1.png └── screenshot-2.png ├── LICENSE ├── README.md ├── env └── .nvmrc ├── modern-images-wp.php ├── readme.txt └── src ├── Plugin.php └── Setting.php /.github/workflows/deploytowp.yml: -------------------------------------------------------------------------------- 1 | name: Deploy to WordPress.org 2 | on: 3 | push: 4 | tags: 5 | - "**" 6 | jobs: 7 | tag: 8 | name: New tag 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@main 12 | - name: WordPress Plugin Deploy 13 | uses: 10up/action-wordpress-plugin-deploy@stable 14 | env: 15 | SVN_USERNAME: ${{ secrets.SVN_USERNAME }} 16 | SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }} 17 | -------------------------------------------------------------------------------- /.github/workflows/performance-check.yml: -------------------------------------------------------------------------------- 1 | on: 2 | pull_request: 3 | push: 4 | branches: 5 | - main 6 | - 'releases/*' 7 | jobs: 8 | performance-test: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout 12 | uses: actions/checkout@v3 13 | 14 | - name: Run performance tests 15 | uses: swissspidy/wp-performance-action@main 16 | with: 17 | plugins: | 18 | ./ 19 | urls: | 20 | / -------------------------------------------------------------------------------- /.github/workflows/plugin-check.yml: -------------------------------------------------------------------------------- 1 | name: 'plugin-check' 2 | on: # rebuild any PRs and main branch changes 3 | pull_request: 4 | push: 5 | branches: 6 | - main 7 | - 'releases/*' 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Checkout 14 | uses: actions/checkout@v3 15 | - name: Run plugin check 16 | uses: swissspidy/wp-plugin-check-action@main 17 | with: 18 | exclude-directories: 'env' -------------------------------------------------------------------------------- /.github/workflows/upateassets.yml: -------------------------------------------------------------------------------- 1 | name: Asset update 2 | on: 3 | push: 4 | branches: 5 | - main 6 | jobs: 7 | master: 8 | name: Push to main 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@main 12 | - name: WordPress.org asset update 13 | uses: 10up/action-wordpress-plugin-asset-update@stable 14 | env: 15 | SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }} 16 | SVN_USERNAME: ${{ secrets.SVN_USERNAME }} 17 | -------------------------------------------------------------------------------- /.wordpress-org/banner-1544x500.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adamsilverstein/modern-images-wp/03b9000f893b8cfefc093ec1c0dd334f98a349ae/.wordpress-org/banner-1544x500.png -------------------------------------------------------------------------------- /.wordpress-org/banner-772x250.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adamsilverstein/modern-images-wp/03b9000f893b8cfefc093ec1c0dd334f98a349ae/.wordpress-org/banner-772x250.png -------------------------------------------------------------------------------- /.wordpress-org/icon-128x128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adamsilverstein/modern-images-wp/03b9000f893b8cfefc093ec1c0dd334f98a349ae/.wordpress-org/icon-128x128.png -------------------------------------------------------------------------------- /.wordpress-org/icon-256x256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adamsilverstein/modern-images-wp/03b9000f893b8cfefc093ec1c0dd334f98a349ae/.wordpress-org/icon-256x256.png -------------------------------------------------------------------------------- /.wordpress-org/screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adamsilverstein/modern-images-wp/03b9000f893b8cfefc093ec1c0dd334f98a349ae/.wordpress-org/screenshot-1.png -------------------------------------------------------------------------------- /.wordpress-org/screenshot-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adamsilverstein/modern-images-wp/03b9000f893b8cfefc093ec1c0dd334f98a349ae/.wordpress-org/screenshot-2.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Modern Images for WordPress 2 | 3 | Specify the default image format used for sub-sized images generated by WordPress. 4 | 5 | ## Technical Notes 6 | 7 | * Requires PHP 5.6+. 8 | * Requires WordPress 5.8+. 9 | * [Issues](https://github.com/adamsilverstein/modern-images-wp/issues) and [Pull Requests](https://github.com/adamsilverstein/modern-images-wp/pulls) welcomed. 10 | 11 | ## Screenshots 12 | 13 | 1. Modern image output format options on `Settings` > `Media` admin page. 14 | 15 | ![image](.wordpress-org/screenshot-1.png) 16 | 17 | 18 | 2. Image format options available for various image types. 19 | 20 | ![image](.wordpress-org/screenshot-2.png) 21 | -------------------------------------------------------------------------------- /env/.nvmrc: -------------------------------------------------------------------------------- 1 | 16 2 | -------------------------------------------------------------------------------- /modern-images-wp.php: -------------------------------------------------------------------------------- 1 | Media. 26 | 27 | == Screenshots == 28 | 29 | 1. Modern image output format options on `Settings` > `Media` admin page. 30 | 2. Image format options available for various image types. 31 | 32 | == Changelog == 33 | 34 | = 1.2.0 = 35 | - Tested up to 6.4. 36 | 37 | = 1.1.0 = 38 | - Correct support for AVIF & JPEGXL with `mime_types` filter. 39 | - Add `wp-env` support. To test the plugin with `wp-env` which currently supports AVIF, use `npm install -g @wordpress/env && wp-env start`. 40 | 41 | = 1.0.4 = 42 | - Tested up to 6.1. 43 | 44 | = 1.0.3 = 45 | * Check correct image engine for mime type format support when multiple engines are available. 46 | 47 | = 1.0.0 = 48 | * Initial plugin release 49 | -------------------------------------------------------------------------------- /src/Plugin.php: -------------------------------------------------------------------------------- 1 | setting = new Setting(); 68 | } 69 | 70 | /** 71 | * Registers the plugin with WordPress. 72 | * 73 | * @since 1.0.0 74 | */ 75 | public function register() { 76 | $this->setting->register(); 77 | 78 | // Filter the default output format. 79 | $format_setting = $this->setting->get(); 80 | if ( ! empty( $format_setting ) ) { 81 | add_filter( 'image_editor_output_format', array( $this, 'filter_image_editor_output_format' ) ); 82 | } 83 | 84 | // Enable supported mime types. 85 | add_filter( 'mime_types', function( $types ) { 86 | $types['avif'] = 'image/avif'; 87 | $types['jpegxl'] = 'image/jxl'; 88 | return $types; 89 | } ); 90 | } 91 | 92 | /** 93 | * Filter the image editor output type. 94 | */ 95 | public function filter_image_editor_output_format( $formats ) { 96 | { 97 | $format_setting = $this->setting->get(); 98 | foreach( $format_setting as $setting => $value) { 99 | $source = 'image/' . str_replace( 'modern_image_output_format_for_', '', $setting ); 100 | $formats[ $source ] = $value; 101 | } 102 | return $formats; 103 | } 104 | } 105 | 106 | /** 107 | * Gets the plugin basename, which consists of the plugin directory name and main file name. 108 | * 109 | * @since 1.0.0 110 | * 111 | * @return string Plugin basename. 112 | */ 113 | public function basename() { 114 | return plugin_basename( $this->main_file ); 115 | } 116 | 117 | /** 118 | * Gets the absolute path for a path relative to the plugin directory. 119 | * 120 | * @since 1.0.0 121 | * 122 | * @param string $relative_path Optional. Relative path. Default '/'. 123 | * @return string Absolute path. 124 | */ 125 | public function path( $relative_path = '/' ) { 126 | return plugin_dir_path( $this->main_file ) . ltrim( $relative_path, '/' ); 127 | } 128 | 129 | /** 130 | * Gets the full URL for a path relative to the plugin directory. 131 | * 132 | * @since 1.0.0 133 | * 134 | * @param string $relative_path Optional. Relative path. Default '/'. 135 | * @return string Full URL. 136 | */ 137 | public function url( $relative_path = '/' ) { 138 | return plugin_dir_url( $this->main_file ) . ltrim( $relative_path, '/' ); 139 | } 140 | 141 | /** 142 | * Retrieves the main instance of the plugin. 143 | * 144 | * @since 1.0.0 145 | * 146 | * @return Plugin Plugin main instance. 147 | */ 148 | public static function instance() { 149 | return static::$instance; 150 | } 151 | 152 | /** 153 | * Loads the plugin main instance and initializes it. 154 | * 155 | * @since 1.0.0 156 | * 157 | * @param string $main_file Absolute path to the plugin main file. 158 | * @return bool True if the plugin main instance could be loaded, false otherwise. 159 | */ 160 | public static function load( $main_file ) { 161 | if ( null !== static::$instance ) { 162 | return false; 163 | } 164 | 165 | static::$instance = new static( $main_file ); 166 | static::$instance->register(); 167 | 168 | return true; 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /src/Setting.php: -------------------------------------------------------------------------------- 1 | 'WebP', 31 | 'image/avif' => 'AVIF', 32 | 'image/jpegxl' => 'JPEGXL', 33 | ); 34 | 35 | /** 36 | * Registers the setting with WordPress. 37 | * 38 | * @since 1.0.0 39 | */ 40 | public function register() { 41 | add_action( 42 | 'admin_init', 43 | function() { 44 | register_setting( 45 | 'media', 46 | self::OPTION_NAME 47 | ); 48 | add_settings_section( 49 | 'modernimageformats', 50 | __( 'Modern image output format', 'modern-images-wp' ), 51 | $this->get_sanitize_callback(), 52 | 'media' 53 | ); 54 | $option = $this->get(); 55 | $sub_settings = $this->get_sub_settings(); 56 | 57 | foreach ( $sub_settings as $sub_setting ) { 58 | if ( ! isset( $sub_setting['id'] ) ) { 59 | continue; 60 | } 61 | add_settings_field( 62 | $sub_setting['id'], 63 | $sub_setting['title'], 64 | function( $args ) use ( $sub_setting, $option ) { 65 | if ( isset( $option[ $sub_setting['id'] ] ) ) { 66 | $value = $option[ $sub_setting['id'] ]; 67 | } elseif ( ! empty( $sub_setting['multiple'] ) ) { 68 | $value = array(); 69 | } else { 70 | $value = ''; 71 | } 72 | 73 | // If there are choices, render a select. 74 | if ( isset( $sub_setting['choices'] ) ) { 75 | if ( ! empty( $sub_setting['multiple'] ) ) { 76 | ?> 77 |
id_attr( $sub_setting ); 80 | $this->class_attr( $sub_setting ); 81 | ?> 82 | > 83 | 84 | $option_label ) { 87 | // Support both associative (value => label) and indexed arrays (item is both value and label). 88 | if ( is_int( $option_value ) ) { 89 | $option_value = $option_label; 90 | } 91 | if ( ! $first ) { 92 | echo '
'; 93 | } 94 | $first = false; 95 | ?> 96 | 107 | 110 |
111 | 114 | 143 | 147 | id_attr( $sub_setting ); 152 | $this->name_attr( $sub_setting ); 153 | $this->class_attr( $sub_setting ); 154 | $this->placeholder_attr( $sub_setting ); 155 | $this->aria_describedby_attr( $sub_setting ); 156 | ?> 157 | /> 158 | 163 |

description_id_attr( $sub_setting ); 166 | ?> 167 | class="description" 168 | > 169 | 170 |

171 | self::OPTION_NAME . '-' . $sub_setting['id'] ) 177 | ); 178 | } 179 | } 180 | ); 181 | } 182 | 183 | /** 184 | * Check if an image type is available. 185 | * 186 | * @param $mime_type string The image mime type to check for support. 187 | */ 188 | private function is_image_format_supported( $mime_type ){ 189 | if ( "" === $mime_type ) { 190 | return true; 191 | } 192 | 193 | // Is the mime type among the choices? 194 | if ( ! isset( self::IMAGE_FORMATS[ $mime_type ] ) ){ 195 | return false; 196 | } 197 | 198 | // Format is the internal name for the image mime type. 199 | $format = self::IMAGE_FORMATS[ $mime_type ]; 200 | 201 | // Check the image editor WordPress uses for this mime type. 202 | $image_editor = _wp_image_editor_choose( array( 'mime_type' => $mime_type ) ); 203 | $image_info = array( 204 | 'gd_info' => extension_loaded( 'gd' ) ? gd_info() : array(), 205 | 'imagick_info' => extension_loaded( 'imagick' ) ? \Imagick::queryFormats() : array(), 206 | ); 207 | 208 | if ( $image_editor === 'WP_Image_Editor_Imagick' && ! empty( $image_info['imagick_info'] ) ) { 209 | return in_array( strtoupper( $format ), $image_info['imagick_info'] ); 210 | } 211 | 212 | // Fall back to GD as the default image editor. 213 | if ( ! empty( $image_info['gd_info'] ) ) { 214 | $supports = sprintf( '%s Support', $format ); 215 | return isset( $image_info['gd_info'][ $supports ] ) && $image_info['gd_info'][ $supports ]; 216 | } 217 | 218 | return false; 219 | } 220 | 221 | /** 222 | * Gets the features list from the option. 223 | * 224 | * @since 1.0.0 225 | * 226 | * @return array Associative array of $policy_name => $policy_origins pairs. 227 | */ 228 | public function get() { 229 | return array_filter( (array) get_option( self::OPTION_NAME ) ); 230 | } 231 | 232 | /** 233 | * Gets sub settings that the setting should contain. 234 | * 235 | * @since 1.0.0 236 | * @since 1.2.0 Added settings for placing existing tags. 237 | * @since 1.3.0 Added settings for providing mock response data for AdSense and Analytics API calls. 238 | * @since 1.4.0 Added settings for controlling feature flags. 239 | * 240 | * @return array List of associative setting definition arrays. 241 | */ 242 | public function get_sub_settings() { 243 | $mime_types = get_allowed_mime_types(); 244 | 245 | $allowed_image_mime_types = array_filter( $mime_types, function( $type ) { 246 | return 0 === strpos( $type, 'image' ); 247 | } ); 248 | 249 | $potential_types = array( 250 | "image/jpeg", 251 | "image/gif", 252 | "image/png", 253 | "image/bmp", 254 | "image/tiff", 255 | "image/webp", 256 | "image/avif", 257 | "image/jpegxl", 258 | ); 259 | 260 | $image_options = array_intersect( $potential_types, $allowed_image_mime_types ); 261 | 262 | $options = array_map( function( $type ) { 263 | $format = str_replace( 'image/', '', $type ); 264 | $choices =array( 265 | '' => __( 'Use original format (default)', 'modern-images-wp' ), 266 | 'image/webp' => __( 'WebP', 'modern-images-wp' ), 267 | 'image/avif' => __( 'AVIF', 'modern-images-wp' ), 268 | 'image/jpegxl' => __( 'JPEG XL', 'modern-images-wp' ), 269 | 270 | ); 271 | return array( 272 | /* translators: %s is the image format, e.g. jpeg. */ 273 | 'id' => sprintf( 'modern_image_output_format_for_%s', $format ), 274 | /* translators: %s is the image format, e.g. Jpeg. */ 275 | 'title' => sprintf( __( 'For %s images', 'modern-images-wp' ), lcfirst( $format ) ), 276 | /* translators: %s is the image format, e.g. Jpeg. */ 277 | 'description' => sprintf( __( 'Uploaded %s images will be output in this format.', 'modern-images-wp' ), lcfirst( $format ) ), 278 | 'section' => 'modernimageformats', 279 | 'choices' => $choices, 280 | ); 281 | }, $image_options ); 282 | 283 | return $options; 284 | } 285 | 286 | /** 287 | * Gets the sanitize callback for the setting. 288 | * 289 | * @since 1.0.0 290 | * 291 | * @return callable Sanitize callback. 292 | */ 293 | public function get_sanitize_callback() { 294 | return function( $value ) { 295 | 296 | // Echo out some image support information. 297 | $image_info = array( 298 | 'gd_info' => extension_loaded( 'gd' ) ? gd_info() : array(), 299 | 'imagick_info' => extension_loaded( 'imagick' ) ? \Imagick::queryFormats() : array(), 300 | ); 301 | 302 | if ( ! empty ( $image_info['gd_info'] ) ) { 303 | $gd_supports_webp = isset( $image_info['gd_info']['WebP Support'] ) && $image_info['gd_info']['WebP Support']; 304 | $gd_supports_avif = isset( $image_info['gd_info']['AVIF Support'] ) && $image_info['gd_info']['AVIF Support']; 305 | $gd_supports_jpegxl = isset( $image_info['gd_info']['JPEGXL Support'] ) && $image_info['gd_info']['JPEGXL Support']; 306 | echo sprintf( 307 | '%1$s%2$s%3$s: %4$s%5$s%6$s%7$s', 308 | '', 309 | esc_html( __( 'LibGD supported formats', 'modern-images-wp' ) ), 310 | '', 311 | $gd_supports_webp ? " WebP" : "", 312 | $gd_supports_avif ? " AVIF" : "", 313 | $gd_supports_jpegxl ? " JPEGXL" : "", 314 | '
' 315 | ); 316 | } 317 | if ( ! empty( $image_info['imagick_info'] ) ) { 318 | $imagick_supports_webp = in_array( 'WEBP', $image_info['imagick_info'] ); 319 | $imagick_supports_avif = in_array( 'AVIF', $image_info['imagick_info'] ); 320 | $imagick_supports_jpegxl = in_array( 'JPEGXL', $image_info['imagick_info'] ); 321 | echo sprintf( 322 | '%1$s%2$s%3$s: %4$s%5$s%6$s%7$s', 323 | '', 324 | esc_html( __( 'Imagick supported formats', 'modern-images-wp' ) ), 325 | '', 326 | $imagick_supports_webp ? " WebP" : "", 327 | $imagick_supports_avif ? " AVIF" : "", 328 | $imagick_supports_jpegxl ? " JPEGXL" : "", 329 | '
' 330 | ); 331 | } 332 | $sub_settings = $this->get_sub_settings(); 333 | 334 | if ( ! is_array( $value ) ) { 335 | $value = array(); 336 | } 337 | 338 | foreach ( $sub_settings as $sub_setting ) { 339 | if ( ! isset( $sub_setting['id'] ) ){ 340 | continue; 341 | } 342 | if ( ! empty( $sub_setting['multiple'] ) ) { 343 | if ( ! isset( $value[ $sub_setting['id'] ] ) || ! is_array( $value[ $sub_setting['id'] ] ) ) { 344 | $value[ $sub_setting['id'] ] = array(); 345 | continue; 346 | } 347 | foreach ( $value[ $sub_setting['id'] ] as $index => $option ) { 348 | $value[ $sub_setting['id'] ][ $index ] = sanitize_text_field( $option ); 349 | } 350 | continue; 351 | } 352 | 353 | if ( ! isset( $value[ $sub_setting['id'] ] ) ) { 354 | $value[ $sub_setting['id'] ] = ''; 355 | continue; 356 | } 357 | 358 | $value[ $sub_setting['id'] ] = sanitize_text_field( $value[ $sub_setting['id'] ] ); 359 | } 360 | 361 | return $value; 362 | }; 363 | } 364 | 365 | /** 366 | * Gets the default value for the setting. 367 | * 368 | * @since 1.0.0 369 | * 370 | * @return array Default value. 371 | */ 372 | protected function get_default() { 373 | $sub_settings = $this->get_sub_settings(); 374 | 375 | $value = array(); 376 | foreach ( $sub_settings as $sub_setting ) { 377 | if ( ! empty( $sub_setting['multiple'] ) ) { 378 | $value[ $sub_setting['id'] ] = array(); 379 | continue; 380 | } 381 | $value[ $sub_setting['id'] ] = ''; 382 | } 383 | 384 | return $value; 385 | } 386 | 387 | 388 | /** 389 | * Prints the 'id' attribute for a specific sub-setting. 390 | * 391 | * @since 1.4.0 392 | * 393 | * @param array $sub_setting Associative array of sub setting definition data. 394 | */ 395 | protected function id_attr( $sub_setting ) { 396 | echo ' id="' . esc_attr( Setting::OPTION_NAME . '-' . $sub_setting['id'] ) . '"'; 397 | } 398 | 399 | /** 400 | * Prints the 'name' attribute for a specific sub-setting. 401 | * 402 | * @since 1.4.0 403 | * 404 | * @param array $sub_setting Associative array of sub setting definition data. 405 | */ 406 | protected function name_attr( $sub_setting ) { 407 | echo ' name="' . esc_attr( Setting::OPTION_NAME . '[' . $sub_setting['id'] . ']' . ( ! empty( $sub_setting['multiple'] ) ? '[]' : '' ) ) . '"'; 408 | } 409 | 410 | /** 411 | * Prints the 'class' attribute for a specific sub-setting, if relevant. 412 | * 413 | * @since 1.4.0 414 | * 415 | * @param array $sub_setting Associative array of sub setting definition data. 416 | */ 417 | protected function class_attr( $sub_setting ) { 418 | if ( empty( $sub_setting['class'] ) ) { 419 | return; 420 | } 421 | 422 | echo ' class="' . esc_attr( $sub_setting['class'] ) . '"'; 423 | } 424 | 425 | /** 426 | * Prints the 'placeholder' attribute for a specific sub-setting, if relevant. 427 | * 428 | * @since 1.4.0 429 | * 430 | * @param array $sub_setting Associative array of sub setting definition data. 431 | */ 432 | protected function placeholder_attr( $sub_setting ) { 433 | if ( empty( $sub_setting['placeholder'] ) ) { 434 | return; 435 | } 436 | 437 | echo ' placeholder="' . esc_attr( $sub_setting['placeholder'] ) . '"'; 438 | } 439 | 440 | /** 441 | * Prints the 'aria-describedby' attribute for a specific sub-setting, if relevant. 442 | * 443 | * @since 1.4.0 444 | * 445 | * @param array $sub_setting Associative array of sub setting definition data. 446 | */ 447 | protected function aria_describedby_attr( $sub_setting ) { 448 | if ( empty( $sub_setting['description'] ) ) { 449 | return; 450 | } 451 | 452 | echo ' aria-describedby="' . esc_attr( Setting::OPTION_NAME . '-' . $sub_setting['id'] . '-description' ) . '"'; 453 | } 454 | 455 | /** 456 | * Prints the 'id' attribute for a specific sub-setting's description, if relevant. 457 | * 458 | * @since 1.4.0 459 | * 460 | * @param array $sub_setting Associative array of sub setting definition data. 461 | */ 462 | protected function description_id_attr( $sub_setting ) { 463 | if ( empty( $sub_setting['description'] ) ) { 464 | return; 465 | } 466 | 467 | echo ' id="' . esc_attr( Setting::OPTION_NAME . '-' . $sub_setting['id'] . '-description' ) . '"'; 468 | } 469 | 470 | /** 471 | * Prints the 'selected' attribute, if relevant. 472 | * 473 | * Expands on the similar WordPress function in that it supports array checks, relevant for multiple-choice UI. 474 | * 475 | * @param mixed $value The value stored in the database, either a single value or an array of values. 476 | * @param mixed $current The current value that the option element is rendered for, if not just true. 477 | */ 478 | protected function selected_attr( $value, $current = true ) { 479 | $enabled = is_array( $value ) && in_array( $current, $value, true ) || ! is_array( $value ) && (string) $value === (string) $current; 480 | selected( $enabled ); 481 | } 482 | } 483 | --------------------------------------------------------------------------------