| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\Core\Base; |
| 3 | 3 | |
| 4 | 4 | use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 5 | +use Elementor\Core\Settings\Page\Model as Page_Model; | |
| 5 | 6 | use Elementor\Core\Utils\Exceptions; |
| 6 | 7 | use Elementor\Plugin; |
| 7 | 8 | use Elementor\Controls_Manager; |
| 8 | 9 | use Elementor\Controls_Stack; |
| @@ -1264,8 +1265,122 @@ | ||
| 1264 | 1265 | } |
| 1265 | 1266 | } |
| 1266 | 1267 | |
| 1267 | 1268 | parent::__construct( $data ); |
| 1269 | + } | |
| 1270 | + | |
| 1271 | + /* | |
| 1272 | + * Get Export Data | |
| 1273 | + * | |
| 1274 | + * Filters a document's data on export | |
| 1275 | + * | |
| 1276 | + * @since 3.2.0 | |
| 1277 | + * @access public | |
| 1278 | + * | |
| 1279 | + * @return array The data to export | |
| 1280 | + */ | |
| 1281 | + public function get_export_data() { | |
| 1282 | + $content = Plugin::$instance->db->iterate_data( $this->get_elements_data(), function( $element_data ) { | |
| 1283 | + $element_data['id'] = Utils::generate_random_string(); | |
| 1284 | + | |
| 1285 | + $element = Plugin::$instance->elements_manager->create_element_instance( $element_data ); | |
| 1286 | + | |
| 1287 | + // If the widget/element isn't exist, like a plugin that creates a widget but deactivated | |
| 1288 | + if ( ! $element ) { | |
| 1289 | + return null; | |
| 1290 | + } | |
| 1291 | + | |
| 1292 | + return $this->process_element_import_export( $element, 'on_export' ); | |
| 1293 | + } ); | |
| 1294 | + | |
| 1295 | + return [ | |
| 1296 | + 'content' => $content, | |
| 1297 | + 'settings' => $this->get_data( 'settings' ), | |
| 1298 | + ]; | |
| 1299 | + } | |
| 1300 | + | |
| 1301 | + /* | |
| 1302 | + * Get Import Data | |
| 1303 | + * | |
| 1304 | + * Filters a document's data on import | |
| 1305 | + * | |
| 1306 | + * @since 3.2.0 | |
| 1307 | + * @access public | |
| 1308 | + * | |
| 1309 | + * @return array The data to import | |
| 1310 | + */ | |
| 1311 | + public function get_import_data( array $data ) { | |
| 1312 | + $data['content'] = Plugin::$instance->db->iterate_data( $data['content'], function( $element_data ) { | |
| 1313 | + $element = Plugin::$instance->elements_manager->create_element_instance( $element_data ); | |
| 1314 | + | |
| 1315 | + // If the widget/element isn't exist, like a plugin that creates a widget but deactivated | |
| 1316 | + if ( ! $element ) { | |
| 1317 | + return null; | |
| 1318 | + } | |
| 1319 | + | |
| 1320 | + return $this->process_element_import_export( $element, 'on_import' ); | |
| 1321 | + } ); | |
| 1322 | + | |
| 1323 | + if ( ! empty( $data['settings'] ) ) { | |
| 1324 | + $template_model = new Page_Model( [ | |
| 1325 | + 'id' => 0, | |
| 1326 | + 'settings' => $data['settings'], | |
| 1327 | + ] ); | |
| 1328 | + | |
| 1329 | + $page_data = $this->process_element_import_export( $template_model, 'on_import' ); | |
| 1330 | + | |
| 1331 | + $data['settings'] = $page_data['settings']; | |
| 1332 | + } | |
| 1333 | + | |
| 1334 | + return $data; | |
| 1335 | + } | |
| 1336 | + | |
| 1337 | + /** | |
| 1338 | + * Import | |
| 1339 | + * | |
| 1340 | + * Allows to import an external data to a document | |
| 1341 | + * | |
| 1342 | + * @since 3.2.0 | |
| 1343 | + * @access public | |
| 1344 | + * | |
| 1345 | + * @param array $data | |
| 1346 | + */ | |
| 1347 | + public function import( array $data ) { | |
| 1348 | + $data = $this->get_import_data( $data ); | |
| 1349 | + | |
| 1350 | + $this->save( [ | |
| 1351 | + 'elements' => $data['content'], | |
| 1352 | + 'settings' => $data['settings'], | |
| 1353 | + ] ); | |
| 1354 | + } | |
| 1355 | + | |
| 1356 | + private function process_element_import_export( Controls_Stack $element, $method ) { | |
| 1357 | + $element_data = $element->get_data(); | |
| 1358 | + | |
| 1359 | + if ( method_exists( $element, $method ) ) { | |
| 1360 | + // TODO: Use the internal element data without parameters. | |
| 1361 | + $element_data = $element->{$method}( $element_data ); | |
| 1362 | + } | |
| 1363 | + | |
| 1364 | + foreach ( $element->get_controls() as $control ) { | |
| 1365 | + $control_class = Plugin::$instance->controls_manager->get_control( $control['type'] ); | |
| 1366 | + | |
| 1367 | + // If the control isn't exist, like a plugin that creates the control but deactivated. | |
| 1368 | + if ( ! $control_class ) { | |
| 1369 | + return $element_data; | |
| 1370 | + } | |
| 1371 | + | |
| 1372 | + if ( method_exists( $control_class, $method ) ) { | |
| 1373 | + $element_data['settings'][ $control['name'] ] = $control_class->{$method}( $element->get_settings( $control['name'] ), $control ); | |
| 1374 | + } | |
| 1375 | + | |
| 1376 | + // On Export, check if the control has an argument 'export' => false. | |
| 1377 | + if ( 'on_export' === $method && isset( $control['export'] ) && false === $control['export'] ) { | |
| 1378 | + unset( $element_data['settings'][ $control['name'] ] ); | |
| 1379 | + } | |
| 1380 | + } | |
| 1381 | + | |
| 1382 | + return $element_data; | |
| 1268 | 1383 | } |
| 1269 | 1384 | |
| 1270 | 1385 | protected function get_remote_library_config() { |
| 1271 | 1386 | $config = [ |