base && self::CPT === $current_screen->post_type;
}
/**
* Add template type.
*
* Register new template type to the list of supported local template types.
*
* @since 1.0.3
* @access public
* @static
*
* @param string $type Template type.
*/
public static function add_template_type( $type ) {
self::$template_types[ $type ] = $type;
}
/**
* Remove template type.
*
* Remove existing template type from the list of supported local template
* types.
*
* @since 1.8.0
* @access public
* @static
*
* @param string $type Template type.
*/
public static function remove_template_type( $type ) {
if ( isset( self::$template_types[ $type ] ) ) {
unset( self::$template_types[ $type ] );
}
}
public static function get_admin_url( $relative = false ) {
$base_url = self::ADMIN_MENU_SLUG;
if ( ! $relative ) {
$base_url = admin_url( $base_url );
}
return add_query_arg( 'tabs_group', 'library', $base_url );
}
/**
* Get local template ID.
*
* Retrieve the local template ID.
*
* @since 1.0.0
* @access public
*
* @return string The local template ID.
*/
public function get_id() {
return 'local';
}
/**
* Get local template title.
*
* Retrieve the local template title.
*
* @since 1.0.0
* @access public
*
* @return string The local template title.
*/
public function get_title() {
return esc_html__( 'Local', 'elementor' );
}
/**
* Register local template data.
*
* Used to register custom template data like a post type, a taxonomy or any
* other data.
*
* The local template class registers a new `elementor_library` post type
* and an `elementor_library_type` taxonomy. They are used to store data for
* local templates saved by the user on his site.
*
* @since 1.0.0
* @access public
*/
public function register_data() {
$labels = [
'name' => esc_html_x( 'My Templates', 'Template Library', 'elementor' ),
'singular_name' => esc_html_x( 'Template', 'Template Library', 'elementor' ),
'add_new' => esc_html__( 'Add New Template', 'elementor' ),
'add_new_item' => esc_html__( 'Add New Template', 'elementor' ),
'edit_item' => esc_html__( 'Edit Template', 'elementor' ),
'new_item' => esc_html__( 'New Template', 'elementor' ),
'all_items' => esc_html__( 'All Templates', 'elementor' ),
'view_item' => esc_html__( 'View Template', 'elementor' ),
'search_items' => esc_html__( 'Search Template', 'elementor' ),
'not_found' => esc_html__( 'No Templates found', 'elementor' ),
'not_found_in_trash' => esc_html__( 'No Templates found in Trash', 'elementor' ),
'parent_item_colon' => esc_html__( 'Parent Template:', 'elementor' ),
'menu_name' => esc_html_x( 'Templates', 'Template Library', 'elementor' ),
];
$args = [
'labels' => $labels,
'public' => true,
'rewrite' => false,
'menu_icon' => 'dashicons-admin-page',
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'exclude_from_search' => true,
'capability_type' => 'post',
'hierarchical' => false,
'supports' => [ 'title', 'thumbnail', 'author', 'elementor', 'custom-fields' ],
'show_in_rest' => true,
];
$this->avoid_rest_access_for_non_admins();
/**
* Register template library post type args.
*
* Filters the post type arguments when registering elementor template library post type.
*
* @since 1.0.0
*
* @param array $args Arguments for registering a post type.
*/
$args = apply_filters( 'elementor/template_library/sources/local/register_post_type_args', $args );
$this->post_type_object = register_post_type( self::CPT, $args );
$args = [
'hierarchical' => false,
'show_ui' => false,
'show_in_nav_menus' => false,
'show_admin_column' => true,
'query_var' => is_admin(),
'rewrite' => false,
'public' => false,
'label' => esc_html_x( 'Type', 'Template Library', 'elementor' ),
];
/**
* Register template library taxonomy args.
*
* Filters the taxonomy arguments when registering elementor template library taxonomy.
*
* @since 1.0.0
*
* @param array $args Arguments for registering a taxonomy.
*/
$args = apply_filters( 'elementor/template_library/sources/local/register_taxonomy_args', $args );
$cpts_to_associate = [ self::CPT ];
/**
* Custom post types to associate.
*
* Filters the list of custom post types when registering elementor template library taxonomy.
*
* @since 1.0.0
*
* @param array $cpts_to_associate Custom post types. Default is `elementor_library` post type.
*/
$cpts_to_associate = apply_filters( 'elementor/template_library/sources/local/register_taxonomy_cpts', $cpts_to_associate );
register_taxonomy( self::TAXONOMY_TYPE_SLUG, $cpts_to_associate, $args );
/**
* Categories
*/
$args = [
'hierarchical' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'show_admin_column' => true,
'query_var' => is_admin(),
'rewrite' => false,
'public' => false,
'labels' => [
'name' => esc_html_x( 'Categories', 'Template Library', 'elementor' ),
'singular_name' => esc_html_x( 'Category', 'Template Library', 'elementor' ),
'all_items' => esc_html_x( 'All Categories', 'Template Library', 'elementor' ),
],
];
/**
* Register template library category args.
*
* Filters the category arguments when registering elementor template library category.
*
* @since 2.4.0
*
* @param array $args Arguments for registering a category.
*/
$args = apply_filters( 'elementor/template_library/sources/local/register_category_args', $args );
register_taxonomy( self::TAXONOMY_CATEGORY_SLUG, self::CPT, $args );
}
private function register_editor_one_menu( Menu_Data_Provider $menu_data_provider ) {
$menu_data_provider->register_menu( new Editor_One_Templates_Menu() );
$menu_data_provider->register_menu( new Editor_One_Saved_Templates_Menu() );
}
public function admin_title( $admin_title, $title ) {
$library_title = $this->get_library_title();
if ( $library_title ) {
$admin_title = str_replace( $title, $library_title, $admin_title );
}
return $admin_title;
}
public function replace_admin_heading() {
if ( ! $this->is_current_screen() ) {
return;
}
global $post_type_object;
$library_title = $this->get_library_title();
if ( $library_title ) {
$post_type_object->labels->name = $library_title;
}
$labels = $this->get_current_template_labels();
$template_singular = $labels['singular'];
$template_plural = $labels['plural'];
if ( ! $template_plural && $library_title ) {
$template_plural = $library_title;
}
if ( $template_singular ) {
$labels = $post_type_object->labels;
$labels->name = $template_plural;
$labels->singular_name = $template_singular;
/* translators: %s: Template label (plural). */
$labels->all_items = sprintf( esc_html__( 'All %s', 'elementor' ), $template_plural );
/* translators: %s: Template label (singular). */
$labels->add_new = sprintf( esc_html__( 'Add New %s', 'elementor' ), $template_singular );
$labels->add_new_item = $labels->add_new;
/* translators: %s: Template label (singular). */
$labels->edit_item = sprintf( esc_html__( 'Edit %s', 'elementor' ), $template_singular );
/* translators: %s: Template label (singular). */
$labels->new_item = sprintf( esc_html__( 'New %s', 'elementor' ), $template_singular );
/* translators: %s: Template label (singular). */
$labels->view_item = sprintf( esc_html__( 'View %s', 'elementor' ), $template_singular );
/* translators: %s: Template label (plural). */
$labels->search_items = sprintf( esc_html__( 'Search %s', 'elementor' ), $template_plural );
/* translators: %s: Template label (plural). */
$labels->not_found = sprintf( esc_html__( 'No %s found', 'elementor' ), $template_plural );
/* translators: %s: Template label (plural). */
$labels->not_found_in_trash = sprintf( esc_html__( 'No %s found in Trash', 'elementor' ), $template_plural );
/* translators: %s: Template label (singular). */
$labels->parent_item_colon = sprintf( esc_html__( 'Parent %s:', 'elementor' ), $template_singular );
$labels->menu_name = $template_plural;
$post_type_object->labels = $labels;
}
}
/**
* Get local templates.
*
* Retrieve local templates saved by the user on his site.
*
* @since 1.0.0
* @access public
*
* @param array $args Optional. Filter templates based on a set of
* arguments. Default is an empty array.
*
* @return array Local templates.
*/
public function get_items( $args = [] ) {
$template_types = array_values( self::$template_types );
if ( ! empty( $args['type'] ) ) {
$template_types = $args['type'];
unset( $args['type'] );
}
$defaults = [
'post_type' => self::CPT,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => [
[
'key' => Document::TYPE_META_KEY,
'value' => $template_types,
],
],
];
$query_args = wp_parse_args( $args, $defaults );
$templates_query = new \WP_Query( $query_args );
$templates = [];
if ( $templates_query->have_posts() ) {
foreach ( $templates_query->get_posts() as $post ) {
$templates[] = $this->get_item( $post->ID );
}
}
return $templates;
}
/**
* Save local template.
*
* Save new or update existing template on the database.
*
* @since 1.0.0
* @access public
*
* @param array $template_data Local template data.
*
* @return \WP_Error|int The ID of the saved/updated template, `WP_Error` otherwise.
*/
public function save_item( $template_data ) {
if ( ! current_user_can( $this->post_type_object->cap->edit_posts ) ) {
return new \WP_Error( 'save_error', esc_html__( 'Access denied.', 'elementor' ) );
}
$defaults = [
'title' => esc_html__( '(no title)', 'elementor' ),
'page_settings' => [],
];
$template_data = wp_parse_args( $template_data, $defaults );
$template_data['status'] = current_user_can( 'publish_posts' ) ? 'publish' : 'pending';
// BC: Allow importing any template type when using CLI
// to support users that rely on this mechanism.
$should_check_template_type = ! $this->is_wp_cli();
if (
$should_check_template_type &&
! $this->is_valid_template_type( $template_data['type'] )
) {
return new \WP_Error( 'invalid_template_type', esc_html__( 'Invalid template type.', 'elementor' ) );
}
$document = Plugin::$instance->documents->create(
$template_data['type'],
[
'post_title' => $template_data['title'],
'post_status' => $template_data['status'],
]
);
if ( is_wp_error( $document ) ) {
/**
* @var \WP_Error $document
*/
return $document;
}
if ( ! empty( $template_data['content'] ) ) {
$template_data['content'] = $this->replace_elements_ids( $template_data['content'] );
}
$document->save( [
'elements' => $template_data['content'],
'settings' => $template_data['page_settings'],
] );
$template_id = $document->get_main_id();
/**
* After template library save.
*
* Fires after Elementor template library was saved.
*
* @since 1.0.1
*
* @param int $template_id The ID of the template.
* @param array $template_data The template data.
*/
do_action( 'elementor/template-library/after_save_template', $template_id, $template_data );
/**
* After template library update.
*
* Fires after Elementor template library was updated.
*
* @since 1.0.1
*
* @param int $template_id The ID of the template.
* @param array $template_data The template data.
*/
do_action( 'elementor/template-library/after_update_template', $template_id, $template_data );
return $template_id;
}
protected function is_valid_template_type( $type ) {
$document_class = Plugin::$instance->documents->get_document_type( $type, false );
if ( ! $document_class ) {
return false;
}
$cpt = $document_class::get_property( 'cpt' );
if ( ! $cpt || ! is_array( $cpt ) || 1 !== count( $cpt ) ) {
return false;
}
$is_valid_template_type = in_array( static::CPT, $cpt, true );
return apply_filters(
'elementor/template_library/sources/local/is_valid_template_type',
$is_valid_template_type,
$cpt,
);
}
/** For testing purposes only, in order to be able to mock the `WP_CLI` constant. */
protected function is_wp_cli() {
return Utils::is_wp_cli();
}
/**
* Update local template.
*
* Update template on the database.
*
* @since 1.0.0
* @access public
*
* @param array $new_data New template data.
*
* @return \WP_Error|true True if template updated, `WP_Error` otherwise.
*/
public function update_item( $new_data ) {
if ( ! current_user_can( $this->post_type_object->cap->edit_post, $new_data['id'] ) ) {
return new \WP_Error( 'save_error', esc_html__( 'Access denied.', 'elementor' ) );
}
$document = Plugin::$instance->documents->get( $new_data['id'] );
if ( ! $document ) {
return new \WP_Error( 'save_error', esc_html__( 'Template not exist.', 'elementor' ) );
}
$save_data = [];
if ( isset( $new_data['title'] ) ) {
$save_data['post_title'] = $new_data['title'];
}
if ( isset( $new_data['content'] ) ) {
$save_data['elements'] = $new_data['content'];
}
$document->save( $save_data );
/**
* After template library update.
*
* Fires after Elementor template library was updated.
*
* @since 1.0.0
*
* @param int $new_data_id The ID of the new template.
* @param array $new_data The new template data.
*/
do_action( 'elementor/template-library/after_update_template', $new_data['id'], $new_data );
return true;
}
/**
* Get local template.
*
* Retrieve a single local template saved by the user on his site.
*
* @since 1.0.0
* @access public
*
* @param int $template_id The template ID.
*
* @return array Local template.
*/
public function get_item( $template_id ) {
$post = get_post( $template_id );
$user = get_user_by( 'id', $post->post_author );
$page = SettingsManager::get_settings_managers( 'page' )->get_model( $template_id );
$page_settings = $page->get_data( 'settings' );
$date = strtotime( $post->post_date );
$data = [
'template_id' => $post->ID,
'source' => $this->get_id(),
'type' => self::get_template_type( $post->ID ),
'title' => $post->post_title,
'thumbnail' => get_the_post_thumbnail_url( $post ),
'date' => $date,
'human_date' => date_i18n( get_option( 'date_format' ), $date ),
'human_modified_date' => date_i18n( get_option( 'date_format' ), strtotime( $post->post_modified ) ),
'author' => $user->display_name,
'status' => $post->post_status,
'hasPageSettings' => ! empty( $page_settings ),
'tags' => [],
'export_link' => $this->get_export_link( $template_id ),
'url' => get_permalink( $post->ID ),
];
/**
* Get template library template.
*
* Filters the template data when retrieving a single template from the
* template library.
*
* @since 1.0.0
*
* @param array $data Template data.
*/
$data = apply_filters( 'elementor/template-library/get_template', $data );
return $data;
}
/**
* Get template data.
*
* Retrieve the data of a single local template saved by the user on his site.
*
* @since 1.5.0
* @access public
*
* @param array $args Custom template arguments.
*
* @return array Local template data.
*/
public function get_data( array $args ) {
$template_id = $args['template_id'];
$document = Plugin::$instance->documents->get( $template_id );
$content = [];
if ( $document ) {
// TODO: Validate the data (in JS too!).
if ( ! empty( $args['display'] ) ) {
$content = $document->get_elements_raw_data( null, true );
} else {
$content = $document->get_elements_data();
}
if ( ! empty( $content ) ) {
$content = $this->replace_elements_ids( $content );
/**
* Filters the template content data.
*
* This filter allows third-party plugins to modify template content
* when loaded via get_template_data(), making the behavior consistent
* with Frontend::get_builder_content().
*
* @since 3.34.0
*
* @param array $content The template content data.
* @param int $template_id The template ID.
*/
$content = apply_filters( 'elementor/frontend/builder_content_data', $content, $template_id );
}
}
$data = [
'content' => $content,
];
if ( ! empty( $args['with_page_settings'] ) ) {
$page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['template_id'] );
$data['page_settings'] = $page->get_data( 'settings' );
}
if ( ! empty( $content ) ) {
$this->attach_global_styles_to_data( $data, $content, $template_id );
}
return $data;
}
/**
* Delete local template.
*
* Delete template from the database.
*
* @since 1.0.0
* @access public
*
* @param int $template_id The template ID.
*
* @return \WP_Post|\WP_Error|false|null Post data on success, false or null
* or 'WP_Error' on failure.
*/
public function delete_template( $template_id ) {
if ( ! current_user_can( $this->post_type_object->cap->delete_post, $template_id ) ) {
return new \WP_Error( 'template_error', esc_html__( 'Access denied.', 'elementor' ) );
}
return wp_delete_post( $template_id, true );
}
public function bulk_delete_items( array $template_ids ) {
foreach ( $template_ids as $template_id ) {
if ( ! current_user_can( $this->post_type_object->cap->delete_post, $template_id ) ) {
return new \WP_Error( 'template_error', esc_html__( 'Access denied.', 'elementor' ) );
}
}
foreach ( $template_ids as $template_id ) {
wp_delete_post( $template_id, true );
}
return true;
}
/**
* Export local template.
*
* Export template to a file.
*
* @since 1.0.0
* @access public
*
* @param int $template_id The template ID.
*
* @return \WP_Error WordPress error if template export failed.
*/
public function export_template( $template_id ) {
$permissions_error = $this->validate_template_export_permissions( $template_id );
if ( is_wp_error( $permissions_error ) ) {
return $permissions_error;
}
$file_data = $this->prepare_template_export( $template_id );
if ( is_wp_error( $file_data ) ) {
return $file_data;
}
$this->send_file_headers( $file_data['name'], strlen( $file_data['content'] ) );
$this->serve_file( $file_data['content'] );
die;
}
private function validate_template_export_permissions( $template_id ) {
$post_id = intval( $template_id );
if ( get_post_type( $post_id ) !== self::CPT ) {
return new \WP_Error( 'template_error', esc_html__( 'Invalid template type or template does not exist.', 'elementor' ) );
}
$post_status = get_post_status( $post_id );
if ( 'private' === $post_status && ! current_user_can( 'read_private_posts', $post_id ) ) {
return new \WP_Error( 'template_error', esc_html__( 'You do not have permission to access this template.', 'elementor' ) );
}
if ( 'publish' !== $post_status && ! current_user_can( 'edit_post', $post_id ) ) {
return new \WP_Error( 'template_error', esc_html__( 'You do not have permission to export this template.', 'elementor' ) );
}
return null;
}
/**
* Export multiple local templates.
*
* Export multiple template to a ZIP file.
*
* @since 1.6.0
* @access public
*
* @param array $template_ids An array of template IDs.
*
* @return \WP_Error WordPress error if export failed.
*/
public function export_multiple_templates( array $template_ids ) {
$files = [];
$wp_upload_dir = wp_upload_dir();
$temp_path = $wp_upload_dir['basedir'] . '/' . self::TEMP_FILES_DIR;
// Create temp path if it doesn't exist
wp_mkdir_p( $temp_path );
// Create all json files
foreach ( $template_ids as $template_id ) {
$file_data = $this->prepare_template_export( $template_id );
if ( is_wp_error( $file_data ) ) {
continue;
}
$complete_path = $temp_path . '/' . $file_data['name'];
$put_contents = file_put_contents( $complete_path, $file_data['content'] );
if ( ! $put_contents ) {
return new \WP_Error( '404', sprintf( 'Cannot create file "%s".', $file_data['name'] ) );
}
$files[] = [
'path' => $complete_path,
'name' => $file_data['name'],
];
}
if ( ! $files ) {
return new \WP_Error( 'empty_files', 'There is no files to export (probably all the requested templates are empty).' );
}
// Create temporary .zip file
$zip_archive_filename = 'elementor-templates-' . gmdate( 'Y-m-d' ) . '.zip';
$zip_archive = new \ZipArchive();
$zip_complete_path = $temp_path . '/' . $zip_archive_filename;
$zip_archive->open( $zip_complete_path, \ZipArchive::CREATE );
foreach ( $files as $file ) {
$zip_archive->addFile( $file['path'], $file['name'] );
}
$zip_archive->close();
foreach ( $files as $file ) {
unlink( $file['path'] );
}
$this->send_file_headers( $zip_archive_filename, $this->filesize( $zip_complete_path ) );
$this->serve_zip( $zip_complete_path );
unlink( $zip_complete_path );
die;
}
/**
* Import local template.
*
* Import template from a file.
*
* @since 1.0.0
* @access public
*
* @param string $name - The file name.
* @param string $path - The file path.
* @return \WP_Error|array An array of items on success, 'WP_Error' on failure.
*/
public function import_template( $name, $path, $import_mode = 'match_site' ) {
if ( empty( $path ) ) {
return new \WP_Error( 'file_error', 'Please upload a file to import' );
}
// Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
$items = [];
// If the import file is a Zip file with potentially multiple JSON files
if ( 'zip' === pathinfo( $name, PATHINFO_EXTENSION ) ) {
$extracted_files = Plugin::$instance->uploads_manager->extract_and_validate_zip( $path, [ 'json' ] );
if ( is_wp_error( $extracted_files ) ) {
// Delete the temporary extraction directory, since it's now not necessary.
Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
return $extracted_files;
}
foreach ( $extracted_files['files'] as $file_path ) {
// Skip macOS metadata files and folders
if ( false !== strpos( $file_path, '__MACOSX' ) || '.' === basename( $file_path )[0] ) {
continue;
}
$import_result = $this->import_single_template( $file_path, $import_mode );
if ( is_wp_error( $import_result ) ) {
// Skip failed files
continue;
}
$items[] = $import_result;
}
// Delete the temporary extraction directory, since it's now not necessary.
Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
} else {
// If the import file is a single JSON file
$import_result = $this->import_single_template( $path, $import_mode );
if ( is_wp_error( $import_result ) ) {
return $import_result;
}
$items[] = $import_result;
}
return $items;
}
/**
* Post row actions.
*
* Add an export link to the template library action links table list.
*
* Fired by `post_row_actions` filter.
*
* @since 1.0.0
* @access public
*
* @param array $actions An array of row action links.
* @param \WP_Post $post The post object.
*
* @return array An updated array of row action links.
*/
public function post_row_actions( $actions, \WP_Post $post ) {
if ( self::is_base_templates_screen() ) {
if ( $this->is_template_supports_export( $post->ID ) ) {
$template_labels = $this->get_template_labels_by_type( self::get_template_type( $post->ID ) );
$export_label = esc_html__( 'Export Template', 'elementor' );
if ( $template_labels['singular'] ) {
/* translators: %s: Template label (singular). */
$export_label = sprintf( esc_html__( 'Export %s', 'elementor' ), $template_labels['singular'] );
}
$actions['export-template'] = sprintf( '%2$s', $this->get_export_link( $post->ID ), $export_label );
}
}
return $actions;
}
/**
* Admin import template form.
*
* The import form displayed in "My Library" screen in WordPress dashboard.
*
* The form allows the user to import template in json/zip format to the site.
*
* Fired by `admin_footer` action.
*
* @since 1.0.0
* @access public
*/
public function admin_import_template_form() {
if ( ! self::is_base_templates_screen() || ! User::is_current_user_can_upload_json() ) {
return;
}
/** @var \Elementor\Core\Common\Modules\Ajax\Module $ajax */
$ajax = Plugin::$instance->common->get_component( 'ajax' );
$template_labels = $this->get_current_template_labels();
$import_label = esc_html__( 'Import Templates', 'elementor' );
$description = esc_html__( 'Choose an Elementor template JSON file or a .zip archive of Elementor templates, and add them to the list of templates available in your library.', 'elementor' );
if ( $template_labels['plural'] ) {
/* translators: %s: Template label (plural). */
$import_label = sprintf( esc_html__( 'Import %s', 'elementor' ), $template_labels['plural'] );
}
if ( $template_labels['singular'] && $template_labels['plural'] ) {
$description = sprintf(
/* translators: 1: Template label (singular). 2: Template label (plural). */
esc_html__( 'Choose an Elementor %1$s JSON file or a .zip archive of Elementor %2$s, and add them to the list of %2$s available in your library.', 'elementor' ),
$template_labels['singular'],
$template_labels['plural']
);
}
?>
post_type && isset( $post_states['elementor'] ) ) {
unset( $post_states['elementor'] );
}
return $post_states;
}
/**
* Get template export link.
*
* Retrieve the link used to export a single template based on the template
* ID.
*
* @since 2.0.0
* @access private
*
* @param int $template_id The template ID.
*
* @return string Template export URL.
*/
private function get_export_link( $template_id ) {
// TODO: BC since 2.3.0 - Use `$ajax->create_nonce()`
/** @var \Elementor\Core\Common\Modules\Ajax\Module $ajax */
// $ajax = Plugin::$instance->common->get_component( 'ajax' );
return add_query_arg(
[
'action' => 'elementor_library_direct_actions',
'library_action' => 'export_template',
'source' => $this->get_id(),
'_nonce' => wp_create_nonce( 'elementor_ajax' ),
'template_id' => $template_id,
],
admin_url( 'admin-ajax.php' )
);
}
/**
* On template save.
*
* Run this method when template is being saved.
*
* Fired by `save_post` action.
*
* @since 1.0.1
* @access public
*
* @param int $post_id Post ID.
* @param \WP_Post $post The current post object.
*/
public function on_save_post( $post_id, \WP_Post $post ) {
if ( self::CPT !== $post->post_type ) {
return;
}
if ( self::get_template_type( $post_id ) ) { // It's already with a type
return;
}
// Don't save type on import, the importer will do it.
if ( did_action( 'import_start' ) ) {
return;
}
$this->save_item_type( $post_id, 'page' );
}
/**
* Save item type.
*
* When saving/updating templates, this method is used to update the post
* meta data and the taxonomy.
*
* @since 1.0.1
* @access private
*
* @param int $post_id Post ID.
* @param string $type Item type.
*/
private function save_item_type( $post_id, $type ) {
update_post_meta( $post_id, Document::TYPE_META_KEY, $type );
wp_set_object_terms( $post_id, $type, self::TAXONOMY_TYPE_SLUG );
}
/**
* Bulk export action.
*
* Adds an 'Export' action to the Bulk Actions drop-down in the template
* library.
*
* Fired by `bulk_actions-edit-elementor_library` filter.
*
* @since 1.6.0
* @access public
*
* @param array $actions An array of the available bulk actions.
*
* @return array An array of the available bulk actions.
*/
public function admin_add_bulk_export_action( $actions ) {
$actions[ self::BULK_EXPORT_ACTION ] = esc_html__( 'Export', 'elementor' );
return $actions;
}
/**
* Add bulk export action.
*
* Handles the template library bulk export action.
*
* Fired by `handle_bulk_actions-edit-elementor_library` filter.
*
* @since 1.6.0
* @access public
*
* @param string $redirect_to The redirect URL.
* @param string $action The action being taken.
* @param array $post_ids The items to take the action on.
*/
public function admin_export_multiple_templates( $redirect_to, $action, $post_ids ) {
if ( self::BULK_EXPORT_ACTION === $action ) {
$result = $this->export_multiple_templates( $post_ids );
// If you reach this line, the export failed
// PHPCS - Not user input.
wp_die( $result->get_error_message() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
/**
* Print admin tabs.
*
* Used to output the template library tabs with their labels.
*
* Fired by `views_edit-elementor_library` filter.
*
* @since 2.0.0
* @access public
*
* @param array $views An array of available list table views.
*
* @return array An updated array of available list table views.
*/
public function admin_print_tabs( $views ) {
//phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
$current_type = Utils::get_super_global_value( $_REQUEST, self::TAXONOMY_TYPE_SLUG ) ?? '';
$active_class = $current_type ? '' : ' nav-tab-active';
$current_tabs_group = $this->get_current_tab_group();
$url_args = [
'post_type' => self::CPT,
'tabs_group' => $current_tabs_group,
];
$baseurl = add_query_arg( $url_args, admin_url( 'edit.php' ) );
$filter = [
'admin_tab_group' => $current_tabs_group,
];
$operator = 'and';
if ( empty( $current_tabs_group ) ) {
// Don't include 'not-supported' or other templates that don't set their `admin_tab_group`.
$operator = 'NOT';
}
$doc_types = Plugin::$instance->documents->get_document_types( $filter, $operator );
if ( 1 >= count( $doc_types ) ) {
return $views;
}
?>