Skip to content

Instantly share code, notes, and snippets.

@sofyansitorus
Created February 11, 2018 04:41
Show Gist options
  • Save sofyansitorus/2347e0a851a64cde65701378d1e80fec to your computer and use it in GitHub Desktop.
Save sofyansitorus/2347e0a851a64cde65701378d1e80fec to your computer and use it in GitHub Desktop.
<?php
/**
* Builder data handling class that deals
* with all database operations.
*
* @since 1.0
*/
final class FLBuilderModel {
/**
* Registers a module with the builder.
*
* @since 1.0
* @param string $class The module's PHP class name.
* @param array $form The module's settings form.
* @param array $params The module constructor paramemeters.
* @return void
*/
static public function register_module( $class, $form, $params = array() ) {
$instance = false;
if ( $class instanceof FLBuilderModule ) {
// Use initialised module class.
$instance = $class;
} elseif ( is_string( $class ) && class_exists( $class ) ) {
// Create a new instance of the module.
$instance = new $class( $params );
}
// Log an error if registered module module is not an instance of FLBuilderModule class.
if ( ! $instance instanceof FLBuilderModule ) {
error_log( __( 'Registered module is not an instance of FLBuilderModule class.', 'fl-builder' ) );
return;
}
// Log an error if a module with this slug already exists.
if ( isset( self::$modules[ $instance->slug ] ) ) {
error_log( sprintf( _x( 'A module with the filename %s.php already exists! Please namespace your module filenames to ensure compatibility with Beaver Builder.', '%s stands for the module filename', 'fl-builder' ), $instance->slug ) );
return;
}
// Filter the enabled flag.
$instance->enabled = apply_filters( 'fl_builder_register_module', $instance->enabled, $instance );
// Save the instance in the modules array.
self::$modules[ $instance->slug ] = $instance;
// Add the form to the instance.
self::$modules[ $instance->slug ]->form = apply_filters( 'fl_builder_register_settings_form', $form, $instance->slug );
self::$modules[ $instance->slug ]->form['advanced'] = self::$settings_forms['module_advanced'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment