import_service = ImportService::get_instance(); $this->post_meta_services = PostMetaServices::get_instance(); $this->niches = NicheLoader::get_instance()->get_niches(); } public function install_niche( string $niche_id, array $allowed_content, $builder = 'gutenberg' ) { $result = array(); if ( ! isset( $this->niches[ $niche_id ] ) ) { throw new \Error( 'Niche not found' ); } update_option( 'brandy_current_niche', $niche_id ); if ( in_array( 'posts', $allowed_content, true ) ) { $this->install_template( $niche_id ); $this->reset_wp_templates(); $result['posts'] = $this->import_posts( $niche_id, $builder ); } if ( in_array( 'products', $allowed_content, true ) ) { $result['products'] = $this->import_sample_products( $niche_id ); } if ( in_array( 'pages', $allowed_content, true ) ) { $result['pages'] = $this->import_pages( $niche_id, $builder ); } if ( in_array( 'menus', $allowed_content, true ) ) { $result['menus'] = $this->import_menus( $niche_id ); } if ( in_array( 'widgets', $allowed_content, true ) ) { $result['imported_widgets'] = $this->import_widgets( $niche_id ); } return $result; } private function install_template( string $niche_id ) { if ( ! isset( $this->niches[ $niche_id ] ) ) { return; } do_action( 'brandy_before_' . $niche_id . '_installing_template_settings' ); $selected_niche = $this->niches[ $niche_id ]; if ( isset( $selected_niche['template']['button'] ) ) { $this->override_button_settings( $selected_niche['template']['button'] ); } if ( isset( $selected_niche['template']['headers'] ) ) { $this->override_builder_settings( $niche_id, $selected_niche['template']['headers'], 'header' ); } if ( isset( $selected_niche['template']['footers'] ) ) { $this->override_builder_settings( $niche_id, $selected_niche['template']['footers'], 'footer' ); } if ( isset( $selected_niche['template']['woocommerce'] ) ) { $this->override_woocommerce_settings( $selected_niche['template']['woocommerce'] ); } do_action( 'brandy_after_' . $niche_id . '_installing_template_settings' ); } private function import_sample_products( $niche_id ) { if ( ! isset( $this->niches[ $niche_id ] ) ) { return array(); } $selected_niche = $this->niches[ $niche_id ]; if ( ! isset( $selected_niche['sample_products'] ) || ! file_exists( $selected_niche['sample_products'] ) ) { return array(); } $csv_file = $selected_niche['sample_products']; $imported_products = $this->import_service::read_products_from_csv( $csv_file ); return $imported_products; } private function import_posts( $niche_id, $builder = 'gutenberg' ) { if ( ! isset( $this->niches[ $niche_id ] ) ) { return array(); } $selected_niche = $this->niches[ $niche_id ]; if ( ! isset( $selected_niche['sample_posts'][ $builder ] ) || ! file_exists( $selected_niche['sample_posts'][ $builder ] ) ) { return array(); } $csv_file = $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; } 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( array_merge( array( 'id' => $inserted_id ), $post_info ) ); } do_action( 'brandy_after_import_posts', $niche_id, $inserted_posts ); return $inserted_posts; } private function import_pages( $niche_id, $builder = 'gutenberg' ) { if ( ! isset( $this->niches[ $niche_id ] ) ) { return; } $selected_niche = $this->niches[ $niche_id ]; if ( ! isset( $selected_niche['sample_pages'][ $builder ] ) || ! file_exists( $selected_niche['sample_pages'][ $builder ] ) ) { return array(); } $csv_file = $selected_niche['sample_pages'][ $builder ]; $sample_pages = $this->import_service::read_posts_from_csv( $csv_file ); do_action( 'brandy_before_import_pages', $niche_id, $sample_pages ); $query = new \WP_Query( array( 'post_type' => 'page', '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 ) { $imported_pages[] = array( 'id' => $p->ID, 'name' => $p->post_name, 'title' => $p->post_title, ); $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'], ); 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['page_display'] ) && 'front_page' === $page_info['page_display'] ) { update_option( 'show_on_front', 'page' ); update_option( 'page_on_front', $new_post_id ); } if ( ! empty( $page_info['page_display'] ) && 'blogs_page' === $page_info['page_display'] ) { update_option( 'show_on_front', 'page' ); update_option( 'page_for_posts', $new_post_id ); } } do_action( 'brandy_after_import_pages', $niche_id, $imported_pages ); return $inserted_pages; } private function import_menus( $niche_id ) { if ( ! isset( $this->niches[ $niche_id ] ) ) { return; } $selected_niche = $this->niches[ $niche_id ]; if ( ! isset( $selected_niche['sample_menus'] ) || ! is_array( $selected_niche['sample_menus'] ) ) { return array(); } $sample_menus = $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 ); foreach ( $menu_info['items'] as $menu_item ) { wp_update_nav_menu_item( $menu_id, 0, empty( $menu_item['object_id'] ) ? array( 'menu-item-title' => $menu_item['title'], 'menu-item-url' => $menu_item['url'], 'menu-item-status' => $menu_item['status'], ) : array( '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'], ) ); } } do_action( 'brandy_after_import_menus', $niche_id, $imported_menus ); return $imported_menus; } private function import_widgets( $niche_id ) { update_option( 'brandy_sample_widgets', $niche_id ); } public static function register_widgets() { $niche_id = get_option( 'brandy_sample_widgets' ); $import_service = ImportService::get_instance(); $niches = NicheLoader::get_instance()->get_niches(); if ( ! isset( $niches[ $niche_id ] ) ) { return; } $selected_niche = $niches[ $niche_id ]; 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' => '