SCI WP Framework

website·Indexed 32 days ago

https://sciwp.com/

An MVC framework for WordPress plugin development. It provides a service container for dependency management, a router for defining standard and REST API routes, an ORM for defining models and executing database queries, and tools for managing plugins, options, views, and assets.

Tokens
23.6K
Snippets
137
Records
166
Agent score
62%

What's inside SCI WP

  1. Overview of SCI WP Framework

    SCI WP Framework provides a modern MVC (Model-View-Controller) architecture for WordPress plugin development. It introduces standard PHP framework functionalities to WordPress, such as routing, autoloading, and dependency injection, without replacing core WordPress functions. It can be used as a standalone official plugin or bundled directly within a custom plugin to allow third-party extensibility.
  2. Implement MVC and Dependency Injection in SCi WP

    SCi WP provides a modern PHP framework structure for WordPress plugins, implementing the MVC (Model-View-Controller) pattern to separate business logic (services), data (models), and presentation (views). It includes built-in support for Autoloading and Dependency Injection.
  3. Handle optional route parameters

    To make a route parameter optional, append a question mark (?) to the parameter name within the braces. Ensure the corresponding action function provides a default value for that parameter to avoid errors.
    // Matches both /cars and /cars/some-model
    Route::create('get', '/cars/{model?}', function($model = false) {
        // $model will be false if not provided in the URL
    });
  4. Retrieve records using findAll and find

    Retrieve all records using findAll() or its alias all(). Both methods support optional $skip and $limit parameters for pagination. To retrieve specific records based on constraints, use the find() method, passing an array containing the field, comparison operator, and value.
    // Retrieve all records
    $orcs = App\Models\Orc::findAll();
    
    // Paginated retrieval: skip 20, limit 10
    $orcs = App\Models\Orc::findAll(20, 10);
    // OR
    $orcs = App\Models\Orc::all(20, 10);
    
    // Retrieve by ID
    $orc = App\Models\Orc::find(2);
    
    // Retrieve with constraints: strength < 20
    $orcs = App\Models\Orc::find(['strength', '<', 20]);
  5. Bundle SCi WP within a distributable plugin

    SCi WP can be bundled into a third-party plugin for distribution using three lines of code. When the plugin is enabled, the framework's namespaces are automatically replaced. This setup also allows other plugins that extend the parent plugin to utilize the framework.
  6. Install SCI WP Framework as a WordPress plugin

    The simplest way to install the framework is as a standalone WordPress plugin:

    1. Download the latest release from GitHub.
    2. Extract the directory into your WordPress plugins folder.
    3. Rename the directory to sciwp (the plugin will attempt to auto-rename if this is not done).
    4. Enable the plugin from the WordPress administration interface.
  7. Create and register an Addon plugin

    To create an Addon (a plugin that requires a main framework plugin to be installed), you must define a unique namespace and register the plugin using the main plugin's Plugin::create method. Two plugins cannot share the same base namespace. You can either create and register the plugin in two steps or combine them into a single line.
    // Option 1: Two-step registration
    namespace MyAddonPlugin;
    
    $myAddonPlugin = \MyPlugin\Sci\Plugin::create(__FILE__);
    $myAddonPlugin->register();
    
    // Option 2: Single-line registration
    namespace MyAddonPlugin;
    
    \MyPlugin\Sci\Plugin::create(__FILE__)->register();
  8. Initialize a Sci Support Collection

    Collections in the Sci framework are sets of elements organized in an array format. You can initialize a new Sci\Support\Collection instance using either the static create method or the class constructor. If you are using a bundled version of the framework within a plugin, prepend your plugin namespace to the Sci namespace (e.g., MyPlugin\Sci\Support\Collection).
    use Sci\Support\Collection;
    
    // Using the create method
    $garage = Collection::create(['a1' => 'Ford Focus', 'a2' => 'Renault Megane']);
    
    // Using the constructor
    $garage = new Collection(['a1' => 'Ford Focus', 'a2' => 'Renault Megane']);
  9. Define a database model in SCI WP

    To create a model in SCI WP, extend the Sci\Database\Model class. The framework provides a zero-configuration abstraction where models are automatically linked to database tables based on the class name. If you are using a bundled version of the framework within a plugin, prepend your plugin namespace to the Sci namespace (e.g., MyPlugin\Sci\View).
    namespace MyPlugin\App\Models;
    
    use Sci\Database\Model;
    
    class Orc extends Model
    {
        // Model definition
    }
  10. Retrieve the Option Service instance

    After a plugin is registered, you can retrieve the Option Service instance via the PluginManager.
    namespace MyPlugin;
    
    use Sci\Plugin\Managers\PluginManager;
    
    // Get the Plugin from the Plugin Manager
    $myPlugin = PluginManager::instance()->get('myplugin');
    
    // Get the Option Service
    $optionService = $myPlugin->service('option');
  11. Enqueue JavaScript scripts using the Script class

    The SCI WP framework simplifies script enqueuing via the Script class. Similar to styles, you can create and register scripts in a single chain. You can specify whether the script should be placed in the header or the footer.
    namespace MyPlugin;
    
    use Sci\Asset\Script;
    
    // Create and register a script (defaults to footer = true)
    Script::create(plugins_url('resources/my-script.js'), '1.0')->register('my-script');
    
    // Register only for frontend
    Script::create(plugins_url('resources/my-script.js'), '1.0')->register('my-script', 'front');
    
    // Register only for admin
    Script::create(plugins_url('resources/my-script.js'), '1.0')->register('my-script', 'admin');