'string' ); register_post_meta( AIEO_Store_CPT::POST_TYPE, self::META_WAY, array( 'type' => 'object', 'single' => true, 'default' => array(), 'sanitize_callback' => array( __CLASS__, 'sanitize_way' ), 'auth_callback' => $auth, 'show_in_rest' => array( 'schema' => array( 'type' => 'object', 'additionalProperties' => false, 'properties' => array( 'title' => $text, 'steps' => array( 'type' => 'array', 'items' => array( 'type' => 'object', 'additionalProperties' => false, 'properties' => array( 'place' => $text, 'text' => $text, 'minutes' => array( 'type' => 'integer' ), ), ), ), 'lines' => array( 'type' => 'array', 'items' => array( 'type' => 'object', 'additionalProperties' => false, 'properties' => array( 'label' => $text, 'text' => $text, 'icon' => $text, ), ), ), ), ), ), ) ); } public static function sanitize_way( $value ) { $value = self::decode( $value ); $out = array( 'title' => sanitize_text_field( (string) ( $value['title'] ?? '' ) ), 'steps' => array(), 'lines' => array(), ); foreach ( (array) ( $value['steps'] ?? array() ) as $s ) { $s = is_array( $s ) ? $s : array(); $place = sanitize_text_field( (string) ( $s['place'] ?? '' ) ); $text = sanitize_text_field( (string) ( $s['text'] ?? '' ) ); if ( '' === $place && '' === $text ) { continue; } $out['steps'][] = array( 'place' => $place, 'text' => $text, 'minutes' => max( 0, min( 600, (int) ( $s['minutes'] ?? 0 ) ) ), ); } foreach ( (array) ( $value['lines'] ?? array() ) as $l ) { $l = is_array( $l ) ? $l : array(); $label = sanitize_text_field( (string) ( $l['label'] ?? '' ) ); $text = sanitize_text_field( (string) ( $l['text'] ?? '' ) ); if ( '' === $label || '' === $text ) { continue; } $icon = (string) ( $l['icon'] ?? '' ); $out['lines'][] = array( 'label' => $label, 'text' => $text, 'icon' => in_array( $icon, self::LINE_ICONS, true ) ? $icon : '', ); } return $out; } const WAY_MAX = 12; public static function way_step_fields() { return array( array( 'key' => 'place', 'label' => _x( 'Place', 'a stop on the way to the shop', 'ai-eshop-optimizer' ), 'required' => true ), array( 'key' => 'text', 'label' => __( 'What to do', 'ai-eshop-optimizer' ), 'width' => 'medium' ), array( 'key' => 'minutes', 'label' => __( 'From the step before', 'ai-eshop-optimizer' ), 'type' => 'number', 'min' => 0, 'max' => 600, 'unit' => _x( 'min', 'minutes, a unit', 'ai-eshop-optimizer' ) ), ); } public static function way_line_fields() { $icons = array(); if ( function_exists( 'rooster_icon' ) && function_exists( 'rooster_has_icon' ) ) { foreach ( self::LINE_ICONS as $slug ) { $svg = rooster_has_icon( $slug ) ? (string) rooster_icon( $slug, array( 'as' => 'svg' ) ) : ''; if ( '' !== $svg ) { $icons[ $slug ] = $svg; } } } return array( array( 'key' => 'label', 'label' => __( 'Label', 'ai-eshop-optimizer' ), 'width' => 'short', 'required' => true ), array( 'key' => 'text', 'label' => __( 'Text', 'ai-eshop-optimizer' ), 'width' => 'medium' ), array( 'key' => 'icon', 'label' => __( 'Icon', 'ai-eshop-optimizer' ), 'type' => 'icon', 'icons' => $icons, 'none' => __( 'No icon', 'ai-eshop-optimizer' ), 'options' => array( 'house' => __( 'Door', 'ai-eshop-optimizer' ), 'shopping-bag' => __( 'Bag', 'ai-eshop-optimizer' ), 'package' => __( 'Parcel', 'ai-eshop-optimizer' ), 'truck' => __( 'Van', 'ai-eshop-optimizer' ), 'map-pin' => _x( 'Pin', 'a map pin, an icon', 'ai-eshop-optimizer' ), 'clock' => __( 'Clock', 'ai-eshop-optimizer' ), 'phone' => __( 'Phone', 'ai-eshop-optimizer' ), 'info' => __( 'Information', 'ai-eshop-optimizer' ), ), ), ); } public static function way( $pid ) { return self::sanitize_way( get_post_meta( (int) $pid, self::META_WAY, true ) ); } private static function decode( $value ) { if ( is_string( $value ) && '' !== $value ) { $value = json_decode( $value, true ); } return is_array( $value ) ? $value : array(); } public static function store_id( $block = null ) { if ( ! class_exists( 'AIEO_Store_CPT' ) ) { return 0; } $pid = ( $block instanceof WP_Block && ! empty( $block->context['postId'] ) ) ? (int) $block->context['postId'] : 0; if ( $pid <= 0 && is_singular( AIEO_Store_CPT::POST_TYPE ) ) { $pid = (int) get_queried_object_id(); } return ( $pid > 0 && AIEO_Store_CPT::POST_TYPE === get_post_type( $pid ) ) ? $pid : 0; } public static function store( $pid ) { $pid = self::canonical( (int) $pid ); $own = $pid > 0 ? get_post( $pid ) : null; if ( ! $own || ! class_exists( 'AIEO_Store_CPT' ) || AIEO_Store_CPT::POST_TYPE !== $own->post_type || 'publish' !== $own->post_status ) { return null; } $local = self::local( $pid ); $post = $local !== $pid && 'publish' === get_post_status( $local ) ? get_post( $local ) : $own; $loc = self::location( $pid ); $name = (string) get_the_title( $post ); $town = trim( (string) ( $loc['locality'] ?? '' ) ); if ( '' === $town || ( $post !== $own && 0 === strcasecmp( $town, trim( (string) get_the_title( $own ) ) ) ) ) { $town = $name; } $tz = class_exists( 'AIEO_Store_Hours' ) ? AIEO_Store_Hours::timezone( $loc, $pid )->getName() : wp_timezone_string(); return array( 'id' => $pid, 'slug' => (string) $own->post_name, 'name' => $name, 'town' => $town, 'timezone' => (string) $tz, 'url' => (string) get_permalink( $post ), 'phone' => trim( (string) ( $loc['phone'] ?? '' ) ), ); } public static function stores() { if ( ! class_exists( 'AIEO_Store_CPT' ) || ! post_type_exists( AIEO_Store_CPT::POST_TYPE ) ) { return array(); } $ids = get_posts( array( 'post_type' => AIEO_Store_CPT::POST_TYPE, 'post_status' => 'publish', 'posts_per_page' => 200, 'orderby' => array( 'menu_order' => 'ASC', 'title' => 'ASC' ), 'fields' => 'ids', 'no_found_rows' => true, ) ); $out = array(); $seen = array(); foreach ( $ids as $id ) { $s = self::store( (int) $id ); if ( $s && ! isset( $seen[ $s['id'] ] ) ) { $seen[ $s['id'] ] = true; unset( $s['phone'] ); $out[] = $s; } } return $out; } public static function canonical( $pid ) { $pid = (int) $pid; if ( $pid > 0 && class_exists( 'AIEO_Store_CPT' ) && has_filter( 'wpml_object_id' ) ) { $def = apply_filters( 'wpml_default_language', null ); if ( is_string( $def ) && '' !== $def ) { $o = (int) apply_filters( 'wpml_object_id', $pid, AIEO_Store_CPT::POST_TYPE, true, $def ); return $o > 0 ? $o : $pid; } } return $pid; } public static function local( $pid ) { $pid = (int) $pid; if ( $pid > 0 && class_exists( 'AIEO_Store_CPT' ) && has_filter( 'wpml_object_id' ) ) { $l = (int) apply_filters( 'wpml_object_id', $pid, AIEO_Store_CPT::POST_TYPE, true ); return $l > 0 ? $l : $pid; } return $pid; } public static function location( $pid ) { if ( ! class_exists( 'AIEO_Google_Reviews_Helpers' ) || ! class_exists( 'AIEO_Google_Reviews_Locations' ) ) { return array(); } $loc = AIEO_Google_Reviews_Helpers::resolve_location_for_post( (int) $pid ); return is_array( $loc ) ? $loc : array(); } public static function people( $store_id ) { if ( ! function_exists( 'rooster_lms_store_people' ) || (int) $store_id <= 0 ) { return array(); } $raw = rooster_lms_store_people( self::canonical( (int) $store_id ) ); $out = array(); foreach ( is_array( $raw ) ? $raw : array() as $p ) { if ( ! is_array( $p ) || empty( $p['user_id'] ) || '' === trim( (string) ( $p['name'] ?? '' ) ) ) { continue; } $name = trim( (string) $p['name'] ); $first = trim( (string) ( $p['first_name'] ?? '' ) ); $out[] = array( 'user_id' => (int) $p['user_id'], 'name' => $name, 'first_name' => '' !== $first ? $first : (string) strtok( $name, ' ' ), 'title' => trim( (string) ( $p['title'] ?? '' ) ), 'about' => trim( (string) ( $p['about'] ?? '' ) ), 'photo_id' => (int) ( $p['photo_id'] ?? 0 ), 'focus' => max( 0, min( 100, (int) ( $p['focus'] ?? 50 ) ) ), 'days' => self::ints( $p['days'] ?? array(), 1, 7 ), 'week' => self::spans( $p['week'] ?? array() ), 'today' => self::spans( $p['today'] ?? array() ), 'next' => ( isset( $p['next'][0], $p['next'][1] ) && (int) $p['next'][1] > (int) $p['next'][0] ) ? array( (int) $p['next'][0], (int) $p['next'][1] ) : null, 'services' => array_values( array_filter( array_map( static function ( $s ) { return is_array( $s ) && '' !== trim( (string) ( $s['name'] ?? '' ) ) ? array( 'id' => (int) ( $s['id'] ?? 0 ), 'slug' => (string) ( $s['slug'] ?? '' ), 'name' => trim( (string) $s['name'] ), 'book_url' => (string) ( $s['book_url'] ?? '' ), ) : null; }, (array) ( $p['services'] ?? array() ) ) ) ), 'elsewhere' => array_values( array_filter( array_map( static function ( $e ) { return is_array( $e ) && ! empty( $e['store_id'] ) ? array( 'store_id' => (int) $e['store_id'], 'days' => self::ints( $e['days'] ?? array(), 1, 7 ) ) : null; }, (array) ( $p['elsewhere'] ?? array() ) ) ) ), ); } return $out; } public static function book_link( $store_id ) { if ( ! function_exists( 'rooster_lms_store_book_link' ) || (int) $store_id <= 0 ) { return null; } $l = rooster_lms_store_book_link( self::canonical( (int) $store_id ) ); return ( is_array( $l ) && '' !== trim( (string) ( $l['label'] ?? '' ) ) && '' !== trim( (string) ( $l['url'] ?? '' ) ) ) ? array( 'label' => trim( (string) $l['label'] ), 'url' => (string) $l['url'] ) : null; } private static function ints( $list, $min, $max ) { $out = array(); foreach ( (array) $list as $v ) { $v = (int) $v; if ( $v >= $min && $v <= $max && ! in_array( $v, $out, true ) ) { $out[] = $v; } } sort( $out ); return $out; } private static function spans( $list ) { $out = array(); foreach ( (array) $list as $s ) { if ( is_array( $s ) && isset( $s[0], $s[1] ) && (int) $s[1] > (int) $s[0] ) { $out[] = array( (int) $s[0], (int) $s[1] ); } } usort( $out, static function ( $a, $b ) { return $a[0] <=> $b[0]; } ); return $out; } public static function register_blocks() { if ( ! class_exists( 'AIEO_Store_Page_Blocks' ) ) { return; } $blocks = array( 'store-staff' => array( 'title' => __( 'Rooster One Shop Staff', 'ai-eshop-optimizer' ), 'description' => __( 'The people of the shop, side by side: the one pointed at opens wide. Their days and whether they are in today, from Rooster LMS Pro\'s appointments; nothing is printed without it.', 'ai-eshop-optimizer' ), 'icon' => 'groups', 'keywords' => array( 'staff', 'people', 'team', 'store', 'shop' ), 'attributes' => array( 'heading' => array( 'type' => 'string', 'default' => '' ) ), 'render' => array( 'AIEO_Store_Page_Blocks', 'render_staff' ), ), 'store-way' => array( 'title' => __( 'Rooster One Shop Way Here', 'ai-eshop-optimizer' ), 'description' => __( 'The way to the shop as steps, from the station to the door, with the lines a visitor needs (access, collection, deliveries) and a link that opens the map app. No map is loaded.', 'ai-eshop-optimizer' ), 'icon' => 'location', 'keywords' => array( 'directions', 'way', 'route', 'store', 'shop' ), 'attributes' => array( 'heading' => array( 'type' => 'string', 'default' => '' ) ), 'render' => array( 'AIEO_Store_Page_Blocks', 'render_way' ), ), 'store-contact' => array( 'title' => __( 'Rooster One Shop Contact', 'ai-eshop-optimizer' ), 'description' => __( 'A shop\'s contact facts for any page: open or closed now, the address with a link to the map, its phone, email and chat apps as its location offers them (always, or during opening hours), and the week\'s hours.', 'ai-eshop-optimizer' ), 'icon' => 'phone', 'keywords' => array( 'contact', 'phone', 'address', 'hours', 'store', 'shop' ), 'attributes' => array( 'locationSlug' => array( 'type' => 'string', 'default' => '' ), 'showState' => array( 'type' => 'boolean', 'default' => true ), 'showAddress' => array( 'type' => 'boolean', 'default' => true ), 'media' => array( 'type' => 'string', 'default' => 'phone,viber,whatsapp,messenger,email' ), 'hours' => array( 'type' => 'string', 'default' => 'grouped', 'enum' => array( 'grouped', 'scale', '' ) ), ), 'render' => array( 'AIEO_Store_Page_Blocks', 'render_contact' ), ), ); foreach ( $blocks as $dir => $b ) { $handle = 'aieo-' . $dir; self::register_style( $handle, 'aieo-' . $dir . '.css' ); register_block_type( __DIR__ . '/blocks/' . $dir, array( 'api_version' => 3, 'title' => $b['title'], 'description' => $b['description'], 'category' => 'widgets', 'icon' => $b['icon'], 'keywords' => $b['keywords'], 'supports' => array( 'align' => array( 'wide', 'full' ), 'anchor' => true ), 'attributes' => $b['attributes'], 'uses_context' => array( 'postId', 'postType' ), 'style' => $handle, 'render_callback' => $b['render'], ) ); } } public static function register_style( $handle, $file ) { if ( wp_style_is( $handle, 'registered' ) ) { return; } $root = dirname( __DIR__, 3 ); $path = $root . '/assets/css/' . $file; wp_register_style( $handle, plugins_url( 'assets/css/' . $file, $root . '/ai-eshop-optimizer.php' ), array(), is_readable( $path ) ? (string) filemtime( $path ) : null ); } public static function need() { self::$need = true; } public static function print_footer() { if ( ! self::$need || self::$printed || is_admin() ) { return; } $dir = __DIR__ . '/'; $file = $dir . 'store-page.js'; $min = $dir . 'store-page.min.js'; if ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) && is_readable( $min ) && ( ! is_readable( $file ) || filemtime( $min ) >= filemtime( $file ) ) ) { $file = $min; } $js = is_readable( $file ) ? trim( (string) file_get_contents( $file ) ) : ''; if ( '' === $js ) { return; } self::$printed = true; echo wp_get_inline_script_tag( str_ireplace( ' self::SCRIPT_ID ) ); } public static function rocket_keep( $list ) { if ( is_array( $list ) ) { $list[] = self::SCRIPT_ID; } return $list; } public static function reset() { self::$need = false; self::$printed = false; } } } if ( ! function_exists( 'aieo_stores' ) ) { function aieo_stores() { return class_exists( 'AIEO_Store_Page' ) ? AIEO_Store_Page::stores() : array(); } } if ( ! function_exists( 'aieo_store' ) ) { function aieo_store( $id ) { return class_exists( 'AIEO_Store_Page' ) ? AIEO_Store_Page::store( (int) $id ) : null; } }