| 1 |
<?php |
| 2 |
defined('ABSPATH') || exit; |
| 3 |
|
| 4 |
/** |
| 5 |
* Module Name: Tab |
| 6 |
* Description: Display Tab content |
| 7 |
*/ |
| 8 |
class TB_Tab_Module extends Themify_Builder_Component_Module { |
| 9 |
|
| 10 |
|
| 11 |
public static function get_module_name():string { |
| 12 |
add_filter('themify_builder_active_vars', [__CLASS__, 'builder_active_enqueue']); |
| 13 |
return __('Tab', 'themify'); |
| 14 |
} |
| 15 |
|
| 16 |
public static function get_module_icon():string { |
| 17 |
return 'layout-tab'; |
| 18 |
} |
| 19 |
|
| 20 |
public static function get_js_css():array { |
| 21 |
$assets = array( |
| 22 |
'css' => 1, |
| 23 |
'js' => 1, |
| 24 |
); |
| 25 |
if (Themify_Builder_Model::is_front_builder_activate()) { |
| 26 |
$assets['js_admin'] = 1; |
| 27 |
} |
| 28 |
return $assets; |
| 29 |
} |
| 30 |
|
| 31 |
public static function builder_active_enqueue(array $vars):array { |
| 32 |
$vars['addons'][THEMIFY_BUILDER_URI . '/js/modules/tab-admin.js'] = THEMIFY_VERSION; |
| 33 |
return $vars; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Render plain content for static content. |
| 39 |
* |
| 40 |
* @param array $module |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
public static function get_static_content(array $module):string { |
| 44 |
$mod_settings = $module['mod_settings']+ array( |
| 45 |
'mod_title_tab' => '', |
| 46 |
'tab_content_tab' => array() |
| 47 |
); |
| 48 |
$text ='' !== $mod_settings['mod_title_tab']?sprintf('<h3>%s</h3>', $mod_settings['mod_title_tab']): ''; |
| 49 |
if (!empty($mod_settings['tab_content_tab'])) { |
| 50 |
$text .= '<ul>'; |
| 51 |
foreach ($mod_settings['tab_content_tab'] as $content) { |
| 52 |
$text .= '<li>'; |
| 53 |
if ( ! empty( $content['title_tab'] ) ) { |
| 54 |
$text .= '<h4>' . $content['title_tab'] . '</h4>'; |
| 55 |
} |
| 56 |
if ( isset( $content['text_tab'] ) ) { |
| 57 |
$text .= $content['text_tab']; |
| 58 |
} else if ( ! empty( $content['builder_content'] ) && is_array( $content['builder_content'] ) ) { |
| 59 |
$text .= ThemifyBuilder_Data_Manager::_get_all_builder_text_content( $content['builder_content'] ); |
| 60 |
} |
| 61 |
$text .= '</li>'; |
| 62 |
} |
| 63 |
$text .= '</ul>'; |
| 64 |
} |
| 65 |
return $text; |
| 66 |
} |
| 67 |
|
| 68 |
public static function get_styling_image_fields() : array { |
| 69 |
return [ |
| 70 |
'bg_i' => '.ui .tab-nav li.current' |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
public static function get_translatable_fields( $module, $classname ) : array { |
| 75 |
$fields = parent::get_translatable_fields( $module, $classname ); |
| 76 |
if ( ! empty( $module['mod_settings']['mod_title_tab'] ) ) { |
| 77 |
$fields[] = [ |
| 78 |
'id' => 'mod_title_tab', |
| 79 |
'value' => $module['mod_settings']['mod_title_tab'], |
| 80 |
]; |
| 81 |
} |
| 82 |
if ( ! empty( $module['mod_settings']['tab_content_tab'] ) && is_array( $module['mod_settings']['tab_content_tab'] ) ) { |
| 83 |
foreach ( $module['mod_settings']['tab_content_tab'] as $row_index => $tab ) { |
| 84 |
if ( ! is_array( $tab ) ) { |
| 85 |
continue; |
| 86 |
} |
| 87 |
$fields[] = [ |
| 88 |
'id' => 'title_tab-' . $row_index, |
| 89 |
'value' => isset( $tab['title_tab'] ) ? $tab['title_tab'] : '', |
| 90 |
]; |
| 91 |
if ( isset( $tab['text_tab'] ) ) { |
| 92 |
$fields[] = [ |
| 93 |
'id' => 'text_tab-' . $row_index, |
| 94 |
'value' => $tab['text_tab'], |
| 95 |
'type' => 'VISUAL', |
| 96 |
]; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return $fields; |
| 102 |
} |
| 103 |
|
| 104 |
public static function translate_module( $module_data, $translations ) { |
| 105 |
if ( empty( $module_data['mod_settings'] ) || ! is_array( $module_data['mod_settings'] ) ) { |
| 106 |
$module_data['mod_settings'] = []; |
| 107 |
} |
| 108 |
if ( empty( $module_data['mod_settings']['tab_content_tab'] ) || ! is_array( $module_data['mod_settings']['tab_content_tab'] ) ) { |
| 109 |
$module_data['mod_settings']['tab_content_tab'] = []; |
| 110 |
} |
| 111 |
|
| 112 |
foreach ( $translations as $item_key => $value ) { |
| 113 |
if ( $item_key === 'mod_title_tab' ) { |
| 114 |
$module_data['mod_settings']['mod_title_tab'] = $value; |
| 115 |
continue; |
| 116 |
} |
| 117 |
|
| 118 |
$dash_pos = strrpos( $item_key, '-' ); |
| 119 |
if ( $dash_pos === false ) { |
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
$field = substr( $item_key, 0, $dash_pos ); |
| 124 |
$index = substr( $item_key, $dash_pos + 1 ); |
| 125 |
if ( $field === '' || $index === '' || ! is_numeric( $index ) ) { |
| 126 |
continue; |
| 127 |
} |
| 128 |
|
| 129 |
if ( ! isset( $module_data['mod_settings']['tab_content_tab'][ $index ] ) || ! is_array( $module_data['mod_settings']['tab_content_tab'][ $index ] ) ) { |
| 130 |
$module_data['mod_settings']['tab_content_tab'][ $index ] = []; |
| 131 |
} |
| 132 |
|
| 133 |
$module_data['mod_settings']['tab_content_tab'][ $index ][ $field ] = $value; |
| 134 |
} |
| 135 |
|
| 136 |
return $module_data; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns a flat array of all nested modules |
| 141 |
*/ |
| 142 |
public static function get_nested_modules( array $data ) : array { |
| 143 |
$modules = []; |
| 144 |
if ( isset( $data['mod_settings']['tab_content_tab'][0]['builder_content'] ) ) { |
| 145 |
foreach ( $data['mod_settings']['tab_content_tab'] as $tab ) { |
| 146 |
foreach ( $tab['builder_content'] as $row ) { |
| 147 |
$modules = array_merge( $modules, Themify_Builder::_get_modules_recursive( $row ) ); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $modules; |
| 153 |
} |
| 154 |
} |