-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #701 from ConvertKit/divi-modules
Divi: Add Form Module
- Loading branch information
Showing
15 changed files
with
749 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
includes/integrations/divi/class-convertkit-divi-extension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
includes/integrations/divi/class-convertkit-divi-module-form.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
200
includes/integrations/divi/class-convertkit-divi-module.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
Oops, something went wrong.