diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index 7898fda..0000000 --- a/.eslintignore +++ /dev/null @@ -1,30 +0,0 @@ -# Build output -dist/ - -# Distributable archives -builds/ - -# Strauss-prefixed runtime deps -dependencies/ - -# Node modules -node_modules/ - -# WordPress environment -.wp-env/ - -# Composer vendor -vendor/ - -# Test artifacts (minified vendor JS / traces — not lintable) -playwright-report/ -test-results/ - -# tests/ holds Node lane-runner scripts (.mjs) + Playwright specs, not block -# source. lint:js is scoped to src/, so tests/ is never a release-gate target; -# ignoring it stops `lint-js --fix` from over-scanning and rewriting these files. -tests/ - -# Root build/tooling config — airo-wp-owned, not DSG block source. `lint-js -# --fix src/` otherwise silently reformats it on every dsg-sync. -webpack.config.js diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 039664c..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,53 +0,0 @@ -module.exports = { - extends: ['plugin:@wordpress/eslint-plugin/recommended'], - rules: { - 'import/no-extraneous-dependencies': 'off', - 'import/no-unresolved': 'off', - 'jsdoc/require-param-description': 'off', - // src/ is 100% DSG-owned and read-only (see CLAUDE.md): airo-wp never - // authors JS here, so lint must accept DSG's own conventions rather than - // error on source it cannot modify. The parent (DesignSetGo) lints this - // same source clean on an older, unpinned @wordpress/eslint-plugin where - // these rules were warnings; newer versions promote them to errors. - '@wordpress/no-unsafe-wp-apis': 'off', - '@wordpress/no-unused-vars-before-return': 'off', - 'jsdoc/require-param-type': 'off', - 'jsdoc/require-returns-description': 'off', - 'jsdoc/check-line-alignment': 'off', - 'no-nested-ternary': 'off', - 'jsdoc/no-undefined-types': [ - 'error', - { - definedTypes: [ - 'JSX', - 'Element', - 'HTMLElement', - 'HTMLImageElement', - 'IntersectionObserver', - 'NodeList', - 'KeyboardEvent', - 'Document', - ], - }, - ], - }, - overrides: [ - { - files: ['tests/**/*.js', '**/*.test.js', '**/*.spec.js'], - env: { - jest: true, - }, - rules: { - 'no-unused-vars': [ - 'error', - { - varsIgnorePattern: '^_', - argsIgnorePattern: '^_', - caughtErrorsIgnorePattern: '^_', - }, - ], - 'jsx-a11y/label-has-associated-control': 'off', - }, - }, - ], -}; diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4d8cadb..cb301e8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - run: npm run lint:php @@ -35,7 +35,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - run: npm run test:unit:php @@ -47,7 +47,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - name: Build distributable zip diff --git a/.github/workflows/pre-release.yml b/.github/workflows/pre-release.yml index 0522add..37c5c34 100644 --- a/.github/workflows/pre-release.yml +++ b/.github/workflows/pre-release.yml @@ -32,7 +32,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - run: npm run lint:php @@ -44,7 +44,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - run: npm run lint:js @@ -57,7 +57,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - run: npm run test:unit:php @@ -69,7 +69,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - name: Build distributable zip @@ -106,7 +106,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - name: Download test zip artifact @@ -139,7 +139,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - name: Download test zip artifact diff --git a/.github/workflows/release-artifact.yml b/.github/workflows/release-artifact.yml index 9118b47..5236b39 100644 --- a/.github/workflows/release-artifact.yml +++ b/.github/workflows/release-artifact.yml @@ -146,7 +146,7 @@ jobs: - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci @@ -244,7 +244,7 @@ jobs: ref: ${{ inputs.tag && format('refs/tags/{0}', inputs.tag) || github.ref }} - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - run: npm run lint:php @@ -260,7 +260,7 @@ jobs: ref: ${{ inputs.tag && format('refs/tags/{0}', inputs.tag) || github.ref }} - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - run: npm run test:unit:php @@ -279,7 +279,7 @@ jobs: ref: ${{ inputs.tag && format('refs/tags/{0}', inputs.tag) || github.ref }} - uses: actions/setup-node@v4 with: - node-version: '20' + node-version: '22' cache: npm - run: npm ci - name: Download built artifact diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..26f1a3f --- /dev/null +++ b/.npmrc @@ -0,0 +1,5 @@ +# Write package-lock.json without "resolved" tarball URLs for registry packages, +# so the lockfile does not pin any particular registry host. "integrity" hashes +# are kept, so every install is still verified; tarballs come from whichever +# registry the installing machine is configured to use. +omit-lockfile-registry-resolved=true diff --git a/.stylelintrc.json b/.stylelintrc.json index 076924a..5e28106 100644 --- a/.stylelintrc.json +++ b/.stylelintrc.json @@ -6,6 +6,8 @@ "no-duplicate-selectors": null, "color-named": [ "never", { "ignore": [ "inside-function" ] } ], "at-rule-no-unknown": null, - "scss/at-rule-no-unknown": true + "scss/at-rule-no-unknown": true, + "property-no-deprecated": null, + "declaration-property-value-keyword-no-deprecated": null } } diff --git a/.wp-env.json b/.wp-env.json index 84f57ac..2b62f86 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -5,5 +5,8 @@ "testsPort": 9190, "plugins": [ "." - ] + ], + "mappings": { + "wp-content/mu-plugins": "./tests/e2e/functional/environment/mu-plugins" + } } diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b307ee..da9d7d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## 0.4.2 + +- Security: Google Fonts used in global styles are now only downloaded from Google's own font servers, and only saved as WOFF2 font files; `http://` and protocol-relative Google Fonts links are upgraded to HTTPS and localised too +- Fixed: malformed font or style values in global styles, such as a numeric font weight or a font families value that is not a list, no longer cause a fatal error when updating global styles +- Fixed: Google Fonts saved locally now load on multisite subsites and on sites with a custom uploads location; previously the font file was saved in the right folder but linked from the wrong address, so it silently failed to load +- Fixed: updating global styles with Google Fonts no longer causes a fatal error on hosts where WordPress writes files over FTP or SSH, and fonts are now saved locally on hosts where only the uploads folder is writable +- Images can now be uploaded from addresses that do not end in a file extension, such as CDN and image-proxy links; previously these were refused before the image was even downloaded +- Updated development and build dependencies to clear security advisories in the toolchain; the compiled block bundle is rebuilt as a result +- Security: the post, page and media tools now check permission on each individual item, so users can only read or change content their WordPress role allows + ## 0.4.1 - The download no longer carries test-only helper classes that were never used at runtime, making the plugin slightly smaller diff --git a/README.md b/README.md index d438f48..fe1a01e 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ |---|---| | **Plugin name** | Airo WP AI Builder | | **Text domain** | `airo-wp` | -| **Version** | 0.4.1 | +| **Version** | 0.4.2 | | **Requires WordPress** | 6.9+ | | **Requires PHP** | 7.4+ | | **License** | [GPLv2 or later](https://www.gnu.org/licenses/gpl-2.0.html) | diff --git a/airo-wp.php b/airo-wp.php index 568a229..431fd84 100644 --- a/airo-wp.php +++ b/airo-wp.php @@ -3,7 +3,7 @@ * Plugin Name: Airo WP AI Builder * Plugin URI: https://github.com/godaddy-wordpress/airo-wp * Description: MCP server and block pattern library for AI-powered site building. - * Version: 0.4.1 + * Version: 0.4.2 * Requires at least: 6.9 * Requires PHP: 7.4 * Author: GoDaddy @@ -47,7 +47,7 @@ defined( 'ABSPATH' ) || exit; if ( ! defined( 'AIRO_WP_VERSION' ) ) { - define( 'AIRO_WP_VERSION', '0.4.1' ); + define( 'AIRO_WP_VERSION', '0.4.2' ); } if ( ! defined( 'AIRO_WP_PLUGIN_FILE' ) ) { define( 'AIRO_WP_PLUGIN_FILE', __FILE__ ); diff --git a/data/frontend-extensions.json b/data/frontend-extensions.json new file mode 100644 index 0000000..ba824ae --- /dev/null +++ b/data/frontend-extensions.json @@ -0,0 +1,64 @@ +{ + "block-animations": { + "needles": [ "has-airo-wp-animation", "data-airo-wp-animation-enabled", "data-airo-wp-svg-draw" ], + "attributes": [ "dsgoAnimationEnabled", "dsgoSvgDraw" ], + "scripts": [ "src/extensions/block-animations/frontend.js", "src/extensions/block-animations/svg-draw.js" ], + "style": "src/extensions/block-animations/animations.scss" + }, + "background-video": { + "needles": [ "airo-wp-has-video-background" ], + "attributes": [ "dsgoVideoUrl" ], + "scripts": [ "src/extensions/background-video/frontend.js" ], + "style": "src/extensions/background-video/style.scss" + }, + "clickable-group": { + "needles": [ "airo-wp-clickable" ], + "attributes": [ "dsgoLinkUrl" ], + "scripts": [ "src/extensions/clickable-group/frontend.js" ], + "style": "src/extensions/clickable-group/styles.scss" + }, + "text-reveal": { + "needles": [ "has-airo-wp-text-reveal", "data-airo-wp-text-reveal-enabled" ], + "attributes": [ "dsgoTextRevealEnabled" ], + "scripts": [ "src/extensions/text-reveal/frontend.js" ], + "style": "src/extensions/text-reveal/style.scss" + }, + "expanding-background": { + "needles": [ "has-airo-wp-expanding-background", "data-airo-wp-expanding-bg-enabled" ], + "attributes": [ "dsgoExpandingBgEnabled" ], + "scripts": [ "src/extensions/expanding-background/frontend.js" ], + "style": "src/extensions/expanding-background/style.scss" + }, + "vertical-scroll-parallax": { + "needles": [ "data-airo-wp-parallax-enabled" ], + "attributes": [ "dsgoParallaxEnabled" ], + "scripts": [ "src/extensions/vertical-scroll-parallax/frontend.js" ] + }, + "hover-effects": { + "needles": [ "airo-wp-hover-effect" ], + "style": "src/extensions/hover-effects/styles.scss" + }, + "reveal-control": { + "needles": [ "airo-wp-has-reveal" ], + "attributes": [ "dsgoRevealOnHover" ], + "style": "src/extensions/reveal-control/styles.scss" + }, + "svg-patterns": { + "needles": [ "has-airo-wp-svg-pattern" ], + "attributes": [ "dsgoSvgPatternEnabled" ], + "style": "src/extensions/svg-patterns/style.scss" + }, + "grid-span": { + "needles": [ "airo-wp-grid__inner" ], + "style": "src/extensions/grid-span/style.scss" + }, + "grid-mobile-order": { + "needles": [ "airo-wp-grid__inner" ], + "style": "src/extensions/grid-mobile-order/style.scss" + }, + "interactions": { + "needles": [ "data-airo-wp-interactions" ], + "attributes": [ "dsgoInteractions" ], + "style": "src/extensions/interactions/style.scss" + } +} diff --git a/data/icon-svg-library.php b/data/icon-svg-library.php index ed54f7f..74e0515 100644 --- a/data/icon-svg-library.php +++ b/data/icon-svg-library.php @@ -279,7 +279,7 @@ function airowp_get_icon_aliases() { return $aliases; } - $file = dirname( dirname( __DIR__ ) ) . '/data/icon-aliases.json'; + $file = __DIR__ . '/icon-aliases.json'; if ( ! file_exists( $file ) ) { $aliases = array(); @@ -327,17 +327,17 @@ function airowp_accordion_render_icon( $icon_style, $is_open ) { switch ( $icon_style ) { case 'plus-minus': if ( $is_open ) { - $icon_svg = ''; + $icon_svg = ''; } else { - $icon_svg = ''; + $icon_svg = ''; } break; case 'caret': - $icon_svg = ''; + $icon_svg = ''; break; case 'chevron': default: - $icon_svg = ''; + $icon_svg = ''; } return ''; diff --git a/data/layout-support.json b/data/layout-support.json new file mode 100644 index 0000000..78182c9 --- /dev/null +++ b/data/layout-support.json @@ -0,0 +1,332 @@ +{ + "devices": { + "desktop": 0, + "tablet": 1024, + "mobile": 767 + }, + "blocks": { + "airo-wp/section": { + "role": "flex", + "inner": ".airo-wp-stack__inner" + }, + "airo-wp/row": { + "role": "flex", + "inner": ".airo-wp-flex__inner" + }, + "airo-wp/grid": { + "role": "grid", + "inner": ".airo-wp-grid__inner" + }, + "core/group": { + "role": "item", + "inner": "" + }, + "core/paragraph": { + "role": "item", + "inner": "" + }, + "core/heading": { + "role": "item", + "inner": "" + }, + "core/image": { + "role": "item", + "inner": "" + }, + "core/buttons": { + "role": "item", + "inner": "" + }, + "core/button": { + "role": "item", + "inner": "" + }, + "core/separator": { + "role": "item", + "inner": "" + }, + "core/spacer": { + "role": "item", + "inner": "" + }, + "airo-wp/icon-button": { + "role": "item", + "inner": "" + }, + "airo-wp/card": { + "role": "item", + "inner": "" + }, + "airo-wp/dynamic-image": { + "role": "item", + "inner": "" + } + }, + "properties": { + "width": { + "css": "width", + "type": "size", + "scope": "all", + "target": "root" + }, + "minWidth": { + "css": "min-width", + "type": "size", + "scope": "all", + "target": "root" + }, + "maxWidth": { + "css": "max-width", + "type": "size", + "scope": "all", + "target": "root" + }, + "height": { + "css": "height", + "type": "size", + "scope": "all", + "target": "root" + }, + "minHeight": { + "css": "min-height", + "type": "size", + "scope": "all", + "target": "root" + }, + "maxHeight": { + "css": "max-height", + "type": "size", + "scope": "all", + "target": "root" + }, + "aspectRatio": { + "css": "aspect-ratio", + "type": "ratio", + "scope": "all", + "target": "root" + }, + "overflow": { + "css": "overflow", + "type": "enum", + "scope": "all", + "target": "root", + "values": [ + "visible", + "hidden", + "clip", + "auto", + "scroll" + ] + }, + "position": { + "css": "position", + "type": "enum", + "scope": "all", + "target": "root", + "values": [ + "static", + "relative", + "absolute" + ] + }, + "top": { + "css": "top", + "type": "offset", + "scope": "all", + "target": "root" + }, + "right": { + "css": "right", + "type": "offset", + "scope": "all", + "target": "root" + }, + "bottom": { + "css": "bottom", + "type": "offset", + "scope": "all", + "target": "root" + }, + "left": { + "css": "left", + "type": "offset", + "scope": "all", + "target": "root" + }, + "zIndex": { + "css": "z-index", + "type": "integer", + "scope": "all", + "target": "root" + }, + "direction": { + "css": "flex-direction", + "type": "enum", + "scope": "flex", + "target": "inner", + "values": [ + "row", + "column", + "row-reverse", + "column-reverse" + ] + }, + "wrap": { + "css": "flex-wrap", + "type": "enum", + "scope": "flex", + "target": "inner", + "values": [ + "nowrap", + "wrap", + "wrap-reverse" + ] + }, + "justifyContent": { + "css": "justify-content", + "type": "enum", + "scope": "container", + "target": "inner", + "values": [ + "normal", + "start", + "end", + "center", + "flex-start", + "flex-end", + "space-between", + "space-around", + "space-evenly" + ] + }, + "alignItems": { + "css": "align-items", + "type": "enum", + "scope": "container", + "target": "inner", + "values": [ + "normal", + "stretch", + "start", + "end", + "center", + "flex-start", + "flex-end", + "baseline" + ] + }, + "gap": { + "css": "gap", + "type": "gap", + "scope": "container", + "target": "inner" + }, + "rowGap": { + "css": "row-gap", + "type": "gap", + "scope": "container", + "target": "inner" + }, + "columnGap": { + "css": "column-gap", + "type": "gap", + "scope": "container", + "target": "inner" + }, + "paddingTop": { + "css": "padding-top", + "type": "length", + "scope": "container", + "target": "root" + }, + "paddingRight": { + "css": "padding-right", + "type": "length", + "scope": "container", + "target": "root" + }, + "paddingBottom": { + "css": "padding-bottom", + "type": "length", + "scope": "container", + "target": "root" + }, + "paddingLeft": { + "css": "padding-left", + "type": "length", + "scope": "container", + "target": "root" + }, + "contentWidth": { + "css": "max-width", + "type": "size", + "scope": "container", + "target": "inner" + }, + "gridTemplateRows": { + "css": "grid-template-rows", + "type": "tracks", + "scope": "grid", + "target": "inner" + }, + "gridTemplateAreas": { + "css": "grid-template-areas", + "type": "areas", + "scope": "grid", + "target": "inner" + }, + "flexBasis": { + "css": "flex-basis", + "type": "size", + "scope": "all", + "target": "root" + }, + "flexGrow": { + "css": "flex-grow", + "type": "number", + "scope": "all", + "target": "root" + }, + "flexShrink": { + "css": "flex-shrink", + "type": "number", + "scope": "all", + "target": "root" + }, + "alignSelf": { + "css": "align-self", + "type": "enum", + "scope": "all", + "target": "root", + "values": [ + "auto", + "normal", + "stretch", + "start", + "end", + "center", + "flex-start", + "flex-end", + "baseline" + ] + }, + "gridColumn": { + "css": "grid-column", + "type": "line", + "scope": "all", + "target": "root" + }, + "gridRow": { + "css": "grid-row", + "type": "line", + "scope": "all", + "target": "root" + }, + "gridArea": { + "css": "grid-area", + "type": "identifier", + "scope": "all", + "target": "root" + } + } +} diff --git a/eslint.config.js b/eslint.config.js new file mode 100644 index 0000000..99f48ba --- /dev/null +++ b/eslint.config.js @@ -0,0 +1,100 @@ +/** + * ESLint flat config. + * + * @wordpress/scripts (35 and later) pulls ESLint 10, which no longer reads `.eslintrc.*` + * or `.eslintignore`. This flat config replaces the former `.eslintrc.js` and + * `.eslintignore` so the DSG source in `src/` — which airo-wp cannot modify — + * keeps linting under the same rule relaxations it did before. It uses + * @wordpress/eslint-plugin's native flat preset (its `.eslintrc` wrapper is + * deprecated and cannot be loaded under ESLint 10 via FlatCompat). + */ +const wordpress = require( '@wordpress/eslint-plugin' ); +const globals = require( 'globals' ); + +module.exports = [ + { + ignores: [ + 'dist/', + 'builds/', + 'dependencies/', + 'node_modules/', + '.wp-env/', + 'vendor/', + 'playwright-report/', + 'test-results/', + // tests/ holds Node lane-runner scripts (.mjs) + Playwright specs, + // not block source. lint:js is scoped to src/, so tests/ is never a + // release-gate target; ignoring it stops `lint-js --fix` from + // over-scanning and rewriting these files. + 'tests/', + // Root build/tooling config — airo-wp-owned, not DSG block source. + // `lint-js --fix src/` otherwise silently reformats it on every + // dsg-sync. + 'webpack.config.js', + 'eslint.config.js', + ], + }, + ...wordpress.configs.recommended, + { + rules: { + 'import/no-extraneous-dependencies': 'off', + 'import/no-unresolved': 'off', + 'jsdoc/require-param-description': 'off', + // src/ is 100% DSG-owned and read-only (see CLAUDE.md): airo-wp never + // authors JS here, so lint must accept DSG's own conventions rather than + // error on source it cannot modify. The parent (DesignSetGo) lints this + // same source clean on an older, unpinned @wordpress/eslint-plugin where + // these rules were warnings; newer versions promote them to errors. + '@wordpress/no-unsafe-wp-apis': 'off', + '@wordpress/no-unused-vars-before-return': 'off', + 'jsdoc/require-param-type': 'off', + 'jsdoc/require-returns-description': 'off', + 'jsdoc/check-line-alignment': 'off', + 'no-nested-ternary': 'off', + // ESLint 9 changed these defaults (`caughtErrors` now "all", `builtinGlobals` + // now true). The DSG source is written against the old ones and is not + // editable here, so restore them. + 'no-unused-vars': [ + 'error', + { ignoreRestSiblings: true, caughtErrors: 'none' }, + ], + 'no-redeclare': [ 'error', { builtinGlobals: false } ], + 'jsdoc/no-undefined-types': [ + 'error', + { + definedTypes: [ + 'JSX', + 'Element', + 'HTMLElement', + 'HTMLImageElement', + 'IntersectionObserver', + 'NodeList', + 'KeyboardEvent', + 'Document', + ], + }, + ], + }, + }, + { + files: [ 'tests/**/*.js', '**/*.test.js', '**/*.spec.js' ], + languageOptions: { + globals: { + ...globals.jest, + }, + }, + rules: { + // Must restate the main block's ESLint-9-default restorations: a later block's options replace, not merge. + 'no-unused-vars': [ + 'error', + { + varsIgnorePattern: '^_', + argsIgnorePattern: '^_', + ignoreRestSiblings: true, + caughtErrors: 'none', + }, + ], + 'jsx-a11y/label-has-associated-control': 'off', + }, + }, +]; diff --git a/functions/blocks/field-render-helpers.php b/functions/blocks/field-render-helpers.php index da71d7e..29a60ac 100644 --- a/functions/blocks/field-render-helpers.php +++ b/functions/blocks/field-render-helpers.php @@ -27,8 +27,16 @@ /** * Build the flex-basis / max-width inline style for a field wrapper. * - * Mirrors the pre-dynamic save() output so field layout is unchanged. The - * calc() keeps a half-gap between side-by-side fields. + * The fields container is a wrapping flex row with + * `gap: var(--airo-wp-form-field-spacing)`. For N equal columns of P% each, + * the row holds N-1 gaps, so every field must give up gap * (N-1)/N, which + * is gap * (100 - P) / 100. The old "half a gap" only fit two columns: three + * 33% fields overflowed and the third wrapped. 33 and 66 map to exact + * thirds so three (or 66 + 33) fields fill the row. + * + * Keep in sync with getFormFieldWidth() in + * src/blocks/form-builder/utils/field-width.js (editor preview); both are + * checked against tests/fixtures/form-field-width-cases.json. * * @param string|int $field_width Width percentage (e.g. '100', '50'). * @return string CSS declarations (no trailing semicolon). @@ -44,11 +52,56 @@ function airowp_form_field_width_style( $field_width ) { return 'flex-basis:100%;max-width:100%'; } - $calc = 'calc(' . $field_width . '% - var(--airo-wp-form-field-spacing, 1.5rem) / 2)'; + $percent_map = array( + '33' => '33.3333', + '66' => '66.6667', + ); + $percent = isset( $percent_map[ $field_width ] ) ? $percent_map[ $field_width ] : $field_width; + + // Share of one gap each field gives up, rounded to 4 decimals with + // trailing zeros trimmed (50 -> 0.5, 33.3333 -> 0.6667). + $gap_factor = rtrim( rtrim( number_format( ( 100 - (float) $percent ) / 100, 4, '.', '' ), '0' ), '.' ); + + $calc = 'calc(' . $percent . '% - var(--airo-wp-form-field-spacing, 1.5rem) * ' . $gap_factor . ')'; return 'flex-basis:' . $calc . ';max-width:' . $calc; } } +if ( ! function_exists( 'airowp_form_field_wrapper_attributes' ) ) { + /** + * Build a form field's wrapper attributes. + * + * Adds `data-airo-wp-field` (the field name) to every field and, when the + * field has conditional logic, the `airo-wp-form-field--conditional` class and + * its sanitized rules as `data-airo-wp-conditions` for the view script. + * get_block_wrapper_attributes() escapes every value. + * + * @param array $attributes Block attributes. + * @param string $type Field type slug for the BEM modifier (e.g. 'text'). + * @param string $style Inline style ('' for none). + * @return string Wrapper attributes. + */ + function airowp_form_field_wrapper_attributes( array $attributes, $type, $style = '' ) { + $extra = array( 'class' => 'airo-wp-form-field airo-wp-form-field--' . $type ); + if ( '' !== $style ) { + $extra['style'] = $style; + } + + $field_name = isset( $attributes['fieldName'] ) ? (string) $attributes['fieldName'] : ''; + if ( '' !== $field_name ) { + $extra['data-airo-wp-field'] = $field_name; + } + + $rules = \airo - wp\Blocks\Form_Conditions::normalize_rules( isset( $attributes['dsgoConditions'] ) ? $attributes['dsgoConditions'] : null ); + if ( null !== $rules ) { + $extra['class'] .= ' airo-wp-form-field--conditional'; + $extra['data-airo-wp-conditions'] = wp_json_encode( $rules ); + } + + return get_block_wrapper_attributes( $extra ); + } +} + if ( ! function_exists( 'airowp_form_field_label_html' ) ) { /** * Build a field