array( 'label' => __( 'Restaurant', 'sofrah-order-board' ), 'name' => 'Sofrah Demo Kitchen', 'tagline' => __( 'Pickup orders, confirmed by the kitchen', 'sofrah-order-board' ), 'address' => "12 Harbour Street\nPort Town", 'phone' => '0100 000 0000', 'hours' => array( 0 => array( '12:00', '20:00' ), 1 => array( '11:00', '22:00' ), 2 => array( '11:00', '22:00' ), 3 => array( '11:00', '22:00' ), 4 => array( '11:00', '22:00' ), 5 => array( '11:00', '23:00' ), 6 => array( '11:00', '23:00' ) ), 'style' => '', 'menu' => array( 'Starters' => array( array( 'Lentil soup', '6.00', 'Vegan. Contains: celery.' ), array( 'Hummus and warm flatbread', '5.50', 'Vegan. Contains: sesame, wheat.' ), ), 'Mains' => array( array( 'Falafel plate', '9.00', 'Vegetarian. Falafel, salad, pickles, tahini. Contains: sesame, wheat.' ), array( 'Falafel wrap', '8.50', 'Vegan. Contains: sesame, wheat.' ), array( 'Grilled chicken wrap', '11.00', 'Allergen information not provided.' ), array( 'Lamb kofta', '12.50', 'With rice and salad. Contains: wheat.', 'outofstock' ), ), 'Drinks' => array( array( 'Fresh mint lemonade', '3.50', 'Vegan.' ) ), 'Desserts' => array( array( 'Pistachio baklava', '4.00', 'Vegetarian. Contains: nuts, wheat, milk.' ) ), ), ), 'cafe' => array( 'label' => __( 'Cafe', 'sofrah-order-board' ), 'name' => 'Sofrah Demo Cafe', 'tagline' => __( 'Coffee and bakes, ready to collect', 'sofrah-order-board' ), 'address' => "4 Market Lane\nPort Town", 'phone' => '0100 000 0001', 'hours' => array( 0 => array( '08:30', '16:00' ), 1 => array( '07:30', '17:00' ), 2 => array( '07:30', '17:00' ), 3 => array( '07:30', '17:00' ), 4 => array( '07:30', '17:00' ), 5 => array( '07:30', '17:00' ), 6 => array( '08:30', '16:00' ) ), 'style' => 'cafe', 'menu' => array( 'Coffee' => array( array( 'Flat white', '3.20', 'Contains: milk. Oat milk on request at the counter.' ), array( 'Cappuccino', '3.20', 'Contains: milk.' ), array( 'Espresso', '2.20', 'Vegan.' ), array( 'Iced latte', '3.60', 'Contains: milk.' ), ), 'Tea and cold' => array( array( 'Fresh mint tea', '2.50', 'Vegan.' ), array( 'Sparkling lemonade', '2.80', 'Vegan.' ), ), 'Bakes' => array( array( 'Almond croissant', '3.40', 'Vegetarian. Contains: nuts, wheat, milk, egg.' ), array( 'Cardamom bun', '3.00', 'Vegetarian. Contains: wheat, milk, egg.' ), array( 'Banana bread', '2.80', 'Allergen information not provided.', 'outofstock' ), ), 'Breakfast' => array( array( 'Shakshuka with bread', '8.50', 'Vegetarian. Contains: egg, wheat.' ), array( 'Avocado toast', '7.50', 'Vegan. Contains: wheat.' ), ), ), ), ); } /** * Imports a demo. * * @param string $which restaurant or cafe. * @return array What was created. */ function sofrah_board_import_demo( $which ) { $demos = sofrah_board_demos(); if ( ! isset( $demos[ $which ] ) || ! function_exists( 'wc_get_products' ) ) { return array(); } $d = $demos[ $which ]; $done = array( 'products' => 0, 'images' => 0, 'pages' => array() ); // Earlier demo dishes go to the bin and their demo photographs are deleted; the owner's own dishes and media stay. foreach ( wc_get_products( array( 'limit' => -1, 'status' => array( 'publish', 'draft', 'private' ), 'meta_key' => '_sofrah_board_demo', 'meta_compare' => 'EXISTS' ) ) as $p ) { // phpcs:ignore WordPress.DB.SlowDBQuery if ( '' !== (string) $p->get_meta( '_sofrah_board_demo' ) ) { // Checked again: only items the demo created. $p->delete( false ); } } foreach ( get_posts( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'numberposts' => -1, 'fields' => 'ids', 'meta_key' => '_sofrah_board_demo', 'meta_compare' => 'EXISTS' ) ) as $att ) { // phpcs:ignore WordPress.DB.SlowDBQuery if ( '' !== (string) get_post_meta( $att, '_sofrah_board_demo', true ) ) { wp_delete_attachment( $att, true ); } } $order = 0; foreach ( $d['menu'] as $cat => $items ) { $term = term_exists( $cat, 'product_cat' ); if ( ! $term ) { $term = wp_insert_term( $cat, 'product_cat' ); } $term_id = (int) $term['term_id']; update_term_meta( $term_id, 'order', $order++ ); update_term_meta( $term_id, '_sofrah_board_demo', $which ); foreach ( $items as $i => $it ) { $p = new WC_Product_Simple(); $p->set_name( $it[0] ); $p->set_regular_price( $it[1] ); $p->set_short_description( $it[2] ); $p->set_category_ids( array( $term_id ) ); $p->set_menu_order( $i ); $p->set_status( 'publish' ); $p->set_stock_status( $it[3] ?? 'instock' ); $p->update_meta_data( '_sofrah_board_demo', $which ); $p->save(); /** * A photograph for a demo dish: the path of a local image file, or empty for none. The Sofrah theme supplies its own. * * @param string $file Path to an image file, or empty. * @param string $which restaurant or cafe. * @param string $name The dish name. */ $file = (string) apply_filters( 'sofrah_board_demo_image', '', $which, $it[0] ); $img = $file ? sofrah_board_demo_attach( $file, $it[0], $p->get_id(), $which ) : 0; if ( $img ) { $p->set_image_id( $img ); $p->save(); ++$done['images']; } ++$done['products']; } } // The other demo's sections go once they are empty; sections the owner made are never touched. // The tag is checked here, term by term: WooCommerce's ordering of product categories can drop a meta query. foreach ( get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false ) ) as $t ) { $tag = (string) get_term_meta( $t->term_id, '_sofrah_board_demo', true ); if ( '' === $tag || $tag === $which || (int) get_option( 'default_product_cat' ) === (int) $t->term_id ) { continue; } if ( ! wc_get_products( array( 'limit' => 1, 'return' => 'ids', 'status' => array( 'publish', 'draft', 'private' ), 'category' => array( $t->slug ) ) ) ) { wp_delete_term( $t->term_id, 'product_cat' ); } } // Venue and hours. $hours = array(); for ( $day = 0; $day < 7; $day++ ) { $hours[ $day ] = isset( $d['hours'][ $day ] ) ? array( 'open' => $d['hours'][ $day ][0], 'close' => $d['hours'][ $day ][1] ) : array( 'open' => '', 'close' => '' ); } update_option( 'sofrah_board_venue', array( 'address' => $d['address'], 'phone' => $d['phone'], 'hours' => $hours, 'last_order' => 30, 'closures' => array(), 'paused' => false, ) ); update_option( 'blogname', $d['name'] ); update_option( 'blogdescription', $d['tagline'] ); // Pay at pickup: WooCommerce's offline method, renamed and turned on. $cod = (array) get_option( 'woocommerce_cod_settings', array() ); $cod['enabled'] = 'yes'; $cod['title'] = __( 'Pay at pickup', 'sofrah-order-board' ); $cod['description'] = __( 'Pay when you collect your order.', 'sofrah-order-board' ); $cod['instructions'] = __( 'Pay when you collect your order.', 'sofrah-order-board' ); $cod['enable_for_virtual'] = 'yes'; update_option( 'woocommerce_cod_settings', $cod ); $s = sofrah_board_settings(); $s['methods'] = array_values( array_unique( array_merge( (array) $s['methods'], array( 'cod' ) ) ) ); update_option( 'sofrah_board_settings', $s ); // Pages. With the Sofrah theme active they are built from its patterns, so they can be edited block by block. $pattern = function ( $slug, $fallback ) { $reg = class_exists( 'WP_Block_Patterns_Registry' ) ? WP_Block_Patterns_Registry::get_instance()->get_registered( $slug ) : null; return $reg ? $reg['content'] : $fallback; }; $pages = array( 'home' => array( $d['name'], $pattern( 'sofrah/home-' . $which, '
' . esc_html( $d['tagline'] ) . '
' ), 'page-no-title' ), 'about' => array( __( 'About', 'sofrah-order-board' ), $pattern( 'sofrah/about-' . $which, '' . esc_html__( 'Tell your story here.', 'sofrah-order-board' ) . '
' ), 'page-no-title' ), 'contact' => array( __( 'Contact', 'sofrah-order-board' ), $pattern( 'sofrah/contact', '[sofrah_venue field="address"]' ), 'page-no-title' ), ); foreach ( $pages as $key => $pg ) { $existing = get_posts( array( 'post_type' => 'page', 'meta_key' => '_sofrah_board_demo_page', 'meta_value' => $key, 'post_status' => 'any', 'fields' => 'ids' ) ); // phpcs:ignore WordPress.DB.SlowDBQuery $args = array( 'post_type' => 'page', 'post_status' => 'publish', 'post_title' => $pg[0], 'post_name' => 'home' === $key ? 'home' : $key, 'post_content' => $pg[1], ); if ( $existing ) { $args['ID'] = $existing[0]; $id = wp_update_post( $args ); } else { $id = wp_insert_post( $args ); update_post_meta( $id, '_sofrah_board_demo_page', $key ); } if ( $pg[2] && 'sofrah' === get_template() ) { update_post_meta( $id, '_wp_page_template', $pg[2] ); } $done['pages'][ $key ] = $id; } update_option( 'show_on_front', 'page' ); update_option( 'page_on_front', $done['pages']['home'] ); $shop = wc_get_page_id( 'shop' ); if ( $shop > 0 ) { wp_update_post( array( 'ID' => $shop, 'post_title' => __( 'Menu', 'sofrah-order-board' ), 'post_name' => 'menu' ) ); } do_action( 'sofrah_board_demo_imported', $which, $d, $done ); flush_rewrite_rules( false ); return $done; } /** * Copies a demo photograph into the media library, tagged so the next import can remove it. * * @param string $file Path to a local image file. * @param string $title The dish name, also used as the picture's text alternative. * @param int $parent The dish. * @param string $which restaurant or cafe. * @return int The attachment, or 0. */ function sofrah_board_demo_attach( $file, $title, $parent, $which ) { if ( ! is_readable( $file ) || ! wp_check_filetype( $file, array( 'jpg|jpeg|jpe' => 'image/jpeg', 'png' => 'image/png', 'webp' => 'image/webp' ) )['type'] ) { return 0; } require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $tmp = wp_tempnam( basename( $file ) ); if ( ! $tmp || ! copy( $file, $tmp ) ) { return 0; } $id = media_handle_sideload( array( 'name' => basename( $file ), 'tmp_name' => $tmp ), $parent, $title ); if ( is_wp_error( $id ) ) { wp_delete_file( $tmp ); return 0; } update_post_meta( $id, '_wp_attachment_image_alt', $title ); update_post_meta( $id, '_sofrah_board_demo', $which ); return (int) $id; } /** * The demo section of the settings screen. */ function sofrah_board_demo_section() { // The import changes the site's title, tagline, front page and look, which only an administrator may change. if ( ! current_user_can( 'manage_options' ) ) { return; } $msg = ''; if ( isset( $_POST['sofrah_board_demo'] ) && check_admin_referer( 'sofrah_board_demo' ) ) { $which = sanitize_key( wp_unslash( $_POST['sofrah_board_demo'] ) ); $done = sofrah_board_import_demo( $which ); /* translators: 1: dish count, 2: demo name */ $msg = $done ? sprintf( __( '%1$d dishes, the venue, opening hours, Pay at pickup and the Home, About and Contact pages were set up for the %2$s demo.', 'sofrah-order-board' ), $done['products'], sofrah_board_demos()[ $which ]['label'] ) : __( 'The demo could not be imported.', 'sofrah-order-board' ); } ?>