PHP examples
Runnable PHP samples for the CoverKit plugin. They are meant for development: load them from Media → CoverKit Examples in wp-admin, or require the helpers from tests.
Admin examples page
Section titled “Admin examples page”- Class:
CoverKit\Examples—includes/class-coverkit-examples.php - Menu: Media → CoverKit Examples (
manage_options). - Behavior: Lists every
*.phpfile inexamples/exceptconstants.phpandexample-bootstrap.php. Selecting a file includes it and shows its output (or invokes a matchingcoverkit_*,example_*, ortest_*function if the file defines one but prints nothing). Errors are caught and shown as admin notices.
Build / shipping
Section titled “Build / shipping”- Define
COVERKIT_LOAD_EXAMPLESasfalse(e.g. inwp-config.php) to disable the admin page. - Release builds may omit the entire
examples/directory; the rest of the plugin must notrequirethose files (only the gatedExamplesclass references that folder).
Shared helpers
Section titled “Shared helpers”| File | Role |
|---|---|
examples/example-bootstrap.php | coverkit_examples_resolve_ids() returns the first published coverkit template ID and a published post ID; coverkit_examples_resolve_ids_clear_cache() clears the cache (tests). |
examples/constants.php | Defines COVERKIT_EXAMPLE_TEMPLATE_ID, COVERKIT_EXAMPLE_POST_ID, and alternate IDs when not already defined (uses the bootstrap resolver). |
Example scripts should start with:
defined( 'ABSPATH' ) || exit;require_once __DIR__ . '/constants.php';Use fully qualified classes (e.g. \CoverKit\Renderer::generate()) unless the file lives in the CoverKit namespace (prefer global functions + \CoverKit\ for consistency with the admin loader).
Renderer entry points (as used in examples)
Section titled “Renderer entry points (as used in examples)”CoverKit\Renderer::generate( $use_case_id, $template_id, $post_id, $options )— primary generation keyed by use case id (opengraph, custom ids).CoverKit\Renderer::generate_from_content( $content, $post_id, $meta, $options )— template post ID, block content, or block object; preferred for previews and direct template runs.
Example files (overview)
Section titled “Example files (overview)”| File | Purpose |
|---|---|
simple-demo.php | Registry smoke test, template apply_data, and Renderer::generate. |
use-case-usage.php | Runnable OpenGraph sample; custom Use_Case registration shown as a copy-paste init snippet (not executed here). |
renderer-usage.php | Walkthrough: Renderer::generate vs generate_from_content, canvas string; coverkit_after_generate as copy-paste snippet only. |
field-mapping-demo.php | _coverkit_use_cases meta + generation (writes template meta — dev only). |
example-usage.php | Usage “cookbook”: use case meta, hooks, batch/cache/error patterns as escaped snippets; one live Renderer::generate demo (no update_option or hook registration on load). |
example-template.php | Reference layout for new example files (docblock, constants, one coverkit_* entry). |
apply-raw-data-test.php | Short demo replacing removed apply_raw_data() with generate_from_content(). |
rest-api-image-test.php | REST / content-string style generation. |
canvas-srcset-test.php | Canvas / responsive image experiments (explicit bottom invoke). |
test-implementation.php | Implementation checks (typed helpers, \CoverKit\ class references). |
feature-image-refactor-test.php | Feature image on-demand collection; creates/deletes temporary posts (explicit bottom invoke). |
PHPUnit reuses example-bootstrap.php (see tests/php/ExampleBootstrapTest.php) and asserts Renderer::generate exists (RendererGenerateApiTest.php). Full image generation against a real DB is optional and not part of the default stub bootstrap.