array( 'X-OM-API-Key' => (string) get_option( 'aieo_rooster_api_key', '' ), 'Accept' => 'application/json', ), 'timeout' => 8, 'sslverify' => (bool) apply_filters('aieo_portal_sslverify', true), ) ); if ( is_wp_error( $resp ) ) { return $resp; } $code = (int) wp_remote_retrieve_response_code( $resp ); $body = json_decode( (string) wp_remote_retrieve_body( $resp ), true ); if ( 200 !== $code || ! is_array( $body ) ) { return new WP_Error( 'aieo_portal_bad_response', sprintf( __( 'The Portal answered %d.', 'ai-eshop-optimizer' ), $code ), array( 'status' => $code ) ); } return $body; } public static function catalog() { $hit = get_transient( self::CATALOG_TRANSIENT ); if ( is_array( $hit ) ) { return $hit; } $body = self::fetch( 'one/reports/catalog' ); if ( is_wp_error( $body ) ) { return $body; } if ( empty( $body['definition_version'] ) || ! isset( $body['definitions'] ) ) { return new WP_Error( 'aieo_portal_bad_catalog', __( 'The Portal catalog is unreadable.', 'ai-eshop-optimizer' ) ); } set_transient( self::CATALOG_TRANSIENT, $body, HOUR_IN_SECONDS ); return $body; } public static function definition( $id ) { $id = sanitize_key( $id ); if ( '' === $id ) { return new WP_Error( 'aieo_portal_bad_id', __( 'No definition id given.', 'ai-eshop-optimizer' ) ); } $hit = get_transient( self::DEF_TRANSIENT . $id ); if ( is_array( $hit ) ) { return $hit; } if ( get_transient( self::MISS_TRANSIENT . $id ) ) { return new WP_Error( 'aieo_portal_miss_cached', __( 'The Portal has no such definition (cached answer).', 'ai-eshop-optimizer' ) ); } $body = self::fetch( 'one/reports/definition/' . rawurlencode( $id ) ); if ( is_wp_error( $body ) || empty( $body['definition']['id'] ) ) { set_transient( self::MISS_TRANSIENT . $id, 1, 15 * MINUTE_IN_SECONDS ); return is_wp_error( $body ) ? $body : new WP_Error( 'aieo_portal_bad_definition', __( 'The Portal definition is unreadable.', 'ai-eshop-optimizer' ) ); } set_transient( self::DEF_TRANSIENT . $id, $body, HOUR_IN_SECONDS ); return $body; } public static function diff_against_local() { $catalog = self::catalog(); if ( is_wp_error( $catalog ) ) { return $catalog; } $portal_ids = array(); foreach ( (array) $catalog['definitions'] as $row ) { if ( ! empty( $row['id'] ) ) { $portal_ids[] = (string) $row['id']; } } $local_ids = array_keys( AIEO_Report_Registry::all() ); sort( $portal_ids ); sort( $local_ids ); return array( 'portal_version' => (string) $catalog['definition_version'], 'local_ids' => $local_ids, 'portal_ids' => $portal_ids, 'portal_only' => array_values( array_diff( $portal_ids, $local_ids ) ), 'local_only' => array_values( array_diff( $local_ids, $portal_ids ) ), ); } }