| @@ -1,7 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\TemplateLibrary; |
| 3 | 3 | |
| 4 | +use Elementor\Core\Admin\Menu\Admin_Menu_Manager; | |
| 4 | 5 | use Elementor\Core\Base\Document; |
| 5 | 6 | use Elementor\Core\Editor\Editor; |
| 6 | 7 | use Elementor\Core\Utils\Collection; |
| 7 | 8 | use Elementor\DB; |
| @@ -14,8 +15,9 @@ | ||
| 14 | 15 | use Elementor\Utils; |
| 15 | 16 | use Elementor\User; |
| 16 | 17 | use Elementor\Core\Isolation\Wordpress_Adapter; |
| 17 | 18 | use Elementor\Core\Isolation\Wordpress_Adapter_Interface; |
| 19 | +use Elementor\Core\Isolation\Elementor_Adapter; | |
| 18 | 20 | use Elementor\Core\Isolation\Elementor_Adapter_Interface; |
| 19 | 21 | use Elementor\Modules\EditorOne\Classes\Menu_Data_Provider; |
| 20 | 22 | use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Editor_One_Saved_Templates_Menu; |
| 21 | 23 | use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Editor_One_Templates_Menu; |
| @@ -351,14 +353,90 @@ | ||
| 351 | 353 | |
| 352 | 354 | register_taxonomy( self::TAXONOMY_CATEGORY_SLUG, self::CPT, $args ); |
| 353 | 355 | } |
| 354 | 356 | |
| 357 | + /** | |
| 358 | + * Remove Add New item from admin menu. | |
| 359 | + * | |
| 360 | + * Fired by `admin_menu` action. | |
| 361 | + * | |
| 362 | + * @since 2.4.0 | |
| 363 | + * @access public | |
| 364 | + */ | |
| 365 | + private function admin_menu_reorder( Admin_Menu_Manager $admin_menu ) { | |
| 366 | + global $submenu; | |
| 355 | 367 | |
| 368 | + if ( ! isset( $submenu[ static::ADMIN_MENU_SLUG ] ) ) { | |
| 369 | + return; | |
| 370 | + } | |
| 371 | + | |
| 372 | + remove_submenu_page( static::ADMIN_MENU_SLUG, static::ADMIN_MENU_SLUG ); | |
| 373 | + | |
| 374 | + $add_new_slug = 'post-new.php?post_type=' . static::CPT; | |
| 375 | + $category_slug = 'edit-tags.php?taxonomy=' . static::TAXONOMY_CATEGORY_SLUG . '&post_type=' . static::CPT; | |
| 376 | + | |
| 377 | + $library_submenu = new Collection( $submenu[ static::ADMIN_MENU_SLUG ] ); | |
| 378 | + | |
| 379 | + $add_new_item = $library_submenu->find( function ( $item ) use ( $add_new_slug ) { | |
| 380 | + return $add_new_slug === $item[2]; | |
| 381 | + } ); | |
| 382 | + | |
| 383 | + $categories_item = $library_submenu->find( function ( $item ) use ( $category_slug ) { | |
| 384 | + return $category_slug === $item[2]; | |
| 385 | + } ); | |
| 386 | + | |
| 387 | + if ( $add_new_item ) { | |
| 388 | + remove_submenu_page( static::ADMIN_MENU_SLUG, $add_new_slug ); | |
| 389 | + | |
| 390 | + $admin_menu->register( admin_url( static::ADMIN_MENU_SLUG . '#add_new' ), new Add_New_Template_Menu_Item() ); | |
| 391 | + } | |
| 392 | + | |
| 393 | + if ( $categories_item ) { | |
| 394 | + remove_submenu_page( static::ADMIN_MENU_SLUG, $category_slug ); | |
| 395 | + | |
| 396 | + $admin_menu->register( $category_slug, new Templates_Categories_Menu_Item() ); | |
| 397 | + } | |
| 398 | + } | |
| 399 | + | |
| 400 | + /** | |
| 401 | + * Add a `current` CSS class to the `Saved Templates` submenu page when it's active. | |
| 402 | + * It should work by default, but since we interfere with WordPress' builtin CPT menus it doesn't work properly. | |
| 403 | + * | |
| 404 | + * @return void | |
| 405 | + */ | |
| 406 | + private function admin_menu_set_current() { | |
| 407 | + global $submenu; | |
| 408 | + | |
| 409 | + if ( $this->is_current_screen() ) { | |
| 410 | + $library_submenu = &$submenu[ static::ADMIN_MENU_SLUG ]; | |
| 411 | + $library_title = $this->get_library_title(); | |
| 412 | + | |
| 413 | + foreach ( $library_submenu as &$item ) { | |
| 414 | + if ( $library_title === $item[0] ) { | |
| 415 | + if ( ! isset( $item[4] ) ) { | |
| 416 | + $item[4] = ''; | |
| 417 | + } | |
| 418 | + $item[4] .= ' current'; | |
| 419 | + } | |
| 420 | + } | |
| 421 | + } | |
| 422 | + } | |
| 423 | + | |
| 424 | + private function register_admin_menu( Admin_Menu_Manager $admin_menu ) { | |
| 425 | + if ( ! $this->is_editor_one_active() ) { | |
| 426 | + $admin_menu->register( static::get_admin_url( true ), new Saved_Templates_Menu_Item() ); | |
| 427 | + } | |
| 428 | + } | |
| 429 | + | |
| 356 | 430 | private function register_editor_one_menu( Menu_Data_Provider $menu_data_provider ) { |
| 357 | 431 | $menu_data_provider->register_menu( new Editor_One_Templates_Menu() ); |
| 358 | 432 | $menu_data_provider->register_menu( new Editor_One_Saved_Templates_Menu() ); |
| 359 | 433 | } |
| 360 | 434 | |
| 435 | + private function is_editor_one_active(): bool { | |
| 436 | + return (bool) Plugin::instance()->modules_manager->get_modules( 'editor-one' ); | |
| 437 | + } | |
| 438 | + | |
| 361 | 439 | public function admin_title( $admin_title, $title ) { |
| 362 | 440 | $library_title = $this->get_library_title(); |
| 363 | 441 | |
| 364 | 442 | if ( $library_title ) { |
| @@ -742,12 +820,8 @@ | ||
| 742 | 820 | |
| 743 | 821 | $data['page_settings'] = $page->get_data( 'settings' ); |
| 744 | 822 | } |
| 745 | 823 | |
| 746 | - if ( ! empty( $content ) ) { | |
| 747 | - $this->attach_global_styles_to_data( $data, $content, $template_id ); | |
| 748 | - } | |
| 749 | - | |
| 750 | 824 | return $data; |
| 751 | 825 | } |
| 752 | 826 | |
| 753 | 827 | /** |
| @@ -922,9 +996,9 @@ | ||
| 922 | 996 | * @param string $name - The file name. |
| 923 | 997 | * @param string $path - The file path. |
| 924 | 998 | * @return \WP_Error|array An array of items on success, 'WP_Error' on failure. |
| 925 | 999 | */ |
| 926 | - public function import_template( $name, $path, $import_mode = 'match_site' ) { | |
| 1000 | + public function import_template( $name, $path ) { | |
| 927 | 1001 | if ( empty( $path ) ) { |
| 928 | 1002 | return new \WP_Error( 'file_error', 'Please upload a file to import' ); |
| 929 | 1003 | } |
| 930 | 1004 | |
| @@ -949,9 +1023,9 @@ | ||
| 949 | 1023 | if ( false !== strpos( $file_path, '__MACOSX' ) || '.' === basename( $file_path )[0] ) { |
| 950 | 1024 | continue; |
| 951 | 1025 | } |
| 952 | 1026 | |
| 953 | - $import_result = $this->import_single_template( $file_path, $import_mode ); | |
| 1027 | + $import_result = $this->import_single_template( $file_path ); | |
| 954 | 1028 | |
| 955 | 1029 | if ( is_wp_error( $import_result ) ) { |
| 956 | 1030 | // Skip failed files |
| 957 | 1031 | continue; |
| @@ -963,9 +1037,9 @@ | ||
| 963 | 1037 | // Delete the temporary extraction directory, since it's now not necessary. |
| 964 | 1038 | Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] ); |
| 965 | 1039 | } else { |
| 966 | 1040 | // If the import file is a single JSON file |
| 967 | - $import_result = $this->import_single_template( $path, $import_mode ); | |
| 1041 | + $import_result = $this->import_single_template( $path ); | |
| 968 | 1042 | |
| 969 | 1043 | if ( is_wp_error( $import_result ) ) { |
| 970 | 1044 | return $import_result; |
| 971 | 1045 | } |
| @@ -1528,10 +1602,10 @@ | ||
| 1528 | 1602 | * |
| 1529 | 1603 | * @return \WP_Error|int|array Local template array, or template ID, or |
| 1530 | 1604 | * `WP_Error`. |
| 1531 | 1605 | */ |
| 1532 | - private function import_single_template( $file_path, $import_mode = 'match_site' ) { | |
| 1533 | - $data = $this->prepare_import_template_data( $file_path, $import_mode ); | |
| 1606 | + private function import_single_template( $file_path ) { | |
| 1607 | + $data = $this->prepare_import_template_data( $file_path ); | |
| 1534 | 1608 | |
| 1535 | 1609 | if ( is_wp_error( $data ) ) { |
| 1536 | 1610 | return $data; |
| 1537 | 1611 | } |
| @@ -1587,10 +1661,8 @@ | ||
| 1587 | 1661 | 'title' => $document->get_main_post()->post_title, |
| 1588 | 1662 | 'type' => self::get_template_type( $template_id ), |
| 1589 | 1663 | ]; |
| 1590 | 1664 | |
| 1591 | - $this->attach_global_styles_to_data( $export_data, $content, $template_id ); | |
| 1592 | - | |
| 1593 | 1665 | return [ |
| 1594 | 1666 | 'name' => 'elementor-' . $template_id . '-' . gmdate( 'Y-m-d' ) . '.json', |
| 1595 | 1667 | 'content' => wp_json_encode( $export_data ), |
| 1596 | 1668 | ]; |
| @@ -1736,8 +1808,12 @@ | ||
| 1736 | 1808 | * @access private |
| 1737 | 1809 | */ |
| 1738 | 1810 | private function add_actions() { |
| 1739 | 1811 | if ( is_admin() ) { |
| 1812 | + add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) { | |
| 1813 | + $this->register_admin_menu( $admin_menu ); | |
| 1814 | + }, static::ADMIN_MENU_PRIORITY ); | |
| 1815 | + | |
| 1740 | 1816 | add_action( 'admin_init', [ $this, 'redirect_categories_page_to_saved_templates_page' ] ); |
| 1741 | 1817 | |
| 1742 | 1818 | add_action( 'elementor/editor-one/menu/register', function ( Menu_Data_Provider $menu_data_provider ) { |
| 1743 | 1819 | $this->register_editor_one_menu( $menu_data_provider ); |
| @@ -1760,8 +1836,16 @@ | ||
| 1760 | 1836 | |
| 1761 | 1837 | return $post_types; |
| 1762 | 1838 | } ); |
| 1763 | 1839 | |
| 1840 | + add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) { | |
| 1841 | + $this->admin_menu_reorder( $admin_menu ); | |
| 1842 | + }, 800 ); | |
| 1843 | + | |
| 1844 | + add_action( 'elementor/admin/menu/after_register', function () { | |
| 1845 | + $this->admin_menu_set_current(); | |
| 1846 | + } ); | |
| 1847 | + | |
| 1764 | 1848 | add_filter( 'admin_title', [ $this, 'admin_title' ], 10, 2 ); |
| 1765 | 1849 | add_action( 'all_admin_notices', [ $this, 'replace_admin_heading' ] ); |
| 1766 | 1850 | add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 ); |
| 1767 | 1851 | add_action( 'admin_footer', [ $this, 'admin_import_template_form' ] ); |
| @@ -1996,18 +2080,33 @@ | ||
| 1996 | 2080 | if ( null === $this->wordpress_adapter ) { |
| 1997 | 2081 | $this->set_wordpress_adapter( new WordPress_Adapter() ); |
| 1998 | 2082 | } |
| 1999 | 2083 | |
| 2084 | + if ( ! $this->should_check_permissions( $args ) ) { | |
| 2085 | + return true; | |
| 2086 | + } | |
| 2087 | + | |
| 2000 | 2088 | $post_id = intval( $args['template_id'] ); |
| 2001 | 2089 | $post_status = $this->wordpress_adapter->get_post_status( $post_id ); |
| 2090 | + $is_private_or_non_published = ( 'private' === $post_status && ! $this->wordpress_adapter->current_user_can( 'read_private_posts', $post_id ) ) || ( 'publish' !== $post_status ); | |
| 2002 | 2091 | |
| 2003 | - if ( 'publish' === $post_status && ! post_password_required( $post_id ) ) { | |
| 2004 | - return true; | |
| 2092 | + $can_read_template = $is_private_or_non_published || $this->wordpress_adapter->current_user_can( 'edit_post', $post_id ); | |
| 2093 | + | |
| 2094 | + return apply_filters( 'elementor/template-library/is_allowed_to_read_template', $can_read_template, $args ); | |
| 2095 | + } | |
| 2096 | + | |
| 2097 | + private function should_check_permissions( array $args ): bool { | |
| 2098 | + if ( null === $this->elementor_adapter ) { | |
| 2099 | + $this->set_elementor_adapter( new Elementor_Adapter() ); | |
| 2005 | 2100 | } |
| 2006 | 2101 | |
| 2007 | - $can_read_template = $this->wordpress_adapter->current_user_can( 'edit_post', $post_id ); | |
| 2102 | + $check_permissions = isset( $args['check_permissions'] ) && false === $args['check_permissions']; | |
| 2008 | 2103 | |
| 2009 | - return apply_filters( 'elementor/template-library/is_allowed_to_read_template', $can_read_template, $args ); | |
| 2104 | + if ( $check_permissions ) { | |
| 2105 | + return false; | |
| 2106 | + } | |
| 2107 | + | |
| 2108 | + return true; | |
| 2010 | 2109 | } |
| 2011 | 2110 | |
| 2012 | 2111 | public function set_wordpress_adapter( Wordpress_Adapter_Interface $wordpress_adapter ) { |
| 2013 | 2112 | $this->wordpress_adapter = $wordpress_adapter; |