import_service = ImportService::get_instance(); $this->post_meta_services = PostMetaServices::get_instance(); $this->niches = NicheLoader::get_instance()->get_niches(); $this->selected_niche = NicheLoader::get_instance()->get_niche( $niche_id ); } public function install_niche( array $allowed_content, $builder = 'gutenberg' ) { $result = array(); if ( empty( $this->selected_niche ) ) { throw new \Error( 'Niche not found' ); } if ( function_exists( 'wc_set_time_limit' ) ) { \wc_set_time_limit( 0 ); } update_option( 'brandy_current_niche', $this->selected_niche['id'] ); if ( in_array( 'clean_data', $allowed_content, true ) ) { $this->clean_sample_data(); } if ( in_array( 'posts', $allowed_content, true ) ) { $this->reset_data(); $this->install_template(); $result['posts'] = $this->import_posts( $builder ); } if ( in_array( 'products', $allowed_content, true ) ) { $result['products'] = $this->import_sample_products(); } if ( in_array( 'product_categories', $allowed_content, true ) ) { $result['products'] = $this->import_product_categories(); } if ( in_array( 'pages', $allowed_content, true ) ) { $result['pages'] = $this->import_pages( $builder ); } if ( in_array( 'menus', $allowed_content, true ) ) { $result['menus'] = $this->import_menus(); } if ( in_array( 'widgets', $allowed_content, true ) ) { $result['imported_widgets'] = $this->import_widgets(); } return $result; } private function install_template() { if ( empty( $this->selected_niche ) ) { return; } $niche_id = $this->selected_niche['id']; do_action( 'brandy_before_' . $niche_id . '_installing_template_settings' ); if ( isset( $this->selected_niche['template'] ) ) { $this->override_template_settings( $this->selected_niche['template'] ); } do_action( 'brandy_after_' . $niche_id . '_installing_template_settings' ); } private function import_sample_products() { if ( empty( $this->selected_niche ) ) { return array(); } if ( ! isset( $this->selected_niche['sample_products'] ) || ! file_exists( $this->selected_niche['sample_products'] ) ) { return array(); } $csv_file = $this->selected_niche['sample_products']; if ( ! file_exists( $csv_file ) ) { return array(); } $imported_products = $this->import_service::read_products_from_csv( $csv_file ); return $imported_products; } private function import_product_categories() { if ( empty( $this->selected_niche ) ) { return array(); } $product_categories = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false, ) ); if ( ! is_wp_error( $product_categories ) ) { foreach ( $product_categories as $cat ) { if ( strpos( $cat->slug, 'brandy-' ) === 0 ) { } \wp_update_term( $cat->term_id, 'product_cat', array( 'name' => trim( str_replace( 'Brandy', '', $cat->name ) ), ) ); } } $category_images_file = $this->selected_niche['sample_product_category_images'] ?? ''; if ( file_exists( $category_images_file ) ) { try { $images_file_content = file_get_contents( $category_images_file ); $images_data = json_decode( $images_file_content ); $images_data = empty( $images_data ) ? array() : $images_data; } catch ( \Error $error ) { $images_data = array(); } foreach ( $images_data as $cat_slug => $img_source ) { $cat = get_term_by( 'slug', $cat_slug, 'product_cat' ); if ( empty( $cat ) ) { continue; } if ( empty( $cat->term_id ) ) { continue; } if ( empty( $img_source ) ) { continue; } if ( ! empty( \get_term_meta( $cat->term_id, 'thumbnail_id' ) ) ) { continue; } try { $image_id = media_sideload_image( $img_source, 0, '', 'id' ); } catch ( \Error $error ) { $bypass = true; } if ( ! is_wp_error( $image_id ) ) { \update_term_meta( $cat->term_id, 'thumbnail_id', absint( $image_id ) ); } } } $sample_ratings = array( array( 'rate' => 4, 'review' => __( 'Great product! It exceeded my expectations in quality and performance. The only downside is the packaging, which could be more eco-friendly.', 'brandy' ), ), array( 'rate' => 3, 'review' => __( 'Decent product but not worth the price. It works as advertised, but I found similar options for less money. Customer service was helpful, though.', 'brandy' ), ), array( 'rate' => 5, 'review' => __( 'Absolutely love this product! It has made my daily routine so much easier. Highly recommend to anyone looking for reliability and efficiency.', 'brandy' ), ), array( 'rate' => 4, 'review' => __( "Very good product overall. It's sturdy and performs well, but it took a bit longer to arrive than I expected. Still, I'd buy it again!", 'brandy' ), ), array( 'rate' => 1, 'review' => __( "Disappointed with this product. It didn't work as promised, and I had issues with durability. Customer support was slow to respond, which made it worse.", 'brandy' ), ), ); try { $product_tags_ids = get_terms( array( 'taxonomy' => 'product_tag', 'search' => 'brandy-', 'fields' => 'ids', ) ); $args = array( 'posts_per_page' => -1, 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'product_tag', 'field' => 'id', 'terms' => $product_tags_ids, ), ), 'post_type' => 'product', 'orderby' => 'title,', ); $product_query = new \WP_Query( $args ); $imported_products = array( 'imported' => array(), ); if ( $product_query->have_posts() ) { foreach ( $product_query->get_posts() as $post ) { $imported_products['imported'][] = $post->ID; } } if ( ! empty( $imported_products['imported'] ) ) { foreach ( $imported_products['imported'] as $id ) { $random_rating_indexes = array_rand( $sample_ratings, wp_rand( 2, 4 ) ); foreach ( $random_rating_indexes as $random_index ) { if ( empty( $sample_ratings[ $random_index ] ) ) { continue; } $rating_data = $sample_ratings[ $random_index ]; $comment_id = wp_new_comment( array( 'comment_post_ID' => $id, 'comment_content' => $rating_data['review'], 'comment_type' => 'review', 'comment_approved' => true, 'user_ID' => get_current_user_id(), 'comment_author_email' => '', 'comment_author' => wp_get_current_user()->display_name, 'comment_author_url' => '#', ) ); if ( false != $comment_id && ! is_wp_error( $comment_id ) ) { add_comment_meta( $comment_id, 'rating', $rating_data['rate'], true ); } } } } } catch ( \Error $error ) { $bypass = true; } return true; } private function import_posts( $builder = 'gutenberg' ) { if ( empty( $this->selected_niche ) ) { return array(); } $niche_id = $this->selected_niche['id']; if ( ! isset( $this->selected_niche['sample_posts'][ $builder ] ) || ! file_exists( $this->selected_niche['sample_posts'][ $builder ] ) ) { return array(); } $csv_file = $this->selected_niche['sample_posts'][ $builder ]; $sample_posts = $this->import_service::read_posts_from_csv( $csv_file ); do_action( 'brandy_before_import_posts', $niche_id, $sample_posts ); $query = new \WP_Query( array( 'post_type' => 'post', 'post_name__in' => array_map( function( $p ) { return $p['name']; }, $sample_posts ), ) ); $existed_posts_name = array_map( function( $p ) { return $p->post_name; }, $query->have_posts() ? $query->posts : array() ); $not_existed_posts = array_filter( $sample_posts, function( $post_info ) use ( $existed_posts_name ) { return ! in_array( $post_info['name'], $existed_posts_name, true ); } ); $inserted_posts = array(); foreach ( $not_existed_posts as $post_info ) { $comment_status = isset( $post_info['comment_status'] ) ? ( empty( $post_info['comment_status'] ) ? 'closed' : 'open' ) : 'open'; $comments = isset( $post_info['comments'] ) ? $post_info['comments'] : array(); $post_args = array( 'post_title' => $post_info['title'], 'post_type' => 'post', 'post_name' => $post_info['name'], 'post_content' => $post_info['content'], 'post_category' => $post_info['post_category'], 'tags_input' => $post_info['tags_input'], 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_author' => 1, 'menu_order' => 0, 'comment_status' => $comment_status, ); $inserted_id = wp_insert_post( $post_args ); if ( 0 === $inserted_id || is_wp_error( $inserted_id ) ) { continue; } if ( ! empty( $post_info['post_meta'] ) ) { foreach ( $post_info['post_meta'] as $key => $value ) { update_post_meta( $inserted_id, $key, $value ); } } foreach ( $comments as $comment ) { wp_insert_comment( array( 'comment_post_ID' => $inserted_id, 'comment_content' => $comment, ) ); } do_action( 'brandy_after_import_post', $niche_id, $builder, $inserted_id, $post_info ); $inserted_posts[] = $inserted_id; $this->import_service::add_featured_image( $inserted_id, $post_info['featured_image'] ?? '', $post_info['name'] ?? '' ); } do_action( 'brandy_after_import_posts', $niche_id, $inserted_posts ); return $inserted_posts; } private function import_pages( $builder = 'gutenberg' ) { if ( empty( $this->selected_niche ) ) { return; } $niche_id = $this->selected_niche['id']; $sample_pages = $this->import_service::read_posts_from_csv( BRANDY_TEMPLATE_DIR . '/inc/Niches/DefaultNiche/sample-data/sample-pages.json' ); if ( isset( $this->selected_niche['sample_pages'][ $builder ] ) && file_exists( $this->selected_niche['sample_pages'][ $builder ] ) ) { $csv_file = $this->selected_niche['sample_pages'][ $builder ]; $sample_pages = array_merge( $sample_pages, $this->import_service::read_posts_from_csv( $csv_file ) ); } if ( empty( $sample_pages ) ) { return array(); } do_action( 'brandy_before_import_pages', $niche_id, $sample_pages ); $query = new \WP_Query( array( 'post_type' => 'page', 'post_status' => ['publish', 'draft'], 'post_name__in' => array_map( function( $p ) { return $p['name']; }, $sample_pages ), ) ); $existed_pages_name = array(); $imported_pages = array(); if ( $query->have_posts() ) { foreach ( $query->posts as $p ) { $same_pages = array_filter( $sample_pages, function( $item ) use ( $p ) { return $p->post_name === $item['name']; } ); $find_page = end( $same_pages ); $data = array( 'id' => $p->ID, 'name' => $p->post_name, 'title' => $p->post_title, ); if ( $find_page ) { $data['post_meta'] = $find_page['post_meta'] ?? array(); } $imported_pages[] = $data; $existed_pages_name[] = $p->post_name; } } $not_existed_pages = array_filter( $sample_pages, function( $page_info ) use ( $existed_pages_name ) { return ! in_array( $page_info['name'], $existed_pages_name, true ); } ); $inserted_pages = array(); foreach ( $not_existed_pages as $page_info ) { $post_args = array_merge( array( 'post_title' => $page_info['title'], 'post_type' => 'page', 'post_name' => $page_info['name'], 'post_content' => $page_info['content'], 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_author' => 1, 'menu_order' => 0, ), isset( $page_info['page_template'] ) ? array( 'page_template' => $page_info['page_template'], ) : array() ); $new_post_id = wp_insert_post( $post_args ); $inserted_pages[] = $new_post_id; $imported_pages[] = array( 'id' => $new_post_id, 'title' => $page_info['title'], 'name' => $page_info['name'], 'post_meta' => $page_info['post_meta'] ?? array(), ); if ( 0 === $new_post_id || is_wp_error( $new_post_id ) ) { continue; } do_action( 'brandy_after_import_page', $niche_id, $builder, $new_post_id, $page_info ); } foreach ( $imported_pages as $data ) { $matching_items = array_values( array_filter( $sample_pages, function( $item ) use ( $data ) { return $item['name'] == $data['name']; } ) ); if ( count( $matching_items ) < 1 ) { continue; } $page_info = $matching_items[0]; $new_post_id = $data['id']; if ( ! empty( $page_info['name'] ) && 'homepage' === $page_info['name'] ) { update_option( 'show_on_front', 'page' ); update_option( 'page_on_front', $new_post_id ); } if ( ! empty( $page_info['name'] ) && 'blog' === $page_info['name'] ) { update_option( 'show_on_front', 'page' ); update_option( 'page_for_posts', $new_post_id ); } if ( ! empty( $page_info['name'] ) && 'terms-and-conditions' === $page_info['name'] ) { update_option( 'woocommerce_terms_page_id', $new_post_id ); } if ( ! empty( $page_info['name'] ) && 'privacy-policy' === $page_info['name'] ) { update_option( 'wp_page_for_privacy_policy', $new_post_id ); } if ( ! empty( $page_info['name'] ) && 'shop' === $page_info['name'] ) { update_option( 'woocommerce_shop_page_id', $new_post_id ); } if ( ! empty( $page_info['name'] ) && 'cart' === $page_info['name'] ) { update_option( 'woocommerce_cart_page_id', $new_post_id ); } if ( ! empty( $page_info['name'] ) && 'checkout' === $page_info['name'] ) { update_option( 'woocommerce_checkout_page_id', $new_post_id ); } wp_update_post( array_merge( array( 'ID' => $new_post_id, 'post_status' => 'publish', ), ! empty( $page_info['page_template'] ) ? array( 'page_template' => $page_info['page_template'], ) : array() ) ); /** * Remove old meta for pages */ foreach ( ( $this->post_meta_services->services ?? array() ) as $meta_service ) { delete_post_meta( $new_post_id, $meta_service::META_NAME ); } if ( ! empty( $page_info['post_meta'] ) ) { foreach ( $page_info['post_meta'] as $key => $value ) { update_post_meta( $new_post_id, $key, $value ); } } } do_action( 'brandy_after_import_pages', $niche_id, $imported_pages ); return $inserted_pages; } private function import_menus() { if ( empty( $this->selected_niche ) ) { return; } $niche_id = $this->selected_niche['id']; if ( ! isset( $this->selected_niche['sample_menus'] ) || ! is_array( $this->selected_niche['sample_menus'] ) ) { return array(); } $niche_instance = NicheLoader::get_instance()->get_niche_instance( $niche_id ); if ( ! empty( $niche_instance ) && is_callable( array( $niche_instance, 'get_sample_menus' ) ) ) { $sample_menus = $niche_instance::get_sample_menus(); } else { $sample_menus = $this->selected_niche['sample_menus']; } do_action( 'brandy_before_import_menus', $niche_id, $sample_menus ); $imported_menus = array(); foreach ( $sample_menus as $menu_info ) { $menu_exists = wp_get_nav_menu_object( $menu_info['name'] ); if ( $menu_exists ) { $imported_menus[] = array_merge( array( 'id' => $menu_exists->term_id ), $menu_info ); continue; } $menu_id = wp_create_nav_menu( $menu_info['name'] ); if ( is_wp_error( $menu_id ) ) { continue; } $imported_menus[] = array_merge( array( 'id' => $menu_id ), $menu_info ); $parent_menus = array(); foreach ( $menu_info['items'] as $menu_item ) { if ( isset( $menu_item['parent'] ) ) { continue; } $id = wp_update_nav_menu_item( $menu_id, 0, empty( $menu_item['object_id'] ) ? array( 'menu-item-title' => $menu_item['title'] ?? 'Sample item', 'menu-item-url' => $menu_item['url'] ?? '#', 'menu-item-status' => $menu_item['status'] ?? 'publish', ) : array( 'menu-item-title' => $menu_item['title'] ?? 'Sample item', 'menu-item-type' => isset( $menu_item['item_type'] ) ? $menu_item['item_type'] : '', 'menu-item-object-id' => $menu_item['object_id'], 'menu-item-object' => $menu_item['item_object'], 'menu-item-status' => $menu_item['status'], ) ); if ( ! is_wp_error( $id ) && ! empty( $menu_item['key'] ) ) { $parent_menus[ $menu_item['key'] ] = $id; } } foreach ( $menu_info['items'] as $submenu ) { if ( ! isset( $submenu['parent'] ) ) { continue; } if ( ! isset( $parent_menus[ $submenu['parent'] ] ) ) { continue; } $id = wp_update_nav_menu_item( $menu_id, 0, empty( $submenu['object_id'] ) ? array( 'menu-item-title' => $submenu['title'] ?? 'Sample item', 'menu-item-url' => $submenu['url'] ?? '#', 'menu-item-status' => $submenu['status'] ?? 'publish', 'menu-item-parent-id' => $parent_menus[ $submenu['parent'] ], ) : array( 'menu-item-type' => isset( $submenu['item_type'] ) ? $submenu['item_type'] : '', 'menu-item-object-id' => $submenu['object_id'], 'menu-item-object' => $submenu['item_object'], 'menu-item-status' => $submenu['status'], 'menu-item-parent-id' => $parent_menus[ $submenu['parent'] ], ) ); } } do_action( 'brandy_after_import_menus', $niche_id, $imported_menus ); return $imported_menus; } private function import_widgets() { if ( empty( $this->selected_niche ) ) { return; } update_option( 'brandy_sample_widgets', $this->selected_niche['id'] ); } public static function register_widgets() { $niche_id = get_option( 'brandy_sample_widgets' ); $import_service = ImportService::get_instance(); $selected_niche = NicheLoader::get_instance()->get_niche( $niche_id ); if ( empty( $selected_niche ) ) { return; } if ( ! isset( $selected_niche['sample_widgets'] ) || ! file_exists( $selected_niche['sample_widgets'] ) ) { return array(); } $csv_file = $selected_niche['sample_widgets']; $sample_widgets = $import_service::read_widgets_from_csv( $csv_file ); foreach ( $sample_widgets as $widget ) { register_sidebar( array( 'name' => $widget['title'], 'id' => $widget['id'], 'description' => esc_html__( 'Add widgets here:', 'brandy' ), 'before_widget' => '', 'before_title' => '