sync = new Prodigy_Synchronization(); add_action( 'rest_api_init', array( $this, 'register_route_product' ) ); } /** * Register REST API routes */ public function register_route_product(): void { register_rest_route( get_prodigy_plugin_directory_name() . '/v1', self::API_IMPORT, array( 'methods' => 'POST', 'callback' => array( $this, 'create' ), 'permission_callback' => function ( WP_REST_Request $request ) { return $this->is_authorization( $request->get_header( 'x-Authorization' ) ); }, 'args' => array( 'validate_callback' => array( $this, 'validate_data' ), ), ) ); } /** * @param WP_REST_Request $request_params * * @return WP_REST_Response * @throws Prodigy_Demo_Content_Exception */ public function create( WP_REST_Request $request_params ): WP_REST_Response { $result = $this->sync->run_import_process( $request_params['data'] ); $response = array(); if ( ! $result ) { $response['success'] = false; $response['message'] = 'Start process failed!'; $response['code'] = \WP_Http::UNPROCESSABLE_ENTITY; } else { $this->cache->reset_catalog(); $response['success'] = true; $response['code'] = \WP_Http::OK; } return new WP_REST_Response( $response, $response['code'] ); } /** * @param array $types * * @return false */ public function validate_data( array $types ): bool { if ( ! empty( $types ) ) { foreach ( $types as $content ) { if ( ! isset( $content['id'] ) && empty( $content['id'] ) && filter_var( $content['id'], FILTER_VALIDATE_INT ) ) { return false; } if ( ! is_string( $content['type'] ) && $content['type'] === self::TYPE ) { return false; } if ( isset( $content['attributes'] ) && ! $this->validate_attributes( $content['attributes'] ) ) { return false; } } } return true; } /** * @param array $attributes * * @return false */ public function validate_attributes( array $attributes ): bool { foreach ( $attributes as $attribute ) { if ( ! isset( $attribute['entity-type'] ) && ! is_string( $attribute['entity-type'] ) ) { return false; } if ( ! is_string( $attribute['completed'] ) && ! is_bool( $attribute['completed'] ) ) { return false; } if ( isset( $attribute['total-count'] ) && ! is_integer( $attribute['total-count'] ) ) { return false; } if ( ! isset( $attribute['url'] ) && ! filter_var( $attribute['url'], FILTER_VALIDATE_URL ) ) { return false; } } return true; } }