| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Utils; |
| 4 |
|
| 5 |
use Templately\Builder\Types\BaseTemplate; |
| 6 |
|
| 7 |
class ElementorHelper extends ImportHelper { |
| 8 |
protected $content; |
| 9 |
|
| 10 |
protected $post_id; |
| 11 |
|
| 12 |
private $nav_menus = []; |
| 13 |
|
| 14 |
private $ea_post_widgets = [ |
| 15 |
"eael-post-grid", |
| 16 |
"eael-post-list", |
| 17 |
"eael-post-timeline", |
| 18 |
"eael-content-timeline", |
| 19 |
"eael-dynamic-filterable-gallery", |
| 20 |
"eael-post-carousel", |
| 21 |
"eael-post-block", |
| 22 |
"eael-woo-product-carousel", |
| 23 |
"eael-woo-product-slider" |
| 24 |
]; |
| 25 |
|
| 26 |
private $el_post_widgets = [ |
| 27 |
'posts', |
| 28 |
'portfolio', |
| 29 |
'archive-posts', |
| 30 |
'woocommerce-products' |
| 31 |
]; |
| 32 |
|
| 33 |
/** |
| 34 |
* @param $template_json |
| 35 |
* @param $template_settings |
| 36 |
* @param $extra_content |
| 37 |
* |
| 38 |
* @return ElementorHelper |
| 39 |
*/ |
| 40 |
public function prepare( $template_json, $template_settings, $extra_content = [] ) { |
| 41 |
|
| 42 |
$extraContent = $extra_content; |
| 43 |
$_data = []; |
| 44 |
foreach ( $template_settings['data'] as $type => $type_data ) { |
| 45 |
if ( empty( $type_data ) ) { |
| 46 |
continue; |
| 47 |
} |
| 48 |
|
| 49 |
if ( $type == 'nav_menus' ) { |
| 50 |
$this->nav_menus = $type_data; |
| 51 |
} |
| 52 |
|
| 53 |
if ( $type == 'form' || $type == 'post_type' ) { |
| 54 |
foreach ( $type_data as $plugin => $plugin_data ) { |
| 55 |
if ( $type == 'post_type' ) { |
| 56 |
$_data[ $plugin_data['id'] ] = [ |
| 57 |
'type' => $type, |
| 58 |
'query' => $plugin_data['query'] |
| 59 |
]; |
| 60 |
} else { |
| 61 |
foreach ( $plugin_data as $value ) { |
| 62 |
if ( empty( $value['settings'] ) ) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
foreach ( $value['settings'] as $key => $v ) { |
| 67 |
if ( ! isset( $extraContent[ $plugin ] ) ) { |
| 68 |
continue; |
| 69 |
} |
| 70 |
$_data[ $value['id'] ][ $key ] = $extraContent[ $plugin ][ $value['id'] ]; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
$this->sse_log('prepare', 'Preparing output for finalize, just a moment...', 1, 'eventLog'); |
| 75 |
} |
| 76 |
} |
| 77 |
$this->sse_log('prepare', 'Preparing output for finalize, just a moment...', 1, 'eventLog'); |
| 78 |
} |
| 79 |
|
| 80 |
// $content = $template_json; |
| 81 |
$this->json_prepare( $template_json['content'], $_data ); |
| 82 |
$this->post_id = $this->map_post_ids[ $template_settings['post_id'] ]; |
| 83 |
$this->content = $template_json; |
| 84 |
$this->content['import_settings'] = $template_settings; |
| 85 |
|
| 86 |
return $this; |
| 87 |
} |
| 88 |
|
| 89 |
public function update() { |
| 90 |
/** |
| 91 |
* @var BaseTemplate $template |
| 92 |
*/ |
| 93 |
$template = templately()->theme_builder::$templates_manager->get( $this->post_id ); |
| 94 |
$this->sse_log('update', 'Updating prepared data, just a moment...', 1, 'eventLog'); |
| 95 |
$template->import( $this->content ); |
| 96 |
|
| 97 |
$this->nav_menus = []; |
| 98 |
$this->content = []; |
| 99 |
} |
| 100 |
|
| 101 |
private function json_prepare( &$elements, $data ) { |
| 102 |
foreach ( $elements as &$element ) { |
| 103 |
if ( ! empty( $data ) ) { |
| 104 |
foreach ( $data as $id => $settings ) { |
| 105 |
if ( $element['id'] == $id ) { |
| 106 |
if ( isset( $settings['type'] ) && $settings['type'] == 'post_type' ) { |
| 107 |
$this->replace_query_data( $element, $settings['query'] ); |
| 108 |
} else { |
| 109 |
foreach ( $settings as $key => $value ) { |
| 110 |
$element['settings'][ $key ] = $value; |
| 111 |
} |
| 112 |
} |
| 113 |
unset( $data[ $id ] ); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Menu Update if needed. |
| 120 |
*/ |
| 121 |
if ( ! empty( $this->nav_menus ) ) { |
| 122 |
$this->nav_menu_update( $element ); |
| 123 |
} |
| 124 |
|
| 125 |
if ( ! empty( $element['elements'] ) ) { |
| 126 |
$this->json_prepare( $element['elements'], $data ); |
| 127 |
} |
| 128 |
$this->sse_log('prepare', 'Preparing output for finalize, just a moment...', 1, 'eventLog'); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
private function nav_menu_update( &$element ) { |
| 133 |
$this->sse_log('nav-menu', 'Updating nav menus, just a moment...', 1, 'eventLog'); |
| 134 |
if ( ! isset( $element['widgetType'] ) ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
switch ( $element['widgetType'] ) { |
| 139 |
case 'eael-simple-menu': |
| 140 |
$element['settings']['eael_simple_menu_menu'] = $this->map_term_ids[ $element['settings']['eael_simple_menu_menu'] ]; |
| 141 |
break; |
| 142 |
case 'eael-advanced-menu': |
| 143 |
$element['settings']['eael_advanced_menu_menu'] = $this->map_term_ids[ $element['settings']['eael_advanced_menu_menu'] ]; |
| 144 |
break; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
private function replace_query_data( &$element, $data ) { |
| 149 |
$this->sse_log('query', 'Finalizing query data, just a moment...', 1, 'eventLog'); |
| 150 |
if ( ! empty( $element['widgetType'] ) ) { |
| 151 |
if ( in_array( $element['widgetType'], $this->ea_post_widgets ) ) { |
| 152 |
if ( ! empty( $data['tax_query'] ) ) { |
| 153 |
foreach ( $data['tax_query'] as $key => $tax_query ) { |
| 154 |
if ( $key === 'relation' ) { |
| 155 |
continue; |
| 156 |
} |
| 157 |
if ( isset( $element['settings'][ $tax_query['taxonomy'] . '_ids' ] ) ) { |
| 158 |
$new_ids = []; |
| 159 |
foreach ( $element['settings'][ $tax_query['taxonomy'] . '_ids' ] as $id ) { |
| 160 |
if ( isset( $this->map_term_ids[ $id ] ) ) { |
| 161 |
$new_ids[] = $this->map_term_ids[ $id ]; |
| 162 |
} |
| 163 |
} |
| 164 |
$element['settings'][ $tax_query['taxonomy'] . '_ids' ] = $new_ids; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
if ( ! empty( $element['settings']['authors'] ) ) { |
| 169 |
$element['settings']['authors'] = [ get_current_user_id() ]; |
| 170 |
} |
| 171 |
} |
| 172 |
if ( in_array( $element['widgetType'], $this->el_post_widgets ) ) { |
| 173 |
if ( ! empty( $data['tax_query'] ) ) { |
| 174 |
$this->replace_term_ids( $element, [ |
| 175 |
'posts_include_term_ids', |
| 176 |
'posts_exclude_term_ids', |
| 177 |
'query_include_term_ids', |
| 178 |
'query_exclude_term_ids', |
| 179 |
] ); |
| 180 |
|
| 181 |
} |
| 182 |
if ( ! empty( $data['author__in'] ) ) { |
| 183 |
$keys = [ 'posts_include_authors', 'query_include_authors' ]; |
| 184 |
foreach ( $keys as $key ) { |
| 185 |
if ( isset( $element['settings'][ $key ] ) ) { |
| 186 |
$element['settings'][ $key ] = [ get_current_user_id() ]; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
if ( $element['widgetType'] == 'eael-woo-product-gallery' ) { |
| 192 |
$this->replace_term_ids( $element, [ 'eael_product_gallery_categories', 'eael_product_gallery_tags' ] ); |
| 193 |
|
| 194 |
} |
| 195 |
if ( $element['widgetType'] == 'eicon-woocommerce' ) { |
| 196 |
$this->replace_term_ids( $element, [ 'eael_product_grid_categories' ] ); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
private function replace_term_ids( &$element, $keys ) { |
| 202 |
foreach ( $keys as $key ) { |
| 203 |
$this->sse_log('terms', 'Finalizing term ids, just a moment...', 1, 'eventLog'); |
| 204 |
if ( ! empty( $element['settings'][ $key ] ) ) { |
| 205 |
$new_ids = []; |
| 206 |
foreach ( $element['settings'][ $key ] as $id ) { |
| 207 |
if ( isset( $this->map_term_ids[ $id ] ) ) { |
| 208 |
$new_ids[] = $this->map_term_ids[ $id ]; |
| 209 |
} |
| 210 |
} |
| 211 |
if(!empty($new_ids)){ |
| 212 |
$element['settings'][ $key ] = $new_ids; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
} |
| 217 |
} |
| 218 |
} |