Skip to content

Custom PHP Code

Running custom PHP code must be avoided at all cost, anyway, Xalok Go want to be a SaaS environment and customize certain behaviors require execute complex rules.

Running custom PHP Scripts

The eval PHP function run on the same context it is called then, no context can be injected (easily).

Then, wherever it is necessary to execute PHP code, it can be written:

php
protected function my_closure_context( my_visible_args ... )
{
    $my_visible_response = ...;
    
    ...
    
    $scriptSetting = 'php-page-post-render';
    $phpScript = $this->get('wf_cms_base.settings_manager')->getSetting($scriptSetting);
    if(!is_null($phpScript) && !empty($phpScript->getParameters())) {
        try {
            eval($phpScript->getParameters());
        } catch (\ParseError $ex) {
            $this->get('monolog.logger.scripting')->error(
                'running ' . $scriptSetting . ' script',
                 preg_split("/(\r\n|\n|\r)/", $ex));
        }
    }
    
    ...
    
    return $my_visible_response;
}

The closure could be static (e.g. to hide the class context) but it is recommended to be inside a specific function (the closure).

Newsletter article render PHP Scripts

Using the logic stated above an additional renderer script has been created to allow custom module treatment for Newsletter HTML output.

It is referred as php-newsletter-article-render and its use can be found in

vendor/xalok-go/cobase/src/App/Bundle/CmsBundle/PageRenderer/HTMLNewsletterPageRenderer.php

Based on the roles defined in the modules its purpose is to apply a custom HTML transformation allowed or expected for newsletter output.

In case of not matching any specified role or not locating the setting it will run the default module transformation.

php
$success = false;

$phpScript = $this->settingsManager->getSetting('php-newsletter-article-render');
if (!is_null($phpScript) && !empty($phpScript->getParameters())) {
    try {
        eval($phpScript->getParameters());
    } catch (ContextErrorException | \ParseError $ex) {
        $this->scriptingLogger->error('running php-newsletter-article-render script', preg_split("/(\r\n|\n|\r)/", $ex));
        $html .= sprintf('<!-- Element %s generated an error at newsletter render; %s -->', $role, preg_split("/(\r\n|\n|\r)/", $ex));
    }
}

if ($success){
    //NOP
}elseif ( ... )