Code Snippets for WordPress

repository·core-beta·Indexed 43 days ago

https://github.com/codesnippetspro/code-snippets

A 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.

Tokens
8.7K
Snippets
24
Records
34
Agent score
56%

What's inside Code Snippets

  1. Overview of Code Snippets WordPress plugin

    core-beta
    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.
  2. ESLint configuration for code-snippets

    core-beta

    The 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-eslint with strict and stylistic type-checked configurations.
    • Stylistic Enforcement: Uses @stylistic/eslint-plugin for formatting (e.g., indentation, quotes, line breaks).
    • React Support: Includes eslint-plugin-react and eslint-plugin-react-hooks with automatic version detection.
    • Accessibility: Enforces jsx-a11y rules for accessible JSX.
    • Import Management: Uses eslint-plugin-import for organized and valid module imports.
    • SVG Support: Uses eslint-plugin-svg-jsx for SVG-in-JSX handling.
  3. Manage AI Agent Demo progress with DemoStage

    core-beta

    The AI agent demo follows a linear, ordered sequence of stages defined in DEMO_STAGES. You can track progress using stageIndex or check if a specific milestone has been met using hasReached.

    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);
  4. Configure Playwright 'use' options for traces, screenshots, and video

    core-beta

    The test suite is configured to capture debugging information automatically upon failure. These settings are applied globally via the use object in playwright.config.ts:

    OptionValueDescription
    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.
  5. Configure Stylelint for SCSS and Stylistic rules

    core-beta

    This project uses a stylelint.config.mjs configuration file that extends stylelint-config-standard-scss. It is configured to support SCSS syntax via postcss-scss and utilizes several plugins to enforce specific coding styles and logical properties.

    Key Configuration Details:

    • Syntax: Uses postcss-scss for parsing SCSS files.
    • Plugins: Includes stylelint-scss, stylelint-use-logical, and @stylistic/stylelint-plugin.
    • Auto-fix: The fix: true setting 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-logical is set to 'always'.
    • SCSS Specifics: scss/at-rule-no-unknown is enabled (true), while the standard at-rule-no-unknown is disabled (null) to prevent conflicts.
    • CSS Functions: color-function-notation is set to 'modern'.
    • Font Families: font-family-no-missing-generic-family-keyword is enabled but ignores 'dashicons'.
    • Patterns: selector-id-pattern and selector-class-pattern are 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
    }
  6. Configure the base URL via WP_E2E_BASE_URL

    core-beta

    The Playwright test suite uses a baseURL for resolving relative paths. By default, it points to http://localhost:8888. You can override this by setting the WP_E2E_BASE_URL environment variable. This is useful when running in environments where the port might differ (e.g., when using wp-env).

    export const WP_E2E_BASE_URL = 'http://your-custom-url:port'
  7. Configure REST API endpoints via window.CODE_SNIPPETS

    core-beta

    The REST API base URLs are derived from the global window.CODE_SNIPPETS object. This allows the application to dynamically resolve endpoints for snippets, imports, preferences, and cloud services. If you are integrating with this system, ensure the restAPI object is populated on the window.CODE_SNIPPETS object 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'
        }
      }
    };
  8. Configure Playwright test execution and reporting

    core-beta

    The 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 2 in CI and 1 locally.
    • Workers: Fixed at 1 worker.
    • Parallelism: fullyParallel is enabled.
    • Timeout: Global test timeout is set to 60000ms (60 seconds).
    • Forbid Only: If process.env.CI is present, test.only is forbidden to prevent accidental local-only tests from running in CI.

    Reporting

    • Local: Generates html, json (to test-results/results.json), and junit (to test-results/results.xml) reports.
    • CI: Generates line, html, json, and junit reports.
  9. Configure ESLint ignores

    core-beta

    The 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 ignores array to prevent linting errors in non-source files.

    Ignored patterns:

    • bundle/*
    • src/dist/*
    • src/vendor/*
    • svn/*
    • *.config.mjs and *.config.js
    • .*/* (hidden directories/files)
    • tmp/*
    • playwright-report/*
  10. Install Code Snippets via WordPress Admin

    core-beta

    To install Code Snippets automatically:

    1. Log into your WordPress admin dashboard.
    2. Navigate to Plugins > Add New.
    3. Search for Code Snippets.
    4. Click Install Now under the
  11. Strip wrapper tags from code snippets

    core-beta

    The stripWrapperTags function removes surrounding markup from a code snippet to ensure it is stored in its bare form. It handles two types of wrappers:

    1. Markdown Code Fences: Removes triple-backtick fences (e.g., ```js).
    2. Language-specific Tags: Removes HTML-style tags that wrap the entire snippet (e.g., <script> for JavaScript, <style> for CSS, or <?php for 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 StripResult object containing the cleaned code and a description of what was removed.

    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"