API

Flynt (namespace)

This is the main namespace of the plugin. In your theme you should only need to call the functions contained in the namespace. The following public functions are available:

initDefaults

function initDefaults()

Initializes a set of defaults used for regular setups.

registerComponent

function registerComponent(string $componentName, string $componentPath = null)

Registers a component for later use. If no $componentPath is specified, the default will be taken.

function registerComponents(array $components = [])

Registers an array of components for later use. The array can consist of component names only (as values), or with the component name as key and component path as value.

(echo|get)HtmlFromConfig

function echoHtmlFromConfig(array $config)
function getHtmlFromConfig(array $config)

Return and optionally echo the HTML generated from processing a given configuration array.

(echo|get)HtmlFromConfigFile

function echoHtmlFromConfigFile(string $fileName)
function getHtmlFromConfigFile(string $fileName)

Return and optionally echo the HTML generated from processing a given configuration file.

ComponentManager (class)

Singleton used internally to manage registered components.

getInstance (static)

Get the singleton of the ComponentManager.

public static function getInstance()

registerComponent

Register a component for later use.

public function registerComponent(string $componentName, string $componentPath = null)

getComponentFilePath

Get the path to a component specific file.

public function getComponentFilePath(string $componentName, string $fileName = 'index.php')

getAll

Get all registered Components.

public function getAll()

removeAll

Remove all registered Components.

public function removeAll()

BuildConstructionPlan (class)

Used internally to create a complete construction plan from a minimal config.

fromConfig (static)

Build the construction plan from a config array.

public static function fromConfig (array $config)

fromConfigFile (static)

Build the construction plan from a config file. Uses filters Flynt/configPath and Flynt/configFileLoader to determine file path and loading logic.

public static function fromConfigFile (string $configFileName)

Render (class)

Used internally to render a construction plan.

fromConstructionPlan (static)

Render HTML from a given construction plan.

public static function fromConstructionPlan (array $constructionPlan)

WordPress Filters

Flynt/componentPath

Modify the path of a component.

Arguments for callable:

$componentPath
the path of the component

$componentName
the name of the component

Default:

add_filter('Flynt/componentPath', ['Flynt\Defaults', 'setComponentPath'], 999, 2);
namespace Flynt;

class Defaults
{
    public static function setComponentPath($componentPath, $componentName)
    {
        if (is_null($componentPath)) {
            $componentPath = self::getComponentsDirectory() . '/' . $componentName;
        }
        return $componentPath;
    }
}

Flynt/configPath

Modify the path of the config files that can be specified in (echo|get)HtmlFromConfigFile

Arguments for callable:

$configPath
the path of the config file

$configFileName
the name of the config file

Default:

add_filter('Flynt/configPath', ['Flynt\Defaults', 'setConfigPath'], 999, 2);
namespace Flynt;

class Defaults
{
    public static function setConfigPath($configPath, $configFileName)
    {
        if (is_null($configPath)) {
            $configPath = get_template_directory() . '/' . self::CONFIG_DIR . '/' . $configFileName;
        }
        return $configPath;
    }
}

Flynt/configFileLoader

Modify the logic of loading a config file. Default loads json files. Can be used to load other formats like yaml.

Arguments for callable:

$config
the loaded config. null by default

$configName
the path of the config file

$configPath
the name of the config file

Default:

add_filter('Flynt/configFileLoader', ['Flynt\Defaults', 'loadConfigFile'], 999, 3);
namespace Flynt;

class Defaults
{
    public static function loadConfigFile($config, $configName, $configPath)
    {
        if (is_null($config)) {
            $config = json_decode(file_get_contents($configPath), true);
        }
        return $config;
    }
}

Flynt/initComponentConfig

Modify the config unsed to build the construction plan.

Component specific filter: Flynt/initComponentConfig?name={$config['name']}

Flynt/addComponentData

Final point to modify the data of a component. Called after the data filters and adding custom data. This is the default place to do data manipulation and preparation before passing it to the render function.

Component specific filter: Flynt/addComponentData?name={$config['name']}

Arguments for callable:

$data
the component’s data that will be used for rendering

$parentData
the component’s parent’s data

$config
entire config of the component

Example:

add_filter('Flynt/addComponentData?name=PageHeader', function ($data, $parentData) {
    if (!empty($parentData['post_thumbnail']) && array_key_exists('url', $parentData['post_thumbnail'])) {
      $data['image'] = $parentData['post_thumbnail']['url'];
    }
    return $data;
}, 10, 2);

Flynt/dynamicSubcomponents?name={$config[‘name’]}

Modify the component’s areas. Can be used to dynamically add sub components based on data comming from the data filter.

Arguments for callable:

$areas
the rendered HTML

$componentData
the component’s data

$parentData
the component’s parent’s data

Example:

add_filter('Flynt/dynamicSubcomponents?name=FlexibleContent', function ($areas, $data, $parentData) {
    $fieldGroup = $data['fieldGroup'];
    if (array_key_exists($fieldGroup, $parentData) && $parentData[$fieldGroup] !== false) {
        $areas['flexibleContent'] = array_map(function ($field) use ($parentData) {
            return [
                'name' => ucfirst($field['acf_fc_layout']),
                'customData' => $field,
                'parentData' => $parentData // overwrite parent data of child components
            ];
        }, $parentData[$data['fieldGroup']]);
    }
    return $areas;
}, 10, 3);

Flynt/renderComponent

Specify the way how components or a single component should be rendered.

Component specific filter: Flynt/renderComponent?name={$componentName}

Arguments for callable:

$output
the rendered HTML

$componentName
the name of the component

$componentData
the component’s data

$areaHtml
the rendered HTML of the component’s areas

Default:

add_filter('Flynt/renderComponent', ['Flynt\Defaults', 'renderComponent'], 999, 4);
namespace Flynt;

class Defaults
{
    public static function renderComponent($output, $componentName, $componentData, $areaHtml)
    {
        if (is_null($output)) {
            $componentManager = ComponentManager::getInstance();
            $filePath = $componentManager->getComponentFilePath($componentName);
            $output = self::renderFile($componentData, $areaHtml, $filePath);
        }
        return $output;
    }
}

Actions

Flynt/registerComponent

Exectuted when any or a specific component is registered. Can be used to load additional files.

Component specific action: Flynt/registerComponent?name={$config['name']}

Arguments for callable:

$componentPath
the path of the component

$componentName*
the name of the component (*not available in component specific action)

Defaults:

add_action('Flynt/registerComponent', ['Flynt\Defaults', 'checkComponentFolder']);
add_action('Flynt/registerComponent', ['Flynt\Defaults', 'loadFunctionsFile']);
namespace Flynt;

class Defaults
{
    public static function checkComponentFolder($componentPath)
    {
        if (!is_dir($componentPath)) {
            trigger_error("Register Component: Folder {$componentPath} not found!", E_USER_WARNING);
        }
    }
    public static function loadFunctionsFile($componentPath)
    {
        $filePath = $componentPath . '/functions.php';
        if (file_exists($filePath)) {
          require_once $filePath;
        }
    }
}