-
Notifications
You must be signed in to change notification settings - Fork 0
Developer Integration API #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Arukuen
wants to merge
5
commits into
develop
Choose a base branch
from
feat/dev-api
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
319afb3
fix: Public JS API — window.cimo.optimizeFiles
Arukuen da41fb7
fix: PHP filters for upload selectors
Arukuen 0185b3d
fix: Public enqueue helper — cimo_enqueue_assets()
Arukuen 89e4b77
chore: developer docs
Arukuen cff1995
chore: dev docs and clearer comment
Arukuen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # Cimo Developer Integration | ||
|
|
||
| Cimo can optimize media in the browser before your plugin or theme uploads it. | ||
|
|
||
| ## Automatic Selector Interception | ||
|
|
||
| Use PHP selector filters when your markup uses normal file inputs or drop zones | ||
| and you want Cimo to intercept them automatically before your existing upload | ||
| handler runs. | ||
|
|
||
| Register selector filters before calling `cimo_enqueue_assets()`. | ||
| `cimo_enqueue_assets()` localizes the filtered selector lists while enqueueing, | ||
| so filters added afterward will not apply to the current page load. | ||
|
|
||
| ```php | ||
| add_filter( 'cimo/select_files/allowed_locations', function ( $locations ) { | ||
| $locations[] = '.my-plugin-uploader'; | ||
| return $locations; | ||
| } ); | ||
|
|
||
| add_filter( 'cimo/drop_zone/allowed_locations', function ( $locations ) { | ||
| $locations[] = '.my-plugin-dropzone'; | ||
| return $locations; | ||
| } ); | ||
| ``` | ||
|
|
||
| For file inputs, register a selector for a wrapper around | ||
| `<input type="file">`. | ||
|
|
||
| ```html | ||
| <div class="my-plugin-uploader"> | ||
| <input type="file" accept="image/*"> | ||
| </div> | ||
| ``` | ||
|
|
||
| For drops, register a selector for the drop target. | ||
|
|
||
| ## Enqueue Cimo | ||
|
|
||
| Admin screens and the block editor already enqueue Cimo. For frontend upload | ||
| forms or custom screens where Cimo is not already loaded, call | ||
| `cimo_enqueue_assets()` after registering any selector filters needed for that | ||
| page. | ||
|
|
||
| ```php | ||
| add_action( 'wp_enqueue_scripts', function () { | ||
| if ( is_page( 'my-upload-form' ) && function_exists( 'cimo_enqueue_assets' ) ) { | ||
| cimo_enqueue_assets(); | ||
| } | ||
| } ); | ||
| ``` | ||
|
|
||
| ## Optimize Files Directly | ||
|
|
||
| Use `window.cimo.optimizeFiles()` when your code already controls the upload | ||
| process and can replace the selected files before uploading. | ||
|
|
||
| ```js | ||
| async function handleFiles( files ) { | ||
| const results = await window.cimo.optimizeFiles( files, { showProgress: true } ) | ||
| const filesToUpload = results.map( result => result.file ) | ||
|
|
||
| // Continue with your plugin/theme upload flow. | ||
| uploadFiles( filesToUpload ) | ||
| } | ||
| ``` | ||
|
Arukuen marked this conversation as resolved.
|
||
|
|
||
| The API accepts a single `File`, a `FileList`, or an array of `File` objects. | ||
| It returns: | ||
|
|
||
| ```js | ||
| [ | ||
| { | ||
| file: File, | ||
| metadata: Object || null, | ||
| }, | ||
| ] | ||
| ``` | ||
|
|
||
| `showProgress` defaults to `true`. Set it to `false` if your UI already shows | ||
| upload or optimization progress. | ||
|
|
||
| If Cimo optimization is disabled or a file type is unsupported, the original | ||
| file is returned with `metadata: null`. | ||
|
|
||
| ## Which Approach To Use | ||
|
|
||
| Use `window.cimo.optimizeFiles()` when you can explicitly await optimization | ||
| before calling your uploader. | ||
|
|
||
| Use PHP selector interception when your existing UI already reacts to file input | ||
| or drop events and you want Cimo to transparently replace files before that flow | ||
| continues. | ||
|
|
||
| ## Free And Premium Behavior | ||
|
|
||
| The same JavaScript API is used in free and premium. If Cimo Premium is loaded, | ||
| premium converters are applied automatically through Cimo's normal converter | ||
| pipeline. There is no separate premium entry point. | ||
|
|
||
| Cimo optimization is pre-upload only. It does not optimize by attachment ID, | ||
| bulk replace existing files, or run server-side Imagick/GD compression. | ||
|
|
||
| On guest frontend uploads, files can still be optimized in the browser. Metadata | ||
| saving may be skipped when the visitor is not logged in or cannot access the | ||
| Cimo metadata endpoint. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import './public-api' | ||
| import './media-manager/drop-zone' | ||
| import './media-manager/select-files' | ||
| import './media-manager/sidebar-info' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { applyFilters } from '@wordpress/hooks' | ||
|
|
||
| const normalizeLocations = locations => { | ||
| if ( ! Array.isArray( locations ) ) { | ||
| return [] | ||
| } | ||
|
|
||
| return locations.filter( location => typeof location === 'string' && location.length > 0 ) | ||
| } | ||
|
|
||
| /** | ||
| * Get current select file selectors from PHP settings and internal JS filters. | ||
| * | ||
| * @return {Array<string>} Selectors where file input changes should be intercepted. | ||
| */ | ||
| export const getSelectFilesAllowedLocations = () => { | ||
| const locations = normalizeLocations( window.cimoSettings?.selectFilesAllowedLocations ) | ||
|
|
||
| return normalizeLocations( applyFilters( 'cimo.selectFiles.allowedLocations', locations ) ) | ||
| } | ||
|
|
||
| /** | ||
| * Get current drop zone selectors from PHP settings and internal JS filters. | ||
| * | ||
| * @return {Array<string>} Selectors where file drops should be intercepted. | ||
| */ | ||
| export const getDropZoneAllowedLocations = () => { | ||
| const locations = normalizeLocations( window.cimoSettings?.dropZoneAllowedLocations ) | ||
|
|
||
| return normalizeLocations( applyFilters( 'cimo.dropZone.allowedLocations', locations ) ) | ||
| } | ||
|
|
||
| /** | ||
| * Find the closest ancestor matching one of Cimo's allowed upload locations. | ||
| * | ||
| * @param {Element} element Element where the upload event started. | ||
| * @param {Array<string>} locations CSS selectors to test. | ||
| * @return {Element|null} Matching element, if one exists. | ||
| */ | ||
| export const closestAllowedLocation = ( element, locations ) => { | ||
| for ( const location of locations ) { | ||
| try { | ||
| // Invalid selectors should not break uploads; ignore them and keep checking. | ||
| const matchedElement = element.closest( location ) | ||
|
|
||
| if ( matchedElement ) { | ||
| return matchedElement | ||
| } | ||
| } catch ( error ) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( `[Cimo] Ignoring invalid selector: ${ location }`, error ) | ||
| } | ||
| } | ||
|
|
||
| return null | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.