namespace ] ) || ! in_array( 'cache', $allowed_endpoints[ $this->namespace ] ) ) { $allowed_endpoints[ $this->namespace ][] = 'cache'; $allowed_endpoints[ $this->namespace ][] = 'category/cache'; $allowed_endpoints[ $this->namespace ][] = 'widgets/cache'; } return $allowed_endpoints; } public function register_flutter_routes() { $cache = [ 'args' => [], [ 'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'get_home_data' ], 'permission_callback' => [ $this, 'flutter_get_items_permissions_check' ], ], ]; register_rest_route( $this->namespace, '/cache', $cache ); register_rest_route( $this->namespace_v3, '/cache', $cache ); $categoryCache = [ 'args' => [], [ 'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'get_category_data' ], 'permission_callback' => [ $this, 'flutter_get_items_permissions_check' ], ], ]; register_rest_route( $this->namespace, '/category/cache', $categoryCache ); register_rest_route( $this->namespace_v3, '/category/cache', $categoryCache ); $widgetCache = [ 'args' => [], [ 'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'get_widgets_data' ], 'permission_callback' => [ $this, 'flutter_get_items_permissions_check' ], ], ]; register_rest_route( $this->namespace, '/widgets/cache', $widgetCache ); register_rest_route( $this->namespace_v3, '/widgets/cache', $widgetCache ); } public function flutter_get_items_permissions_check() { return get_option( 'maanstore_purchase_code' ) === '1'; } private function get_config_file_path( $lang ) { if ( ! isset( $lang ) ) { $configs = FlutterUtils::get_all_json_files(); if ( ! empty( $configs ) ) { return FlutterUtils::get_json_file_path( $configs[0] ); } else { return new WP_Error( 'existed_config', "Config file hasn't been uploaded yet.", [ 'status' => 400 ] ); } } else { return FlutterUtils::get_json_file_path( 'config_' . $lang . '.json' ); } } private function arrayWhitelist( $array, $whitelist ) { $results = []; for ( $i = 0; $i < count( $array ); $i++ ) { $results[] = array_intersect_key( $array[ $i ], array_flip( $whitelist ) ); } return $results; } private function arrayMetaDataWhitelist( $array ) { return array_values( array_filter( $array, function( $v, $k ) { foreach ( $this->metaDataWhilelist as $whilelist ) { if ( strpos( $v->__get( 'key' ), $whilelist ) !== false ) { return true; } } return false; }, ARRAY_FILTER_USE_BOTH ) ); } /** * Get Home Data for caching * * @param object $request * * @return json */ public function get_home_data( $request ) { $lang = sanitize_text_field( $request['lang'] ); $homeCache = FlutterUtils::get_home_cache_path( $lang ); if ( $request['reset'] == 'false' && file_exists( $homeCache ) ) { $fileContent = file_get_contents( $homeCache ); return json_decode( $fileContent, true ); } $api = new WC_REST_Products_Controller(); $path = $this->get_config_file_path( $lang ); if ( is_wp_error( $path ) ) { return $path; } if ( file_exists( $path ) ) { $fileContent = file_get_contents( $path ); $array = json_decode( $fileContent, true ); //get products for horizontal layout $countDataLayout = 0; $results = []; $horizontalLayout = $array['HorizonLayout']; foreach ( $horizontalLayout as $layout ) { if ( ( isset( $layout['category'] ) || isset( $layout['tag'] ) ) && in_array( $layout['layout'], $this->supportedLayouts ) ) { if ( $countDataLayout < 4 ) { $layout['data'] = $this->getProductsByLayout( $layout, $api, $request ); $countDataLayout += 1; } $results[] = $layout; } else { if ( isset( $layout['items'] ) && count( $layout['items'] ) > 0 ) { $items = []; foreach ( $layout['items'] as $item ) { if ( $countDataLayout < 4 && array_key_exists( 'layout', $item ) && in_array( $item['layout'], $this->supportedLayouts ) ) { $item['data'] = $this->getProductsByLayout( $item, $api, $request ); $countDataLayout += 1; } $items[] = $item; } $layout['items'] = $items; } $results[] = $layout; } } $array['HorizonLayout'] = $results; //get products for vertical layout if ( isset( $array['VerticalLayout'] ) ) { $layout = $array['VerticalLayout']; if ( isset( $layout['category'] ) ) { if ( $countDataLayout < 4 ) { $layout['data'] = $this->getProductsByLayout( $layout, $api, $request ); $countDataLayout += 1; } $array['VerticalLayout'] = $layout; } } //save data to cache file file_put_contents( $homeCache, json_encode( $array ) ); return $array; } else { return new WP_Error( 'existed_config', "Config file hasn't been uploaded yet.", [ 'status' => 400 ] ); } } function getProductsByLayout( $layout, $api, $request ) { if ( ( ! isset( $layout['category'] ) && ! isset( $layout['tag'] ) ) || ( isset( $layout['category'] ) && ( $layout['category'] == null || $layout['category'] == '-1' ) ) || ( isset( $layout['tag'] ) && ( $layout['tag'] == null || $layout['tag'] == '-1' ) ) ) { return []; } $params = [ 'order' => 'desc', 'orderby' => 'date', 'status' => 'publish', ]; if ( isset( $layout['category'] ) ) { $params['category'] = $layout['category']; } if ( isset( $layout['tag'] ) ) { $params['tag'] = $layout['tag']; } if ( isset( $layout['feature'] ) ) { $params['feature'] = $layout['feature']; } if ( isset( $layout['layout'] ) && $layout['layout'] == 'saleOff' ) { $params['on_sale'] = 'true'; } $limit = get_option( 'maanstore_limit_product' ); $limit = ( ! isset( $limit ) || $limit == false ) ? 10 : $limit; $limit = isset( $layout['limit'] ) && is_int( $layout['limit'] ) ? $layout['limit'] : $limit; $params['per_page'] = $limit; $params['page'] = 0; $request->set_query_params( $params ); $response = $api->get_items( $request ); $products = $response->get_data(); $products = $this->arrayWhitelist( $products, $this->whilelist ); foreach ( $products as &$value ) { if ( isset( $value['meta_data'] ) ) { $value['meta_data'] = $this->arrayMetaDataWhitelist( $value['meta_data'] ); } } return $products; } /** * Get Category Data for caching * * @param object $request * * @return json */ public function get_category_data( $request ) { $api = new WC_REST_Products_Controller(); $ids = $request['categoryIds']; if ( isset( $ids ) ) { $ids = explode( ',', $ids ); } else { $ids = []; } if ( count( $ids ) > 0 ) { $results = []; foreach ( $ids as $id ) { $results[ $id ] = $this->getProductsByLayout( [ 'category' => $id ], $api, $request ); } return $results; } else { return new WP_Error( 'empty_ids', 'categoryIds is empty', [ 'status' => 400 ] ); } } public function get_widgets_data( $request ) { $api = new WC_REST_Products_Controller(); $path = $this->get_config_file_path( sanitize_text_field( $request['lang'] ) ); if ( is_wp_error( $path ) ) { return $path; } if ( file_exists( $path ) ) { $fileContent = file_get_contents( $path ); $array = json_decode( $fileContent, true ); if ( isset( $array['Widgets'] ) ) { $layout = $array['Widgets']; if ( isset( $layout['category'] ) || isset( $layout['tag'] ) ) { $layout['data'] = $this->getProductsByLayout( $layout, $api, $request ); } return $layout; } else { return new WP_Error( 'invalid_format', "The config file doesn't have 'Widgets' property", [ 'status' => 400 ] ); } } else { return new WP_Error( 'existed_config', "Config file hasn't been uploaded yet.", [ 'status' => 400 ] ); } } } new FlutterHome();