documents->get( $template_id ); $elements = $document ? $document->get_elements_data() : array(); $conditions = Conditions::get_template_data( $template_id ); return array( 'format' => self::FORMAT, 'version' => BUILDIFY_VERSION, 'title' => get_the_title( $template_id ), 'type' => Template_Types::of( $template_id ), 'content' => $elements, 'page_settings' => Locations::document_settings( $template_id ), 'conditions' => $conditions['conditions'], 'visibility' => $conditions['visibility'], 'priority' => (int) get_post_meta( $template_id, Post_Type::PRIORITY_META, true ), ); } /** * Downloads one or more templates as a JSON file. */ public function handle_export() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified right below. $ids = isset( $_GET['ids'] ) ? array_filter( array_map( 'absint', explode( ',', sanitize_text_field( wp_unslash( $_GET['ids'] ) ) ) ) ) : array(); check_admin_referer( 'buildify_export' ); if ( ! $ids || ! current_user_can( Post_Type::capability() ) ) { wp_die( esc_html__( 'You are not allowed to export these templates.', 'buildify' ), '', array( 'response' => 403 ) ); } $templates = array(); foreach ( $ids as $id ) { if ( Post_Type::POST_TYPE === get_post_type( $id ) && current_user_can( 'edit_post', $id ) ) { $templates[] = self::get_export_data( $id ); } } if ( ! $templates ) { wp_die( esc_html__( 'No templates found.', 'buildify' ) ); } $name = 1 === count( $templates ) ? sanitize_file_name( 'buildify-' . $templates[0]['type'] . '-' . $templates[0]['title'] ) : 'buildify-templates-' . gmdate( 'Y-m-d' ); $data = 1 === count( $templates ) ? $templates[0] : array( 'format' => self::FORMAT . 's', 'version' => BUILDIFY_VERSION, 'templates' => $templates, ); nocache_headers(); header( 'Content-Type: application/json; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . $name . '.json"' ); echo wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); exit; } /** * Imports templates from an uploaded JSON file. */ public function handle_import() { check_admin_referer( 'buildify_import' ); // Elementor data may contain HTML and scripts, so only administrators can import. if ( ! current_user_can( 'manage_options' ) || ! current_user_can( Post_Type::capability() ) ) { wp_die( esc_html__( 'You are not allowed to import templates.', 'buildify' ), '', array( 'response' => 403 ) ); } $redirect = admin_url( 'admin.php?page=buildify-settings&tab=tools' ); // Only the scalar fields of the upload are read, each one sanitized. $_FILES is not slashed by WordPress. $tmp_name = isset( $_FILES['buildify_file']['tmp_name'] ) ? sanitize_text_field( (string) $_FILES['buildify_file']['tmp_name'] ) : ''; $name = isset( $_FILES['buildify_file']['name'] ) ? sanitize_file_name( (string) $_FILES['buildify_file']['name'] ) : ''; $size = isset( $_FILES['buildify_file']['size'] ) ? absint( $_FILES['buildify_file']['size'] ) : 0; $error = isset( $_FILES['buildify_file']['error'] ) ? absint( $_FILES['buildify_file']['error'] ) : UPLOAD_ERR_NO_FILE; if ( UPLOAD_ERR_OK !== $error || '' === $tmp_name || ! is_uploaded_file( $tmp_name ) ) { wp_safe_redirect( add_query_arg( 'buildify_notice', 'import_missing', $redirect ) ); exit; } if ( 'json' !== strtolower( pathinfo( $name, PATHINFO_EXTENSION ) ) || $size > 10 * MB_IN_BYTES ) { wp_safe_redirect( add_query_arg( 'buildify_notice', 'import_invalid', $redirect ) ); exit; } $raw = file_get_contents( $tmp_name ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local uploaded file, checked with is_uploaded_file(). $data = json_decode( (string) $raw, true ); $created = self::import_data( $data ); wp_safe_redirect( add_query_arg( array( 'buildify_notice' => $created ? 'imported' : 'import_invalid', 'count' => $created, ), $redirect ) ); exit; } /** * Imports decoded data. * * @param mixed $data Decoded JSON. * @return int Number of templates created. */ public static function import_data( $data ) { if ( ! is_array( $data ) || empty( $data['format'] ) ) { return 0; } $items = array(); if ( self::FORMAT === $data['format'] ) { $items[] = $data; } elseif ( self::FORMAT . 's' === $data['format'] && ! empty( $data['templates'] ) && is_array( $data['templates'] ) ) { $items = array_slice( $data['templates'], 0, 100 ); } $created = 0; foreach ( $items as $item ) { if ( self::create_from_data( $item, 'draft' ) ) { ++$created; } } return $created; } /** * Creates a template from export data. * * @param array $item Template data. * @param string $status Post status. * @return int Template ID or 0. */ public static function create_from_data( $item, $status = 'draft' ) { if ( ! is_array( $item ) || empty( $item['type'] ) || ! Template_Types::exists( (string) $item['type'] ) ) { return 0; } $type = (string) $item['type']; $title = isset( $item['title'] ) ? sanitize_text_field( (string) $item['title'] ) : Template_Types::label( $type ); $document = \Elementor\Plugin::$instance->documents->create( Template_Types::doc_type( $type ), array( 'post_title' => $title, 'post_status' => $status, 'post_type' => Post_Type::POST_TYPE, ), array( Post_Type::TYPE_META => $type, ) ); if ( is_wp_error( $document ) || ! $document ) { return 0; } // Imported data is never trusted: the element tree and every setting are sanitized, and new element IDs avoid clashes. $elements = self::sanitize_elements( isset( $item['content'] ) && is_array( $item['content'] ) ? $item['content'] : array() ); $settings = self::sanitize_settings( isset( $item['page_settings'] ) && is_array( $item['page_settings'] ) ? $item['page_settings'] : array() ); $document->save( array( 'elements' => $elements, 'settings' => $settings, ) ); $template_id = $document->get_main_id(); if ( isset( $item['conditions'] ) || isset( $item['visibility'] ) ) { Conditions::save_template_data( $template_id, isset( $item['conditions'] ) ? $item['conditions'] : array(), isset( $item['visibility'] ) ? $item['visibility'] : array(), isset( $item['priority'] ) ? (int) $item['priority'] : 0 ); } return $template_id; } /** * Sanitizes an imported Elementor element tree. * * Only known element types and keys are kept, widget names are sanitized as keys, * every setting goes through sanitize_settings() and each element gets a new ID. * * @param array $elements Elements. * @param int $depth Current nesting depth. * @return array */ public static function sanitize_elements( $elements, $depth = 0 ) { $clean = array(); if ( ! is_array( $elements ) || $depth > 20 ) { return $clean; } foreach ( $elements as $element ) { if ( ! is_array( $element ) || empty( $element['elType'] ) || ! in_array( $element['elType'], array( 'container', 'section', 'column', 'widget' ), true ) ) { continue; } $item = array( 'id' => \Elementor\Utils::generate_random_string(), 'elType' => $element['elType'], 'isInner' => ! empty( $element['isInner'] ), 'settings' => self::sanitize_settings( isset( $element['settings'] ) ? $element['settings'] : array() ), 'elements' => self::sanitize_elements( isset( $element['elements'] ) ? $element['elements'] : array(), $depth + 1 ), ); if ( 'widget' === $element['elType'] ) { $widget = isset( $element['widgetType'] ) ? sanitize_key( (string) $element['widgetType'] ) : ''; if ( '' === $widget ) { continue; } $item['widgetType'] = $widget; } $clean[] = $item; if ( count( $clean ) >= 500 ) { break; } } return $clean; } /** * Sanitizes imported settings recursively. * * Keys may only contain letters, numbers, underscores and hyphens. Text goes through * wp_kses_post(), so scripts, event handlers and unsafe URLs are removed; numbers and * booleans are kept as they are. * * @param mixed $settings Settings. * @param int $depth Current nesting depth. * @return array */ public static function sanitize_settings( $settings, $depth = 0 ) { $clean = array(); if ( ! is_array( $settings ) || $depth > 10 ) { return $clean; } foreach ( $settings as $key => $value ) { if ( ! is_int( $key ) && ! preg_match( '/^[A-Za-z0-9_\-]{1,100}$/', (string) $key ) ) { continue; } if ( is_array( $value ) ) { $clean[ $key ] = self::sanitize_settings( $value, $depth + 1 ); } elseif ( is_bool( $value ) || is_int( $value ) || is_float( $value ) || null === $value ) { $clean[ $key ] = $value; } elseif ( is_string( $value ) ) { $clean[ $key ] = wp_kses_post( $value ); } } return $clean; } /** * Bundled starter designs. * * @return array slug => array( title, type, file ) */ public static function starter_designs() { $designs = array(); $dir = BUILDIFY_PATH . 'library/'; foreach ( (array) glob( $dir . '*.json' ) as $file ) { $slug = basename( $file, '.json' ); $data = json_decode( (string) file_get_contents( $file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Bundled local file. if ( is_array( $data ) && ! empty( $data['type'] ) && Template_Types::exists( $data['type'] ) ) { $designs[ $slug ] = array( 'title' => isset( $data['title'] ) ? (string) $data['title'] : $slug, 'type' => (string) $data['type'], 'file' => $file, ); } } return $designs; } /** * Creates a new template (blank or from a starter design) and opens it in Elementor. */ public function handle_new_template() { check_admin_referer( 'buildify_new_template' ); if ( ! current_user_can( Post_Type::capability() ) ) { wp_die( esc_html__( 'You are not allowed to create templates.', 'buildify' ), '', array( 'response' => 403 ) ); } $type = isset( $_POST['template_type'] ) ? sanitize_key( wp_unslash( $_POST['template_type'] ) ) : ''; $title = isset( $_POST['template_title'] ) ? sanitize_text_field( wp_unslash( $_POST['template_title'] ) ) : ''; $design = isset( $_POST['template_design'] ) ? sanitize_key( wp_unslash( $_POST['template_design'] ) ) : ''; if ( ! Template_Types::exists( $type ) ) { wp_safe_redirect( admin_url( 'admin.php?page=buildify' ) ); exit; } if ( '' === $title ) { /* translators: %s: template type. */ $title = sprintf( __( 'My %s', 'buildify' ), Template_Types::label( $type ) ); } $template_id = 0; $designs = self::starter_designs(); if ( '' !== $design && isset( $designs[ $design ] ) && $designs[ $design ]['type'] === $type ) { $data = json_decode( (string) file_get_contents( $designs[ $design ]['file'] ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Bundled local file. if ( is_array( $data ) ) { $data['title'] = $title; unset( $data['conditions'], $data['visibility'] ); $template_id = self::create_from_data( $data, 'draft' ); } } if ( ! $template_id ) { $document = \Elementor\Plugin::$instance->documents->create( Template_Types::doc_type( $type ), array( 'post_title' => $title, 'post_status' => 'draft', 'post_type' => Post_Type::POST_TYPE, ), array( Post_Type::TYPE_META => $type, ) ); $template_id = ( $document && ! is_wp_error( $document ) ) ? $document->get_main_id() : 0; } if ( ! $template_id ) { wp_die( esc_html__( 'The template could not be created.', 'buildify' ) ); } // Sensible default conditions so a published template works right away. $defaults = self::default_conditions( $type ); if ( $defaults ) { Conditions::save_template_data( $template_id, $defaults, array(), 0 ); } $document = \Elementor\Plugin::$instance->documents->get( $template_id ); wp_safe_redirect( $document ? $document->get_edit_url() : admin_url( 'post.php?post=' . $template_id . '&action=edit' ) ); exit; } /** * Default conditions of a new template. * * @param string $type Template type. * @return array */ public static function default_conditions( $type ) { $rule = array( 'header' => 'entire_site', 'footer' => 'entire_site', 'popup' => 'entire_site', 'single' => 'singular_post_type', 'archive' => 'archive_all', 'single-product' => 'singular_post_type', 'product-archive' => 'woo_shop', ); if ( ! isset( $rule[ $type ] ) ) { return array(); } $sub = ''; if ( 'single' === $type ) { $sub = 'post'; } elseif ( 'single-product' === $type ) { $sub = 'product'; } $conditions = array( array( 'mode' => 'include', 'rule' => $rule[ $type ], 'sub' => $sub, 'id' => 0, ), ); if ( 'product-archive' === $type ) { $conditions[] = array( 'mode' => 'include', 'rule' => 'archive_post_type', 'sub' => 'product', 'id' => 0, ); } return $conditions; } }