| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
use ET\Builder\Framework\DependencyManagement\Interfaces\DependencyInterface; |
| 8 |
use ET\Builder\FrontEnd\Module\Style; |
| 9 |
use ET\Builder\Framework\Utility\HTMLUtility; |
| 10 |
use ET\Builder\Packages\Module\Module; |
| 11 |
use ET\Builder\Packages\Module\Options\Css\CssStyle; |
| 12 |
use ET\Builder\Packages\Module\Options\Element\ElementClassnames; |
| 13 |
use ET\Builder\Packages\ModuleLibrary\ModuleRegistration; |
| 14 |
|
| 15 |
if ( ! class_exists( 'MPHB_Divi_5_Modules' ) ) { |
| 16 |
class MPHB_Divi_5_Modules implements DependencyInterface { |
| 17 |
private $module_map = array(); |
| 18 |
|
| 19 |
public function __construct( $module_map ) { |
| 20 |
$this->module_map = $module_map; |
| 21 |
} |
| 22 |
|
| 23 |
public function load() { |
| 24 |
add_action( 'init', array( $this, 'register_modules' ) ); |
| 25 |
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
public function register_rest_routes() { |
| 29 |
register_rest_route( |
| 30 |
'mphb-divi/v1', |
| 31 |
'/render-module', |
| 32 |
array( |
| 33 |
'methods' => \WP_REST_Server::CREATABLE, |
| 34 |
'callback' => array( $this, 'render_module_rest' ), |
| 35 |
'permission_callback' => static function () { |
| 36 |
return current_user_can( 'edit_posts' ); |
| 37 |
}, |
| 38 |
) |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
public function register_modules() { |
| 43 |
foreach ( $this->module_map as $module_name => $config ) { |
| 44 |
$module_json = trailingslashit( $config['path'] ) . 'module.json'; |
| 45 |
|
| 46 |
if ( ! file_exists( $module_json ) ) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
ModuleRegistration::register_module( |
| 51 |
$config['path'], |
| 52 |
array( |
| 53 |
'render_callback' => function ( $attrs = array(), $content = '', $block = null, $elements = null ) use ( $module_name ) { |
| 54 |
return $this->render_module( $module_name, $attrs, $content, $block, $elements ); |
| 55 |
}, |
| 56 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public function render_module( $module_name, $attrs, $content = '', $block = null, $elements = null ) { |
| 62 |
if ( ! isset( $this->module_map[ $module_name ]['renderer'] ) ) { |
| 63 |
return ''; |
| 64 |
} |
| 65 |
|
| 66 |
$renderer = $this->module_map[ $module_name ]['renderer']; |
| 67 |
|
| 68 |
if ( ! is_callable( array( 'MPHB_Divi_Renderers', $renderer ) ) ) { |
| 69 |
return ''; |
| 70 |
} |
| 71 |
|
| 72 |
$normalized = $this->normalize_module_atts( $attrs ); |
| 73 |
$html = call_user_func( array( 'MPHB_Divi_Renderers', $renderer ), $normalized ); |
| 74 |
|
| 75 |
if ( ! $block || ! $elements || ! class_exists( Module::class ) ) { |
| 76 |
return $html; |
| 77 |
} |
| 78 |
|
| 79 |
$module_inner = HTMLUtility::render( |
| 80 |
array( |
| 81 |
'tag' => 'div', |
| 82 |
'attributes' => array( |
| 83 |
'class' => 'et_pb_module_inner', |
| 84 |
), |
| 85 |
'childrenSanitizer' => 'et_core_esc_previously', |
| 86 |
'children' => $html, |
| 87 |
) |
| 88 |
); |
| 89 |
|
| 90 |
return Module::render( |
| 91 |
array( |
| 92 |
'orderIndex' => $block->parsed_block['orderIndex'] ?? 0, |
| 93 |
'storeInstance' => $block->parsed_block['storeInstance'] ?? null, |
| 94 |
'attrs' => $attrs, |
| 95 |
'elements' => $elements, |
| 96 |
'id' => $block->parsed_block['id'] ?? '', |
| 97 |
'name' => $block->block_type->name ?? $module_name, |
| 98 |
'moduleCategory' => $block->block_type->category ?? 'module', |
| 99 |
'classnamesFunction' => array( __CLASS__, 'module_classnames' ), |
| 100 |
'stylesComponent' => array( __CLASS__, 'module_styles' ), |
| 101 |
'scriptDataComponent' => array( __CLASS__, 'module_script_data' ), |
| 102 |
'children' => $module_inner, |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
public function render_module_rest( $request ) { |
| 108 |
$module_name = $request->get_param( 'moduleName' ); |
| 109 |
$attrs = $request->get_param( 'attrs' ); |
| 110 |
|
| 111 |
if ( ! is_string( $module_name ) || ! isset( $this->module_map[ $module_name ] ) ) { |
| 112 |
return new \WP_Error( |
| 113 |
'mphb_divi_invalid_module', |
| 114 |
esc_html__( 'Invalid Divi module.', 'mphb-divi' ), |
| 115 |
array( |
| 116 |
'status' => 400, |
| 117 |
) |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
if ( ! is_array( $attrs ) ) { |
| 122 |
$attrs = array(); |
| 123 |
} |
| 124 |
|
| 125 |
return rest_ensure_response( |
| 126 |
array( |
| 127 |
'html' => $this->render_module( $module_name, $attrs ), |
| 128 |
) |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
public static function module_styles( $args ) { |
| 133 |
$elements = $args['elements']; |
| 134 |
|
| 135 |
Style::add( |
| 136 |
array( |
| 137 |
'id' => $args['id'], |
| 138 |
'name' => $args['name'], |
| 139 |
'orderIndex' => $args['orderIndex'], |
| 140 |
'storeInstance' => $args['storeInstance'], |
| 141 |
'styles' => array( |
| 142 |
$elements->style( |
| 143 |
array( |
| 144 |
'attrName' => 'module', |
| 145 |
'styleProps' => array( |
| 146 |
'disabledOn' => array( |
| 147 |
'disabledModuleVisibility' => $args['settings']['disabledModuleVisibility'] ?? null, |
| 148 |
), |
| 149 |
), |
| 150 |
) |
| 151 |
), |
| 152 |
CssStyle::style( |
| 153 |
array( |
| 154 |
'selector' => $args['orderClass'], |
| 155 |
'attr' => $args['attrs']['css'] ?? array(), |
| 156 |
'cssFields' => \WP_Block_Type_Registry::get_instance()->get_registered( $args['name'] )->customCssFields, |
| 157 |
) |
| 158 |
), |
| 159 |
), |
| 160 |
) |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
public static function module_script_data( $args ) { |
| 165 |
$args['elements']->script_data( |
| 166 |
array( |
| 167 |
'attrName' => 'module', |
| 168 |
) |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
public static function module_classnames( $args ) { |
| 173 |
$args['classnamesInstance']->add( |
| 174 |
ElementClassnames::classnames( |
| 175 |
array( |
| 176 |
'attrs' => $args['attrs']['module']['decoration'] ?? array(), |
| 177 |
) |
| 178 |
) |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
public function normalize_module_atts( $attrs ) { |
| 183 |
|
| 184 |
if ( ! isset( $attrs['shortcode']['advanced'] ) ) { |
| 185 |
return $attrs; |
| 186 |
} |
| 187 |
|
| 188 |
$normalized = array(); |
| 189 |
|
| 190 |
foreach ( $attrs['shortcode']['advanced'] as $attr => $value ) { |
| 191 |
|
| 192 |
if ( isset( $value['desktop']['value'] ) ) { |
| 193 |
$normalized[ $attr ] = $value['desktop']['value']; |
| 194 |
} else { |
| 195 |
$normalized[ $attr ] = $value; |
| 196 |
} |
| 197 |
|
| 198 |
if ( 'selected_attributes' === $attr && is_array( $normalized[ $attr ] ) ) { |
| 199 |
$normalized[ $attr ] = implode( |
| 200 |
',', |
| 201 |
array_filter( |
| 202 |
array_map( 'strval', $normalized[ $attr ] ) |
| 203 |
) |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
if ( 'selected_attribute' === $attr && is_array( $normalized[ $attr ] ) ) { |
| 208 |
$normalized[ $attr ] = ! empty( $normalized[ $attr ] ) ? (string) reset( $normalized[ $attr ] ) : ''; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
return $normalized; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|