Skip to content

Commit

Permalink
Merge pull request #701 from ConvertKit/divi-modules
Browse files Browse the repository at this point in the history
Divi: Add Form Module
  • Loading branch information
n7studios authored Aug 29, 2024
2 parents 139f7f2 + 4800a9c commit b202e8b
Show file tree
Hide file tree
Showing 15 changed files with 749 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ jobs:
- name: Start chromedriver
run: |
export DISPLAY=:99
chromedriver --url-base=/wd/hub &
chromedriver --port=9515 --url-base=/wd/hub &
sudo Xvfb -ac :99 -screen 0 1920x1080x24 > /dev/null 2>&1 & # optional
# Write any secrets, such as API keys, to the .env.dist.testing file now.
Expand Down
1 change: 1 addition & 0 deletions includes/class-wp-convertkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ private function initialize_global() {
$this->classes['pre_publish_action_broadcast_export'] = new ConvertKit_Pre_Publish_Action_Broadcast_Export();
$this->classes['broadcasts_exporter'] = new ConvertKit_Broadcasts_Exporter();
$this->classes['broadcasts_importer'] = new ConvertKit_Broadcasts_Importer();
$this->classes['divi'] = new ConvertKit_Divi();
$this->classes['elementor'] = new ConvertKit_Elementor();
$this->classes['gutenberg'] = new ConvertKit_Gutenberg();
$this->classes['media_library'] = new ConvertKit_Media_Library();
Expand Down
66 changes: 66 additions & 0 deletions includes/integrations/divi/class-convertkit-divi-extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Divi Extension class.
*
* @package ConvertKit
* @author ConvertKit
*/

/**
* Registers Plugin as an extension in Divi.
*
* @package ConvertKit
* @author ConvertKit
*/
class ConvertKit_Divi_Extension extends DiviExtension {

/**
* The gettext domain for the extension's translations.
*
* @since 2.5.6
*
* @var string
*/
public $gettext_domain = 'convertkit';

/**
* The extension's WP Plugin name.
*
* @since 2.5.6
*
* @var string
*/
public $name = 'convertkit-divi';

/**
* The extension's version.
*
* @since 2.5.6
*
* @var string
*/
public $version = '2.5.6';

/**
* Constructor.
*
* @since 2.5.6
*
* @param string $name Extension name.
* @param array $args Arguments.
*/
public function __construct( $name = 'convertkit-divi', $args = array() ) {

$this->plugin_dir = CONVERTKIT_PLUGIN_PATH . '/includes/integrations/divi/';
$this->plugin_dir_url = CONVERTKIT_PLUGIN_URL . 'includes/integrations/divi/';

// Store any JS data that can be accessed by builder-bundle.min.js using window.ConvertkitDiviBuilderData.
$this->_builder_js_data = convertkit_get_blocks();

// Call parent construct.
parent::__construct( $name, $args );

}
}

new ConvertKit_Divi_Extension();
37 changes: 37 additions & 0 deletions includes/integrations/divi/class-convertkit-divi-module-form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Divi Module: ConvertKit Form.
*
* @package ConvertKit
* @author ConvertKit
*/

/**
* Registers the ConvertKit Form Block as a Divi Module.
*
* @package ConvertKit
* @author ConvertKit
*/
class ConvertKit_Divi_Module_Form extends ConvertKit_Divi_Module {

/**
* The ConvertKit block name.
*
* @since 2.5.6
*
* @var string
*/
public $block_name = 'form';

/**
* The ConvertKit Divi module name.
*
* @since 2.5.6
*
* @var string
*/
public $slug = 'convertkit_form';

}

new ConvertKit_Divi_Module_Form();
200 changes: 200 additions & 0 deletions includes/integrations/divi/class-convertkit-divi-module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php
/**
* Divi Module
*
* @package ConvertKit
* @author ConvertKit
*/

/**
* Registers blocks as Divi Modules.
*
* @package ConvertKit
* @author ConvertKit
*/
class ConvertKit_Divi_Module extends ET_Builder_Module {

/**
* How modules are supported in the Visual Builder (off|partial|on)
*
* @since 2.5.6
*
* @var string
*/
public $vb_support = 'on';

/**
* The ConvertKit block name.
*
* @since 2.5.6
*
* @var string
*/
public $block_name = '';

/**
* The ConvertKit Divi module name.
*
* @since 2.5.6
*
* @var string
*/
public $slug = '';

/**
* Holds the block definition, properties and fields.
*
* @since 2.5.6
*
* @var bool|WP_Error|array
*/
public $block = false;

/**
* Defines the Module name
*
* @since 2.5.6
*/
public function init() {

// Get block.
$blocks = convertkit_get_blocks();

// Bail if no blocks are available.
if ( ! is_array( $blocks ) || ! count( $blocks ) ) {
return;
}

// Bail if the block doesn't exist.
if ( ! array_key_exists( $this->block_name, $blocks ) ) {
return;
}

// Define the block and its name.
$this->block = $blocks[ $this->block_name ];
$this->name = esc_html( $this->block['title'] );

}

/**
* Defines the fields that can be configured for this Module
*
* @since 2.5.6
*/
public function get_fields() {

// Bail if no block.
if ( is_wp_error( $this->block ) ) {
return array();
}

// Bail if no fields.
if ( ! is_array( $this->block['fields'] ) ) {
return array();
}

// Build fields.
$fields = array();
foreach ( $this->block['fields'] as $field_name => $field ) {
// Start building field definition.
$fields[ $field_name ] = array(
'type' => $field['type'],
'default' => $this->get_default_value( $field ),
'description' => ( isset( $field['description'] ) ? $field['description'] : '' ),
'label' => $field['label'],
'toggle_slug' => 'main_content',
);

// Add/change field parameters depending on the field's type.
switch ( $field['type'] ) {
/**
* Number
*/
case 'number':
$fields[ $field_name ] = array_merge(
$fields[ $field_name ],
array(
'type' => 'range',
'range_settings' => array(
'min' => $field['min'],
'max' => $field['max'],
'step' => $field['step'],
),
'unitless' => true,
)
);
break;

/**
* Select
*/
case 'select':
$fields[ $field_name ]['options'] = $field['values'];
break;

/**
* Toggle
*/
case 'toggle':
$fields[ $field_name ] = array_merge(
$fields[ $field_name ],
array(
'type' => 'yes_no_button',
'default' => ( $fields[ $field_name ]['default'] ? 'on' : 'off' ),
'options' => array(
'off' => __( 'No', 'convertkit' ),
'on' => __( 'Yes', 'convertkit' ),
),
)
);
break;

}
}

// Return.
return $fields;

}

/**
* Render the module.
*
* @since 2.5.6
*
* @param array|string $unprocessed_props Unprocessed properties.
* @param array|string $content Content.
* @param string $render_slug Slug.
* @return string Block's output.
*/
public function render( $unprocessed_props, $content, $render_slug ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter

// Render using Block class' render() function.
// Output is already escaped in render() function.
return WP_ConvertKit()->get_class( 'blocks_convertkit_' . $this->block_name )->render( $unprocessed_props ); // phpcs:ignore WordPress.Security.EscapeOutput

}

/**
* Returns the default value for the given field configuration.
*
* If the field's default value is an array, it's converted to a string,
* to prevent Divi builder timeout errors on the frontend.
*
* @since 2.5.6
*
* @param array $field Field.
* @return string|int|object Default Value
*/
private function get_default_value( $field ) {

// Return a blank string if the field doesn't specify a default value.
if ( ! array_key_exists( 'default_value', $field ) ) {
return '';
}

return $field['default_value'];

}

}
40 changes: 40 additions & 0 deletions includes/integrations/divi/class-convertkit-divi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Divi Integration class.
*
* @package ConvertKit
* @author ConvertKit
*/

/**
* Registers this Plugin as a Divi extension, so that
* Divi modules can then be registered.
*
* @package ConvertKit
* @author ConvertKit
*/
class ConvertKit_Divi {

/**
* Constructor
*
* @since 2.5.6
*/
public function __construct() {

add_action( 'divi_extensions_init', array( $this, 'divi_extensions_init' ) );

}

/**
* Loads the ConvertKi Divi extension, which registers ConvertKit-specific Divi modules.
*
* @since 2.5.6
*/
public function divi_extensions_init() {

require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/divi/class-convertkit-divi-extension.php';

}

}
19 changes: 19 additions & 0 deletions includes/integrations/divi/loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Divi module loader.
*
* Divi automagically loads this file based on the `plugin_dir` defined
* in the ConvertKit_Divi_Extension class.
*
* @package ConvertKit
* @author ConvertKit
*/

// Bail if Divi isn't loaded.
if ( ! class_exists( 'ET_Builder_Element' ) ) {
return;
}

// Load Divi modules.
require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/divi/class-convertkit-divi-module.php';
require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/divi/class-convertkit-divi-module-form.php';
Loading

0 comments on commit b202e8b

Please sign in to comment.