| @@ -1,7 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\Core\Base; |
| 3 | 3 | |
| 4 | +use Elementor\Core\Base\Elements_Iteration_Actions\Assets as Assets_Iteration_Action; | |
| 5 | +use Elementor\Core\Base\Elements_Iteration_Actions\Base as Elements_Iteration_Action; | |
| 4 | 6 | use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 5 | 7 | use Elementor\Core\Settings\Page\Model as Page_Model; |
| 6 | 8 | use Elementor\Core\Utils\Exceptions; |
| 7 | 9 | use Elementor\Plugin; |
| @@ -6,8 +8,9 @@ | ||
| 6 | 8 | use Elementor\Core\Utils\Exceptions; |
| 7 | 9 | use Elementor\Plugin; |
| 8 | 10 | use Elementor\Controls_Manager; |
| 9 | 11 | use Elementor\Controls_Stack; |
| 12 | +use Elementor\TemplateLibrary\Source_Local; | |
| 10 | 13 | use Elementor\User; |
| 11 | 14 | use Elementor\Core\Settings\Manager as SettingsManager; |
| 12 | 15 | use Elementor\Utils; |
| 13 | 16 | use Elementor\Widget_Base; |
| @@ -71,8 +74,13 @@ | ||
| 71 | 74 | |
| 72 | 75 | private static $properties = []; |
| 73 | 76 | |
| 74 | 77 | /** |
| 78 | + * @var Elements_Iteration_Action[] | |
| 79 | + */ | |
| 80 | + private $elements_iteration_actions = []; | |
| 81 | + | |
| 82 | + /** | |
| 75 | 83 | * Document post data. |
| 76 | 84 | * |
| 77 | 85 | * Holds the document post data. |
| 78 | 86 | * |
| @@ -154,8 +162,12 @@ | ||
| 154 | 162 | public static function get_title() { |
| 155 | 163 | return __( 'Document', 'elementor' ); |
| 156 | 164 | } |
| 157 | 165 | |
| 166 | + public static function get_plural_title() { | |
| 167 | + return static::get_title(); | |
| 168 | + } | |
| 169 | + | |
| 158 | 170 | /** |
| 159 | 171 | * Get property. |
| 160 | 172 | * |
| 161 | 173 | * Retrieve the document property. |
| @@ -186,14 +198,24 @@ | ||
| 186 | 198 | public static function get_class_full_name() { |
| 187 | 199 | return get_called_class(); |
| 188 | 200 | } |
| 189 | 201 | |
| 202 | + public static function get_create_url() { | |
| 203 | + $base_create_url = Plugin::$instance->documents->get_create_new_post_url( Source_Local::CPT ); | |
| 204 | + | |
| 205 | + return add_query_arg( [ 'template_type' => static::get_type() ], $base_create_url ); | |
| 206 | + } | |
| 207 | + | |
| 208 | + public function get_name() { | |
| 209 | + return static::get_type(); | |
| 210 | + } | |
| 211 | + | |
| 190 | 212 | /** |
| 191 | 213 | * @since 2.0.0 |
| 192 | 214 | * @access public |
| 193 | 215 | */ |
| 194 | 216 | public function get_unique_name() { |
| 195 | - return $this->get_name() . '-' . $this->post->ID; | |
| 217 | + return static::get_type() . '-' . $this->post->ID; | |
| 196 | 218 | } |
| 197 | 219 | |
| 198 | 220 | /** |
| 199 | 221 | * @since 2.3.0 |
| @@ -205,17 +227,8 @@ | ||
| 205 | 227 | return $post_type_object->labels->singular_name; |
| 206 | 228 | } |
| 207 | 229 | |
| 208 | 230 | /** |
| 209 | - * @since 2.0.12 | |
| 210 | - * @deprecated 2.4.0 Use `Document::get_remote_library_config()` instead | |
| 211 | - * @access public | |
| 212 | - */ | |
| 213 | - public function get_remote_library_type() { | |
| 214 | - _deprecated_function( __METHOD__, '2.4.0', __CLASS__ . '::get_remote_library_config()' ); | |
| 215 | - } | |
| 216 | - | |
| 217 | - /** | |
| 218 | 231 | * @since 2.0.0 |
| 219 | 232 | * @access public |
| 220 | 233 | */ |
| 221 | 234 | public function get_main_id() { |
| @@ -269,19 +282,8 @@ | ||
| 269 | 282 | public function get_main_post() { |
| 270 | 283 | return get_post( $this->get_main_id() ); |
| 271 | 284 | } |
| 272 | 285 | |
| 273 | - /** | |
| 274 | - * @since 2.0.6 | |
| 275 | - * @deprecated 2.4.0 Use `Document::get_container_attributes()` instead | |
| 276 | - * @access public | |
| 277 | - */ | |
| 278 | - public function get_container_classes() { | |
| 279 | - _deprecated_function( __METHOD__, '2.4.0', __CLASS__ . '::get_container_attributes()' ); | |
| 280 | - | |
| 281 | - return ''; | |
| 282 | - } | |
| 283 | - | |
| 284 | 286 | public function get_container_attributes() { |
| 285 | 287 | $id = $this->get_main_id(); |
| 286 | 288 | |
| 287 | 289 | $attributes = [ |
| @@ -572,8 +574,18 @@ | ||
| 572 | 574 | * |
| 573 | 575 | * @return bool |
| 574 | 576 | */ |
| 575 | 577 | public function save( $data ) { |
| 578 | + /** | |
| 579 | + * Fires when document save starts on Elementor. | |
| 580 | + * | |
| 581 | + * @param array $data. | |
| 582 | + * @param \Elementor\Core\Base\Document $this The current document. | |
| 583 | + * | |
| 584 | + * @since 3.3.0 | |
| 585 | + */ | |
| 586 | + $data = apply_filters( 'elementor/document/save/data', $data, $this ); | |
| 587 | + | |
| 576 | 588 | $this->add_handle_revisions_changed_filter(); |
| 577 | 589 | |
| 578 | 590 | if ( ! $this->is_editable_by_current_user() ) { |
| 579 | 591 | return false; |
| @@ -605,10 +617,9 @@ | ||
| 605 | 617 | } |
| 606 | 618 | |
| 607 | 619 | $this->save_settings( $data['settings'] ); |
| 608 | 620 | |
| 609 | - // Refresh post after save settings. | |
| 610 | - $this->post = get_post( $this->post->ID ); | |
| 621 | + $this->refresh_post(); | |
| 611 | 622 | } |
| 612 | 623 | |
| 613 | 624 | // Don't check is_empty, because an empty array should be saved. |
| 614 | 625 | if ( isset( $data['elements'] ) && is_array( $data['elements'] ) ) { |
| @@ -642,8 +653,12 @@ | ||
| 642 | 653 | |
| 643 | 654 | return true; |
| 644 | 655 | } |
| 645 | 656 | |
| 657 | + public function refresh_post() { | |
| 658 | + $this->post = get_post( $this->post->ID ); | |
| 659 | + } | |
| 660 | + | |
| 646 | 661 | /** |
| 647 | 662 | * @param array $new_settings |
| 648 | 663 | * |
| 649 | 664 | * @return static |
| @@ -1038,8 +1053,14 @@ | ||
| 1038 | 1053 | do_action( 'elementor/db/before_save', $this->post->post_status, $is_meta_updated ); |
| 1039 | 1054 | |
| 1040 | 1055 | Plugin::$instance->db->save_plain_text( $this->post->ID ); |
| 1041 | 1056 | |
| 1057 | + $elements_iteration_actions = $this->get_elements_iteration_actions(); | |
| 1058 | + | |
| 1059 | + if ( $elements_iteration_actions ) { | |
| 1060 | + $this->iterate_elements( $elements, $elements_iteration_actions, 'save' ); | |
| 1061 | + } | |
| 1062 | + | |
| 1042 | 1063 | /** |
| 1043 | 1064 | * After saving data. |
| 1044 | 1065 | * |
| 1045 | 1066 | * Fires after Elementor saves data to the database. |
| @@ -1294,11 +1315,20 @@ | ||
| 1294 | 1315 | |
| 1295 | 1316 | return [ |
| 1296 | 1317 | 'content' => $content, |
| 1297 | 1318 | 'settings' => $this->get_data( 'settings' ), |
| 1319 | + 'metadata' => $this->get_export_metadata(), | |
| 1298 | 1320 | ]; |
| 1299 | 1321 | } |
| 1300 | 1322 | |
| 1323 | + public function get_export_summary() { | |
| 1324 | + return [ | |
| 1325 | + 'title' => $this->post->post_title, | |
| 1326 | + 'doc_type' => $this->get_name(), | |
| 1327 | + 'thumbnail' => get_the_post_thumbnail_url( $this->post ), | |
| 1328 | + ]; | |
| 1329 | + } | |
| 1330 | + | |
| 1301 | 1331 | /* |
| 1302 | 1332 | * Get Import Data |
| 1303 | 1333 | * |
| 1304 | 1334 | * Filters a document's data on import |
| @@ -1350,8 +1380,20 @@ | ||
| 1350 | 1380 | $this->save( [ |
| 1351 | 1381 | 'elements' => $data['content'], |
| 1352 | 1382 | 'settings' => $data['settings'], |
| 1353 | 1383 | ] ); |
| 1384 | + | |
| 1385 | + if ( $data['import_settings']['thumbnail'] ) { | |
| 1386 | + $attachment = Plugin::$instance->templates_manager->get_import_images_instance()->import( [ 'url' => $data['import_settings']['thumbnail'] ] ); | |
| 1387 | + | |
| 1388 | + set_post_thumbnail( $this->get_main_post(), $attachment['id'] ); | |
| 1389 | + } | |
| 1390 | + | |
| 1391 | + if ( ! empty( $data['metadata'] ) ) { | |
| 1392 | + foreach ( $data['metadata'] as $key => $value ) { | |
| 1393 | + $this->update_meta( $key, $value ); | |
| 1394 | + } | |
| 1395 | + } | |
| 1354 | 1396 | } |
| 1355 | 1397 | |
| 1356 | 1398 | private function process_element_import_export( Controls_Stack $element, $method ) { |
| 1357 | 1399 | $element_data = $element->get_data(); |
| @@ -1381,8 +1423,24 @@ | ||
| 1381 | 1423 | |
| 1382 | 1424 | return $element_data; |
| 1383 | 1425 | } |
| 1384 | 1426 | |
| 1427 | + protected function get_export_metadata() { | |
| 1428 | + $metadata = get_post_meta( $this->get_main_id() ); | |
| 1429 | + | |
| 1430 | + foreach ( $metadata as $meta_key => $meta_value ) { | |
| 1431 | + if ( is_protected_meta( $meta_key, 'post' ) ) { | |
| 1432 | + unset( $metadata[ $meta_key ] ); | |
| 1433 | + | |
| 1434 | + continue; | |
| 1435 | + } | |
| 1436 | + | |
| 1437 | + $metadata[ $meta_key ] = $meta_value[0]; | |
| 1438 | + } | |
| 1439 | + | |
| 1440 | + return $metadata; | |
| 1441 | + } | |
| 1442 | + | |
| 1385 | 1443 | protected function get_remote_library_config() { |
| 1386 | 1444 | $config = [ |
| 1387 | 1445 | 'type' => 'block', |
| 1388 | 1446 | 'default_route' => 'templates/blocks', |
| @@ -1409,8 +1467,15 @@ | ||
| 1409 | 1467 | * @since 2.1.3 |
| 1410 | 1468 | * @access protected |
| 1411 | 1469 | */ |
| 1412 | 1470 | protected function print_elements( $elements_data ) { |
| 1471 | + // Collect all data updaters that should be updated on runtime. | |
| 1472 | + $runtime_elements_iteration_actions = $this->get_runtime_elements_iteration_actions(); | |
| 1473 | + | |
| 1474 | + if ( $runtime_elements_iteration_actions ) { | |
| 1475 | + $this->iterate_elements( $elements_data, $runtime_elements_iteration_actions, 'render' ); | |
| 1476 | + } | |
| 1477 | + | |
| 1413 | 1478 | foreach ( $elements_data as $element_data ) { |
| 1414 | 1479 | $element = Plugin::$instance->elements_manager->create_element_instance( $element_data ); |
| 1415 | 1480 | |
| 1416 | 1481 | if ( ! $element ) { |
| @@ -1497,6 +1562,62 @@ | ||
| 1497 | 1562 | } |
| 1498 | 1563 | |
| 1499 | 1564 | private function remove_handle_revisions_changed_filter() { |
| 1500 | 1565 | remove_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'handle_revisions_changed' ] ); |
| 1566 | + } | |
| 1567 | + | |
| 1568 | + private function get_runtime_elements_iteration_actions() { | |
| 1569 | + $runtime_elements_iteration_actions = []; | |
| 1570 | + | |
| 1571 | + $elements_iteration_actions = $this->get_elements_iteration_actions(); | |
| 1572 | + | |
| 1573 | + foreach ( $elements_iteration_actions as $elements_iteration_action ) { | |
| 1574 | + if ( $elements_iteration_action->is_action_needed() ) { | |
| 1575 | + $runtime_elements_iteration_actions[] = $elements_iteration_action; | |
| 1576 | + } | |
| 1577 | + } | |
| 1578 | + | |
| 1579 | + return $runtime_elements_iteration_actions; | |
| 1580 | + } | |
| 1581 | + | |
| 1582 | + private function iterate_elements( $elements, $elements_iteration_actions, $mode ) { | |
| 1583 | + $unique_page_elements = []; | |
| 1584 | + | |
| 1585 | + foreach ( $elements_iteration_actions as $elements_iteration_action ) { | |
| 1586 | + $elements_iteration_action->set_mode( $mode ); | |
| 1587 | + } | |
| 1588 | + | |
| 1589 | + Plugin::$instance->db->iterate_data( $elements, function( array $element_data ) use ( &$unique_page_elements, $elements_iteration_actions ) { | |
| 1590 | + $element_type = 'widget' === $element_data['elType'] ? $element_data['widgetType'] : $element_data['elType']; | |
| 1591 | + | |
| 1592 | + $element = Plugin::$instance->elements_manager->create_element_instance( $element_data ); | |
| 1593 | + | |
| 1594 | + if ( $element ) { | |
| 1595 | + if ( ! in_array( $element_type, $unique_page_elements, true ) ) { | |
| 1596 | + $unique_page_elements[] = $element_type; | |
| 1597 | + | |
| 1598 | + foreach ( $elements_iteration_actions as $elements_iteration_action ) { | |
| 1599 | + $elements_iteration_action->unique_element_action( $element ); | |
| 1600 | + } | |
| 1601 | + } | |
| 1602 | + | |
| 1603 | + foreach ( $elements_iteration_actions as $elements_iteration_action ) { | |
| 1604 | + $elements_iteration_action->element_action( $element ); | |
| 1605 | + } | |
| 1606 | + } | |
| 1607 | + | |
| 1608 | + return $element_data; | |
| 1609 | + } ); | |
| 1610 | + | |
| 1611 | + foreach ( $elements_iteration_actions as $elements_iteration_action ) { | |
| 1612 | + $elements_iteration_action->after_elements_iteration(); | |
| 1613 | + } | |
| 1614 | + } | |
| 1615 | + | |
| 1616 | + private function get_elements_iteration_actions() { | |
| 1617 | + if ( ! $this->elements_iteration_actions ) { | |
| 1618 | + $this->elements_iteration_actions[] = new Assets_Iteration_Action( $this ); | |
| 1619 | + } | |
| 1620 | + | |
| 1621 | + return $this->elements_iteration_actions; | |
| 1501 | 1622 | } |
| 1502 | 1623 | } |