Code Snippets for WordPress
repository·core-beta·Indexed 43 days ago
https://github.com/codesnippetspro/code-snippetsA WordPress plugin that provides a graphical interface for managing and executing custom code snippets, allowing developers to extend WordPress functionality without modifying core or theme files directly. Version 4.0.0-beta.12.
What's inside Code Snippets
- Code Snippets is a WordPress plugin designed to manage executable code snippets through a graphical user interface. It allows users to add and manage custom code without manually editing theme files, providing a safer and more organized way to extend WordPress functionality.
ESLint configuration for code-snippets
core-betaThe project uses a flat configuration file (
eslint.config.mjs) based on ESLint's modern configuration system. It integrates TypeScript, React, Accessibility (JSX-A11y), and Stylistic rules to enforce code quality and consistent formatting across the repository.Key features include:
- Strict Type Checking: Uses
typescript-eslintwith strict and stylistic type-checked configurations. - Stylistic Enforcement: Uses
@stylistic/eslint-pluginfor formatting (e.g., indentation, quotes, line breaks). - React Support: Includes
eslint-plugin-reactandeslint-plugin-react-hookswith automatic version detection. - Accessibility: Enforces
jsx-a11yrules for accessible JSX. - Import Management: Uses
eslint-plugin-importfor organized and valid module imports. - SVG Support: Uses
eslint-plugin-svg-jsxfor SVG-in-JSX handling.
- Strict Type Checking: Uses
Manage AI Agent Demo progress with DemoStage
core-betaThe AI agent demo follows a linear, ordered sequence of stages defined in
DEMO_STAGES. You can track progress usingstageIndexor check if a specific milestone has been met usinghasReached.import { DEMO_STAGES, stageIndex, hasReached, type DemoStage } from './path-to-file'; const currentStage: DemoStage = 'planning'; // Check if the user has reached the 'plan-ready' milestone if (hasReached(currentStage, 'plan-ready')) { // Logic for when the plan is ready } // Get the numeric index of the current stage const index = stageIndex(currentStage);Configure Playwright 'use' options for traces, screenshots, and video
core-betaThe test suite is configured to capture debugging information automatically upon failure. These settings are applied globally via the
useobject inplaywright.config.ts:Option Value Description trace'retain-on-failure'Keeps the trace only if the test fails. screenshot'only-on-failure'Captures a screenshot only when a test fails. video'retain-on-failure'Records video and keeps it only if the test fails. baseURL(Dynamic) The base URL for all relative navigation. Configure Stylelint for SCSS and Stylistic rules
core-betaThis project uses a
stylelint.config.mjsconfiguration file that extendsstylelint-config-standard-scss. It is configured to support SCSS syntax viapostcss-scssand utilizes several plugins to enforce specific coding styles and logical properties.Key Configuration Details:
- Syntax: Uses
postcss-scssfor parsing SCSS files. - Plugins: Includes
stylelint-scss,stylelint-use-logical, and@stylistic/stylelint-plugin. - Auto-fix: The
fix: truesetting is enabled, allowing Stylelint to automatically correct fixable violations.
Active Rules:
- Stylistic:
@stylistic/color-hex-case: Set to'lower'.@stylistic/indentation: Set to'tab'.
- Logical Properties:
csstools/use-logicalis set to'always'. - SCSS Specifics:
scss/at-rule-no-unknownis enabled (true), while the standardat-rule-no-unknownis disabled (null) to prevent conflicts. - CSS Functions:
color-function-notationis set to'modern'. - Font Families:
font-family-no-missing-generic-family-keywordis enabled but ignores'dashicons'. - Patterns:
selector-id-patternandselector-class-patternare disabled (null).
/** @type {import('stylelint').Config} */ export default { extends: ['stylelint-config-standard-scss'], plugins: [ 'stylelint-scss', 'stylelint-use-logical', '@stylistic/stylelint-plugin' ], customSyntax: 'postcss-scss', rules: { '@stylistic/color-hex-case': 'lower', '@stylistic/indentation': 'tab', 'at-rule-no-unknown': null, 'csstools/use-logical': 'always', 'color-function-notation': 'modern', 'font-family-no-missing-generic-family-keyword': [true, { ignoreFontFamilies: ['dashicons'] }], 'selector-id-pattern': null, 'selector-class-pattern': null, 'scss/at-rule-no-unknown': true, }, fix: true }- Syntax: Uses
Configure the base URL via WP_E2E_BASE_URL
core-betaThe Playwright test suite uses a
baseURLfor resolving relative paths. By default, it points tohttp://localhost:8888. You can override this by setting theWP_E2E_BASE_URLenvironment variable. This is useful when running in environments where the port might differ (e.g., when usingwp-env).export const WP_E2E_BASE_URL = 'http://your-custom-url:port'Configure REST API endpoints via window.CODE_SNIPPETS
core-betaThe REST API base URLs are derived from the global
window.CODE_SNIPPETSobject. This allows the application to dynamically resolve endpoints for snippets, imports, preferences, and cloud services. If you are integrating with this system, ensure therestAPIobject is populated on thewindow.CODE_SNIPPETSobject before the application initializes.// Example of the expected global configuration structure window.CODE_SNIPPETS = { restAPI: { base: '/wp-json', snippets: '/wp-json/codesnippets/v1/snippets', recentlyActive: '/wp-json/codesnippets/v1/recently-active', importPlugins: '/wp-json/codesnippets/v1/import-plugins', importFiles: '/wp-json/codesnippets/v1/import-files', snippetView: '/wp-json/codesnippets/v1/snippet-view', insightsView: '/wp-json/codesnippets/v1/insights-view', demosSeen: '/wp-json/codesnippets/v1/demos-seen', cloud: { snippets: '/wp-json/codesnippets/v1/cloud/snippets', token: 'YOUR_CLOUD_TOKEN' } } };Configure Playwright test execution and reporting
core-betaThe project uses a custom Playwright configuration that adjusts behavior based on whether it is running in a CI environment (detected via
process.env.CI).Execution Settings
- Retries: Set to
2in CI and1locally. - Workers: Fixed at
1worker. - Parallelism:
fullyParallelis enabled. - Timeout: Global test timeout is set to
60000ms (60 seconds). - Forbid Only: If
process.env.CIis present,test.onlyis forbidden to prevent accidental local-only tests from running in CI.
Reporting
- Local: Generates
html,json(totest-results/results.json), andjunit(totest-results/results.xml) reports. - CI: Generates
line,html,json, andjunitreports.
- Retries: Set to
Configure ESLint ignores
core-betaThe ESLint configuration defines several directories and file patterns to be ignored during linting. If you add new build artifacts or temporary directories, ensure they are added to the
ignoresarray to prevent linting errors in non-source files.Ignored patterns:
bundle/*src/dist/*src/vendor/*svn/**.config.mjsand*.config.js.*/*(hidden directories/files)tmp/*playwright-report/*
Configure assertion timeouts and screenshot diffs
core-betaThe suite defines specific timeouts for assertions and visual regression testing via the
expectconfiguration:- Assertion Timeout:
30000ms (30 seconds). - Visual Regression:
toHaveScreenshotis configured with amaxDiffPixelsof100to allow for minor pixel variations.
expect: { timeout: 30000, toHaveScreenshot: { maxDiffPixels: 100 } }- Assertion Timeout:
Install Code Snippets via WordPress Admin
core-betaTo install Code Snippets automatically:
- Log into your WordPress admin dashboard.
- Navigate to Plugins > Add New.
- Search for Code Snippets.
- Click Install Now under the
Strip wrapper tags from code snippets
core-betaThe
stripWrapperTagsfunction removes surrounding markup from a code snippet to ensure it is stored in its bare form. It handles two types of wrappers:- Markdown Code Fences: Removes triple-backtick fences (e.g.,
```js). - Language-specific Tags: Removes HTML-style tags that wrap the entire snippet (e.g.,
<script>for JavaScript,<style>for CSS, or<?phpfor PHP).
Note that this function only removes wrappers that surround the entire snippet. Tags located partway through the code are treated as part of the author's content and are preserved. This utility ensures the editor view matches the sanitized data that will be stored on the server.
Returns a
StripResultobject containing the cleanedcodeand a description of what wasremoved.import { stripWrapperTags } from './path/to/tags'; const input = "```php\n<?php echo 'hello'; ?>\n```"; const result = stripWrapperTags(input, 'php'); // result.code: "<?php echo 'hello'; ?>" // result.removed: "code fence and opening PHP tag"- Markdown Code Fences: Removes triple-backtick fences (e.g.,