├── .distignore ├── .editorconfig ├── .eslintignore ├── .eslintrc.js ├── .github └── workflows │ ├── run-test-and-deploy.yml │ └── run-test.yml ├── .gitignore ├── .husky └── pre-commit ├── .markdownlint.json ├── .npmrc ├── .nvmrc ├── .prettierrc.js ├── .stylelintignore ├── .stylelintrc.js ├── .wordpress-org ├── banner-1544x500.png ├── banner-772x250.png ├── blueprints │ └── blueprint.json ├── icon-128x128.png ├── icon-256x256.png ├── screenshot-1.png └── screenshot-2.gif ├── .wp-env.json ├── LICENSE ├── README.md ├── composer.json ├── composer.lock ├── enable-responsive-image.php ├── package-lock.json ├── package.json ├── phpcs.ruleset.xml ├── playwright.config.ts ├── readme.txt ├── src ├── block-edit-preview.tsx ├── constants.ts ├── editor.scss ├── index.tsx ├── source-editor.tsx ├── source-list.tsx ├── store.ts └── types.ts ├── test └── e2e │ ├── assets │ ├── 1000x750.png │ ├── 400x300.png │ └── 600x450.png │ └── test.spec.ts └── tsconfig.json /.distignore: -------------------------------------------------------------------------------- 1 | /.git 2 | /.github 3 | /.husky 4 | /.wordpress-org 5 | /artifacts 6 | /node_modules 7 | /test 8 | /vendor 9 | .distignore 10 | .editorconfig 11 | .eslintignore 12 | .eslintrc.js 13 | .markdownlint.json 14 | .gitignore 15 | .npmrc 16 | .nvmrc 17 | .prettierrc.js 18 | .stylelintignore 19 | .stylelintrc.js 20 | .wp-env.json 21 | composer.json 22 | composer.lock 23 | playwright.config.ts 24 | package-lock.json 25 | package.json 26 | phpcs.ruleset.xml 27 | README.md 28 | tsconfig.json 29 | *.tsbuildinfo 30 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | trim_trailing_whitespace = true 8 | indent_style = tab 9 | indent_size = 2 10 | 11 | [*.{yml,yaml}] 12 | indent_style = space 13 | indent_size = 2 14 | 15 | [*.md] 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | vendor/ 3 | build/ 4 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [ 3 | 'plugin:@wordpress/eslint-plugin/recommended', 4 | 'plugin:@typescript-eslint/eslint-recommended', 5 | ], 6 | parser: '@typescript-eslint/parser', 7 | rules: { 8 | 'react/jsx-boolean-value': 'error', 9 | 'react/jsx-curly-brace-presence': [ 'error', { props: 'never', children: 'never' } ], 10 | 'import/no-extraneous-dependencies': 'off', 11 | 'import/no-unresolved': 'off', 12 | '@wordpress/no-unsafe-wp-apis': 'off', 13 | '@wordpress/dependency-group': 'error', 14 | '@wordpress/i18n-text-domain': [ 15 | 'error', 16 | { 17 | allowedTextDomain: 'enable-responsive-image', 18 | }, 19 | ], 20 | 'no-nested-ternary': 'off', 21 | 'prettier/prettier': [ 22 | 'error', 23 | { 24 | useTabs: true, 25 | tabWidth: 2, 26 | singleQuote: true, 27 | printWidth: 100, 28 | bracketSpacing: true, 29 | parenSpacing: true, 30 | bracketSameLine: false, 31 | }, 32 | ], 33 | }, 34 | }; 35 | -------------------------------------------------------------------------------- /.github/workflows/run-test-and-deploy.yml: -------------------------------------------------------------------------------- 1 | name: Test and Deploy 2 | 3 | on: 4 | push: 5 | tags: 6 | - '*' 7 | 8 | jobs: 9 | test: 10 | runs-on: ubuntu-latest 11 | strategy: 12 | matrix: 13 | include: 14 | - php: '7.4' 15 | wp: WordPress 16 | - php: '7.4' 17 | wp: WordPress#6.8.1 18 | - php: '7.4' 19 | wp: WordPress#6.7.2 20 | - php: '7.4' 21 | wp: WordPress#6.6.2 22 | - php: '8.0' 23 | wp: WordPress 24 | - php: '8.0' 25 | wp: WordPress#6.8.1 26 | - php: '8.0' 27 | wp: WordPress#6.7.2 28 | - php: '8.0' 29 | wp: WordPress#6.6.2 30 | - php: '8.2' 31 | wp: WordPress 32 | - php: '8.2' 33 | wp: WordPress#6.8.1 34 | - php: '8.2' 35 | wp: WordPress#6.7.2 36 | - php: '8.2' 37 | wp: WordPress#6.6.2 38 | - php: '8.4' 39 | wp: WordPress 40 | - php: '8.4' 41 | wp: WordPress#6.8.1 42 | - php: '8.4' 43 | wp: WordPress#6.7.2 44 | name: PHP ${{ matrix.php }} / ${{ matrix.wp }} Test 45 | 46 | steps: 47 | - name: Checkout 48 | uses: actions/checkout@v4 49 | 50 | - name: Use desired version of Node.js 51 | uses: actions/setup-node@v4 52 | with: 53 | node-version-file: '.nvmrc' 54 | 55 | - name: Npm install and build 56 | run: | 57 | npm ci 58 | npm run build 59 | 60 | - name: Composer install and set phpcs 61 | run: | 62 | composer install 63 | composer phpcs 64 | 65 | - name: Running lint check 66 | run: npm run lint 67 | 68 | - name: Install Playwright dependencies 69 | run: npx playwright install chromium firefox webkit --with-deps 70 | 71 | - name: Install WordPress 72 | run: | 73 | WP_ENV_CORE=WordPress/${{ matrix.wp }} WP_ENV_PHP_VERSION=${{ matrix.php }} npm run wp-env start 74 | npm run wp-env run cli wp core version 75 | npm run wp-env run cli wp cli info 76 | 77 | - name: Running e2e tests 78 | run: npm run test:e2e 79 | 80 | deploy: 81 | name: Deploy to WP.org 82 | runs-on: ubuntu-latest 83 | needs: [test] 84 | 85 | steps: 86 | - name: Checkout 87 | uses: actions/checkout@v4 88 | 89 | - name: Use desired version of Node.js 90 | uses: actions/setup-node@v4 91 | with: 92 | node-version-file: '.nvmrc' 93 | 94 | - name: Npm install and build 95 | run: | 96 | npm ci 97 | npm run build 98 | 99 | - name: WordPress Plugin Deploy 100 | id: deploy 101 | uses: 10up/action-wordpress-plugin-deploy@stable 102 | with: 103 | generate-zip: true 104 | env: 105 | SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }} 106 | SVN_USERNAME: ${{ secrets.SVN_USERNAME }} 107 | SLUG: enable-responsive-image 108 | 109 | - name: Create Release 110 | id: create_release 111 | uses: actions/create-release@v1 112 | env: 113 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 114 | with: 115 | tag_name: ${{ github.ref }} 116 | release_name: Release ${{ github.ref }} 117 | draft: false 118 | prerelease: false 119 | commitish: main 120 | 121 | - name: Upload release asset 122 | uses: actions/upload-release-asset@v1 123 | env: 124 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 125 | with: 126 | upload_url: ${{ steps.create_release.outputs.upload_url }} 127 | asset_path: ${{ steps.deploy.outputs.zip-path }} 128 | asset_name: ${{ github.event.repository.name }}.zip 129 | asset_content_type: application/zip 130 | -------------------------------------------------------------------------------- /.github/workflows/run-test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - main 8 | 9 | # Cancels all previous workflow runs for pull requests that have not completed. 10 | concurrency: 11 | # The concurrency group contains the workflow name and the branch name for pull requests 12 | # or the commit hash for any other events. 13 | group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }} 14 | cancel-in-progress: true 15 | 16 | jobs: 17 | test: 18 | runs-on: ubuntu-latest 19 | strategy: 20 | matrix: 21 | include: 22 | - php: '7.4' 23 | wp: WordPress 24 | - php: '7.4' 25 | wp: WordPress#6.8.1 26 | - php: '7.4' 27 | wp: WordPress#6.7.2 28 | - php: '7.4' 29 | wp: WordPress#6.6.2 30 | - php: '8.0' 31 | wp: WordPress 32 | - php: '8.0' 33 | wp: WordPress#6.8.1 34 | - php: '8.0' 35 | wp: WordPress#6.7.2 36 | - php: '8.0' 37 | wp: WordPress#6.6.2 38 | - php: '8.2' 39 | wp: WordPress 40 | - php: '8.2' 41 | wp: WordPress#6.8.1 42 | - php: '8.2' 43 | wp: WordPress#6.7.2 44 | - php: '8.2' 45 | wp: WordPress#6.6.2 46 | - php: '8.4' 47 | wp: WordPress 48 | - php: '8.4' 49 | wp: WordPress#6.8.1 50 | - php: '8.4' 51 | wp: WordPress#6.7.2 52 | name: PHP ${{ matrix.php }} / ${{ matrix.wp }} Test 53 | 54 | steps: 55 | - name: Checkout 56 | uses: actions/checkout@v4 57 | 58 | - name: Use desired version of Node.js 59 | uses: actions/setup-node@v4 60 | with: 61 | node-version-file: '.nvmrc' 62 | 63 | - name: Npm install and build 64 | run: | 65 | npm ci 66 | npm run build 67 | 68 | - name: Composer install and set phpcs 69 | run: | 70 | composer install 71 | composer phpcs 72 | 73 | - name: Running lint check 74 | run: npm run lint 75 | 76 | - name: Install Playwright dependencies 77 | run: npx playwright install chromium firefox webkit --with-deps 78 | 79 | - name: Install WordPress 80 | run: | 81 | WP_ENV_CORE=WordPress/${{ matrix.wp }} WP_ENV_PHP_VERSION=${{ matrix.php }} npm run wp-env start 82 | npm run wp-env run cli wp core version 83 | npm run wp-env run cli wp cli info 84 | 85 | - name: Running e2e tests 86 | run: npm run test:e2e 87 | 88 | - name: Archive debug artifacts 89 | uses: actions/upload-artifact@v4 90 | if: always() 91 | with: 92 | name: failures-artifacts-${{ matrix.php }}-${{ matrix.wp }} 93 | path: artifacts 94 | if-no-files-found: ignore 95 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | artifacts/ 3 | build/ 4 | node_modules/ 5 | vendor/ 6 | .wp-env.override.json 7 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | lint-staged 2 | -------------------------------------------------------------------------------- /.markdownlint.json: -------------------------------------------------------------------------------- 1 | { 2 | "default": true, 3 | "MD013": { "line_length": 9999 }, 4 | "MD024": false, 5 | "no-hard-tabs": false 6 | } 7 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | save-exact = true 2 | engine-strict = true 3 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 22 2 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | const config = { 2 | ...require( '@wordpress/prettier-config' ), 3 | semi: true, 4 | useTabs: true, 5 | tabWidth: 2, 6 | singleQuote: true, 7 | printWidth: 100, 8 | bracketSpacing: true, 9 | parenSpacing: true, 10 | bracketSameLine: false, 11 | }; 12 | 13 | module.exports = config; 14 | -------------------------------------------------------------------------------- /.stylelintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | build/ 3 | vendor/ 4 | *.js 5 | *.xml 6 | *.md 7 | -------------------------------------------------------------------------------- /.stylelintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [ '@wordpress/stylelint-config/scss-stylistic' ], 3 | rules: { 4 | 'no-descending-specificity': null, 5 | 'font-weight-notation': null, 6 | 'selector-class-pattern': null, 7 | }, 8 | }; 9 | -------------------------------------------------------------------------------- /.wordpress-org/banner-1544x500.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t-hamano/enable-responsive-image/4ed3dbf68c5543d0c40f9eed9d6cbd1e9ba1e998/.wordpress-org/banner-1544x500.png -------------------------------------------------------------------------------- /.wordpress-org/banner-772x250.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t-hamano/enable-responsive-image/4ed3dbf68c5543d0c40f9eed9d6cbd1e9ba1e998/.wordpress-org/banner-772x250.png -------------------------------------------------------------------------------- /.wordpress-org/blueprints/blueprint.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://playground.wordpress.net/blueprint-schema.json", 3 | "landingPage": "/wp-admin/post.php?post=5&action=edit", 4 | "steps": [ 5 | { 6 | "step": "login", 7 | "username": "admin" 8 | }, 9 | { 10 | "step": "installPlugin", 11 | "pluginData": { 12 | "resource": "wordpress.org/plugins", 13 | "slug": "enable-responsive-image" 14 | } 15 | }, 16 | { 17 | "step": "runPHP", 18 | "code": " 5,\n'post_title' => 'Enable Responsive Image',\n'post_content' => '\n

Select the Image block, open the block sidebar, and check the \"Image sources\" section.

\n\n\n
\"\"/
\n',\n'post_status' => 'publish',\n'post_author' => 1\n));" 19 | } 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /.wordpress-org/icon-128x128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t-hamano/enable-responsive-image/4ed3dbf68c5543d0c40f9eed9d6cbd1e9ba1e998/.wordpress-org/icon-128x128.png -------------------------------------------------------------------------------- /.wordpress-org/icon-256x256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t-hamano/enable-responsive-image/4ed3dbf68c5543d0c40f9eed9d6cbd1e9ba1e998/.wordpress-org/icon-256x256.png -------------------------------------------------------------------------------- /.wordpress-org/screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t-hamano/enable-responsive-image/4ed3dbf68c5543d0c40f9eed9d6cbd1e9ba1e998/.wordpress-org/screenshot-1.png -------------------------------------------------------------------------------- /.wordpress-org/screenshot-2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t-hamano/enable-responsive-image/4ed3dbf68c5543d0c40f9eed9d6cbd1e9ba1e998/.wordpress-org/screenshot-2.gif -------------------------------------------------------------------------------- /.wp-env.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | ".", 4 | "https://downloads.wordpress.org/plugin/wordpress-beta-tester.zip", 5 | "https://downloads.wordpress.org/plugin/wp-downgrade.zip" 6 | ], 7 | "env": { 8 | "development": { 9 | "phpmyadminPort": 9000 10 | }, 11 | "tests": { 12 | "phpmyadminPort": 9001, 13 | "config": { 14 | "WP_DEBUG": true, 15 | "SCRIPT_DEBUG": true 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Enable Responsive Image 2 | 3 | [![Test](https://github.com/t-hamano/enable-responsive-image/actions/workflows/run-test.yml/badge.svg)](https://github.com/t-hamano/enable-responsive-image/actions/workflows/run-test.yml) 4 | 5 | [![Test and Deploy](https://github.com/t-hamano/enable-responsive-image/actions/workflows/run-test-and-deploy.yml/badge.svg)](https://github.com/t-hamano/enable-responsive-image/actions/workflows/run-test-and-deploy.yml) 6 | 7 | WordPress plugin that adds settings to the Image block to display different images depending on the width of the screen. 8 | 9 | ## Screenshot 10 | 11 | ![Settings added to the block sidebar of the image block](https://raw.githubusercontent.com/t-hamano/enable-responsive-image/main/.wordpress-org/screenshot-1.png) 12 | 13 | ![How the image changes depending on the screen width](https://raw.githubusercontent.com/t-hamano/enable-responsive-image/main/.wordpress-org/screenshot-2.gif) 14 | 15 | ## How to build 16 | 17 | ```sh 18 | npm install 19 | npm run build 20 | ``` 21 | 22 | ## Filters 23 | 24 | ### `enable_responsive_image_default_media_value( $media_value )` 25 | 26 | Filters the default media value (px). 27 | 28 | #### Parameters 29 | 30 | - `$media_value` 31 | 32 | *(int)* The media value (px). Default is 600. 33 | 34 | #### Example 35 | 36 | ```php 37 | function custom_enable_responsive_image_default_media_value( $default_media_value ) { 38 | // Override default media value. 39 | return 400; 40 | } 41 | add_filter( 'enable_responsive_image_default_media_value', 'custom_enable_responsive_image_default_media_value' ); 42 | ``` 43 | 44 | ### `enable_responsive_image_show_preview_button( $show_preview_button )` 45 | 46 | Filters whether the preview button is displayed on the block toolbar. 47 | 48 | #### Parameters 49 | 50 | - `$show_preview_button` 51 | 52 | *(boolean)* Whether the preview button is displayed on the block toolbar. Default is `true`. 53 | 54 | #### Example 55 | 56 | ```php 57 | // Disable the preview button. 58 | add_filter( 'enable_responsive_image_show_preview_button', '__return_false' ); 59 | ``` 60 | 61 | ## Resources 62 | 63 | ### Image for screenshot 64 | 65 | - License: Public Domain 66 | - Source: 67 | 68 | ## Author 69 | 70 | [Aki Hamano (Github)](https://github.com/t-hamano) 71 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "t-hamano/enable-responsive-image", 3 | "description": "Add settings to the image block that allow you to register images for mobile devices.", 4 | "license": "GPL-2.0-or-later", 5 | "authors": [ 6 | { 7 | "name": "Aki Hamano" 8 | } 9 | ], 10 | "require-dev": { 11 | "squizlabs/php_codesniffer": "*", 12 | "wp-coding-standards/wpcs": "^3.1" 13 | }, 14 | "scripts": { 15 | "phpcs": "phpcs --config-set installed_paths vendor/wp-coding-standards/wpcs,vendor/phpcsstandards/phpcsextra,vendor/phpcsstandards/phpcsutils", 16 | "lint": "phpcs ./ --standard=./phpcs.ruleset.xml" 17 | }, 18 | "config": { 19 | "allow-plugins": { 20 | "dealerdirect/phpcodesniffer-composer-installer": true 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "66d5b9ee1305eb4e1829cded05c8929f", 8 | "packages": [], 9 | "packages-dev": [ 10 | { 11 | "name": "dealerdirect/phpcodesniffer-composer-installer", 12 | "version": "v1.0.0", 13 | "source": { 14 | "type": "git", 15 | "url": "https://github.com/PHPCSStandards/composer-installer.git", 16 | "reference": "4be43904336affa5c2f70744a348312336afd0da" 17 | }, 18 | "dist": { 19 | "type": "zip", 20 | "url": "https://api.github.com/repos/PHPCSStandards/composer-installer/zipball/4be43904336affa5c2f70744a348312336afd0da", 21 | "reference": "4be43904336affa5c2f70744a348312336afd0da", 22 | "shasum": "" 23 | }, 24 | "require": { 25 | "composer-plugin-api": "^1.0 || ^2.0", 26 | "php": ">=5.4", 27 | "squizlabs/php_codesniffer": "^2.0 || ^3.1.0 || ^4.0" 28 | }, 29 | "require-dev": { 30 | "composer/composer": "*", 31 | "ext-json": "*", 32 | "ext-zip": "*", 33 | "php-parallel-lint/php-parallel-lint": "^1.3.1", 34 | "phpcompatibility/php-compatibility": "^9.0", 35 | "yoast/phpunit-polyfills": "^1.0" 36 | }, 37 | "type": "composer-plugin", 38 | "extra": { 39 | "class": "PHPCSStandards\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin" 40 | }, 41 | "autoload": { 42 | "psr-4": { 43 | "PHPCSStandards\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/" 44 | } 45 | }, 46 | "notification-url": "https://packagist.org/downloads/", 47 | "license": [ 48 | "MIT" 49 | ], 50 | "authors": [ 51 | { 52 | "name": "Franck Nijhof", 53 | "email": "franck.nijhof@dealerdirect.com", 54 | "homepage": "http://www.frenck.nl", 55 | "role": "Developer / IT Manager" 56 | }, 57 | { 58 | "name": "Contributors", 59 | "homepage": "https://github.com/PHPCSStandards/composer-installer/graphs/contributors" 60 | } 61 | ], 62 | "description": "PHP_CodeSniffer Standards Composer Installer Plugin", 63 | "homepage": "http://www.dealerdirect.com", 64 | "keywords": [ 65 | "PHPCodeSniffer", 66 | "PHP_CodeSniffer", 67 | "code quality", 68 | "codesniffer", 69 | "composer", 70 | "installer", 71 | "phpcbf", 72 | "phpcs", 73 | "plugin", 74 | "qa", 75 | "quality", 76 | "standard", 77 | "standards", 78 | "style guide", 79 | "stylecheck", 80 | "tests" 81 | ], 82 | "support": { 83 | "issues": "https://github.com/PHPCSStandards/composer-installer/issues", 84 | "source": "https://github.com/PHPCSStandards/composer-installer" 85 | }, 86 | "time": "2023-01-05T11:28:13+00:00" 87 | }, 88 | { 89 | "name": "phpcsstandards/phpcsextra", 90 | "version": "1.2.1", 91 | "source": { 92 | "type": "git", 93 | "url": "https://github.com/PHPCSStandards/PHPCSExtra.git", 94 | "reference": "11d387c6642b6e4acaf0bd9bf5203b8cca1ec489" 95 | }, 96 | "dist": { 97 | "type": "zip", 98 | "url": "https://api.github.com/repos/PHPCSStandards/PHPCSExtra/zipball/11d387c6642b6e4acaf0bd9bf5203b8cca1ec489", 99 | "reference": "11d387c6642b6e4acaf0bd9bf5203b8cca1ec489", 100 | "shasum": "" 101 | }, 102 | "require": { 103 | "php": ">=5.4", 104 | "phpcsstandards/phpcsutils": "^1.0.9", 105 | "squizlabs/php_codesniffer": "^3.8.0" 106 | }, 107 | "require-dev": { 108 | "php-parallel-lint/php-console-highlighter": "^1.0", 109 | "php-parallel-lint/php-parallel-lint": "^1.3.2", 110 | "phpcsstandards/phpcsdevcs": "^1.1.6", 111 | "phpcsstandards/phpcsdevtools": "^1.2.1", 112 | "phpunit/phpunit": "^4.5 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0" 113 | }, 114 | "type": "phpcodesniffer-standard", 115 | "extra": { 116 | "branch-alias": { 117 | "dev-stable": "1.x-dev", 118 | "dev-develop": "1.x-dev" 119 | } 120 | }, 121 | "notification-url": "https://packagist.org/downloads/", 122 | "license": [ 123 | "LGPL-3.0-or-later" 124 | ], 125 | "authors": [ 126 | { 127 | "name": "Juliette Reinders Folmer", 128 | "homepage": "https://github.com/jrfnl", 129 | "role": "lead" 130 | }, 131 | { 132 | "name": "Contributors", 133 | "homepage": "https://github.com/PHPCSStandards/PHPCSExtra/graphs/contributors" 134 | } 135 | ], 136 | "description": "A collection of sniffs and standards for use with PHP_CodeSniffer.", 137 | "keywords": [ 138 | "PHP_CodeSniffer", 139 | "phpcbf", 140 | "phpcodesniffer-standard", 141 | "phpcs", 142 | "standards", 143 | "static analysis" 144 | ], 145 | "support": { 146 | "issues": "https://github.com/PHPCSStandards/PHPCSExtra/issues", 147 | "security": "https://github.com/PHPCSStandards/PHPCSExtra/security/policy", 148 | "source": "https://github.com/PHPCSStandards/PHPCSExtra" 149 | }, 150 | "funding": [ 151 | { 152 | "url": "https://github.com/PHPCSStandards", 153 | "type": "github" 154 | }, 155 | { 156 | "url": "https://github.com/jrfnl", 157 | "type": "github" 158 | }, 159 | { 160 | "url": "https://opencollective.com/php_codesniffer", 161 | "type": "open_collective" 162 | } 163 | ], 164 | "time": "2023-12-08T16:49:07+00:00" 165 | }, 166 | { 167 | "name": "phpcsstandards/phpcsutils", 168 | "version": "1.0.12", 169 | "source": { 170 | "type": "git", 171 | "url": "https://github.com/PHPCSStandards/PHPCSUtils.git", 172 | "reference": "87b233b00daf83fb70f40c9a28692be017ea7c6c" 173 | }, 174 | "dist": { 175 | "type": "zip", 176 | "url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/87b233b00daf83fb70f40c9a28692be017ea7c6c", 177 | "reference": "87b233b00daf83fb70f40c9a28692be017ea7c6c", 178 | "shasum": "" 179 | }, 180 | "require": { 181 | "dealerdirect/phpcodesniffer-composer-installer": "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7 || ^1.0", 182 | "php": ">=5.4", 183 | "squizlabs/php_codesniffer": "^3.10.0 || 4.0.x-dev@dev" 184 | }, 185 | "require-dev": { 186 | "ext-filter": "*", 187 | "php-parallel-lint/php-console-highlighter": "^1.0", 188 | "php-parallel-lint/php-parallel-lint": "^1.3.2", 189 | "phpcsstandards/phpcsdevcs": "^1.1.6", 190 | "yoast/phpunit-polyfills": "^1.1.0 || ^2.0.0" 191 | }, 192 | "type": "phpcodesniffer-standard", 193 | "extra": { 194 | "branch-alias": { 195 | "dev-stable": "1.x-dev", 196 | "dev-develop": "1.x-dev" 197 | } 198 | }, 199 | "autoload": { 200 | "classmap": [ 201 | "PHPCSUtils/" 202 | ] 203 | }, 204 | "notification-url": "https://packagist.org/downloads/", 205 | "license": [ 206 | "LGPL-3.0-or-later" 207 | ], 208 | "authors": [ 209 | { 210 | "name": "Juliette Reinders Folmer", 211 | "homepage": "https://github.com/jrfnl", 212 | "role": "lead" 213 | }, 214 | { 215 | "name": "Contributors", 216 | "homepage": "https://github.com/PHPCSStandards/PHPCSUtils/graphs/contributors" 217 | } 218 | ], 219 | "description": "A suite of utility functions for use with PHP_CodeSniffer", 220 | "homepage": "https://phpcsutils.com/", 221 | "keywords": [ 222 | "PHP_CodeSniffer", 223 | "phpcbf", 224 | "phpcodesniffer-standard", 225 | "phpcs", 226 | "phpcs3", 227 | "standards", 228 | "static analysis", 229 | "tokens", 230 | "utility" 231 | ], 232 | "support": { 233 | "docs": "https://phpcsutils.com/", 234 | "issues": "https://github.com/PHPCSStandards/PHPCSUtils/issues", 235 | "security": "https://github.com/PHPCSStandards/PHPCSUtils/security/policy", 236 | "source": "https://github.com/PHPCSStandards/PHPCSUtils" 237 | }, 238 | "funding": [ 239 | { 240 | "url": "https://github.com/PHPCSStandards", 241 | "type": "github" 242 | }, 243 | { 244 | "url": "https://github.com/jrfnl", 245 | "type": "github" 246 | }, 247 | { 248 | "url": "https://opencollective.com/php_codesniffer", 249 | "type": "open_collective" 250 | } 251 | ], 252 | "time": "2024-05-20T13:34:27+00:00" 253 | }, 254 | { 255 | "name": "squizlabs/php_codesniffer", 256 | "version": "3.10.3", 257 | "source": { 258 | "type": "git", 259 | "url": "https://github.com/PHPCSStandards/PHP_CodeSniffer.git", 260 | "reference": "62d32998e820bddc40f99f8251958aed187a5c9c" 261 | }, 262 | "dist": { 263 | "type": "zip", 264 | "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/62d32998e820bddc40f99f8251958aed187a5c9c", 265 | "reference": "62d32998e820bddc40f99f8251958aed187a5c9c", 266 | "shasum": "" 267 | }, 268 | "require": { 269 | "ext-simplexml": "*", 270 | "ext-tokenizer": "*", 271 | "ext-xmlwriter": "*", 272 | "php": ">=5.4.0" 273 | }, 274 | "require-dev": { 275 | "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.3.4" 276 | }, 277 | "bin": [ 278 | "bin/phpcbf", 279 | "bin/phpcs" 280 | ], 281 | "type": "library", 282 | "extra": { 283 | "branch-alias": { 284 | "dev-master": "3.x-dev" 285 | } 286 | }, 287 | "notification-url": "https://packagist.org/downloads/", 288 | "license": [ 289 | "BSD-3-Clause" 290 | ], 291 | "authors": [ 292 | { 293 | "name": "Greg Sherwood", 294 | "role": "Former lead" 295 | }, 296 | { 297 | "name": "Juliette Reinders Folmer", 298 | "role": "Current lead" 299 | }, 300 | { 301 | "name": "Contributors", 302 | "homepage": "https://github.com/PHPCSStandards/PHP_CodeSniffer/graphs/contributors" 303 | } 304 | ], 305 | "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", 306 | "homepage": "https://github.com/PHPCSStandards/PHP_CodeSniffer", 307 | "keywords": [ 308 | "phpcs", 309 | "standards", 310 | "static analysis" 311 | ], 312 | "support": { 313 | "issues": "https://github.com/PHPCSStandards/PHP_CodeSniffer/issues", 314 | "security": "https://github.com/PHPCSStandards/PHP_CodeSniffer/security/policy", 315 | "source": "https://github.com/PHPCSStandards/PHP_CodeSniffer", 316 | "wiki": "https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki" 317 | }, 318 | "funding": [ 319 | { 320 | "url": "https://github.com/PHPCSStandards", 321 | "type": "github" 322 | }, 323 | { 324 | "url": "https://github.com/jrfnl", 325 | "type": "github" 326 | }, 327 | { 328 | "url": "https://opencollective.com/php_codesniffer", 329 | "type": "open_collective" 330 | } 331 | ], 332 | "time": "2024-09-18T10:38:58+00:00" 333 | }, 334 | { 335 | "name": "wp-coding-standards/wpcs", 336 | "version": "3.1.0", 337 | "source": { 338 | "type": "git", 339 | "url": "https://github.com/WordPress/WordPress-Coding-Standards.git", 340 | "reference": "9333efcbff231f10dfd9c56bb7b65818b4733ca7" 341 | }, 342 | "dist": { 343 | "type": "zip", 344 | "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/9333efcbff231f10dfd9c56bb7b65818b4733ca7", 345 | "reference": "9333efcbff231f10dfd9c56bb7b65818b4733ca7", 346 | "shasum": "" 347 | }, 348 | "require": { 349 | "ext-filter": "*", 350 | "ext-libxml": "*", 351 | "ext-tokenizer": "*", 352 | "ext-xmlreader": "*", 353 | "php": ">=5.4", 354 | "phpcsstandards/phpcsextra": "^1.2.1", 355 | "phpcsstandards/phpcsutils": "^1.0.10", 356 | "squizlabs/php_codesniffer": "^3.9.0" 357 | }, 358 | "require-dev": { 359 | "php-parallel-lint/php-console-highlighter": "^1.0.0", 360 | "php-parallel-lint/php-parallel-lint": "^1.3.2", 361 | "phpcompatibility/php-compatibility": "^9.0", 362 | "phpcsstandards/phpcsdevtools": "^1.2.0", 363 | "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0" 364 | }, 365 | "suggest": { 366 | "ext-iconv": "For improved results", 367 | "ext-mbstring": "For improved results" 368 | }, 369 | "type": "phpcodesniffer-standard", 370 | "notification-url": "https://packagist.org/downloads/", 371 | "license": [ 372 | "MIT" 373 | ], 374 | "authors": [ 375 | { 376 | "name": "Contributors", 377 | "homepage": "https://github.com/WordPress/WordPress-Coding-Standards/graphs/contributors" 378 | } 379 | ], 380 | "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions", 381 | "keywords": [ 382 | "phpcs", 383 | "standards", 384 | "static analysis", 385 | "wordpress" 386 | ], 387 | "support": { 388 | "issues": "https://github.com/WordPress/WordPress-Coding-Standards/issues", 389 | "source": "https://github.com/WordPress/WordPress-Coding-Standards", 390 | "wiki": "https://github.com/WordPress/WordPress-Coding-Standards/wiki" 391 | }, 392 | "funding": [ 393 | { 394 | "url": "https://opencollective.com/php_codesniffer", 395 | "type": "custom" 396 | } 397 | ], 398 | "time": "2024-03-25T16:39:00+00:00" 399 | } 400 | ], 401 | "aliases": [], 402 | "minimum-stability": "stable", 403 | "stability-flags": [], 404 | "prefer-stable": false, 405 | "prefer-lowest": false, 406 | "platform": [], 407 | "platform-dev": [], 408 | "plugin-api-version": "2.6.0" 409 | } 410 | -------------------------------------------------------------------------------- /enable-responsive-image.php: -------------------------------------------------------------------------------- 1 | (int) apply_filters( 'enable_responsive_image_default_media_value', 600 ), 38 | 'showPreviewButton' => (bool) apply_filters( 'enable_responsive_image_show_preview_button', true ), 39 | ) 40 | ); 41 | 42 | wp_set_script_translations( 43 | 'enable-responsive-image', 44 | 'enable-responsive-image', 45 | ); 46 | 47 | wp_enqueue_style( 48 | 'enable-responsive-image', 49 | $plugin_url . '/build/index.css', 50 | array(), 51 | filemtime( $plugin_path . '/build/index.css' ) 52 | ); 53 | } 54 | 55 | add_action( 'enqueue_block_editor_assets', 'enable_responsive_image_enqueue_block_editor_assets' ); 56 | 57 | function enable_responsive_image_render_block_image( $block_content, $block ) { 58 | if ( ! isset( $block['attrs']['enableResponsiveImageSources'] ) ) { 59 | return $block_content; 60 | } 61 | 62 | if ( ! is_array( $block['attrs']['enableResponsiveImageSources'] ) ) { 63 | return $block_content; 64 | } 65 | 66 | $filtered_sources = array_filter( 67 | $block['attrs']['enableResponsiveImageSources'], 68 | function ( $source ) { 69 | return isset( $source['srcset'] ) && isset( $source['mediaType'] ) && isset( $source['mediaValue'] ); 70 | } 71 | ); 72 | 73 | if ( empty( $filtered_sources ) ) { 74 | return $block_content; 75 | } 76 | 77 | preg_match( '//', $block_content, $matches ); 78 | 79 | if ( ! isset( $matches[0] ) ) { 80 | return $block_content; 81 | } 82 | 83 | $image_tag = $matches[0]; 84 | $allowed_media_types = array( 'min-width', 'max-width' ); 85 | 86 | /** 87 | * Filters the default media value (px). 88 | * 89 | * @since 1.0.0 90 | * 91 | * @param int $media_value The media value (px). Default is 600. 92 | */ 93 | $default_media_value = (int) apply_filters( 'enable_responsive_image_default_media_value', 600 ); 94 | 95 | $source_tags = ''; 96 | 97 | foreach ( $filtered_sources as $source ) { 98 | $media_type = in_array( $source['mediaType'], $allowed_media_types, true ) ? $source['mediaType'] : 'max-width'; 99 | $source_tags .= sprintf( 100 | '', 101 | esc_url( $source['srcset'] ), 102 | $source['mediaType'], 103 | $source['mediaValue'] ? (int) $source['mediaValue'] : $default_media_value, 104 | ); 105 | } 106 | 107 | $new_image_tag = sprintf( 108 | '%1$s%2$s', 109 | $source_tags, 110 | $image_tag 111 | ); 112 | 113 | $block_content = str_replace( $image_tag, $new_image_tag, $block_content ); 114 | 115 | return $block_content; 116 | } 117 | 118 | // The image block has the same hook with priority 10 and 119 | // adds a button element for the Lightbox after the img tag. 120 | // This hook must be run after so that this button element is 121 | // not wrapped in a picture tag. 122 | add_filter( 'render_block_core/image', 'enable_responsive_image_render_block_image', 20, 2 ); 123 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "enable-responsive-image", 3 | "version": "1.4.0", 4 | "description": "WordPress plugin that adds settings to the Image block to display different images depending on the width of the screen.", 5 | "author": "Aki Hamano", 6 | "license": "GPL-2.0-or-later", 7 | "keywords": [ 8 | "gutenberg", 9 | "block", 10 | "image", 11 | "responsive" 12 | ], 13 | "homepage": "https://github.com/t-hamano/enable-responsive-image", 14 | "repository": "git+https://github.com/t-hamano/enable-responsive-image.git", 15 | "bugs": { 16 | "url": "https://github.com/t-hamano/enable-responsive-image/issues" 17 | }, 18 | "engines": { 19 | "node": ">=22.0.0", 20 | "npm": ">=10.9.2" 21 | }, 22 | "volta": { 23 | "node": "22.16.0", 24 | "npm": "10.9.2" 25 | }, 26 | "devDependencies": { 27 | "@types/wordpress__block-editor": "11.5.16", 28 | "@wordpress/base-styles": "6.0.0", 29 | "@wordpress/core-data": "7.24.0", 30 | "@wordpress/env": "^10.24.0", 31 | "@wordpress/icons": "10.24.0", 32 | "@wordpress/scripts": "^30.17.0", 33 | "clsx": "2.1.1", 34 | "husky": "^9.1.7", 35 | "lint-staged": "16.1.0", 36 | "prettier": "npm:wp-prettier@3.0.3", 37 | "typescript": "5.8.3" 38 | }, 39 | "scripts": { 40 | "wp-env": "wp-env", 41 | "stop": "wp-env stop", 42 | "start": "wp-scripts start", 43 | "build": "wp-scripts build", 44 | "check-licenses": "wp-scripts check-licenses", 45 | "lint": "npm run lint:css && npm run lint:js && npm run lint:types && npm run lint:php && npm run lint:md-docs && npm run lint:pkg-json", 46 | "lint:css": "wp-scripts lint-style", 47 | "lint:js": "wp-scripts lint-js", 48 | "lint:types": "tsc", 49 | "lint:php": "composer lint", 50 | "lint:md-docs": "wp-scripts lint-md-docs", 51 | "lint:pkg-json": "wp-scripts lint-pkg-json", 52 | "format": "wp-scripts format", 53 | "test": "npm run lint:js && npm run test:e2e", 54 | "test:e2e": "wp-scripts test-playwright", 55 | "test:e2e:debug": "wp-scripts test-playwright --debug", 56 | "prepare": "husky" 57 | }, 58 | "lint-staged": { 59 | "*.{js,json,ts,tsx,yml,yaml}": [ 60 | "wp-scripts format" 61 | ], 62 | "*.{js,ts,tsx}": [ 63 | "wp-scripts lint-js" 64 | ], 65 | "*.scss": [ 66 | "wp-scripts lint-style" 67 | ], 68 | "*.md": [ 69 | "wp-scripts lint-md-docs" 70 | ], 71 | "package.json": [ 72 | "wp-scripts lint-pkg-json" 73 | ] 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /phpcs.ruleset.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | A custom set of code standard rules to check for WordPress themes. 4 | 5 | 6 | 7 | 8 | */node_modules/* 9 | */vendor/* 10 | */build/* 11 | */*.js 12 | */*.css 13 | 14 | -------------------------------------------------------------------------------- /playwright.config.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * WordPress dependencies 3 | */ 4 | const config = require( '@wordpress/scripts/config/playwright.config.js' ); 5 | 6 | export default { 7 | ...config, 8 | testDir: './test/e2e', 9 | }; 10 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Enable Responsive Image === 2 | Contributors: wildworks, Toro_Unit 3 | Tags: gutenberg, block, image, responsive 4 | Requires at least: 6.6 5 | Tested up to: 6.8 6 | Stable tag: 1.4.0 7 | Requires PHP: 7.4 8 | License: GPLv2 or later 9 | License URI: https://www.gnu.org/licenses/gpl-2.0.html 10 | 11 | WordPress plugin that adds settings to the Image block to display different images depending on the width of the screen. 12 | 13 | == Description == 14 | 15 | Enable Responsive Image adds settings to the Image block to display different images depending on the width of the screen. You can add multiple images and set media queries and resolution for each image. If the screen width matches the conditions of that media query, it will switch to the corresponding image. 16 | 17 | == Installation == 18 | 19 | 1. Upload the `enable-responsive-image` folder to the `/wp-content/plugins/` directory. 20 | 2. Activate the plugin through the \'Plugins\' menu in WordPress. 21 | 22 | == Frequently Asked Questions == 23 | 24 | = How does this plugin work? = 25 | 26 | This plugin rewrites the HTML markup for the image block rendered on the front end. Wrap the img element with a picture element, and add source elements with srcset and media attributes inside the picture element based on the settings of the added image. 27 | 28 | = It does not work correctly when multiple image sources are set. = 29 | 30 | Try rearranging the order of the images. For example, if both images have a Media Query Type of max-width, the one with the smaller value should be ordered on top. 31 | 32 | = Even if I switch the screen width or device on the editor, it does not switch to the set image. = 33 | 34 | On the editor side, images do not switch by default. Click the "Enable responsive image preview" button on the block toolbar. 35 | 36 | = What filters can I use? = 37 | 38 | You can find a list of the available filters in the [Github readme](https://github.com/t-hamano/enable-responsive-image#filters). 39 | 40 | == Screenshots == 41 | 42 | 1. Settings added to the block sidebar of the image block 43 | 2. How the image changes depending on the screen width 44 | 45 | == Resources == 46 | 47 | = Image for screenshot = 48 | 49 | * License: Public Domain 50 | * Source: https://openverse.org/image/cd8e5cc5-d38a-462e-b4c1-1ea5c6f94e20 51 | 52 | == Changelog == 53 | 54 | = 1.4.0 = 55 | * Tested to WordPress 6.8 56 | * Enhancement: Show full srcset url 57 | * Accessibility: Respect user preference for CSS transitions 58 | * Drop support for WordPress 6.5 59 | 60 | = 1.3.0 = 61 | * Tested to WordPress 6.7 62 | * Drop support for WordPress 6.4 63 | 64 | = 1.2.0 = 65 | * Tested to WordPress 6.6 66 | 67 | = 1.1.1 = 68 | * Remove unnecessary changelog 69 | 70 | = 1.1.0 = 71 | * Tested to WordPress 6.5 72 | * Enhancement: Polish block sidebar 73 | 74 | = 1.0.0 = 75 | * Initial release 76 | -------------------------------------------------------------------------------- /src/block-edit-preview.tsx: -------------------------------------------------------------------------------- 1 | /** 2 | * External dependencies 3 | */ 4 | import clsx from 'clsx'; 5 | 6 | /** 7 | * WordPress dependencies 8 | */ 9 | import { 10 | RichText, 11 | useBlockProps, 12 | // @ts-ignore: has no exported member 13 | __experimentalGetElementClassName, 14 | // @ts-ignore: has no exported member 15 | __experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles, 16 | // @ts-ignore: has no exported member 17 | __experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles, 18 | } from '@wordpress/block-editor'; 19 | import type { BlockEditProps } from '@wordpress/blocks'; 20 | 21 | /** 22 | * Internal dependencies 23 | */ 24 | import type { BlockAttributes } from './types'; 25 | 26 | export default function BlockEditPreview( { attributes }: BlockEditProps< BlockAttributes > ) { 27 | const { 28 | url, 29 | alt, 30 | caption, 31 | align, 32 | width, 33 | height, 34 | aspectRatio, 35 | scale, 36 | enableResponsiveImageSources: sources, 37 | } = attributes; 38 | 39 | const borderProps = getBorderClassesAndStyles( attributes ); 40 | const shadowProps = 41 | typeof getShadowClassesAndStyles === 'function' ? getShadowClassesAndStyles( attributes ) : {}; 42 | 43 | const filteredSources = sources.filter( ( { srcset, mediaType, mediaValue } ) => { 44 | return srcset && mediaType && mediaValue; 45 | } ); 46 | 47 | const classes = clsx( { 48 | [ `align${ align }` ]: align, 49 | 'has-custom-border': 50 | !! borderProps.className || 51 | ( borderProps.style && Object.keys( borderProps.style ).length > 0 ), 52 | } ); 53 | 54 | const blockProps = useBlockProps( { 55 | className: classes, 56 | } ); 57 | 58 | return ( 59 |
60 | 61 | { filteredSources.length > 0 && 62 | filteredSources.map( ( { srcset, mediaType, mediaValue }, index ) => ( 63 | 68 | ) ) } 69 | { 82 | 83 | { ! RichText.isEmpty( caption ) && ( 84 | 89 | ) } 90 |
91 | ); 92 | } 93 | -------------------------------------------------------------------------------- /src/constants.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * WordPress dependencies 3 | */ 4 | import { __ } from '@wordpress/i18n'; 5 | 6 | export const DEFAULT_MEDIA_VALUE = isNaN( 7 | parseInt( window?.enableResponsiveImage?.defaultMediaValue ) 8 | ) 9 | ? 600 10 | : parseInt( window?.enableResponsiveImage?.defaultMediaValue ); 11 | 12 | export const SHOW_PREVIEW_BUTTON = window?.enableResponsiveImage?.showPreviewButton === '1'; 13 | 14 | export const MEDIA_TYPES = [ 15 | { 16 | label: __( 'max-width', 'enable-responsive-image' ), 17 | value: 'max-width', 18 | }, 19 | { 20 | label: __( 'min-width', 'enable-responsive-image' ), 21 | value: 'min-width', 22 | }, 23 | ]; 24 | 25 | export const MAX_SOURCES = 5; 26 | export const MIN_MEDIA_VALUE = 100; 27 | export const MAX_MEDIA_VALUE = 2000; 28 | -------------------------------------------------------------------------------- /src/editor.scss: -------------------------------------------------------------------------------- 1 | @use "~@wordpress/base-styles/variables" as variables; 2 | @use "~@wordpress/base-styles/colors" as colors; 3 | 4 | .enable-responsive-image { 5 | 6 | .components-base-control { 7 | margin-bottom: 0; 8 | } 9 | 10 | hr { 11 | margin: 0; 12 | border-color: colors.$gray-200; 13 | } 14 | } 15 | 16 | .enable-responsive-image__add-source { 17 | justify-content: center; 18 | } 19 | 20 | .enable-responsive-image__container { 21 | position: relative; 22 | 23 | &:hover, 24 | &:focus, 25 | &:focus-within { 26 | 27 | .enable-responsive-image__movers, 28 | .enable-responsive-image__actions { 29 | opacity: 1; 30 | } 31 | } 32 | } 33 | 34 | .enable-responsive-image__toggle, 35 | .enable-responsive-image__preview { 36 | width: 100%; 37 | padding: 0; 38 | box-shadow: 0 0 0 0 var(--wp-admin-theme-color); 39 | overflow: hidden; 40 | display: flex; 41 | justify-content: center; 42 | height: 150px; 43 | background-color: colors.$gray-100; 44 | 45 | @media not (prefers-reduced-motion) { 46 | transition: all 0.1s ease-out; 47 | } 48 | } 49 | 50 | .enable-responsive-image__preview { 51 | height: auto; 52 | } 53 | 54 | .enable-responsive-image__toggle { 55 | border-radius: 2px; 56 | 57 | &:hover { 58 | background: colors.$gray-300; 59 | color: colors.$gray-900; 60 | } 61 | } 62 | 63 | .enable-responsive-image__movers { 64 | top: 0; 65 | opacity: 0; 66 | right: 0; 67 | padding: variables.$grid-unit-10; 68 | position: absolute; 69 | 70 | @media not (prefers-reduced-motion) { 71 | transition: opacity 50ms ease-out; 72 | } 73 | } 74 | 75 | .enable-responsive-image__mover { 76 | backdrop-filter: blur(16px) saturate(180%); 77 | background: rgba(255, 255, 255, 0.75); 78 | } 79 | 80 | .enable-responsive-image__actions { 81 | bottom: 0; 82 | opacity: 0; 83 | padding: variables.$grid-unit-10; 84 | position: absolute; 85 | 86 | @media not (prefers-reduced-motion) { 87 | transition: opacity 50ms ease-out; 88 | } 89 | } 90 | 91 | .enable-responsive-image__action { 92 | backdrop-filter: blur(16px) saturate(180%); 93 | background: rgba(255, 255, 255, 0.75); 94 | flex-grow: 1; 95 | justify-content: center; 96 | } 97 | 98 | .enable-responsive-image__url { 99 | font-size: 12px; 100 | word-break: break-all; 101 | } 102 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | /** 2 | * WordPress dependencies 3 | */ 4 | import { __ } from '@wordpress/i18n'; 5 | import { addFilter } from '@wordpress/hooks'; 6 | import { useSelect, useDispatch } from '@wordpress/data'; 7 | import { BlockControls, InspectorControls } from '@wordpress/block-editor'; 8 | import { seen } from '@wordpress/icons'; 9 | import { ToolbarGroup, ToolbarButton } from '@wordpress/components'; 10 | import type { BlockEditProps } from '@wordpress/blocks'; 11 | 12 | /** 13 | * Internal dependencies 14 | */ 15 | import BlockEditPreview from './block-edit-preview'; 16 | import SourceList from './source-list'; 17 | import './editor.scss'; 18 | import './store'; 19 | import { SHOW_PREVIEW_BUTTON } from './constants'; 20 | import type { BlockAttributes } from './types'; 21 | 22 | const addSourceAttributes = ( settings: { [ key: string ]: any } ) => { 23 | if ( 'core/image' !== settings.name ) { 24 | return settings; 25 | } 26 | 27 | return { 28 | ...settings, 29 | attributes: { 30 | ...settings.attributes, 31 | enableResponsiveImageSources: { 32 | type: 'array', 33 | items: { 34 | type: 'number', 35 | }, 36 | default: [], 37 | }, 38 | }, 39 | }; 40 | }; 41 | 42 | addFilter( 43 | 'blocks.registerBlockType', 44 | 'enable-responsive-image/add-source-attributes', 45 | addSourceAttributes 46 | ); 47 | 48 | const withInspectorControl = 49 | ( BlockEdit: React.ComponentType< BlockEditProps< BlockAttributes > > ) => 50 | ( 51 | props: BlockEditProps< BlockAttributes > & { 52 | name: string; 53 | } 54 | ) => { 55 | if ( props.name !== 'core/image' ) { 56 | return ; 57 | } 58 | 59 | const { attributes } = props; 60 | const { url, enableResponsiveImageSources: sources } = attributes; 61 | 62 | const isPreview = useSelect( 63 | ( select ) => 64 | select( 'enable-responsive-image' ) 65 | // @ts-ignore 66 | .getIsPreview(), 67 | [] 68 | ); 69 | 70 | const { setIsPreview } = useDispatch( 'enable-responsive-image' ); 71 | 72 | return ( 73 | <> 74 | { url && sources?.length > 0 && isPreview ? ( 75 | 76 | ) : ( 77 | 78 | ) } 79 | { url && sources?.length > 0 && SHOW_PREVIEW_BUTTON && ( 80 | 81 | 82 | setIsPreview( ! isPreview ) } 91 | /> 92 | 93 | 94 | ) } 95 | { url && ( 96 | 97 | 98 | 99 | ) } 100 | 101 | ); 102 | }; 103 | 104 | addFilter( 105 | 'editor.BlockEdit', 106 | 'enable-responsive-image/with-inspector-control', 107 | withInspectorControl 108 | ); 109 | -------------------------------------------------------------------------------- /src/source-editor.tsx: -------------------------------------------------------------------------------- 1 | /** 2 | * WordPress dependencies 3 | */ 4 | import { useState } from '@wordpress/element'; 5 | import { __ } from '@wordpress/i18n'; 6 | import { 7 | Button, 8 | DropZone, 9 | ExternalLink, 10 | RangeControl, 11 | Spinner, 12 | SelectControl, 13 | __experimentalHStack as HStack, 14 | __experimentalVStack as VStack, 15 | __experimentalToggleGroupControl as ToggleGroupControl, 16 | __experimentalToggleGroupControlOption as ToggleGroupControlOption, 17 | } from '@wordpress/components'; 18 | 19 | import { MediaUpload, store as blockEditorStore } from '@wordpress/block-editor'; 20 | import { useDispatch, useSelect } from '@wordpress/data'; 21 | import { store as coreStore } from '@wordpress/core-data'; 22 | import { store as noticesStore } from '@wordpress/notices'; 23 | import { chevronUp, chevronDown } from '@wordpress/icons'; 24 | import { isBlobURL } from '@wordpress/blob'; 25 | 26 | /** 27 | * Internal dependencies 28 | */ 29 | import type { Media, Source } from './types'; 30 | import { DEFAULT_MEDIA_VALUE, MEDIA_TYPES, MIN_MEDIA_VALUE, MAX_MEDIA_VALUE } from './constants'; 31 | 32 | type Props = { 33 | source?: Source; 34 | disableMoveUp: boolean; 35 | disableMoveDown: boolean; 36 | disableActions?: boolean; 37 | onChange: ( {}: Source ) => void; 38 | onRemove: () => void; 39 | onChangeOrder?: ( direction: number ) => void; 40 | isSelected: boolean; 41 | }; 42 | 43 | export default function SourceEditor( { 44 | source = { 45 | srcset: undefined, 46 | id: undefined, 47 | slug: undefined, 48 | mediaType: undefined, 49 | mediaValue: undefined, 50 | }, 51 | disableMoveUp = false, 52 | disableMoveDown = false, 53 | disableActions = false, 54 | onChangeOrder, 55 | onChange, 56 | onRemove, 57 | isSelected, 58 | }: Props ) { 59 | const { id, srcset, mediaType, mediaValue, slug: srcsetSlug } = source; 60 | const { image } = useSelect( 61 | ( select ) => { 62 | return { 63 | image: 64 | id && isSelected 65 | ? select( coreStore ) 66 | // @ts-ignore 67 | .getMedia( id, { context: 'view' } ) 68 | : null, 69 | }; 70 | }, 71 | [ id, isSelected ] 72 | ); 73 | 74 | const { mediaUpload, imageSizes } = useSelect( 75 | ( select ) => ( { 76 | imageSizes: select( blockEditorStore ) 77 | // @ts-ignore 78 | .getSettings().imageSizes, 79 | mediaUpload: select( blockEditorStore ) 80 | // @ts-ignore 81 | .getSettings().mediaUpload, 82 | } ), 83 | [] 84 | ); 85 | 86 | const { createErrorNotice, removeAllNotices } = useDispatch( noticesStore ); 87 | 88 | const [ isLoading, setIsLoading ] = useState( false ); 89 | 90 | const imageSizeOptions = imageSizes 91 | .filter( ( { slug }: { slug: string } ) => { 92 | return image?.media_details?.sizes?.[ slug ]?.source_url; 93 | } ) 94 | .map( ( { name, slug }: { name: string; slug: string } ) => ( { 95 | value: slug, 96 | label: name, 97 | } ) ); 98 | 99 | function onSelectImage( media: Media ) { 100 | if ( ! media ) { 101 | return; 102 | } 103 | 104 | onChange( { 105 | ...source, 106 | srcset: media.url, 107 | id: media.id, 108 | slug: source.slug || 'full', 109 | mediaType: source.mediaType || MEDIA_TYPES[ 0 ].value, 110 | mediaValue: source.mediaValue || DEFAULT_MEDIA_VALUE, 111 | } ); 112 | } 113 | 114 | function onChangeMediaType( value: string | number | undefined ) { 115 | const filteredMediaType = MEDIA_TYPES.find( ( type ) => type.value === value ); 116 | if ( ! filteredMediaType ) { 117 | return; 118 | } 119 | onChange( { 120 | ...source, 121 | mediaType: filteredMediaType.value, 122 | } ); 123 | } 124 | 125 | function onChangeMediaValue( value: number | undefined ) { 126 | onChange( { 127 | ...source, 128 | mediaValue: value, 129 | } ); 130 | } 131 | 132 | function onChangeResolution( newSlug: string ) { 133 | const newUrl = image?.media_details?.sizes?.[ newSlug ]?.source_url; 134 | if ( ! newUrl ) { 135 | return null; 136 | } 137 | 138 | onChange( { 139 | ...source, 140 | srcset: newUrl, 141 | slug: newSlug, 142 | } ); 143 | } 144 | 145 | function onDropFiles( filesList: File[] ) { 146 | mediaUpload( { 147 | allowedTypes: [ 'image' ], 148 | filesList, 149 | onFileChange( [ newImage ]: Media[] ) { 150 | if ( isBlobURL( newImage?.url ) ) { 151 | setIsLoading( true ); 152 | return; 153 | } 154 | onSelectImage( newImage ); 155 | setIsLoading( false ); 156 | }, 157 | onError( message: string ) { 158 | // @ts-ignore 159 | removeAllNotices(); 160 | // @ts-ignore 161 | createErrorNotice( message, { type: 'snackbar' } ); 162 | }, 163 | } ); 164 | } 165 | 166 | return ( 167 | 168 | 169 | ( 174 |
175 | 189 | 190 | { ! ( disableMoveUp && disableMoveDown ) && ( 191 | <> 192 | 223 | 230 | 231 | ) } 232 | 233 |
234 | ) } 235 | /> 236 | { !! id && srcset && ( 237 | 238 | { srcset } 239 | 240 | ) } 241 |
242 | { !! id && srcset && ( 243 | <> 244 | 252 | { MEDIA_TYPES.map( ( { label, value } ) => ( 253 | 254 | ) ) } 255 | 256 | 267 | 276 | 277 | ) } 278 |
279 | ); 280 | } 281 | -------------------------------------------------------------------------------- /src/source-list.tsx: -------------------------------------------------------------------------------- 1 | /** 2 | * WordPress dependencies 3 | */ 4 | import { __ } from '@wordpress/i18n'; 5 | import { Fragment } from '@wordpress/element'; 6 | import { Button, PanelBody, __experimentalVStack as VStack } from '@wordpress/components'; 7 | import { MediaUploadCheck } from '@wordpress/block-editor'; 8 | import type { BlockEditProps } from '@wordpress/blocks'; 9 | 10 | /** 11 | * Internal dependencies 12 | */ 13 | import SourceEditor from './source-editor'; 14 | import type { BlockAttributes, Source } from './types'; 15 | import { MAX_SOURCES } from './constants'; 16 | 17 | export default function ImageList( props: BlockEditProps< BlockAttributes > ) { 18 | const { attributes, setAttributes } = props; 19 | const { enableResponsiveImageSources: sources } = attributes; 20 | 21 | function onChange( newSource: Source, index: number ) { 22 | const newSources = [ ...sources ]; 23 | newSources[ index ] = newSource; 24 | setAttributes( { enableResponsiveImageSources: newSources } ); 25 | } 26 | 27 | function onAddSource() { 28 | const newSources = [ ...sources ]; 29 | newSources.push( { 30 | srcset: undefined, 31 | id: undefined, 32 | slug: undefined, 33 | mediaType: undefined, 34 | mediaValue: undefined, 35 | } ); 36 | setAttributes( { enableResponsiveImageSources: newSources } ); 37 | } 38 | 39 | function onChangeOrder( direction: number, index: number ) { 40 | const newSources = [ ...sources ]; 41 | const newIndex = index + direction; 42 | const movedSource = newSources.splice( index, 1 )[ 0 ]; 43 | newSources.splice( newIndex, 0, movedSource ); 44 | setAttributes( { enableResponsiveImageSources: newSources } ); 45 | } 46 | 47 | function onRemoveSource( index: number ) { 48 | const newSources = [ ...sources ]; 49 | newSources.splice( index, 1 ); 50 | setAttributes( { enableResponsiveImageSources: newSources } ); 51 | } 52 | 53 | return ( 54 | 58 | 61 | { __( 62 | 'To edit the image, you need permission to upload media.', 63 | 'enable-responsive-image' 64 | ) } 65 |

66 | } 67 | > 68 | 69 | { sources.length > 0 ? ( 70 | <> 71 | { sources.map( ( source, index ) => ( 72 | 73 | onChangeOrder( direction, index ) } 82 | onChange={ ( newSource: Source ) => onChange( newSource, index ) } 83 | onRemove={ () => onRemoveSource( index ) } 84 | /> 85 | { index < sources.length - 1 &&
} 86 |
87 | ) ) } 88 | { ( sources.length > 1 || 89 | ( sources.length === 1 && sources[ 0 ].id ) || 90 | sources[ 0 ].srcset ) && ( 91 | <> 92 |
93 | 102 | 103 | ) } 104 | 105 | ) : ( 106 | onChange( newSource, 0 ) } 112 | onRemove={ () => onRemoveSource( 0 ) } 113 | /> 114 | ) } 115 |
116 |
117 |
118 | ); 119 | } 120 | -------------------------------------------------------------------------------- /src/store.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * WordPress dependencies 3 | */ 4 | import { createReduxStore, register } from '@wordpress/data'; 5 | 6 | const DEFAULT_STATE = { 7 | isPreview: false, 8 | }; 9 | 10 | export const store = createReduxStore( 'enable-responsive-image', { 11 | reducer: ( state = DEFAULT_STATE, action ) => { 12 | if ( action.type === 'UPDATE_IS_PREVIEW' ) { 13 | return { 14 | ...state, 15 | isPreview: ! state.isPreview, 16 | }; 17 | } 18 | 19 | return state; 20 | }, 21 | selectors: { 22 | getIsPreview( state: { isPreview: boolean } ) { 23 | return state.isPreview; 24 | }, 25 | }, 26 | actions: { 27 | setIsPreview( value ) { 28 | return { 29 | type: 'UPDATE_IS_PREVIEW', 30 | value, 31 | }; 32 | }, 33 | }, 34 | } ); 35 | 36 | register( store ); 37 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | export interface BlockAttributes { 2 | enableResponsiveImageSources: Source[]; 3 | [ key: string ]: any; 4 | } 5 | 6 | export interface Source { 7 | srcset: string | undefined; 8 | id: number | undefined; 9 | slug: string | undefined; 10 | mediaType: string | undefined; 11 | mediaValue: number | undefined; 12 | } 13 | 14 | export interface Media { 15 | id: number | undefined; 16 | [ key: string ]: any; 17 | } 18 | 19 | export interface enableResponsiveImageVars { 20 | defaultMediaValue: string; 21 | showPreviewButton: string; 22 | } 23 | 24 | declare global { 25 | interface Window { 26 | enableResponsiveImage: enableResponsiveImageVars; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test/e2e/assets/1000x750.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t-hamano/enable-responsive-image/4ed3dbf68c5543d0c40f9eed9d6cbd1e9ba1e998/test/e2e/assets/1000x750.png -------------------------------------------------------------------------------- /test/e2e/assets/400x300.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t-hamano/enable-responsive-image/4ed3dbf68c5543d0c40f9eed9d6cbd1e9ba1e998/test/e2e/assets/400x300.png -------------------------------------------------------------------------------- /test/e2e/assets/600x450.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t-hamano/enable-responsive-image/4ed3dbf68c5543d0c40f9eed9d6cbd1e9ba1e998/test/e2e/assets/600x450.png -------------------------------------------------------------------------------- /test/e2e/test.spec.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * External dependencies 3 | */ 4 | import type { Page } from '@playwright/test'; 5 | import path from 'path'; 6 | import fs from 'fs'; 7 | import os from 'os'; 8 | import { v4 as uuid } from 'uuid'; 9 | 10 | /** 11 | * WordPress dependencies 12 | */ 13 | import { test as base, expect } from '@wordpress/e2e-test-utils-playwright'; 14 | 15 | const test = base.extend< { 16 | mediaUtils: MediaUtils; 17 | } >( { 18 | mediaUtils: async ( { page }, use ) => { 19 | await use( new MediaUtils( { page } ) ); 20 | }, 21 | } ); 22 | 23 | test.describe( 'Image Block', () => { 24 | test.beforeEach( async ( { admin } ) => { 25 | await admin.createNewPost(); 26 | } ); 27 | 28 | test.afterAll( async ( { requestUtils } ) => { 29 | await requestUtils.deleteAllMedia(); 30 | } ); 31 | 32 | test( 'should create image with image sources', async ( { editor, page, mediaUtils } ) => { 33 | // Insert Image block. 34 | await editor.insertBlock( { name: 'core/image' } ); 35 | const imageBlock = editor.canvas.getByRole( 'document', { 36 | name: 'Block: Image', 37 | } ); 38 | await expect( imageBlock ).toBeVisible(); 39 | 40 | // Upload image. 41 | await mediaUtils.uploadImage( 42 | imageBlock.locator( 'data-testid=form-file-upload-input' ), 43 | '1000x750.png' 44 | ); 45 | 46 | const blockSettings = page.getByRole( 'region', { 47 | name: 'Editor settings', 48 | } ); 49 | const ImageSourcesPanel = await blockSettings.locator( '.enable-responsive-image' ); 50 | 51 | // Add first image source. 52 | await editor.openDocumentSettingsSidebar(); 53 | const firstSourceFilename = await mediaUtils.uploadSource( '600x450.png' ); 54 | const firstSource = ImageSourcesPanel.locator( 'img' ); 55 | await expect( firstSource ).toBeVisible(); 56 | await expect( firstSource ).toHaveAttribute( 'src', new RegExp( firstSourceFilename ) ); 57 | 58 | // Change first image setting. 59 | await mediaUtils.changeMediaQueryValue( '800' ); 60 | await mediaUtils.changeResolution( 'Medium' ); 61 | 62 | // Add second image source. 63 | await page.getByRole( 'button', { name: 'Add image source' } ).click(); 64 | const secondSourceFilename = await mediaUtils.uploadSource( '400x300.png' ); 65 | const secondSource = ImageSourcesPanel.locator( 'img' ).nth( 1 ); 66 | 67 | await expect( secondSource ).toBeVisible(); 68 | await expect( secondSource ).toHaveAttribute( 'src', new RegExp( secondSourceFilename ) ); 69 | 70 | // Chage second image setting. 71 | await mediaUtils.changeMediaQueryValue( '500', 1 ); 72 | await mediaUtils.changeMediaQueryType( 'min-width', 1 ); 73 | await mediaUtils.changeResolution( 'Thumbnail', 1 ); 74 | 75 | const blocks = await editor.getBlocks(); 76 | 77 | await expect.poll( editor.getBlocks ).toMatchObject( [ 78 | { 79 | name: 'core/image', 80 | attributes: { 81 | enableResponsiveImageSources: [ 82 | { 83 | slug: 'medium', 84 | mediaType: 'max-width', 85 | mediaValue: 800, 86 | }, 87 | { 88 | slug: 'thumbnail', 89 | mediaType: 'min-width', 90 | mediaValue: 500, 91 | }, 92 | ], 93 | }, 94 | }, 95 | ] ); 96 | 97 | const sources = blocks[ 0 ].attributes.enableResponsiveImageSources; 98 | expect( sources?.[ 0 ].srcset.includes( firstSourceFilename ) ).toBe( true ); 99 | expect( sources?.[ 1 ].srcset.includes( secondSourceFilename ) ).toBe( true ); 100 | } ); 101 | } ); 102 | 103 | class MediaUtils { 104 | page: Page; 105 | basePath: string; 106 | 107 | constructor( { page } ) { 108 | this.page = page; 109 | this.basePath = path.join( __dirname, 'assets' ); 110 | } 111 | 112 | async uploadImage( inputElement, customFile ) { 113 | const tmpDirectory = await fs.mkdtempSync( path.join( os.tmpdir(), 'test-image-' ) ); 114 | const filename = uuid(); 115 | const tmpFileName = path.join( tmpDirectory, filename + '.png' ); 116 | const filepath = path.join( this.basePath, customFile ); 117 | await fs.copyFileSync( filepath, tmpFileName ); 118 | await inputElement.setInputFiles( tmpFileName ); 119 | return filename; 120 | } 121 | 122 | async uploadSource( customFile ) { 123 | await this.page.getByRole( 'button', { name: 'Set image source' } ).click(); 124 | const filename = await this.uploadImage( 125 | this.page.locator( '.media-modal .moxie-shim input[type=file]' ), 126 | customFile 127 | ); 128 | await this.page 129 | .getByRole( 'dialog' ) 130 | .getByRole( 'button', { name: 'Select', exact: true } ) 131 | .click(); 132 | return filename; 133 | } 134 | 135 | async changeMediaQueryType( option, index = 0 ) { 136 | const blockSettings = this.page.getByRole( 'region', { 137 | name: 'Editor settings', 138 | } ); 139 | const ImageSourcesPanel = await blockSettings.locator( '.enable-responsive-image' ); 140 | await ImageSourcesPanel.getByRole( 'radiogroup', { name: 'Media query type' } ) 141 | .nth( index ) 142 | .getByRole( 'radio', { name: option } ) 143 | .setChecked( true ); 144 | } 145 | 146 | async changeMediaQueryValue( value, index = 0 ) { 147 | const blockSettings = this.page.getByRole( 'region', { 148 | name: 'Editor settings', 149 | } ); 150 | const ImageSourcesPanel = await blockSettings.locator( '.enable-responsive-image' ); 151 | await ImageSourcesPanel.getByRole( 'spinbutton', { name: 'Media query value (px)' } ) 152 | .nth( index ) 153 | .fill( value ); 154 | } 155 | 156 | async changeResolution( option, index = 0 ) { 157 | const blockSettings = this.page.getByRole( 'region', { 158 | name: 'Editor settings', 159 | } ); 160 | const ImageSourcesPanel = await blockSettings.locator( '.enable-responsive-image' ); 161 | 162 | await ImageSourcesPanel.getByRole( 'combobox', { name: 'Resolution' } ) 163 | .nth( index ) 164 | .selectOption( { 165 | label: option, 166 | } ); 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "strict": true, 4 | "noEmit": true, 5 | "jsx": "preserve", 6 | "lib": [ "dom", "esnext" ], 7 | "noUnusedLocals": true, 8 | "noUnusedParameters": true, 9 | "esModuleInterop": true, 10 | "resolveJsonModule": true, 11 | "skipLibCheck": true 12 | }, 13 | "include": [ "src/**/*", "src/**/*.json" ], 14 | "exclude": [ "**/build/**" ] 15 | } 16 | --------------------------------------------------------------------------------