| 1 |
<?php |
| 2 |
/** |
| 3 |
* Divi Module |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers blocks as Divi Modules. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Divi_Module extends ET_Builder_Module { |
| 16 |
|
| 17 |
/** |
| 18 |
* How modules are supported in the Visual Builder (off|partial|on) |
| 19 |
* |
| 20 |
* @since 2.5.6 |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $vb_support = 'on'; |
| 25 |
|
| 26 |
/** |
| 27 |
* The ConvertKit block name. |
| 28 |
* |
| 29 |
* @since 2.5.6 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public $block_name = ''; |
| 34 |
|
| 35 |
/** |
| 36 |
* The ConvertKit Divi module name. |
| 37 |
* |
| 38 |
* @since 2.5.6 |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public $slug = ''; |
| 43 |
|
| 44 |
/** |
| 45 |
* The full path to the SVG icon for this Divi module. |
| 46 |
* |
| 47 |
* @since 2.5.7 |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
public $icon_path = ''; |
| 52 |
|
| 53 |
/** |
| 54 |
* Holds the block definition, properties and fields. |
| 55 |
* |
| 56 |
* @since 2.5.6 |
| 57 |
* |
| 58 |
* @var bool|WP_Error|array |
| 59 |
*/ |
| 60 |
public $block = false; |
| 61 |
|
| 62 |
/** |
| 63 |
* Defines the Module name |
| 64 |
* |
| 65 |
* @since 2.5.6 |
| 66 |
*/ |
| 67 |
public function init() { |
| 68 |
|
| 69 |
// Get block. |
| 70 |
$blocks = convertkit_get_blocks(); |
| 71 |
|
| 72 |
// Bail if no blocks are available. |
| 73 |
if ( ! count( $blocks ) ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
// Bail if the block doesn't exist. |
| 78 |
if ( ! array_key_exists( $this->block_name, $blocks ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// Define the block, name and icon. |
| 83 |
$this->block = $blocks[ $this->block_name ]; |
| 84 |
$this->name = esc_html( $this->block['title'] ); |
| 85 |
$this->icon_path = CONVERTKIT_PLUGIN_PATH . '/' . $this->block['icon']; |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Defines the fields that can be configured for this Module |
| 91 |
* |
| 92 |
* @since 2.5.6 |
| 93 |
*/ |
| 94 |
public function get_fields() { |
| 95 |
|
| 96 |
// Bail if no block. |
| 97 |
if ( is_wp_error( $this->block ) ) { |
| 98 |
return array(); |
| 99 |
} |
| 100 |
|
| 101 |
// Bail if the block is false. |
| 102 |
// This happens on Divi 5 theme activation. |
| 103 |
if ( ! $this->block ) { |
| 104 |
return array(); |
| 105 |
} |
| 106 |
|
| 107 |
// Bail if no fields i.e. this is a frontend request. |
| 108 |
if ( ! array_key_exists( 'fields', $this->block ) ) { |
| 109 |
return array(); |
| 110 |
} |
| 111 |
if ( ! is_array( $this->block['fields'] ) ) { |
| 112 |
return array(); |
| 113 |
} |
| 114 |
|
| 115 |
// Build fields. |
| 116 |
$fields = array(); |
| 117 |
foreach ( $this->block['fields'] as $field_name => $field ) { |
| 118 |
// Start building field definition. |
| 119 |
$fields[ $field_name ] = array( |
| 120 |
'type' => $field['type'], |
| 121 |
'default' => $this->get_default_value( $field ), |
| 122 |
'description' => ( isset( $field['description'] ) ? $field['description'] : '' ), |
| 123 |
'label' => $field['label'], |
| 124 |
'toggle_slug' => 'main_content', |
| 125 |
); |
| 126 |
|
| 127 |
// Add field condition, if defined. |
| 128 |
if ( isset( $field['display_if'] ) ) { |
| 129 |
// Define value as 'on' or 'off' if it's 1 or 0 and the comparison field is a toggle field. |
| 130 |
$value = $field['display_if']['value']; |
| 131 |
if ( $this->block['fields'][ $field['display_if']['key'] ]['type'] === 'toggle' ) { |
| 132 |
$value = ( ( $value === '1' ) || ( $value === 1 ) ? 'on' : 'off' ); |
| 133 |
} |
| 134 |
|
| 135 |
// Add the comparison condition. |
| 136 |
$fields[ $field_name ]['show_if'] = array( |
| 137 |
$field['display_if']['key'] => $value, |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
// Add/change field parameters depending on the field's type. |
| 142 |
switch ( $field['type'] ) { |
| 143 |
/** |
| 144 |
* Number |
| 145 |
*/ |
| 146 |
case 'number': |
| 147 |
$fields[ $field_name ] = array_merge( |
| 148 |
$fields[ $field_name ], |
| 149 |
array( |
| 150 |
'type' => 'range', |
| 151 |
'range_settings' => array( |
| 152 |
'min' => $field['min'], |
| 153 |
'max' => $field['max'], |
| 154 |
'step' => $field['step'], |
| 155 |
), |
| 156 |
'unitless' => true, |
| 157 |
) |
| 158 |
); |
| 159 |
break; |
| 160 |
|
| 161 |
/** |
| 162 |
* Select |
| 163 |
*/ |
| 164 |
case 'resource': |
| 165 |
case 'select': |
| 166 |
// For select dropdowns, Divi treats the first <option> as the default. If it's selected, |
| 167 |
// Divi won't pass the underlying value, resulting in no output. |
| 168 |
// Forcing a 'None' option as the first ensures the user must select an <option>, therefore |
| 169 |
// ensuring output is correct. |
| 170 |
$fields[ $field_name ]['type'] = 'select'; |
| 171 |
$fields[ $field_name ]['options'] = array( 0 => __( '(None)', 'convertkit' ) ) + $field['values']; |
| 172 |
break; |
| 173 |
|
| 174 |
/** |
| 175 |
* Toggle |
| 176 |
*/ |
| 177 |
case 'toggle': |
| 178 |
$fields[ $field_name ] = array_merge( |
| 179 |
$fields[ $field_name ], |
| 180 |
array( |
| 181 |
'type' => 'yes_no_button', |
| 182 |
'default' => ( $fields[ $field_name ]['default'] ? 'on' : 'off' ), |
| 183 |
'options' => array( |
| 184 |
'off' => __( 'No', 'convertkit' ), |
| 185 |
'on' => __( 'Yes', 'convertkit' ), |
| 186 |
), |
| 187 |
) |
| 188 |
); |
| 189 |
break; |
| 190 |
|
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// Return. |
| 195 |
return $fields; |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Render the module. |
| 201 |
* |
| 202 |
* @since 2.5.6 |
| 203 |
* |
| 204 |
* @param array|string $unprocessed_props Unprocessed properties. |
| 205 |
* @param array|string $content Content. |
| 206 |
* @param string $render_slug Slug. |
| 207 |
* @return string Block's output. |
| 208 |
*/ |
| 209 |
public function render( $unprocessed_props, $content, $render_slug ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter |
| 210 |
|
| 211 |
// Render using Block class' render() function. |
| 212 |
// Output is already escaped in render() function. |
| 213 |
return WP_ConvertKit()->get_class( 'blocks_convertkit_' . $this->block_name )->render( $unprocessed_props ); |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Returns the default value for the given field configuration. |
| 219 |
* |
| 220 |
* If the field's default value is an array, it's converted to a string, |
| 221 |
* to prevent Divi builder timeout errors on the frontend. |
| 222 |
* |
| 223 |
* @since 2.5.6 |
| 224 |
* |
| 225 |
* @param array $field Field. |
| 226 |
* @return string|int|object Default Value |
| 227 |
*/ |
| 228 |
private function get_default_value( $field ) { |
| 229 |
|
| 230 |
// Return a blank string if the field doesn't specify a default value. |
| 231 |
if ( ! array_key_exists( 'default_value', $field ) ) { |
| 232 |
return ''; |
| 233 |
} |
| 234 |
|
| 235 |
return $field['default_value']; |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
} |
| 240 |
|