Introduction

The Script Launcher is a simplified version of the Custom Script, allowing developer to easily launch scripts without the hassle of registering a complete PHP class and logic.

Launcher Scripts defined by developers are located in the Script Launcher of the “Tools > Scripts” admin menu.

Register Script

In order to register a script, you have to use the acfe/init hook alongside with the acfe_register_launcher_script() function.

You can read more about the Recursive Mode in the Custom Script documentation.

add_action('acfe/init', 'my_acfe_scripts');
function my_acfe_scripts(){
    
    // non-recursive script
    acfe_register_launcher_script(array(
        'name'  => 'my_script',
        'label' => 'My Script',
    ));
    
    // recursive script
    acfe_register_launcher_script(array(
        'name'      => 'my_recursive_script',
        'label'     => 'My Recursive Script',
        'recursive' => true,
    ));
    
}

Script Start

This is the preparation phase which allows developer to gather global data and store it in a transient if needed. It is optional and can be skipped.

/**
 * acfe/script_launcher/start
 */
action('acfe/script_launcher/start',                $script);
action('acfe/script_launcher/start/name=my_script', $script);
add_action('acfe/script_launcher/start/name=my_script', 'my_script_start');
function my_script_start($script){
    
    // collecting data...
    
    // send message
    $script->send_response(array(
        'message' => 'Script started. Collecting data...',
        'status'  => 'success',
    ));
    
}

Script Request

This is the core of the script, where the job is actually done. The request action will be run one time (if not recursive) or multiple times (if recursive).

/**
 * acfe/script_launcher/request
 */
action('acfe/script_launcher/request',                $script);
action('acfe/script_launcher/request/name=my_script', $script);
add_action('acfe/script_launcher/request/name=my_script', 'my_script_request');
function my_script_request($script){
    
    // do something
    
    // send message
    $script->send_response(array(
        'message' => 'Request processed',
        'status'  => 'success',
    ));
    
}

Script Stop

This action is triggered either by the user (stop button) or by the code (if the script is finished). This phase allows developer to cleanup stored data if needed. It is also optional and can be skipped.

/**
 * acfe/script_launcher/stop
 */
action('acfe/script_launcher/stop',                $script);
action('acfe/script_launcher/stop/name=my_script', $script);
add_action('acfe/script_launcher/stop/name=my_script', 'my_script_stop');
function my_script_stop($script){
    
    // cleanup data...
    
    // send message
    $this->send_response(array(
        'message' => 'Script finished. Cleanup data.',
        'status'  => 'success',
    ));
    
}

Script Example

Here is a simple script example that will send an email each time the script is run.

<?php

// register script
add_action('acfe/init', 'my_register_acfe_email_script');
function my_register_acfe_email_script(){
    
    acfe_register_launcher_script(array(
        'name'  => 'send_email',
        'label' => 'Send Email',
    ));
    
}

// script request
add_action('acfe/script_launcher/request/name=send_email', 'my_acfe_email_script_request');
function my_acfe_email_script_request($script){
    
    // vars
    $from    = '[email protected]';
    $to      = '[email protected]';
    $subject = 'Email Script';
    $html    = '';
    
    ob_start();
    ?>
    
        <p>Hello,</p>
        <p>This is a test email send via the Script Launcher</p>
        <p>Regards.</p>
    
    <?php
    $html = ob_get_clean();
    
    // send email
    $sent = wp_mail($to, $subject, $html, array('Content-Type: text/html; charset=UTF-8', 'From: ' . $from));
    
    // success
    if($sent){
        
        $script->send_response(array(
            'message' => 'Email succesfully sent',
            'status'  => 'success',
        ));
    
    // error
    }else{
    
        $script->send_response(array(
            'message' => 'Email not sent. Something went wrong',
            'status'  => 'error',
        ));
        
    }
    
}

References

For further information about advanced usage, please refer to the Custom Script documentation.