Skip to content

Extension plugins

ole edited this page Nov 3, 2019 · 3 revisions

While WP ISPConfig 3 supports Gutenberg Blocks it may be necessary to add additional fields or functions.

If a field is not (yet) provided or is missing an important option, check out the supported action/filter hooks to customize it.

When a more advanced solution is required, building up a small Wordpress plugin and extend the IspconfigAbstract can be helpful. This allows control over all the ISPConfig REST API and enables a quick access to it using [YourClass] shortcodes

Check out the below shortcode example of [IspconfigShowTemplates]

/**
 * Plugin Name: WP-ISPConfig3 Show Templates Example
 * Description: Example plugin to display all available ISPConfig3 client templates using shortcode [IspconfigShowTemplates]
 * Version: 1.0.0
 * Author: ole1986 <ole.k@web.de>
 * Author URI: https://github.com/ole1986/wp-ispconfig3
 */
require_once ABSPATH . '/wp-content/plugins/wp-ispconfig3/ispconfig-abstract.php';

class IspconfigShowTemplates extends IspconfigAbstract
{
    public function Display($attr, $content = null)
    {
        // connect to ispconfig3 through REST API
        $this->withSoap();
        // fetch all available client templates
        $templates = $this->GetClientTemplates();
        // output only the names
        $templateNames = array_map(function ($item) {
            return $item['template_name'];
        }, $templates);

        echo implode('<br />', $templateNames);
    }
}

new IspconfigShowTemplates();

The next lines is a more complex example of a registration form using the shortcode [IspconfigRegisterClient]

<?php
/**
 * Plugin Name: WP-ISPConfig3 Registration Form
 * Description: Example plugin allowing a user to register an new client and website using shortcode [IspconfigRegisterClient]
 * Version: 1.0.0
 * Author: ole1986 <ole.k@web.de>
 * Author URI: https://github.com/ole1986/wp-ispconfig3
 */
if (!defined('ABSPATH')) {
    exit;
}

require_once ABSPATH . '/wp-content/plugins/wp-ispconfig3/ispconfig-abstract.php';

class IspconfigRegisterClient extends IspconfigAbstract
{
    /**
     * Called when user submits the data from register form - see Ispconfig::Display() for more details
     */
    protected function onPost()
    {
        if ('POST' !== $_SERVER[ 'REQUEST_METHOD' ]) {
            return;
        }

        if (empty($_POST)) {
            return;
        }
        
        try {
            // at least chekc if the client limit template exists in ISPConfig
            $templates = $this->withSoap()->GetClientTemplates();
            $filtered = array_filter(
                $templates,
                function ($v) {
                    return $v['template_id'] == $_POST['template'];
                }
            );
            if (empty($filtered)) {
                throw new Exception('No Limit template found for ID ' . $_POST['template']);
            }
            $opt = ['company_name' => $_POST['empresa'],
                    'contact_name' => $_POST['cliente'],
                    'email' => $_POST['email'],
                    'domain' => $_POST['domain'],
                    'username' => $_POST['username'],
                    'password' => $_POST['password'],
                    'template_master' => $_POST['template']
            ];
            
            $client = $this->GetClientByUser($opt['username']);
            
            if (!empty($client)) {
                throw new Exception('The user already exist. Please choice a different name');
            }
            
            // add the customer and website
            $this->AddClient($opt)->AddWebsite(['domain' => $opt['domain'], 'password' => $_POST['password']]);
            
            echo "<div class='ispconfig-msg ispconfig-msg-success'>" . sprintf(__('Your account %s has been created', 'wp-ispconfig3'), $opt['username']) ."</div>";
            
            // send confirmation mail to customer
            $sent = $this->SendConfirmation($opt['email'], $opt);
            if ($sent) {
                echo "<div class='ispconfig-msg ispconfig-msg-success'>" . __('You will receive a confirmation email shortly', 'wp-ispconfig3') . "</div>";
            }
            
            echo "<div class='ispconfig-msg'>" . __('You can now login here', 'wp-ispconfig3') .": <a href=\"https://".$_SERVER['HTTP_HOST'].":8080/\">click</a></div>";
            
            $this->closeSoap();
        } catch (SoapFault $e) {
            //WPISPConfig3::soap->__getLastResponse();
            echo '<div class="ispconfig-msg ispconfig-msg-error">SOAP Error: '.$e->getMessage() .'</div>';
        } catch (Exception $e) {
            echo '<div class="ispconfig-msg ispconfig-msg-error">Exception: '.$e->getMessage() . "</div>";
        }
    }
    
    /**
     * Display the formular and submit button
     * Usually called through shortcode
     *
     * @param array $opt options
     */
    public function Display($attr, $content = null)
    {
        $defaultOptions = ['title' => 'WP-ISPConfig3', 'button' => 'Click to create Client', 'subtitle' => 'New Client (incl. Website and Domain)','showtitle' => true];
        
        if (is_array($attr)) {
            $opt = array_merge($defaultOptions, $attr);
        } else {
            $opt = $defaultOptions;
        }
            
        ?>
        <div class="wrap">
            <h2>
            <?php if ($opt['showtitle']) {
                _e($opt['title'], 'wp-ispconfig3');
            } ?>
            </h2>
            <?php
                $this->onPost();
            ?>
            <form method="post" class="ispconfig" action="">
            <div id="poststuff" class="metabox-holder has-right-sidebar">
                <div id="post-body">
                    <div id="post-body-content">
                        <div id="normal-sortables" class="meta-box-sortables ui-sortable">
                            <div class="postbox inside">
                                <h3><?php _e($opt['subtitle'], 'wp-ispconfig3');?></h3>
                                <div class="inside">
                                    <div>
                                    <?php
                                        WPISPConfig3::getField('domain', 'Domain:', 'text', ['container' => 'div']);
                                        WPISPConfig3::getField('cliente', 'Full Name:', 'text', ['container' => 'div']);
                                        WPISPConfig3::getField('email', 'e-Mail:', 'email', ['container' => 'div']);
                                        WPISPConfig3::getField('empresa', 'Company Name:', 'text', ['container' => 'div']);
                                        WPISPConfig3::getField('username', 'Username:', 'text', ['container' => 'div']);
                                        WPISPConfig3::getField('password', 'Password:', 'password', ['container' => 'div']);
                                        WPISPConfig3::getField('ip', 'Client IP:', 'text', ['container' => 'div']);
                                        WPISPConfig3::getField('ns1', 'NameServer 1:', 'text', ['container' => 'div']);
                                        WPISPConfig3::getField('ns2', 'NameServer 2:', 'text', ['container' => 'div']);
                                    ?>
                                    <div>
                                    <label><?php echo __('Template:'); ?></label>
                                    <select name="template">
                                        <option value="1">5GB Webspace</option>
                                        <option value="2">2GB Webspace</option>
                                        <option value="3">10GB Webspace</option>
                                        <option value="4">Free Webspace</option>
                                    </select>
                                    </div>
                                </div>
                                <p></p>
                                <p><input type="submit" class="button-primary" name="submit" value="<?php _e($opt['button']);?>" /></p>
                                <p></p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            </form>

            </div>
        <?php
    }
}

new IspconfigRegisterClient();
Clone this wiki locally